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

Checks that post params is not empty for update and create #369

Merged
merged 2 commits into from
Aug 23, 2018

Conversation

peichins
Copy link
Member

Fixes #366. Fixes #361.

If there are no params, determines whether the request body was empty or if it just was not parsed properly, and responds with approprtiate error code depending on the situation.
If the body was empty, includes an error link to the new action.
If the body was not empty, responds with unsupported_media_type.

…e and create

If there are no params, determines whether the request body was empty or if it just was not parsed properly, and responds with approprtiate error code depending on the situation.
If the body was empty, includes an error link to the new action.
If the body was not empty, responds with unsupported_media_type.
@peichins peichins requested a review from atruskie August 23, 2018 00:47
Copy link
Member

@atruskie atruskie left a comment

Choose a reason for hiding this comment

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

Great changes.

Can I get you to make those small tweaks and then we'll merge.

Please run the entire test suite to double check as the CI is broken 😕

# an empty body parsed as json will have the form {model => {}}
# json body with application/x-www-form-urlencoded content type will have the form {json_string => {}}
# json or form encoded with text/plain content type will have the form {}
if request.POST.values.all? { |val| val.nil? || (val.is_a?(Hash) && val.empty?) }
Copy link
Member

Choose a reason for hiding this comment

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

Simplify this predicate by using .blank?

# json or form encoded with text/plain content type will have the form {}
if request.POST.values.all? { |val| val.nil? || (val.is_a?(Hash) && val.empty?) }

if (request.body.string == "")
Copy link
Member

Choose a reason for hiding this comment

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

More idiomatic ruby would use .blank? or .empty?. Also I believe the parentheses are not needed here

message = "Failed to parse the request body. Ensure that it is formed correctly and matches the content-type (#{request.content_type})"
end
end
render_error(status, message,nil,'validate_contains_params', options)
Copy link
Member

Choose a reason for hiding this comment

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

spacing around commas

@env['HTTP_AUTHORIZATION'] = admin_token

@create_dataset_item_url = "/datasets/" + dataset.id.to_s + "/items"
@update_dataset_item_url = "/datasets/" + dataset_item.dataset_id.to_s + "/items/" + dataset_item.id.to_s
Copy link
Member

Choose a reason for hiding this comment

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

Can we use url_helpers here? Or any rails method for generating the route?
If not, if you use string interpolation (which should do the .to_s part for you).

@env['CONTENT_TYPE'] = "application/json"
params = nil
post @create_dataset_item_url, params, @env
expect(response).to have_http_status(400)
Copy link
Member

Choose a reason for hiding this comment

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

should this test have a new resource link as well (like the test below)?

put @update_dataset_item_url, params, @env
expect(response).to have_http_status(200)
end

Copy link
Member

Choose a reason for hiding this comment

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

the update route doesn't get the empty body (400 response) tests?

@atruskie atruskie merged commit 9be7764 into develop Aug 23, 2018
@atruskie atruskie deleted the fix-error-incorrect-content-type branch June 4, 2020 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants