From 86bc1ec0c8ed97b6d1e81fd2b78d2376197531b8 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sat, 9 Jun 2018 12:43:10 +1000 Subject: [PATCH] feat: add ${pactbroker.providerVersionNumber} to webhook templates --- ...8_add_verification_to_triggered_webhook.rb | 7 +++ lib/pact_broker/doc/views/webhooks.markdown | 1 + lib/pact_broker/domain/webhook.rb | 4 +- lib/pact_broker/domain/webhook_request.rb | 24 ++++----- lib/pact_broker/pacts/service.rb | 2 +- lib/pact_broker/verifications/service.rb | 3 +- lib/pact_broker/webhooks/render.rb | 3 +- lib/pact_broker/webhooks/repository.rb | 3 +- lib/pact_broker/webhooks/service.rb | 12 ++--- lib/pact_broker/webhooks/triggered_webhook.rb | 3 +- spec/integration/webhooks/certificate_spec.rb | 2 +- .../domain/webhook_request_spec.rb | 51 ++++++++++--------- spec/lib/pact_broker/domain/webhook_spec.rb | 7 +-- .../pact_broker/verifications/service_spec.rb | 9 ++++ spec/lib/pact_broker/webhooks/render_spec.rb | 27 ++++++++-- .../pact_broker/webhooks/repository_spec.rb | 12 ++++- spec/lib/pact_broker/webhooks/service_spec.rb | 21 ++++++-- spec/support/test_data_builder.rb | 7 ++- 18 files changed, 134 insertions(+), 64 deletions(-) create mode 100644 db/migrations/20180608_add_verification_to_triggered_webhook.rb diff --git a/db/migrations/20180608_add_verification_to_triggered_webhook.rb b/db/migrations/20180608_add_verification_to_triggered_webhook.rb new file mode 100644 index 000000000..9aa7140c3 --- /dev/null +++ b/db/migrations/20180608_add_verification_to_triggered_webhook.rb @@ -0,0 +1,7 @@ +Sequel.migration do + change do + alter_table(:triggered_webhooks) do + add_foreign_key(:verification_id, :verifications) + end + end +end diff --git a/lib/pact_broker/doc/views/webhooks.markdown b/lib/pact_broker/doc/views/webhooks.markdown index 94aee6e2a..67901fcbc 100644 --- a/lib/pact_broker/doc/views/webhooks.markdown +++ b/lib/pact_broker/doc/views/webhooks.markdown @@ -88,6 +88,7 @@ The following variables may be used in the request parameters or body, and will `${pactbroker.pactUrl}`: the "permalink" URL to the newly published pact (the URL specifying the consumer version URL, rather than the "/latest" format.) `${pactbroker.consumerVersionNumber}`: the version number of the most recent consumer version associated with the pact content. +`${pactbroker.providerVersionNumber}`: the provider version number for the verification result Example usage: diff --git a/lib/pact_broker/domain/webhook.rb b/lib/pact_broker/domain/webhook.rb index 54611cf83..b9fab9102 100644 --- a/lib/pact_broker/domain/webhook.rb +++ b/lib/pact_broker/domain/webhook.rb @@ -32,9 +32,9 @@ def request_description request && request.description end - def execute pact, options + def execute pact, verification, options logger.info "Executing #{self}" - request.execute pact, options + request.execute pact, verification, options end def to_s diff --git a/lib/pact_broker/domain/webhook_request.rb b/lib/pact_broker/domain/webhook_request.rb index 663a68bdf..7f9d2b800 100644 --- a/lib/pact_broker/domain/webhook_request.rb +++ b/lib/pact_broker/domain/webhook_request.rb @@ -52,11 +52,11 @@ def display_password password.nil? ? nil : "**********" end - def execute pact, options = {} + def execute pact, verification, options = {} logs = StringIO.new execution_logger = Logger.new(logs) begin - execute_and_build_result(pact, options, logs, execution_logger) + execute_and_build_result(pact, verification, options, logs, execution_logger) rescue StandardError => e handle_error_and_build_result(e, options, logs, execution_logger) end @@ -64,9 +64,9 @@ def execute pact, options = {} private - def execute_and_build_result pact, options, logs, execution_logger - uri = build_uri(pact) - req = build_request(uri, pact, execution_logger) + def execute_and_build_result pact, verification, options, logs, execution_logger + uri = build_uri(pact, verification) + req = build_request(uri, pact, verification, execution_logger) response = do_request(uri, req) log_response(response, execution_logger, options) result = WebhookExecutionResult.new(response, logs.string) @@ -80,9 +80,9 @@ def handle_error_and_build_result e, options, logs, execution_logger WebhookExecutionResult.new(nil, logs.string, e) end - def build_request uri, pact, execution_logger + def build_request uri, pact, verification, execution_logger req = http_request(uri) - execution_logger.info "HTTP/1.1 #{method.upcase} #{url_with_credentials(pact)}" + execution_logger.info "HTTP/1.1 #{method.upcase} #{url_with_credentials(pact, verification)}" headers.each_pair do | name, value | execution_logger.info Webhooks::RedactLogs.call("#{name}: #{value}") @@ -92,7 +92,7 @@ def build_request uri, pact, execution_logger req.basic_auth(username, password) if username unless body.nil? - req.body = PactBroker::Webhooks::Render.call(String === body ? body : body.to_json, pact) + req.body = PactBroker::Webhooks::Render.call(String === body ? body : body.to_json, pact, verification) end execution_logger.info(req.body) if req.body @@ -173,12 +173,12 @@ def http_request(uri) Net::HTTP.const_get(method.capitalize).new(uri) end - def build_uri pact - URI(PactBroker::Webhooks::Render.call(url, pact){ | value | CGI::escape(value)} ) + def build_uri(pact, verification) + URI(PactBroker::Webhooks::Render.call(url, pact, verification){ | value | CGI::escape(value)} ) end - def url_with_credentials pact - u = build_uri(pact) + def url_with_credentials pact, verification + u = build_uri(pact, verification) u.userinfo = "#{CGI::escape username}:#{display_password}" if username u end diff --git a/lib/pact_broker/pacts/service.rb b/lib/pact_broker/pacts/service.rb index d119f0159..45d868947 100644 --- a/lib/pact_broker/pacts/service.rb +++ b/lib/pact_broker/pacts/service.rb @@ -107,7 +107,7 @@ def update_pact params, existing_pact updated_pact = pact_repository.update existing_pact.id, params if existing_pact.json_content != updated_pact.json_content - webhook_service.execute_webhooks updated_pact, PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED + webhook_service.execute_webhooks updated_pact, nil, PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED end updated_pact diff --git a/lib/pact_broker/verifications/service.rb b/lib/pact_broker/verifications/service.rb index eb8c0b5e4..50ec80c67 100644 --- a/lib/pact_broker/verifications/service.rb +++ b/lib/pact_broker/verifications/service.rb @@ -22,8 +22,9 @@ def create next_verification_number, params, pact provider_version_number = params.fetch('providerApplicationVersion') PactBroker::Api::Decorators::VerificationDecorator.new(verification).from_hash(params) verification.number = next_verification_number - webhook_service.execute_webhooks pact, PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED verification = verification_repository.create(verification, provider_version_number, pact) + webhook_service.execute_webhooks pact, verification, PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED + verification end def errors params diff --git a/lib/pact_broker/webhooks/render.rb b/lib/pact_broker/webhooks/render.rb index ec6ff0e97..184ad90b5 100644 --- a/lib/pact_broker/webhooks/render.rb +++ b/lib/pact_broker/webhooks/render.rb @@ -5,7 +5,8 @@ def self.call(body, pact, verification = nil, &escaper) base_url = PactBroker.configuration.base_url params = { '${pactbroker.pactUrl}' => PactBroker::Api::PactBrokerUrls.pact_url(base_url, pact), - '${pactbroker.consumerVersionNumber}' => pact.consumer_version_number + '${pactbroker.consumerVersionNumber}' => pact.consumer_version_number, + '${pactbroker.providerVersionNumber}' => verification ? verification.provider_version_number : "" } if escaper diff --git a/lib/pact_broker/webhooks/repository.rb b/lib/pact_broker/webhooks/repository.rb index c4ab20985..4285a403b 100644 --- a/lib/pact_broker/webhooks/repository.rb +++ b/lib/pact_broker/webhooks/repository.rb @@ -78,11 +78,12 @@ def find_by_consumer_and_provider_existing_at consumer, provider, date_time .collect(&:to_domain) end - def create_triggered_webhook trigger_uuid, webhook, pact, trigger_type + def create_triggered_webhook trigger_uuid, webhook, pact, verification, trigger_type db_webhook = Webhook.where(uuid: webhook.uuid).single_record TriggeredWebhook.create( status: TriggeredWebhook::STATUS_NOT_RUN, pact_publication_id: pact.id, + verification: verification, webhook: db_webhook, webhook_uuid: db_webhook.uuid, trigger_uuid: trigger_uuid, diff --git a/lib/pact_broker/webhooks/service.rb b/lib/pact_broker/webhooks/service.rb index 1e9fb9fc7..2d4f389e8 100644 --- a/lib/pact_broker/webhooks/service.rb +++ b/lib/pact_broker/webhooks/service.rb @@ -59,8 +59,8 @@ def self.find_all webhook_repository.find_all end - def self.execute_webhook_now webhook, pact - triggered_webhook = webhook_repository.create_triggered_webhook(next_uuid, webhook, pact, USER) + 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", show_response: PactBroker.configuration.show_webhook_response?} webhook_execution_result = execute_triggered_webhook_now triggered_webhook, options if webhook_execution_result.success? @@ -85,21 +85,21 @@ def self.find_by_consumer_and_provider consumer, provider webhook_repository.find_by_consumer_and_provider consumer, provider end - def self.execute_webhooks pact, event_name + def self.execute_webhooks pact, verification, event_name webhooks = webhook_repository.find_by_consumer_and_provider_and_event_name pact.consumer, pact.provider, event_name if webhooks.any? - run_later(webhooks, pact, event_name) + run_later(webhooks, pact, verification, event_name) else logger.debug "No webhook found for consumer \"#{pact.consumer.name}\" and provider \"#{pact.provider.name}\"" end end - def self.run_later webhooks, pact, event_name + def self.run_later webhooks, pact, verification, event_name trigger_uuid = next_uuid webhooks.each do | webhook | begin - triggered_webhook = webhook_repository.create_triggered_webhook(trigger_uuid, webhook, pact, RESOURCE_CREATION) + triggered_webhook = webhook_repository.create_triggered_webhook(trigger_uuid, webhook, pact, verification, RESOURCE_CREATION) logger.info "Scheduling job for #{webhook.description} with uuid #{webhook.uuid}" # Bit of a dodgey hack to make sure the request transaction has finished before we execute the webhook Job.perform_in(5, triggered_webhook: triggered_webhook) diff --git a/lib/pact_broker/webhooks/triggered_webhook.rb b/lib/pact_broker/webhooks/triggered_webhook.rb index bdc039c5c..5a2018997 100644 --- a/lib/pact_broker/webhooks/triggered_webhook.rb +++ b/lib/pact_broker/webhooks/triggered_webhook.rb @@ -37,6 +37,7 @@ def not_run associate(:one_to_many, :webhook_executions, :class => "PactBroker::Webhooks::Execution", :key => :triggered_webhook_id, :primary_key => :id, :order => :id) associate(:many_to_one, :webhook, :class => "PactBroker::Webhooks::Webhook", :key => :webhook_id, :primary_key => :id) associate(:many_to_one, :pact_publication, :class => "PactBroker::Pacts::PactPublication", :key => :pact_publication_id, :primary_key => :id) + associate(:many_to_one, :verification, :class => "PactBroker::Domain::Verification", :key => :verification_id, :primary_key => :id) associate(:many_to_one, :provider, :class => "PactBroker::Domain::Pacticipant", :key => :provider_id, :primary_key => :id) associate(:many_to_one, :consumer, :class => "PactBroker::Domain::Pacticipant", :key => :consumer_id, :primary_key => :id) @@ -49,7 +50,7 @@ def execute options # getting a random 'no method to_domain for null' error # not sure on which object, so splitting this out into two lines pact = pact_publication.to_domain - webhook.to_domain.execute(pact, options) + webhook.to_domain.execute(pact, verification, options) end def consumer_name diff --git a/spec/integration/webhooks/certificate_spec.rb b/spec/integration/webhooks/certificate_spec.rb index 419716916..ab3b7db30 100644 --- a/spec/integration/webhooks/certificate_spec.rb +++ b/spec/integration/webhooks/certificate_spec.rb @@ -16,7 +16,7 @@ let(:pact) { td.create_pact_with_hierarchy.and_return(:pact) } - subject { webhook_request.execute(pact) } + subject { webhook_request.execute(pact, nil) } context "without the correct cacert" do it "fails" do diff --git a/spec/lib/pact_broker/domain/webhook_request_spec.rb b/spec/lib/pact_broker/domain/webhook_request_spec.rb index 984752113..7b49bb6c0 100644 --- a/spec/lib/pact_broker/domain/webhook_request_spec.rb +++ b/spec/lib/pact_broker/domain/webhook_request_spec.rb @@ -22,6 +22,8 @@ module Domain let(:options) { {failure_log_message: 'oops', show_response: show_response} } let(:show_response) { true } let(:pact) { instance_double('PactBroker::Domain::Pact') } + let(:verification) { instance_double('PactBroker::Domain::Verification') } + let(:logs) { execute.logs } subject do WebhookRequest.new( @@ -33,7 +35,8 @@ module Domain body: body) end - let(:logs) { subject.execute(pact, options).logs } + let(:execute) { subject.execute(pact, verification, options) } + describe "description" do it "returns a brief description of the HTTP request" do @@ -65,20 +68,20 @@ module Domain let(:request_body) { 'body' } it "renders the url template" do - expect(PactBroker::Webhooks::Render).to receive(:call).with("http://example.org/hook", pact) do | content, pact, verification, &block | + expect(PactBroker::Webhooks::Render).to receive(:call).with("http://example.org/hook", pact, verification) do | content, pact, verification, &block | expect(content).to eq "http://example.org/hook" expect(pact).to be pact - expect(verification).to be nil + expect(verification).to be verification expect(block.call("foo bar")).to eq "foo+bar" "http://example.org/hook" end - subject.execute(pact, options) + execute end context "when the body is a string" do it "renders the body template with the String" do - expect(PactBroker::Webhooks::Render).to receive(:call).with('body', pact) - subject.execute(pact, options) + expect(PactBroker::Webhooks::Render).to receive(:call).with('body', pact, verification) + execute end end @@ -87,20 +90,20 @@ module Domain let(:request_body) { '{"foo":"bar"}' } it "renders the body template with JSON" do - expect(PactBroker::Webhooks::Render).to receive(:call).with(request_body, pact) - subject.execute(pact, options) + expect(PactBroker::Webhooks::Render).to receive(:call).with(request_body, pact, verification) + execute end end it "executes the configured request" do - subject.execute(pact, options) + execute expect(http_request).to have_been_made end it "logs the request" do allow(PactBroker.logger).to receive(:info) expect(PactBroker.logger).to receive(:info).with(/POST.*example.*text.*body/) - subject.execute(pact, options) + execute end it "logs the response" do @@ -109,7 +112,7 @@ module Domain expect(PactBroker.logger).to receive(:info).with(/response.*200/) expect(PactBroker.logger).to receive(:debug).with(/content-type/) expect(PactBroker.logger).to receive(:debug).with(/respbod/) - subject.execute(pact, options) + execute end describe "execution logs" do @@ -204,7 +207,7 @@ module Domain let(:password) { "password" } it "uses the credentials" do - subject.execute(pact, options) + execute expect(http_request_with_basic_auth).to have_been_made end end @@ -214,7 +217,7 @@ module Domain let(:password) { "p@$$w0rd!" } it "uses the credentials" do - subject.execute(pact, options) + execute expect(http_request_with_basic_auth).to have_been_made end end @@ -231,7 +234,7 @@ module Domain end it "uses SSL" do - subject.execute(pact, options) + execute expect(https_request).to have_been_made end end @@ -246,18 +249,18 @@ module Domain end it "executes the request without a body" do - subject.execute(pact, options) + execute expect(http_request).to have_been_made end end context "when the request is successful" do it "returns a WebhookExecutionResult with success=true" do - expect(subject.execute(pact, options).success?).to be true + expect(execute.success?).to be true end it "sets the response on the result" do - expect(subject.execute(pact, options).response).to be_instance_of(Net::HTTPOK) + expect(execute.response).to be_instance_of(Net::HTTPOK) end end @@ -270,11 +273,11 @@ module Domain end it "returns a WebhookExecutionResult with success=false" do - expect(subject.execute(pact, options).success?).to be false + expect(execute.success?).to be false end it "sets the response on the result" do - expect(subject.execute(pact, options).response).to be_instance_of(Net::HTTPInternalServerError) + expect(execute.response).to be_instance_of(Net::HTTPInternalServerError) end end @@ -285,7 +288,7 @@ module Domain end it "removes the non UTF-8 characters before saving the logs so they don't blow up the database" do - result = subject.execute(pact, options) + result = execute expect(result.logs).to include "This has some invalid chars" end @@ -293,7 +296,7 @@ module Domain logger = double("logger").as_null_object allow(Logger).to receive(:new).and_return(logger) expect(logger).to receive(:debug).with(/Note that invalid UTF-8 byte sequences were removed/) - subject.execute(pact, options) + execute end end @@ -308,15 +311,15 @@ class WebhookTestError < StandardError; end it "logs the error" do expect(PactBroker.logger).to receive(:error).with(/Error.*WebhookTestError.*blah/) - subject.execute(pact, options) + execute end it "returns a WebhookExecutionResult with success=false" do - expect(subject.execute(pact, options).success?).to be false + expect(execute.success?).to be false end it "returns a WebhookExecutionResult with an error" do - expect(subject.execute(pact, options).error).to be_instance_of WebhookTestError + expect(execute.error).to be_instance_of WebhookTestError end it "logs the failure_log_message" do diff --git a/spec/lib/pact_broker/domain/webhook_spec.rb b/spec/lib/pact_broker/domain/webhook_spec.rb index 82d56399b..e415b4c4a 100644 --- a/spec/lib/pact_broker/domain/webhook_spec.rb +++ b/spec/lib/pact_broker/domain/webhook_spec.rb @@ -12,6 +12,7 @@ module Domain let(:request) { instance_double(PactBroker::Domain::WebhookRequest, execute: nil)} let(:options) { double('options') } let(:pact) { double('pact') } + let(:verification) { double('verification') } subject { Webhook.new(request: request, consumer: consumer, provider: provider,) } @@ -24,14 +25,14 @@ module Domain describe "execute" do it "executes the request" do - expect(request).to receive(:execute).with(pact, options) - subject.execute pact, options + expect(request).to receive(:execute).with(pact, verification, options) + subject.execute pact, verification, options end it "logs before and after" do allow(PactBroker.logger).to receive(:info) expect(PactBroker.logger).to receive(:info).with(/Executing/) - subject.execute pact, options + subject.execute pact, verification, options end end end diff --git a/spec/lib/pact_broker/verifications/service_spec.rb b/spec/lib/pact_broker/verifications/service_spec.rb index 3fa4909cd..f924174aa 100644 --- a/spec/lib/pact_broker/verifications/service_spec.rb +++ b/spec/lib/pact_broker/verifications/service_spec.rb @@ -9,6 +9,10 @@ module Verifications subject { PactBroker::Verifications::Service } describe "#create" do + before do + allow(PactBroker::Webhooks::Service).to receive(:execute_webhooks) + end + let(:params) { {'success' => true, 'providerApplicationVersion' => '4.5.6'} } let(:pact) { TestDataBuilder.new.create_pact_with_hierarchy.and_return(:pact) } let(:create_verification) { subject.create 3, params, pact } @@ -36,6 +40,11 @@ module Verifications expect(verification.provider_version).to_not be nil expect(verification.provider_version_number).to eq '4.5.6' end + + it "invokes the webhooks for the verification" do + verification = create_verification + expect(PactBroker::Webhooks::Service).to have_received(:execute_webhooks).with(pact, verification, PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED) + end end describe "#errors" do diff --git a/spec/lib/pact_broker/webhooks/render_spec.rb b/spec/lib/pact_broker/webhooks/render_spec.rb index ae79bb08c..c70d879c0 100644 --- a/spec/lib/pact_broker/webhooks/render_spec.rb +++ b/spec/lib/pact_broker/webhooks/render_spec.rb @@ -10,25 +10,42 @@ module Webhooks end let(:body) do - "Foo ${pactbroker.pactUrl} ${pactbroker.consumerVersionNumber}" + "Foo ${pactbroker.pactUrl} ${pactbroker.consumerVersionNumber} ${pactbroker.providerVersionNumber}" end let(:pact) do instance_double("pact", consumer_version_number: "1.2.3+foo") end - subject { Render.call(body, pact, nil) } + let(:verification) do + instance_double("verification", provider_version_number: "3") + end + + subject { Render.call(body, pact, verification) } + + it { is_expected.to eq "Foo http://foo 1.2.3+foo 3" } + + + context "when the verification is nil" do + let(:verification) { nil } - it { is_expected.to eq "Foo http://foo 1.2.3+foo" } + let(:body) do + "${pactbroker.providerVersionNumber}" + end + + it "inserts an empty string" do + expect(subject).to eq "" + end + end context "with an escaper" do subject do - Render.call(body, pact, nil) do | value | + Render.call(body, pact, verification) do | value | CGI.escape(value) end end - it { is_expected.to eq "Foo http%3A%2F%2Ffoo 1.2.3%2Bfoo" } + it { is_expected.to eq "Foo http%3A%2F%2Ffoo 1.2.3%2Bfoo 3" } end end end diff --git a/spec/lib/pact_broker/webhooks/repository_spec.rb b/spec/lib/pact_broker/webhooks/repository_spec.rb index 1cb045e2c..5c9aa545b 100644 --- a/spec/lib/pact_broker/webhooks/repository_spec.rb +++ b/spec/lib/pact_broker/webhooks/repository_spec.rb @@ -333,14 +333,16 @@ module Webhooks .create_webhook .create_consumer_version .create_pact + .create_verification end - subject { Repository.new.create_triggered_webhook '1234', td.webhook, td.pact, 'publication' } + subject { Repository.new.create_triggered_webhook '1234', td.webhook, td.pact, td.verification, 'publication' } it "creates a TriggeredWebhook" do expect(subject.webhook_uuid ).to eq td.webhook.uuid expect(subject.consumer).to eq td.consumer expect(subject.provider).to eq td.provider + expect(subject.verification).to eq td.verification expect(subject.trigger_uuid).to eq '1234' expect(subject.trigger_type).to eq 'publication' end @@ -364,6 +366,14 @@ module Webhooks it "sets the PactPublication" do expect(subject.pact_publication.id).to eq td.pact.id end + + context "without a verification" do + subject { Repository.new.create_triggered_webhook '1234', td.webhook, td.pact, nil, 'publication' } + + it "does not set the verification" do + expect(subject.verification).to be nil + end + end end describe "create_execution" do diff --git a/spec/lib/pact_broker/webhooks/service_spec.rb b/spec/lib/pact_broker/webhooks/service_spec.rb index 7753b879a..8b824600c 100644 --- a/spec/lib/pact_broker/webhooks/service_spec.rb +++ b/spec/lib/pact_broker/webhooks/service_spec.rb @@ -30,6 +30,7 @@ module Webhooks describe ".execute_webhooks" do + let(:verification) { instance_double(PactBroker::Domain::Verification)} let(:pact) { instance_double(PactBroker::Domain::Pact, consumer: consumer, provider: provider, consumer_version: consumer_version)} let(:consumer_version) { PactBroker::Domain::Version.new(number: '1.2.3') } let(:consumer) { PactBroker::Domain::Pacticipant.new(name: 'Consumer') } @@ -43,7 +44,7 @@ module Webhooks allow(Job).to receive(:perform_in) end - subject { Service.execute_webhooks pact, PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED } + subject { Service.execute_webhooks pact, verification, PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED } it "finds the webhooks" do expect_any_instance_of(PactBroker::Webhooks::Repository).to receive(:find_by_consumer_and_provider_and_event_name).with(consumer, provider, PactBroker::Webhooks::WebhookEvent::DEFAULT_EVENT_NAME) @@ -52,7 +53,7 @@ module Webhooks context "when webhooks are found" do it "executes the webhook" do - expect(Service).to receive(:run_later).with(webhooks, pact, PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED) + expect(Service).to receive(:run_later).with(webhooks, pact, verification, PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED) subject end end @@ -97,10 +98,11 @@ module Webhooks .create_consumer_version .create_pact .create_webhook(method: 'GET', url: 'http://example.org') + .create_verification .and_return(:pact) end - subject { PactBroker::Webhooks::Service.execute_webhook_now td.webhook, pact } + subject { PactBroker::Webhooks::Service.execute_webhook_now td.webhook, pact, td.verification } it "executes the triggered webhook with the correct options" do allow(PactBroker.configuration).to receive(:show_webhook_response?).and_return('foo') @@ -118,6 +120,16 @@ module Webhooks expect { subject }.to change { PactBroker::Webhooks::TriggeredWebhook.count }.by(1) end + it "saves the pact" do + subject + expect(PactBroker::Webhooks::TriggeredWebhook.order(:id).last.pact_publication_id).to_not be nil + end + + it "saves the verification" do + subject + expect(PactBroker::Webhooks::TriggeredWebhook.order(:id).last.verification_id).to_not be nil + end + it "saves the execution" do expect { subject }.to change { PactBroker::Webhooks::Execution.count }.by(1) end @@ -141,11 +153,12 @@ module Webhooks .create_provider .create_consumer_version .create_pact + .create_verification .create_webhook(method: 'GET', url: 'http://example.org', events: events) .and_return(:pact) end - subject { PactBroker::Webhooks::Service.execute_webhooks pact, PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED } + subject { PactBroker::Webhooks::Service.execute_webhooks pact, td.verification, PactBroker::Webhooks::WebhookEvent::CONTRACT_CONTENT_CHANGED } it "executes the HTTP request of the webhook" do subject diff --git a/spec/support/test_data_builder.rb b/spec/support/test_data_builder.rb index 87432f520..bbebbe547 100644 --- a/spec/support/test_data_builder.rb +++ b/spec/support/test_data_builder.rb @@ -35,6 +35,7 @@ class TestDataBuilder attr_reader :provider attr_reader :consumer_version attr_reader :pact + attr_reader :verification attr_reader :webhook attr_reader :webhook_execution attr_reader :triggered_webhook @@ -254,10 +255,14 @@ def create_webhook params = {} self end + def create_verification_webhook params = {} + create_webhook params.merge(events: [{ name: PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED }]) + end + def create_triggered_webhook params = {} params.delete(:comment) trigger_uuid = params[:trigger_uuid] || webhook_service.next_uuid - @triggered_webhook = webhook_repository.create_triggered_webhook trigger_uuid, @webhook, @pact, PactBroker::Webhooks::Service::RESOURCE_CREATION + @triggered_webhook = webhook_repository.create_triggered_webhook trigger_uuid, @webhook, @pact, nil, PactBroker::Webhooks::Service::RESOURCE_CREATION @triggered_webhook.update(status: params[:status]) if params[:status] set_created_at_if_set params[:created_at], :triggered_webhooks, {id: @triggered_webhook.id} self