diff --git a/lib/pact/cli.rb b/lib/pact/cli.rb index 936a5f71..d1337022 100755 --- a/lib/pact/cli.rb +++ b/lib/pact/cli.rb @@ -8,6 +8,7 @@ class CLI < Thor desc 'verify', "Verify a pact" method_option :pact_helper, aliases: "-h", desc: "Pact helper file", :required => true method_option :pact_uri, aliases: "-p", desc: "Pact URI" + method_option :wip, type: :boolean, default: false, desc: "If WIP, process will always exit with exit code 0", hide: true method_option :pact_broker_username, aliases: "-u", desc: "Pact broker user name" method_option :pact_broker_password, aliases: "-w", desc: "Pact broker password" method_option :backtrace, aliases: "-b", desc: "Show full backtrace", :default => false, :type => :boolean @@ -33,6 +34,5 @@ def docs require 'pact/doc/generator' Pact::Doc::Generate.call(options[:pact_dir], options[:doc_dir], [Pact::Doc::Markdown::Generator]) end - end end diff --git a/lib/pact/cli/run_pact_verification.rb b/lib/pact/cli/run_pact_verification.rb index 5de5e464..1fdbaca1 100644 --- a/lib/pact/cli/run_pact_verification.rb +++ b/lib/pact/cli/run_pact_verification.rb @@ -71,7 +71,8 @@ def pact_spec_options full_backtrace: options[:backtrace], criteria: SpecCriteria.call(options), format: options[:format], - out: options[:out] + out: options[:out], + wip: options[:wip] } end end diff --git a/lib/pact/hal/entity.rb b/lib/pact/hal/entity.rb index 4a09852a..235398dd 100644 --- a/lib/pact/hal/entity.rb +++ b/lib/pact/hal/entity.rb @@ -48,8 +48,8 @@ def response @response end - def fetch(key) - @links[key] + def fetch(key, fallback_key = nil) + @links[key] || (fallback_key && @links[fallback_key]) end def method_missing(method_name, *args, &block) diff --git a/lib/pact/pact_broker/fetch_pacts.rb b/lib/pact/pact_broker/fetch_pacts.rb index cdca3459..3670b1d8 100644 --- a/lib/pact/pact_broker/fetch_pacts.rb +++ b/lib/pact/pact_broker/fetch_pacts.rb @@ -11,6 +11,7 @@ class FetchPacts LATEST_PROVIDER_TAG_RELATION = 'pb:latest-provider-pacts-with-tag'.freeze LATEST_PROVIDER_RELATION = 'pb:latest-provider-pacts'.freeze PACTS = 'pacts'.freeze + PB_PACTS = 'pb:pacts'.freeze HREF = 'href'.freeze def initialize(provider, tags, broker_base_url, http_client_options) @@ -79,7 +80,7 @@ def get_latest_pacts_for_provider end def get_pact_urls(link_by_provider) - link_by_provider.fetch(PACTS).collect do |pact| + link_by_provider.fetch(PB_PACTS, PACTS).collect do |pact| Pact::Provider::PactURI.new(pact[HREF], http_client_options) end end diff --git a/lib/pact/pact_broker/fetch_wip_pacts.rb b/lib/pact/pact_broker/fetch_wip_pacts.rb new file mode 100644 index 00000000..c8616e85 --- /dev/null +++ b/lib/pact/pact_broker/fetch_wip_pacts.rb @@ -0,0 +1,62 @@ +require 'pact/hal/entity' +require 'pact/hal/http_client' +require 'pact/provider/pact_uri' + +module Pact + module PactBroker + class FetchWipPacts + attr_reader :provider, :tags, :broker_base_url, :http_client_options, :http_client, :index_entity + + WIP_PROVIDER_RELATION = 'pb:wip-provider-pacts'.freeze + PACTS = 'pacts'.freeze + PB_PACTS = 'pb:pacts'.freeze + HREF = 'href'.freeze + + def initialize(provider, broker_base_url, http_client_options) + @provider = provider + @http_client_options = http_client_options + @broker_base_url = broker_base_url + @http_client = Pact::Hal::HttpClient.new(http_client_options) + end + + def self.call(provider, broker_base_url, http_client_options) + new(provider, broker_base_url, http_client_options).call + end + + def call + log_message + if get_index.success? + get_wip_pacts_for_provider + else + raise Pact::Error.new("Error retrieving #{broker_base_url} status=#{index_entity.response.code} #{index_entity.response.raw_body}") + end + end + + private + + def get_index + @index_entity = Pact::Hal::Link.new({ "href" => broker_base_url }, http_client).get + end + + def get_wip_pacts_for_provider + link = index_entity._link(WIP_PROVIDER_RELATION) + if link + get_pact_urls(link.expand(provider: provider).get) + else + [] + end + end + + def get_pact_urls(link_by_provider) + link_by_provider.fetch(PB_PACTS, PACTS).collect do |pact| + Pact::Provider::PactURI.new(pact[HREF], http_client_options) + end + end + + def log_message + message = "INFO: Fetching WIP pacts for #{provider} from #{broker_base_url}" + Pact.configuration.output_stream.puts message + end + end + end +end diff --git a/lib/pact/provider/pact_spec_runner.rb b/lib/pact/provider/pact_spec_runner.rb index b219dcbc..2d6e887a 100644 --- a/lib/pact/provider/pact_spec_runner.rb +++ b/lib/pact/provider/pact_spec_runner.rb @@ -36,6 +36,7 @@ def run ensure ::RSpec.reset Pact.clear_provider_world + Pact.clear_consumer_world end end @@ -90,7 +91,7 @@ def run_specs ::RSpec::Core::CommandLine.new(NoConfigurationOptions.new) .run(::RSpec.configuration.output_stream, ::RSpec.configuration.error_stream) end - exit_code + options[:wip] ? 0 : exit_code end def rspec_runner_options @@ -118,7 +119,8 @@ def pact_jsons def initialize_specs pact_sources.each do | pact_source | options = { - criteria: @options[:criteria] + criteria: @options[:criteria], + wip: @options[:wip] } honour_pactfile pact_source.uri, ordered_pact_json(pact_source.pact_json), options end @@ -144,6 +146,8 @@ def configure_output end ::RSpec.configuration.full_backtrace = @options[:full_backtrace] + + ::RSpec.configuration.failure_color = :yellow if @options[:wip] end def ordered_pact_json(pact_json) diff --git a/lib/pact/provider/rspec.rb b/lib/pact/provider/rspec.rb index e6ed3c2d..923b3242 100644 --- a/lib/pact/provider/rspec.rb +++ b/lib/pact/provider/rspec.rb @@ -21,10 +21,11 @@ module ClassMethods include ::RSpec::Core::DSL def honour_pactfile pact_uri, pact_json, options - Pact.configuration.output_stream.puts "INFO: Reading pact at #{pact_uri}" + pact_description = options[:wip] ? "WIP pact" : "pact" + Pact.configuration.output_stream.puts "INFO: Reading #{pact_description} at #{pact_uri}" Pact.configuration.output_stream.puts "INFO: Filtering interactions by: #{options[:criteria]}" if options[:criteria] && options[:criteria].any? consumer_contract = Pact::ConsumerContract.from_json(pact_json) - ::RSpec.describe "Verifying a pact between #{consumer_contract.consumer.name} and #{consumer_contract.provider.name}", pactfile_uri: pact_uri do + ::RSpec.describe "Verifying a #{pact_description} between #{consumer_contract.consumer.name} and #{consumer_contract.provider.name}", pactfile_uri: pact_uri do honour_consumer_contract consumer_contract, options.merge(pact_json: pact_json, pact_uri: pact_uri) end end @@ -72,7 +73,8 @@ def describe_interaction interaction, options pact: :verify, pact_interaction: interaction, pact_interaction_example_description: interaction_description_for_rerun_command(interaction), - pact_uri: options[:pact_uri] + pact_uri: options[:pact_uri], + pact_wip: options[:wip] } describe description_for(interaction), metadata do diff --git a/lib/pact/provider/rspec/formatter_rspec_3.rb b/lib/pact/provider/rspec/formatter_rspec_3.rb index 4e00e436..0bcb9fd8 100644 --- a/lib/pact/provider/rspec/formatter_rspec_3.rb +++ b/lib/pact/provider/rspec/formatter_rspec_3.rb @@ -42,9 +42,21 @@ def failed_interactions_count(summary) summary.failed_examples.collect{ |e| e.metadata[:pact_interaction_example_description] }.uniq.size end + def wip?(summary) + summary.failed_examples.any?{ |e| e.metadata[:pact_wip] } + end + + def failure_title summary + if wip?(summary) + "#{failed_interactions_count(summary)} pending" + else + ::RSpec::Core::Formatters::Helpers.pluralize(failed_interactions_count(summary), "failure") + end + end + def totals_line summary line = ::RSpec::Core::Formatters::Helpers.pluralize(interactions_count(summary), "interaction") - line << ", " << ::RSpec::Core::Formatters::Helpers.pluralize(failed_interactions_count(summary), "failure") + line << ", " << failure_title(summary) line end @@ -57,7 +69,11 @@ def color_for_summary summary end def print_rerun_commands summary - output.puts("\nFailed interactions:\n\n") + if wip?(summary) + output.puts("\nPending interactions: (Failures listed here are expected and do not affect your suite's status)\n\n") + else + output.puts("\nFailed interactions:\n\n") + end interaction_rerun_commands(summary).each do | message | output.puts(message) end diff --git a/lib/pact/tasks/task_helper.rb b/lib/pact/tasks/task_helper.rb index 92091270..0431afad 100644 --- a/lib/pact/tasks/task_helper.rb +++ b/lib/pact/tasks/task_helper.rb @@ -10,8 +10,8 @@ module TaskHelper extend self - def execute_pact_verify pact_uri = nil, pact_helper = nil, rspec_opts = nil - execute_cmd verify_command(pact_helper || Pact::Provider::PactHelperLocater.pact_helper_path, pact_uri, rspec_opts) + def execute_pact_verify pact_uri = nil, pact_helper = nil, rspec_opts = nil, verification_opts = {} + execute_cmd verify_command(pact_helper || Pact::Provider::PactHelperLocater.pact_helper_path, pact_uri, rspec_opts, verification_opts) end def handle_verification_failure @@ -19,7 +19,7 @@ def handle_verification_failure abort if exit_status != 0 end - def verify_command pact_helper, pact_uri, rspec_opts + def verify_command pact_helper, pact_uri, rspec_opts, verification_opts command_parts = [] # Clear SPEC_OPTS, otherwise we can get extra formatters, creating duplicate output eg. CI Reporting. # Allow deliberate configuration using rspec_opts in VerificationTask. @@ -28,6 +28,7 @@ def verify_command pact_helper, pact_uri, rspec_opts command_parts << "-S pact verify" command_parts << "--pact-helper" << Shellwords.escape(pact_helper.end_with?(".rb") ? pact_helper : pact_helper + ".rb") (command_parts << "--pact-uri" << pact_uri) if pact_uri + command_parts << "--wip" if verification_opts[:wip] command_parts << "--pact-broker-username" << ENV['PACT_BROKER_USERNAME'] if ENV['PACT_BROKER_USERNAME'] command_parts << "--pact-broker-password" << ENV['PACT_BROKER_PASSWORD'] if ENV['PACT_BROKER_PASSWORD'] command_parts << "--backtrace" if ENV['BACKTRACE'] == 'true' diff --git a/lib/pact/tasks/verification_task.rb b/lib/pact/tasks/verification_task.rb index 5203e7d1..e328cbf9 100644 --- a/lib/pact/tasks/verification_task.rb +++ b/lib/pact/tasks/verification_task.rb @@ -30,9 +30,11 @@ class VerificationTask < ::Rake::TaskLib attr_reader :pact_spec_configs attr_accessor :rspec_opts + attr_accessor :wip def initialize(name) @rspec_opts = nil + @wip = false @pact_spec_configs = [] @name = name yield self @@ -74,7 +76,7 @@ def rake_task require 'pact/tasks/task_helper' exit_statuses = pact_spec_configs.collect do | config | - Pact::TaskHelper.execute_pact_verify config[:uri], config[:pact_helper], rspec_opts + Pact::TaskHelper.execute_pact_verify config[:uri], config[:pact_helper], rspec_opts, { wip: wip } end Pact::TaskHelper.handle_verification_failure do diff --git a/spec/lib/pact/hal/entity_spec.rb b/spec/lib/pact/hal/entity_spec.rb index 041da19e..ce947568 100644 --- a/spec/lib/pact/hal/entity_spec.rb +++ b/spec/lib/pact/hal/entity_spec.rb @@ -75,18 +75,31 @@ module Hal end describe 'fetch' do - context 'when the key exist' do + context 'when the key exists' do it 'returns fetched value' do - expect(subject.fetch('pb:provider')).to be do - {href: 'http://provider'} - end + expect(subject.fetch('pb:provider')).to eq("href" => 'http://provider') end end + context "when the key doesn't not exist" do it 'returns nil' do expect(subject.fetch('i-dont-exist')).to be nil end end + + context "when a fallback key is provided" do + context "when the fallback value exists" do + it "returns the fallback value" do + expect(subject.fetch('i-dont-exist', 'pb:provider')).to eq("href" => 'http://provider') + end + end + + context "when the fallback value does not exist" do + it "returns nil" do + expect(subject.fetch('i-dont-exist', 'i-also-dont-exist')).to be nil + end + end + end end end end diff --git a/spec/lib/pact/pact_broker/fetch_pacts_spec.rb b/spec/lib/pact/pact_broker/fetch_pacts_spec.rb index 9349cecf..b101fc61 100644 --- a/spec/lib/pact/pact_broker/fetch_pacts_spec.rb +++ b/spec/lib/pact/pact_broker/fetch_pacts_spec.rb @@ -5,15 +5,16 @@ module PactBroker describe FetchPacts do describe "call" do + before do + allow(Pact.configuration).to receive(:output_stream).and_return(double('output stream').as_null_object) + stub_request(:get, "http://broker.org/").to_return(status: 500, body: "foo", headers: {}) + end + let(:provider) { "Foo"} let(:tags) { ["master", "prod"] } let(:broker_base_url) { "http://broker.org" } let(:http_client_options) { {} } - before do - stub_request(:get, "http://broker.org/").to_return(status: 500, body: "foo", headers: {}) - end - subject { FetchPacts.call(provider, tags, broker_base_url, http_client_options)} let(:subject_with_rescue) do diff --git a/spec/lib/pact/pact_broker/fetch_wip_pacts_spec.rb b/spec/lib/pact/pact_broker/fetch_wip_pacts_spec.rb new file mode 100644 index 00000000..7acc3b28 --- /dev/null +++ b/spec/lib/pact/pact_broker/fetch_wip_pacts_spec.rb @@ -0,0 +1,54 @@ +require 'pact/pact_broker/fetch_wip_pacts' + +module Pact + module PactBroker + describe FetchWipPacts do + describe "call" do + before do + allow(Pact.configuration).to receive(:output_stream).and_return(double('output stream').as_null_object) + end + + let(:provider) { "Foo"} + let(:broker_base_url) { "http://broker.org" } + let(:http_client_options) { {} } + subject { FetchWipPacts.call(provider, broker_base_url, http_client_options)} + + context "when there is an error retrieving the index resource" do + before do + stub_request(:get, "http://broker.org/").to_return(status: 500, body: "foo", headers: {}) + end + + let(:subject_with_rescue) do + begin + subject + rescue Pact::Error + # can't be bothered stubbing out everything to make the rest of the code execute nicely + # when all we care about is the message + end + end + + it "raises a Pact::Error" do + expect { subject }.to raise_error Pact::Error, /500.*foo/ + end + end + + context "when the pb:wip-provider-pacts relation does not exist" do + before do + stub_request(:get, "http://broker.org/").to_return(status: 200, body: response_body, headers: response_headers) + end + + let(:response_headers) { { "Content-Type" => "application/hal+json" } } + let(:response_body) do + { + _links: {} + }.to_json + end + + it "returns an empty list" do + expect(subject).to eq [] + end + end + end + end + end +end diff --git a/spec/lib/pact/provider/rspec/formatter_rspec_3_spec.rb b/spec/lib/pact/provider/rspec/formatter_rspec_3_spec.rb index bc5fae02..7cb52d0e 100644 --- a/spec/lib/pact/provider/rspec/formatter_rspec_3_spec.rb +++ b/spec/lib/pact/provider/rspec/formatter_rspec_3_spec.rb @@ -19,7 +19,8 @@ module RSpec pact_interaction: interaction, pactfile_uri: pactfile_uri, pact_interaction_example_description: description, - pact_json: pact_json + pact_json: pact_json, + pact_wip: wip } end let(:metadata_2) { metadata.merge(pact_interaction_example_description: 'another interaction')} @@ -33,6 +34,7 @@ module RSpec let(:summary) { double("summary", failure_count: 1, failed_examples: failed_examples, examples: examples)} let(:pact_executing_language) { 'ruby' } let(:pact_interaction_rerun_command) { Pact::TaskHelper::PACT_INTERACTION_RERUN_COMMAND } + let(:wip) { nil } subject { Formatter.new output } @@ -102,6 +104,19 @@ module RSpec subject.dump_summary summary end end + + context "when wip is true" do + let(:wip) { true } + + it "reports failures as pending" do + expect(output_result).to include("1 pending") + expect(output_result).to_not include("1 failure") + end + + it "explains that failures will not affect the test results" do + expect(output_result).to include "Pending interactions: (Failures listed here are expected and do not affect your suite's status)" + end + end end end end diff --git a/spec/lib/pact/tasks/task_helper_spec.rb b/spec/lib/pact/tasks/task_helper_spec.rb index b1be294b..385fe6f5 100644 --- a/spec/lib/pact/tasks/task_helper_spec.rb +++ b/spec/lib/pact/tasks/task_helper_spec.rb @@ -8,6 +8,8 @@ module Pact let(:ruby_path) { "/path/to/ruby" } let(:pact_uri) { "/pact/uri" } let(:default_pact_helper_path) { "/pact/helper/path.rb" } + let(:verification_options) { { wip: wip } } + let(:wip) { nil } before do stub_const("FileUtils::RUBY", ruby_path) @@ -117,6 +119,14 @@ module Pact end end + context "with wip: true" do + let(:wip) { true } + it "executes the command with --wip" do + expect(TaskHelper).to receive(:execute_cmd).with(/ --wip\b/) + TaskHelper.execute_pact_verify(pact_uri, nil, nil, verification_options) + end + end + context "with PACT_BROKER_USERNAME set" do before do ENV['PACT_BROKER_USERNAME'] = 'pact_username' diff --git a/spec/lib/pact/tasks/verification_task_spec.rb b/spec/lib/pact/tasks/verification_task_spec.rb index 2a99578e..d86520c6 100644 --- a/spec/lib/pact/tasks/verification_task_spec.rb +++ b/spec/lib/pact/tasks/verification_task_spec.rb @@ -1,5 +1,6 @@ require 'spec_helper' require 'pact/tasks/verification_task' +require 'pact/tasks/task_helper' module Pact describe VerificationTask do @@ -8,6 +9,7 @@ module Pact @pact_uri = 'http://example.org/pact.json' @task_name = 'pact:verify:pact_rake_spec' @task_name_with_explict_pact_helper = 'pact:verify:pact_rake_spec_with_explict_pact_helper' + @task_name_wip = 'pact:verify:pact_rake_spec_wip' @consumer = 'some-consumer' @criteria = {:description => /wiffle/} @@ -18,15 +20,17 @@ module Pact VerificationTask.new(:pact_rake_spec) do | pact | pact.uri @pact_uri end + + VerificationTask.new(:pact_rake_spec_wip) do | pact | + pact.uri @pact_uri + pact.wip = true + end end before do allow(Pact::TaskHelper).to receive(:execute_pact_verify).and_return(0) end - let(:exit_code) {0} - - describe '.initialize' do context 'with an explict pact_helper' do it 'creates the tasks' do @@ -41,29 +45,33 @@ module Pact end describe 'execute' do - context "with no explicit pact_helper" do it 'verifies the pacts using the TaskHelper' do - expect(Pact::TaskHelper).to receive(:execute_pact_verify).with(@pact_uri, nil, nil) + expect(Pact::TaskHelper).to receive(:execute_pact_verify).with(@pact_uri, nil, nil, { wip: false }) Rake::Task[@task_name].execute end end context "with an explict pact_helper" do let(:verification_config) { [ uri: @pact_uri, pact_helper: @pact_helper] } - it 'verifies the pacts using the TaskHelper' do - expect(Pact::TaskHelper).to receive(:execute_pact_verify).with(@pact_uri, @pact_helper, nil) + it 'verifies the pacts using specified pact_helper' do + expect(Pact::TaskHelper).to receive(:execute_pact_verify).with(@pact_uri, @pact_helper, nil, { wip: false }) Rake::Task[@task_name_with_explict_pact_helper].execute end end - context 'when all specs pass' do + context "with wip: true" do + it 'verifies the pacts with wip: true' do + expect(Pact::TaskHelper).to receive(:execute_pact_verify).with(@pact_uri, anything, anything, { wip: true }) + Rake::Task[@task_name_wip].execute + end + end + context 'when all specs pass' do it 'does not raise an exception' do Rake::Task[@task_name].execute end end - end end end diff --git a/spec/service_providers/helper.rb b/spec/service_providers/helper.rb new file mode 100644 index 00000000..9d41fd1d --- /dev/null +++ b/spec/service_providers/helper.rb @@ -0,0 +1,10 @@ +require 'pact/consumer/rspec' + +Pact.service_consumer 'Pact Ruby' do + has_pact_with 'Pact Broker' do + mock_service :pact_broker do + port 1234 + pact_specification_version '2.0.0' + end + end +end diff --git a/spec/service_providers/pact_ruby_fetch_pacts_test.rb b/spec/service_providers/pact_ruby_fetch_pacts_test.rb index 6a10970d..65305310 100644 --- a/spec/service_providers/pact_ruby_fetch_pacts_test.rb +++ b/spec/service_providers/pact_ruby_fetch_pacts_test.rb @@ -1,17 +1,13 @@ -require 'pact/consumer/rspec' +require_relative 'helper' require 'pact/pact_broker/fetch_pacts' -require 'pact/provider/pact_uri' -Pact.service_consumer 'Pact Ruby' do - has_pact_with 'Pact Broker' do - mock_service :pact_broker do - port 1234 - pact_specification_version '2.0.0' - end - end -end describe Pact::PactBroker::FetchPacts, pact: true do + + before do + allow($stdout).to receive(:puts) + end + let(:get_headers) { { Accept: 'application/hal+json' } } describe 'fetch pacts' do @@ -71,7 +67,7 @@ status: 200, body: { _links: { - pacts: [ + 'pb:pacts' => [ { href: Pact.term('http://pact-broker-url-for-consumer-1', %r{http://.*}) }, @@ -111,7 +107,7 @@ status: 200, body: { _links: { - pacts: [ + 'pb:pacts' => [ { href: Pact.term('http://pact-broker-url-for-consumer-1-tag-1', %r{http://.*}) }, @@ -134,7 +130,7 @@ status: 200, body: { _links: { - pacts: [ + 'pb:pacts' => [ { href: Pact.term('http://pact-broker-url-for-consumer-1-tag-2', %r{http://.*}) }, @@ -177,7 +173,7 @@ status: 200, body: { _links: { - pacts: [] + 'pb:pacts' => [] } } ) @@ -193,7 +189,7 @@ status: 200, body: { _links: { - pacts: [ + 'pb:pacts' => [ { href: Pact.term('http://pact-broker-url-for-consumer-1-master', %r{http://.*}) }, @@ -233,7 +229,7 @@ status: 200, body: { _links: { - pacts: [] + 'pb:pacts' => [] } } ) @@ -262,7 +258,7 @@ status: 200, body: { _links: { - pacts: [ + 'pb:pacts' => [ { href: Pact.term('http://pact-broker-url-for-consumer-1-tag-1', %r{http://.*}) }, @@ -286,7 +282,7 @@ status: 200, body: { _links: { - pacts: [ + 'pb:pacts' => [ { href: Pact.term('http://pact-broker-url-for-consumer-1-tag-2-all', %r{http://.*}) }, @@ -329,7 +325,7 @@ status: 200, body: { _links: { - pacts: [ + 'pb:pacts' => [ { href: Pact.term('http://pact-broker-url-for-consumer-1-all', %r{http://.*}) }, diff --git a/spec/service_providers/pact_ruby_fetch_wip_pacts_test.rb b/spec/service_providers/pact_ruby_fetch_wip_pacts_test.rb new file mode 100644 index 00000000..4238bf8f --- /dev/null +++ b/spec/service_providers/pact_ruby_fetch_wip_pacts_test.rb @@ -0,0 +1,74 @@ +require_relative 'helper' +require 'pact/pact_broker/fetch_wip_pacts' + +describe Pact::PactBroker::FetchWipPacts, pact: true do + before do + allow($stdout).to receive(:puts) + end + + let(:get_headers) { { Accept: 'application/hal+json' } } + + describe 'fetch pacts' do + let(:provider) { 'provider-1' } + let(:broker_base_url) { pact_broker.mock_service_base_url + '/' } + let(:basic_auth_options) { { username: 'foo', password: 'bar' } } + + before do + pact_broker + .given('the relations for retrieving WIP pacts exist in the index resource') + .upon_receiving('a request for the index resource') + .with( + method: :get, + path: '/', + headers: get_headers + ) + .will_respond_with( + status: 200, + body: { + _links: { + 'pb:wip-provider-pacts' => { + href: Pact.term( + generate: broker_base_url + 'pacts/provider/{provider}/wip', + matcher: %r{/pacts/provider/{provider}/wip$} + ) + } + } + } + ) + end + + context 'retrieving WIP pacts by provider' do + before do + pact_broker + .given('consumer-1 has a WIP pact with provider provider-1') + .upon_receiving('a request to retrieve the WIP pacts for provider') + .with( + method: :get, + path: '/pacts/provider/provider-1/wip', + headers: get_headers + ) + .will_respond_with( + status: 200, + body: { + _links: { + 'pb:pacts' => [ + { + href: Pact.term('http://pact-broker-url-for-consumer-1', %r{http://.*}) + } + ] + } + } + ) + end + + it 'returns the array of pact urls' do + pacts = Pact::PactBroker::FetchWipPacts.call(provider, broker_base_url, basic_auth_options) + expect(pacts).to eq( + [ + Pact::Provider::PactURI.new('http://pact-broker-url-for-consumer-1', basic_auth_options) + ] + ) + end + end + end +end diff --git a/spec/support/bar_pact_helper.rb b/spec/support/bar_pact_helper.rb index 3b001de2..a4e3521f 100644 --- a/spec/support/bar_pact_helper.rb +++ b/spec/support/bar_pact_helper.rb @@ -25,7 +25,7 @@ def call env end honours_pact_with 'Foo' do - pact_uri './spec/support/foo-bar.json' + pact_uri './spec/pacts/foo-bar.json' end end end diff --git a/tasks/foo-bar.rake b/tasks/foo-bar.rake index a7037737..a90ce4e1 100644 --- a/tasks/foo-bar.rake +++ b/tasks/foo-bar.rake @@ -29,6 +29,10 @@ Pact::VerificationTask.new('foobar') do | pact | pact.uri nil, pact_helper: './spec/support/bar_pact_helper.rb' end +Pact::VerificationTask.new('foobar:wip') do | pact | + pact.uri './spec/pacts/foo-bar-wip.json', pact_helper: './spec/support/bar_pact_helper.rb' + pact.wip = true +end Pact::VerificationTask.new(:foobar_using_broker) do | pact | pact.uri "#{BROKER_BASE_URL}/pacts/provider/Bar/consumer/Foo/version/1.0.0", :pact_helper => './spec/support/bar_pact_helper.rb', username: BROKER_USERNAME, password: BROKER_PASSWORD diff --git a/tasks/pact-test.rake b/tasks/pact-test.rake index 9d49fd2a..415afc46 100644 --- a/tasks/pact-test.rake +++ b/tasks/pact-test.rake @@ -53,6 +53,12 @@ Pact::VerificationTask.new('test_app:fail') do | pact | pact.uri './spec/support/test_app_fail.json', pact_helper: './spec/support/pact_helper.rb' end +Pact::VerificationTask.new('test_app:wip') do | pact | + pact.uri './spec/support/test_app_fail.json', pact_helper: './spec/support/pact_helper.rb' + pact.wip = true +end + + task :bethtest => ['pact:tests:all','pact:tests:all:with_active_support'] namespace :pact do @@ -68,6 +74,7 @@ namespace :pact do Rake::Task['pact:verify:test_app:content_type'].execute Rake::Task['pact:verify:case_insensitive_response_header_matching'].execute Rake::Task['pact:verify:term_v2'].execute + Rake::Task['pact:verify:test_app:wip'].execute end desc "All the verification tests with active support loaded"