MCPcopy Index your code
hub / github.com/coder/coder / TestNotificationDuplicates

Function TestNotificationDuplicates

coderd/notifications/notifications_test.go:2027–2071  ·  view source on GitHub ↗

TestNotificationDuplicates validates that identical notifications cannot be sent on the same day.

(t *testing.T)

Source from the content-addressed store, hash-verified

2025
2026// TestNotificationDuplicates validates that identical notifications cannot be sent on the same day.
2027func TestNotificationDuplicates(t *testing.T) {
2028 t.Parallel()
2029
2030 ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
2031 store, pubsub := dbtestutil.NewDB(t)
2032 logbuf := strings.Builder{}
2033 logger := testutil.Logger(t).AppendSinks(sloghuman.Sink(&logbuf)).Leveled(slog.LevelDebug)
2034
2035 method := database.NotificationMethodSmtp
2036 cfg := defaultNotificationsConfig(method)
2037
2038 mgr, err := notifications.NewManager(cfg, store, pubsub, defaultHelpers(), createMetrics(), logger.Named("manager"))
2039 require.NoError(t, err)
2040 t.Cleanup(func() {
2041 assert.NoError(t, mgr.Stop(ctx))
2042 })
2043
2044 // Set the time to a known value.
2045 mClock := quartz.NewMock(t)
2046 mClock.Set(time.Date(2024, 1, 15, 9, 0, 0, 0, time.UTC))
2047
2048 enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), mClock)
2049 require.NoError(t, err)
2050 user := createSampleUser(t, store)
2051
2052 // GIVEN: two notifications are enqueued with identical properties.
2053 _, err = enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted,
2054 map[string]string{"initiator": "danny"}, "test", user.ID)
2055 require.NoError(t, err)
2056
2057 // WHEN: the second is enqueued, the enqueuer will reject it as a duplicate.
2058 ids, err := enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted,
2059 map[string]string{"initiator": "danny"}, "test", user.ID)
2060 require.NoError(t, err)
2061 require.Contains(t, logbuf.String(), notifications.ErrDuplicate.Error())
2062 require.Empty(t, ids)
2063
2064 // THEN: when the clock is advanced 24h, the notification will be accepted.
2065 // NOTE: the time is used in the dedupe hash, so by advancing 24h we're creating a distinct notification from the one
2066 // which was enqueued "yesterday".
2067 mClock.Advance(time.Hour * 24)
2068 _, err = enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted,
2069 map[string]string{"initiator": "danny"}, "test", user.ID)
2070 require.NoError(t, err)
2071}
2072
2073func TestNotificationMethodCannotDefaultToInbox(t *testing.T) {
2074 t.Parallel()

Callers

nothing calls this directly

Calls 15

StopMethod · 0.95
EnqueueMethod · 0.95
AsNotifierFunction · 0.92
ContextFunction · 0.92
NewDBFunction · 0.92
LoggerFunction · 0.92
NewManagerFunction · 0.92
NewStoreEnqueuerFunction · 0.92
defaultHelpersFunction · 0.85
createMetricsFunction · 0.85
createSampleUserFunction · 0.85

Tested by

no test coverage detected