Skip to content

Commit

Permalink
fix(verifications): tag provider version, not consumer version
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Nov 16, 2017
1 parent cd2b79e commit b347588
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
18 changes: 11 additions & 7 deletions lib/pact/provider/verification_results/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,21 @@ def publication_url
end

def tag_url tag
href = pact_source.pact_hash.fetch('_links', {}).fetch('pb:tag-version', {})['href']
href ? href.gsub('{tag}', tag) : nil
# This is so dodgey, need to use proper HAL
if publication_url
u = URI(publication_url)
if match = publication_url.match(%r{/provider/([^/]+)})
provider_name = match[1]
base_url = "#{u.scheme}://#{u.host}:#{u.host == u.default_port ? '' : u.port}"
provider_application_version = Pact.configuration.provider.application_version
"#{base_url}/pacticipants/#{provider_name}/versions/#{provider_application_version}/tags/#{tag}"
end
end
end

def tag_versions_if_configured
if Pact.configuration.provider.tags.any?
if tag_url('')
tag_versions
else
Pact.configuration.error_stream.puts "WARN: Cannot tag provider version as there is no link named pb:tag-version in the pact JSON."
end
tag_versions if tag_url('')
end
end

Expand Down
30 changes: 15 additions & 15 deletions spec/lib/pact/provider/verification_results/publish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module VerificationResults
describe Publish do
describe "call" do
let(:publish_verification_url) { nil }
let(:stubbed_publish_verification_url) { 'http://broker/something/provider/Bar/verifications' }
let(:tag_version_url) { 'http://tag-me/{tag}' }
let(:pact_source) { instance_double("Pact::Provider::PactSource", pact_hash: pact_hash, uri: pact_url)}
let(:pact_url) { instance_double("Pact::Provider::PactURI", basic_auth?: basic_auth, username: 'username', password: 'password')}
Expand All @@ -18,8 +19,7 @@ module VerificationResults
'_links' => {
'pb:publish-verification-results'=> {
'href' => publish_verification_url
},
'pb:tag-version'=> {'href' => tag_version_url}
}
}
}
end
Expand All @@ -46,30 +46,30 @@ module VerificationResults
end

let(:provider_configuration) do
double('provider config', publish_verification_results?: publish_verification_results, tags: tags)
double('provider config', publish_verification_results?: publish_verification_results, tags: tags, application_version: '1.2.3')
end

before do
allow($stdout).to receive(:puts)
allow(Pact.configuration).to receive(:provider).and_return(provider_configuration)
stub_request(:post, 'http://broker/verifications').to_return(status: 200, body: created_verification_body)
stub_request(:put, /tag-me/)
stub_request(:post, stubbed_publish_verification_url).to_return(status: 200, body: created_verification_body)
stub_request(:put, 'http://broker/pacticipants/Bar/versions/1.2.3/tags/foo')
end

subject { Publish.call(pact_source, verification) }

context "when publish_verification_results is false" do
it "does not publish the verification" do
subject
expect(WebMock).to_not have_requested(:post, 'http://broker/verifications')
expect(WebMock).to_not have_requested(:post, 'http://broker/something/provider/Bar/verifications')
end
end

context "when publish_verification_results is true" do
let(:publish_verification_results) { true }

context "when the publish-verification link is present" do
let(:publish_verification_url) { 'http://broker/verifications' }
let(:publish_verification_url) { stubbed_publish_verification_url }

it "publishes the verification" do
subject
Expand All @@ -81,7 +81,7 @@ module VerificationResults

it "does not publish the verification" do
subject
expect(WebMock).to_not have_requested(:post, 'http://broker/verifications')
expect(WebMock).to_not have_requested(:post, stubbed_publish_verification_url)
end
end

Expand All @@ -90,17 +90,17 @@ module VerificationResults

it "tags the provider version" do
subject
expect(WebMock).to have_requested(:put, 'http://tag-me/foo').with(headers: {'Content-Type' => 'application/json'})
expect(WebMock).to have_requested(:put, 'http://broker/pacticipants/Bar/versions/1.2.3/tags/foo').with(headers: {'Content-Type' => 'application/json'})
end

context "when there is no pb:tag-version link" do
context "when there is no pb:publish-verification-results link" do
before do
pact_hash['_links'].delete('pb:tag-version')
pact_hash['_links'].delete('pb:publish-verification-results')
end

it "prints a warning" do
expect($stderr).to receive(:puts).with /WARN: Cannot tag provider version/
it "does not tag the version" do
subject
expect(WebMock).to_not have_requested(:put, /.*/)
end
end
end
Expand All @@ -127,7 +127,7 @@ module VerificationResults

context "when an HTTP error is returned" do
it "raises a PublicationError" do
stub_request(:post, 'http://broker/verifications').to_return(status: 500, body: 'some error')
stub_request(:post, stubbed_publish_verification_url).to_return(status: 500, body: 'some error')
expect{ subject }.to raise_error(PublicationError, /Error returned/)
end
end
Expand All @@ -143,7 +143,7 @@ module VerificationResults
before do
stub_request(:post, publish_verification_url).to_return(status: 200, body: created_verification_body)
end
let(:publish_verification_url) { 'https://broker/verifications' }
let(:publish_verification_url) { stubbed_publish_verification_url.gsub('http', 'https') }

it "uses ssl" do
subject
Expand Down

0 comments on commit b347588

Please sign in to comment.