Skip to content

Commit

Permalink
fix: gracefully handle badge redirects when the pact is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Feb 27, 2020
1 parent b697cf6 commit 2da3657
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
1 change: 1 addition & 0 deletions lib/pact_broker/badges/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def clear_cache
private

def badge_title pact, label, initials
return 'pact not found' if pact.nil?
title = case (label || '').downcase
when 'consumer' then prepare_name(pact.consumer_name, initials)
when 'provider' then prepare_name(pact.provider_name, initials)
Expand Down
57 changes: 36 additions & 21 deletions spec/lib/pact_broker/badges/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,46 @@

module PactBroker
module Badges
module Service
describe "#pact_verification_badge" do
let(:pacticipant_name) { "Foo-Bar_Thing Service" }
let(:pact) { double("pact", consumer_name: "Foo-Bar", provider_name: "Thing_Blah") }
let(:label) { nil }
let(:initials) { false }
let(:pseudo_branch_verification_status) { :success }
let(:logger) { double('logger').as_null_object }
let(:expected_url) { "https://img.shields.io/badge/#{expected_left_text}-#{expected_right_text}-#{expected_color}.svg" }
let(:expected_color) { "brightgreen" }
let(:expected_right_text) { "verified" }
let(:expected_left_text) { "foo--bar%2fthing__blah%20pact" }
let(:response_status) { 200 }
let!(:http_request) do
stub_request(:get, expected_url).to_return(:status => response_status, :body => "svg")
end
describe Service do
let(:pacticipant_name) { "Foo-Bar_Thing Service" }
let(:pact) { double("pact", consumer_name: "Foo-Bar", provider_name: "Thing_Blah") }
let(:label) { nil }
let(:initials) { false }
let(:pseudo_branch_verification_status) { :success }
let(:logger) { double('logger').as_null_object }
let(:expected_url) { "https://img.shields.io/badge/#{expected_left_text}-#{expected_right_text}-#{expected_color}.svg" }
let(:expected_color) { "brightgreen" }
let(:expected_right_text) { "verified" }
let(:expected_left_text) { "foo--bar%2fthing__blah%20pact" }
let(:response_status) { 200 }
let!(:http_request) do
stub_request(:get, expected_url).to_return(:status => response_status, :body => "svg")
end

subject { PactBroker::Badges::Service.pact_verification_badge pact, label, initials, pseudo_branch_verification_status }

subject { PactBroker::Badges::Service.pact_verification_badge pact, label, initials, pseudo_branch_verification_status }
let(:pact_verification_badge_url) { PactBroker::Badges::Service.pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status) }

let(:pact_verification_badge_url) { PactBroker::Badges::Service.pact_verification_badge_url(pact, label, initials, pseudo_branch_verification_status) }
before do
Service.clear_cache
allow(Service).to receive(:logger).and_return(logger)
end

before do
Service.clear_cache
allow(Service).to receive(:logger).and_return(logger)
describe "pact_verification_badge_url" do
context "with the pact is nil" do
let(:pact) { nil }
let(:expected_left_text) { "pact%20not%20found" }
let(:expected_right_text) { "unknown" }
let(:expected_color) { "lightgrey" }
let(:pseudo_branch_verification_status) { :never }

it "returns a link to a 'pact not found' badge" do
expect(pact_verification_badge_url).to eq URI(expected_url)
end
end
end

describe "#pact_verification_badge" do

it "returns the svg file" do
expect(subject).to eq "svg"
Expand Down

0 comments on commit 2da3657

Please sign in to comment.