Skip to content

Commit 16b60cf

Browse files
committed
fake payload
1 parent 7b5874d commit 16b60cf

File tree

1 file changed

+13
-36
lines changed

1 file changed

+13
-36
lines changed

coderd/notifications/notificationstest/fake_enqueuer.go

+13-36
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ type Enqueuer interface {
2121
}
2222

2323
type FakeEnqueuer struct {
24-
// authorizer rbac.Authorizer
2524
mu sync.Mutex
2625
sent []*FakeNotification
2726
Store database.Store
@@ -35,40 +34,6 @@ type FakeNotification struct {
3534
Targets []uuid.UUID
3635
}
3736

38-
/*
39-
// TODO: replace this with actual calls to dbauthz.
40-
// See: https://github.com/coder/coder/issues/15481
41-
func (f *FakeEnqueuer) assertRBACNoLock(ctx context.Context) {
42-
if f.mu.TryLock() {
43-
panic("Developer error: do not call assertRBACNoLock outside of a mutex lock!")
44-
}
45-
46-
// If we get here, we are locked.
47-
if f.authorizer == nil {
48-
f.authorizer = rbac.NewStrictCachingAuthorizer(prometheus.NewRegistry())
49-
}
50-
51-
act, ok := dbauthz.ActorFromContext(ctx)
52-
if !ok {
53-
panic("Developer error: no actor in context, you may need to use dbauthz.AsNotifier(ctx)")
54-
}
55-
56-
for _, a := range []policy.Action{policy.ActionCreate, policy.ActionRead} {
57-
err := f.authorizer.Authorize(ctx, act, a, rbac.ResourceNotificationMessage)
58-
if err == nil {
59-
return
60-
}
61-
62-
if rbac.IsUnauthorizedError(err) {
63-
panic(fmt.Sprintf("Developer error: not authorized to %s %s. "+
64-
"Ensure that you are using dbauthz.AsXXX with an actor that has "+
65-
"policy.ActionCreate on rbac.ResourceNotificationMessage", a, rbac.ResourceNotificationMessage.Type))
66-
}
67-
panic("Developer error: failed to check auth:" + err.Error())
68-
}
69-
}
70-
*/
71-
7237
func (f *FakeEnqueuer) Enqueue(ctx context.Context, userID, templateID uuid.UUID, labels map[string]string, createdBy string, targets ...uuid.UUID) ([]uuid.UUID, error) {
7338
return f.EnqueueWithData(ctx, userID, templateID, labels, nil, createdBy, targets...)
7439
}
@@ -82,11 +47,23 @@ func (f *FakeEnqueuer) enqueueWithDataLock(ctx context.Context, userID, template
8247
defer f.mu.Unlock()
8348
id := uuid.New()
8449
var err error
50+
51+
// To avoid a duplicate notification, make a unique-enough payload out of what
52+
// we have.
53+
fakePayload := make(map[string]any)
54+
fakePayload["template_id"] = templateID
55+
fakePayload["labels"] = labels
56+
fakePayload["data"] = data
57+
fakePayloadBytes, err := json.Marshal(fakePayload)
58+
if err != nil {
59+
return nil, err
60+
}
61+
8562
if err = f.Store.EnqueueNotificationMessage(ctx, database.EnqueueNotificationMessageParams{
8663
ID: id,
8764
UserID: userID,
8865
NotificationTemplateID: templateID,
89-
Payload: json.RawMessage(`{}`),
66+
Payload: fakePayloadBytes,
9067
Method: database.NotificationMethodInbox,
9168
Targets: targets,
9269
CreatedBy: createdBy,

0 commit comments

Comments
 (0)