From 273078b0f2f53655b6f7c2b733fe129acaa8e03c Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 19 Jun 2018 20:09:43 +1000 Subject: [PATCH] feat: use the latest matching pact or verification to test webhook execution, or a placeholder if either is not found --- .../api/decorators/webhook_decorator.rb | 2 +- .../api/resources/webhook_execution.rb | 6 +- lib/pact_broker/doc/views/webhooks.markdown | 2 +- lib/pact_broker/pacts/placeholder_pact.rb | 17 ++++ lib/pact_broker/pacts/repository.rb | 14 ++++ lib/pact_broker/pacts/service.rb | 4 + .../verifications/placeholder_verification.rb | 23 ++++++ lib/pact_broker/verifications/repository.rb | 9 +++ lib/pact_broker/verifications/service.rb | 4 + lib/pact_broker/webhooks/service.rb | 15 ++++ script/seed.rb | 79 +++++++------------ spec/features/execute_webhook_spec.rb | 26 ++---- .../api/resources/webhook_execution_spec.rb | 10 +-- spec/lib/pact_broker/pacts/repository_spec.rb | 40 +++++++++- .../verifications/repository_spec.rb | 36 +++++++++ spec/lib/pact_broker/webhooks/render_spec.rb | 15 ++++ spec/lib/pact_broker/webhooks/service_spec.rb | 77 ++++++++++++++++++ 17 files changed, 294 insertions(+), 85 deletions(-) create mode 100644 lib/pact_broker/pacts/placeholder_pact.rb create mode 100644 lib/pact_broker/verifications/placeholder_verification.rb diff --git a/lib/pact_broker/api/decorators/webhook_decorator.rb b/lib/pact_broker/api/decorators/webhook_decorator.rb index 538e5841a..d05044cbf 100644 --- a/lib/pact_broker/api/decorators/webhook_decorator.rb +++ b/lib/pact_broker/api/decorators/webhook_decorator.rb @@ -39,7 +39,7 @@ class WebhookEventDecorator < BaseDecorator link :'pb:execute' do | options | { - title: "Test the execution of the webhook by sending a POST request to this URL", + title: "Test the execution of the webhook with the latest matching pact or verification by sending a POST request to this URL", href: webhook_execution_url(represented, options[:base_url]) } end diff --git a/lib/pact_broker/api/resources/webhook_execution.rb b/lib/pact_broker/api/resources/webhook_execution.rb index eae508b46..0366e950e 100644 --- a/lib/pact_broker/api/resources/webhook_execution.rb +++ b/lib/pact_broker/api/resources/webhook_execution.rb @@ -14,7 +14,7 @@ def allowed_methods end def process_post - webhook_execution_result = webhook_service.execute_webhook_now webhook, pact + webhook_execution_result = webhook_service.test_execution(webhook) response.headers['Content-Type'] = 'application/hal+json;charset=utf-8' response.body = post_response_body webhook_execution_result if webhook_execution_result.success? @@ -39,10 +39,6 @@ def webhook @webhook ||= webhook_service.find_by_uuid uuid end - def pact - @pact ||= pact_service.find_latest_pact consumer_name: webhook.consumer_name, provider_name: webhook.provider_name - end - def uuid identifier_from_path[:uuid] end diff --git a/lib/pact_broker/doc/views/webhooks.markdown b/lib/pact_broker/doc/views/webhooks.markdown index 1ef848845..bd66bd2b8 100644 --- a/lib/pact_broker/doc/views/webhooks.markdown +++ b/lib/pact_broker/doc/views/webhooks.markdown @@ -133,7 +133,7 @@ Pact Broker Github repository. ### Testing -To test a webhook, navigate to the webhook in the HAL browser, then make a POST request to the "execute" relation. The response or error will be shown in the window. +To test a webhook, navigate to the webhook in the HAL browser, then make a POST request to the "pb:execute" relation. The latest matching pact/verification will be used in the template, or a placeholder if none exists. The response or error will be shown in the window. ### Deleting diff --git a/lib/pact_broker/pacts/placeholder_pact.rb b/lib/pact_broker/pacts/placeholder_pact.rb new file mode 100644 index 000000000..02a3107c0 --- /dev/null +++ b/lib/pact_broker/pacts/placeholder_pact.rb @@ -0,0 +1,17 @@ +require 'pact_broker/domain/pact' + +module PactBroker + module Pacts + class PlaceholderPact < PactBroker::Domain::Pact + def initialize + consumer = OpenStruct.new(name: "placeholder-consumer") + @provider = OpenStruct.new(name: "placeholder-provider") + @consumer_version = OpenStruct.new(number: "1", pacticipant: consumer, tags: [OpenStruct.new(name: "master")]) + @consumer_version_number = @consumer_version.number + @created_at = DateTime.now + @revision_number = 1 + @pact_version_sha = "5d445a4612743728dfd99ccd4210423c052bb9db" + end + end + end +end diff --git a/lib/pact_broker/pacts/repository.rb b/lib/pact_broker/pacts/repository.rb index b2192b278..67e123ece 100644 --- a/lib/pact_broker/pacts/repository.rb +++ b/lib/pact_broker/pacts/repository.rb @@ -121,6 +121,20 @@ def find_latest_pact(consumer_name, provider_name, tag = nil) query.latest.all.collect(&:to_domain_with_content)[0] end + # Allows optional consumer_name and provider_name + def search_for_latest_pact(consumer_name, provider_name, tag = nil) + query = LatestPactPublicationsByConsumerVersion.select_all_qualified + query = query.consumer(consumer_name) if consumer_name + query = query.provider(provider_name) if provider_name + + if tag == :untagged + query = query.untagged + elsif tag + query = query.tag(tag) + end + query.latest.all.collect(&:to_domain_with_content)[0] + end + def find_pact consumer_name, consumer_version, provider_name, pact_version_sha = nil query = if pact_version_sha AllPactPublications diff --git a/lib/pact_broker/pacts/service.rb b/lib/pact_broker/pacts/service.rb index 4ad8525ad..5e5fd38e2 100644 --- a/lib/pact_broker/pacts/service.rb +++ b/lib/pact_broker/pacts/service.rb @@ -17,6 +17,10 @@ def find_latest_pact params pact_repository.find_latest_pact(params[:consumer_name], params[:provider_name], params[:tag]) end + def search_for_latest_pact params + pact_repository.search_for_latest_pact(params[:consumer_name], params[:provider_name], params[:tag]) + end + def find_latest_pacts pact_repository.find_latest_pacts end diff --git a/lib/pact_broker/verifications/placeholder_verification.rb b/lib/pact_broker/verifications/placeholder_verification.rb new file mode 100644 index 000000000..3e165bd19 --- /dev/null +++ b/lib/pact_broker/verifications/placeholder_verification.rb @@ -0,0 +1,23 @@ +module PactBroker + module Verifications + class PlaceholderVerification + attr_accessor :id, :number, :success, + :consumer_name, :provider_name, :provider_version, + :provider_version_number, :build_url, + :execution_date, :created_at, :pact_version_sha + + def initialize + @provider_name = "placeholder-provider" + @consumer_name = "placeholder-consumer" + @number = 1 + @success = true + @pact_version_sha = "5d445a4612743728dfd99ccd4210423c052bb9db" + tags = [OpenStruct.new(name: "master")] + @provider_version = OpenStruct.new(number: "aaaabbbbccccddddeeeeffff1111222233334444", tags: tags) + @provider_version_number = @provider_version.number + @execution_date = DateTime.now + @created_at = DateTime.now + end + end + end +end diff --git a/lib/pact_broker/verifications/repository.rb b/lib/pact_broker/verifications/repository.rb index b82d4c4f1..1482783c1 100644 --- a/lib/pact_broker/verifications/repository.rb +++ b/lib/pact_broker/verifications/repository.rb @@ -37,6 +37,15 @@ def find consumer_name, provider_name, pact_version_sha, verification_number .verification_number(verification_number).single_record end + def search_for_latest consumer_name, provider_name + query = LatestVerificationsByConsumerVersion + .select_all_qualified + .join(:all_pact_publications, pact_version_id: :pact_version_id) + query = query.consumer(consumer_name) if consumer_name + query = query.provider(provider_name) if provider_name + query.reverse(:execution_date, :id).first + end + def find_latest_verifications_for_consumer_version consumer_name, consumer_version_number # Use LatestPactPublicationsByConsumerVersion not AllPactPublcations because we don't # want verifications for shadowed revisions as it would be misleading. diff --git a/lib/pact_broker/verifications/service.rb b/lib/pact_broker/verifications/service.rb index 6b0e8d591..212a909c5 100644 --- a/lib/pact_broker/verifications/service.rb +++ b/lib/pact_broker/verifications/service.rb @@ -53,6 +53,10 @@ def find_latest_verification_for_tags consumer_name, provider_name, consumer_ver verification_repository.find_latest_verification_for_tags(consumer_name, provider_name, consumer_version_tag_name, provider_version_tag_name) end + def search_for_latest consumer_name, provider_name + verification_repository.search_for_latest(consumer_name, provider_name) + end + def verification_summary_for_consumer_version params verifications = find_latest_verifications_for_consumer_version(params) pacts = pact_service.find_by_consumer_version(params) diff --git a/lib/pact_broker/webhooks/service.rb b/lib/pact_broker/webhooks/service.rb index 3cba55d6b..6784a1aab 100644 --- a/lib/pact_broker/webhooks/service.rb +++ b/lib/pact_broker/webhooks/service.rb @@ -1,4 +1,5 @@ require 'pact_broker/repositories' +require 'pact_broker/services' require 'pact_broker/logging' require 'base64' require 'securerandom' @@ -6,6 +7,8 @@ require 'pact_broker/webhooks/triggered_webhook' require 'pact_broker/webhooks/status' require 'pact_broker/webhooks/webhook_event' +require 'pact_broker/verifications/placeholder_verification' +require 'pact_broker/pacts/placeholder_pact' module PactBroker @@ -16,6 +19,7 @@ class Service USER = PactBroker::Webhooks::TriggeredWebhook::TRIGGER_TYPE_USER extend Repositories + extend Services include Logging def self.next_uuid @@ -59,6 +63,17 @@ def self.find_all webhook_repository.find_all end + def self.test_execution webhook + options = { failure_log_message: "Webhook execution failed", show_response: PactBroker.configuration.show_webhook_response?} + verification = nil + if webhook.trigger_on_provider_verification_published? + verification = verification_service.search_for_latest(webhook.consumer_name, webhook.provider_name) || PactBroker::Verifications::PlaceholderVerification.new + end + + pact = pact_service.search_for_latest_pact(consumer_name: webhook.consumer_name, provider_name: webhook.provider_name) || PactBroker::Pacts::PlaceholderPact.new + webhook.execute(pact, verification, options) + end + def self.execute_webhook_now webhook, pact, verification = nil triggered_webhook = webhook_repository.create_triggered_webhook(next_uuid, webhook, pact, verification, USER) options = { failure_log_message: "Webhook execution failed"} diff --git a/script/seed.rb b/script/seed.rb index 0ee9985e1..07a785430 100755 --- a/script/seed.rb +++ b/script/seed.rb @@ -8,54 +8,46 @@ ENV['RACK_ENV'] = 'development' require 'sequel' require 'logger' -DATABASE_CREDENTIALS = {logger: Logger.new($stdout), adapter: "sqlite", database: ARGV[0], :encoding => 'utf8'} +DATABASE_CREDENTIALS = {logger: Logger.new($stdout), adapter: "sqlite", database: ARGV[0], :encoding => 'utf8'}.tap { |it| puts it } #DATABASE_CREDENTIALS = {adapter: "postgres", database: "pact_broker", username: 'pact_broker', password: 'pact_broker', :encoding => 'utf8'} connection = Sequel.connect(DATABASE_CREDENTIALS) connection.timezone = :utc +# Uncomment these lines to open a pry session for inspecting the database +# require 'table_print' +# require 'pry'; pry(binding); +# exit + require 'pact_broker/db' PactBroker::DB.connection = connection require 'pact_broker' require 'support/test_data_builder' -# Uncomment these lines to open a pry session for inspecting the database -# require 'table_print' -# require 'pry'; pry(binding); -# exit require 'database/table_dependency_calculator' PactBroker::Database::TableDependencyCalculator.call(connection).each do | table_name | connection[table_name].delete end -class TestDataBuilder - def method_missing *args - self - end - - def publish_pact params = {} - create_pact params - end -end - -# latest verifications -# TestDataBuilder.new -# .create_consumer("Foo") -# .create_provider("Bar") -# .create_consumer_version("1.2.3") -# .create_pact -# .create_verification(provider_version: "4.5.6", success: true) -# .create_provider("Wiffle") -# .create_pact -# .create_verification(provider_version: "5.6.7", success: false) -# .create_provider("Meep") -# .create_pact + # .create_webhook(method: 'GET', url: 'https://localhost:9393?url=${pactbroker.pactUrl}', body: '${pactbroker.pactUrl}') +webhook_body = { + 'pactUrl' => '${pactbroker.pactUrl}', + 'verificationResultUrl' => '${pactbroker.verificationResultUrl}', + 'consumerVersionNumber' => '${pactbroker.consumerVersionNumber}', + 'providerVersionNumber' => '${pactbroker.providerVersionNumber}', + 'providerVersionTags' => '${pactbroker.providerVersionTags}', + 'consumerVersionTags' => '${pactbroker.consumerVersionTags}', + 'consumerName' => '${pactbroker.consumerName}', + 'providerName' => '${pactbroker.providerName}', + 'githubVerificationStatus' => '${pactbroker.githubVerificationStatus}' +} - # .create_webhook(method: 'GET', url: 'https://localhost:9393?url=${pactbroker.pactUrl}', body: '${pactbroker.pactUrl}') TestDataBuilder.new .create_global_webhook(method: 'GET', url: "http://example.org?consumer=${pactbroker.consumerName}&provider=${pactbroker.providerName}") .create_certificate(path: 'spec/fixtures/certificates/self-signed.badssl.com.pem') + .create_consumer("Bethtest") + .create_verification_webhook(method: 'POST', url: "http://localhost:9292", body: webhook_body) .create_consumer("Foo") .create_label("microservice") .create_provider("Bar") @@ -65,33 +57,33 @@ def publish_pact params = {} .create_provider_webhook(method: 'GET', url: 'https://theage.com.au') .create_webhook(method: 'GET', url: 'https://self-signed.badssl.com') .create_consumer_version("1.2.100") - .publish_pact + .create_pact .create_verification(provider_version: "1.4.234", success: true, execution_date: DateTime.now - 15) .revise_pact .create_consumer_version("1.2.101") .create_consumer_version_tag('prod') - .publish_pact + .create_pact .create_verification(provider_version: "9.9.10", success: false, execution_date: DateTime.now - 15) .create_consumer_version("1.2.102") - .publish_pact(created_at: (Date.today - 7).to_datetime) + .create_pact(created_at: (Date.today - 7).to_datetime) .create_verification(provider_version: "9.9.9", success: true, execution_date: DateTime.now - 14) .create_provider("Animals") .create_webhook(method: 'GET', url: 'http://localhost:9393/') - .publish_pact(created_at: (Time.now - 140).to_datetime) + .create_pact(created_at: (Time.now - 140).to_datetime) .create_verification(provider_version: "2.0.366", execution_date: Date.today - 2) #changed .create_provider("Wiffles") - .publish_pact + .create_pact .create_verification(provider_version: "3.6.100", success: false, execution_date: Date.today - 7) .create_provider("Hello World App") .create_consumer_version("1.2.107") - .publish_pact(created_at: (Date.today - 1).to_datetime) + .create_pact(created_at: (Date.today - 1).to_datetime) .create_consumer("The Android App") .create_provider("The back end") .create_webhook(method: 'GET', url: 'http://localhost:9393/') .create_consumer_version("1.2.106") .create_consumer_version_tag("production") .create_consumer_version_tag("feat-x") - .publish_pact + .create_pact .create_consumer("Some other app") .create_provider("A service") .create_webhook(method: 'GET', url: 'http://localhost:9393/') @@ -101,20 +93,5 @@ def publish_pact params = {} .create_triggered_webhook(status: 'failure') .create_webhook_execution .create_consumer_version("1.2.106") - .publish_pact(created_at: (Date.today - 26).to_datetime) + .create_pact(created_at: (Date.today - 26).to_datetime) .create_verification(provider_version: "4.8.152", execution_date: DateTime.now) - -# TestDataBuilder.new -# .create_pact_with_hierarchy("A", "1", "B") -# .create_consumer_version_tag("master") -# .create_consumer_version_tag("prod") -# .create_verification(provider_version: "1") -# .create_consumer_version("2") -# .create_consumer_version_tag("master") -# .create_pact -# .create_verification(provider_version: "2") - -# TestDataBuilder.new -# .create_pact_with_hierarchy("Foo", "1", "Bar") -# .create_webhook(method: 'GET', url: 'http://localhost:9393', events: [{ name: 'provider_verification_published' }, {name: ''}]) - diff --git a/spec/features/execute_webhook_spec.rb b/spec/features/execute_webhook_spec.rb index af199572c..1fc1080d6 100644 --- a/spec/features/execute_webhook_spec.rb +++ b/spec/features/execute_webhook_spec.rb @@ -36,12 +36,12 @@ expect(subject.status).to be 200 end - it "saves a TriggeredWebhook" do - expect { subject }.to change { PactBroker::Webhooks::TriggeredWebhook.count }.by(1) + it "does not save a TriggeredWebhook" do + expect { subject }.to_not change { PactBroker::Webhooks::TriggeredWebhook.count } end - it "saves a WebhookExecution" do - expect { subject }.to change { PactBroker::Webhooks::Execution.count }.by(1) + it "does not save a WebhookExecution" do + expect { subject }.to_not change { PactBroker::Webhooks::Execution.count } end context "when a webhook host whitelist is not configured" do @@ -52,11 +52,6 @@ it "does not show any response details" do expect(subject.body).to_not include response_body end - - it "does not log any response details" do - subject - expect(PactBroker::Webhooks::Execution.order(:id).last.logs).to_not include response_body - end end context "when a webhook host whitelist is configured" do @@ -67,11 +62,6 @@ it "includes response details" do expect(subject.body).to include response_body end - - it "logs the response details" do - subject - expect(PactBroker::Webhooks::Execution.order(:id).last.logs).to include response_body - end end end @@ -98,12 +88,12 @@ expect(subject.status).to be 500 end - it "saves a TriggeredWebhook" do - expect { subject }.to change { PactBroker::Webhooks::TriggeredWebhook.count }.by(1) + it "does not save a TriggeredWebhook" do + expect { subject }.to_not change { PactBroker::Webhooks::TriggeredWebhook.count } end - it "saves a WebhookExecution" do - expect { subject }.to change { PactBroker::Webhooks::Execution.count }.by(1) + it "does not save a WebhookExecution" do + expect { subject }.to_not change { PactBroker::Webhooks::Execution.count } end end end diff --git a/spec/lib/pact_broker/api/resources/webhook_execution_spec.rb b/spec/lib/pact_broker/api/resources/webhook_execution_spec.rb index ab220c838..a230ed963 100644 --- a/spec/lib/pact_broker/api/resources/webhook_execution_spec.rb +++ b/spec/lib/pact_broker/api/resources/webhook_execution_spec.rb @@ -35,18 +35,12 @@ module Resources let(:provider_name) { "bar" } before do - allow(PactBroker::Webhooks::Service).to receive(:execute_webhook_now).and_return(execution_result) + allow(PactBroker::Webhooks::Service).to receive(:test_execution).and_return(execution_result) allow(PactBroker::Api::Decorators::WebhookExecutionResultDecorator).to receive(:new).and_return(decorator) - allow(PactBroker::Pacts::Service).to receive(:find_latest_pact).and_return(pact) - end - - it "finds the latest pact for the webhook" do - expect(PactBroker::Pacts::Service).to receive(:find_latest_pact).with(consumer_name: consumer_name, provider_name: provider_name) - subject end it "executes the webhook" do - expect(PactBroker::Webhooks::Service).to receive(:execute_webhook_now).with(webhook, pact) + expect(PactBroker::Webhooks::Service).to receive(:test_execution).with(webhook) subject end diff --git a/spec/lib/pact_broker/pacts/repository_spec.rb b/spec/lib/pact_broker/pacts/repository_spec.rb index 114b0dd1c..153ce6240 100644 --- a/spec/lib/pact_broker/pacts/repository_spec.rb +++ b/spec/lib/pact_broker/pacts/repository_spec.rb @@ -697,6 +697,45 @@ module Pacts end end + describe "search_for_latest_pact" do + context "when one or more versions of a pact exist without any tags" do + before do + TestDataBuilder.new + .create_consumer("Consumer") + .create_provider("Provider") + .create_consumer_version("1") + .create_pact + .create_provider("Another Provider") + .create_consumer_version("2") + .create_pact + end + + context "with both consumer and provider names" do + let(:pact) { Repository.new.search_for_latest_pact("Consumer", "Provider") } + + it "returns the latest" do + expect(pact.consumer_version.number).to eq("1") + end + end + + context "with only consumer name" do + let(:pact) { Repository.new.search_for_latest_pact("Consumer", nil) } + + it "returns the latest" do + expect(pact.consumer_version.number).to eq("2") + end + end + + context "with only provider name" do + let(:pact) { Repository.new.search_for_latest_pact(nil, "Another Provider") } + + it "returns the latest" do + expect(pact.consumer_version.number).to eq("2") + end + end + end + end + describe "find_latest_pacts" do before do TestDataBuilder.new @@ -736,7 +775,6 @@ module Pacts it "includes the timestamps - need to update view" do pacts = Repository.new.find_latest_pacts - expect(pacts[0].created_at).to be_datey end end diff --git a/spec/lib/pact_broker/verifications/repository_spec.rb b/spec/lib/pact_broker/verifications/repository_spec.rb index f20a02a7c..435505eff 100644 --- a/spec/lib/pact_broker/verifications/repository_spec.rb +++ b/spec/lib/pact_broker/verifications/repository_spec.rb @@ -71,6 +71,42 @@ module Verifications end end + describe "#search_for_latest" do + before do + TestDataBuilder.new + .create_pact_with_hierarchy("Foo", "1", "Bar") + .create_verification(provider_version: "2") + .create_verification(provider_version: "3", number: 2) + .create_provider("Wiffle") + .create_pact + .create_verification(provider_version: "4") + end + + context "with just the consumer name" do + subject { Repository.new.search_for_latest("Foo", nil) } + + its(:provider_version_number) { is_expected.to eq "4" } + end + + context "with the consumer and provider name" do + subject { Repository.new.search_for_latest("Foo", "Bar") } + + its(:provider_version_number) { is_expected.to eq "3" } + end + + context "with just the provider name" do + subject { Repository.new.search_for_latest(nil, "Bar") } + + its(:provider_version_number) { is_expected.to eq "3" } + end + + context "with neither name" do + subject { Repository.new.search_for_latest(nil, nil) } + + its(:provider_version_number) { is_expected.to eq "4" } + end + end + describe "#find_latest_verification_for" do context "when there is a revision" do before do diff --git a/spec/lib/pact_broker/webhooks/render_spec.rb b/spec/lib/pact_broker/webhooks/render_spec.rb index 15ca02992..1d1d1a184 100644 --- a/spec/lib/pact_broker/webhooks/render_spec.rb +++ b/spec/lib/pact_broker/webhooks/render_spec.rb @@ -1,4 +1,6 @@ require 'pact_broker/webhooks/render' +require 'pact_broker/pacts/placeholder_pact' +require 'pact_broker/verifications/placeholder_verification' require 'cgi' module PactBroker @@ -115,6 +117,19 @@ module Webhooks it { is_expected.to eq "http%3A%2F%2Ffoo" } end end + + describe "#call with placeholder domain objects" do + let(:placeholder_pact) { PactBroker::Pacts::PlaceholderPact.new } + let(:placeholder_verification) { PactBroker::Verifications::PlaceholderVerification.new } + + it "does not blow up with a placeholder pact" do + Render.call("", placeholder_pact) + end + + it "does not blow up with a placeholder verification" do + Render.call("", placeholder_pact, placeholder_verification) + end + end end end end diff --git a/spec/lib/pact_broker/webhooks/service_spec.rb b/spec/lib/pact_broker/webhooks/service_spec.rb index 48480e6d2..5bf8d29d3 100644 --- a/spec/lib/pact_broker/webhooks/service_spec.rb +++ b/spec/lib/pact_broker/webhooks/service_spec.rb @@ -84,6 +84,83 @@ module Webhooks end end + describe ".test_execution" do + let(:webhook) do + instance_double(PactBroker::Domain::Webhook, + trigger_on_provider_verification_published?: trigger_on_verification, + consumer_name: 'consumer', + provider_name: 'provider', + execute: result + ) + end + let(:pact) { instance_double(PactBroker::Domain::Pact) } + let(:verification) { instance_double(PactBroker::Domain::Verification) } + let(:trigger_on_verification) { false } + let(:result) { double('result') } + let(:options) do + { + failure_log_message: "Webhook execution failed", + show_response: 'foo' + } + end + + before do + allow(PactBroker::Pacts::Service).to receive(:search_for_latest_pact).and_return(pact) + allow(PactBroker::Verifications::Service).to receive(:search_for_latest).and_return(verification) + allow(PactBroker.configuration).to receive(:show_webhook_response?).and_return('foo') + end + + subject { Service.test_execution(webhook) } + + it "searches for the latest matching pact" do + expect(PactBroker::Pacts::Service).to receive(:search_for_latest_pact).with(consumer_name: 'consumer', provider_name: 'provider') + subject + end + + it "returns the result" do + expect(subject).to be result + end + + context "when the trigger is not for a verification" do + it "executes the webhook with the pact" do + expect(webhook).to receive(:execute).with(pact, nil, options) + subject + end + end + + context "when a pact cannot be found" do + let(:pact) { nil } + + it "executes the webhook with a placeholder pact" do + expect(webhook).to receive(:execute).with(an_instance_of(PactBroker::Pacts::PlaceholderPact), anything, anything) + subject + end + end + + context "when the trigger is for a verification publication" do + let(:trigger_on_verification) { true } + + it "searches for the latest matching verification" do + expect(PactBroker::Verifications::Service).to receive(:search_for_latest).with('consumer', 'provider') + subject + end + + it "executes the webhook with the pact and the verification" do + expect(webhook).to receive(:execute).with(pact, verification, options) + subject + end + + context "when a verification cannot be found" do + let(:verification) { nil } + + it "executes the webhook with a placeholder verification" do + expect(webhook).to receive(:execute).with(anything, an_instance_of(PactBroker::Verifications::PlaceholderVerification), anything) + subject + end + end + end + end + describe ".execute_webhook_now integration test" do let(:td) { TestDataBuilder.new }