@@ -705,6 +705,94 @@ func TestNotifcationTemplatesBody(t *testing.T) {
705
705
}
706
706
}
707
707
708
+ // TestDisabledBeforeEnqueue ensures that notifications cannot be enqueued once a user has disabled that notification template
709
+ func TestDisabledBeforeEnqueue (t * testing.T ) {
710
+ t .Parallel ()
711
+
712
+ // SETUP
713
+ if ! dbtestutil .WillUsePostgres () {
714
+ t .Skip ("This test requires postgres; it is testing business-logic implemented in the database" )
715
+ }
716
+
717
+ ctx , logger , db := setup (t )
718
+
719
+ // GIVEN: an enqueuer & a sample user
720
+ cfg := defaultNotificationsConfig (database .NotificationMethodSmtp )
721
+ enq , err := notifications .NewStoreEnqueuer (cfg , db , defaultHelpers (), logger .Named ("enqueuer" ))
722
+ require .NoError (t , err )
723
+ user := createSampleUser (t , db )
724
+
725
+ // WHEN: the user has a preference set to not receive the "workspace deleted" notification
726
+ templateId := notifications .TemplateWorkspaceDeleted
727
+ n , err := db .UpdateUserNotificationPreferences (ctx , database.UpdateUserNotificationPreferencesParams {
728
+ UserID : user .ID ,
729
+ NotificationTemplateIds : []uuid.UUID {templateId },
730
+ Disableds : []bool {true },
731
+ })
732
+ require .NoError (t , err , "failed to set preferences" )
733
+ require .EqualValues (t , 1 , n , "unexpected number of affected rows" )
734
+
735
+ // THEN: enqueuing the "workspace deleted" notification should fail with an error
736
+ _ , err = enq .Enqueue (ctx , user .ID , templateId , map [string ]string {}, "test" )
737
+ require .ErrorIs (t , err , notifications .ErrCannotEnqueueDisabledNotification , "enqueueing did not fail with expected error" )
738
+ }
739
+
740
+ // TestDisabledAfterEnqueue ensures that notifications enqueued before a notification template was disabled will not be
741
+ // sent, and will instead be marked as "inhibited".
742
+ func TestDisabledAfterEnqueue (t * testing.T ) {
743
+ t .Parallel ()
744
+
745
+ // SETUP
746
+ if ! dbtestutil .WillUsePostgres () {
747
+ t .Skip ("This test requires postgres; it is testing business-logic implemented in the database" )
748
+ }
749
+
750
+ ctx , logger , db := setup (t )
751
+
752
+ method := database .NotificationMethodSmtp
753
+ cfg := defaultNotificationsConfig (method )
754
+
755
+ mgr , err := notifications .NewManager (cfg , db , createMetrics (), logger .Named ("manager" ))
756
+ require .NoError (t , err )
757
+ t .Cleanup (func () {
758
+ assert .NoError (t , mgr .Stop (ctx ))
759
+ })
760
+
761
+ enq , err := notifications .NewStoreEnqueuer (cfg , db , defaultHelpers (), logger .Named ("enqueuer" ))
762
+ require .NoError (t , err )
763
+ user := createSampleUser (t , db )
764
+
765
+ // GIVEN: a notification is enqueued which has not (yet) been disabled
766
+ templateId := notifications .TemplateWorkspaceDeleted
767
+ msgId , err := enq .Enqueue (ctx , user .ID , templateId , map [string ]string {}, "test" )
768
+ require .NoError (t , err )
769
+
770
+ // Disable the notification template.
771
+ n , err := db .UpdateUserNotificationPreferences (ctx , database.UpdateUserNotificationPreferencesParams {
772
+ UserID : user .ID ,
773
+ NotificationTemplateIds : []uuid.UUID {templateId },
774
+ Disableds : []bool {true },
775
+ })
776
+ require .NoError (t , err , "failed to set preferences" )
777
+ require .EqualValues (t , 1 , n , "unexpected number of affected rows" )
778
+
779
+ // WHEN: running the manager to trigger dequeueing of (now-disabled) messages
780
+ mgr .Run (ctx )
781
+
782
+ // THEN: the message should not be sent, and must be set to "inhibited"
783
+ require .EventuallyWithT (t , func (ct * assert.CollectT ) {
784
+ m , err := db .GetNotificationMessagesByStatus (ctx , database.GetNotificationMessagesByStatusParams {
785
+ Status : database .NotificationMessageStatusInhibited ,
786
+ Limit : 10 ,
787
+ })
788
+ assert .NoError (ct , err )
789
+ if assert .Equal (ct , len (m ), 1 ) {
790
+ assert .Equal (ct , m [0 ].ID .String (), msgId .String ())
791
+ assert .Contains (ct , m [0 ].StatusReason .String , "disabled by user" )
792
+ }
793
+ }, testutil .WaitLong , testutil .IntervalFast , "did not find the expected inhibited message" )
794
+ }
795
+
708
796
type fakeHandler struct {
709
797
mu sync.RWMutex
710
798
succeeded , failed []string
0 commit comments