Skip to content

Commit

Permalink
Add endpoint to skip validation in Cypress tests (#669)
Browse files Browse the repository at this point in the history
* Expose MaMpf to another port to avoid clashing with dev

* Add an endpoint to skip validation when creating factory
  • Loading branch information
Splines authored Aug 12, 2024
1 parent 742ee15 commit 4a0e103
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
22 changes: 18 additions & 4 deletions app/controllers/cypress/factories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,38 @@ def create
raise(ArgumentError, msg)
end

attributes = params_to_attributes(params.except(:controller, :action, :number))
res = FactoryBot.create(*attributes)
attributes, should_validate = params_to_attributes(params.except(:controller, :action,
:number))

res = if should_validate
FactoryBot.create(*attributes) # default case
else
FactoryBot.build(*attributes).tap { |instance| instance.save(validate: false) }
end

render json: res.to_json, status: :created
end

private

def params_to_attributes(params)
params.to_unsafe_hash.map do |_key, value|
should_validate = true

attributes = params.to_unsafe_hash.filter_map do |_key, value|
if value.is_a?(Hash)
value.transform_keys(&:to_sym)
if value.key?("validate")
should_validate = (value["validate"] != "false")
else
value.transform_keys(&:to_sym)
end
elsif value.is_a?(String)
value.to_sym
else
throw("Value is neither a hash nor a string: #{value}")
end
end

return attributes, should_validate
end
end
end
2 changes: 1 addition & 1 deletion docker/test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ services:
dockerfile: docker/test/Dockerfile
image: mampf:tests
ports:
- "3000:3000"
- "3100:3000"
environment:
RAILS_ENV: test
TEST_DATABASE_ADAPTER: postgresql
Expand Down
5 changes: 5 additions & 0 deletions spec/cypress/support/factorybot.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class FactoryBot {
create(...args) {
return BackendCaller.callCypressRoute("factories", "FactoryBot.create()", args);
}

createNoValidate(...args) {
args.push({ validate: false });
return this.create(...args);
}
}

export default new FactoryBot();

0 comments on commit 4a0e103

Please sign in to comment.