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

Return 422 when request is unprocessable by Publishing API #1028

Merged
merged 1 commit into from
Nov 27, 2024
Merged
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
2 changes: 2 additions & 0 deletions app/controllers/manuals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def update
end
rescue ActionController::UnknownFormat
render json: { status: "error", errors: "Invalid Accept header" }, status: :not_acceptable
rescue GdsApi::HTTPUnprocessableEntity => e
render json: { status: "error", errors: e }, status: :unprocessable_entity
rescue ValidationError
render json: { status: "error", errors: manual.errors.full_messages }, status: :unprocessable_entity
end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/sections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def update
end
rescue ActionController::UnknownFormat
render json: { status: "error", errors: "Invalid Accept header" }, status: :not_acceptable
rescue GdsApi::HTTPUnprocessableEntity => e
render json: { status: "error", errors: e }, status: :unprocessable_entity
rescue ValidationError
render json: { status: "error", errors: section.errors.full_messages }, status: :unprocessable_entity
end
Expand Down
9 changes: 3 additions & 6 deletions spec/requests/manual_sections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
describe "manual sections resource" do
include GdsApi::TestHelpers::PublishingApi
include LinksUpdateHelper
include PublishingApiHelper

let(:maximal_section_endpoint) do
"/hmrc-manuals/#{maximal_manual_slug}/sections/#{maximal_section_slug}"
Expand Down Expand Up @@ -65,12 +66,12 @@
expect(response.status).to eq(503)
end

it "handles some other error with the Publishing API" do
it "handles the Publishing API returning an unproccessable entity error" do
publishing_api_validation_error

put_json maximal_section_endpoint, maximal_section

expect(response.status).to eq(500)
expect(response.status).to eq(422)
end

it "returns the status code from the Publishing API response" do
Expand Down Expand Up @@ -103,8 +104,4 @@
def publishing_api_times_out
stub_request(:any, /#{GdsApi::TestHelpers::PublishingApi::PUBLISHING_API_V2_ENDPOINT}\/.*/).to_timeout
end

def publishing_api_validation_error
stub_request(:any, /#{GdsApi::TestHelpers::PublishingApi::PUBLISHING_API_V2_ENDPOINT}\/.*/).to_return(status: 422)
end
end
9 changes: 9 additions & 0 deletions spec/requests/manuals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
describe "manuals resource" do
include GdsApi::TestHelpers::PublishingApi
include LinksUpdateHelper
include PublishingApiHelper

it "confirms update of the manual" do
stub_any_publishing_api_call
Expand Down Expand Up @@ -37,6 +38,14 @@
expect(response.status).to eq(500)
end

it "handles the Publishing API returning an unproccessable entity error" do
publishing_api_validation_error

put_json "/hmrc-manuals/#{maximal_manual_slug}", maximal_manual

expect(response.status).to eq(422)
end

it "returns the status code from the Publishing API response" do
stub_any_publishing_api_call
stub_any_publishing_api_put_content.to_return(body: { version: nil }.to_json)
Expand Down
5 changes: 5 additions & 0 deletions spec/support/publishing_api_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module PublishingApiHelper
def publishing_api_validation_error
stub_request(:any, /#{GdsApi::TestHelpers::PublishingApi::PUBLISHING_API_V2_ENDPOINT}\/.*/).to_return(status: 422)
end
end