Skip to content

Commit

Permalink
feat(pacts for verification): allow pending status information to be …
Browse files Browse the repository at this point in the history
…optionall included
  • Loading branch information
bethesque committed Nov 21, 2019
1 parent 0d53909 commit a80f2fd
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class VerifiablePactsJSONQuerySchema
optional(:latest).filled(included_in?: [true, false])
end
end
optional(:includePendingStatus).filled(included_in?: [true, false])
end

def self.call(params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class VerifiablePactsQuerySchema
optional(:latest).filled(included_in?: ["true", "false"])
end
end
optional(:include_pending_status).filled(included_in?: ["true", "false"])
end

def self.call(params)
Expand Down
8 changes: 5 additions & 3 deletions lib/pact_broker/api/decorators/verifiable_pact_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ def initialize(verifiable_pact)
end

property :verification_properties, as: :verificationProperties do
property :pending
property :pending_reason, as: :pendingReason, exec_context: :decorator
property :inclusion_reason, as: :inclusionReason, exec_context: :decorator
property :pending,
if: ->(context) { context[:options][:user_options][:include_pending_status] }
property :pending_reason, as: :pendingReason, exec_context: :decorator,
if: ->(context) { context[:options][:user_options][:include_pending_status] }
property :inclusion_reason, as: :inclusionReason, exec_context: :decorator

def inclusion_reason
PactBroker::Pacts::VerifiablePactMessages.new(represented).inclusion_reason
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class VerifiablePactsQueryDecorator < BaseDecorator
}
end

property :include_pending_status, default: true,
setter: ->(fragment:, represented:, **) {
represented.include_pending_status = (fragment == 'true' || fragment == true)
}

def from_hash(hash)
# This handles both the snakecase keys from the GET query and the camelcase JSON POST body
super(hash&.snakecase_keys)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
require 'pact_broker/api/contracts/verifiable_pacts_query_schema'
require 'pact_broker/api/decorators/verifiable_pacts_query_decorator'
require 'pact_broker/api/contracts/verifiable_pacts_json_query_schema'
require 'pact_broker/hash_refinements'

module PactBroker
module Api
module Resources
class ProviderPactsForVerification < ProviderPacts
using PactBroker::HashRefinements

def allowed_methods
["GET", "POST", "OPTIONS"]
end
Expand Down Expand Up @@ -48,7 +51,6 @@ def to_json
PactBroker::Api::Decorators::VerifiablePactsDecorator.new(pacts).to_json(to_json_options)
end


def query_schema
if request.get?
PactBroker::Api::Contracts::VerifiablePactsQuerySchema
Expand All @@ -70,6 +72,10 @@ def query
end
end
end

def to_json_options
super.deep_merge(user_options: { include_pending_status: parsed_query_params.include_pending_status })
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/pending_pacts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def publish_pact
end

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

def pact_url_from(pacts_for_verification_response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ module Decorators
let(:pending_provider_tags) { %w[dev] }
let(:consumer_tags) { %w[dev] }
let(:json) { decorator.to_json(options) }
let(:options) { { user_options: { base_url: 'http://example.org' } } }
let(:options) { { user_options: { base_url: 'http://example.org', include_pending_status: include_pending_status } } }
let(:include_pending_status) { true }

subject { JSON.parse(json) }

Expand All @@ -49,6 +50,18 @@ module Decorators
expect(decorator).to receive(:pact_version_url).with(pact, 'http://example.org')
subject
end

context "when include_pending_status is false" do
let(:include_pending_status) { false }

it "does not include the pending flag" do
expect(subject['verificationProperties']).to_not have_key('pending')
end

it "does not include the pending reason" do
expect(subject['verificationProperties']).to_not have_key('pendingReason')
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module Resources
let(:query) do
{
provider_version_tags: ['master'],
consumer_version_selectors: [ { tag: "dev", latest: "true" }]
consumer_version_selectors: [ { tag: "dev", latest: "true" }],
include_pending_status: false
}
end

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

Expand All @@ -47,7 +51,8 @@ module Resources
let(:request_body) do
{
providerVersionTags: ['master'],
consumerVersionSelectors: [ { tag: "dev", latest: true }]
consumerVersionSelectors: [ { tag: "dev", latest: true }],
includePendingStatus: true
}
end

Expand All @@ -62,7 +67,10 @@ module Resources

it "finds the pacts for verification by the provider" do
# Naughty not mocking out the query parsing...
expect(PactBroker::Pacts::Service).to receive(:find_for_verification).with("Bar", ["master"], [OpenStruct.new(tag: "dev", latest: true)])
expect(PactBroker::Pacts::Service).to receive(:find_for_verification).with(
"Bar",
["master"],
[OpenStruct.new(tag: "dev", latest: true)])
subject
end

Expand All @@ -79,9 +87,10 @@ module Resources
end
end

it "sets the correct resource title" do
it "uses the correct options for the decorator" do
expect(decorator).to receive(:to_json) do | options |
expect(options[:user_options][:title]).to eq "Pacts to be verified by provider Bar"
expect(options[:user_options][:include_pending_status]).to eq false
end
subject
end
Expand Down

0 comments on commit a80f2fd

Please sign in to comment.