Skip to content

Commit d69c8e6

Browse files
committed
feat(matrix badges): add badge for provider/tag and consumer/tag
1 parent 26c18d9 commit d69c8e6

File tree

8 files changed

+198
-1
lines changed

8 files changed

+198
-1
lines changed

lib/pact_broker/api.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ module PactBroker
6161

6262
# matrix
6363
add ['matrix', 'provider', :provider_name, 'consumer', :consumer_name], Api::Resources::MatrixForConsumerAndProvider, {resource_name: "matrix_consumer_provider"}
64+
add ['matrix', 'provider', :provider_name, 'latest', :provider_tag, 'consumer', :consumer_name, 'latest', :tag, 'badge'], Api::Resources::MatrixBadge, {resource_name: "matrix_tag_badge"}
6465
add ['matrix'], Api::Resources::Matrix, {resource_name: "matrix"}
6566

6667
add [], Api::Resources::Index, {resource_name: "index"}
@@ -73,5 +74,4 @@ module PactBroker
7374

7475
pact_api.adapter
7576
end
76-
7777
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'pact_broker/api/resources/badge'
2+
3+
module PactBroker
4+
module Api
5+
module Resources
6+
class MatrixBadge < Badge
7+
8+
private
9+
10+
def latest_verification
11+
@latest_verification ||= begin
12+
matrix_row = matrix_service.find_for_consumer_and_provider_with_tags(identifier_from_path)
13+
if matrix_row && matrix_row[:verification_id]
14+
verification_service.find_by_id(matrix_row[:verification_id])
15+
end
16+
end
17+
end
18+
end
19+
end
20+
end
21+
end

lib/pact_broker/matrix/service.rb

+20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ def find_for_consumer_and_provider params
1616
matrix_repository.find_for_consumer_and_provider params[:consumer_name], params[:provider_name]
1717
end
1818

19+
def find_for_consumer_and_provider_with_tags params
20+
consumer_criteria = {
21+
pacticipant_name: params[:consumer_name],
22+
tag: params[:tag],
23+
latest: true
24+
}
25+
provider_criteria = {
26+
pacticipant_name: params[:provider_name],
27+
tag: params[:provider_tag],
28+
latest: true
29+
}
30+
selectors = [consumer_criteria, provider_criteria]
31+
options = { latestby: 'cvpv' }
32+
if validate_selectors(selectors).empty?
33+
matrix_repository.find(selectors, options).first
34+
else
35+
nil
36+
end
37+
end
38+
1939
def find_compatible_pacticipant_versions criteria
2040
matrix_repository.find_compatible_pacticipant_versions criteria
2141
end

lib/pact_broker/verifications/service.rb

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def find params
3535
verification_repository.find(params.fetch(:consumer_name), params.fetch(:provider_name), params.fetch(:pact_version_sha), params.fetch(:verification_number))
3636
end
3737

38+
def find_by_id id
39+
PactBroker::Domain::Verification.find(id: id)
40+
end
41+
3842
def find_latest_verifications_for_consumer_version params
3943
verification_repository.find_latest_verifications_for_consumer_version params[:consumer_name], params[:consumer_version_number]
4044
end
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'webmock/rspec'
2+
3+
describe "get latest matrix badge with tags" do
4+
5+
before do
6+
PactBroker.configuration.enable_public_badge_access = true
7+
TestDataBuilder.new
8+
.create_consumer('consumer')
9+
.create_provider('provider')
10+
.create_consumer_version('1')
11+
.create_consumer_version_tag('prod')
12+
.create_pact
13+
.create_verification(provider_version: '4')
14+
.use_provider_version('4')
15+
.create_provider_version_tag('master')
16+
end
17+
18+
let!(:http_request) do
19+
stub_request(:get, /http/).to_return(:status => 200, :body => "<svg/>")
20+
end
21+
22+
let(:path) { "/matrix/provider/provider/latest/master/consumer/consumer/latest/prod/badge" }
23+
24+
# In the full app, the .svg extension is turned into an Accept header
25+
# by ConvertFileExtensionToAcceptHeader
26+
27+
subject { get path, nil, {'HTTP_ACCEPT' => "image/svg+xml"}; last_response }
28+
29+
it "returns a 200 status" do
30+
expect(subject.status).to eq 200
31+
end
32+
33+
it "returns an svg/xml response" do
34+
expect(subject.headers['Content-Type']).to include("image/svg+xml")
35+
end
36+
37+
it "returns an svg body" do
38+
expect(subject.body).to include "<svg/>"
39+
end
40+
end

