Skip to content

Commit d865a42

Browse files
committed
fix(webhooks): correctly set updated consumer and provider
1 parent 9327271 commit d865a42

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

lib/pact_broker/webhooks/repository.rb

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def find_by_uuid uuid
3434

3535
def update_by_uuid uuid, webhook
3636
existing_webhook = Webhook.find(uuid: uuid)
37+
existing_webhook.consumer_id = pacticipant_repository.find_by_name(webhook.consumer.name).id if webhook.consumer
38+
existing_webhook.provider_id = pacticipant_repository.find_by_name(webhook.provider.name).id if webhook.provider
3739
existing_webhook.update_from_domain(webhook).save
3840
existing_webhook.headers.collect(&:delete)
3941
existing_webhook.events.collect(&:delete)

spec/features/update_webhook_spec.rb

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'support/test_data_builder'
2+
3+
describe "Updating a webhook" do
4+
let(:webhook) do
5+
TestDataBuilder.new
6+
.create_pact_with_hierarchy("Some Consumer", "1", "Some Provider")
7+
.create_global_webhook
8+
.and_return(:webhook)
9+
end
10+
11+
let(:headers) { {'CONTENT_TYPE' => 'application/json'} }
12+
let(:response_body) { JSON.parse(last_response.body, symbolize_names: true)}
13+
let(:webhook_json) { webhook_hash.to_json }
14+
let(:webhook_hash) do
15+
{
16+
description: "trigger build",
17+
consumer: {
18+
name: "Some Consumer"
19+
},
20+
enabled: false,
21+
events: [{
22+
name: 'contract_published'
23+
}],
24+
request: {
25+
method: 'POST',
26+
url: 'https://example.org',
27+
headers: {
28+
:"Content-Type" => "application/json"
29+
},
30+
body: {
31+
a: 'body'
32+
}
33+
}
34+
}
35+
end
36+
37+
subject { put(path, webhook_json, headers) }
38+
39+
let(:path) { "/webhooks/#{webhook.uuid}" }
40+
41+
context "with valid attributes" do
42+
it "returns the newly created webhook" do
43+
subject
44+
expect(response_body).to include description: "trigger build"
45+
end
46+
end
47+
end

spec/lib/pact_broker/webhooks/repository_spec.rb

+16-7
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ module Webhooks
196196
end
197197

198198
describe "update_by_uuid" do
199+
before do
200+
td.create_consumer("Foo")
201+
.create_provider
202+
.create_webhook(old_webhook_params)
203+
.create_consumer("Foo2")
204+
end
205+
199206
let(:uuid) { '1234' }
200207
let(:old_webhook_params) do
201208
{
@@ -209,24 +216,25 @@ module Webhooks
209216
password: 'password'
210217
}
211218
end
212-
let(:new_webhook_params) do
219+
let(:new_request_webhook_params) do
213220
{
214221
method: 'GET',
215222
url: 'http://example.com',
216223
body: 'foo',
217224
headers: {'Content-Type' => 'text/plain'}
218225
}
219226
end
227+
let(:new_request) { PactBroker::Domain::WebhookRequest.new(new_request_webhook_params) }
220228
let(:new_event) do
221229
PactBroker::Webhooks::WebhookEvent.new(name: 'something_else')
222230
end
223-
before do
224-
td.create_consumer
225-
.create_provider
226-
.create_webhook(old_webhook_params)
227-
end
231+
let(:new_consumer) { PactBroker::Domain::Pacticipant.new(name: "Foo2") }
228232
let(:new_webhook) do
229-
PactBroker::Domain::Webhook.new(events: [new_event], request: PactBroker::Domain::WebhookRequest.new(new_webhook_params))
233+
PactBroker::Domain::Webhook.new(
234+
consumer: new_consumer,
235+
events: [new_event],
236+
request: new_request
237+
)
230238
end
231239

232240
subject { Repository.new.update_by_uuid(uuid, new_webhook) }
@@ -241,6 +249,7 @@ module Webhooks
241249
expect(updated_webhook.request.username).to eq nil
242250
expect(updated_webhook.request.password).to eq nil
243251
expect(updated_webhook.events.first.name).to eq 'something_else'
252+
expect(updated_webhook.consumer.name).to eq "Foo2"
244253
end
245254
end
246255

0 commit comments

Comments
 (0)