forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications_test.go
More file actions
319 lines (254 loc) · 9.83 KB
/
Copy pathnotifications_test.go
File metadata and controls
319 lines (254 loc) · 9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package coderd_test
import (
"net/http"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
"github.com/coder/serpent"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/notifications"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)
func createOpts(t *testing.T) *coderdtest.Options {
t.Helper()
dt := coderdtest.DeploymentValues(t)
return &coderdtest.Options{
DeploymentValues: dt,
}
}
func TestUpdateNotificationsSettings(t *testing.T) {
t.Parallel()
t.Run("Permissions denied", func(t *testing.T) {
t.Parallel()
api := coderdtest.New(t, createOpts(t))
firstUser := coderdtest.CreateFirstUser(t, api)
anotherClient, _ := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)
// given
expected := codersdk.NotificationsSettings{
NotifierPaused: true,
}
ctx := testutil.Context(t, testutil.WaitShort)
// when
err := anotherClient.PutNotificationsSettings(ctx, expected)
// then
var sdkError *codersdk.Error
require.Error(t, err)
require.ErrorAsf(t, err, &sdkError, "error should be of type *codersdk.Error")
require.Equal(t, http.StatusForbidden, sdkError.StatusCode())
})
t.Run("Settings modified", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, createOpts(t))
_ = coderdtest.CreateFirstUser(t, client)
// given
expected := codersdk.NotificationsSettings{
NotifierPaused: true,
}
ctx := testutil.Context(t, testutil.WaitShort)
// when
err := client.PutNotificationsSettings(ctx, expected)
require.NoError(t, err)
// then
actual, err := client.GetNotificationsSettings(ctx)
require.NoError(t, err)
require.Equal(t, expected, actual)
})
t.Run("Settings not modified", func(t *testing.T) {
t.Parallel()
// Empty state: notifications Settings are undefined now (default).
client := coderdtest.New(t, createOpts(t))
_ = coderdtest.CreateFirstUser(t, client)
ctx := testutil.Context(t, testutil.WaitShort)
// Change the state: pause notifications
err := client.PutNotificationsSettings(ctx, codersdk.NotificationsSettings{
NotifierPaused: true,
})
require.NoError(t, err)
// Verify the state: notifications are paused.
actual, err := client.GetNotificationsSettings(ctx)
require.NoError(t, err)
require.True(t, actual.NotifierPaused)
// Change the stage again: notifications are paused.
expected := actual
err = client.PutNotificationsSettings(ctx, codersdk.NotificationsSettings{
NotifierPaused: true,
})
require.NoError(t, err)
// Verify the state: notifications are still paused, and there is no error returned.
actual, err = client.GetNotificationsSettings(ctx)
require.NoError(t, err)
require.Equal(t, expected.NotifierPaused, actual.NotifierPaused)
})
}
func TestNotificationPreferences(t *testing.T) {
t.Parallel()
t.Run("Initial state", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitSuperLong)
api := coderdtest.New(t, createOpts(t))
firstUser := coderdtest.CreateFirstUser(t, api)
// Given: a member in its initial state.
memberClient, member := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)
// When: calling the API.
prefs, err := memberClient.GetUserNotificationPreferences(ctx, member.ID)
require.NoError(t, err)
// Then: no preferences will be returned.
require.Len(t, prefs, 0)
})
t.Run("Insufficient permissions", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitSuperLong)
api := coderdtest.New(t, createOpts(t))
firstUser := coderdtest.CreateFirstUser(t, api)
// Given: 2 members.
_, member1 := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)
member2Client, _ := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)
// When: attempting to retrieve the preferences of another member.
_, err := member2Client.GetUserNotificationPreferences(ctx, member1.ID)
// Then: the API should reject the request.
var sdkError *codersdk.Error
require.Error(t, err)
require.ErrorAsf(t, err, &sdkError, "error should be of type *codersdk.Error")
// NOTE: ExtractUserParam gets in the way here, and returns a 400 Bad Request instead of a 403 Forbidden.
// This is not ideal, and we should probably change this behavior.
require.Equal(t, http.StatusBadRequest, sdkError.StatusCode())
})
t.Run("Admin may read any users' preferences", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitSuperLong)
api := coderdtest.New(t, createOpts(t))
firstUser := coderdtest.CreateFirstUser(t, api)
// Given: a member.
_, member := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)
// When: attempting to retrieve the preferences of another member as an admin.
prefs, err := api.GetUserNotificationPreferences(ctx, member.ID)
// Then: the API should not reject the request.
require.NoError(t, err)
require.Len(t, prefs, 0)
})
t.Run("Admin may update any users' preferences", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitSuperLong)
api := coderdtest.New(t, createOpts(t))
firstUser := coderdtest.CreateFirstUser(t, api)
// Given: a member.
memberClient, member := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)
// When: attempting to modify and subsequently retrieve the preferences of another member as an admin.
prefs, err := api.UpdateUserNotificationPreferences(ctx, member.ID, codersdk.UpdateUserNotificationPreferences{
TemplateDisabledMap: map[string]bool{
notifications.TemplateWorkspaceMarkedForDeletion.String(): true,
},
})
// Then: the request should succeed and the user should be able to query their own preferences to see the same result.
require.NoError(t, err)
require.Len(t, prefs, 1)
memberPrefs, err := memberClient.GetUserNotificationPreferences(ctx, member.ID)
require.NoError(t, err)
require.Len(t, memberPrefs, 1)
require.Equal(t, prefs[0].NotificationTemplateID, memberPrefs[0].NotificationTemplateID)
require.Equal(t, prefs[0].Disabled, memberPrefs[0].Disabled)
})
t.Run("Add preferences", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitSuperLong)
api := coderdtest.New(t, createOpts(t))
firstUser := coderdtest.CreateFirstUser(t, api)
// Given: a member with no preferences.
memberClient, member := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)
prefs, err := memberClient.GetUserNotificationPreferences(ctx, member.ID)
require.NoError(t, err)
require.Len(t, prefs, 0)
// When: attempting to add new preferences.
template := notifications.TemplateWorkspaceDeleted
prefs, err = memberClient.UpdateUserNotificationPreferences(ctx, member.ID, codersdk.UpdateUserNotificationPreferences{
TemplateDisabledMap: map[string]bool{
template.String(): true,
},
})
// Then: the returning preferences should be set as expected.
require.NoError(t, err)
require.Len(t, prefs, 1)
require.Equal(t, prefs[0].NotificationTemplateID, template)
require.True(t, prefs[0].Disabled)
})
t.Run("Modify preferences", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitSuperLong)
api := coderdtest.New(t, createOpts(t))
firstUser := coderdtest.CreateFirstUser(t, api)
// Given: a member with preferences.
memberClient, member := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)
prefs, err := memberClient.UpdateUserNotificationPreferences(ctx, member.ID, codersdk.UpdateUserNotificationPreferences{
TemplateDisabledMap: map[string]bool{
notifications.TemplateWorkspaceDeleted.String(): true,
notifications.TemplateWorkspaceDormant.String(): true,
},
})
require.NoError(t, err)
require.Len(t, prefs, 2)
// When: attempting to modify their preferences.
prefs, err = memberClient.UpdateUserNotificationPreferences(ctx, member.ID, codersdk.UpdateUserNotificationPreferences{
TemplateDisabledMap: map[string]bool{
notifications.TemplateWorkspaceDeleted.String(): true,
notifications.TemplateWorkspaceDormant.String(): false, // <--- this one was changed
},
})
require.NoError(t, err)
require.Len(t, prefs, 2)
// Then: the modified preferences should be set as expected.
var found bool
for _, p := range prefs {
switch p.NotificationTemplateID {
case notifications.TemplateWorkspaceDormant:
found = true
require.False(t, p.Disabled)
case notifications.TemplateWorkspaceDeleted:
require.True(t, p.Disabled)
}
}
require.True(t, found, "dormant notification preference was not found")
})
}
func TestNotificationDispatchMethods(t *testing.T) {
t.Parallel()
defaultOpts := createOpts(t)
webhookOpts := createOpts(t)
webhookOpts.DeploymentValues.Notifications.Method = serpent.String(database.NotificationMethodWebhook)
tests := []struct {
name string
opts *coderdtest.Options
expectedDefault string
}{
{
name: "default",
opts: defaultOpts,
expectedDefault: string(database.NotificationMethodSmtp),
},
{
name: "non-default",
opts: webhookOpts,
expectedDefault: string(database.NotificationMethodWebhook),
},
}
var allMethods []string
for _, nm := range database.AllNotificationMethodValues() {
allMethods = append(allMethods, string(nm))
}
slices.Sort(allMethods)
// nolint:paralleltest // Not since Go v1.22.
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitSuperLong)
api := coderdtest.New(t, tc.opts)
_ = coderdtest.CreateFirstUser(t, api)
resp, err := api.GetNotificationDispatchMethods(ctx)
require.NoError(t, err)
slices.Sort(resp.AvailableNotificationMethods)
require.EqualValues(t, resp.AvailableNotificationMethods, allMethods)
require.Equal(t, tc.expectedDefault, resp.DefaultNotificationMethod)
})
}
}