Skip to content

Commit

Permalink
Merge pull request #3162 from target/webhook-smoke-test
Browse files Browse the repository at this point in the history
feat: add smoke test for on call notification webhooks
  • Loading branch information
mastercactapus committed Jul 12, 2023
2 parents 6e43408 + a6398e4 commit a9f0120
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
11 changes: 7 additions & 4 deletions test/smoke/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/target/goalert/test/smoke/harness"
)

Expand All @@ -22,16 +21,20 @@ type WebhookTestingAlert struct {
func TestWebhookAlert(t *testing.T) {
t.Parallel()

ch := make(chan WebhookTestingAlert)
ch := make(chan WebhookTestingAlert, 1)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var alert WebhookTestingAlert

data, err := io.ReadAll(r.Body)
require.Nil(t, err)
if !assert.NoError(t, err) {
return
}

err = json.Unmarshal(data, &alert)
require.Nil(t, err)
if !assert.NoError(t, err) {
return
}

ch <- alert
}))
Expand Down
87 changes: 87 additions & 0 deletions test/smoke/webhookoncall_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package smoke

import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/target/goalert/expflag"
"github.com/target/goalert/test/smoke/harness"
)

type POSTDataOnCallUser struct {
ID string
Name string
URL string
}

type POSTDataOnCallNotification struct {
AppName string
Type string
Users []POSTDataOnCallUser
ScheduleID string
ScheduleName string
ScheduleURL string
}

// TestWebhookOnCallNotification tests that the configured rule sends the intended notification to the webhook.
func TestWebhookOnCallNotification(t *testing.T) {
t.Parallel()

ch := make(chan POSTDataOnCallNotification, 1)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var alert POSTDataOnCallNotification

data, err := io.ReadAll(r.Body)
if !assert.NoError(t, err) {
return
}

err = json.Unmarshal(data, &alert)
if !assert.NoError(t, err) {
return
}

ch <- alert
}))

defer ts.Close()

sql := `
insert into users (id, name, email)
values
({{uuid "user"}}, 'bob', 'joe');
insert into schedules (id, name, time_zone)
values
({{uuid "sid"}}, 'testschedule', 'UTC');
insert into schedule_rules (id, schedule_id, sunday, monday, tuesday, wednesday, thursday, friday, saturday, start_time, end_time, tgt_user_id)
values
({{uuid "ruleID"}}, {{uuid "sid"}}, true, true, true, true, true, true, true, '00:00:00', '00:00:00', {{uuid "user"}});
insert into notification_channels (id, type, name, value)
values
({{uuid "webhook"}}, 'WEBHOOK', 'url', '` + ts.URL + `');
insert into schedule_data (schedule_id, data)
values
({{uuid "sid"}}, '{"V1":{"OnCallNotificationRules": [{"ChannelID": {{uuidJSON "webhook"}}, "Time": "00:00" }]}}');
`

h := harness.NewHarnessWithFlags(t, sql, "webhook-notification-channel-type", expflag.FlagSet{expflag.ChanWebhook})
defer h.Close()

h.Trigger()

h.FastForward(24 * time.Hour)

alert := <-ch
assert.Equal(t, alert.Users[0].Name, "bob")
assert.Equal(t, alert.ScheduleName, "testschedule")
}

0 comments on commit a9f0120

Please sign in to comment.