diff --git a/lib/pact_broker/webhooks/repository.rb b/lib/pact_broker/webhooks/repository.rb index f1d6aabc4..057ccf7db 100644 --- a/lib/pact_broker/webhooks/repository.rb +++ b/lib/pact_broker/webhooks/repository.rb @@ -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 diff --git a/spec/features/create_webhook_spec.rb b/spec/features/create_webhook_spec.rb index f3c6e6d97..ad3539566 100644 --- a/spec/features/create_webhook_spec.rb +++ b/spec/features/create_webhook_spec.rb @@ -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 diff --git a/spec/lib/pact_broker/webhooks/repository_spec.rb b/spec/lib/pact_broker/webhooks/repository_spec.rb index 88ddd65ce..516ea10a5 100644 --- a/spec/lib/pact_broker/webhooks/repository_spec.rb +++ b/spec/lib/pact_broker/webhooks/repository_spec.rb @@ -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'} } @@ -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 } @@ -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