From a80f2fd696dc7d594c8e988114cdcf2df41cd9d0 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 21 Nov 2019 11:55:31 +1100 Subject: [PATCH] feat(pacts for verification): allow pending status information to be optionall included --- .../verifiable_pacts_json_query_schema.rb | 1 + .../verifiable_pacts_query_schema.rb | 1 + .../decorators/verifiable_pact_decorator.rb | 8 +++++--- .../verifiable_pacts_query_decorator.rb | 5 +++++ .../provider_pacts_for_verification.rb | 8 +++++++- spec/features/pending_pacts_spec.rb | 2 +- .../verifiable_pact_decorator_spec.rb | 15 ++++++++++++++- .../provider_pacts_for_verification_spec.rb | 19 ++++++++++++++----- 8 files changed, 48 insertions(+), 11 deletions(-) diff --git a/lib/pact_broker/api/contracts/verifiable_pacts_json_query_schema.rb b/lib/pact_broker/api/contracts/verifiable_pacts_json_query_schema.rb index b86e1b20d..46c8f27b6 100644 --- a/lib/pact_broker/api/contracts/verifiable_pacts_json_query_schema.rb +++ b/lib/pact_broker/api/contracts/verifiable_pacts_json_query_schema.rb @@ -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) diff --git a/lib/pact_broker/api/contracts/verifiable_pacts_query_schema.rb b/lib/pact_broker/api/contracts/verifiable_pacts_query_schema.rb index 90ce5c489..a3b07918e 100644 --- a/lib/pact_broker/api/contracts/verifiable_pacts_query_schema.rb +++ b/lib/pact_broker/api/contracts/verifiable_pacts_query_schema.rb @@ -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) diff --git a/lib/pact_broker/api/decorators/verifiable_pact_decorator.rb b/lib/pact_broker/api/decorators/verifiable_pact_decorator.rb index acc042232..79d416c79 100644 --- a/lib/pact_broker/api/decorators/verifiable_pact_decorator.rb +++ b/lib/pact_broker/api/decorators/verifiable_pact_decorator.rb @@ -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 diff --git a/lib/pact_broker/api/decorators/verifiable_pacts_query_decorator.rb b/lib/pact_broker/api/decorators/verifiable_pacts_query_decorator.rb index 03e106c99..fc3509492 100644 --- a/lib/pact_broker/api/decorators/verifiable_pacts_query_decorator.rb +++ b/lib/pact_broker/api/decorators/verifiable_pacts_query_decorator.rb @@ -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) diff --git a/lib/pact_broker/api/resources/provider_pacts_for_verification.rb b/lib/pact_broker/api/resources/provider_pacts_for_verification.rb index c30711f80..b2f586007 100644 --- a/lib/pact_broker/api/resources/provider_pacts_for_verification.rb +++ b/lib/pact_broker/api/resources/provider_pacts_for_verification.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/features/pending_pacts_spec.rb b/spec/features/pending_pacts_spec.rb index 18b4ba0bb..18a528886 100644 --- a/spec/features/pending_pacts_spec.rb +++ b/spec/features/pending_pacts_spec.rb @@ -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) diff --git a/spec/lib/pact_broker/api/decorators/verifiable_pact_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/verifiable_pact_decorator_spec.rb index 687b91c31..9560371f2 100644 --- a/spec/lib/pact_broker/api/decorators/verifiable_pact_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/verifiable_pact_decorator_spec.rb @@ -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) } @@ -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 diff --git a/spec/lib/pact_broker/api/resources/provider_pacts_for_verification_spec.rb b/spec/lib/pact_broker/api/resources/provider_pacts_for_verification_spec.rb index ce175003a..7829cfba5 100644 --- a/spec/lib/pact_broker/api/resources/provider_pacts_for_verification_spec.rb +++ b/spec/lib/pact_broker/api/resources/provider_pacts_for_verification_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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