diff --git a/lib/pact_broker/api/resources/matrix_badge.rb b/lib/pact_broker/api/resources/matrix_badge.rb index 6ff027631..daa78e78d 100644 --- a/lib/pact_broker/api/resources/matrix_badge.rb +++ b/lib/pact_broker/api/resources/matrix_badge.rb @@ -8,6 +8,7 @@ class MatrixBadge < Badge private def latest_verification + return nil unless pact @latest_verification ||= verification_service.find_latest_verification_for_tags( identifier_from_path[:consumer_name], identifier_from_path[:provider_name], diff --git a/lib/pact_broker/pacticipants/repository.rb b/lib/pact_broker/pacticipants/repository.rb index 32583f21a..629167161 100644 --- a/lib/pact_broker/pacticipants/repository.rb +++ b/lib/pact_broker/pacticipants/repository.rb @@ -15,6 +15,12 @@ def find_by_name name pacticipants.first end + def find_by_name! name + pacticipant = find_by_name(name) + raise PactBroker::Error, "No pacticipant found with name '#{name}'" unless pacticipant + pacticipant + end + def find_by_id id PactBroker::Domain::Pacticipant.where(id: id).single_record end diff --git a/lib/pact_broker/pacticipants/service.rb b/lib/pact_broker/pacticipants/service.rb index f11435a7d..3b689c8cd 100644 --- a/lib/pact_broker/pacticipants/service.rb +++ b/lib/pact_broker/pacticipants/service.rb @@ -40,6 +40,10 @@ def self.find_pacticipant_by_name name pacticipant_repository.find_by_name(name) end + def self.find_pacticipant_by_name! name + pacticipant_repository.find_by_name!(name) + end + def self.find_by_id id pacticipant_repository.find_by_id(id) end diff --git a/lib/pact_broker/verifications/repository.rb b/lib/pact_broker/verifications/repository.rb index 5ba157c56..4d35c4aa1 100644 --- a/lib/pact_broker/verifications/repository.rb +++ b/lib/pact_broker/verifications/repository.rb @@ -107,8 +107,8 @@ def find_latest_verification_for consumer_name, provider_name, consumer_version_ def find_latest_verification_for_tags consumer_name, provider_name, consumer_version_tag, provider_version_tag view_name = PactBroker::Domain::Verification.table_name - consumer = pacticipant_repository.find_by_name(consumer_name) - provider = pacticipant_repository.find_by_name(provider_name) + consumer = pacticipant_repository.find_by_name!(consumer_name) + provider = pacticipant_repository.find_by_name!(provider_name) consumer_tag_filter = PactBroker::Repositories::Helpers.name_like(Sequel.qualify(:consumer_tags, :name), consumer_version_tag) provider_tag_filter = PactBroker::Repositories::Helpers.name_like(Sequel.qualify(:provider_tags, :name), provider_version_tag) diff --git a/spec/features/get_matrix_badge_spec.rb b/spec/features/get_matrix_badge_spec.rb index 7b39cd5b6..0c6806204 100644 --- a/spec/features/get_matrix_badge_spec.rb +++ b/spec/features/get_matrix_badge_spec.rb @@ -37,4 +37,13 @@ it "returns an svg body" do expect(subject.body).to include "" end + + context "when one of the pacticipants does not exist" do + let(:path) { "/matrix/provider/provider/latest/master/consumer/consumer2/latest/prod/badge" } + + it "returns a 200 status as the badge content indicated the pact is not found" do + expect(subject.status).to eq 200 + expect(subject.body).to include "not found" + end + end end diff --git a/spec/lib/pact_broker/api/resources/badge_spec.rb b/spec/lib/pact_broker/api/resources/badge_spec.rb index 621502c6f..9741bed90 100644 --- a/spec/lib/pact_broker/api/resources/badge_spec.rb +++ b/spec/lib/pact_broker/api/resources/badge_spec.rb @@ -63,6 +63,15 @@ module Resources subject end + context "when the pact is not found" do + let(:pact) { nil } + + it "does not retrieve the latest verification" do + expect(PactBroker::Verifications::Service).to_not receive(:find_latest_verification_for) + subject + end + end + it "determines the pact's verification status based on the latest pact and latest verification" do expect(PactBroker::Verifications::PseudoBranchStatus).to receive(:new).with(pact, verification) subject @@ -156,32 +165,43 @@ module Resources let(:path) { "/matrix/provider/provider/latest/master/consumer/consumer/latest/prod/badge" } let(:row) { { consumer_name: 'consumer', provider_name: 'provider' } } - it "looks up the verification" do - expect(PactBroker::Verifications::Service).to receive(:find_latest_verification_for_tags) do | consumer, provider, tag| - expect(consumer.name).to eq 'consumer' - expect(provider.name).to eq 'provider' - expect(tag).to eq 'prod' + context "when a pact is found" do + it "looks up the verification" do + expect(PactBroker::Verifications::Service).to receive(:find_latest_verification_for_tags) do | consumer, provider, tag| + expect(consumer.name).to eq 'consumer' + expect(provider.name).to eq 'provider' + expect(tag).to eq 'prod' + end + subject end - subject - end - context "when a verification is found" do - before do - allow(PactBroker::Verifications::Service).to receive(:find_latest_verification_for_tags).and_return(verification) + context "when a verification is found" do + before do + allow(PactBroker::Verifications::Service).to receive(:find_latest_verification_for_tags).and_return(verification) + end + + it "returns the badge" do + expect(subject.body).to end_with "badge" + end end - it "returns the badge" do - expect(subject.body).to end_with "badge" + context "when a verification is not found" do + before do + allow(PactBroker::Verifications::Service).to receive(:find_latest_verification_for_tags).and_return(nil) + end + + it "returns the badge" do + expect(subject.body).to end_with "badge" + end end end - context "when a verification is not found" do - before do - allow(PactBroker::Verifications::Service).to receive(:find_latest_verification_for_tags).and_return(nil) - end + context "when a pact is not found" do + let(:pact) { nil } - it "returns the badge" do - expect(subject.body).to end_with "badge" + it "does not look up the verification" do + expect(PactBroker::Verifications::Service).to_not receive(:find_latest_verification_for_tags) + subject end end end diff --git a/spec/lib/pact_broker/verifications/repository_spec.rb b/spec/lib/pact_broker/verifications/repository_spec.rb index 3928fb443..1211d88b2 100644 --- a/spec/lib/pact_broker/verifications/repository_spec.rb +++ b/spec/lib/pact_broker/verifications/repository_spec.rb @@ -295,6 +295,26 @@ module Verifications expect(subject.number).to eq 2 end end + + context "when the consumer does not exist" do + subject { Repository.new.find_latest_verification_for_tags("Foo", "Bar", "feat-x", "feat-y") } + + it "raises an error" do + expect{ subject }.to raise_error PactBroker::Error, /Foo/ + end + end + + context "when the provider does not exist" do + before do + td.create_consumer("Foo") + end + + subject { Repository.new.find_latest_verification_for_tags("Foo", "Bar", "feat-x", "feat-y") } + + it "raises an error" do + expect{ subject }.to raise_error PactBroker::Error, /Bar/ + end + end end describe "delete_by_provider_version_id" do