Skip to content

Commit a80f2fd

Browse files
committed
feat(pacts for verification): allow pending status information to be optionall included
1 parent 0d53909 commit a80f2fd

8 files changed

+48
-11
lines changed

lib/pact_broker/api/contracts/verifiable_pacts_json_query_schema.rb

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class VerifiablePactsJSONQuerySchema
1717
optional(:latest).filled(included_in?: [true, false])
1818
end
1919
end
20+
optional(:includePendingStatus).filled(included_in?: [true, false])
2021
end
2122

2223
def self.call(params)

lib/pact_broker/api/contracts/verifiable_pacts_query_schema.rb

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class VerifiablePactsQuerySchema
1616
optional(:latest).filled(included_in?: ["true", "false"])
1717
end
1818
end
19+
optional(:include_pending_status).filled(included_in?: ["true", "false"])
1920
end
2021

2122
def self.call(params)

lib/pact_broker/api/decorators/verifiable_pact_decorator.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ def initialize(verifiable_pact)
2121
end
2222

2323
property :verification_properties, as: :verificationProperties do
24-
property :pending
25-
property :pending_reason, as: :pendingReason, exec_context: :decorator
26-
property :inclusion_reason, as: :inclusionReason, exec_context: :decorator
24+
property :pending,
25+
if: ->(context) { context[:options][:user_options][:include_pending_status] }
26+
property :pending_reason, as: :pendingReason, exec_context: :decorator,
27+
if: ->(context) { context[:options][:user_options][:include_pending_status] }
28+
property :inclusion_reason, as: :inclusionReason, exec_context: :decorator
2729

2830
def inclusion_reason
2931
PactBroker::Pacts::VerifiablePactMessages.new(represented).inclusion_reason

lib/pact_broker/api/decorators/verifiable_pacts_query_decorator.rb

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class VerifiablePactsQueryDecorator < BaseDecorator
1919
}
2020
end
2121

22+
property :include_pending_status, default: true,
23+
setter: ->(fragment:, represented:, **) {
24+
represented.include_pending_status = (fragment == 'true' || fragment == true)
25+
}
26+
2227
def from_hash(hash)
2328
# This handles both the snakecase keys from the GET query and the camelcase JSON POST body
2429
super(hash&.snakecase_keys)

lib/pact_broker/api/resources/provider_pacts_for_verification.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
require 'pact_broker/api/contracts/verifiable_pacts_query_schema'
44
require 'pact_broker/api/decorators/verifiable_pacts_query_decorator'
55
require 'pact_broker/api/contracts/verifiable_pacts_json_query_schema'
6+
require 'pact_broker/hash_refinements'
67

78
module PactBroker
89
module Api
910
module Resources
1011
class ProviderPactsForVerification < ProviderPacts
12+
using PactBroker::HashRefinements
13+
1114
def allowed_methods
1215
["GET", "POST", "OPTIONS"]
1316
end
@@ -48,7 +51,6 @@ def to_json
4851
PactBroker::Api::Decorators::VerifiablePactsDecorator.new(pacts).to_json(to_json_options)
4952
end
5053

51-
5254
def query_schema
5355
if request.get?
5456
PactBroker::Api::Contracts::VerifiablePactsQuerySchema
@@ -70,6 +72,10 @@ def query
7072
end
7173
end
7274
end
75+
76+
def to_json_options
77+
super.deep_merge(user_options: { include_pending_status: parsed_query_params.include_pending_status })
78+
end
7379
end
7480
end
7581
end

spec/features/pending_pacts_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def publish_pact
2020
end
2121

2222
def get_pacts_for_verification
23-
get("/pacts/provider/Bar/for-verification", nil, request_headers)
23+
post("/pacts/provider/Bar/for-verification", { includePendingStatus: true }.to_json, request_headers)
2424
end
2525

2626
def pact_url_from(pacts_for_verification_response)

spec/lib/pact_broker/api/decorators/verifiable_pact_decorator_spec.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ module Decorators
3737
let(:pending_provider_tags) { %w[dev] }
3838
let(:consumer_tags) { %w[dev] }
3939
let(:json) { decorator.to_json(options) }
40-
let(:options) { { user_options: { base_url: 'http://example.org' } } }
40+
let(:options) { { user_options: { base_url: 'http://example.org', include_pending_status: include_pending_status } } }
41+
let(:include_pending_status) { true }
4142

4243
subject { JSON.parse(json) }
4344

@@ -49,6 +50,18 @@ module Decorators
4950
expect(decorator).to receive(:pact_version_url).with(pact, 'http://example.org')
5051
subject
5152
end
53+
54+
context "when include_pending_status is false" do
55+
let(:include_pending_status) { false }
56+
57+
it "does not include the pending flag" do
58+
expect(subject['verificationProperties']).to_not have_key('pending')
59+
end
60+
61+
it "does not include the pending reason" do
62+
expect(subject['verificationProperties']).to_not have_key('pendingReason')
63+
end
64+
end
5265
end
5366
end
5467
end

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ module Resources
1717
let(:query) do
1818
{
1919
provider_version_tags: ['master'],
20-
consumer_version_selectors: [ { tag: "dev", latest: "true" }]
20+
consumer_version_selectors: [ { tag: "dev", latest: "true" }],
21+
include_pending_status: false
2122
}
2223
end
2324

@@ -26,7 +27,10 @@ module Resources
2627
describe "GET" do
2728
it "finds the pacts for verification by the provider" do
2829
# Naughty not mocking out the query parsing...
29-
expect(PactBroker::Pacts::Service).to receive(:find_for_verification).with("Bar", ["master"], [OpenStruct.new(tag: "dev", latest: true)])
30+
expect(PactBroker::Pacts::Service).to receive(:find_for_verification).with(
31+
"Bar",
32+
["master"],
33+
[OpenStruct.new(tag: "dev", latest: true)])
3034
subject
3135
end
3236

@@ -47,7 +51,8 @@ module Resources
4751
let(:request_body) do
4852
{
4953
providerVersionTags: ['master'],
50-
consumerVersionSelectors: [ { tag: "dev", latest: true }]
54+
consumerVersionSelectors: [ { tag: "dev", latest: true }],
55+
includePendingStatus: true
5156
}
5257
end
5358

@@ -62,7 +67,10 @@ module Resources
6267

6368
it "finds the pacts for verification by the provider" do
6469
# Naughty not mocking out the query parsing...
65-
expect(PactBroker::Pacts::Service).to receive(:find_for_verification).with("Bar", ["master"], [OpenStruct.new(tag: "dev", latest: true)])
70+
expect(PactBroker::Pacts::Service).to receive(:find_for_verification).with(
71+
"Bar",
72+
["master"],
73+
[OpenStruct.new(tag: "dev", latest: true)])
6674
subject
6775
end
6876

@@ -79,9 +87,10 @@ module Resources
7987
end
8088
end
8189

82-
it "sets the correct resource title" do
90+
it "uses the correct options for the decorator" do
8391
expect(decorator).to receive(:to_json) do | options |
8492
expect(options[:user_options][:title]).to eq "Pacts to be verified by provider Bar"
93+
expect(options[:user_options][:include_pending_status]).to eq false
8594
end
8695
subject
8796
end

0 commit comments

Comments
 (0)