Skip to content

Commit

Permalink
fix: gracefully handle fetching matrix badge when specified pact does…
Browse files Browse the repository at this point in the history
… not exist
  • Loading branch information
bethesque committed Apr 30, 2020
1 parent 5363ab2 commit e8ec410
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 20 deletions.
1 change: 1 addition & 0 deletions lib/pact_broker/api/resources/matrix_badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
6 changes: 6 additions & 0 deletions lib/pact_broker/pacticipants/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/pacticipants/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/verifications/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions spec/features/get_matrix_badge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@
it "returns an svg body" do
expect(subject.body).to include "<svg/>"
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
56 changes: 38 additions & 18 deletions spec/lib/pact_broker/api/resources/badge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/pact_broker/verifications/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e8ec410

Please sign in to comment.