From ae7bae50917ff3ac5daa80f12c10c47f4f0600f9 Mon Sep 17 00:00:00 2001 From: Stefan Jacobi Date: Mon, 19 Feb 2024 10:55:14 +0100 Subject: [PATCH] fix(webhooks): fix HasEvent logic * fix premature return in the check which skips all following events when the first one was false * add test cases Closes: #1354 --- backend/webhooks/webhook.go | 4 +++- backend/webhooks/webhook_test.go | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/backend/webhooks/webhook.go b/backend/webhooks/webhook.go index 66a666c0b..08e68f9e2 100644 --- a/backend/webhooks/webhook.go +++ b/backend/webhooks/webhook.go @@ -34,7 +34,9 @@ type BaseWebhook struct { func (bh *BaseWebhook) HasEvent(evt events.Event) bool { for _, event := range bh.Events { - return strings.HasPrefix(string(evt), string(event)) + if strings.HasPrefix(string(evt), string(event)) { + return true + } } return false diff --git a/backend/webhooks/webhook_test.go b/backend/webhooks/webhook_test.go index 9edeee979..8e984b47b 100644 --- a/backend/webhooks/webhook_test.go +++ b/backend/webhooks/webhook_test.go @@ -20,6 +20,26 @@ func TestBaseWebhook_HasEvent(t *testing.T) { require.True(t, baseHook.HasEvent(events.EmailCreate)) } +func TestWebhooks_HasEvent_WithMultipleEvents(t *testing.T) { + baseHook := BaseWebhook{ + Logger: nil, + Callback: "http://ipsum.lorem", + Events: events.Events{events.UserCreate, events.UserUpdate}, + } + + require.True(t, baseHook.HasEvent(events.UserUpdate)) +} + +func TestWebhooks_HasSubEvent_WithMultipleEvents(t *testing.T) { + baseHook := BaseWebhook{ + Logger: nil, + Callback: "http://ipsum.lorem", + Events: events.Events{events.UserCreate, events.UserUpdate}, + } + + require.True(t, baseHook.HasEvent(events.EmailCreate)) +} + func TestBaseWebhook_HasSubEvent(t *testing.T) { baseHook := BaseWebhook{ Logger: nil,