forked from xyproto/permissions2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserstate_test.go
213 lines (169 loc) · 5.92 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
package permissions
import (
"github.com/xyproto/pinterface"
"testing"
)
func TestPerm(t *testing.T) {
userstate := NewUserStateSimple()
userstate.AddUser("bob", "hunter1", "[email protected]")
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", "[email protected]")
if !userstate.HasUser("bob") {
t.Error("Error, user bob should exist")
}
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+")
}
userstate.RemoveUser("bob")
}
// Check if the functionality for backwards compatible hashing works
func TestPasswordNotBackward(t *testing.T) {
userstate := NewUserStateSimple()
userstate.SetPasswordAlgo("bcrypt")
userstate.AddUser("bob", "hunter1", "[email protected]")
if !userstate.HasUser("bob") {
t.Error("Error, user bob should exist")
}
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")
}
userstate.RemoveUser("bob")
}
func TestPasswordAlgoMatching(t *testing.T) {
userstate := NewUserStateSimple()
// generate two different password using the same credentials but different algos
userstate.SetPasswordAlgo("sha256")
sha256Hash := userstate.HashPassword("[email protected]", "textpassword")
userstate.SetPasswordAlgo("bcrypt")
bcryptHash := userstate.HashPassword("[email protected]", "textpassword")
// they shouldn't match
if sha256Hash == bcryptHash {
t.Error("Error, different algorithms should not have a password match")
}
}
func TestUserStateKeeper(t *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", "[email protected]")
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", "[email protected]")
if !userstate.HasUser("bob") {
t.Error("Error, user bob should exist")
}
// 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!")
}
userstate.RemoveUser("bob")
}