forked from presentator/presentator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_users_test.go
372 lines (343 loc) · 10.5 KB
/
api_users_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
package presentator
import (
"net/http"
"strings"
"testing"
"github.com/labstack/echo/v5"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tests"
"github.com/pocketbase/pocketbase/tokens"
)
func TestUsersList(t *testing.T) {
t.Parallel()
scenarios := []tests.ApiScenario{
{
Name: "list users as guest",
Method: http.MethodGet,
Url: "/api/collections/users/records",
TestAppFactory: setupTestApp,
ExpectedStatus: 200,
ExpectedContent: []string{`"items":[]`},
ExpectedEvents: map[string]int{"OnRecordsListRequest": 1},
},
{
Name: "list users via project link token",
Method: http.MethodGet,
Url: "/api/collections/users/records",
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "links", "test1"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 200,
ExpectedEvents: map[string]int{"OnRecordsListRequest": 1},
ExpectedContent: []string{
`"totalItems":2`,
`"username":"test2"`,
`"username":"test1"`,
`"[email protected]"`, // public email
},
NotExpectedContent: []string{
`"[email protected]"`, // private email
},
},
{
Name: "list users via user token",
Method: http.MethodGet,
Url: "/api/collections/users/records",
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "users", "test2"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 200,
ExpectedEvents: map[string]int{"OnRecordsListRequest": 1},
ExpectedContent: []string{
`"totalItems":3`,
`"username":"test4"`,
`"username":"test2"`,
`"username":"test1"`,
// public emails
`"[email protected]"`,
`"[email protected]"`,
},
NotExpectedContent: []string{
// unverified
`"username":"test3"`,
// private emails
`"[email protected]"`,
},
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}
func TestUsersView(t *testing.T) {
t.Parallel()
scenarios := []tests.ApiScenario{
{
Name: "view user as guest",
Method: http.MethodGet,
Url: "/api/collections/users/records/nwl39aj35c02p7l",
TestAppFactory: setupTestApp,
ExpectedStatus: 404,
ExpectedContent: []string{`"data":{}`},
},
{
Name: "view user via project link token (no project owner)",
Method: http.MethodGet,
Url: "/api/collections/users/records/0dwe3z1d444g9mo",
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "links", "test1"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 404,
ExpectedContent: []string{`"data":{}`},
},
{
Name: "view user via project link token (project owner)",
Method: http.MethodGet,
Url: "/api/collections/users/records/nwl39aj35c02p7l",
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "links", "test1"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 200,
ExpectedEvents: map[string]int{"OnRecordViewRequest": 1},
ExpectedContent: []string{
`"username":"test1"`,
},
},
{
Name: "view user via different user token",
Method: http.MethodGet,
Url: "/api/collections/users/records/nwl39aj35c02p7l",
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "users", "test2"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 200,
ExpectedEvents: map[string]int{"OnRecordViewRequest": 1},
ExpectedContent: []string{
`"username":"test1"`,
},
},
{
Name: "view user via owner user token",
Method: http.MethodGet,
Url: "/api/collections/users/records/nwl39aj35c02p7l",
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "users", "test1"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 200,
ExpectedEvents: map[string]int{"OnRecordViewRequest": 1},
ExpectedContent: []string{
`"username":"test1"`,
},
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}
func TestUsersCreate(t *testing.T) {
t.Parallel()
scenarios := []tests.ApiScenario{
{
Name: "everyone should be able to create a user",
Method: http.MethodPost,
Url: "/api/collections/users/records",
Body: strings.NewReader(`{
"name": "test_new",
"email": "[email protected]",
"password": "1234567890",
"passwordConfirm": "1234567890"
}`),
TestAppFactory: setupTestApp,
ExpectedStatus: 200,
ExpectedEvents: map[string]int{
"OnModelAfterCreate": 1,
"OnModelBeforeCreate": 1,
"OnRecordAfterCreateRequest": 1,
"OnRecordBeforeCreateRequest": 1,
},
ExpectedContent: []string{
`"name":"test_new"`,
`"verified":false`,
},
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}
func TestUsersUpdate(t *testing.T) {
t.Parallel()
scenarios := []tests.ApiScenario{
{
Name: "guest, try to update user",
Method: http.MethodPatch,
Url: "/api/collections/users/records/nwl39aj35c02p7l",
Body: strings.NewReader(`{"name":"test_update"}`),
TestAppFactory: setupTestApp,
ExpectedStatus: 404,
ExpectedContent: []string{`"data":{}`},
},
{
Name: "auth as project link, try to update project owner",
Method: http.MethodPatch,
Url: "/api/collections/users/records/nwl39aj35c02p7l",
Body: strings.NewReader(`{"name":"test_update"}`),
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "links", "test1"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 404,
ExpectedContent: []string{`"data":{}`},
},
{
Name: "auth as user, try to update another user that shares a project ownership",
Method: http.MethodPatch,
Url: "/api/collections/users/records/7rs5wkqeb5gggmn",
Body: strings.NewReader(`{"name":"test_update"}`),
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "users", "test1"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 404,
ExpectedContent: []string{`"data":{}`},
},
{
Name: "auth as user, update own record",
Method: http.MethodPatch,
Url: "/api/collections/users/records/nwl39aj35c02p7l",
Body: strings.NewReader(`{"name":"test_update"}`),
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "users", "test1"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 200,
ExpectedEvents: map[string]int{
"OnModelAfterUpdate": 1,
"OnModelBeforeUpdate": 1,
"OnRecordAfterUpdateRequest": 1,
"OnRecordBeforeUpdateRequest": 1,
},
ExpectedContent: []string{`"name":"test_update"`},
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}
func TestUsersDelete(t *testing.T) {
t.Parallel()
scenarios := []tests.ApiScenario{
{
Name: "guest, try to delete user",
Method: http.MethodDelete,
Url: "/api/collections/users/records/nwl39aj35c02p7l",
TestAppFactory: setupTestApp,
ExpectedStatus: 404,
ExpectedContent: []string{`"data":{}`},
},
{
Name: "auth as project link, try to delete project owner",
Method: http.MethodDelete,
Url: "/api/collections/users/records/nwl39aj35c02p7l",
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "links", "test1"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 404,
ExpectedContent: []string{`"data":{}`},
},
{
Name: "auth as user, try to delete another user that shares a project ownership",
Method: http.MethodDelete,
Url: "/api/collections/users/records/7rs5wkqeb5gggmn",
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "users", "test1"),
},
TestAppFactory: setupTestApp,
ExpectedStatus: 404,
ExpectedContent: []string{`"data":{}`},
},
{
Name: "auth as user, delete own record",
Method: http.MethodDelete,
Url: "/api/collections/users/records/nwl39aj35c02p7l",
RequestHeaders: map[string]string{
"Authorization": getAuthToken(t, "users", "test1"),
},
TestAppFactory: setupTestApp,
BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
ownedProjects, err := app.Dao().FindRecordsByFilter("projects", "users.id ?= {:user}", "", 0, 0, dbx.Params{
"user": "nwl39aj35c02p7l",
})
if err != nil || len(ownedProjects) == 0 {
t.Fatalf("Failed to fetch owned projects: %v", err)
}
app.Store().Set("ownedProjects", ownedProjects)
},
AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) {
ownedProjects, ok := app.Store().Get("ownedProjects").([]*models.Record)
if !ok || len(ownedProjects) == 0 {
t.Fatalf("Failed to retrieve previously fetched owned projects")
}
// ensure that only the single owner projects are deleted
for _, p := range ownedProjects {
_, err := app.Dao().FindRecordById("projects", p.Id)
exists := err == nil
expectToBeDeleted := len(p.GetStringSlice("users")) > 1
if expectToBeDeleted != exists {
t.Fatalf("Expected deleted state %v, got %v", expectToBeDeleted, exists)
}
}
// all user related projects preferences should have been deleted
prefs, err := app.Dao().FindRecordsByFilter("projectUserPreferences", "user ?= {:user}", "", 0, 0, dbx.Params{
"user": "nwl39aj35c02p7l",
})
if err != nil || len(prefs) > 0 {
t.Fatalf("Failed to fetch prefs or not all prefs are deleted - err:%v, total:%d", err, len(prefs))
}
},
ExpectedStatus: 204,
ExpectedEvents: map[string]int{
"OnRecordAfterDeleteRequest": 1,
"OnRecordBeforeDeleteRequest": 1,
// include also the cascade rels operations
"OnModelAfterDelete": 26,
"OnModelBeforeDelete": 26,
"OnModelAfterUpdate": 2,
"OnModelBeforeUpdate": 2,
},
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}
// -------------------------------------------------------------------
func setupTestApp(t *testing.T) *tests.TestApp {
testApp, err := tests.NewTestApp("./test_pb_data")
if err != nil {
t.Fatal(err)
}
bindAppHooks(testApp)
return testApp
}
func getAuthToken(t *testing.T, collection, username string) string {
app := setupTestApp(t)
defer app.Cleanup()
record, err := app.Dao().FindAuthRecordByUsername(collection, username)
if err != nil {
t.Fatalf("Failed to find auth record with username %q: %v", username, err)
}
token, err := tokens.NewRecordAuthToken(app, record)
if err != nil {
t.Fatalf("Failed to generate token for auth record with username %q: %v", username, err)
}
return token
}