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

Respond with an error if request is invalid #800

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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
18 changes: 18 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,24 @@
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