-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmngr_test.go
409 lines (328 loc) · 10 KB
/
mngr_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
package mgoauth_test
import (
"github.com/kidstuff/auth-mongo-mngr"
"github.com/kidstuff/auth/authmodel"
"labix.org/v2/mgo"
"testing"
"time"
)
func newManager(dbname string) authmodel.Manager {
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
session.SetMode(mgo.Monotonic, true)
db := session.DB(dbname)
mgoauth.EnsureIndex(db)
return mgoauth.NewMgoManager(db)
}
func tearDown(dbname string) {
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
session.DB(dbname).DropDatabase()
}
func TestMain(t *testing.T) {
dbname := "mgoauth_test_user_mannager"
mngr := newManager(dbname)
defer tearDown(dbname)
uid := testManagerAddUser(t, mngr)
gid := testManagerAddGroupDetail(t, mngr)
testManagerUpdateUserDetail(t, mngr, uid, gid)
uid2 := testManagerAddUserDetail(t, mngr, gid)
testManagerDeleteGroup(t, mngr, gid, uid2)
testManagerDeleteUser(t, mngr, uid)
testManagerDeleteUser(t, mngr, uid2)
gid = testManagerAddGroupDetail(t, mngr)
testManagerFindAllUser(t, mngr, gid)
testManagerLogin(t, mngr, testManagerAddUser(t, mngr))
}
// testManagerAddUser check if add user work
func testManagerAddUser(t *testing.T, mngr authmodel.Manager) string {
ps := "zaq123456"
u1, err := mngr.AddUser("[email protected]", ps, true)
if err != nil {
t.Fatal("cannot create new user:", err)
}
if u1.LastActivity != u1.Profile.JoinDay || u1.LastActivity == nil {
t.Fatal("must initial the LastActivity and JoinDay")
}
_, err = mngr.AddUser("[email protected]", ps, true)
if err != authmodel.ErrDuplicateEmail {
t.Fatal("must check for duplicate email:", err)
}
err = mngr.ComparePassword(ps, u1.Pwd)
if err != nil {
t.Fatal("password hash error:", err)
}
return *u1.Id
}
// testManagerUpdateUserDetail test update user operation and check the password hash function.
func testManagerUpdateUserDetail(t *testing.T, mngr authmodel.Manager, uid, gid string) {
ps := "testing12345"
app := false
code := map[string]string{"tested": "notyet"}
err := mngr.UpdateUserDetail(uid, &ps, &app, []string{"testing"}, code, nil, []string{gid})
if err != nil {
t.Fatal("cannot update user detail:", err)
}
u, err := mngr.FindUser(uid)
if err != nil {
t.Fatal("Cannot find user:", err)
}
if *u.Approved != app {
t.Fatal("update user approved failed")
}
if len(u.Privileges) != 1 {
t.Fatal("update user privileges failed")
}
if u.Privileges[0] != "testing" {
t.Fatal("update user privileges failed")
}
if u.ConfirmCodes["tested"] != "notyet" {
t.Fatal("update user confirm code failed")
}
if len(u.Groups) != 1 {
t.Fatal("update user group failed")
}
if *u.Groups[0].Id != gid {
t.Fatal("update user group failed")
}
err = mngr.ComparePassword(ps, u.Pwd)
if err != nil {
t.Fatal("password hash error:", err)
}
}
// testManagerAddUserDetail check if add user operation work
func testManagerAddUserDetail(t *testing.T, mngr authmodel.Manager, gid string) string {
code := map[string]string{"tested": "notyet"}
u, err := mngr.AddUserDetail("[email protected]", "test123edc", true, []string{"testing"}, code, nil, []string{gid})
if err != nil {
t.Fatal("cannot update user detail:", err)
}
u, err = mngr.FindUser(*u.Id)
if err != nil {
t.Fatal("Cannot find user:", err)
}
if *u.Approved != true {
t.Fatal("update user approved failed")
}
if len(u.Privileges) != 1 {
t.Fatal("update user privileges failed")
}
if u.Privileges[0] != "testing" {
t.Fatal("update user privileges failed")
}
if u.ConfirmCodes["tested"] != "notyet" {
t.Fatal("update user confirm code failed")
}
if len(u.Groups) != 1 {
t.Fatal("update user group failed")
}
if *u.Groups[0].Id != gid {
t.Fatal("update user group failed")
}
return *u.Id
}
// testManagerDeleteUser check if delete user operation work.
func testManagerDeleteUser(t *testing.T, mngr authmodel.Manager, uid string) {
err := mngr.DeleteUser(uid)
if err != nil {
t.Fatal("delete user failed:", err)
}
_, err = mngr.FindUser(uid)
if err != authmodel.ErrNotFound {
t.Fatal("delete user not work")
}
}
// testManagerFindAllUser add 10 users the do test about partial and projection select
func testManagerFindAllUser(t *testing.T, mngr authmodel.Manager, gid string) {
users := make([]*authmodel.User, 10)
var err error
users[0], err = mngr.AddUser("[email protected]", "testing123edc", true)
if err != nil {
t.Fatal("cannot add user", err)
}
users[1], err = mngr.AddUser("[email protected]", "testing123edc", true)
if err != nil {
t.Fatal("cannot add user", err)
}
users[2], err = mngr.AddUser("[email protected]", "testing123edc", true)
if err != nil {
t.Fatal("cannot add user", err)
}
users[3], err = mngr.AddUser("[email protected]", "testing123edc", true)
if err != nil {
t.Fatal("cannot add user", err)
}
users[4], err = mngr.AddUser("[email protected]", "testing123edc", true)
if err != nil {
t.Fatal("cannot add user", err)
}
users[5], err = mngr.AddUser("[email protected]", "testing123edc", true)
if err != nil {
t.Fatal("cannot add user", err)
}
users[6], err = mngr.AddUser("[email protected]", "testing123edc", true)
if err != nil {
t.Fatal("cannot add user", err)
}
users[7], err = mngr.AddUser("[email protected]", "testing123edc", true)
if err != nil {
t.Fatal("cannot add user", err)
}
users[8], err = mngr.AddUserDetail("[email protected]", "testing123edc", true, nil, nil, nil, []string{gid})
if err != nil {
t.Fatal("cannot add user", err)
}
users[9], err = mngr.AddUserDetail("[email protected]", "testing123edc", true, nil, nil, nil, []string{gid})
if err != nil {
t.Fatal("cannot add user", err)
}
// get all user at the same time
// should return the full list of 10 users
userList, err := mngr.FindAllUser(-1, "", nil, nil)
if n := len(userList); n != 10 {
t.Fatal("get all user failed, expect 10 users, found", n)
}
// get all user belong to gid
userList, err = mngr.FindAllUser(-1, "", nil, []string{gid})
if n := len(userList); n != 2 {
t.Fatal("get all user with groups id failed, expect 2 users, found", n)
}
// get a single user with specific feilds.
userList, err = mngr.FindAllUser(1, "", []string{"Id", "Approved"}, nil)
if n := len(userList); n != 1 {
t.Fatal("get all user failed, expect 1 users, found", n)
}
if userList[0].Id == nil || userList[0].Approved == nil {
t.Fatal("get all user failed retrieve specifiec fields")
}
if u := userList[0]; u.Email != nil || u.Pwd != nil || u.LastActivity != nil {
t.Fatal("get all user failed not retrieved unspecific fields")
}
// get a part of user list
userList1, err := mngr.FindAllUser(5, "", nil, nil)
if n := len(userList1); n != 5 {
t.Fatal("get all user failed, expect 5 but got", n)
}
// get the other part
userList2, err := mngr.FindAllUser(5, *userList1[4].Id, nil, nil)
if n := len(userList2); n != 5 {
t.Fatal("get all user with offset id failed, expect 5 but got", n)
}
// and check if they are "paging" right
for _, u1 := range userList1 {
for _, u2 := range userList2 {
if *u1.Id == *u2.Id {
t.Fatal("get all user failed with limit and offset")
}
}
}
}
// testManagerAddGroupDetail simply check if the create group operation work.
func testManagerAddGroupDetail(t *testing.T, mngr authmodel.Manager) string {
info := authmodel.GroupInfo{}
desc := "testing privelege"
info.Description = &desc
g1, err := mngr.AddGroupDetail("staff", []string{"testing"}, &info)
if err != nil {
t.Fatal("cannot create new group:", err)
}
_, err = mngr.AddGroupDetail("staff", nil, nil)
if err == nil {
t.Fatal("must check for duplicated group name")
}
g1, err = mngr.FindGroup(*g1.Id)
if err != nil {
t.Fatal("cannot created group:", err)
}
if len(g1.Privileges) != 1 {
t.Fatal("add group privilege failed")
}
if g1.Privileges[0] != "testing" {
t.Fatal("add group privilege failed")
}
if g1.Info == nil || g1.Info.Description == nil || *g1.Info.Description != desc {
t.Fatal("add group info failed")
}
return *g1.Id
}
// testManagerDeleteGroup recieve a group id and a user id who belong to that group.
// After group delettion, check if the group infomation of in user account removed or not.
func testManagerDeleteGroup(t *testing.T, mngr authmodel.Manager, gid, uid string) {
err := mngr.DeleteGroup(gid)
if err != nil {
t.Fatal("delete group failed:", err)
}
u, err := mngr.FindUser(uid)
if err != nil {
t.Fatal("cannot find user:", err)
}
if len(u.Groups) > 0 {
t.Fatal("delete group must remove group info in user")
}
}
// testManagerLogin login the same user by uid 3 times to get 3 different tokens.
// Then use them to test the Logout method.
func testManagerLogin(t *testing.T, mngr authmodel.Manager, uid string) {
token, err := mngr.Login(uid, time.Minute)
if err != nil {
t.Fatal("cannot login:", err)
}
u, err := mngr.GetUser(token)
if err != nil {
t.Fatal("cannot get logged user:", err)
}
if *u.Id != uid {
t.Fatal("loged user and user returned not the same")
}
token2, err := mngr.Login(uid, time.Minute)
if err != nil {
t.Fatal("cannot login:", err)
}
u, err = mngr.GetUser(token2)
if err != nil {
t.Fatal("cannot get logged user:", err)
}
if *u.Id != uid {
t.Fatal("loged user and user returned not the same")
}
token3, err := mngr.Login(uid, time.Minute)
if err != nil {
t.Fatal("cannot login:", err)
}
u, err = mngr.GetUser(token3)
if err != nil {
t.Fatal("cannot get logged user:", err)
}
if *u.Id != uid {
t.Fatal("loged user and user returned not the same")
}
if token == token2 || token2 == token3 || token == token3 {
t.Fatal("token must not be the same")
}
err = mngr.Logout(token, false)
if err != nil {
t.Fatal("cannot logout user:", err)
}
u, err = mngr.GetUser(token)
if err == nil || u != nil {
t.Fatal("logout don't work")
}
err = mngr.Logout(token2, true)
if err != nil {
t.Fatal("cannot logout user:", err)
}
u, err = mngr.GetUser(token2)
if err == nil || u != nil {
t.Fatal("logout didn't work")
}
u, err = mngr.GetUser(token3)
if err == nil || u != nil {
t.Fatal("logout all user's session didn't work")
}
}