Skip to content

Commit a420f37

Browse files
committed
feat(notifications): work on tests
1 parent bf558cb commit a420f37

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

coderd/notifications/metrics_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestMetrics(t *testing.T) {
4444

4545
reg := prometheus.NewRegistry()
4646
metrics := notifications.NewMetrics(reg)
47-
tmplate := notifications.TemplateWorkspaceDeleted
47+
tmpl := notifications.TemplateWorkspaceDeleted
4848

4949
const (
5050
method = database.NotificationMethodSmtp
@@ -76,7 +76,7 @@ func TestMetrics(t *testing.T) {
7676
user := createSampleUser(t, api.Database)
7777

7878
// Build fingerprints for the two different series we expect.
79-
methodTemplateFP := fingerprintLabels(notifications.LabelMethod, string(method), notifications.LabelTemplateID, tmplate.String())
79+
methodTemplateFP := fingerprintLabels(notifications.LabelMethod, string(method), notifications.LabelTemplateID, tmpl.String())
8080
methodFP := fingerprintLabels(notifications.LabelMethod, string(method))
8181

8282
expected := map[string]func(metric *dto.Metric, series string) bool{
@@ -90,7 +90,7 @@ func TestMetrics(t *testing.T) {
9090

9191
var match string
9292
for result, val := range results {
93-
seriesFP := fingerprintLabels(notifications.LabelMethod, string(method), notifications.LabelTemplateID, tmplate.String(), notifications.LabelResult, result)
93+
seriesFP := fingerprintLabels(notifications.LabelMethod, string(method), notifications.LabelTemplateID, tmpl.String(), notifications.LabelResult, result)
9494
if !hasMatchingFingerprint(metric, seriesFP) {
9595
continue
9696
}
@@ -165,9 +165,9 @@ func TestMetrics(t *testing.T) {
165165
}
166166

167167
// WHEN: 2 notifications are enqueued, 1 of which will fail until its retries are exhausted, and another which will succeed
168-
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "success"}, "test") // this will succeed
168+
_, err = enq.Enqueue(ctx, user.ID, tmpl, map[string]string{"type": "success"}, "test") // this will succeed
169169
require.NoError(t, err)
170-
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "failure"}, "test2") // this will fail and retry (maxAttempts - 1) times
170+
_, err = enq.Enqueue(ctx, user.ID, tmpl, map[string]string{"type": "failure"}, "test2") // this will fail and retry (maxAttempts - 1) times
171171
require.NoError(t, err)
172172

173173
mgr.Run(ctx)
@@ -217,7 +217,7 @@ func TestPendingUpdatesMetric(t *testing.T) {
217217

218218
reg := prometheus.NewRegistry()
219219
metrics := notifications.NewMetrics(reg)
220-
tmplate := notifications.TemplateWorkspaceDeleted
220+
tmpl := notifications.TemplateWorkspaceDeleted
221221

222222
const method = database.NotificationMethodSmtp
223223

@@ -248,9 +248,9 @@ func TestPendingUpdatesMetric(t *testing.T) {
248248
user := createSampleUser(t, api.Database)
249249

250250
// WHEN: 2 notifications are enqueued, one of which will fail and one which will succeed
251-
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "success"}, "test") // this will succeed
251+
_, err = enq.Enqueue(ctx, user.ID, tmpl, map[string]string{"type": "success"}, "test") // this will succeed
252252
require.NoError(t, err)
253-
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "failure"}, "test2") // this will fail and retry (maxAttempts - 1) times
253+
_, err = enq.Enqueue(ctx, user.ID, tmpl, map[string]string{"type": "failure"}, "test2") // this will fail and retry (maxAttempts - 1) times
254254
require.NoError(t, err)
255255

256256
mgr.Run(ctx)
@@ -301,7 +301,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
301301

302302
reg := prometheus.NewRegistry()
303303
metrics := notifications.NewMetrics(reg)
304-
tmplate := notifications.TemplateWorkspaceDeleted
304+
tmpl := notifications.TemplateWorkspaceDeleted
305305

306306
const method = database.NotificationMethodSmtp
307307

@@ -334,7 +334,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
334334

335335
// WHEN: notifications are enqueued which will succeed (and be delayed during dispatch)
336336
for i := 0; i < msgCount; i++ {
337-
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "success", "i": strconv.Itoa(i)}, "test")
337+
_, err = enq.Enqueue(ctx, user.ID, tmpl, map[string]string{"type": "success", "i": strconv.Itoa(i)}, "test")
338338
require.NoError(t, err)
339339
}
340340

@@ -343,7 +343,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
343343
// THEN:
344344
// Ensure we see the dispatches of the messages inflight.
345345
require.Eventually(t, func() bool {
346-
return promtest.ToFloat64(metrics.InflightDispatches.WithLabelValues(string(method), tmplate.String())) == msgCount
346+
return promtest.ToFloat64(metrics.InflightDispatches.WithLabelValues(string(method), tmpl.String())) == msgCount
347347
}, testutil.WaitShort, testutil.IntervalFast)
348348

