From 118bbee1eb93b0e2d8ef572282798662feb45b37 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 6 Jun 2019 14:10:16 +1000 Subject: [PATCH] fix: correctly remove webhook consumer/provider when update params do not contain a consumer/provider --- lib/pact_broker/webhooks/repository.rb | 4 +-- .../pact_broker/webhooks/repository_spec.rb | 32 +++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/pact_broker/webhooks/repository.rb b/lib/pact_broker/webhooks/repository.rb index bc7a2e18e..15c8b42d8 100644 --- a/lib/pact_broker/webhooks/repository.rb +++ b/lib/pact_broker/webhooks/repository.rb @@ -31,8 +31,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.consumer_id = webhook.consumer ? pacticipant_repository.find_by_name(webhook.consumer.name).id : nil + existing_webhook.provider_id = webhook.provider ? pacticipant_repository.find_by_name(webhook.provider.name).id : nil existing_webhook.update_from_domain(webhook).save existing_webhook.events.collect(&:delete) (webhook.events || []).each do | webhook_event | diff --git a/spec/lib/pact_broker/webhooks/repository_spec.rb b/spec/lib/pact_broker/webhooks/repository_spec.rb index 4cdd20fc8..427facf2b 100644 --- a/spec/lib/pact_broker/webhooks/repository_spec.rb +++ b/spec/lib/pact_broker/webhooks/repository_spec.rb @@ -228,16 +228,28 @@ module Webhooks subject { Repository.new.update_by_uuid(uuid, new_webhook) } it "updates the webhook" do - updated_webhook = subject - expect(updated_webhook.uuid).to eq uuid - expect(updated_webhook.request.method).to eq 'GET' - expect(updated_webhook.request.url).to eq 'http://example.com' - expect(updated_webhook.request.body).to eq 'foo' - expect(updated_webhook.request.headers).to eq 'Content-Type' => 'text/plain' - 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" + expect(subject.uuid).to eq uuid + expect(subject.request.method).to eq 'GET' + expect(subject.request.url).to eq 'http://example.com' + expect(subject.request.body).to eq 'foo' + expect(subject.request.headers).to eq 'Content-Type' => 'text/plain' + expect(subject.request.username).to eq nil + expect(subject.request.password).to eq nil + expect(subject.events.first.name).to eq 'something_else' + expect(subject.consumer.name).to eq "Foo2" + end + + context "when the updated params do not contain a consumer or provider" do + let(:new_webhook) do + PactBroker::Domain::Webhook.new( + events: [new_event], + request: new_request + ) + end + + it "removes the existing consumer or provider" do + expect(subject.consumer).to be nil + end end end