Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit542fff7

Browse files
authored
chore: improve notifications tests (#13863)
1 parentc8484b4 commit542fff7

File tree

5 files changed

+133
-92
lines changed

5 files changed

+133
-92
lines changed

‎coderd/database/dbauthz/dbauthz_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2493,7 +2493,8 @@ func (s *MethodTestSuite) TestSystemFunctions() {
24932493
}))
24942494
s.Run("FetchNewMessageMetadata",s.Subtest(func(db database.Store,check*expects) {
24952495
// TODO: update this test once we have a specific role for notifications
2496-
check.Args(database.FetchNewMessageMetadataParams{}).Asserts(rbac.ResourceSystem,policy.ActionRead)
2496+
u:=dbgen.User(s.T(),db, database.User{})
2497+
check.Args(database.FetchNewMessageMetadataParams{UserID:u.ID}).Asserts(rbac.ResourceSystem,policy.ActionRead)
24972498
}))
24982499
s.Run("GetNotificationMessagesByStatus",s.Subtest(func(db database.Store,check*expects) {
24992500
// TODO: update this test once we have a specific role for notifications

‎coderd/database/dbmem/dbmem.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,15 +1229,15 @@ func (*FakeQuerier) BulkMarkNotificationMessagesFailed(_ context.Context, arg da
12291229
iferr!=nil {
12301230
return0,err
12311231
}
1232-
return-1,nil
1232+
returnint64(len(arg.IDs)),nil
12331233
}
12341234

12351235
func (*FakeQuerier)BulkMarkNotificationMessagesSent(_ context.Context,arg database.BulkMarkNotificationMessagesSentParams) (int64,error) {
12361236
err:=validateDatabaseType(arg)
12371237
iferr!=nil {
12381238
return0,err
12391239
}
1240-
return-1,nil
1240+
returnint64(len(arg.IDs)),nil
12411241
}
12421242

12431243
func (*FakeQuerier)CleanTailnetCoordinators(_ context.Context)error {
@@ -1864,20 +1864,31 @@ func (q *FakeQuerier) FavoriteWorkspace(_ context.Context, arg uuid.UUID) error
18641864
returnnil
18651865
}
18661866

1867-
func (*FakeQuerier)FetchNewMessageMetadata(_ context.Context,arg database.FetchNewMessageMetadataParams) (database.FetchNewMessageMetadataRow,error) {
1867+
func (q*FakeQuerier)FetchNewMessageMetadata(_ context.Context,arg database.FetchNewMessageMetadataParams) (database.FetchNewMessageMetadataRow,error) {
18681868
err:=validateDatabaseType(arg)
18691869
iferr!=nil {
18701870
return database.FetchNewMessageMetadataRow{},err
18711871
}
18721872

1873+
user,err:=q.getUserByIDNoLock(arg.UserID)
1874+
iferr!=nil {
1875+
return database.FetchNewMessageMetadataRow{},xerrors.Errorf("fetch user: %w",err)
1876+
}
1877+
1878+
// Mimic COALESCE in query
1879+
userName:=user.Name
1880+
ifuserName=="" {
1881+
userName=user.Username
1882+
}
1883+
18731884
actions,err:=json.Marshal([]types.TemplateAction{{URL:"http://xyz.com",Label:"XYZ"}})
18741885
iferr!=nil {
18751886
return database.FetchNewMessageMetadataRow{},err
18761887
}
18771888

18781889
return database.FetchNewMessageMetadataRow{
1879-
UserEmail:"test@test.com",
1880-
UserName:"Testy McTester",
1890+
UserEmail:user.Email,
1891+
UserName:userName,
18811892
NotificationName:"Some notification",
18821893
Actions:actions,
18831894
UserID:arg.UserID,

‎coderd/notifications/manager_test.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@ import (
1212
"github.com/stretchr/testify/require"
1313
"golang.org/x/xerrors"
1414

15-
"cdr.dev/slog"
16-
"cdr.dev/slog/sloggers/slogtest"
1715
"github.com/coder/coder/v2/coderd/database"
1816
"github.com/coder/coder/v2/coderd/database/dbgen"
19-
"github.com/coder/coder/v2/coderd/database/dbmem"
20-
"github.com/coder/coder/v2/coderd/database/dbtestutil"
2117
"github.com/coder/coder/v2/coderd/notifications"
2218
"github.com/coder/coder/v2/coderd/notifications/dispatch"
2319
"github.com/coder/coder/v2/coderd/notifications/types"
@@ -29,17 +25,15 @@ func TestBufferedUpdates(t *testing.T) {
2925
t.Parallel()
3026

3127
// setup
32-
if!dbtestutil.WillUsePostgres() {
33-
t.Skip("This test requires postgres")
34-
}
28+
ctx,logger,db:=setupInMemory(t)
3529

36-
ctx,logger,db:=setup(t)
3730
interceptor:=&bulkUpdateInterceptor{Store:db}
3831
santa:=&santaHandler{}
3932

4033
cfg:=defaultNotificationsConfig(database.NotificationMethodSmtp)
4134
cfg.StoreSyncInterval=serpent.Duration(time.Hour)// Ensure we don't sync the store automatically.
4235

36+
// GIVEN: a manager which will pass or fail notifications based on their "nice" labels
4337
mgr,err:=notifications.NewManager(cfg,interceptor,logger.Named("notifications-manager"))
4438
require.NoError(t,err)
4539
mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{
@@ -50,18 +44,17 @@ func TestBufferedUpdates(t *testing.T) {
5044

5145
user:=dbgen.User(t,db, database.User{})
5246

53-
//given
47+
//WHEN: notifications are enqueued which should succeed and fail
5448
_,err=enq.Enqueue(ctx,user.ID,notifications.TemplateWorkspaceDeleted,map[string]string{"nice":"true"},"")// Will succeed.
5549
require.NoError(t,err)
5650
_,err=enq.Enqueue(ctx,user.ID,notifications.TemplateWorkspaceDeleted,map[string]string{"nice":"true"},"")// Will succeed.
5751
require.NoError(t,err)
5852
_,err=enq.Enqueue(ctx,user.ID,notifications.TemplateWorkspaceDeleted,map[string]string{"nice":"false"},"")// Will fail.
5953
require.NoError(t,err)
6054

61-
// when
6255
mgr.Run(ctx)
6356

64-
//then
57+
//THEN:
6558

6659
const (
6760
expectedSuccess=2
@@ -99,15 +92,18 @@ func TestBufferedUpdates(t *testing.T) {
9992
funcTestBuildPayload(t*testing.T) {
10093
t.Parallel()
10194

102-
// given
95+
// SETUP
96+
ctx,logger,db:=setupInMemory(t)
97+
98+
// GIVEN: a set of helpers to be injected into the templates
10399
constlabel="Click here!"
104100
consturl="http://xyz.com/"
105101
helpers:=map[string]any{
106102
"my_label":func()string {returnlabel },
107103
"my_url":func()string {returnurl },
108104
}
109105

110-
db:=dbmem.New()
106+
// GIVEN: an enqueue interceptor which returns mock metadata
111107
interceptor:=newEnqueueInterceptor(db,
112108
// Inject custom message metadata to influence the payload construction.
113109
func() database.FetchNewMessageMetadataRow {
@@ -130,17 +126,14 @@ func TestBuildPayload(t *testing.T) {
130126
}
131127
})
132128

133-
logger:=slogtest.Make(t,&slogtest.Options{IgnoreErrors:true,IgnoredErrorIs: []error{}}).Leveled(slog.LevelDebug)
134129
enq,err:=notifications.NewStoreEnqueuer(defaultNotificationsConfig(database.NotificationMethodSmtp),interceptor,helpers,logger.Named("notifications-enqueuer"))
135130
require.NoError(t,err)
136131

137-
ctx:=testutil.Context(t,testutil.WaitShort)
138-
139-
// when
132+
// WHEN: a notification is enqueued
140133
_,err=enq.Enqueue(ctx,uuid.New(),notifications.TemplateWorkspaceDeleted,nil,"test")
141134
require.NoError(t,err)
142135

143-
//then
136+
//THEN: expect that a payload will be constructed and have the expected values
144137
payload:=testutil.RequireRecvCtx(ctx,t,interceptor.payload)
145138
require.Len(t,payload.Actions,1)
146139
require.Equal(t,label,payload.Actions[0].Label)
@@ -150,12 +143,14 @@ func TestBuildPayload(t *testing.T) {
150143
funcTestStopBeforeRun(t*testing.T) {
151144
t.Parallel()
152145

153-
ctx:=context.Background()
154-
logger:=slogtest.Make(t,&slogtest.Options{IgnoreErrors:true,IgnoredErrorIs: []error{}}).Leveled(slog.LevelDebug)
155-
mgr,err:=notifications.NewManager(defaultNotificationsConfig(database.NotificationMethodSmtp),dbmem.New(),logger.Named("notifications-manager"))
146+
// SETUP
147+
ctx,logger,db:=setupInMemory(t)
148+
149+
// GIVEN: a standard manager
150+
mgr,err:=notifications.NewManager(defaultNotificationsConfig(database.NotificationMethodSmtp),db,logger.Named("notifications-manager"))
156151
require.NoError(t,err)
157152

158-
//Call stop before notifier is started withRun().
153+
//THEN: validate that the manager can be stopped safely withoutRun() having been called yet
159154
require.Eventually(t,func()bool {
160155
assert.NoError(t,mgr.Stop(ctx))
161156
returntrue

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp