-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle_test.go
More file actions
431 lines (375 loc) · 12.8 KB
/
Copy pathbundle_test.go
File metadata and controls
431 lines (375 loc) · 12.8 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Patchwork - automated patch tracking system
// Copyright (C) The Patchwork Contributors (see CONTRIBUTORS)
//
// SPDX-License-Identifier: GPL-2.0-or-later
package api
import (
"context"
"encoding/json"
"fmt"
"testing"
)
func TestBundleList(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
userID := s.insertUser(t, "bundler", "b@test")
s.exec(t, `INSERT INTO bundle (owner_id, project_id, name, public)
VALUES (?, ?, 'my bundle', true)`, userID, projID)
items := getList(t, s, "/api/1.4/bundles/")
if len(items) != 1 {
t.Fatalf("got %d, want 1", len(items))
}
b := items[0]
assertField(t, b, "id")
assertField(t, b, "name")
assertField(t, b, "public")
assertNested(t, b, "owner", "id")
assertNested(t, b, "project", "id")
}
func TestBundleListEmpty(t *testing.T) {
s := newTestServer(t)
items := getList(t, s, "/api/1.4/bundles/")
if len(items) != 0 {
t.Errorf("got %d, want 0", len(items))
}
}
func TestBundleListPublicOnly(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
userID := s.insertUser(t, "bauth", "bauth@test")
s.exec(t, `INSERT INTO bundle (owner_id, project_id, name, public)
VALUES (?, ?, 'public', true)`, userID, projID)
s.exec(t, `INSERT INTO bundle (owner_id, project_id, name, public)
VALUES (?, ?, 'private', false)`, userID, projID)
items := getList(t, s, "/api/1.4/bundles/")
if len(items) != 1 {
t.Errorf("got %d, want 1", len(items))
}
if items[0]["name"] != "public" {
t.Errorf("name = %v, want public", items[0]["name"])
}
}
func TestBundleListAuthSeesOwnPrivate(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
userID := s.insertUser(t, "me", "me@test")
token := s.insertToken(t, userID)
otherID := s.insertUser(t, "other", "other@test")
s.exec(t, `INSERT INTO bundle (owner_id, project_id, name, public)
VALUES (?, ?, 'my-private', false)`, userID, projID)
s.exec(t, `INSERT INTO bundle (owner_id, project_id, name, public)
VALUES (?, ?, 'other-private', false)`, otherID, projID)
s.exec(t, `INSERT INTO bundle (owner_id, project_id, name, public)
VALUES (?, ?, 'public-one', true)`, otherID, projID)
resp := s.authRequest(t, "GET", "/api/1.4/bundles/", token, nil)
if resp.StatusCode != 200 {
t.Fatalf("status = %d", resp.StatusCode)
}
var items []map[string]any
json.NewDecoder(resp.Body).Decode(&items)
resp.Body.Close()
if len(items) != 2 {
t.Fatalf("got %d, want 2 (own private + public)", len(items))
}
}
func TestBundleDetail(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
userID := s.insertUser(t, "bd", "bd@test")
var bundleID int
s.db.NewRaw(`INSERT INTO bundle (owner_id, project_id, name, public)
VALUES (?, ?, 'detail bundle', true) RETURNING id`,
userID, projID).Scan(context.Background(), &bundleID)
b := getOne(t, s, fmt.Sprintf("/api/1.4/bundles/%d", bundleID))
if b["name"] != "detail bundle" {
t.Errorf("name = %v", b["name"])
}
}
func TestBundleDetailInvalid(t *testing.T) {
s := newTestServer(t)
resp := s.get(t, "/api/1.4/bundles/invalid")
if resp.StatusCode != 422 {
t.Errorf("status = %d, want 422", resp.StatusCode)
}
}
func TestBundleDetailPatches(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
userID := s.insertUser(t, "bp", "bp@test")
patchID := s.insertPatch(t, projID, "<bp@test>", "bundle patch")
var bundleID int
s.db.NewRaw(`INSERT INTO bundle (owner_id, project_id, name, public)
VALUES (?, ?, 'with patches', true) RETURNING id`,
userID, projID).Scan(context.Background(), &bundleID)
s.exec(t, `INSERT INTO bundle_patch (bundle_id, patch_id, "order")
VALUES (?, ?, 0)`, bundleID, patchID)
b := getOne(t, s, fmt.Sprintf("/api/1.4/bundles/%d", bundleID))
assertValue(t, b, "name", "with patches")
assertValue(t, b, "public", true)
assertContains(t, b, "url", fmt.Sprintf("/bundles/%d", bundleID))
assertContains(t, b, "mbox", fmt.Sprintf("/bundles/%d/mbox", bundleID))
assertNested(t, b, "owner", "id")
assertNested(t, b, "project", "id")
patches := b["patches"].([]any)
if len(patches) != 1 {
t.Fatalf("patches: got %d, want 1", len(patches))
}
patchObj := patches[0].(map[string]any)
if int(patchObj["id"].(float64)) != int(patchID) {
t.Errorf("patches[0].id = %v, want %d", patchObj["id"], patchID)
}
if patchObj["name"] != "bundle patch" {
t.Errorf("patches[0].name = %v, want 'bundle patch'", patchObj["name"])
}
}
func TestBundleNotFound(t *testing.T) {
s := newTestServer(t)
resp := s.get(t, "/api/1.4/bundles/99999")
if resp.StatusCode != 404 {
t.Errorf("status = %d, want 404", resp.StatusCode)
}
}
func TestBundleFilterOwner(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
userID := s.insertUser(t, "bfo", "bfo@test")
s.exec(t, `INSERT INTO bundle (owner_id, project_id, name, public)
VALUES (?, ?, 'b', true)`, userID, projID)
items := getList(t, s, fmt.Sprintf("/api/1.4/bundles/?owner=%d", userID))
if len(items) != 1 {
t.Errorf("got %d, want 1", len(items))
}
items = getList(t, s, "/api/1.4/bundles/?owner=99999")
if len(items) != 0 {
t.Errorf("got %d, want 0", len(items))
}
}
func TestBundleFilterProject(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
userID := s.insertUser(t, "bf", "bf@test")
s.exec(t, `INSERT INTO bundle (owner_id, project_id, name, public)
VALUES (?, ?, 'b', true)`, userID, projID)
items := getList(t, s, fmt.Sprintf("/api/1.4/bundles/?project=%d", projID))
if len(items) != 1 {
t.Errorf("got %d, want 1", len(items))
}
items = getList(t, s, "/api/1.4/bundles/?project=99999")
if len(items) != 0 {
t.Errorf("got %d, want 0", len(items))
}
}
// --- Create ---
func TestBundleCreateAnonymous(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
patchID := s.insertPatch(t, projID, "<ca@test>", "p")
resp := s.authRequest(t, "POST", "/api/1.4/bundles/", "",
map[string]any{"name": "b", "patches": []int{patchID}})
if resp.StatusCode != 401 {
t.Errorf("status = %d, want 401", resp.StatusCode)
}
}
func TestBundleCreateValid(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
userID := s.insertUser(t, "creator", "creator@test")
token := s.insertToken(t, userID)
p1 := s.insertPatch(t, projID, "<cv1@test>", "patch 1")
p2 := s.insertPatch(t, projID, "<cv2@test>", "patch 2")
resp := s.authRequest(t, "POST", "/api/1.4/bundles/", token,
map[string]any{
"name": "my-bundle",
"public": true,
"patches": []int{p1, p2},
})
if resp.StatusCode != 201 {
t.Fatalf("status = %d, want 201", resp.StatusCode)
}
var b map[string]any
json.NewDecoder(resp.Body).Decode(&b)
resp.Body.Close()
assertValue(t, b, "name", "my-bundle")
assertValue(t, b, "public", true)
assertNested(t, b, "owner", "id")
assertNested(t, b, "project", "id")
patches := b["patches"].([]any)
if len(patches) != 2 {
t.Fatalf("patches: got %d, want 2", len(patches))
}
}
func TestBundleCreateEmptyPatches(t *testing.T) {
s := newTestServer(t)
s.insertProject(t)
userID := s.insertUser(t, "empty", "empty@test")
token := s.insertToken(t, userID)
resp := s.authRequest(t, "POST", "/api/1.4/bundles/", token,
map[string]any{
"name": "empty-bundle",
"patches": []int{},
})
if resp.StatusCode != 400 {
t.Errorf("status = %d, want 400", resp.StatusCode)
}
}
func TestBundleCreateCrossProject(t *testing.T) {
s := newTestServer(t)
proj1 := s.insertProject(t)
userID := s.insertUser(t, "cross", "cross@test")
token := s.insertToken(t, userID)
p1 := s.insertPatch(t, proj1, "<cp1@test>", "patch 1")
var proj2 int
s.db.NewRaw(`INSERT INTO project
(linkname, name, listid, listemail, subject_match,
web_url, scm_url, webscm_url,
list_archive_url, list_archive_url_format, commit_url_format,
send_notifications, use_tags, show_dependencies, auto_supersede)
VALUES ('proj2', 'Project 2', 'proj2.lists.test', 'proj2@lists.test', '',
'', '', '', '', '', '', false, false, false, false)
RETURNING id`).Scan(context.Background(), &proj2)
p2 := s.insertPatch(t, proj2, "<cp2@test>", "patch 2")
resp := s.authRequest(t, "POST", "/api/1.4/bundles/", token,
map[string]any{
"name": "cross-bundle",
"patches": []int{p1, p2},
})
if resp.StatusCode != 400 {
t.Errorf("status = %d, want 400", resp.StatusCode)
}
}
// --- Update ---
func TestBundleUpdateOwner(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
userID := s.insertUser(t, "updater", "updater@test")
token := s.insertToken(t, userID)
patchID := s.insertPatch(t, projID, "<u1@test>", "p1")
// create the bundle first
resp := s.authRequest(t, "POST", "/api/1.4/bundles/", token,
map[string]any{
"name": "orig-name",
"public": false,
"patches": []int{patchID},
})
if resp.StatusCode != 201 {
t.Fatalf("create status = %d", resp.StatusCode)
}
var created map[string]any
json.NewDecoder(resp.Body).Decode(&created)
resp.Body.Close()
bundleID := int(created["id"].(float64))
// update name and public
resp = s.authRequest(t, "PATCH", fmt.Sprintf("/api/1.4/bundles/%d", bundleID), token,
map[string]any{
"name": "new-name",
"public": true,
})
if resp.StatusCode != 200 {
t.Fatalf("update status = %d, want 200", resp.StatusCode)
}
var updated map[string]any
json.NewDecoder(resp.Body).Decode(&updated)
resp.Body.Close()
assertValue(t, updated, "name", "new-name")
assertValue(t, updated, "public", true)
}
func TestBundleUpdatePatches(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
userID := s.insertUser(t, "upatcher", "upatcher@test")
token := s.insertToken(t, userID)
p1 := s.insertPatch(t, projID, "<up1@test>", "p1")
p2 := s.insertPatch(t, projID, "<up2@test>", "p2")
p3 := s.insertPatch(t, projID, "<up3@test>", "p3")
resp := s.authRequest(t, "POST", "/api/1.4/bundles/", token,
map[string]any{
"name": "patch-update",
"patches": []int{p1},
})
var created map[string]any
json.NewDecoder(resp.Body).Decode(&created)
resp.Body.Close()
bundleID := int(created["id"].(float64))
// replace patches
resp = s.authRequest(t, "PATCH", fmt.Sprintf("/api/1.4/bundles/%d", bundleID), token,
map[string]any{
"patches": []int{p2, p3},
})
if resp.StatusCode != 200 {
t.Fatalf("status = %d, want 200", resp.StatusCode)
}
var updated map[string]any
json.NewDecoder(resp.Body).Decode(&updated)
resp.Body.Close()
patches := updated["patches"].([]any)
if len(patches) != 2 {
t.Fatalf("patches: got %d, want 2", len(patches))
}
}
func TestBundleUpdateNonOwner(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
ownerID := s.insertUser(t, "bowner", "bowner@test")
otherID := s.insertUser(t, "bother", "bother@test")
otherToken := s.insertToken(t, otherID)
patchID := s.insertPatch(t, projID, "<no1@test>", "p1")
var bundleID int
s.db.NewRaw(`INSERT INTO bundle (owner_id, project_id, name, public)
VALUES (?, ?, 'owned', true) RETURNING id`,
ownerID, projID).Scan(context.Background(), &bundleID)
s.exec(t, `INSERT INTO bundle_patch (bundle_id, patch_id, "order")
VALUES (?, ?, 0)`, bundleID, patchID)
resp := s.authRequest(t, "PATCH", fmt.Sprintf("/api/1.4/bundles/%d", bundleID), otherToken,
map[string]any{"name": "hijacked"})
if resp.StatusCode != 403 {
t.Errorf("status = %d, want 403", resp.StatusCode)
}
}
// --- Delete ---
func TestBundleDeleteOwner(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
userID := s.insertUser(t, "deleter", "deleter@test")
token := s.insertToken(t, userID)
patchID := s.insertPatch(t, projID, "<d1@test>", "p1")
resp := s.authRequest(t, "POST", "/api/1.4/bundles/", token,
map[string]any{
"name": "to-delete",
"patches": []int{patchID},
})
var created map[string]any
json.NewDecoder(resp.Body).Decode(&created)
resp.Body.Close()
bundleID := int(created["id"].(float64))
resp = s.authRequest(t, "DELETE", fmt.Sprintf("/api/1.4/bundles/%d", bundleID), token, nil)
if resp.StatusCode != 204 {
t.Errorf("status = %d, want 204", resp.StatusCode)
}
// verify it's gone
resp = s.get(t, fmt.Sprintf("/api/1.4/bundles/%d", bundleID))
if resp.StatusCode != 404 {
t.Errorf("after delete: status = %d, want 404", resp.StatusCode)
}
}
func TestBundleDeleteNonOwner(t *testing.T) {
s := newTestServer(t)
projID := s.insertProject(t)
ownerID := s.insertUser(t, "downer", "downer@test")
otherID := s.insertUser(t, "dother", "dother@test")
otherToken := s.insertToken(t, otherID)
var bundleID int
s.db.NewRaw(`INSERT INTO bundle (owner_id, project_id, name, public)
VALUES (?, ?, 'nodelete', true) RETURNING id`,
ownerID, projID).Scan(context.Background(), &bundleID)
resp := s.authRequest(t, "DELETE", fmt.Sprintf("/api/1.4/bundles/%d", bundleID), otherToken, nil)
if resp.StatusCode != 403 {
t.Errorf("status = %d, want 403", resp.StatusCode)
}
}
func TestBundleDeleteAnonymous(t *testing.T) {
s := newTestServer(t)
resp := s.authRequest(t, "DELETE", "/api/1.4/bundles/1", "", nil)
if resp.StatusCode != 401 {
t.Errorf("status = %d, want 401", resp.StatusCode)
}
}