forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_test.go
258 lines (221 loc) · 6.25 KB
/
admin_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
package daos_test
import (
"testing"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tests"
)
func TestAdminQuery(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
expected := "SELECT {{_admins}}.* FROM `_admins`"
sql := app.Dao().AdminQuery().Build().SQL()
if sql != expected {
t.Errorf("Expected sql %s, got %s", expected, sql)
}
}
func TestFindAdminById(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
id string
expectError bool
}{
{" ", true},
{"missing", true},
{"9q2trqumvlyr3bd", false},
}
for i, scenario := range scenarios {
admin, err := app.Dao().FindAdminById(scenario.id)
hasErr := err != nil
if hasErr != scenario.expectError {
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err)
}
if admin != nil && admin.Id != scenario.id {
t.Errorf("(%d) Expected admin with id %s, got %s", i, scenario.id, admin.Id)
}
}
}
func TestFindAdminByEmail(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
email string
expectError bool
}{
{"", true},
{"invalid", true},
{"[email protected]", true},
{"[email protected]", false},
}
for i, scenario := range scenarios {
admin, err := app.Dao().FindAdminByEmail(scenario.email)
hasErr := err != nil
if hasErr != scenario.expectError {
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err)
continue
}
if !scenario.expectError && admin.Email != scenario.email {
t.Errorf("(%d) Expected admin with email %s, got %s", i, scenario.email, admin.Email)
}
}
}
func TestFindAdminByToken(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
token string
baseKey string
expectedEmail string
expectError bool
}{
// invalid auth token
{
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhZG1pbiIsImV4cCI6MTY0MDk5MTY2MX0.qrbkI2TITtFKMP6vrATrBVKPGjEiDIBeQ0mlqPGMVeY",
app.Settings().AdminAuthToken.Secret,
"",
true,
},
// expired token
{
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhZG1pbiIsImV4cCI6MTY0MDk5MTY2MX0.I7w8iktkleQvC7_UIRpD7rNzcU4OnF7i7SFIUu6lD_4",
app.Settings().AdminAuthToken.Secret,
"",
true,
},
// wrong base token (password reset token secret instead of auth secret)
{
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhZG1pbiIsImV4cCI6MjIwODk4NTI2MX0.M1m--VOqGyv0d23eeUc0r9xE8ZzHaYVmVFw1VZW6gT8",
app.Settings().AdminPasswordResetToken.Secret,
"",
true,
},
// valid token
{
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhZG1pbiIsImV4cCI6MjIwODk4NTI2MX0.M1m--VOqGyv0d23eeUc0r9xE8ZzHaYVmVFw1VZW6gT8",
app.Settings().AdminAuthToken.Secret,
false,
},
}
for i, scenario := range scenarios {
admin, err := app.Dao().FindAdminByToken(scenario.token, scenario.baseKey)
hasErr := err != nil
if hasErr != scenario.expectError {
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err)
continue
}
if !scenario.expectError && admin.Email != scenario.expectedEmail {
t.Errorf("(%d) Expected admin model %s, got %s", i, scenario.expectedEmail, admin.Email)
}
}
}
func TestTotalAdmins(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
result1, err := app.Dao().TotalAdmins()
if err != nil {
t.Fatal(err)
}
if result1 != 3 {
t.Fatalf("Expected 3 admins, got %d", result1)
}
// delete all
app.Dao().DB().NewQuery("delete from {{_admins}}").Execute()
result2, err := app.Dao().TotalAdmins()
if err != nil {
t.Fatal(err)
}
if result2 != 0 {
t.Fatalf("Expected 0 admins, got %d", result2)
}
}
func TestIsAdminEmailUnique(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
email string
excludeId string
expected bool
}{
{"", "", false},
{"[email protected]", "", false},
{"[email protected]", "", false},
{"[email protected]", "", false},
{"[email protected]", "", true},
{"[email protected]", "sywbhecnh46rhm0", true},
}
for i, scenario := range scenarios {
result := app.Dao().IsAdminEmailUnique(scenario.email, scenario.excludeId)
if result != scenario.expected {
t.Errorf("(%d) Expected %v, got %v", i, scenario.expected, result)
}
}
}
func TestDeleteAdmin(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
// try to delete unsaved admin model
deleteErr0 := app.Dao().DeleteAdmin(&models.Admin{})
if deleteErr0 == nil {
t.Fatal("Expected error, got nil")
}
admin1, err := app.Dao().FindAdminByEmail("[email protected]")
if err != nil {
t.Fatal(err)
}
admin2, err := app.Dao().FindAdminByEmail("[email protected]")
if err != nil {
t.Fatal(err)
}
admin3, err := app.Dao().FindAdminByEmail("[email protected]")
if err != nil {
t.Fatal(err)
}
deleteErr1 := app.Dao().DeleteAdmin(admin1)
if deleteErr1 != nil {
t.Fatal(deleteErr1)
}
deleteErr2 := app.Dao().DeleteAdmin(admin2)
if deleteErr2 != nil {
t.Fatal(deleteErr2)
}
// cannot delete the only remaining admin
deleteErr3 := app.Dao().DeleteAdmin(admin3)
if deleteErr3 == nil {
t.Fatal("Expected delete error, got nil")
}
total, _ := app.Dao().TotalAdmins()
if total != 1 {
t.Fatalf("Expected only 1 admin, got %d", total)
}
}
func TestSaveAdmin(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
// create
newAdmin := &models.Admin{}
newAdmin.Email = "[email protected]"
newAdmin.SetPassword("123456")
saveErr1 := app.Dao().SaveAdmin(newAdmin)
if saveErr1 != nil {
t.Fatal(saveErr1)
}
if newAdmin.Id == "" {
t.Fatal("Expected admin id to be set")
}
// update
existingAdmin, err := app.Dao().FindAdminByEmail("[email protected]")
if err != nil {
t.Fatal(err)
}
updatedEmail := "[email protected]"
existingAdmin.Email = updatedEmail
saveErr2 := app.Dao().SaveAdmin(existingAdmin)
if saveErr2 != nil {
t.Fatal(saveErr2)
}
existingAdmin, _ = app.Dao().FindAdminById(existingAdmin.Id)
if existingAdmin.Email != updatedEmail {
t.Fatalf("Expected admin email to be %s, got %s", updatedEmail, existingAdmin.Email)
}
}