Skip to content

Commit

Permalink
Respond with an error if request is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jun 17, 2024
1 parent de14e37 commit d680090
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
16 changes: 12 additions & 4 deletions app/controllers/v1/purls_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ def update
retry
end

PurlCocinaUpdater.update(@purl, cocina_object)
write_public_files

render json: true, status: :accepted
begin
PurlCocinaUpdater.update(@purl, cocina_object)
write_public_files

render json: true, status: :accepted
rescue Cocina::Models::ValidationError => e
render json: {
errors: [
{ title: 'bad request', detail: e.message }
]
}, status: :bad_request
end
end

def destroy
Expand Down
17 changes: 17 additions & 0 deletions spec/requests/v1/purls_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@
end
end

context 'with invalid cocina json' do
let(:data) { { invalid: true}.to_json }
before do
allow(PurlCocinaUpdater).to receive(:update)
end

context 'with a new item' do
it 'creates a new purl entry' do
expect do
post "/purls/#{druid}", params: data, headers:
end.to change(Purl, :count).by(1)
expect(response).to have_http_status(:bad_request)
expect(PurlCocinaUpdater).not_to have_received(:update)
end
end
end

context 'when no authorization token is provided' do
it 'returns 401' do
post "/purls/#{druid}", params: data, headers: headers.except('Authorization')
Expand Down

0 comments on commit d680090

Please sign in to comment.