@@ -2,21 +2,29 @@ package notificationstest
2
2
3
3
import (
4
4
"context"
5
- "fmt"
5
+ "encoding/json"
6
+ "strings"
6
7
"sync"
7
8
8
9
"github.com/google/uuid"
9
- "github.com/prometheus/client_golang/prometheus"
10
10
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 "
14
14
)
15
15
16
+ type Enqueuer interface {
17
+ notifications.Enqueuer
18
+
19
+ Sent (matchers ... func (* FakeNotification ) bool ) []* FakeNotification
20
+ Clear ()
21
+ }
22
+
16
23
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
20
28
}
21
29
22
30
type FakeNotification struct {
@@ -27,6 +35,7 @@ type FakeNotification struct {
27
35
Targets []uuid.UUID
28
36
}
29
37
38
+ /*
30
39
// TODO: replace this with actual calls to dbauthz.
31
40
// See: https://github.com/coder/coder/issues/15481
32
41
func (f *FakeEnqueuer) assertRBACNoLock(ctx context.Context) {
@@ -58,6 +67,7 @@ func (f *FakeEnqueuer) assertRBACNoLock(ctx context.Context) {
58
67
panic("Developer error: failed to check auth:" + err.Error())
59
68
}
60
69
}
70
+ */
61
71
62
72
func (f * FakeEnqueuer ) Enqueue (ctx context.Context , userID , templateID uuid.UUID , labels map [string ]string , createdBy string , targets ... uuid.UUID ) ([]uuid.UUID , error ) {
63
73
return f .EnqueueWithData (ctx , userID , templateID , labels , nil , createdBy , targets ... )
@@ -70,7 +80,25 @@ func (f *FakeEnqueuer) EnqueueWithData(ctx context.Context, userID, templateID u
70
80
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 ) {
71
81
f .mu .Lock ()
72
82
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
+ }
74
102
75
103
f .sent = append (f .sent , & FakeNotification {
76
104
UserID : userID ,
@@ -81,8 +109,7 @@ func (f *FakeEnqueuer) enqueueWithDataLock(ctx context.Context, userID, template
81
109
Targets : targets ,
82
110
})
83
111
84
- id := uuid .New ()
85
- return []uuid.UUID {id }, nil
112
+ return []uuid.UUID {id }, err
86
113
}
87
114
88
115
func (f * FakeEnqueuer ) Clear () {
0 commit comments