Skip to content

Commit 4a2ec9b

Browse files
chore: add cli tests
1 parent fc49ec4 commit 4a2ec9b

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

cli/notifications_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ 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/notifications"
16+
"github.com/coder/coder/v2/coderd/notifications/notificationstest"
1517
"github.com/coder/coder/v2/codersdk"
1618
"github.com/coder/coder/v2/testutil"
1719
)
@@ -109,3 +111,55 @@ func TestPauseNotifications_RegularUser(t *testing.T) {
109111
require.NoError(t, err)
110112
require.False(t, settings.NotifierPaused) // still running
111113
}
114+
115+
func TestNotificationsTest(t *testing.T) {
116+
t.Parallel()
117+
118+
t.Run("OwnerCanSendTestNotification", func(t *testing.T) {
119+
notifyEnq := &notificationstest.FakeEnqueuer{}
120+
121+
// Given: An owner user.
122+
ownerClient := coderdtest.New(t, &coderdtest.Options{
123+
DeploymentValues: coderdtest.DeploymentValues(t),
124+
NotificationsEnqueuer: notifyEnq,
125+
})
126+
_ = coderdtest.CreateFirstUser(t, ownerClient)
127+
128+
// When: The owner user attempts to send the test notification.
129+
inv, root := clitest.New(t, "notifications", "test")
130+
clitest.SetupConfig(t, ownerClient, root)
131+
132+
// Then: we expect a notification to be sent.
133+
err := inv.Run()
134+
require.NoError(t, err)
135+
136+
sent := notifyEnq.Sent(notificationstest.WithTemplateID(notifications.TemplateTestNotification))
137+
require.Len(t, sent, 1)
138+
})
139+
140+
t.Run("MemberCannotSendTestNotification", func(t *testing.T) {
141+
notifyEnq := &notificationstest.FakeEnqueuer{}
142+
143+
// Given: A member user.
144+
ownerClient := coderdtest.New(t, &coderdtest.Options{
145+
DeploymentValues: coderdtest.DeploymentValues(t),
146+
NotificationsEnqueuer: notifyEnq,
147+
})
148+
ownerUser := coderdtest.CreateFirstUser(t, ownerClient)
149+
memberClient, _ := coderdtest.CreateAnotherUser(t, ownerClient, ownerUser.OrganizationID)
150+
151+
// When: The member user attempts to send the test notification.
152+
inv, root := clitest.New(t, "notifications", "test")
153+
clitest.SetupConfig(t, memberClient, root)
154+
155+
// Then: we expect an error and no notifications to be sent.
156+
err := inv.Run()
157+
var sdkError *codersdk.Error
158+
require.Error(t, err)
159+
require.ErrorAsf(t, err, &sdkError, "error should be of type *codersdk.Error")
160+
assert.Equal(t, http.StatusForbidden, sdkError.StatusCode())
161+
162+
sent := notifyEnq.Sent(notificationstest.WithTemplateID(notifications.TemplateTestNotification))
163+
require.Len(t, sent, 0)
164+
})
165+
}

0 commit comments

Comments
 (0)