diff --git a/bullet_train-outgoing_webhooks/app/models/concerns/webhooks/outgoing/endpoint_support.rb b/bullet_train-outgoing_webhooks/app/models/concerns/webhooks/outgoing/endpoint_support.rb index 33c542f79..69cc5efa9 100644 --- a/bullet_train-outgoing_webhooks/app/models/concerns/webhooks/outgoing/endpoint_support.rb +++ b/bullet_train-outgoing_webhooks/app/models/concerns/webhooks/outgoing/endpoint_support.rb @@ -34,7 +34,7 @@ def creative_concepts end def event_types - event_type_ids.map { |id| Webhooks::Outgoing::EventType.find(id) } + Webhooks::Outgoing::EventType.where(id: event_type_ids) end def touch_parent diff --git a/bullet_train-outgoing_webhooks/test/models/webhooks/outgoing/endpoint_test.rb b/bullet_train-outgoing_webhooks/test/models/webhooks/outgoing/endpoint_test.rb new file mode 100644 index 000000000..938c8f2d8 --- /dev/null +++ b/bullet_train-outgoing_webhooks/test/models/webhooks/outgoing/endpoint_test.rb @@ -0,0 +1,51 @@ +require "test_helper" + +class Webhooks::Outgoing::EndpointTest < ActiveSupport::TestCase + include ActiveJob::TestHelper + + setup do + @team = Team.create!(name: "test-team") + @endpoint = Webhooks::Outgoing::Endpoint.create!(url: "https://example.com/webhook", name: "test", team: @team) + end + + test "#create should accept valid event_types" do + @endpoint = Webhooks::Outgoing::Endpoint.create!( + url: "https://example.com/webhook", + name: "test", + team: @team, + event_type_ids: [Webhooks::Outgoing::EventType.all.first.id] + ) + assert @endpoint.persisted? + end + + test "#create should accept non-existent event_types" do + @endpoint = Webhooks::Outgoing::Endpoint.create!( + url: "https://example.com/webhook", + name: "test", + team: @team, + event_type_ids: ["fake-thing.create"] + ) + assert @endpoint.persisted? + end + + test "#event_types should return existent EventTypes" do + valid_event_type = Webhooks::Outgoing::EventType.all.first + @endpoint = Webhooks::Outgoing::Endpoint.create!( + url: "https://example.com/webhook", + name: "test", + team: @team, + event_type_ids: [valid_event_type.id] + ) + assert_equal [valid_event_type.id], @endpoint.event_types.map(&:id) + end + + test "#event_types should not raise an error for non-existent event_type_ids" do + @endpoint = Webhooks::Outgoing::Endpoint.create!( + url: "https://example.com/webhook", + name: "test", + team: @team, + event_type_ids: ["fake-thing.create"] + ) + assert_equal [], @endpoint.event_types + end +end