From d865a42990caab8eae82916737dac03e251a83a8 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 27 May 2019 16:46:57 +1000 Subject: [PATCH] fix(webhooks): correctly set updated consumer and provider --- lib/pact_broker/webhooks/repository.rb | 2 + spec/features/update_webhook_spec.rb | 47 +++++++++++++++++++ .../pact_broker/webhooks/repository_spec.rb | 23 ++++++--- 3 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 spec/features/update_webhook_spec.rb diff --git a/lib/pact_broker/webhooks/repository.rb b/lib/pact_broker/webhooks/repository.rb index f55c6d43f..bda7f99bb 100644 --- a/lib/pact_broker/webhooks/repository.rb +++ b/lib/pact_broker/webhooks/repository.rb @@ -34,6 +34,8 @@ def find_by_uuid uuid def update_by_uuid uuid, webhook existing_webhook = Webhook.find(uuid: uuid) + existing_webhook.consumer_id = pacticipant_repository.find_by_name(webhook.consumer.name).id if webhook.consumer + existing_webhook.provider_id = pacticipant_repository.find_by_name(webhook.provider.name).id if webhook.provider existing_webhook.update_from_domain(webhook).save existing_webhook.headers.collect(&:delete) existing_webhook.events.collect(&:delete) diff --git a/spec/features/update_webhook_spec.rb b/spec/features/update_webhook_spec.rb new file mode 100644 index 000000000..a8e9c39ad --- /dev/null +++ b/spec/features/update_webhook_spec.rb @@ -0,0 +1,47 @@ +require 'support/test_data_builder' + +describe "Updating a webhook" do + let(:webhook) do + TestDataBuilder.new + .create_pact_with_hierarchy("Some Consumer", "1", "Some Provider") + .create_global_webhook + .and_return(:webhook) + end + + let(:headers) { {'CONTENT_TYPE' => 'application/json'} } + let(:response_body) { JSON.parse(last_response.body, symbolize_names: true)} + let(:webhook_json) { webhook_hash.to_json } + let(:webhook_hash) do + { + description: "trigger build", + consumer: { + name: "Some Consumer" + }, + enabled: false, + events: [{ + name: 'contract_published' + }], + request: { + method: 'POST', + url: 'https://example.org', + headers: { + :"Content-Type" => "application/json" + }, + body: { + a: 'body' + } + } + } + end + + subject { put(path, webhook_json, headers) } + + let(:path) { "/webhooks/#{webhook.uuid}" } + + context "with valid attributes" do + it "returns the newly created webhook" do + subject + expect(response_body).to include description: "trigger build" + end + end +end diff --git a/spec/lib/pact_broker/webhooks/repository_spec.rb b/spec/lib/pact_broker/webhooks/repository_spec.rb index a35502d31..572b6f463 100644 --- a/spec/lib/pact_broker/webhooks/repository_spec.rb +++ b/spec/lib/pact_broker/webhooks/repository_spec.rb @@ -196,6 +196,13 @@ module Webhooks end describe "update_by_uuid" do + before do + td.create_consumer("Foo") + .create_provider + .create_webhook(old_webhook_params) + .create_consumer("Foo2") + end + let(:uuid) { '1234' } let(:old_webhook_params) do { @@ -209,7 +216,7 @@ module Webhooks password: 'password' } end - let(:new_webhook_params) do + let(:new_request_webhook_params) do { method: 'GET', url: 'http://example.com', @@ -217,16 +224,17 @@ module Webhooks headers: {'Content-Type' => 'text/plain'} } end + let(:new_request) { PactBroker::Domain::WebhookRequest.new(new_request_webhook_params) } let(:new_event) do PactBroker::Webhooks::WebhookEvent.new(name: 'something_else') end - before do - td.create_consumer - .create_provider - .create_webhook(old_webhook_params) - end + let(:new_consumer) { PactBroker::Domain::Pacticipant.new(name: "Foo2") } let(:new_webhook) do - PactBroker::Domain::Webhook.new(events: [new_event], request: PactBroker::Domain::WebhookRequest.new(new_webhook_params)) + PactBroker::Domain::Webhook.new( + consumer: new_consumer, + events: [new_event], + request: new_request + ) end subject { Repository.new.update_by_uuid(uuid, new_webhook) } @@ -241,6 +249,7 @@ module Webhooks expect(updated_webhook.request.username).to eq nil expect(updated_webhook.request.password).to eq nil expect(updated_webhook.events.first.name).to eq 'something_else' + expect(updated_webhook.consumer.name).to eq "Foo2" end end