Skip to content

Commit

Permalink
validate nil attachment POST param (#3721)
Browse files Browse the repository at this point in the history
  • Loading branch information
omgitsbillryan authored Jan 7, 2020
1 parent 178eb2d commit a7a671c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
5 changes: 4 additions & 1 deletion app/controllers/concerns/form_attachment_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def create
form_attachment_model = self.class::FORM_ATTACHMENT_MODEL
form_attachment = form_attachment_model.new
namespace = form_attachment_model.to_s.underscore.split('/').last
form_attachment.set_file_data!(params[namespace][:file_data])

file_data = params.require(namespace).permit(:file_data)

form_attachment.set_file_data!(file_data)
form_attachment.save!
render(json: form_attachment)
end
Expand Down
7 changes: 7 additions & 0 deletions app/swagger/requests/hca_attachments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class HcaAttachments
end
end
end

response 400 do
key :description, 'Bad Gateway: incorrect parameters'
schema do
key :'$ref', :Errors
end
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/swagger/requests/upload_supporting_evidence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class UploadSupportingEvidence
end
end

response 500 do
response 400 do
key :description, 'Bad Gateway: incorrect parameters'
schema do
key :'$ref', :Errors
Expand Down
7 changes: 7 additions & 0 deletions spec/controllers/v0/hca_attachments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,12 @@

expect(JSON.parse(response.body)['data']['attributes']['guid']).to eq HcaAttachment.last.guid
end

it 'validates input parameters' do
post(:create)
expect(response).to have_http_status(:bad_request)
expect(JSON.parse(response.body)['errors'].first['detail'])
.to eq('The required parameter "hca_attachment", is missing')
end
end
end
8 changes: 6 additions & 2 deletions spec/request/swagger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@
)
end

it 'returns a 400 if no attachment data is given' do
expect(subject).to validate(:post, '/v0/hca_attachments', 400, '')
end

it 'supports submitting a health care application', run_at: '2017-01-31' do
VCR.use_cassette('hca/submit_anon', match_requests_on: [:body]) do
expect(subject).to validate(
Expand Down Expand Up @@ -568,8 +572,8 @@
)
end

it 'returns a 500 if no attachment data is given' do
expect(subject).to validate(:post, '/v0/upload_supporting_evidence', 500, '')
it 'returns a 400 if no attachment data is given' do
expect(subject).to validate(:post, '/v0/upload_supporting_evidence', 400, '')
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/request/upload_supporting_evidence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
context 'with invalid parameters' do
it 'returns a 500 with no parameters' do
post '/v0/upload_supporting_evidence', params: nil
expect(response).to have_http_status(:internal_server_error)
expect(response).to have_http_status(:bad_request)
end

it 'returns a 500 with no file_data' do
post '/v0/upload_supporting_evidence', params: JSON.parse('{"supporting_evidence_attachment": {}}')
expect(response).to have_http_status(:internal_server_error)
expect(response).to have_http_status(:bad_request)
end
end
end
Expand Down

0 comments on commit a7a671c

Please sign in to comment.