Skip to content

Commit

Permalink
fix: set consumer and provider relationships when a webhook is create…
Browse files Browse the repository at this point in the history
…d and consumer/provider are set in the body of the request rather than the URL

Fixes: #331
  • Loading branch information
bethesque committed Mar 10, 2020
1 parent 9ff7bbe commit 5ccdd31
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 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 @@ -17,6 +17,8 @@ class Repository
include Repositories

def create uuid, webhook, consumer, provider
consumer = pacticipant_repository.find_by_name(webhook.consumer.name) if webhook.consumer
provider = pacticipant_repository.find_by_name(webhook.provider.name) if webhook.provider
db_webhook = Webhook.from_domain webhook, consumer, provider
db_webhook.uuid = uuid
db_webhook.save
Expand Down
11 changes: 11 additions & 0 deletions spec/features/create_webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,19 @@
context "with a UUID" do
let(:path) { "/webhooks/1234123412341234" }

before do
webhook_hash[:provider] = { name: "Some Provider" }
webhook_hash[:consumer] = { name: "Some Consumer" }
end

subject { put(path, webhook_json, headers) }

its(:status) { is_expected.to be 201 }

it "is expected to have a consumer and provider" do
subject
expect(PactBroker::Webhooks::Webhook.first.consumer).to_not be nil
expect(PactBroker::Webhooks::Webhook.first.provider).to_not be nil
end
end
end
20 changes: 14 additions & 6 deletions spec/lib/pact_broker/webhooks/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
module PactBroker
module Webhooks
describe Repository do

let(:td) { TestDataBuilder.new }
let(:url) { 'http://example.org' }
let(:body) { {'some' => 'json' } }
let(:headers) { {'Content-Type' => 'application/json', 'Accept' => 'application/json'} }
Expand All @@ -22,9 +20,9 @@ module Webhooks
PactBroker::Webhooks::WebhookEvent.new(name: 'something_happened')
end
let(:events) { [event]}
let(:webhook) { Domain::Webhook.new(request: request, events: events)}
let(:consumer) { td.create_pacticipant 'Consumer'; td.pacticipant}
let(:provider) { td.create_pacticipant 'Provider'; td.pacticipant}
let(:webhook) { Domain::Webhook.new(request: request, events: events) }
let(:consumer) { td.create_pacticipant('Consumer').and_return(:pacticipant) }
let(:provider) { td.create_pacticipant('Provider').and_return(:pacticipant) }
let(:uuid) { 'the-uuid' }
let(:created_webhook_record) { ::DB::PACT_BROKER_DB[:webhooks].order(:id).last }
let(:created_events) { ::DB::PACT_BROKER_DB[:webhook_events].where(webhook_id: created_webhook_record[:id]).order(:name).all }
Expand Down Expand Up @@ -57,10 +55,20 @@ module Webhooks
it "saves the webhook events" do
expect(subject.events.first[:name]).to eq "something_happened"
end

context "when consumer and provider domain objects are set on the object rather than passed in" do
let(:webhook) { Domain::Webhook.new(request: request, events: events, consumer: consumer, provider: provider) }

subject { Repository.new.create(uuid, webhook, nil, nil) }

it "sets the consumer and provider relationships" do
expect(subject.consumer.id).to eq consumer.id
expect(subject.provider.id).to eq provider.id
end
end
end

describe "delete_by_uuid" do

before do
Repository.new.create uuid, webhook, consumer, provider
Repository.new.create 'another-uuid', webhook, consumer, provider
Expand Down

0 comments on commit 5ccdd31

Please sign in to comment.