349349
for i := 0; i < msgCount; i++ {
@@ -380,7 +380,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
380380
var (
381381
reg = prometheus.NewRegistry()
382382
metrics = notifications.NewMetrics(reg)
383-
tmplate = notifications.TemplateWorkspaceDeleted
383+
tmpl = notifications.TemplateWorkspaceDeleted
384384
anotherTemplate = notifications.TemplateWorkspaceDormant
385385
)
386386

@@ -391,7 +391,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
391391

392392
// GIVEN: a template whose notification method differs from the default.
393393
out, err := api.Database.UpdateNotificationTemplateMethodByID(ctx, database.UpdateNotificationTemplateMethodByIDParams{
394-
ID: tmplate,
394+
ID: tmpl,
395395
Method: database.NullNotificationMethod{NotificationMethod: customMethod, Valid: true},
396396
})
397397
require.NoError(t, err)
@@ -417,7 +417,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
417417

418418
user := createSampleUser(t, api.Database)
419419

420-
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "success"}, "test")
420+
_, err = enq.Enqueue(ctx, user.ID, tmpl, map[string]string{"type": "success"}, "test")
421421
require.NoError(t, err)
422422
_, err = enq.Enqueue(ctx, user.ID, anotherTemplate, map[string]string{"type": "success"}, "test")
423423
require.NoError(t, err)
@@ -438,7 +438,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
438438
// THEN: we should have metric series for both the default and custom notification methods.
439439
require.Eventually(t, func() bool {
440440
return promtest.ToFloat64(metrics.DispatchAttempts.WithLabelValues(string(defaultMethod), anotherTemplate.String(), notifications.ResultSuccess)) > 0 &&
441-
promtest.ToFloat64(metrics.DispatchAttempts.WithLabelValues(string(customMethod), tmplate.String(), notifications.ResultSuccess)) > 0
441+
promtest.ToFloat64(metrics.DispatchAttempts.WithLabelValues(string(customMethod), tmpl.String(), notifications.ResultSuccess)) > 0
442442
}, testutil.WaitShort, testutil.IntervalFast)
443443
}
444444

coderd/notifications/notifications_test.go

+32-3
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,9 @@ func TestNotificationTemplates_Golden(t *testing.T) {
708708
name string
709709
id uuid.UUID
710710
payload types.MessagePayload
711+
712+
appName string
713+
logoURL string
711714
}{
712715
{
713716
name: "TemplateWorkspaceDeleted",
@@ -958,6 +961,22 @@ func TestNotificationTemplates_Golden(t *testing.T) {
958961
},
959962
},
960963
},
964+
{
965+
name: "TemplateWorkspaceDeleted_CustomAppearance",
966+
id: notifications.TemplateWorkspaceDeleted,
967+
payload: types.MessagePayload{
968+
UserName: "Bobby",
969+
UserEmail: "bobby@coder.com",
970+
UserUsername: "bobby",
971+
Labels: map[string]string{
972+
"name": "bobby-workspace",
973+
"reason": "autodeleted due to dormancy",
974+
"initiator": "autobuild",
975+
},
976+
},
977+
appName: "Custom Application Name",
978+
logoURL: "https://custom.application/logo.png",
979+
},
961980
}
962981

963982
// We must have a test case for every notification_template. This is enforced below:
@@ -1079,6 +1098,16 @@ func TestNotificationTemplates_Golden(t *testing.T) {
10791098
)
10801099
require.NoError(t, err)
10811100

1101+
if tc.appName != "" {
1102+
err = (*db).UpsertApplicationName(ctx, "Custom Application")
1103+
require.NoError(t, err)
1104+
}
1105+
1106+
if tc.logoURL != "" {
1107+
err = (*db).UpsertLogoURL(ctx, "https://custom.application/logo.png")
1108+
require.NoError(t, err)
1109+
}
1110+
10821111
smtpManager.Run(ctx)
10831112

10841113
notificationCfg := defaultNotificationsConfig(database.NotificationMethodSmtp)
@@ -1414,12 +1443,12 @@ func TestCustomNotificationMethod(t *testing.T) {
14141443

14151444
// GIVEN: a notification template which has a method explicitly set
14161445
var (
1417-
tmplate = notifications.TemplateWorkspaceDormant
1446+
tmpl = notifications.TemplateWorkspaceDormant
14181447
defaultMethod = database.NotificationMethodSmtp
14191448
customMethod = database.NotificationMethodWebhook
14201449
)
14211450
out, err := api.Database.UpdateNotificationTemplateMethodByID(ctx, database.UpdateNotificationTemplateMethodByIDParams{
1422-
ID: tmplate,
1451+
ID: tmpl,
14231452
Method: database.NullNotificationMethod{NotificationMethod: customMethod, Valid: true},
14241453
})
14251454
require.NoError(t, err)
@@ -1447,7 +1476,7 @@ func TestCustomNotificationMethod(t *testing.T) {
14471476

14481477
// WHEN: a notification of that template is enqueued, it should be delivered with the configured method - not the default.
14491478
user := createSampleUser(t, api.Database)
1450-
msgID, err := enq.Enqueue(ctx, user.ID, tmplate, map[string]string{}, "test")
1479+
msgID, err := enq.Enqueue(ctx, user.ID, tmpl, map[string]string{}, "test")
14511480
require.NoError(t, err)
14521481

14531482
// THEN: the notification should be received by the custom dispatch method

0 commit comments

Comments
 (0)