Skip to content

Commit

Permalink
fix(webhooks): fix HasEvent logic
Browse files Browse the repository at this point in the history
* fix premature return in the check which skips all following events when the first one was false
* add test cases

Closes: #1354
  • Loading branch information
Stefan Jacobi committed Feb 19, 2024
1 parent cfd11cd commit ae7bae5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion backend/webhooks/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions backend/webhooks/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ae7bae5

Please sign in to comment.