@@ -2,6 +2,7 @@ package notifications_test
2
2
3
3
import (
4
4
"context"
5
+ "strconv"
5
6
"testing"
6
7
"time"
7
8
@@ -17,7 +18,9 @@ import (
17
18
18
19
"github.com/coder/serpent"
19
20
21
+ "github.com/coder/coder/v2/coderd/coderdtest"
20
22
"github.com/coder/coder/v2/coderd/database"
23
+ "github.com/coder/coder/v2/coderd/database/dbauthz"
21
24
"github.com/coder/coder/v2/coderd/database/dbtestutil"
22
25
"github.com/coder/coder/v2/coderd/notifications"
23
26
"github.com/coder/coder/v2/coderd/notifications/dispatch"
@@ -33,7 +36,9 @@ func TestMetrics(t *testing.T) {
33
36
t .Skip ("This test requires postgres; it relies on business-logic only implemented in the database" )
34
37
}
35
38
36
- ctx ,logger ,store := setup (t )
39
+ // nolint:gocritic // Unit test.
40
+ ctx := dbauthz .AsSystemRestricted (testutil .Context (t ,testutil .WaitSuperLong ))
41
+ _ ,_ ,api := coderdtest .NewWithAPI (t ,nil )
37
42
38
43
reg := prometheus .NewRegistry ()
39
44
metrics := notifications .NewMetrics (reg )
@@ -53,7 +58,7 @@ func TestMetrics(t *testing.T) {
53
58
cfg .RetryInterval = serpent .Duration (time .Millisecond * 50 )
54
59
cfg .StoreSyncInterval = serpent .Duration (time .Millisecond * 100 )// Twice as long as fetch interval to ensure we catch pending updates.
55
60
56
- mgr ,err := notifications .NewManager (cfg ,store ,defaultHelpers (),metrics ,logger .Named ("manager" ))
61
+ mgr ,err := notifications .NewManager (cfg ,api . Database ,defaultHelpers (),metrics ,api . Logger .Named ("manager" ))
57
62
require .NoError (t ,err )
58
63
t .Cleanup (func () {
59
64
assert .NoError (t ,mgr .Stop (ctx ))
@@ -63,10 +68,10 @@ func TestMetrics(t *testing.T) {
63
68
method :handler ,
64
69
})
65
70
66
- enq ,err := notifications .NewStoreEnqueuer (cfg ,store ,defaultHelpers (),logger .Named ("enqueuer" ),quartz .NewReal ())
71
+ enq ,err := notifications .NewStoreEnqueuer (cfg ,api . Database ,defaultHelpers (),api . Logger .Named ("enqueuer" ),quartz .NewReal ())
67
72
require .NoError (t ,err )
68
73
69
- user := createSampleUser (t ,store )
74
+ user := createSampleUser (t ,api . Database )
70
75
71
76
// Build fingerprints for the two different series we expect.
72
77
methodTemplateFP := fingerprintLabels (notifications .LabelMethod ,string (method ),notifications .LabelTemplateID ,template .String ())
@@ -204,7 +209,9 @@ func TestPendingUpdatesMetric(t *testing.T) {
204
209
t .Parallel ()
205
210
206
211
// SETUP
207
- ctx ,logger ,store := setupInMemory (t )
212
+ // nolint:gocritic // Unit test.
213
+ ctx := dbauthz .AsSystemRestricted (testutil .Context (t ,testutil .WaitSuperLong ))
214
+ _ ,_ ,api := coderdtest .NewWithAPI (t ,nil )
208
215
209
216
reg := prometheus .NewRegistry ()
210
217
metrics := notifications .NewMetrics (reg )
@@ -218,9 +225,9 @@ func TestPendingUpdatesMetric(t *testing.T) {
218
225
cfg .RetryInterval = serpent .Duration (time .Hour )// Delay retries so they don't interfere.
219
226
cfg .StoreSyncInterval = serpent .Duration (time .Millisecond * 100 )
220
227
221
- syncer := & syncInterceptor {Store :store }
228
+ syncer := & syncInterceptor {Store :api . Database }
222
229
interceptor := newUpdateSignallingInterceptor (syncer )
223
- mgr ,err := notifications .NewManager (cfg ,interceptor ,defaultHelpers (),metrics ,logger .Named ("manager" ))
230
+ mgr ,err := notifications .NewManager (cfg ,interceptor ,defaultHelpers (),metrics ,api . Logger .Named ("manager" ))
224
231
require .NoError (t ,err )
225
232
t .Cleanup (func () {
226
233
assert .NoError (t ,mgr .Stop (ctx ))
@@ -230,10 +237,10 @@ func TestPendingUpdatesMetric(t *testing.T) {
230
237
method :handler ,
231
238
})
232
239
233
- enq ,err := notifications .NewStoreEnqueuer (cfg ,store ,defaultHelpers (),logger .Named ("enqueuer" ),quartz .NewReal ())
240
+ enq ,err := notifications .NewStoreEnqueuer (cfg ,api . Database ,defaultHelpers (),api . Logger .Named ("enqueuer" ),quartz .NewReal ())
234
241
require .NoError (t ,err )
235
242
236
- user := createSampleUser (t ,store )
243
+ user := createSampleUser (t ,api . Database )
237
244
238
245
// WHEN: 2 notifications are enqueued, one of which will fail and one which will succeed
239
246
_ ,err = enq .Enqueue (ctx ,user .ID ,template ,map [string ]string {"type" :"success" },"test" )// this will succeed
@@ -279,7 +286,9 @@ func TestInflightDispatchesMetric(t *testing.T) {
279
286
t .Parallel ()
280
287
281
288
// SETUP
282
- ctx ,logger ,store := setupInMemory (t )
289
+ // nolint:gocritic // Unit test.
290
+ ctx := dbauthz .AsSystemRestricted (testutil .Context (t ,testutil .WaitSuperLong ))
291
+ _ ,_ ,api := coderdtest .NewWithAPI (t ,nil )
283
292
284
293
reg := prometheus .NewRegistry ()
285
294
metrics := notifications .NewMetrics (reg )
@@ -294,7 +303,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
294
303
cfg .RetryInterval = serpent .Duration (time .Hour )// Delay retries so they don't interfere.
295
304
cfg .StoreSyncInterval = serpent .Duration (time .Millisecond * 100 )
296
305
297
- mgr ,err := notifications .NewManager (cfg ,store ,defaultHelpers (),metrics ,logger .Named ("manager" ))
306
+ mgr ,err := notifications .NewManager (cfg ,api . Database ,defaultHelpers (),metrics ,api . Logger .Named ("manager" ))
298
307
require .NoError (t ,err )
299
308
t .Cleanup (func () {
300
309
assert .NoError (t ,mgr .Stop (ctx ))
@@ -307,15 +316,15 @@ func TestInflightDispatchesMetric(t *testing.T) {
307
316
method :delayer ,
308
317
})
309
318
310
- enq ,err := notifications .NewStoreEnqueuer (cfg ,store ,defaultHelpers (),logger .Named ("enqueuer" ),quartz .NewReal ())
319
+ enq ,err := notifications .NewStoreEnqueuer (cfg ,api . Database ,defaultHelpers (),api . Logger .Named ("enqueuer" ),quartz .NewReal ())
311
320
require .NoError (t ,err )
312
321
313
- user := createSampleUser (t ,store )
322
+ user := createSampleUser (t ,api . Database )
314
323
315
324
// WHEN: notifications are enqueued which will succeed (and be delayed during dispatch)
316
325
const msgCount = 2
317
326
for i := 0 ;i < msgCount ;i ++ {
318
- _ ,err = enq .Enqueue (ctx ,user .ID ,template ,map [string ]string {"type" :"success" },"test" )
327
+ _ ,err = enq .Enqueue (ctx ,user .ID ,template ,map [string ]string {"type" :"success" , "i" : strconv . Itoa ( i ) },"test" )
319
328
require .NoError (t ,err )
320
329
}
321
330
@@ -349,7 +358,10 @@ func TestCustomMethodMetricCollection(t *testing.T) {
349
358
// UpdateNotificationTemplateMethodByID only makes sense with a real database.
350
359
t .Skip ("This test requires postgres; it relies on business-logic only implemented in the database" )
351
360
}
352
- ctx ,logger ,store := setup (t )
361
+
362
+ // nolint:gocritic // Unit test.
363
+ ctx := dbauthz .AsSystemRestricted (testutil .Context (t ,testutil .WaitSuperLong ))
364
+ _ ,_ ,api := coderdtest .NewWithAPI (t ,nil )
353
365
354
366
var (
355
367
reg = prometheus .NewRegistry ()
@@ -364,7 +376,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
364
376
)
365
377
366
378
// GIVEN: a template whose notification method differs from the default.
367
- out ,err := store .UpdateNotificationTemplateMethodByID (ctx , database.UpdateNotificationTemplateMethodByIDParams {
379
+ out ,err := api . Database .UpdateNotificationTemplateMethodByID (ctx , database.UpdateNotificationTemplateMethodByIDParams {
368
380
ID :template ,
369
381
Method : database.NullNotificationMethod {NotificationMethod :customMethod ,Valid :true },
370
382
})
@@ -373,7 +385,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
373
385
374
386
// WHEN: two notifications (each with different templates) are enqueued.
375
387
cfg := defaultNotificationsConfig (defaultMethod )
376
- mgr ,err := notifications .NewManager (cfg ,store ,defaultHelpers (),metrics ,logger .Named ("manager" ))
388
+ mgr ,err := notifications .NewManager (cfg ,api . Database ,defaultHelpers (),metrics ,api . Logger .Named ("manager" ))
377
389
require .NoError (t ,err )
378
390
t .Cleanup (func () {
379
391
assert .NoError (t ,mgr .Stop (ctx ))
@@ -386,10 +398,10 @@ func TestCustomMethodMetricCollection(t *testing.T) {
386
398
customMethod :webhookHandler ,
387
399
})
388
400
389
- enq ,err := notifications .NewStoreEnqueuer (cfg ,store ,defaultHelpers (),logger .Named ("enqueuer" ),quartz .NewReal ())
401
+ enq ,err := notifications .NewStoreEnqueuer (cfg ,api . Database ,defaultHelpers (),api . Logger .Named ("enqueuer" ),quartz .NewReal ())
390
402
require .NoError (t ,err )
391
403
392
- user := createSampleUser (t ,store )
404
+ user := createSampleUser (t ,api . Database )
393
405
394
406
_ ,err = enq .Enqueue (ctx ,user .ID ,template ,map [string ]string {"type" :"success" },"test" )
395
407
require .NoError (t ,err )