diff --git a/lib/pact_broker/api/decorators/verifiable_pact_decorator.rb b/lib/pact_broker/api/decorators/verifiable_pact_decorator.rb index 30bae2305..86c2cbdcc 100644 --- a/lib/pact_broker/api/decorators/verifiable_pact_decorator.rb +++ b/lib/pact_broker/api/decorators/verifiable_pact_decorator.rb @@ -21,36 +21,40 @@ def initialize(verifiable_pact) end property :verification_properties, as: :verificationProperties do + include PactBroker::Api::PactBrokerUrls + property :pending, if: ->(context) { context[:options][:user_options][:include_pending_status] } property :wip, if: -> (context) { context[:represented].wip } - property :inclusion_reason, as: :inclusionReason, exec_context: :decorator - property :pending_reason, as: :pendingReason, exec_context: :decorator, - if: ->(context) { context[:options][:user_options][:include_pending_status] } property :notices, getter: -> (context) { context[:decorator].notices(context[:options][:user_options]) } property :noteToDevelopers, getter: -> (_) { "Please print out the text from the 'notices' rather than using the inclusionReason and the pendingReason fields. These will be removed when this API moves out of beta."} - def inclusion_reason - PactBroker::Pacts::VerifiablePactMessages.new(represented).inclusion_reason + def inclusion_reason(pact_url) + PactBroker::Pacts::VerifiablePactMessages.new(represented, pact_url).inclusion_reason end - def pending_reason - PactBroker::Pacts::VerifiablePactMessages.new(represented).pending_reason + def pending_reason(pact_url) + PactBroker::Pacts::VerifiablePactMessages.new(represented, pact_url).pending_reason end def notices(user_options) + pact_url = pact_version_url(represented, user_options[:base_url]) mess = [{ - text: inclusion_reason + timing: 'pre_verification', + text: inclusion_reason(pact_url) }] - mess << { text: pending_reason } if user_options[:include_pending_status] + mess << { + timing: 'pre_verification', + text: pending_reason(pact_url) + } if user_options[:include_pending_status] mess end end - link :self do | context | + link :self do | user_options | { - href: pact_version_url(represented, context[:base_url]), + href: pact_version_url(represented, user_options[:base_url]), name: represented.name } end diff --git a/lib/pact_broker/pacts/verifiable_pact_messages.rb b/lib/pact_broker/pacts/verifiable_pact_messages.rb index c3c9efffe..24a02defc 100644 --- a/lib/pact_broker/pacts/verifiable_pact_messages.rb +++ b/lib/pact_broker/pacts/verifiable_pact_messages.rb @@ -8,20 +8,21 @@ class VerifiablePactMessages delegate [:consumer_name, :provider_name, :head_consumer_tags, :pending_provider_tags, :non_pending_provider_tags, :pending?, :wip?] => :verifiable_pact - def initialize(verifiable_pact) + def initialize(verifiable_pact, pact_version_url) @verifiable_pact = verifiable_pact + @pact_version_url = pact_version_url end def inclusion_reason version_text = head_consumer_tags.size == 1 ? "version" : "versions" if wip? # WIP pacts will always have tags, because it is part of the definition of being a WIP pact - "This pact is being verified because it is a 'work in progress' pact (ie. it is the pact for the latest #{version_text} of Foo tagged with #{joined_head_consumer_tags} and is still in pending state). #{READ_MORE_WIP}" + "The pact at #{pact_version_url} is being verified because it is a 'work in progress' pact (ie. it is the pact for the latest #{version_text} of Foo tagged with #{joined_head_consumer_tags} and is still in pending state). #{READ_MORE_WIP}" else if head_consumer_tags.any? - "This pact is being verified because it is the pact for the latest #{version_text} of Foo tagged with #{joined_head_consumer_tags}" + "The pact at #{pact_version_url} is being verified because it is the pact for the latest #{version_text} of Foo tagged with #{joined_head_consumer_tags}" else - "This pact is being verified because it is the latest pact between #{consumer_name} and #{provider_name}." + "The pact at #{pact_version_url} is being verified because it is the latest pact between #{consumer_name} and #{provider_name}." end end end @@ -36,7 +37,7 @@ def pending_reason private - attr_reader :verifiable_pact + attr_reader :verifiable_pact, :pact_version_url def join(list, last_joiner = " and ") quoted_list = list.collect { | tag | "'#{tag}'" } 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 15b992769..904900227 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 @@ -5,7 +5,7 @@ module Api module Decorators describe VerifiablePactDecorator do before do - allow(decorator).to receive(:pact_version_url).and_return('/pact-version-url') + allow_any_instance_of(PactBroker::Api::PactBrokerUrls).to receive(:pact_version_url).and_return('/pact-version-url') allow_any_instance_of(PactBroker::Pacts::VerifiablePactMessages).to receive(:inclusion_reason).and_return("the inclusion reason") allow_any_instance_of(PactBroker::Pacts::VerifiablePactMessages).to receive(:pending_reason).and_return(pending_reason) end @@ -20,9 +20,7 @@ module Decorators }, { "text" => pending_reason } - ], - "pendingReason" => pending_reason, - "inclusionReason" => "the inclusion reason" + ] }, "_links" => { "self" => { @@ -61,6 +59,11 @@ module Decorators subject end + it "creates the inclusion message" do + expect(PactBroker::Pacts::VerifiablePactMessages).to receive(:new).twice.with(pact, '/pact-version-url').and_call_original + subject + end + context "when include_pending_status is false" do let(:include_pending_status) { false } let(:notices) { subject['verificationProperties']['notices'].collect{ | notice | notice['text'] } } diff --git a/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb b/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb index c63c84740..8bde55da4 100644 --- a/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb +++ b/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb @@ -20,27 +20,28 @@ module Pacts wip?: wip ) end + let(:pact_version_url) { "http://pact" } - subject { VerifiablePactMessages.new(verifiable_pact) } + subject { VerifiablePactMessages.new(verifiable_pact, pact_version_url) } describe "#inclusion_reason" do context "when there are no head consumer tags" do - its(:inclusion_reason) { is_expected.to include "This pact is being verified because it is the latest pact between Foo and Bar." } + its(:inclusion_reason) { is_expected.to include "The pact at http://pact is being verified because it is the latest pact between Foo and Bar." } end context "when there is 1 head consumer tags" do let(:head_consumer_tags) { %w[dev] } - its(:inclusion_reason) { is_expected.to include "This pact is being verified because it is the pact for the latest version of Foo tagged with 'dev'" } + its(:inclusion_reason) { is_expected.to include "The pact at http://pact is being verified because it is the pact for the latest version of Foo tagged with 'dev'" } end context "when there are 2 head consumer tags" do let(:head_consumer_tags) { %w[dev prod] } - its(:inclusion_reason) { is_expected.to include "This pact is being verified because it is the pact for the latest versions of Foo tagged with 'dev' and 'prod' (both have the same content)" } + its(:inclusion_reason) { is_expected.to include "The pact at http://pact is being verified because it is the pact for the latest versions of Foo tagged with 'dev' and 'prod' (both have the same content)" } end context "when there are 3 head consumer tags" do let(:head_consumer_tags) { %w[dev prod feat-x] } - its(:inclusion_reason) { is_expected.to include "This pact is being verified because it is the pact for the latest versions of Foo tagged with 'dev', 'prod' and 'feat-x' (all have the same content)" } + its(:inclusion_reason) { is_expected.to include "The pact at http://pact is being verified because it is the pact for the latest versions of Foo tagged with 'dev', 'prod' and 'feat-x' (all have the same content)" } end context "when there are 4 head consumer tags" do @@ -54,7 +55,7 @@ module Pacts let(:head_consumer_tags) { %w[feat-x] } let(:pending_provider_tags) { %w[dev] } - its(:inclusion_reason) { is_expected.to include "This pact is being verified because it is a 'work in progress' pact (ie. it is the pact for the latest version of Foo tagged with 'feat-x' and is still in pending state)."} + its(:inclusion_reason) { is_expected.to include "The pact at http://pact is being verified because it is a 'work in progress' pact (ie. it is the pact for the latest version of Foo tagged with 'feat-x' and is still in pending state)."} end end