Skip to content

Commit

Permalink
fix(verifications): gracefully handle a verification number in the UR…
Browse files Browse the repository at this point in the history
…L that is not an integer
  • Loading branch information
bethesque committed Apr 11, 2021
1 parent 34c7098 commit 7fe98a7
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
12 changes: 11 additions & 1 deletion lib/pact_broker/api/resources/verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ def allowed_methods
end

def resource_exists?
if identifier_from_path[:verification_number] == "all"
if verification_number == "all"
set_json_error_message("To see all the verifications for a pact, use the Matrix page")
false
elsif !verification_number_is_integer?
false
else
!!verification
end
Expand Down Expand Up @@ -66,6 +68,14 @@ def decorator_for model
def extended_decorator_for model
decorator_class(:extended_verification_decorator).new(model)
end

def verification_number
identifier_from_path[:verification_number]
end

def verification_number_is_integer?
verification_number =~ /^\d+$/
end
end
end
end
Expand Down
83 changes: 76 additions & 7 deletions spec/lib/pact_broker/api/resources/verification_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,84 @@
require 'pact_broker/api/resources/verification'

module PactBroker
module Api
module Resources
describe Verification do
context "when someone tries to get all the verifications for a pact" do
subject { get("/pacts/provider/Bar/consumer/Foo/pact-version/1/verification-results/all") }
before do
allow_any_instance_of(described_class).to receive(:verification_service).and_return(verification_service)
allow(verification_service).to receive(:find).and_return(verification)
allow(PactBroker::Api::Decorators::VerificationDecorator).to receive(:new).and_return(decorator)
end

let(:verification) { instance_double("PactBroker::Domain::Verification") }
let(:parsed_verification) { double("parsed verification") }
let(:verification_service) { class_double("PactBroker::Verifications::Service").as_stubbed_const }
let(:path) { "/pacts/provider/Bar/consumer/Foo/pact-version/1/verification-results/2" }

let(:rack_headers) do
{
"HTTP_ACCEPT" => "application/hal+json"
}
end

let(:decorator) do
instance_double("PactBroker::Api::Decorators::VerificationDecorator",
to_json: "response",
from_json: parsed_verification
)
end

describe "GET" do
let(:identifier_params) { { consumer_name: "Foo", provider_name: "Bar", pact_version_sha: "1", verification_number: "2" } }

subject { get(path, nil, rack_headers) }

it "finds the Verification" do
expect(PactBroker::Verifications::Service).to receive(:find).with(hash_including(identifier_params))
subject
end

context "when the verification does not exist" do
let(:verification) { nil }

it { is_expected.to be_a_404_response }
end

context "when someone tries to get all the verifications for a pact" do
let(:path) { "/pacts/provider/Bar/consumer/Foo/pact-version/1/verification-results/all" }

it "does not attempt to find the verification" do
expect(PactBroker::Verifications::Service).to_not receive(:find)
subject
end

it "tells them to use the matrix" do
expect(subject.status).to eq 404
expect(subject.body).to include "Matrix"
end
end

context "when the verification number specified is not a number" do
let(:path) { "/pacts/provider/Bar/consumer/Foo/pact-version/1/verification-results/5*5" }

it "does not attempt to find the verification" do
expect(PactBroker::Verifications::Service).to_not receive(:find)
subject
end

its(:status) { is_expected.to eq 404 }
end

context "when the Verification exists" do
it "generates a JSON representation of the Verification" do
expect(PactBroker::Api::Decorators::VerificationDecorator).to receive(:new).with(verification)
expect(decorator).to receive(:to_json).with(user_options: hash_including(base_url: "http://example.org"))
subject
end

it { is_expected.to be_a_hal_json_success_response }

it "tells them to use the matrix" do
expect(subject.status).to eq 404
expect(subject.body).to include "Matrix"
it "includes the JSON representation in the response body" do
expect(subject.body).to eq "response"
end
end
end
end
Expand Down

0 comments on commit 7fe98a7

Please sign in to comment.