@@ -12,6 +12,8 @@ import (
12
12
13
13
"github.com/coder/coder/v2/cli/clitest"
14
14
"github.com/coder/coder/v2/coderd/coderdtest"
15
+ "github.com/coder/coder/v2/coderd/notifications"
16
+ "github.com/coder/coder/v2/coderd/notifications/notificationstest"
15
17
"github.com/coder/coder/v2/codersdk"
16
18
"github.com/coder/coder/v2/testutil"
17
19
)
@@ -109,3 +111,59 @@ func TestPauseNotifications_RegularUser(t *testing.T) {
109
111
require .NoError (t , err )
110
112
require .False (t , settings .NotifierPaused ) // still running
111
113
}
114
+
115
+ func TestNotificationsTest (t * testing.T ) {
116
+ t .Parallel ()
117
+
118
+ t .Run ("OwnerCanSendTestNotification" , func (t * testing.T ) {
119
+ t .Parallel ()
120
+
121
+ notifyEnq := & notificationstest.FakeEnqueuer {}
122
+
123
+ // Given: An owner user.
124
+ ownerClient := coderdtest .New (t , & coderdtest.Options {
125
+ DeploymentValues : coderdtest .DeploymentValues (t ),
126
+ NotificationsEnqueuer : notifyEnq ,
127
+ })
128
+ _ = coderdtest .CreateFirstUser (t , ownerClient )
129
+
130
+ // When: The owner user attempts to send the test notification.
131
+ inv , root := clitest .New (t , "notifications" , "test" )
132
+ clitest .SetupConfig (t , ownerClient , root )
133
+
134
+ // Then: we expect a notification to be sent.
135
+ err := inv .Run ()
136
+ require .NoError (t , err )
137
+
138
+ sent := notifyEnq .Sent (notificationstest .WithTemplateID (notifications .TemplateTestNotification ))
139
+ require .Len (t , sent , 1 )
140
+ })
141
+
142
+ t .Run ("MemberCannotSendTestNotification" , func (t * testing.T ) {
143
+ t .Parallel ()
144
+
145
+ notifyEnq := & notificationstest.FakeEnqueuer {}
146
+
147
+ // Given: A member user.
148
+ ownerClient := coderdtest .New (t , & coderdtest.Options {
149
+ DeploymentValues : coderdtest .DeploymentValues (t ),
150
+ NotificationsEnqueuer : notifyEnq ,
151
+ })
152
+ ownerUser := coderdtest .CreateFirstUser (t , ownerClient )
153
+ memberClient , _ := coderdtest .CreateAnotherUser (t , ownerClient , ownerUser .OrganizationID )
154
+
155
+ // When: The member user attempts to send the test notification.
156
+ inv , root := clitest .New (t , "notifications" , "test" )
157
+ clitest .SetupConfig (t , memberClient , root )
158
+
159
+ // Then: we expect an error and no notifications to be sent.
160
+ err := inv .Run ()
161
+ var sdkError * codersdk.Error
162
+ require .Error (t , err )
163
+ require .ErrorAsf (t , err , & sdkError , "error should be of type *codersdk.Error" )
164
+ assert .Equal (t , http .StatusForbidden , sdkError .StatusCode ())
165
+
166
+ sent := notifyEnq .Sent (notificationstest .WithTemplateID (notifications .TemplateTestNotification ))
167
+ require .Len (t , sent , 0 )
168
+ })
169
+ }
0 commit comments