forked from apexskier/httpauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend_test.go
209 lines (191 loc) · 5.43 KB
/
backend_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
package httpauth_test
import (
"bytes"
"testing"
"github.com/lootch/httpauth"
)
func testBackendAuthorizer(t *testing.T, backend httpauth.AuthBackend) {
roles := make(map[string]httpauth.Role)
roles["user"] = 40
roles["admin"] = 80
_, err := httpauth.NewAuthorizer(backend, []byte("testkey"), "user", roles)
if err != nil {
t.Fatal(err)
}
}
func testBackendSaveUser(t *testing.T, backend httpauth.AuthBackend) {
user2 := httpauth.UserData{"username2", "email2", []byte("passwordhash2"), "role2"}
if err := backend.SaveUser(user2); err != nil {
t.Fatalf("SaveUser sql error: %v", err)
}
user := httpauth.UserData{"username", "email", []byte("passwordhash"), "role"}
if err := backend.SaveUser(user); err != nil {
t.Fatalf("SaveUser sql error: %v", err)
}
}
func testBackendNewAuthBackend_existing(t *testing.T, backend httpauth.AuthBackend) {
user, err := backend.User("username")
if err != nil {
t.Fatal("Secondary backend failed")
}
if user.Username != "username" {
t.Fatal("Username not correct.")
}
if user.Email != "email" {
t.Fatal("User email not correct.")
}
if !bytes.Equal(user.Hash, []byte("passwordhash")) {
t.Fatal("User password not correct.")
}
}
func testBackendUser_existing(t *testing.T, backend httpauth.AuthBackend) {
if user, err := backend.User("username"); err == nil {
if user.Username != "username" {
t.Error("Username not correct.")
}
if user.Email != "email" {
t.Error("User email not correct.")
}
if !bytes.Equal(user.Hash, []byte("passwordhash")) {
t.Error("User password not correct.")
}
} else {
t.Errorf("User not found: %v", err)
}
if user, err := backend.User("username2"); err == nil {
if user.Username != "username2" {
t.Error("Username not correct.")
}
if user.Email != "email2" {
t.Error("User email not correct.")
}
if !bytes.Equal(user.Hash, []byte("passwordhash2")) {
t.Error("User password not correct.")
}
} else {
t.Fatalf("User not found: %v", err)
}
}
func testBackendUser_notexisting(t *testing.T, backend httpauth.AuthBackend) {
if _, err := backend.User("notexist"); err != httpauth.ErrMissingUser {
t.Fatal("Not existing user found.")
}
}
func testBackendUsers(t *testing.T, backend httpauth.AuthBackend) {
var (
u1 httpauth.UserData
u2 httpauth.UserData
)
users, err := backend.Users()
if err != nil {
t.Fatal(err.Error())
}
if len(users) != 2 {
t.Fatal("Wrong amount of users found.")
}
if users[0].Username == "username" {
u1 = users[0]
u2 = users[1]
} else if users[1].Username == "username" {
u1 = users[1]
u2 = users[0]
} else {
t.Fatal("One of the users not found.")
}
if u1.Username != "username" {
t.Error("Username not correct.")
}
if u1.Email != "email" {
t.Error("User email not correct.")
}
if !bytes.Equal(u1.Hash, []byte("passwordhash")) {
t.Error("User password not correct.")
}
if u2.Username != "username2" {
t.Error("Username not correct.")
}
if u2.Email != "email2" {
t.Error("User email not correct.")
}
if !bytes.Equal(u2.Hash, []byte("passwordhash2")) {
t.Error("User password not correct.")
}
}
func testBackendUpdateUser(t *testing.T, backend httpauth.AuthBackend) {
user2 := httpauth.UserData{"username", "newemail", []byte("newpassword"), "newrole"}
if err := backend.SaveUser(user2); err != nil {
t.Fatalf("SaveUser sql error: %v", err)
}
u2, err := backend.User("username")
if err != nil {
t.Fatal("Updated user not found")
}
if u2.Username != "username" {
t.Fatal("Username not correct.")
}
if u2.Email != "newemail" {
t.Fatal("User email not correct.")
}
if u2.Role != "newrole" {
t.Fatalf("User role not correct: found %v, expected %v", u2.Role, "newrole")
}
if !bytes.Equal(u2.Hash, []byte("newpassword")) {
t.Fatal("User password not correct.")
}
}
func testBackendDeleteUser(t *testing.T, backend httpauth.AuthBackend) {
if err := backend.DeleteUser("username"); err != nil {
t.Fatalf("DeleteUser error: %v", err)
}
err := backend.DeleteUser("username")
if err == nil {
t.Fatalf("DeleteUser should have raised error")
} else if err != httpauth.ErrDeleteNull {
t.Fatalf("DeleteUser raised unexpected error: %v", err)
}
}
func testBackendClose(t *testing.T, backend httpauth.AuthBackend) {
backend.Close()
}
func testBackend(t *testing.T, backend httpauth.AuthBackend) {
testBackendAuthorizer(t, backend)
testBackendSaveUser(t, backend)
testBackendNewAuthBackend_existing(t, backend)
testBackendUser_existing(t, backend)
testBackendUser_notexisting(t, backend)
testBackendUsers(t, backend)
testBackendUpdateUser(t, backend)
testBackendDeleteUser(t, backend)
testBackendClose(t, backend)
}
func testAfterReopen(t *testing.T, backend httpauth.AuthBackend) {
users, err := backend.Users()
if err != nil {
t.Fatal(err.Error())
}
if len(users) != 1 {
t.Fatalf("Users not loaded properly. length = %d", len(users))
}
if users[0].Username != "username2" {
t.Error("Username not correct.")
}
if users[0].Email != "email2" {
t.Error("User email not correct.")
}
if !bytes.Equal(users[0].Hash, []byte("passwordhash2")) {
t.Error("User password not correct.")
}
}
func testDelete2(t *testing.T, backend httpauth.AuthBackend) {
if err := backend.DeleteUser("username2"); err != nil {
t.Fatalf("DeleteUser error: %v", err)
}
}
func testClose2(t *testing.T, backend httpauth.AuthBackend) {
backend.Close()
}
func testBackend2(t *testing.T, backend httpauth.AuthBackend) {
testAfterReopen(t, backend)
testDelete2(t, backend)
testClose2(t, backend)
}