spec/lib/pact_broker/api/resources/badge_spec.rb

+61
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'pact_broker/api/resources/badge'
22
require 'pact_broker/badges/service'
3+
require 'pact_broker/matrix/service'
34

45
module PactBroker
56
module Api
@@ -118,6 +119,66 @@ module Resources
118119
subject
119120
end
120121
end
122+
123+
context "when retrieving the badge for a matrix row by tag" do
124+
before do
125+
allow(PactBroker::Matrix::Service).to receive(:find_for_consumer_and_provider_with_tags).and_return(row)
126+
allow(PactBroker::Verifications::Service).to receive(:find_by_id).and_return(verification)
127+
end
128+
129+
let(:path) { "/matrix/provider/provider/latest/master/consumer/consumer/latest/prod/badge" }
130+
let(:row) { { verification_id: 1 } }
131+
132+
133+
it "looks up the matrix row" do
134+
expect(PactBroker::Matrix::Service).to receive(:find_for_consumer_and_provider_with_tags).with(
135+
hash_including(consumer_name: 'consumer',
136+
provider_name: 'provider',
137+
tag: 'prod',
138+
provider_tag: 'master'
139+
))
140+
subject
141+
end
142+
143+
context "when a matrix row is found" do
144+
context "when there is a verification_id" do
145+
it "looks up the verification" do
146+
expect(PactBroker::Verifications::Service).to receive(:find_by_id).with(1)
147+
subject
148+
end
149+
150+
it "returns the badge" do
151+
expect(subject.body).to eq "badge"
152+
end
153+
end
154+
155+
context "when there is not a verification_id" do
156+
let(:row) { {} }
157+
158+
it "does not look up the verification" do
159+
expect(PactBroker::Verifications::Service).to_not receive(:find_by_id)
160+
subject
161+
end
162+
163+
it "returns the badge" do
164+
expect(subject.body).to eq "badge"
165+
end
166+
end
167+
end
168+
169+
context "when a matrix row is not found" do
170+
let(:row) { nil }
171+
172+
it "does not look up the verification" do
173+
expect(PactBroker::Verifications::Service).to_not receive(:find_by_id)
174+
subject
175+
end
176+
177+
it "returns the badge" do
178+
expect(subject.body).to eq "badge"
179+
end
180+
end
181+
end
121182
end
122183
end
123184
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'pact_broker/api/resources/matrix_badge'
2+
3+
module PactBroker
4+
module Api
5+
module Resources
6+
describe MatrixBadge do
7+
# spec is in spec/lib/pact_broker/api/resources/badge_spec.rb#123 to avoid duplication
8+
end
9+
end
10+
end
11+
end

spec/lib/pact_broker/matrix/service_spec.rb

+40
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,46 @@ module Matrix
122122
end
123123
end
124124
end
125+
126+
describe "find_for_consumer_and_provider_with_tags integration test" do
127+
128+
let(:params) do
129+
{
130+
consumer_name: 'consumer',
131+
provider_name: 'provider',
132+
tag: 'prod',
133+
provider_tag: 'master'
134+
}
135+
end
136+
137+
subject { Service.find_for_consumer_and_provider_with_tags(params) }
138+
139+
context "when the specified row exists" do
140+
before do
141+
td.create_pact_with_hierarchy('consumer', '1', 'provider')
142+
.create_consumer_version_tag('prod')
143+
.create_verification(provider_version: '2')
144+
.use_provider_version('2')
145+
.create_provider_version_tag('master')
146+
.create_verification(provider_version: '3', number: 2)
147+
.create_consumer_version('2')
148+
.create_pact
149+
end
150+
151+
it "returns the row" do
152+
expect(subject[:consumer_name]).to eq 'consumer'
153+
expect(subject[:provider_name]).to eq 'provider'
154+
expect(subject[:consumer_version_number]).to eq '1'
155+
expect(subject[:provider_version_number]).to eq '2'
156+
end
157+
end
158+
159+
context "when the specified row does not exist" do
160+
it "returns nil" do
161+
expect(subject).to be nil
162+
end
163+
end
164+
end
125165
end
126166
end
127167
end

0 commit comments

Comments
 (0)