feat: allow notification templates to be disabled by default (#16093)

Change as part of https://github.com/coder/coder/pull/16071

It has been decided that we want to be able to have some notification
templates be disabled _by default_
https://github.com/coder/coder/pull/16071#issuecomment-2580757061.

This adds a new column (`enabled_by_default`) to
`notification_templates` that defaults to `TRUE`. It also modifies the
`inhibit_enqueue_if_disabled` function to reject notifications for
templates that have `enabled_by_default = FALSE` with the user not
explicitly enabling it.
This commit is contained in:
Danielle Maywood
2025-01-13 15:01:47 +00:00
committed by GitHub
parent 22236f2988
commit 009069cd47
20 changed files with 231 additions and 62 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ import (
)
var (
ErrCannotEnqueueDisabledNotification = xerrors.New("user has disabled this notification")
ErrCannotEnqueueDisabledNotification = xerrors.New("notification is not enabled")
ErrDuplicate = xerrors.New("duplicate notification")
)
@@ -1106,6 +1106,20 @@ func TestNotificationTemplates_Golden(t *testing.T) {
r.Name = tc.payload.UserName
},
)
// With the introduction of notifications that can be disabled
// by default, we want to make sure the user preferences have
// the notification enabled.
_, err := adminClient.UpdateUserNotificationPreferences(
context.Background(),
user.ID,
codersdk.UpdateUserNotificationPreferences{
TemplateDisabledMap: map[string]bool{
tc.id.String(): false,
},
})
require.NoError(t, err)
return &db, &api.Logger, &user
}()
@@ -1275,6 +1289,20 @@ func TestNotificationTemplates_Golden(t *testing.T) {
r.Name = tc.payload.UserName
},
)
// With the introduction of notifications that can be disabled
// by default, we want to make sure the user preferences have
// the notification enabled.
_, err := adminClient.UpdateUserNotificationPreferences(
context.Background(),
user.ID,
codersdk.UpdateUserNotificationPreferences{
TemplateDisabledMap: map[string]bool{
tc.id.String(): false,
},
})
require.NoError(t, err)
return &db, &api.Logger, &user
}()
@@ -1410,6 +1438,30 @@ func normalizeGoldenWebhook(content []byte) []byte {
return content
}
func TestDisabledByDefaultBeforeEnqueue(t *testing.T) {
t.Parallel()
if !dbtestutil.WillUsePostgres() {
t.Skip("This test requires postgres; it is testing business-logic implemented in the database")
}
// nolint:gocritic // Unit test.
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := testutil.Logger(t)
cfg := defaultNotificationsConfig(database.NotificationMethodSmtp)
enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewReal())
require.NoError(t, err)
user := createSampleUser(t, store)
// We want to try enqueuing a notification on a template that is disabled
// by default. We expect this to fail.
templateID := notifications.TemplateWorkspaceManuallyUpdated
_, err = enq.Enqueue(ctx, user.ID, templateID, map[string]string{}, "test")
require.ErrorIs(t, err, notifications.ErrCannotEnqueueDisabledNotification, "enqueuing did not fail with expected error")
}
// TestDisabledBeforeEnqueue ensures that notifications cannot be enqueued once a user has disabled that notification template
func TestDisabledBeforeEnqueue(t *testing.T) {
t.Parallel()