Skip to content

Commit

Permalink
fix(webhooks): correctly set updated consumer and provider
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 27, 2019
1 parent 9327271 commit d865a42
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/pact_broker/webhooks/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
47 changes: 47 additions & 0 deletions spec/features/update_webhook_spec.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 16 additions & 7 deletions spec/lib/pact_broker/webhooks/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -209,24 +216,25 @@ module Webhooks
password: 'password'
}
end
let(:new_webhook_params) do
let(:new_request_webhook_params) do
{
method: 'GET',
url: 'http://example.com',
body: 'foo',
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) }
Expand All @@ -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

Expand Down

0 comments on commit d865a42

Please sign in to comment.