-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimmutable_fields_test.go
More file actions
718 lines (663 loc) · 27.5 KB
/
Copy pathimmutable_fields_test.go
File metadata and controls
718 lines (663 loc) · 27.5 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
package main
import (
"encoding/json"
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
unusedID = "00000000-dead-beef-0000-000000000001"
forgedCreated = "1999-01-01T00:00:00Z"
fixtureLogID = "4f95b0d0-042e-11ed-8253-d8bbc146d165"
fixtureBundleID = "e1f2a3b4-c5d6-4e7f-8a9b-0c1d2e3f4a5b"
softwarePath = "/v1/software/"
logPath = "/v1/logs/"
publisherPath = "/v1/publishers/"
catalogPath = "/v1/catalogs/"
catalogSoftwarePath = catalogPath + italiaID + "/software/"
catalogPubPath = catalogPath + italiaID + "/publishers/"
)
// patchJSON sends a RFC 6902 patch, the content type that skips the DTO
// validation and reaches the entity fields directly.
func patchJSON(t *testing.T, path, body string) (int, []byte) {
t.Helper()
return patch(t, path, "application/json-patch+json", body)
}
func patchMerge(t *testing.T, path, body string) (int, []byte) {
t.Helper()
return patch(t, path, "application/json", body)
}
func patch(t *testing.T, path, contentType, body string) (int, []byte) {
t.Helper()
req, err := newTestRequest(http.MethodPatch, path, strings.NewReader(body))
require.NoError(t, err)
req.Header.Set("Content-Type", contentType)
req.Header.Set("Authorization", goodToken)
res, err := app.Test(req, -1)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, res.Body.Close()) })
out, err := io.ReadAll(res.Body)
require.NoError(t, err)
return res.StatusCode, out
}
func problem(title, detail string) string {
return `{"title":"` + title + `","detail":"` + detail + `","status":422}`
}
// TestPatchRejectsProtectedPaths covers every endpoint that runs an incoming
// patch over the whole entity. An operation on a field the API never lets a
// client choose is refused as a whole: the second, harmless operation in each
// patch must not reach the database either.
func TestPatchRejectsProtectedPaths(t *testing.T) {
const detail = "operation not allowed on a read-only field: "
tests := []struct {
description string
prefix string
id string
ops string
title string
table string
column string
want string
}{
{
"software id", softwarePath, italiaSoftwareID,
`{"op": "replace", "path": "/id", "value": "` + unusedID + `"},
{"op": "replace", "path": "/publiccodeYml", "value": "patched"}`,
"can't update Software", "software", "publiccode_yml", "-",
},
{
"software createdAt", softwarePath, italiaSoftwareID,
`{"op": "replace", "path": "/createdAt", "value": "` + forgedCreated + `"},
{"op": "replace", "path": "/publiccodeYml", "value": "patched"}`,
"can't update Software", "software", "publiccode_yml", "-",
},
{
"software updatedAt", softwarePath, italiaSoftwareID,
`{"op": "replace", "path": "/updatedAt", "value": "` + forgedCreated + `"},
{"op": "replace", "path": "/publiccodeYml", "value": "patched"}`,
"can't update Software", "software", "publiccode_yml", "-",
},
{
"software catalogId", softwarePath, italiaSoftwareID,
`{"op": "replace", "path": "/catalogId", "value": "` + swissID + `"},
{"op": "replace", "path": "/publiccodeYml", "value": "patched"}`,
"can't update Software", "software", "publiccode_yml", "-",
},
{
"software add id", softwarePath, italiaSoftwareID,
`{"op": "add", "path": "/id", "value": "` + unusedID + `"},
{"op": "replace", "path": "/publiccodeYml", "value": "patched"}`,
"can't update Software", "software", "publiccode_yml", "-",
},
{
"software remove createdAt", softwarePath, italiaSoftwareID,
`{"op": "remove", "path": "/createdAt"},
{"op": "replace", "path": "/publiccodeYml", "value": "patched"}`,
"can't update Software", "software", "publiccode_yml", "-",
},
{
"software move from createdAt", softwarePath, italiaSoftwareID,
`{"op": "move", "from": "/createdAt", "path": "/publiccodeYml"}`,
"can't update Software", "software", "publiccode_yml", "-",
},
{
"software copy from id", softwarePath, italiaSoftwareID,
`{"op": "copy", "from": "/id", "path": "/publiccodeYml"}`,
"can't update Software", "software", "publiccode_yml", "-",
},
{
"catalog software id", catalogSoftwarePath, italiaSoftwareID,
`{"op": "replace", "path": "/id", "value": "` + unusedID + `"},
{"op": "replace", "path": "/publiccodeYml", "value": "patched"}`,
"can't update Software", "software", "publiccode_yml", "-",
},
{
"catalog software createdAt", catalogSoftwarePath, italiaSoftwareID,
`{"op": "replace", "path": "/createdAt", "value": "` + forgedCreated + `"},
{"op": "replace", "path": "/publiccodeYml", "value": "patched"}`,
"can't update Software", "software", "publiccode_yml", "-",
},
{
"catalog software catalogId", catalogSoftwarePath, italiaSoftwareID,
`{"op": "replace", "path": "/catalogId", "value": "` + swissID + `"},
{"op": "replace", "path": "/publiccodeYml", "value": "patched"}`,
"can't update Software", "software", "publiccode_yml", "-",
},
{
"publisher id", publisherPath, italiaPublisherID,
`{"op": "replace", "path": "/id", "value": "` + unusedID + `"},
{"op": "replace", "path": "/description", "value": "patched"}`,
"can't update Publisher", "publishers", "description", "Publisher description 1",
},
{
"publisher createdAt", publisherPath, italiaPublisherID,
`{"op": "replace", "path": "/createdAt", "value": "` + forgedCreated + `"},
{"op": "replace", "path": "/description", "value": "patched"}`,
"can't update Publisher", "publishers", "description", "Publisher description 1",
},
{
"publisher updatedAt", publisherPath, italiaPublisherID,
`{"op": "replace", "path": "/updatedAt", "value": "` + forgedCreated + `"},
{"op": "replace", "path": "/description", "value": "patched"}`,
"can't update Publisher", "publishers", "description", "Publisher description 1",
},
{
"publisher catalogId", publisherPath, italiaPublisherID,
`{"op": "replace", "path": "/catalogId", "value": "` + swissID + `"},
{"op": "replace", "path": "/description", "value": "patched"}`,
"can't update Publisher", "publishers", "description", "Publisher description 1",
},
{
"publisher nested createdAt", publisherPath, italiaPublisherID,
`{"op": "replace", "path": "/codeHosting/0/createdAt", "value": "` + forgedCreated + `"},
{"op": "replace", "path": "/description", "value": "patched"}`,
"can't update Publisher", "publishers", "description", "Publisher description 1",
},
{
"publisher nested updatedAt", publisherPath, italiaPublisherID,
`{"op": "replace", "path": "/codeHosting/0/updatedAt", "value": "` + forgedCreated + `"},
{"op": "replace", "path": "/description", "value": "patched"}`,
"can't update Publisher", "publishers", "description", "Publisher description 1",
},
{
"catalog publisher id", catalogPubPath, italiaPublisherID,
`{"op": "replace", "path": "/id", "value": "` + unusedID + `"},
{"op": "replace", "path": "/description", "value": "patched"}`,
"can't update Publisher", "publishers", "description", "Publisher description 1",
},
{
"catalog publisher createdAt", catalogPubPath, italiaPublisherID,
`{"op": "replace", "path": "/createdAt", "value": "` + forgedCreated + `"},
{"op": "replace", "path": "/description", "value": "patched"}`,
"can't update Publisher", "publishers", "description", "Publisher description 1",
},
{
"catalog publisher catalogId", catalogPubPath, italiaPublisherID,
`{"op": "replace", "path": "/catalogId", "value": "` + swissID + `"},
{"op": "replace", "path": "/description", "value": "patched"}`,
"can't update Publisher", "publishers", "description", "Publisher description 1",
},
{
"catalog id", catalogPath, italiaID,
`{"op": "replace", "path": "/id", "value": "` + unusedID + `"},
{"op": "replace", "path": "/name", "value": "patched"}`,
"can't update Catalog", "catalogs", "name", "Italian Catalog",
},
{
"catalog createdAt", catalogPath, italiaID,
`{"op": "replace", "path": "/createdAt", "value": "` + forgedCreated + `"},
{"op": "replace", "path": "/name", "value": "patched"}`,
"can't update Catalog", "catalogs", "name", "Italian Catalog",
},
{
"catalog updatedAt", catalogPath, italiaID,
`{"op": "replace", "path": "/updatedAt", "value": "` + forgedCreated + `"},
{"op": "replace", "path": "/name", "value": "patched"}`,
"can't update Catalog", "catalogs", "name", "Italian Catalog",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
loadFixtures(t)
code, body := patchJSON(t, test.prefix+test.id, "["+test.ops+"]")
assert.Equal(t, http.StatusUnprocessableEntity, code)
assert.Contains(t, string(body), `"detail":"`+detail)
assert.Equal(t, test.want, dbValue(t, test.table, test.column, "id", test.id))
assert.Equal(t, 0, dbCount(t, test.table, "id", unusedID))
})
}
}
// TestJSONPatchValidatesPatchedEntity proves that RFC 6902 patches cannot
// bypass the field validation normally applied to merge-patch request DTOs.
// Each patch includes a valid change as well, which must remain uncommitted
// when another operation leaves the entity invalid.
func TestJSONPatchValidatesPatchedEntity(t *testing.T) {
tests := []struct {
description string
path string
ops string
table string
column string
id string
want string
}{
{
"software URL", softwarePath + italiaSoftwareID,
`{"op": "replace", "path": "/publiccodeYml", "value": "patched"},
{"op": "replace", "path": "/url", "value": "not-a-url"}`,
"software", "publiccode_yml", italiaSoftwareID, "-",
},
{
"catalog software alias", catalogSoftwarePath + italiaSoftwareID,
`{"op": "replace", "path": "/publiccodeYml", "value": "patched"},
{"op": "add", "path": "/aliases/-", "value": "not-a-url"}`,
"software", "publiccode_yml", italiaSoftwareID, "-",
},
{
"publisher email", publisherPath + italiaPublisherID,
`{"op": "replace", "path": "/description", "value": "patched"},
{"op": "replace", "path": "/email", "value": "not-an-email"}`,
"publishers", "description", italiaPublisherID, "Publisher description 1",
},
{
"catalog publisher code hosting URL", catalogPubPath + italiaPublisherID,
`{"op": "replace", "path": "/description", "value": "patched"},
{"op": "replace", "path": "/codeHosting/0/url", "value": "not-a-url"}`,
"publishers", "description", italiaPublisherID, "Publisher description 1",
},
{
"catalog source URL", catalogPath + italiaID,
`{"op": "replace", "path": "/name", "value": "patched"},
{"op": "replace", "path": "/sources/0/url", "value": "not-a-url"}`,
"catalogs", "name", italiaID, "Italian Catalog",
},
{
"bundle name", "/v1/bundles/" + fixtureBundleID,
`{"op": "replace", "path": "/description", "value": "patched"},
{"op": "replace", "path": "/name", "value": ""}`,
"bundles", "name", fixtureBundleID, "Bundle for municipalities",
},
{
"log message", logPath + fixtureLogID,
`{"op": "replace", "path": "/message", "value": "x"}`,
"logs", "message", fixtureLogID, "A log message",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
loadFixtures(t)
code, body := patchJSON(t, test.path, "["+test.ops+"]")
assert.Equal(t, http.StatusUnprocessableEntity, code, "body: %s", body)
assert.Contains(t, string(body), `"validationErrors"`)
assert.Equal(t, test.want, dbValue(t, test.table, test.column, "id", test.id))
})
}
}
// TestMergePatchValidatesPatchedEntity covers the null case of a merge
// patch. The request DTO decodes a null into a nil pointer, the same as an
// absent field, so it cannot refuse it, while the merge clears the field on
// the entity. Each body carries a valid change as well, which must stay
// uncommitted.
func TestMergePatchValidatesPatchedEntity(t *testing.T) {
tests := []struct {
description string
path string
body string
table string
column string
id string
want string
}{
{
"software url", softwarePath + italiaSoftwareID,
`{"publiccodeYml": "patched", "url": null}`,
"software", "publiccode_yml", italiaSoftwareID, "-",
},
{
"catalog software url", catalogSoftwarePath + italiaSoftwareID,
`{"publiccodeYml": "patched", "url": null}`,
"software", "publiccode_yml", italiaSoftwareID, "-",
},
{
"publisher code hosting", publisherPath + italiaPublisherID,
`{"description": "patched", "codeHosting": null}`,
"publishers", "description", italiaPublisherID, "Publisher description 1",
},
{
"catalog publisher code hosting", catalogPubPath + italiaPublisherID,
`{"description": "patched", "codeHosting": null}`,
"publishers", "description", italiaPublisherID, "Publisher description 1",
},
{
"catalog name", catalogPath + italiaID,
`{"active": false, "name": null}`,
"catalogs", "name", italiaID, "Italian Catalog",
},
{
"log message", logPath + fixtureLogID,
`{"message": null}`,
"logs", "message", fixtureLogID, "A log message",
},
{
"bundle name", "/v1/bundles/" + fixtureBundleID,
`{"description": "patched", "name": null}`,
"bundles", "name", fixtureBundleID, "Bundle for municipalities",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
loadFixtures(t)
code, body := patchMerge(t, test.path, test.body)
assert.Equal(t, http.StatusUnprocessableEntity, code, "body: %s", body)
assert.Contains(t, string(body), `"validationErrors"`)
assert.Equal(t, test.want, dbValue(t, test.table, test.column, "id", test.id))
})
}
}
// TestMergePatchRefusesNullActive covers a merge patch nulling active. The
// column is NOT NULL with a database default, so the write would fail deep
// in the database instead of telling the client what is wrong. Each body
// carries a valid change as well, which must stay uncommitted.
func TestMergePatchRefusesNullActive(t *testing.T) {
tests := []struct {
description string
path string
body string
table string
column string
id string
want string
}{
{
"publisher", publisherPath + italiaPublisherID,
`{"description": "patched", "active": null}`,
"publishers", "description", italiaPublisherID, "Publisher description 1",
},
{
"software", softwarePath + italiaSoftwareID,
`{"publiccodeYml": "patched", "active": null}`,
"software", "publiccode_yml", italiaSoftwareID, "-",
},
{
"catalog", catalogPath + italiaID,
`{"name": "patched", "active": null}`,
"catalogs", "name", italiaID, "Italian Catalog",
},
{
"bundle", "/v1/bundles/" + fixtureBundleID,
`{"description": "patched", "active": null}`,
"bundles", "description", fixtureBundleID, "Recommended software for municipalities",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
loadFixtures(t)
code, body := patchMerge(t, test.path, test.body)
assert.Equal(t, http.StatusUnprocessableEntity, code, "body: %s", body)
assert.Contains(t, string(body), "active cannot be null")
assert.Equal(t, test.want, dbValue(t, test.table, test.column, "id", test.id))
})
}
}
// TestPatchTestOpOnProtectedPathIsAllowed pins that a read-only field can
// still be named in a test operation, which reads it and changes nothing.
func TestPatchTestOpOnProtectedPathIsAllowed(t *testing.T) {
tests := []struct {
description string
prefix string
id string
ops string
table string
column string
want string
}{
{
"software", softwarePath, italiaSoftwareID,
`{"op": "test", "path": "/id", "value": "` + italiaSoftwareID + `"},
{"op": "replace", "path": "/publiccodeYml", "value": "patched"}`,
"software", "publiccode_yml", "patched",
},
{
"publisher", publisherPath, italiaPublisherID,
`{"op": "test", "path": "/catalogId", "value": "` + italiaID + `"},
{"op": "replace", "path": "/description", "value": "patched"}`,
"publishers", "description", "patched",
},
{
"catalog", catalogPath, italiaID,
`{"op": "test", "path": "/id", "value": "` + italiaID + `"},
{"op": "replace", "path": "/name", "value": "patched"}`,
"catalogs", "name", "patched",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
loadFixtures(t)
code, body := patchJSON(t, test.prefix+test.id, "["+test.ops+"]")
assert.Equal(t, http.StatusOK, code, "body: %s", body)
var response map[string]interface{}
require.NoError(t, json.Unmarshal(body, &response))
assert.Equal(t, test.id, response["id"])
assert.Equal(t, test.want, dbValue(t, test.table, test.column, "id", test.id))
})
}
}
// TestPatchWithAFailingTestOpWritesNothing pins the atomicity of the patch
// past the protected path check: a test that fails refuses the whole patch.
func TestPatchWithAFailingTestOpWritesNothing(t *testing.T) {
loadFixtures(t)
ops := `[
{"op": "replace", "path": "/publiccodeYml", "value": "patched"},
{"op": "test", "path": "/id", "value": "` + unusedID + `"}
]`
code, _ := patchJSON(t, softwarePath+italiaSoftwareID, ops)
assert.Equal(t, http.StatusUnprocessableEntity, code)
assert.Equal(t, "-", dbValue(t, "software", "publiccode_yml", "id", italiaSoftwareID))
}
// TestPatchRejectsImmutableFieldsInMergePatch covers the other content type,
// where the request is decoded into a patch struct that has no such field.
func TestPatchRejectsImmutableFieldsInMergePatch(t *testing.T) {
tests := []TestCase{
{
description: "PATCH software with an id",
query: "PATCH /v1/software/" + italiaSoftwareID,
body: `{"id": "` + unusedID + `"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't update Software","detail":"unknown field in JSON input","status":422}`,
},
{
description: "PATCH software with a createdAt",
query: "PATCH /v1/software/" + italiaSoftwareID,
body: `{"createdAt": "` + forgedCreated + `"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't update Software","detail":"unknown field in JSON input","status":422}`,
},
{
description: "PATCH publisher with an id",
query: "PATCH /v1/publishers/" + italiaPublisherID,
body: `{"id": "` + unusedID + `"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't update Publisher","detail":"unknown field in JSON input","status":422}`,
},
{
description: "PATCH catalog with an id",
query: "PATCH /v1/catalogs/" + italiaID,
body: `{"id": "` + unusedID + `"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't update Catalog","detail":"unknown field in JSON input","status":422}`,
},
{
description: "PATCH log with an id",
query: "PATCH /v1/logs/2dfb2bc2-042d-11ed-9338-d8bbc146d165",
body: `{"id": "` + unusedID + `", "message": "patched"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't update Log","detail":"unknown field in JSON input","status":422}`,
},
}
runTestCases(t, tests)
}
// TestPostRejectsServerOwnedFields covers the create side. The request is
// decoded into a struct that carries no id, no timestamps and no catalog
// assignment, so naming one has to be refused rather than quietly dropped.
func TestPostRejectsServerOwnedFields(t *testing.T) {
tests := []TestCase{
{
description: "POST publisher with an id",
query: "POST /v1/publishers",
body: `{"id": "` + unusedID + `", "description": "new", "codeHosting": [{"url": "https://post-1.example.org"}], "email": "post-1@example.org"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Publisher","detail":"unknown field in JSON input","status":422}`,
},
{
description: "POST publisher with a createdAt",
query: "POST /v1/publishers",
body: `{"createdAt": "` + forgedCreated + `", "description": "new", "codeHosting": [{"url": "https://post-2.example.org"}], "email": "post-2@example.org"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Publisher","detail":"unknown field in JSON input","status":422}`,
},
{
description: "POST publisher with a catalogId",
query: "POST /v1/publishers",
body: `{"catalogId": "` + swissID + `", "description": "new", "codeHosting": [{"url": "https://post-3.example.org"}], "email": "post-3@example.org"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Publisher","detail":"unknown field in JSON input","status":422}`,
},
{
description: "POST software with an id",
query: "POST /v1/software",
body: `{"id": "` + unusedID + `", "publiccodeYml": "-", "url": "https://post-4.example.org"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Software","detail":"unknown field in JSON input","status":422}`,
},
{
description: "POST software with a createdAt",
query: "POST /v1/software",
body: `{"createdAt": "` + forgedCreated + `", "publiccodeYml": "-", "url": "https://post-5.example.org"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Software","detail":"unknown field in JSON input","status":422}`,
},
{
description: "POST software with a catalogId",
query: "POST /v1/software",
body: `{"catalogId": "` + swissID + `", "publiccodeYml": "-", "url": "https://post-6.example.org"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Software","detail":"unknown field in JSON input","status":422}`,
},
{
description: "POST catalog with an id",
query: "POST /v1/catalogs",
body: `{"id": "` + unusedID + `", "name": "New Catalog", "sources": [{"url": "https://github.com/example/post-7"}]}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Catalog","detail":"unknown field in JSON input","status":422}`,
},
{
description: "POST catalog with a createdAt",
query: "POST /v1/catalogs",
body: `{"createdAt": "` + forgedCreated + `", "name": "New Catalog", "sources": [{"url": "https://github.com/example/post-8"}]}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Catalog","detail":"unknown field in JSON input","status":422}`,
},
{
description: "POST catalog publisher with a catalogId",
query: "POST /v1/catalogs/" + italiaID + "/publishers",
body: `{"catalogId": "` + swissID + `", "description": "new", "codeHosting": [{"url": "https://post-9.example.org"}], "email": "post-9@example.org"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Publisher","detail":"unknown field in JSON input","status":422}`,
},
{
description: "POST catalog software with a catalogId",
query: "POST /v1/catalogs/" + italiaID + "/software",
body: `{"catalogId": "` + swissID + `", "publiccodeYml": "-", "url": "https://post-10.example.org"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Software","detail":"unknown field in JSON input","status":422}`,
},
{
description: "POST log with an id",
query: "POST /v1/logs",
body: `{"id": "` + unusedID + `", "message": "new"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Log","detail":"unknown field in JSON input","status":422}`,
},
{
description: "POST webhook with an id",
query: "POST /v1/software/" + italiaSoftwareID + "/webhooks",
body: `{"id": "` + unusedID + `", "url": "https://post-11.example.org/hook"}`,
headers: patchHeaders(),
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Webhook","detail":"unknown field in JSON input","status":422}`,
},
}
runTestCases(t, tests)
}
// TestPostAssignsItsOwnIdentifiers pins that a create answers with a server
// generated id and with timestamps of its own, so nothing a client sends can
// end up in those columns.
func TestPostAssignsItsOwnIdentifiers(t *testing.T) {
tests := []TestCase{
{
description: "POST publisher",
query: "POST /v1/publishers",
body: `{"description": "new", "codeHosting": [{"url": "https://post-12.example.org"}], "email": "post-12@example.org"}`,
headers: patchHeaders(),
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]interface{}) {
t.Helper()
assertUUID(t, response["id"])
assert.NotEqual(t, unusedID, response["id"])
assertTimestamps(t, response)
},
},
{
description: "POST software",
query: "POST /v1/software",
body: `{"publiccodeYml": "-", "url": "https://post-13.example.org"}`,
headers: patchHeaders(),
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]interface{}) {
t.Helper()
assertUUID(t, response["id"])
assert.NotEqual(t, unusedID, response["id"])
assertTimestamps(t, response)
},
},
{
description: "POST catalog",
query: "POST /v1/catalogs",
body: `{"name": "New Catalog", "sources": [{"url": "https://github.com/example/post-14"}]}`,
headers: patchHeaders(),
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]interface{}) {
t.Helper()
assertUUID(t, response["id"])
assert.NotEqual(t, unusedID, response["id"])
assertTimestamps(t, response)
},
},
}
runTestCases(t, tests)
}
func patchHeaders() map[string][]string {
return map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
}
}