Skip to content

Commit 118bbee

Browse files
committed
fix: correctly remove webhook consumer/provider when update params do not contain a consumer/provider
1 parent 481e045 commit 118bbee

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

lib/pact_broker/webhooks/repository.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def find_by_uuid uuid
3131

3232
def update_by_uuid uuid, webhook
3333
existing_webhook = Webhook.find(uuid: uuid)
34-
existing_webhook.consumer_id = pacticipant_repository.find_by_name(webhook.consumer.name).id if webhook.consumer
35-
existing_webhook.provider_id = pacticipant_repository.find_by_name(webhook.provider.name).id if webhook.provider
34+
existing_webhook.consumer_id = webhook.consumer ? pacticipant_repository.find_by_name(webhook.consumer.name).id : nil
35+
existing_webhook.provider_id = webhook.provider ? pacticipant_repository.find_by_name(webhook.provider.name).id : nil
3636
existing_webhook.update_from_domain(webhook).save
3737
existing_webhook.events.collect(&:delete)
3838
(webhook.events || []).each do | webhook_event |

spec/lib/pact_broker/webhooks/repository_spec.rb

+22-10
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,28 @@ module Webhooks
228228
subject { Repository.new.update_by_uuid(uuid, new_webhook) }
229229

230230
it "updates the webhook" do
231-
updated_webhook = subject
232-
expect(updated_webhook.uuid).to eq uuid
233-
expect(updated_webhook.request.method).to eq 'GET'
234-
expect(updated_webhook.request.url).to eq 'http://example.com'
235-
expect(updated_webhook.request.body).to eq 'foo'
236-
expect(updated_webhook.request.headers).to eq 'Content-Type' => 'text/plain'
237-
expect(updated_webhook.request.username).to eq nil
238-
expect(updated_webhook.request.password).to eq nil
239-
expect(updated_webhook.events.first.name).to eq 'something_else'
240-
expect(updated_webhook.consumer.name).to eq "Foo2"
231+
expect(subject.uuid).to eq uuid
232+
expect(subject.request.method).to eq 'GET'
233+
expect(subject.request.url).to eq 'http://example.com'
234+
expect(subject.request.body).to eq 'foo'
235+
expect(subject.request.headers).to eq 'Content-Type' => 'text/plain'
236+
expect(subject.request.username).to eq nil
237+
expect(subject.request.password).to eq nil
238+
expect(subject.events.first.name).to eq 'something_else'
239+
expect(subject.consumer.name).to eq "Foo2"
240+
end
241+
242+
context "when the updated params do not contain a consumer or provider" do
243+
let(:new_webhook) do
244+
PactBroker::Domain::Webhook.new(
245+
events: [new_event],
246+
request: new_request
247+
)
248+
end
249+
250+
it "removes the existing consumer or provider" do
251+
expect(subject.consumer).to be nil
252+
end
241253
end
242254
end
243255

0 commit comments

Comments
 (0)