Skip to content

Commit 1791404

Browse files
chore: apply suggestions
1 parent 9736f13 commit 1791404

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

coderd/autobuild/lifecycle_executor_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func TestExecutorAutostartTemplateUpdated(t *testing.T) {
274274
}
275275

276276
if tc.expectNotification {
277-
sent := enqueuer.SentWithTemplateID(notifications.TemplateWorkspaceAutoUpdated)
277+
sent := enqueuer.Sent(notificationstest.WithTemplateID(notifications.TemplateWorkspaceAutoUpdated))
278278
require.Len(t, sent, 1)
279279
require.Equal(t, sent[0].UserID, workspace.OwnerID)
280280
require.Contains(t, sent[0].Targets, workspace.TemplateID)
@@ -285,7 +285,8 @@ func TestExecutorAutostartTemplateUpdated(t *testing.T) {
285285
require.Equal(t, "autobuild", sent[0].Labels["initiator"])
286286
require.Equal(t, "autostart", sent[0].Labels["reason"])
287287
} else {
288-
require.Empty(t, enqueuer.SentWithTemplateID(notifications.TemplateWorkspaceAutoUpdated))
288+
sent := enqueuer.Sent(notificationstest.WithTemplateID(notifications.TemplateWorkspaceAutoUpdated))
289+
require.Empty(t, sent)
289290
}
290291
})
291292
}

coderd/notifications/notificationstest/fake_enqueuer.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,31 @@ func (f *FakeEnqueuer) Clear() {
9292
f.sent = nil
9393
}
9494

95-
func (f *FakeEnqueuer) Sent() []*FakeNotification {
96-
f.mu.Lock()
97-
defer f.mu.Unlock()
98-
return append([]*FakeNotification{}, f.sent...)
99-
}
100-
101-
func (f *FakeEnqueuer) SentWithTemplateID(id uuid.UUID) []*FakeNotification {
95+
func (f *FakeEnqueuer) Sent(matchers ...func(*FakeNotification) bool) []*FakeNotification {
10296
f.mu.Lock()
10397
defer f.mu.Unlock()
10498

10599
sent := []*FakeNotification{}
106100
for _, notif := range f.sent {
107-
if notif.TemplateID == id {
101+
// Check this notification matches all given matchers
102+
matches := true
103+
for _, matcher := range matchers {
104+
if !matcher(notif) {
105+
matches = false
106+
break
107+
}
108+
}
109+
110+
if matches {
108111
sent = append(sent, notif)
109112
}
110113
}
114+
111115
return sent
112116
}
117+
118+
func WithTemplateID(id uuid.UUID) func(*FakeNotification) bool {
119+
return func(n *FakeNotification) bool {
120+
return n.TemplateID == id
121+
}
122+
}

coderd/workspaces_test.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,17 +577,47 @@ func TestPostWorkspacesByOrganization(t *testing.T) {
577577
enqueuer := notificationstest.FakeEnqueuer{}
578578
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true, NotificationsEnqueuer: &enqueuer})
579579
user := coderdtest.CreateFirstUser(t, client)
580+
memberClient, memberUser := coderdtest.CreateAnotherUser(t, client, user.OrganizationID)
580581

581582
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
582583
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
583584
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
584585

585-
workspace := coderdtest.CreateWorkspace(t, client, template.ID)
586+
workspace := coderdtest.CreateWorkspace(t, memberClient, template.ID)
587+
coderdtest.AwaitWorkspaceBuildJobCompleted(t, memberClient, workspace.LatestBuild.ID)
588+
589+
sent := enqueuer.Sent(notificationstest.WithTemplateID(notifications.TemplateWorkspaceCreated))
590+
require.Len(t, sent, 1)
591+
require.Equal(t, memberUser.ID, sent[0].UserID)
592+
require.Contains(t, sent[0].Targets, template.ID)
593+
require.Contains(t, sent[0].Targets, workspace.ID)
594+
require.Contains(t, sent[0].Targets, workspace.OrganizationID)
595+
require.Contains(t, sent[0].Targets, workspace.OwnerID)
596+
})
597+
598+
t.Run("CreateSendsNotificationToCorrectUser", func(t *testing.T) {
599+
t.Parallel()
600+
601+
enqueuer := notificationstest.FakeEnqueuer{}
602+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true, NotificationsEnqueuer: &enqueuer})
603+
user := coderdtest.CreateFirstUser(t, client)
604+
_, memberUser := coderdtest.CreateAnotherUser(t, client, user.OrganizationID)
605+
606+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
607+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
608+
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
609+
610+
ctx := testutil.Context(t, testutil.WaitShort)
611+
workspace, err := client.CreateUserWorkspace(ctx, memberUser.Username, codersdk.CreateWorkspaceRequest{
612+
TemplateID: template.ID,
613+
Name: coderdtest.RandomUsername(t),
614+
})
615+
require.NoError(t, err)
586616
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
587617

588-
sent := enqueuer.SentWithTemplateID(notifications.TemplateWorkspaceCreated)
618+
sent := enqueuer.Sent(notificationstest.WithTemplateID(notifications.TemplateWorkspaceCreated))
589619
require.Len(t, sent, 1)
590-
require.Equal(t, user.UserID, sent[0].UserID)
620+
require.Equal(t, memberUser.ID, sent[0].UserID)
591621
require.Contains(t, sent[0].Targets, template.ID)
592622
require.Contains(t, sent[0].Targets, workspace.ID)
593623
require.Contains(t, sent[0].Targets, workspace.OrganizationID)
@@ -3619,7 +3649,7 @@ func TestWorkspaceNotifications(t *testing.T) {
36193649

36203650
// Then
36213651
require.NoError(t, err, "mark workspace as dormant")
3622-
sent := notifyEnq.SentWithTemplateID(notifications.TemplateWorkspaceDormant)
3652+
sent := notifyEnq.Sent(notificationstest.WithTemplateID(notifications.TemplateWorkspaceDormant))
36233653
require.Len(t, sent, 1)
36243654
require.Equal(t, sent[0].TemplateID, notifications.TemplateWorkspaceDormant)
36253655
require.Equal(t, sent[0].UserID, workspace.OwnerID)
@@ -3657,7 +3687,7 @@ func TestWorkspaceNotifications(t *testing.T) {
36573687

36583688
// Then
36593689
require.NoError(t, err, "mark workspace as dormant")
3660-
require.Len(t, notifyEnq.SentWithTemplateID(notifications.TemplateWorkspaceDormant), 0)
3690+
require.Len(t, notifyEnq.Sent(notificationstest.WithTemplateID(notifications.TemplateWorkspaceDormant)), 0)
36613691
})
36623692

36633693
t.Run("ActivateDormantWorkspace", func(t *testing.T) {

0 commit comments

Comments
 (0)