@@ -33,6 +33,7 @@ import (
33
33
34
34
"github.com/coder/coder/v2/coderd/coderdtest"
35
35
"github.com/coder/coder/v2/coderd/database"
36
+ "github.com/coder/coder/v2/coderd/database/dbauthz"
36
37
"github.com/coder/coder/v2/coderd/database/dbgen"
37
38
"github.com/coder/coder/v2/coderd/database/dbtestutil"
38
39
"github.com/coder/coder/v2/coderd/notifications"
@@ -119,7 +120,8 @@ func TestSMTPDispatch(t *testing.T) {
119
120
t .Parallel ()
120
121
121
122
// SETUP
122
- ctx , logger , db := setupInMemory (t )
123
+ ctx := dbauthz .AsSystemRestricted (testutil .Context (t , testutil .WaitSuperLong ))
124
+ _ , _ , api := coderdtest .NewWithAPI (t , nil )
123
125
124
126
// start mock SMTP server
125
127
mockSMTPSrv := smtpmock .New (smtpmock.ConfigurationAttr {
@@ -140,17 +142,17 @@ func TestSMTPDispatch(t *testing.T) {
140
142
Smarthost : serpent.HostPort {Host : "localhost" , Port : fmt .Sprintf ("%d" , mockSMTPSrv .PortNumber ())},
141
143
Hello : "localhost" ,
142
144
}
143
- handler := newDispatchInterceptor (dispatch .NewSMTPHandler (cfg .SMTP , defaultHelpers (), logger .Named ("smtp" )))
144
- mgr , err := notifications .NewManager (cfg , db , defaultHelpers (), createMetrics (), logger .Named ("manager" ))
145
+ handler := newDispatchInterceptor (dispatch .NewSMTPHandler (cfg .SMTP , defaultHelpers (), api . Logger .Named ("smtp" )))
146
+ mgr , err := notifications .NewManager (cfg , api . Database , defaultHelpers (), createMetrics (), api . Logger .Named ("manager" ))
145
147
require .NoError (t , err )
146
148
mgr .WithHandlers (map [database.NotificationMethod ]notifications.Handler {method : handler })
147
149
t .Cleanup (func () {
148
150
assert .NoError (t , mgr .Stop (ctx ))
149
151
})
150
- enq , err := notifications .NewStoreEnqueuer (cfg , db , defaultHelpers (), logger .Named ("enqueuer" ), quartz .NewReal ())
152
+ enq , err := notifications .NewStoreEnqueuer (cfg , api . Database , defaultHelpers (), api . Logger .Named ("enqueuer" ), quartz .NewReal ())
151
153
require .NoError (t , err )
152
154
153
- user := createSampleUser (t , db )
155
+ user := createSampleUser (t , api . Database )
154
156
155
157
// WHEN: a message is enqueued
156
158
msgID , err := enq .Enqueue (ctx , user .ID , notifications .TemplateWorkspaceDeleted , map [string ]string {}, "test" )
@@ -177,7 +179,8 @@ func TestWebhookDispatch(t *testing.T) {
177
179
t .Parallel ()
178
180
179
181
// SETUP
180
- ctx , logger , db := setupInMemory (t )
182
+ ctx := dbauthz .AsSystemRestricted (testutil .Context (t , testutil .WaitSuperLong ))
183
+ _ , _ , api := coderdtest .NewWithAPI (t , nil )
181
184
182
185
sent := make (chan dispatch.WebhookPayload , 1 )
183
186
// Mock server to simulate webhook endpoint.
@@ -202,20 +205,20 @@ func TestWebhookDispatch(t *testing.T) {
202
205
cfg .Webhook = codersdk.NotificationsWebhookConfig {
203
206
Endpoint : * serpent .URLOf (endpoint ),
204
207
}
205
- mgr , err := notifications .NewManager (cfg , db , defaultHelpers (), createMetrics (), logger .Named ("manager" ))
208
+ mgr , err := notifications .NewManager (cfg , api . Database , defaultHelpers (), createMetrics (), api . Logger .Named ("manager" ))
206
209
require .NoError (t , err )
207
210
t .Cleanup (func () {
208
211
assert .NoError (t , mgr .Stop (ctx ))
209
212
})
210
- enq , err := notifications .NewStoreEnqueuer (cfg , db , defaultHelpers (), logger .Named ("enqueuer" ), quartz .NewReal ())
213
+ enq , err := notifications .NewStoreEnqueuer (cfg , api . Database , defaultHelpers (), api . Logger .Named ("enqueuer" ), quartz .NewReal ())
211
214
require .NoError (t , err )
212
215
213
216
const (
214
217
email = "bob@coder.com"
215
218
name = "Robert McBobbington"
216
219
username = "bob"
217
220
)
218
- user := dbgen .User (t , db , database.User {
221
+ user := dbgen .User (t , api . Database , database.User {
219
222
Email : email ,
220
223
Username : username ,
221
224
Name : name ,
@@ -533,7 +536,7 @@ func TestExpiredLeaseIsRequeued(t *testing.T) {
533
536
func TestInvalidConfig (t * testing.T ) {
534
537
t .Parallel ()
535
538
536
- _ , logger , db := setupInMemory ( t )
539
+ _ , _ , api := coderdtest . NewWithAPI ( t , nil )
537
540
538
541
// GIVEN: invalid config with dispatch period <= lease period
539
542
const (
@@ -545,7 +548,7 @@ func TestInvalidConfig(t *testing.T) {
545
548
cfg .DispatchTimeout = serpent .Duration (leasePeriod )
546
549
547
550
// WHEN: the manager is created with invalid config
548
- _ , err := notifications .NewManager (cfg , db , defaultHelpers (), createMetrics (), logger .Named ("manager" ))
551
+ _ , err := notifications .NewManager (cfg , api . Database , defaultHelpers (), createMetrics (), api . Logger .Named ("manager" ))
549
552
550
553
// THEN: the manager will fail to be created, citing invalid config as error
551
554
require .ErrorIs (t , err , notifications .ErrInvalidDispatchTimeout )
@@ -555,55 +558,57 @@ func TestNotifierPaused(t *testing.T) {
555
558
t .Parallel ()
556
559
557
560
// setup
558
- ctx , logger , db := setupInMemory (t )
561
+ ctx := dbauthz .AsSystemRestricted (testutil .Context (t , testutil .WaitSuperLong ))
562
+ _ , _ , api := coderdtest .NewWithAPI (t , nil )
559
563
560
564
// Prepare the test
561
565
handler := & fakeHandler {}
562
566
method := database .NotificationMethodSmtp
563
- user := createSampleUser (t , db )
567
+ user := createSampleUser (t , api . Database )
564
568
569
+ const fetchInterval = time .Millisecond * 100
565
570
cfg := defaultNotificationsConfig (method )
566
- mgr , err := notifications .NewManager (cfg , db , defaultHelpers (), createMetrics (), logger .Named ("manager" ))
571
+ cfg .FetchInterval = serpent .Duration (fetchInterval )
572
+ mgr , err := notifications .NewManager (cfg , api .Database , defaultHelpers (), createMetrics (), api .Logger .Named ("manager" ))
567
573
require .NoError (t , err )
568
574
mgr .WithHandlers (map [database.NotificationMethod ]notifications.Handler {method : handler })
569
575
t .Cleanup (func () {
570
576
assert .NoError (t , mgr .Stop (ctx ))
571
577
})
572
- enq , err := notifications .NewStoreEnqueuer (cfg , db , defaultHelpers (), logger .Named ("enqueuer" ), quartz .NewReal ())
578
+ enq , err := notifications .NewStoreEnqueuer (cfg , api . Database , defaultHelpers (), api . Logger .Named ("enqueuer" ), quartz .NewReal ())
573
579
require .NoError (t , err )
574
580
575
581
mgr .Run (ctx )
576
582
577
- // Notifier is on, enqueue the first message.
578
- sid , err := enq .Enqueue (ctx , user .ID , notifications .TemplateWorkspaceDeleted , map [string ]string {"type" : "success" }, "test" )
579
- require .NoError (t , err )
580
- require .Eventually (t , func () bool {
581
- handler .mu .RLock ()
582
- defer handler .mu .RUnlock ()
583
- return slices .Contains (handler .succeeded , sid .String ())
584
- }, testutil .WaitShort , testutil .IntervalFast )
585
-
586
583
// Pause the notifier.
587
584
settingsJSON , err := json .Marshal (& codersdk.NotificationsSettings {NotifierPaused : true })
588
585
require .NoError (t , err )
589
- err = db .UpsertNotificationsSettings (ctx , string (settingsJSON ))
586
+ err = api . Database .UpsertNotificationsSettings (ctx , string (settingsJSON ))
590
587
require .NoError (t , err )
591
588
592
589
// Notifier is paused, enqueue the next message.
593
- sid , err = enq .Enqueue (ctx , user .ID , notifications .TemplateWorkspaceDeleted , map [string ]string {"type" : "success" }, "test" )
590
+ sid , err : = enq .Enqueue (ctx , user .ID , notifications .TemplateWorkspaceDeleted , map [string ]string {"type" : "success" , "i" : "1 " }, "test" )
594
591
require .NoError (t , err )
592
+
593
+ // Sleep for a few fetch intervals to be sure we aren't getting false-positives in the next step.
594
+ // TODO: use quartz instead.
595
+ time .Sleep (fetchInterval * 5 )
596
+
597
+ // Ensure we have a pending message and it's the expected one.
595
598
require .Eventually (t , func () bool {
596
- pendingMessages , err := db .GetNotificationMessagesByStatus (ctx , database.GetNotificationMessagesByStatusParams {
599
+ pendingMessages , err := api . Database .GetNotificationMessagesByStatus (ctx , database.GetNotificationMessagesByStatusParams {
597
600
Status : database .NotificationMessageStatusPending ,
601
+ Limit : 10 ,
598
602
})
599
603
assert .NoError (t , err )
600
- return len (pendingMessages ) == 1
604
+ return len (pendingMessages ) == 1 &&
605
+ pendingMessages [0 ].ID .String () == sid .String ()
601
606
}, testutil .WaitShort , testutil .IntervalFast )
602
607
603
608
// Unpause the notifier.
604
609
settingsJSON , err = json .Marshal (& codersdk.NotificationsSettings {NotifierPaused : false })
605
610
require .NoError (t , err )
606
- err = db .UpsertNotificationsSettings (ctx , string (settingsJSON ))
611
+ err = api . Database .UpsertNotificationsSettings (ctx , string (settingsJSON ))
607
612
require .NoError (t , err )
608
613
609
614
// Notifier is running again, message should be dequeued.
@@ -723,19 +728,6 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
723
728
},
724
729
},
725
730
},
726
- {
727
- name : "TemplateWorkspaceMarkedForDeletionInOneWeek" ,
728
- id : notifications .TemplateWorkspaceMarkedForDeletion ,
729
- payload : types.MessagePayload {
730
- UserName : "bobby" ,
731
- Labels : map [string ]string {
732
- "name" : "bobby-workspace" ,
733
- "reason" : "template updated to new dormancy policy" ,
734
- "dormancyHours" : "168" , // 168 hours = 7 days = 1 week
735
- "timeTilDormant" : "1 week" ,
736
- },
737
- },
738
- },
739
731
{
740
732
name : "TemplateUserAccountCreated" ,
741
733
id : notifications .TemplateUserAccountCreated ,
@@ -1048,7 +1040,7 @@ func TestNotificationsTemplates(t *testing.T) {
1048
1040
t .Skip ("This test requires postgres; it relies on business-logic only implemented in the database" )
1049
1041
}
1050
1042
1051
- ctx := testutil .Context (t , testutil .WaitLong )
1043
+ ctx := dbauthz . AsSystemRestricted ( testutil .Context (t , testutil .WaitSuperLong ) )
1052
1044
api := coderdtest .New (t , createOpts (t ))
1053
1045
1054
1046
// GIVEN: the first user (owner) and a regular member
0 commit comments