TestDisabledAfterEnqueue ensures that notifications enqueued before a notification template was disabled will not be sent, and will instead be marked as "inhibited".
(t *testing.T)
| 1846 | // TestDisabledAfterEnqueue ensures that notifications enqueued before a notification template was disabled will not be |
| 1847 | // sent, and will instead be marked as "inhibited". |
| 1848 | func TestDisabledAfterEnqueue(t *testing.T) { |
| 1849 | t.Parallel() |
| 1850 | |
| 1851 | ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong)) |
| 1852 | store, pubsub := dbtestutil.NewDB(t) |
| 1853 | logger := testutil.Logger(t) |
| 1854 | |
| 1855 | method := database.NotificationMethodSmtp |
| 1856 | cfg := defaultNotificationsConfig(method) |
| 1857 | |
| 1858 | mgr, err := notifications.NewManager(cfg, store, pubsub, defaultHelpers(), createMetrics(), logger.Named("manager")) |
| 1859 | require.NoError(t, err) |
| 1860 | t.Cleanup(func() { |
| 1861 | assert.NoError(t, mgr.Stop(ctx)) |
| 1862 | }) |
| 1863 | |
| 1864 | enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewReal()) |
| 1865 | require.NoError(t, err) |
| 1866 | user := createSampleUser(t, store) |
| 1867 | |
| 1868 | // GIVEN: a notification is enqueued which has not (yet) been disabled |
| 1869 | templateID := notifications.TemplateWorkspaceDeleted |
| 1870 | msgID, err := enq.Enqueue(ctx, user.ID, templateID, map[string]string{}, "test") |
| 1871 | require.NoError(t, err) |
| 1872 | |
| 1873 | // Disable the notification template. |
| 1874 | n, err := store.UpdateUserNotificationPreferences(ctx, database.UpdateUserNotificationPreferencesParams{ |
| 1875 | UserID: user.ID, |
| 1876 | NotificationTemplateIds: []uuid.UUID{templateID}, |
| 1877 | Disableds: []bool{true}, |
| 1878 | }) |
| 1879 | require.NoError(t, err, "failed to set preferences") |
| 1880 | require.EqualValues(t, 1, n, "unexpected number of affected rows") |
| 1881 | |
| 1882 | // WHEN: running the manager to trigger dequeueing of (now-disabled) messages |
| 1883 | mgr.Run(ctx) |
| 1884 | |
| 1885 | // THEN: the message should not be sent, and must be set to "inhibited" |
| 1886 | require.EventuallyWithT(t, func(ct *assert.CollectT) { |
| 1887 | m, err := store.GetNotificationMessagesByStatus(ctx, database.GetNotificationMessagesByStatusParams{ |
| 1888 | Status: database.NotificationMessageStatusInhibited, |
| 1889 | Limit: 10, |
| 1890 | }) |
| 1891 | assert.NoError(ct, err) |
| 1892 | if assert.Equal(ct, len(m), 2) { |
| 1893 | assert.Contains(ct, []string{m[0].ID.String(), m[1].ID.String()}, msgID[0].String()) |
| 1894 | assert.Contains(ct, m[0].StatusReason.String, "disabled by user") |
| 1895 | } |
| 1896 | }, testutil.WaitLong, testutil.IntervalFast, "did not find the expected inhibited message") |
| 1897 | } |
| 1898 | |
| 1899 | func TestCustomNotificationMethod(t *testing.T) { |
| 1900 | t.Parallel() |
nothing calls this directly
no test coverage detected