Skip to content

Commit 2da3657

Browse files
committed
fix: gracefully handle badge redirects when the pact is not found
1 parent b697cf6 commit 2da3657

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

lib/pact_broker/badges/service.rb

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def clear_cache
3939
private
4040

4141
def badge_title pact, label, initials
42+
return 'pact not found' if pact.nil?
4243
title = case (label || '').downcase
4344
when 'consumer' then prepare_name(pact.consumer_name, initials)
4445
when 'provider' then prepare_name(pact.provider_name, initials)

spec/lib/pact_broker/badges/service_spec.rb

+36-21
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,46 @@
33

44
module PactBroker
55
module Badges
6-
module Service
7-
describe "#pact_verification_badge" do
8-
let(:pacticipant_name) { "Foo-Bar_Thing Service" }
9-
let(:pact) { double("pact", consumer_name: "Foo-Bar", provider_name: "Thing_Blah") }
10-
let(:label) { nil }
11-
let(:initials) { false }
12-
let(:pseudo_branch_verification_status) { :success }
13-
let(:logger) { double('logger').as_null_object }
14-
let(:expected_url) { "https://img.shields.io/badge/#{expected_left_text}-#{expected_right_text}-#{expected_color}.svg" }
15-
let(:expected_color) { "brightgreen" }
16-
let(:expected_right_text) { "verified" }
17-
let(:expected_left_text) { "foo--bar%2fthing__blah%20pact" }
18-
let(:response_status) { 200 }
19-
let!(:http_request) do
20-
stub_request(:get, expected_url).to_return(:status => response_status, :body => "svg")
21-
end
6+
describe Service do
7+
let(:pacticipant_name) { "Foo-Bar_Thing Service" }
8+
let(:pact) { double("pact", consumer_name: "Foo-Bar", provider_name: "Thing_Blah") }
9+
let(:label) { nil }
10+
let(:initials) { false }
11+
let(:pseudo_branch_verification_status) { :success }
12+
let(:logger) { double('logger').as_null_object }
13+
let(:expected_url) { "https://img.shields.io/badge/#{expected_left_text}-#{expected_right_text}-#{expected_color}.svg" }
14+
let(:expected_color) { "brightgreen" }
15+
let(:expected_right_text) { "verified" }
16+
let(:expected_left_text) { "foo--bar%2fthing__blah%20pact" }
17+
let(:response_status) { 200 }
18+
let!(:http_request) do
19+
stub_request(:get, expected_url).to_return(:status => response_status, :body => "svg")
20+
end
21+
22+
subject { PactBroker::Badges::Service.pact_verification_badge pact, label, initials, pseudo_branch_verification_status }
2223

23-
subject { PactBroker::Badges::Service.pact_verification_badge pact, label, initials, pseudo_branch_verification_status }
24+
let(:pact_verification_badge_url) { PactBroker::Badges::Service.pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status) }
2425

25-
let(:pact_verification_badge_url) { PactBroker::Badges::Service.pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status) }
26+
before do
27+
Service.clear_cache
28+
allow(Service).to receive(:logger).and_return(logger)
29+
end
2630

27-
before do
28-
Service.clear_cache
29-
allow(Service).to receive(:logger).and_return(logger)
31+
describe "pact_verification_badge_url" do
32+
context "with the pact is nil" do
33+
let(:pact) { nil }
34+
let(:expected_left_text) { "pact%20not%20found" }
35+
let(:expected_right_text) { "unknown" }
36+
let(:expected_color) { "lightgrey" }
37+
let(:pseudo_branch_verification_status) { :never }
38+
39+
it "returns a link to a 'pact not found' badge" do
40+
expect(pact_verification_badge_url).to eq URI(expected_url)
41+
end
3042
end
43+
end
44+
45+
describe "#pact_verification_badge" do
3146

3247
it "returns the svg file" do
3348
expect(subject).to eq "svg"

0 commit comments

Comments
 (0)