Skip to content

Commit 7b5874d

Browse files
committed
use notifications.FakeEnqueuer to wrap StoreEnqueuer
1 parent d78215c commit 7b5874d

16 files changed

+235
-102
lines changed

cli/notifications_test.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/coder/coder/v2/cli/clitest"
1414
"github.com/coder/coder/v2/coderd/coderdtest"
15+
"github.com/coder/coder/v2/coderd/database/dbtestutil"
1516
"github.com/coder/coder/v2/coderd/notifications"
1617
"github.com/coder/coder/v2/coderd/notifications/notificationstest"
1718
"github.com/coder/coder/v2/codersdk"
@@ -118,12 +119,15 @@ func TestNotificationsTest(t *testing.T) {
118119
t.Run("OwnerCanSendTestNotification", func(t *testing.T) {
119120
t.Parallel()
120121

121-
notifyEnq := &notificationstest.FakeEnqueuer{}
122+
db, ps := dbtestutil.NewDB(t)
123+
notifyEnq := &notificationstest.FakeEnqueuer{Store: db}
122124

123125
// Given: An owner user.
124126
ownerClient := coderdtest.New(t, &coderdtest.Options{
125127
DeploymentValues: coderdtest.DeploymentValues(t),
126128
NotificationsEnqueuer: notifyEnq,
129+
Database: db,
130+
Pubsub: ps,
127131
})
128132
_ = coderdtest.CreateFirstUser(t, ownerClient)
129133

@@ -142,12 +146,15 @@ func TestNotificationsTest(t *testing.T) {
142146
t.Run("MemberCannotSendTestNotification", func(t *testing.T) {
143147
t.Parallel()
144148

145-
notifyEnq := &notificationstest.FakeEnqueuer{}
149+
db, ps := dbtestutil.NewDB(t)
150+
notifyEnq := &notificationstest.FakeEnqueuer{Store: db}
146151

147152
// Given: A member user.
148153
ownerClient := coderdtest.New(t, &coderdtest.Options{
149154
DeploymentValues: coderdtest.DeploymentValues(t),
150155
NotificationsEnqueuer: notifyEnq,
156+
Database: db,
157+
Pubsub: ps,
151158
})
152159
ownerUser := coderdtest.CreateFirstUser(t, ownerClient)
153160
memberClient, _ := coderdtest.CreateAnotherUser(t, ownerClient, ownerUser.OrganizationID)

coderd/agentapi/resources_monitoring_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func resourceMonitorAPI(t *testing.T) (*agentapi.ResourcesMonitoringAPI, databas
5555
ResourceID: resource.ID,
5656
})
5757

58-
notifyEnq := &notificationstest.FakeEnqueuer{}
58+
notifyEnq := &notificationstest.FakeEnqueuer{Store: db}
5959
clock := quartz.NewMock(t)
6060

6161
return &agentapi.ResourcesMonitoringAPI{

coderd/autobuild/lifecycle_executor_test.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,16 @@ func TestExecutorAutostartTemplateUpdated(t *testing.T) {
188188
tickCh = make(chan time.Time)
189189
statsCh = make(chan autobuild.Stats)
190190
logger = slogtest.Make(t, &slogtest.Options{IgnoreErrors: !tc.expectStart}).Leveled(slog.LevelDebug)
191-
enqueuer = notificationstest.FakeEnqueuer{}
191+
db, ps = dbtestutil.NewDB(t)
192+
enqueuer = notificationstest.FakeEnqueuer{Store: db}
192193
client = coderdtest.New(t, &coderdtest.Options{
193194
AutobuildTicker: tickCh,
194195
IncludeProvisionerDaemon: true,
195196
AutobuildStats: statsCh,
196197
Logger: &logger,
197198
NotificationsEnqueuer: &enqueuer,
199+
Database: db,
200+
Pubsub: ps,
198201
})
199202
// Given: we have a user with a workspace that has autostart enabled
200203
workspace = mustProvisionWorkspace(t, client, func(cwr *codersdk.CreateWorkspaceRequest) {
@@ -1052,7 +1055,7 @@ func TestExecutorFailedWorkspace(t *testing.T) {
10521055
ProvisionApply: echo.ApplyFailed,
10531056
})
10541057
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
1055-
ctr.FailureTTLMillis = ptr.Ref[int64](failureTTL.Milliseconds())
1058+
ctr.FailureTTLMillis = ptr.Ref(failureTTL.Milliseconds())
10561059
})
10571060
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
10581061
ws := coderdtest.CreateWorkspace(t, client, template.ID)
@@ -1103,7 +1106,7 @@ func TestExecutorInactiveWorkspace(t *testing.T) {
11031106
})
11041107
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
11051108
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
1106-
ctr.TimeTilDormantMillis = ptr.Ref[int64](inactiveTTL.Milliseconds())
1109+
ctr.TimeTilDormantMillis = ptr.Ref(inactiveTTL.Milliseconds())
11071110
})
11081111
ws := coderdtest.CreateWorkspace(t, client, template.ID)
11091112
build := coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, ws.LatestBuild.ID)
@@ -1125,13 +1128,16 @@ func TestNotifications(t *testing.T) {
11251128
var (
11261129
ticker = make(chan time.Time)
11271130
statCh = make(chan autobuild.Stats)
1128-
notifyEnq = notificationstest.FakeEnqueuer{}
1131+
db, ps = dbtestutil.NewDB(t)
1132+
notifyEnq = notificationstest.FakeEnqueuer{Store: db}
11291133
timeTilDormant = time.Minute
11301134
client = coderdtest.New(t, &coderdtest.Options{
11311135
AutobuildTicker: ticker,
11321136
AutobuildStats: statCh,
11331137
IncludeProvisionerDaemon: true,
11341138
NotificationsEnqueuer: &notifyEnq,
1139+
Database: db,
1140+
Pubsub: ps,
11351141
TemplateScheduleStore: schedule.MockTemplateScheduleStore{
11361142
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions) (database.Template, error) {
11371143
template.TimeTilDormant = int64(options.TimeTilDormant)

coderd/coderdtest/coderdtest.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
262262
}
263263

264264
if options.NotificationsEnqueuer == nil {
265-
options.NotificationsEnqueuer = &notificationstest.FakeEnqueuer{}
265+
options.NotificationsEnqueuer = &notificationstest.FakeEnqueuer{Store: options.Database}
266266
}
267267

268268
accessControlStore := &atomic.Pointer[dbauthz.AccessControlStore]{}
@@ -331,7 +331,7 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
331331
t.Cleanup(closeBatcher)
332332
}
333333
if options.NotificationsEnqueuer == nil {
334-
options.NotificationsEnqueuer = &notificationstest.FakeEnqueuer{}
334+
options.NotificationsEnqueuer = &notificationstest.FakeEnqueuer{Store: options.Database}
335335
}
336336

337337
if options.OneTimePasscodeValidityPeriod == 0 {

coderd/notifications/notificationstest/fake_enqueuer.go

+38-11
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,29 @@ package notificationstest
22

33
import (
44
"context"
5-
"fmt"
5+
"encoding/json"
6+
"strings"
67
"sync"
78

89
"github.com/google/uuid"
9-
"github.com/prometheus/client_golang/prometheus"
1010

11-
"github.com/coder/coder/v2/coderd/database/dbauthz"
12-
"github.com/coder/coder/v2/coderd/rbac"
13-
"github.com/coder/coder/v2/coderd/rbac/policy"
11+
"github.com/coder/coder/v2/coderd/database"
12+
"github.com/coder/coder/v2/coderd/database/dbtime"
13+
"github.com/coder/coder/v2/coderd/notifications"
1414
)
1515

16+
type Enqueuer interface {
17+
notifications.Enqueuer
18+
19+
Sent(matchers ...func(*FakeNotification) bool) []*FakeNotification
20+
Clear()
21+
}
22+
1623
type FakeEnqueuer struct {
17-
authorizer rbac.Authorizer
18-
mu sync.Mutex
19-
sent []*FakeNotification
24+
// authorizer rbac.Authorizer
25+
mu sync.Mutex
26+
sent []*FakeNotification
27+
Store database.Store
2028
}
2129

2230
type FakeNotification struct {
@@ -27,6 +35,7 @@ type FakeNotification struct {
2735
Targets []uuid.UUID
2836
}
2937

38+
/*
3039
// TODO: replace this with actual calls to dbauthz.
3140
// See: https://github.com/coder/coder/issues/15481
3241
func (f *FakeEnqueuer) assertRBACNoLock(ctx context.Context) {
@@ -58,6 +67,7 @@ func (f *FakeEnqueuer) assertRBACNoLock(ctx context.Context) {
5867
panic("Developer error: failed to check auth:" + err.Error())
5968
}
6069
}
70+
*/
6171

6272
func (f *FakeEnqueuer) Enqueue(ctx context.Context, userID, templateID uuid.UUID, labels map[string]string, createdBy string, targets ...uuid.UUID) ([]uuid.UUID, error) {
6373
return f.EnqueueWithData(ctx, userID, templateID, labels, nil, createdBy, targets...)
@@ -70,7 +80,25 @@ func (f *FakeEnqueuer) EnqueueWithData(ctx context.Context, userID, templateID u
7080
func (f *FakeEnqueuer) enqueueWithDataLock(ctx context.Context, userID, templateID uuid.UUID, labels map[string]string, data map[string]any, createdBy string, targets ...uuid.UUID) ([]uuid.UUID, error) {
7181
f.mu.Lock()
7282
defer f.mu.Unlock()
73-
f.assertRBACNoLock(ctx)
83+
id := uuid.New()
84+
var err error
85+
if err = f.Store.EnqueueNotificationMessage(ctx, database.EnqueueNotificationMessageParams{
86+
ID: id,
87+
UserID: userID,
88+
NotificationTemplateID: templateID,
89+
Payload: json.RawMessage(`{}`),
90+
Method: database.NotificationMethodInbox,
91+
Targets: targets,
92+
CreatedBy: createdBy,
93+
CreatedAt: dbtime.Now(),
94+
}); err != nil {
95+
// TODO: just use the real thing. See https://github.com/coder/coder/issues/15481
96+
if strings.Contains(err.Error(), notifications.ErrCannotEnqueueDisabledNotification.Error()) {
97+
err = notifications.ErrCannotEnqueueDisabledNotification
98+
} else if database.IsUniqueViolation(err, database.UniqueNotificationMessagesDedupeHashIndex) {
99+
err = notifications.ErrDuplicate
100+
}
101+
}
74102

75103
f.sent = append(f.sent, &FakeNotification{
76104
UserID: userID,
@@ -81,8 +109,7 @@ func (f *FakeEnqueuer) enqueueWithDataLock(ctx context.Context, userID, template
81109
Targets: targets,
82110
})
83111

84-
id := uuid.New()
85-
return []uuid.UUID{id}, nil
112+
return []uuid.UUID{id}, err
86113
}
87114

88115
func (f *FakeEnqueuer) Clear() {

coderd/notifications/reports/generator_internal_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ func setup(t *testing.T) (context.Context, slog.Logger, database.Store, pubsub.P
509509
ctx := dbauthz.AsSystemRestricted(context.Background())
510510
logger := slogtest.Make(t, &slogtest.Options{})
511511
db, ps := dbtestutil.NewDB(t)
512-
notifyEnq := &notificationstest.FakeEnqueuer{}
512+
notifyEnq := &notificationstest.FakeEnqueuer{Store: db}
513513
clk := quartz.NewMock(t)
514514
return ctx, logger, db, ps, notifyEnq, clk
515515
}

coderd/notifications_test.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/coder/coder/v2/coderd/coderdtest"
1313
"github.com/coder/coder/v2/coderd/database"
14+
"github.com/coder/coder/v2/coderd/database/dbtestutil"
1415
"github.com/coder/coder/v2/coderd/notifications"
1516
"github.com/coder/coder/v2/coderd/notifications/notificationstest"
1617
"github.com/coder/coder/v2/codersdk"
@@ -330,10 +331,13 @@ func TestNotificationTest(t *testing.T) {
330331

331332
ctx := testutil.Context(t, testutil.WaitShort)
332333

333-
notifyEnq := &notificationstest.FakeEnqueuer{}
334+
db, ps := dbtestutil.NewDB(t)
335+
notifyEnq := &notificationstest.FakeEnqueuer{Store: db}
334336
ownerClient := coderdtest.New(t, &coderdtest.Options{
335337
DeploymentValues: coderdtest.DeploymentValues(t),
336338
NotificationsEnqueuer: notifyEnq,
339+
Database: db,
340+
Pubsub: ps,
337341
})
338342

339343
// Given: A user with owner permissions.
@@ -353,10 +357,13 @@ func TestNotificationTest(t *testing.T) {
353357

354358
ctx := testutil.Context(t, testutil.WaitShort)
355359

356-
notifyEnq := &notificationstest.FakeEnqueuer{}
360+
db, ps := dbtestutil.NewDB(t)
361+
notifyEnq := &notificationstest.FakeEnqueuer{Store: db}
357362
ownerClient := coderdtest.New(t, &coderdtest.Options{
358363
DeploymentValues: coderdtest.DeploymentValues(t),
359364
NotificationsEnqueuer: notifyEnq,
365+
Database: db,
366+
Pubsub: ps,
360367
})
361368

362369
// Given: A user without owner permissions.

0 commit comments

Comments
 (0)