Skip to content

Commit

Permalink
Merge db5e5a1 into f7f4bef
Browse files Browse the repository at this point in the history
  • Loading branch information
ndushay committed Nov 15, 2019
2 parents f7f4bef + db5e5a1 commit 5f3091d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
14 changes: 9 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ def strip_druid(id)
id&.split(':', 2)&.last
end

def bad_request
render plain: '400 bad request', status: :bad_request
def bad_request(exception)
msg = '400 bad request'
msg = "#{msg}: #{exception.message}" if exception
render plain: msg, status: :bad_request
end

def not_found
render plain: '404 Not Found', status: :not_found
def not_found(exception)
msg = '404 Not Found'
msg = "#{msg}: #{exception.message}" if exception
render plain: msg, status: :not_found
end

# TODO: get rid of this once https://github.com/sul-dlss/moab-versioning/issues/159 is implemented
def refine_invalid_druid_error!(err)
# make a specific moab-versioning StandardError into something more easily manageable by ApplicationController...
raise InvalidSuriSyntax, err.message if err.message.include?('Identifier has invalid suri syntax')
raise # ...but just re-raise what we got if it was something else
raise err # re-raise what we got if it was something else
end
end
4 changes: 2 additions & 2 deletions app/controllers/objects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def checksum

# return the checksums and filesize for a list of druid (supplied with druid: prefix)
# note: this is deliberately allowed to be a POST to allow for a large number of druids to be passed in
# GET OR POST /objects/checksums?druids[]=druida&druids[]=druidb&druids[]=druidc
# GET OR POST /objects/checksums?druids[]=druid1&druids[]=druid2&druids[]=druid3
def checksums
unless druids.present?
render(plain: '400 bad request', status: :bad_request)
render(plain: "400 bad request - druids param must be populated", status: :bad_request)
return
end

Expand Down
24 changes: 23 additions & 1 deletion spec/requests/objects_controller_checksums_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,34 @@
post checksums_objects_url, params: { druids: [prefixed_druid, 'druid:xx123yy9999'], format: :json }
expect(response).to have_http_status(:not_found)
end
it 'body has additional information from the exception if available' do
post checksums_objects_url, params: { druids: [prefixed_druid, 'druid:xx123yy9999'], format: :json }
expect(response.body).to eq '404 Not Found: No storage object found for xx123yy9999'
end
end

context 'when no druids param provided' do
context 'when druids param is empty' do
it 'body has additional information from the exception if available' do
post checksums_objects_url, params: { druids: [], format: :json }
expect(response).to have_http_status(:bad_request)
expect(response.body).to eq '400 bad request: Identifier has invalid suri syntax: nil or empty'
end
end
context "when no druids param" do
it 'body has additional information from the exception if available' do
post checksums_objects_url, params: { format: :json }
expect(response).to have_http_status(:bad_request)
expect(response.body).to eq '400 bad request - druids param must be populated'
end
end
end

context 'when bad parameter passed in' do
it 'returns a 400 response code with a bad druid passed in' do
post checksums_objects_url, params: { druids: [prefixed_druid, 'not a druid'], format: :json }
post checksums_objects_url, params: { druids: [prefixed_druid, 'foobar'], format: :json }
expect(response).to have_http_status(:bad_request)
expect(response.body).to eq '400 bad request: Identifier has invalid suri syntax: foobar'
end

it 'returns a 406 response code when an unsupported response format is provided' do
Expand Down
20 changes: 10 additions & 10 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups

# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = "doc"
end

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
Expand All @@ -58,16 +68,6 @@
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = "doc"
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
Expand Down

0 comments on commit 5f3091d

Please sign in to comment.