From 1f5fc9c84feb831dfeb7820c41f320f4b21a3053 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sat, 11 Jan 2020 14:36:40 +1100 Subject: [PATCH] feat(verify): allow includePendingStatus to be specified when fetching pacts --- lib/pact/hal/link.rb | 19 ++++++++-- .../fetch_pact_uris_for_verification.rb | 29 ++++++++------- spec/lib/pact/hal/entity_spec.rb | 4 +-- spec/lib/pact/hal/link_spec.rb | 4 +-- ..._ruby_fetch_pacts_for_verification_test.rb | 35 +++++++++++-------- 5 files changed, 55 insertions(+), 36 deletions(-) diff --git a/lib/pact/hal/link.rb b/lib/pact/hal/link.rb index 7e828586..b9abd219 100644 --- a/lib/pact/hal/link.rb +++ b/lib/pact/hal/link.rb @@ -6,6 +6,15 @@ module Hal class Link attr_reader :request_method, :href + DEFAULT_GET_HEADERS = { + "Accept" => "application/hal+json" + }.freeze + + DEFAULT_POST_HEADERS = { + "Accept" => "application/hal+json", + "Content-Type" => "application/json" + }.freeze + def initialize(attrs, http_client) @attrs = attrs @request_method = attrs.fetch(:method, :get).to_sym @@ -37,7 +46,7 @@ def name end def get(payload = {}, headers = {}) - wrap_response(href, @http_client.get(href, payload, headers)) + wrap_response(href, @http_client.get(href, payload, DEFAULT_GET_HEADERS.merge(headers))) end def get!(*args) @@ -45,11 +54,15 @@ def get!(*args) end def put(payload = nil, headers = {}) - wrap_response(href, @http_client.put(href, payload ? payload.to_json : nil, headers)) + wrap_response(href, @http_client.put(href, payload ? payload.to_json : nil, DEFAULT_POST_HEADERS.merge(headers))) end def post(payload = nil, headers = {}) - wrap_response(href, @http_client.post(href, payload ? payload.to_json : nil, headers)) + wrap_response(href, @http_client.post(href, payload ? payload.to_json : nil, DEFAULT_POST_HEADERS.merge(headers))) + end + + def post!(payload = nil, headers = {}) + post(payload, headers).assert_success! end def expand(params) diff --git a/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb b/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb index ca94501f..f7cb8f1d 100644 --- a/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb +++ b/lib/pact/pact_broker/fetch_pact_uris_for_verification.rb @@ -7,7 +7,7 @@ module Pact module PactBroker class FetchPactURIsForVerification - attr_reader :provider, :consumer_version_selectors, :provider_version_tags, :broker_base_url, :http_client_options, :http_client + attr_reader :provider, :consumer_version_selectors, :provider_version_tags, :broker_base_url, :http_client_options, :http_client, :options PACTS_FOR_VERIFICATION_RELATION = 'beta:provider-pacts-for-verification'.freeze PACTS = 'pacts'.freeze @@ -16,17 +16,18 @@ class FetchPactURIsForVerification SELF = 'self'.freeze EMBEDDED = '_embedded'.freeze - def initialize(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options) + def initialize(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options, options = {}) @provider = provider @consumer_version_selectors = consumer_version_selectors || [] @provider_version_tags = provider_version_tags || [] @http_client_options = http_client_options @broker_base_url = broker_base_url @http_client = Pact::Hal::HttpClient.new(http_client_options) + @options = options end - def self.call(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options) - new(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options).call + def self.call(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options, options = {}) + new(provider, consumer_version_selectors, provider_version_tags, broker_base_url, http_client_options, options).call end def call @@ -50,8 +51,7 @@ def pacts_for_verification pacts_for_verification_entity.response.body[EMBEDDED][PACTS].collect do | pact | metadata = { pending: pact["verificationProperties"]["pending"], - pending_reason: pact["verificationProperties"]["pendingReason"], - inclusion_reason: pact["verificationProperties"]["inclusionReason"], + notices: extract_notices(pact) } Pact::Provider::PactURI.new(pact[LINKS][SELF][HREF], http_client_options, metadata) end @@ -61,22 +61,21 @@ def pacts_for_verification_entity index ._link(PACTS_FOR_VERIFICATION_RELATION) .expand(provider: provider) - .with_query(query) - .get! + .post!(query) end def query q = {} - if consumer_version_selectors.any? - q["consumer_version_selectors"] = consumer_version_selectors - end - - if provider_version_tags.any? - q["provider_version_tags"] = provider_version_tags - end + q["includePendingStatus"] = true if options[:include_pending_status] + q["consumerVersionSelectors"] = consumer_version_selectors if consumer_version_selectors.any? + q["providerVersionTags"] = provider_version_tags if provider_version_tags.any? q end + def extract_notices(pact) + (pact["verificationProperties"]["notices"] || []).collect{ |notice| notice["text"] }.compact + end + def log_message latest = consumer_version_selectors.any? ? "" : "latest " message = "INFO: Fetching #{latest}pacts for #{provider} from #{broker_base_url}" diff --git a/spec/lib/pact/hal/entity_spec.rb b/spec/lib/pact/hal/entity_spec.rb index 4eb4744d..c6b809a0 100644 --- a/spec/lib/pact/hal/entity_spec.rb +++ b/spec/lib/pact/hal/entity_spec.rb @@ -42,7 +42,7 @@ module Hal let(:post_provider) { entity.post("pb:provider", {'some' => 'data'} ) } it "executes an http request" do - expect(http_client).to receive(:post).with("http://provider", '{"some":"data"}', {}) + expect(http_client).to receive(:post).with("http://provider", '{"some":"data"}', {"Accept" => "application/hal+json", "Content-Type" => "application/json"}) post_provider end @@ -54,7 +54,7 @@ module Hal let(:post_provider) { entity._link("pb:version-tag").expand(version: "1", tag: "prod").post({'some' => 'data'} ) } it "posts to the expanded URL" do - expect(http_client).to receive(:post).with("http://provider/version/1/tag/prod", '{"some":"data"}', {}) + expect(http_client).to receive(:post).with("http://provider/version/1/tag/prod", '{"some":"data"}', {"Accept" => "application/hal+json", "Content-Type" => "application/json"}) post_provider end end diff --git a/spec/lib/pact/hal/link_spec.rb b/spec/lib/pact/hal/link_spec.rb index 7e982719..8e83d532 100644 --- a/spec/lib/pact/hal/link_spec.rb +++ b/spec/lib/pact/hal/link_spec.rb @@ -79,7 +79,7 @@ module Hal let(:do_get) { subject.get({ 'foo' => 'bar' }) } it "executes an HTTP Get request" do - expect(http_client).to receive(:get).with('http://foo/{bar}', { 'foo' => 'bar' }, {}) + expect(http_client).to receive(:get).with('http://foo/{bar}', { 'foo' => 'bar' }, { 'Accept' => 'application/hal+json' }) do_get end end @@ -89,7 +89,7 @@ module Hal context "with custom headers" do it "executes an HTTP Post request with the custom headers" do - expect(http_client).to receive(:post).with('http://foo/{bar}', '{"foo":"bar"}', { 'Accept' => 'foo' }) + expect(http_client).to receive(:post).with('http://foo/{bar}', '{"foo":"bar"}', { 'Accept' => 'foo', 'Content-Type' => 'application/json' }) do_post end end diff --git a/spec/service_providers/pact_ruby_fetch_pacts_for_verification_test.rb b/spec/service_providers/pact_ruby_fetch_pacts_for_verification_test.rb index 75c2e93a..e8b36f07 100644 --- a/spec/service_providers/pact_ruby_fetch_pacts_for_verification_test.rb +++ b/spec/service_providers/pact_ruby_fetch_pacts_for_verification_test.rb @@ -6,19 +6,26 @@ allow($stdout).to receive(:puts) end - let(:get_headers) { { Accept: 'application/hal+json' } } + let(:get_headers) { { "Accept" => 'application/hal+json' } } + let(:post_headers) do + { + "Accept" => 'application/hal+json', + "Content-Type" => "application/json" + } + end let(:pacts_for_verification_relation) { Pact::PactBroker::FetchPactURIsForVerification::PACTS_FOR_VERIFICATION_RELATION } - let(:query) do + let(:body) do { - "provider_version_tags[]" => "pdev", - "consumer_version_selectors[][tag]" => "cdev", - "consumer_version_selectors[][latest]" => "true", + "providerVersionTags" => ["pdev"], + "consumerVersionSelectors" => [{ "tag" => "cdev", "latest" => true}], + "includePendingStatus" => true } end let(:provider_version_tags) { %w[pdev] } let(:consumer_version_selectors) { [ { tag: "cdev", latest: true }] } + let(:options) { { include_pending_status: true }} - subject { Pact::PactBroker::FetchPactURIsForVerification.call(provider, consumer_version_selectors, provider_version_tags, broker_base_url, basic_auth_options) } + subject { Pact::PactBroker::FetchPactURIsForVerification.call(provider, consumer_version_selectors, provider_version_tags, broker_base_url, basic_auth_options, options) } describe 'fetch pacts' do let(:provider) { 'Bar' } @@ -36,6 +43,7 @@ ) .will_respond_with( status: 200, + headers: { "Content-Type" => Pact.term("application/hal+json", /hal/) }, body: { _links: { pacts_for_verification_relation => { @@ -52,23 +60,23 @@ context 'retrieving pacts for verification by provider' do before do pact_broker - .given('Foo has a pact with provider Bar') + .given('Foo has a pact tagged cdev with provider Bar') .upon_receiving('a request to retrieve the pacts for verification for a provider') .with( - method: :get, + method: :post, path: '/pacts/provider/Bar/for-verification', - query: query, - headers: get_headers + body: body, + headers: post_headers ) .will_respond_with( status: 200, + headers: { "Content-Type" => Pact.term("application/hal+json", /hal/) }, body: { "_embedded" => { "pacts" => [{ "verificationProperties" => { "pending" => Pact.like(true), - "pendingReason" => Pact.like("pending reason"), - "inclusionReason" => Pact.like("inclusion reason") + "notices" => Pact.each_like("text" => "some text") }, '_links' => { "self" => { @@ -84,8 +92,7 @@ let(:expected_metadata) do { pending: true, - inclusion_reason: "inclusion reason", - pending_reason: "pending reason" + notices: ["some text"] } end