Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint to skip validation in Cypress tests #669

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Loading