-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockokta_test.go
452 lines (338 loc) · 13 KB
/
mockokta_test.go
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package mockokta
import (
"context"
"math/rand"
"reflect"
"testing"
"time"
"github.com/okta/okta-sdk-golang/v2/okta"
"github.com/stretchr/testify/assert"
)
func TestGroupResource_CreateGroup(t *testing.T) {
t.Run("should not create group with empty name", func(t *testing.T) {
groupNameArg := ""
client := NewClient()
group := NewGroup(groupNameArg)
_, _, err := client.Group.CreateGroup(context.TODO(), *group)
if err == nil {
t.Errorf("expected error but didn't get one")
}
})
t.Run("should not create group with invalid length", func(t *testing.T) {
groupNameArg := RandStringRunes(256)
client := NewClient()
group := NewGroup(groupNameArg)
_, _, err := client.Group.CreateGroup(context.TODO(), *group)
if err == nil {
t.Errorf("expected error but didn't get one")
}
})
t.Run("should not create group if it already exists", func(t *testing.T) {
groupNameArg := "TestGroup"
client := NewClient()
group := NewGroup(groupNameArg)
client.Group.CreateGroup(context.TODO(), *group)
_, _, err := client.Group.CreateGroup(context.TODO(), *group)
if err == nil {
t.Errorf("expected error but didn't get one")
}
})
t.Run("should create group", func(t *testing.T) {
groupNameArg := "TestGroup"
client := NewClient()
group := NewGroup(groupNameArg)
client.Group.CreateGroup(context.TODO(), *group)
want := group
got := client.Group.Groups[0]
if reflect.DeepEqual(got, want) {
t.Fatalf("got %v want %v", got, want)
}
})
}
func TestGroupResource_ListGroups(t *testing.T) {
group1NameArg := "TestGroup1"
group2NameArg := "TestGroup2"
client := NewClient()
group1 := NewGroup(group1NameArg)
group2 := NewGroup(group2NameArg)
client.Group.Groups = append(client.Group.Groups, group1, group2)
want := []*okta.Group{group1, group2}
got, _, _ := client.Group.ListGroups(context.TODO(), nil)
assert.ElementsMatch(t, got, want)
}
func TestGroupResource_AssignRoleToGroup(t *testing.T) {
t.Run("should not assign role with invalid name to group", func(t *testing.T) {
groupNameArg := "TestGroup"
roleArg := "Invalid_Role"
roleRequest := NewAssignRoleRequest(roleArg)
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
_, _, err := client.Group.AssignRoleToGroup(context.TODO(), group.Id, roleRequest, nil)
if err == nil {
t.Errorf("Expected err, but didn't get one")
}
})
t.Run("should return error if group doesn't exist", func(t *testing.T) {
groupNameArg := "NonexistentGroup"
client := NewClient()
roleRequest := RandAdminRoleRequest()
_, _, err := client.Group.AssignRoleToGroup(context.TODO(), groupNameArg, roleRequest, nil)
if err == nil {
t.Errorf("Expected err, but didn't get one")
}
})
t.Run("should return error if role exists for group", func(t *testing.T) {
groupNameArg := "TestGroup"
roleRequest := RandAdminRoleRequest()
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
client.Group.AssignRoleToGroup(context.TODO(), group.Id, roleRequest, nil)
_, _, err := client.Group.AssignRoleToGroup(context.TODO(), group.Id, roleRequest, nil)
if err == nil {
t.Errorf("Expected err, but didn't get one")
}
})
t.Run("should assign role to group", func(t *testing.T) {
groupNameArg := "TestGroup"
roleRequest := RandAdminRoleRequest()
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
client.Group.AssignRoleToGroup(context.TODO(), group.Id, roleRequest, nil)
got := client.Group.GroupContainsRole(*group, roleRequest.Type)
if got != true {
t.Errorf("expected group %+v to contain role %v but was not found", client.Group, roleRequest.Type)
}
})
}
// This is because of a bug where we were returning []*okta.Role(nil) instead of []*okta.Role
// so the solution was to create the map with make, and append to it. Otherwise production
// code could have a bug if checking a variable against nil, which would pass here since
// []*okta.Role(nil) will == nil but []*okta.Role{} won't
func TestGroupResource_ListGroupAssignedRoles(t *testing.T) {
t.Run("should return empty slice instead of slice(nil)", func(t *testing.T) {
groupNameArg := "NonExistentGroup"
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
want := []*okta.Role{}
got, _, _ := client.ListGroupAssignedRoles(context.TODO(), group.Id, nil)
if reflect.TypeOf(got) != reflect.TypeOf(want) || got == nil {
t.Errorf("got %#v want %#v", got, want)
}
})
t.Run("should list assigned roles", func(t *testing.T) {
groupNameArg := "TestGroup"
roleTypeArg1 := "SUPER_ADMIN"
roleTypeArg2 := "GROUP_ADMIN"
roleRequest1 := NewAssignRoleRequest(roleTypeArg1)
roleRequest2 := NewAssignRoleRequest(roleTypeArg2)
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
role1, _, _ := client.Group.AssignRoleToGroup(context.TODO(), group.Id, roleRequest1, nil)
role2, _, _ := client.Group.AssignRoleToGroup(context.TODO(), group.Id, roleRequest2, nil)
want := []*okta.Role{role1, role2}
got, _, _ := client.Group.ListGroupAssignedRoles(context.TODO(), group.Id, nil)
if !reflect.DeepEqual(got, want) {
t.Errorf("expected roles %v to match %v", got, want)
}
})
}
func TestUserResource_CreateUser(t *testing.T) {
t.Run("should err if user exists", func(t *testing.T) {
userEmail := "[email protected]"
client := NewClient()
client.User.CreateUser(userEmail)
_, err := client.User.CreateUser(userEmail)
if err == nil {
t.Errorf("expected error but didn't get one")
}
})
t.Run("should create user", func(t *testing.T) {
userEmail := "[email protected]"
client := NewClient()
want, _ := client.User.CreateUser(userEmail)
got, _ := client.User.GetUserByEmail(userEmail)
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
}
func TestGroupResource_AddUserToGroup(t *testing.T) {
t.Run("should err if group doesn't exist", func(t *testing.T) {
userEmailArg := "[email protected]"
client := NewClient()
user, _ := client.User.CreateUser(userEmailArg)
_, err := client.Group.AddUserToGroup(context.TODO(), "1", user.Id)
if err == nil {
t.Errorf("expected error but didn't get one")
}
})
t.Run("should err if user doesn't exist", func(t *testing.T) {
groupNameArg := "TestGroup"
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
_, err := client.Group.AddUserToGroup(context.TODO(), group.Id, "1")
if err == nil {
t.Errorf("expected error but didn't get one %v", err)
}
})
t.Run("should add user to group", func(t *testing.T) {
userEmailArg := "[email protected]"
groupNameArg := "TestGroup"
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
user, _ := client.User.CreateUser(userEmailArg)
client.Group.AddUserToGroup(context.TODO(), group.Id, user.Id)
if !client.Group.GroupContainsUser(*group, userEmailArg) {
t.Errorf("expected group %v to contain user %v but it did not", groupNameArg, userEmailArg)
}
})
}
func TestGroupResource_RemoveUserFromGroup(t *testing.T) {
t.Run("should err if group doesn't exist", func(t *testing.T) {
userEmailArg := "[email protected]"
client := NewClient()
user, _ := client.User.CreateUser(userEmailArg)
_, err := client.Group.RemoveUserFromGroup(context.TODO(), "1", user.Id)
if err == nil {
t.Errorf("expected error but didn't get one")
}
})
t.Run("should err if user doesn't exist", func(t *testing.T) {
groupNameArg := "TestGroup"
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
_, err := client.Group.RemoveUserFromGroup(context.TODO(), group.Id, "1")
if err == nil {
t.Errorf("expected error but didn't get one")
}
})
t.Run("should remove user from group", func(t *testing.T) {
userEmailArg := "[email protected]"
groupNameArg := "TestGroup"
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
user, _ := client.User.CreateUser(userEmailArg)
client.Group.AddUserToGroup(context.TODO(), group.Id, user.Id)
client.Group.RemoveUserFromGroup(context.TODO(), group.Id, user.Id)
if client.Group.GroupContainsUser(*group, userEmailArg) {
t.Errorf("expected group %v to not contain user %v", groupNameArg, userEmailArg)
}
})
t.Run("should not remove other users", func(t *testing.T) {
userEmailArg1 := "[email protected]"
userEmailArg2 := "[email protected]"
userEmailArg3 := "[email protected]"
groupNameArg := "TestGroup"
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
user1, _ := client.User.CreateUser(userEmailArg1)
user2, _ := client.User.CreateUser(userEmailArg2)
user3, _ := client.User.CreateUser(userEmailArg3)
client.Group.AddUserToGroup(context.TODO(), group.Id, user1.Id)
client.Group.AddUserToGroup(context.TODO(), group.Id, user2.Id)
client.Group.AddUserToGroup(context.TODO(), group.Id, user3.Id)
client.Group.RemoveUserFromGroup(context.TODO(), group.Id, user2.Id)
want := 2
got := len(client.Group.GroupUsers[group.Profile.Name])
if got != want {
t.Errorf("expected group %v to have %d users but found %d", groupNameArg, want, got)
}
})
}
func TestGroupResource_DeleteGroup(t *testing.T) {
t.Run("should err if group doesn't exist", func(t *testing.T) {
groupIDArg := "NonExistentId"
client := NewClient()
_, err := client.Group.DeleteGroup(context.TODO(), groupIDArg)
if err == nil {
t.Errorf("expected error but didn't get one")
}
})
t.Run("should delete group", func(t *testing.T) {
groupNameArg := "TestGroup"
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
client.Group.DeleteGroup(context.TODO(), group.Id)
want := []*okta.Group{}
got, _, _ := client.Group.ListGroups(context.TODO(), nil)
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
t.Run("should not delete extra groups", func(t *testing.T) {
groupNameArg1 := "TestGroup1"
groupNameArg2 := "TestGroup2"
groupNameArg3 := "TestGroup3"
client := NewClient()
group1, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg1))
group2, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg2))
group3, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg3))
client.Group.DeleteGroup(context.TODO(), group2.Id)
want := []*okta.Group{group1, group3}
got, _, _ := client.Group.ListGroups(context.TODO(), nil)
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
}
func TestGroupResource_ListGroupUsers(t *testing.T) {
t.Run("list existing users", func(t *testing.T) {
userEmailArg1 := "[email protected]"
userEmailArg2 := "[email protected]"
userEmailArg3 := "[email protected]"
groupNameArg := "TestGroup"
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
user1, _ := client.User.CreateUser(userEmailArg1)
user2, _ := client.User.CreateUser(userEmailArg2)
user3, _ := client.User.CreateUser(userEmailArg3)
client.Group.AddUserToGroup(context.TODO(), group.Id, user1.Id)
client.Group.AddUserToGroup(context.TODO(), group.Id, user2.Id)
client.Group.AddUserToGroup(context.TODO(), group.Id, user3.Id)
want := []*okta.User{user1, user2, user3}
got, _, _ := client.Group.ListGroupUsers(context.TODO(), group.Id, nil)
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
t.Run("empty users should return empty slice and not nil", func(t *testing.T) {
groupNameArg := "TestGroup"
client := NewClient()
group, _, _ := client.Group.CreateGroup(context.TODO(), *NewGroup(groupNameArg))
want := []*okta.User{}
got, _, _ := client.Group.ListGroupUsers(context.TODO(), group.Id, nil)
if !reflect.DeepEqual(got, want) {
t.Errorf("got %#v want %#v", got, want)
}
})
}
func TestUserResource_ListUsers(t *testing.T) {
t.Run("should list users", func(t *testing.T) {
client := NewClient()
userEmailArg1 := "TestUser1"
userEmailArg2 := "TestUser2"
user1, _ := client.User.CreateUser(userEmailArg1)
user2, _ := client.User.CreateUser(userEmailArg2)
want := []*okta.User{user1, user2}
got, _, _ := client.User.ListUsers(context.TODO(), nil)
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
t.Run("empty slice should not be nil", func(t *testing.T) {
client := NewClient()
got, _, _ := client.User.ListUsers(context.TODO(), nil)
if got == nil {
t.Errorf("got %#v want non nil slice", got)
}
})
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandStringRunes(n int) string {
rand.Seed(time.Now().UnixNano())
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}