-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userstate_test.go
380 lines (308 loc) · 11 KB
/
userstate_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
package permissions
import (
"testing"
"time"
"github.com/xyproto/pinterface"
)
func TestPerm(t *testing.T) {
userstate := NewUserStateSimple()
userstate.AddUser("bob", "hunter1", "bob@zombo.com")
if !userstate.HasUser("bob") {
t.Error("Error, user bob should exist")
}
if userstate.IsConfirmed("bob") {
t.Error("Error, user bob should not be confirmed right now.")
}
userstate.MarkConfirmed("bob")
if !userstate.IsConfirmed("bob") {
t.Error("Error, user bob should be marked as confirmed right now.")
}
if userstate.IsAdmin("bob") {
t.Error("Error, user bob should not have admin rights")
}
userstate.SetAdminStatus("bob")
if !userstate.IsAdmin("bob") {
t.Error("Error, user bob should have admin rights")
}
userstate.RemoveUser("bob")
if userstate.HasUser("bob") {
t.Error("Error, user bob should not exist")
}
}
func TestPasswordBasic(t *testing.T) {
userstate := NewUserStateSimple()
// Assert that the default password algorithm is "bcrypt+"
if userstate.PasswordAlgo() != "bcrypt+" {
t.Error("Error, bcrypt+ should be the default password algorithm")
}
// Set password algorithm
userstate.SetPasswordAlgo("sha256")
// Assert that the algorithm is now sha256
if userstate.PasswordAlgo() != "sha256" {
t.Error("Error, setting password algorithm failed")
}
}
func TestPasswordBasic2(t *testing.T) {
// Test the other method for connecting to Redis
userstate, err := NewUserStateSimple2()
if err != nil {
t.Error("Error, " + err.Error())
}
// Assert that the default password algorithm is "bcrypt+"
if userstate.PasswordAlgo() != "bcrypt+" {
t.Error("Error, bcrypt+ should be the default password algorithm")
}
// Set password algorithm
userstate.SetPasswordAlgo("sha256")
// Assert that the algorithm is now sha256
if userstate.PasswordAlgo() != "sha256" {
t.Error("Error, setting password algorithm failed")
}
}
// Check if the functionality for backwards compatible hashing works
func TestPasswordBackward(t *testing.T) {
userstate := NewUserStateSimple()
userstate.SetPasswordAlgo("sha256")
userstate.AddUser("bob", "hunter1", "bob@zombo.com")
if !userstate.HasUser("bob") {
t.Error("Error, user bob should exist")
}
defer userstate.RemoveUser("bob")
userstate.SetPasswordAlgo("sha256")
if !userstate.CorrectPassword("bob", "hunter1") {
t.Error("Error, the sha256 password really is correct")
}
userstate.SetPasswordAlgo("bcrypt")
if userstate.CorrectPassword("bob", "hunter1") {
t.Error("Error, the password as stored as sha256, not bcrypt")
}
userstate.SetPasswordAlgo("bcrypt+")
if !userstate.CorrectPassword("bob", "hunter1") {
t.Error("Error, the sha256 password is not correct when checking with bcrypt+")
}
}
// Check if the functionality for backwards compatible hashing works
func TestPasswordNotBackward(t *testing.T) {
userstate := NewUserStateSimple()
userstate.SetPasswordAlgo("bcrypt")
userstate.AddUser("bob", "hunter1", "bob@zombo.com")
if !userstate.HasUser("bob") {
t.Error("Error, user bob should exist")
}
defer userstate.RemoveUser("bob")
userstate.SetPasswordAlgo("sha256")
if userstate.CorrectPassword("bob", "hunter1") {
t.Error("Error, the password is stored as bcrypt, should not be okay with sha256")
}
userstate.SetPasswordAlgo("bcrypt")
if !userstate.CorrectPassword("bob", "hunter1") {
t.Error("Error, the password should be correct when checking with bcrypt")
}
}
func TestPasswordAlgoMatching(t *testing.T) {
userstate := NewUserStateSimple()
// generate two different password using the same credentials but different algos
userstate.SetPasswordAlgo("sha256")
sha256Hash := userstate.HashPassword("testuser@example.com", "textpassword")
userstate.SetPasswordAlgo("bcrypt")
bcryptHash := userstate.HashPassword("testuser@example.com", "textpassword")
// they shouldn't match
if sha256Hash == bcryptHash {
t.Error("Error, different algorithms should not have a password match")
}
}
func TestUserStateKeeper(_ *testing.T) {
userstate := NewUserStateSimple()
// Check that the userstate qualifies for the IUserState interface
var _ pinterface.IUserState = userstate
}
func TestHostPassword(t *testing.T) {
//userstate := NewUserStateWithPassword("localhost", "foobared")
userstate := NewUserStateWithPassword("localhost", "")
userstate.AddUser("bob", "hunter1", "bob@zombo.com")
if !userstate.HasUser("bob") {
t.Error("Error, user bob should exist")
}
// Remove bob
userstate.RemoveUser("bob")
if userstate.HasUser("bob") {
t.Error("Error, user bob should not exist")
}
}
func TestChangePassword(t *testing.T) {
userstate := NewUserStateSimple()
userstate.AddUser("bob", "hunter1", "bob@zombo.com")
if !userstate.HasUser("bob") {
t.Error("Error, user bob should exist")
}
defer userstate.RemoveUser("bob")
// Check that the password is "hunter1"
if !userstate.CorrectPassword("bob", "hunter1") {
t.Error("Error, password is incorrect: should be hunter1!")
}
// Check that the password is not "hunter2"
if userstate.CorrectPassword("bob", "hunter2") {
t.Error("Error, password is incorrect: should not be hunter2!")
}
// Change the password for user "bob" to "hunter2"
username := "bob"
password := "hunter2"
passwordHash := userstate.HashPassword(username, password)
userstate.Users().Set(username, "password", passwordHash)
// Check that the password is "hunter2"
if !userstate.CorrectPassword("bob", "hunter2") {
t.Error("Error, password is incorrect: should be hunter2!")
}
// Check that the password is not "hunter1"
if userstate.CorrectPassword("bob", "hunter1") {
t.Error("Error, password is incorrect: should not be hunter1!")
}
// Change the password back to "hunter1"
userstate.SetPassword("bob", "hunter1")
// Check that the password is "hunter1"
if !userstate.CorrectPassword("bob", "hunter1") {
t.Error("Error, password is incorrect: should be hunter1!")
}
// Check that the password is not "hunter2"
if userstate.CorrectPassword("bob", "hunter2") {
t.Error("Error, password is incorrect: should not be hunter2!")
}
if len(userstate.Properties("bob")) != 5 {
t.Error("Error, there should be 5 properties on bob now!")
}
}
func TestTokens(t *testing.T) {
userstate := NewUserStateSimple()
// Add bob
userstate.AddUser("bob", "hunter1", "bob@zombo.com")
if !userstate.HasUser("bob") {
t.Error("Error, user bob should exist")
}
defer userstate.RemoveUser("bob")
// Set a token that will expire in 200 milliseconds
userstate.SetToken("bob", "asdf123", time.Millisecond*200)
// Check that the token is "asdf123"
retval, err := userstate.GetToken("bob")
if err != nil {
t.Error("Error, could not get token")
}
if retval != "asdf123" {
t.Error("Error, token is incorrect: should be asdf123!")
}
// Wait 400 milliseconds
time.Sleep(time.Millisecond * 400)
// Check that the token is now gone
retval, err = userstate.GetToken("bob")
if err == nil || retval != "" {
t.Error("Error, token is incorrect: should be gone!")
}
}
func TestEmail(t *testing.T) {
userstate := NewUserStateSimple()
userstate.AddUser("bob", "hunter1", "bob@zombo.com")
username, err := userstate.HasEmail("bob@zombo.com")
if err != nil {
t.Error("Error, the e-mail should exist:", err)
}
if username != "bob" {
t.Error("Error, the e-mail address should belong to bob, but belongs to:", username)
}
username, err = userstate.HasEmail("rob@zombo.com")
if err != ErrNotFound {
t.Error("Error, the e-mail should not exist: " + username)
}
}
func TestTiming(t *testing.T) {
userstate := NewUserStateSimple()
userstate.SetPasswordAlgo("bcrypt")
userstate.AddUser("bob", "hunter1", "bob@zombo.com")
if !userstate.HasUser("bob") {
t.Error("Error, user bob should exist")
}
defer userstate.RemoveUser("bob")
// First run takes a bit longer
_ = userstate.CorrectPassword("bob", "asdf")
start1 := time.Now()
if !userstate.CorrectPassword("bob", "hunter1") {
t.Error("Error, the password IS correct")
}
elapsed1 := time.Since(start1)
start2 := time.Now()
if userstate.CorrectPassword("bob", "_") {
t.Error("Error, the password is NOT correct")
}
elapsed2 := time.Since(start2)
start3 := time.Now()
if userstate.CorrectPassword("bob", "abc123asfdasdfasfasdfqwertyqwertyqwerty") {
t.Error("Error, the password is NOT correct")
}
elapsed3 := time.Since(start3)
start4 := time.Now()
if userstate.CorrectPassword("bob", "abc123asfdasdfasfasdfqwertyqwertyqwertyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy") {
t.Error("Error, the password is NOT correct")
}
elapsed4 := time.Since(start4)
// The tolerance is +- one of the first elapsed times. Hard to find a good value here, since the values differ from machine to machine.
baseMin := elapsed1 - time.Duration(elapsed1)
baseMax := elapsed1 + time.Duration(elapsed1)
if elapsed2 < baseMin || elapsed2 > baseMax {
t.Error("Checking passwords of different lengths should not take much longer or shorter")
}
if elapsed3 < baseMin || elapsed3 > baseMax {
t.Error("Checking passwords of different lengths should not take much longer or shorter")
}
if elapsed4 < baseMin || elapsed4 > baseMax {
t.Error("Checking passwords of different lengths should not take much longer or shorter")
}
//fmt.Println(baseMin, base_max, elapsed1, elapsed2, elapsed3, elapsed4)
}
func TestHasEmail(t *testing.T) {
const (
username1 = "alice"
password1 = "password1"
email1 = "alice@example.com"
username2 = "bob"
password2 = "password2"
email2 = "bob@example.com"
username3 = "charlie"
password3 = "password3"
email3 = "charlie@example.com"
nonExistentEmail = "dave@example.com"
)
userstate := NewUserStateSimple()
defer userstate.Close()
// Add test users
userstate.AddUser(username1, password1, email1)
defer userstate.RemoveUser(username1)
userstate.AddUser(username2, password2, email2)
defer userstate.RemoveUser(username2)
userstate.AddUser(username3, password3, email3)
defer userstate.RemoveUser(username3)
// Test HasEmail with an existing email
username, err := userstate.HasEmail(email2)
if err != nil {
t.Errorf("Expected to find email '%s', but got error: %v", email2, err)
} else if username != username2 {
t.Errorf("Expected username '%s', but got '%s'", username2, username)
} else {
t.Logf("Successfully found username '%s' for email '%s'", username, email2)
}
// Test HasEmail with a non-existent email
username, err = userstate.HasEmail(nonExistentEmail)
if err != ErrNotFound {
t.Errorf("Expected ErrNotFound for email '%s', but got error: %v", nonExistentEmail, err)
} else if username != "" {
t.Errorf("Expected empty username for non-existent email, but got '%s'", username)
} else {
t.Logf("Correctly did not find username for non-existent email '%s'", nonExistentEmail)
}
// Test HasEmail with an empty email
username, err = userstate.HasEmail("")
if err != ErrNotFound {
t.Errorf("Expected ErrNotFound for empty email, but got error: %v", err)
} else if username != "" {
t.Errorf("Expected empty username for empty email, but got '%s'", username)
} else {
t.Log("Correctly handled empty email input")
}
}