Skip to content

Commit

Permalink
feat(verify): allow includePendingStatus to be specified when fetchin…
Browse files Browse the repository at this point in the history
…g pacts
  • Loading branch information
bethesque committed Jan 11, 2020
1 parent 7a81742 commit 1f5fc9c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 36 deletions.
19 changes: 16 additions & 3 deletions lib/pact/hal/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,19 +46,23 @@ 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)
get(*args).assert_success!
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)
Expand Down
29 changes: 14 additions & 15 deletions lib/pact/pact_broker/fetch_pact_uris_for_verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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}"
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/pact/hal/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/pact/hal/link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand All @@ -36,6 +43,7 @@
)
.will_respond_with(
status: 200,
headers: { "Content-Type" => Pact.term("application/hal+json", /hal/) },
body: {
_links: {
pacts_for_verification_relation => {
Expand All @@ -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" => {
Expand All @@ -84,8 +92,7 @@
let(:expected_metadata) do
{
pending: true,
inclusion_reason: "inclusion reason",
pending_reason: "pending reason"
notices: ["some text"]
}
end

Expand Down

0 comments on commit 1f5fc9c

Please sign in to comment.