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

FI-2318 Allow for form data at session create endpoint #429

Merged
merged 17 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion lib/inferno/apps/web/controllers/test_sessions/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class Create < Controller

def handle(req, res)
params = req.params.to_h
params.merge!(JSON.parse(req.body.string).symbolize_keys) unless req.body.string.blank?
if !req.body.string.blank? && !req.env['CONTENT_TYPE'].include?('multipart/form-data')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use present? instead of !blank?.

Why check that the content type isn't form data instead of checking whether the content type is json? Also, that isn't the only content type used by forms.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I've changed it to just check for json instead before attempting to parse as a json. I'm still learning in's and out's of request handling -- is there any form data that is non json that could need additional handling? My understanding is that Hanabi will handle most forms automatically.

params.merge!(JSON.parse(req.body.string).symbolize_keys)
end

session = repo.create(create_params(params))

Expand Down
4 changes: 4 additions & 0 deletions spec/request_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def post_json(path, data)
post path, data.to_json, 'CONTENT_TYPE' => 'application/json'
end

def post_form_data(path, data)
post path, data, 'CONTENT_TYPE' => 'multipart/form-data'
end

def parsed_body
JSON.parse(last_response.body)
end
Expand Down
12 changes: 12 additions & 0 deletions spec/requests/test_sessions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@
expect(persisted_session.suite_options).to eq(expected_options)
end
end

context 'with form data' do
it 'renders the test session json' do
post_form_data create_path, input

expect(last_response.status).to eq(200)

expect(parsed_body).to include(*response_fields)
expect(parsed_body['id']).to be_present
expect(parsed_body['test_suite_id']).to eq(test_suite_id)
end
end
end

context 'with invalid input' do
Expand Down
Loading