TestDisabledBeforeEnqueue ensures that notifications cannot be enqueued once a user has disabled that notification template
(t *testing.T)
| 1812 | |
| 1813 | // TestDisabledBeforeEnqueue ensures that notifications cannot be enqueued once a user has disabled that notification template |
| 1814 | func TestDisabledBeforeEnqueue(t *testing.T) { |
| 1815 | t.Parallel() |
| 1816 | |
| 1817 | ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong)) |
| 1818 | store, _ := dbtestutil.NewDB(t) |
| 1819 | logbuf := strings.Builder{} |
| 1820 | logger := testutil.Logger(t).AppendSinks(sloghuman.Sink(&logbuf)).Leveled(slog.LevelDebug) |
| 1821 | |
| 1822 | // GIVEN: an enqueuer & a sample user |
| 1823 | cfg := defaultNotificationsConfig(database.NotificationMethodSmtp) |
| 1824 | enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewReal()) |
| 1825 | require.NoError(t, err) |
| 1826 | user := createSampleUser(t, store) |
| 1827 | |
| 1828 | // WHEN: the user has a preference set to not receive the "workspace deleted" notification |
| 1829 | templateID := notifications.TemplateWorkspaceDeleted |
| 1830 | n, err := store.UpdateUserNotificationPreferences(ctx, database.UpdateUserNotificationPreferencesParams{ |
| 1831 | UserID: user.ID, |
| 1832 | NotificationTemplateIds: []uuid.UUID{templateID}, |
| 1833 | Disableds: []bool{true}, |
| 1834 | }) |
| 1835 | require.NoError(t, err, "failed to set preferences") |
| 1836 | require.EqualValues(t, 1, n, "unexpected number of affected rows") |
| 1837 | |
| 1838 | // THEN: enqueuing the "workspace deleted" notification should fail be |
| 1839 | // a no-op that produces a debug log |
| 1840 | notifIDs, err := enq.Enqueue(ctx, user.ID, templateID, map[string]string{}, "test") |
| 1841 | require.NoError(t, err) |
| 1842 | require.Contains(t, logbuf.String(), notifications.ErrCannotEnqueueDisabledNotification.Error()) |
| 1843 | require.Empty(t, notifIDs) |
| 1844 | } |
| 1845 | |
| 1846 | // TestDisabledAfterEnqueue ensures that notifications enqueued before a notification template was disabled will not be |
| 1847 | // sent, and will instead be marked as "inhibited". |
nothing calls this directly
no test coverage detected