@@ -35,6 +35,7 @@ import (
35
35
"golang.org/x/xerrors"
36
36
37
37
"cdr.dev/slog"
38
+ "cdr.dev/slog/sloggers/sloghuman"
38
39
"github.com/coder/quartz"
39
40
"github.com/coder/serpent"
40
41
@@ -1654,18 +1655,21 @@ func TestDisabledByDefaultBeforeEnqueue(t *testing.T) {
1654
1655
1655
1656
ctx := dbauthz .AsNotifier (testutil .Context (t ,testutil .WaitSuperLong ))
1656
1657
store ,_ := dbtestutil .NewDB (t )
1657
- logger := testutil .Logger (t )
1658
+ logbuf := strings.Builder {}
1659
+ logger := testutil .Logger (t ).AppendSinks (sloghuman .Sink (& logbuf )).Leveled (slog .LevelDebug )
1658
1660
1659
1661
cfg := defaultNotificationsConfig (database .NotificationMethodSmtp )
1660
1662
enq ,err := notifications .NewStoreEnqueuer (cfg ,store ,defaultHelpers (),logger .Named ("enqueuer" ),quartz .NewReal ())
1661
1663
require .NoError (t ,err )
1662
1664
user := createSampleUser (t ,store )
1663
1665
1664
1666
// We want to try enqueuing a notification on a template that is disabled
1665
- // by default. We expect this tofail .
1667
+ // by default. We expect this tobe a no-op that produces a debug log .
1666
1668
templateID := notifications .TemplateWorkspaceManuallyUpdated
1667
- _ ,err = enq .Enqueue (ctx ,user .ID ,templateID ,map [string ]string {},"test" )
1668
- require .ErrorIs (t ,err ,notifications .ErrCannotEnqueueDisabledNotification ,"enqueuing did not fail with expected error" )
1669
+ notifIDs ,err := enq .Enqueue (ctx ,user .ID ,templateID ,map [string ]string {},"test" )
1670
+ require .NoError (t ,err )
1671
+ require .Contains (t ,logbuf .String (),notifications .ErrCannotEnqueueDisabledNotification .Error ())
1672
+ require .Empty (t ,notifIDs )
1669
1673
}
1670
1674
1671
1675
// TestDisabledBeforeEnqueue ensures that notifications cannot be enqueued once a user has disabled that notification template
@@ -1679,7 +1683,8 @@ func TestDisabledBeforeEnqueue(t *testing.T) {
1679
1683
1680
1684
ctx := dbauthz .AsNotifier (testutil .Context (t ,testutil .WaitSuperLong ))
1681
1685
store ,_ := dbtestutil .NewDB (t )
1682
- logger := testutil .Logger (t )
1686
+ logbuf := strings.Builder {}
1687
+ logger := testutil .Logger (t ).AppendSinks (sloghuman .Sink (& logbuf )).Leveled (slog .LevelDebug )
1683
1688
1684
1689
// GIVEN: an enqueuer & a sample user
1685
1690
cfg := defaultNotificationsConfig (database .NotificationMethodSmtp )
@@ -1697,9 +1702,12 @@ func TestDisabledBeforeEnqueue(t *testing.T) {
1697
1702
require .NoError (t ,err ,"failed to set preferences" )
1698
1703
require .EqualValues (t ,1 ,n ,"unexpected number of affected rows" )
1699
1704
1700
- // THEN: enqueuing the "workspace deleted" notification should fail with an error
1701
- _ ,err = enq .Enqueue (ctx ,user .ID ,templateID ,map [string ]string {},"test" )
1702
- require .ErrorIs (t ,err ,notifications .ErrCannotEnqueueDisabledNotification ,"enqueueing did not fail with expected error" )
1705
+ // THEN: enqueuing the "workspace deleted" notification should fail be
1706
+ // a no-op that produces a debug log
1707
+ notifIDs ,err := enq .Enqueue (ctx ,user .ID ,templateID ,map [string ]string {},"test" )
1708
+ require .NoError (t ,err )
1709
+ require .Contains (t ,logbuf .String (),notifications .ErrCannotEnqueueDisabledNotification .Error ())
1710
+ require .Empty (t ,notifIDs )
1703
1711
}
1704
1712
1705
1713
// TestDisabledAfterEnqueue ensures that notifications enqueued before a notification template was disabled will not be
@@ -1909,7 +1917,8 @@ func TestNotificationDuplicates(t *testing.T) {
1909
1917
1910
1918
ctx := dbauthz .AsNotifier (testutil .Context (t ,testutil .WaitSuperLong ))
1911
1919
store ,pubsub := dbtestutil .NewDB (t )
1912
- logger := testutil .Logger (t )
1920
+ logbuf := strings.Builder {}
1921
+ logger := testutil .Logger (t ).AppendSinks (sloghuman .Sink (& logbuf )).Leveled (slog .LevelDebug )
1913
1922
1914
1923
method := database .NotificationMethodSmtp
1915
1924
cfg := defaultNotificationsConfig (method )
@@ -1933,10 +1942,12 @@ func TestNotificationDuplicates(t *testing.T) {
1933
1942
map [string ]string {"initiator" :"danny" },"test" ,user .ID )
1934
1943
require .NoError (t ,err )
1935
1944
1936
- // WHEN: the second is enqueued, the enqueuer will rejectthe request .
1937
- _ ,err = enq .Enqueue (ctx ,user .ID ,notifications .TemplateWorkspaceDeleted ,
1945
+ // WHEN: the second is enqueued, the enqueuer will rejectit as a duplicate .
1946
+ ids ,err : =enq .Enqueue (ctx ,user .ID ,notifications .TemplateWorkspaceDeleted ,
1938
1947
map [string ]string {"initiator" :"danny" },"test" ,user .ID )
1939
- require .ErrorIs (t ,err ,notifications .ErrDuplicate )
1948
+ require .NoError (t ,err )
1949
+ require .Contains (t ,logbuf .String (),notifications .ErrDuplicate .Error ())
1950
+ require .Empty (t ,ids )
1940
1951
1941
1952
// THEN: when the clock is advanced 24h, the notification will be accepted.
1942
1953
// NOTE: the time is used in the dedupe hash, so by advancing 24h we're creating a distinct notification from the one