Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf558cb

Browse files
author
defelmnq
committed
feat(notifications): add golden file for custom appearance
1 parent 0a9a66a commit bf558cb

File tree

6 files changed

+36
-40
lines changed

6 files changed

+36
-40
lines changed
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package dispatch_test
22

3-
var (
4-
helpers = map[string]any{
5-
"base_url": func() string { return "http://test.com" },
6-
"current_year": func() string { return "2024" },
7-
"logo_url": func() string { return "https://coder.com/coder-logo-horizontal.png" },
8-
"app_name": func() string { return "Coder" },
9-
}
10-
)
3+
var helpers = map[string]any{
4+
"base_url": func() string { return "http://test.com" },
5+
"current_year": func() string { return "2024" },
6+
"logo_url": func() string { return "https://coder.com/coder-logo-horizontal.png" },
7+
"app_name": func() string { return "Coder" },
8+
}

coderd/notifications/dispatch/webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NewWebhookHandler(cfg codersdk.NotificationsWebhookConfig, log slog.Logger)
4242
return &WebhookHandler{cfg: cfg, log: log, cl: &http.Client{}}
4343
}
4444

45-
func (w *WebhookHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, titleMarkdown, bodyMarkdown string) (DeliveryFunc, error) {
45+
func (w *WebhookHandler) Dispatcher(_ template.FuncMap, payload types.MessagePayload, titleMarkdown, bodyMarkdown string) (DeliveryFunc, error) {
4646
if w.cfg.Endpoint.String() == "" {
4747
return nil, xerrors.New("webhook endpoint not defined")
4848
}

coderd/notifications/manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ type santaHandler struct {
206206
nice atomic.Int32
207207
}
208208

209-
func (s *santaHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
210-
return func(ctx context.Context, msgID uuid.UUID) (retryable bool, err error) {
209+
func (s *santaHandler) Dispatcher(_ template.FuncMap, payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
210+
return func(_ context.Context, _ uuid.UUID) (retryable bool, err error) {
211211
if payload.Labels["nice"] != "true" {
212212
s.naughty.Add(1)
213213
return false, xerrors.New("be nice")

coderd/notifications/metrics_test.go

Lines changed: 15 additions & 15 deletions
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-
template := notifications.TemplateWorkspaceDeleted
47+
tmplate := 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, template.String())
79+
methodTemplateFP := fingerprintLabels(notifications.LabelMethod, string(method), notifications.LabelTemplateID, tmplate.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, template.String(), notifications.LabelResult, result)
93+
seriesFP := fingerprintLabels(notifications.LabelMethod, string(method), notifications.LabelTemplateID, tmplate.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, template, map[string]string{"type": "success"}, "test") // this will succeed
168+
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "success"}, "test") // this will succeed
169169
require.NoError(t, err)
170-
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "failure"}, "test2") // this will fail and retry (maxAttempts - 1) times
170+
_, err = enq.Enqueue(ctx, user.ID, tmplate, 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-
template := notifications.TemplateWorkspaceDeleted
220+
tmplate := 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, template, map[string]string{"type": "success"}, "test") // this will succeed
251+
_, err = enq.Enqueue(ctx, user.ID, tmplate, map[string]string{"type": "success"}, "test") // this will succeed
252252
require.NoError(t, err)
253-
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "failure"}, "test2") // this will fail and retry (maxAttempts - 1) times
253+
_, err = enq.Enqueue(ctx, user.ID, tmplate, 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-
template := notifications.TemplateWorkspaceDeleted
304+
tmplate := 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, template, map[string]string{"type": "success", "i": strconv.Itoa(i)}, "test")
337+
_, err = enq.Enqueue(ctx, user.ID, tmplate, 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), template.String())) == msgCount
346+
return promtest.ToFloat64(metrics.InflightDispatches.WithLabelValues(string(method), tmplate.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-
template = notifications.TemplateWorkspaceDeleted
383+
tmplate = 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: template,
394+
ID: tmplate,
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, template, map[string]string{"type": "success"}, "test")
420+
_, err = enq.Enqueue(ctx, user.ID, tmplate, 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), template.String(), notifications.ResultSuccess)) > 0
441+
promtest.ToFloat64(metrics.DispatchAttempts.WithLabelValues(string(customMethod), tmplate.String(), notifications.ResultSuccess)) > 0
442442
}, testutil.WaitShort, testutil.IntervalFast)
443443
}
444444

coderd/notifications/notifications_test.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,12 +1414,12 @@ func TestCustomNotificationMethod(t *testing.T) {
14141414

14151415
// GIVEN: a notification template which has a method explicitly set
14161416
var (
1417-
template = notifications.TemplateWorkspaceDormant
1417+
tmplate = notifications.TemplateWorkspaceDormant
14181418
defaultMethod = database.NotificationMethodSmtp
14191419
customMethod = database.NotificationMethodWebhook
14201420
)
14211421
out, err := api.Database.UpdateNotificationTemplateMethodByID(ctx, database.UpdateNotificationTemplateMethodByIDParams{
1422-
ID: template,
1422+
ID: tmplate,
14231423
Method: database.NullNotificationMethod{NotificationMethod: customMethod, Valid: true},
14241424
})
14251425
require.NoError(t, err)
@@ -1447,7 +1447,7 @@ func TestCustomNotificationMethod(t *testing.T) {
14471447

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

14531453
// THEN: the notification should be received by the custom dispatch method
@@ -1562,7 +1562,7 @@ type fakeHandler struct {
15621562
succeeded, failed []string
15631563
}
15641564

1565-
func (f *fakeHandler) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
1565+
func (f *fakeHandler) Dispatcher(_ template.FuncMap, payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
15661566
return func(_ context.Context, msgID uuid.UUID) (retryable bool, err error) {
15671567
f.mu.Lock()
15681568
defer f.mu.Unlock()
@@ -1629,15 +1629,13 @@ func TestNotificationTemplates_GoldenWithCustomAppearance(t *testing.T) {
16291629
hint = "run \"DB=ci make update-golden-files\" and commit the changes"
16301630
)
16311631

1632-
var (
1633-
payload = types.MessagePayload{
1634-
Labels: map[string]string{
1635-
"name": "bobby-workspace",
1636-
"reason": "autodeleted due to dormancy",
1637-
"initiator": "autobuild",
1638-
},
1639-
}
1640-
)
1632+
payload := types.MessagePayload{
1633+
Labels: map[string]string{
1634+
"name": "bobby-workspace",
1635+
"reason": "autodeleted due to dormancy",
1636+
"initiator": "autobuild",
1637+
},
1638+
}
16411639

16421640
// Spin up the DB
16431641
db, logger, user := func() (database.Store, *slog.Logger, *codersdk.User) {

coderd/notifications/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func newDispatchInterceptor(h notifications.Handler) *dispatchInterceptor {
7070
return &dispatchInterceptor{handler: h}
7171
}
7272

73-
func (i *dispatchInterceptor) Dispatcher(helpers template.FuncMap, payload types.MessagePayload, title, body string) (dispatch.DeliveryFunc, error) {
73+
func (i *dispatchInterceptor) Dispatcher(_ template.FuncMap, payload types.MessagePayload, title, body string) (dispatch.DeliveryFunc, error) {
7474
return func(ctx context.Context, msgID uuid.UUID) (retryable bool, err error) {
7575
deliveryFn, err := i.handler.Dispatcher(defaultHelpers(), payload, title, body)
7676
if err != nil {

0 commit comments

Comments
 (0)