Repository navigation
Expand file tree
/
Copy pathlogs_test.go
More file actions
580 lines (501 loc) · 18.5 KB
/
Copy pathlogs_test.go
File metadata and controls
580 lines (501 loc) · 18.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
package main
import (
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLogsEndpoints(t *testing.T) {
tests := []TestCase{
// GET /logs
{
query: "GET /v1/logs",
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
data := assertListResponse(t, response)
assert.Equal(t, 25, len(data))
// Default pagination size is 25, so all the logs fit into a page
// and cursors should be empty
assertPaginationLinks(t, response, nil, "?page[after]=WyIyMDEwLTAxLTE1VDIzOjU5OjU5WiIsIjEyZjMwZDllLTA0MmUtMTFlZC04ZGRjLWQ4YmJjMTQ2ZDE2NSJd")
var prevCreatedAt *time.Time = nil
for _, item := range data {
log := item
assert.NotEmpty(t, log["message"])
assertUUID(t, log["id"])
createdAt := assertRFC3339(t, log["createdAt"])
assertRFC3339(t, log["updatedAt"])
// Only certain logs from the fixtures have an associated entity.
//
// FIXME: This is ugly, see the issue about improving tests:
// https://github.com/publiccodeyml/open-catalog-api/issues/91
if log["id"] == "2dfb2bc2-042d-11ed-9338-d8bbc146d165" ||
log["id"] == "12f30d9e-042e-11ed-8ddc-d8bbc146d165" ||
log["id"] == "18a70362-042e-11ed-b793-d8bbc146d165" {
assert.Equal(t, "/software/c353756e-8597-4e46-a99b-7da2e141603b", log["entity"])
} else if log["id"] == "53650508-042e-11ed-9b84-d8bbc146d165" {
assert.Equal(t, "/publishers/2ded32eb-c45e-4167-9166-a44e18b8adde", log["entity"])
} else {
assert.Nil(t, log["entity"])
}
assertOnlyKeys(t, log, "id", "createdAt", "updatedAt", "message", "entity")
// Check the logs are ordered by descending createdAt
if prevCreatedAt != nil {
assert.GreaterOrEqual(t, *prevCreatedAt, createdAt)
}
prevCreatedAt = &createdAt
}
},
},
{
description: "GET with page[size] query param",
query: "GET /v1/logs?page[size]=3",
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
data := assertListResponse(t, response)
assert.Equal(t, 3, len(data))
assertPaginationLinks(t, response, nil, "?page[after]=WyIyMDEwLTA4LTAxVDIzOjU5OjU5WiIsIjRiNGExYjljLTA0MmUtMTFlZC04MmE4LWQ4YmJjMTQ2ZDE2NSJd&page[size]=3")
},
},
{
description: "GET with from and page[size] preserves both in links",
query: "GET /v1/logs?from=2010-03-01T09:56:23Z&page[size]=3",
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
data := assertListResponse(t, response)
assert.Equal(t, 3, len(data))
links := response["links"].(map[string]any)
next := links["next"].(string)
assert.Contains(t, next, "from=2010-03-01T09%3A56%3A23Z")
assert.Contains(t, next, "page[after]=")
assert.Contains(t, next, "page[size]=3")
},
},
{
description: "GET with invalid format for page[size] query param",
query: "GET /v1/logs?page[size]=NOT_AN_INT",
expectedCode: 422,
expectedContentType: "application/problem+json",
validateFunc: func(t *testing.T, response map[string]any) {
assert.Equal(t, `can't get Logs`, response["title"])
assert.Equal(t, "page[size] must be an integer", response["detail"])
},
},
{
description: "GET with page[size] bigger than the max of 100 caps the size",
query: "GET /v1/logs?page[size]=200",
expectedCode: 200,
expectedContentType: "application/json",
setupFunc: addLogsForPaginationCapTest,
validateFunc: func(t *testing.T, response map[string]any) {
items := assertListResponse(t, response)
assert.Equal(t, 100, len(items))
},
},
{
description: `GET with "page[after]" query param`,
query: "GET /v1/logs?page[after]=WyIyMDEwLTA3LTAxVDIzOjU5OjU5WiIsIjg1MWZlMGY0LTA0MmUtMTFlZC05MzNlLWQ4YmJjMTQ2ZDE2NSJd",
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
data := assertListResponse(t, response)
assert.Equal(t, 18, len(data))
assertPaginationLinks(t, response, "?page[before]=WyIyMDEwLTA2LTMwVDIzOjU5OjU5WiIsIjgyNTZmODgwLTA0MmUtMTFlZC04MmI5LWQ4YmJjMTQ2ZDE2NSJd", nil)
},
},
{
description: `GET with invalid "page[after]" query param`,
query: "GET /v1/logs?page[after]=NOT_A_VALID_CURSOR",
expectedCode: 422,
expectedContentType: "application/problem+json",
validateFunc: func(t *testing.T, response map[string]any) {
assert.Equal(t, `can't get Logs`, response["title"])
assert.Equal(t, "wrong cursor format in page[after] or page[before]", response["detail"])
},
},
{
description: "GET with page[before] query param",
query: "GET /v1/logs?page[before]=WyIyMDEwLTA2LTMwVDIzOjU5OjU5WiIsIjgyNTZmODgwLTA0MmUtMTFlZC04MmI5LWQ4YmJjMTQ2ZDE2NSJd",
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
data := assertListResponse(t, response)
assert.Equal(t, 8, len(data))
assertPaginationLinks(t, response, nil, "?page[after]=WyIyMDEwLTA3LTAxVDIzOjU5OjU5WiIsIjg1MWZlMGY0LTA0MmUtMTFlZC05MzNlLWQ4YmJjMTQ2ZDE2NSJd")
},
},
{
description: `GET with invalid "page[before]" query param`,
query: "GET /v1/logs?page[before]=NOT_A_VALID_CURSOR",
expectedCode: 422,
expectedContentType: "application/problem+json",
validateFunc: func(t *testing.T, response map[string]any) {
assert.Equal(t, `can't get Logs`, response["title"])
assert.Equal(t, "wrong cursor format in page[after] or page[before]", response["detail"])
},
},
{
description: `GET with "from" query param`,
query: "GET /v1/logs?from=2010-03-01T09:56:23Z",
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
data := assertListResponse(t, response)
assert.Equal(t, 20, len(data))
},
},
{
description: `GET with "from" query param carrying a zone offset`,
query: "GET /v1/logs?from=2010-03-01T11:56:23%2B02:00",
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
data := assertListResponse(t, response)
assert.Equal(t, 20, len(data))
},
},
{
description: `GET with "from" query param carrying fractional seconds`,
query: "GET /v1/logs?from=2010-03-01T09:56:23.000000Z",
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
data := assertListResponse(t, response)
assert.Equal(t, 20, len(data))
},
},
{
description: `GET with invalid "from" query param`,
query: "GET /v1/logs?from=3",
expectedCode: 422,
expectedContentType: "application/problem+json",
validateFunc: func(t *testing.T, response map[string]any) {
assert.Equal(t, `can't get Logs`, response["title"])
assert.Equal(t, "invalid date time format (RFC 3339 needed)", response["detail"])
},
},
{
description: `GET with "to" query param`,
query: "GET /v1/logs?to=2010-03-01T09:56:23Z",
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
data := response["data"].([]any)
assert.Equal(t, 6, len(data))
},
},
{
description: `GET with "to" query param carrying a zone offset`,
query: "GET /v1/logs?to=2010-03-01T11:56:23%2B02:00",
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
data := response["data"].([]any)
assert.Equal(t, 6, len(data))
},
},
{
description: `GET with invalid "to" query param`,
query: "GET /v1/logs?to=3",
expectedCode: 422,
expectedContentType: "application/problem+json",
validateFunc: func(t *testing.T, response map[string]any) {
assert.Equal(t, `can't get Logs`, response["title"])
assert.Equal(t, "invalid date time format (RFC 3339 needed)", response["detail"])
},
},
{
description: `GET with "search" query param`,
query: "GET /v1/logs?search=bad publiccode.yml",
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
data := response["data"].([]any)
assert.Equal(t, 5, len(data))
},
},
{
description: `GET with "from" and "to" query params`,
query: "GET /v1/logs?from=2010-03-01T09:56:23Z&to=2010-06-01T09:56:23Z",
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
data := response["data"].([]any)
assert.Equal(t, 9, len(data))
},
},
{
description: `GET with "to" and "search" query params`,
query: "GET /v1/logs?to=2010-03-01T09:56:23Z&search=bad publiccode.yml",
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
data := response["data"].([]any)
assert.Equal(t, 0, len(data))
},
},
{
description: "Non-existent log",
query: "GET /v1/logs/eea19c82-0449-11ed-bd84-d8bbc146d165",
expectedCode: 404,
expectedBody: `{"title":"can't get Log","detail":"Log was not found","status":404}`,
expectedContentType: "application/problem+json",
},
// POST /logs
{
description: "POST with valid body",
query: "POST /v1/logs",
body: `{"message": "New log from test suite"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
assert.Equal(t, "New log from test suite", response["message"])
assertUUID(t, response["id"])
assertTimestamps(t, response)
assert.Nil(t, response["entity"])
},
},
{
description: "POST log - wrong payload",
query: "POST /v1/logs",
body: `{"wrong": "payload"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Log","detail":"unknown field in JSON input","status":422}`,
},
{
description: "POST log - wrong token",
query: "POST /v1/logs",
body: `{"message": "new log"}`,
headers: map[string][]string{
"Authorization": {badToken},
"Content-Type": {"application/json"},
},
expectedCode: 401,
expectedBody: `{"title":"token authentication failed","status":401}`,
expectedContentType: "application/problem+json",
},
{
description: "POST log with invalid JSON",
query: "POST /v1/logs",
body: `INVALID_JSON`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 400,
expectedContentType: "application/problem+json",
validateFunc: func(t *testing.T, response map[string]any) {
assert.Equal(t, `can't create Log`, response["title"])
assert.Equal(t, "invalid or malformed JSON", response["detail"])
},
},
{
description: "POST /v1/logs with JSON with extra fields",
query: "POST /v1/logs",
body: `{"message": "new log", "EXTRA_FIELD": "extra field not in schema"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 422,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Log","detail":"unknown field in JSON input","status":422}`,
},
{
description: "POST log with validation errors",
query: "POST /v1/logs",
body: `{"message": ""}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 422,
expectedContentType: "application/problem+json",
validateFunc: func(t *testing.T, response map[string]any) {
assert.Equal(t, `can't create Log`, response["title"])
assert.Equal(t, "invalid format: message is required", response["detail"])
assert.IsType(t, []any{}, response["validationErrors"])
validationErrors := response["validationErrors"].([]any)
assert.Equal(t, 1, len(validationErrors))
firstValidationError := validationErrors[0].(map[string]any)
for key := range firstValidationError {
assert.Contains(t, []string{"field", "rule", "value"}, key)
}
},
},
{
description: "POST log with empty body",
query: "POST /v1/logs",
body: "",
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 400,
expectedContentType: "application/problem+json",
validateFunc: func(t *testing.T, response map[string]any) {
assert.Equal(t, `can't create Log`, response["title"])
assert.Equal(t, "invalid or malformed JSON", response["detail"])
},
},
// POST /catalogs/:id/logs
{
description: "POST catalog log - existing catalog by alternativeId",
query: "POST /v1/catalogs/italia/logs",
body: `{"message": "log scoped to italia catalog"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
assert.Equal(t, "log scoped to italia catalog", response["message"])
assertUUID(t, response["id"])
assertTimestamps(t, response)
},
},
{
description: "POST catalog log - root catalog (∅) succeeds with null entity",
query: "POST /v1/catalogs/%E2%88%85/logs",
body: `{"message": "log on the implicit root catalog"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 200,
expectedContentType: "application/json",
validateFunc: func(t *testing.T, response map[string]any) {
assert.Equal(t, "log on the implicit root catalog", response["message"])
assertUUID(t, response["id"])
assertTimestamps(t, response)
},
},
{
description: "POST catalog log - non-existing catalog returns 404",
query: "POST /v1/catalogs/00000000-0000-0000-0000-000000000000/logs",
body: `{"message": "should not happen"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
},
expectedCode: 404,
expectedContentType: "application/problem+json",
expectedBody: `{"title":"can't create Log","detail":"Catalog was not found","status":404}`,
},
}
runTestCases(t, tests)
}
func addLogsForPaginationCapTest(t *testing.T) {
t.Helper()
const (
insertCount = 110
message = "Pagination cap test log"
)
query := fmt.Sprintf(
"INSERT INTO logs (id, message, created_at, updated_at) VALUES (%s, %s, %s, %s)",
placeholder(1),
placeholder(2),
placeholder(3),
placeholder(4),
)
for i := range insertCount {
createdAt := time.Date(2011, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(i) * time.Second)
_, err := db.Exec(
query,
fmt.Sprintf("00000000-0000-0000-0000-%012d", i),
message,
createdAt,
createdAt,
)
require.NoError(t, err)
}
t.Cleanup(func() {
_, _ = db.Exec("DELETE FROM logs WHERE message = "+placeholder(1), message)
})
}
func TestLogsDBChecks(t *testing.T) {
t.Run("POST log persists changes to DB", func(t *testing.T) {
loadFixtures(t)
const message = "New log persisted by DB check"
req, err := newTestRequest("POST", "/v1/logs", strings.NewReader(`{"message":"`+message+`"}`))
require.NoError(t, err)
req.Header = map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
}
res, err := app.Test(req, -1)
require.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, 1, dbCount(t, "logs", "message", message))
})
t.Run("PATCH log persists changes to DB", func(t *testing.T) {
loadFixtures(t)
const (
logID = "4f95b0d0-042e-11ed-8253-d8bbc146d165"
message = "Updated log message from DB check"
)
req, err := newTestRequest("PATCH", "/v1/logs/"+logID, strings.NewReader(`{"message":"`+message+`"}`))
require.NoError(t, err)
req.Header = map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
}
res, err := app.Test(req, -1)
require.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, message, dbValue(t, "logs", "message", "id", logID))
})
t.Run("PATCH log via JSON Patch persists changes to DB", func(t *testing.T) {
loadFixtures(t)
const (
logID = "4f95b0d0-042e-11ed-8253-d8bbc146d165"
message = "Updated log message via JSON Patch"
)
body := `[{"op":"replace","path":"/message","value":"` + message + `"}]`
req, err := newTestRequest("PATCH", "/v1/logs/"+logID, strings.NewReader(body))
require.NoError(t, err)
req.Header = map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json-patch+json"},
}
res, err := app.Test(req, -1)
require.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, message, dbValue(t, "logs", "message", "id", logID))
})
t.Run("PATCH log preserves the entity association", func(t *testing.T) {
loadFixtures(t)
const (
logID = "2dfb2bc2-042d-11ed-9338-d8bbc146d165"
entityID = "c353756e-8597-4e46-a99b-7da2e141603b"
entityType = "software"
message = "Updated log message keeping its entity"
)
req, err := newTestRequest("PATCH", "/v1/logs/"+logID, strings.NewReader(`{"message":"`+message+`"}`))
require.NoError(t, err)
req.Header = map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
}
res, err := app.Test(req, -1)
require.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, message, dbValue(t, "logs", "message", "id", logID))
assert.Equal(t, entityID, dbValue(t, "logs", "entity_id", "id", logID))
assert.Equal(t, entityType, dbValue(t, "logs", "entity_type", "id", logID))
})
}