@@ -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,55 @@ 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
+ 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