diff --git a/.gitignore b/.gitignore index 83ab4de..f85f7e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ dist/ tmp/ +.idea/ +.vscode test.db test.db-shm diff --git a/developers-italia.oas.yaml b/developers-italia.oas.yaml index bfcf830..015c5a5 100644 --- a/developers-italia.oas.yaml +++ b/developers-italia.oas.yaml @@ -643,6 +643,13 @@ paths: in: query name: to description: Only logs before this time (RFC 3339 datetime) + - schema: + type: string + maxLength: 255 + example: 'BAD publiccode' + in: query + name: search + description: Only logs matching the search string in their message post: summary: Create Logs description: Create Logs diff --git a/internal/handlers/general/query.go b/internal/handlers/general/query.go index 0b7362c..ab2d427 100644 --- a/internal/handlers/general/query.go +++ b/internal/handlers/general/query.go @@ -3,6 +3,8 @@ package general import ( "time" + "github.com/gofiber/fiber/v2/utils" + "github.com/gofiber/fiber/v2" "github.com/italia/developers-italia-api/internal/common" "gorm.io/gorm" @@ -37,5 +39,9 @@ func Clauses(ctx *fiber.Ctx, stmt *gorm.DB, searchFieldName string) (*gorm.DB, e ret = stmt.Where("created_at < ?", at) } + if search := ctx.Query("search", ""); search != "" { + ret = stmt.Where("LOWER(message) LIKE ?", "%"+utils.ToLower(search)+"%") + } + return ret, nil } diff --git a/main_test.go b/main_test.go index 5d922b9..ec03382 100644 --- a/main_test.go +++ b/main_test.go @@ -1673,7 +1673,8 @@ func TestSoftwareEndpoints(t *testing.T) { assert.Contains(t, []string{"id", "createdAt", "updatedAt", "url", "aliases", "publiccodeYml", "active", "vitality"}, key) } }, - }, { + }, + { description: "GET software with vitality field", query: "GET /v1/software/9f135268-a37e-4ead-96ec-e4a24bb9344a", expectedCode: 200, @@ -2905,7 +2906,7 @@ func TestLogsEndpoints(t *testing.T) { assert.IsType(t, []interface{}{}, response["data"]) data := response["data"].([]interface{}) - assert.Equal(t, 21, len(data)) + assert.Equal(t, 25, len(data)) // Default pagination size is 25, so all the logs fit into a page // and cursors should be empty @@ -2913,7 +2914,7 @@ func TestLogsEndpoints(t *testing.T) { links := response["links"].(map[string]interface{}) assert.Nil(t, links["prev"]) - assert.Nil(t, links["next"]) + assert.Equal(t, "?page[after]=WyIyMDEwLTAxLTE1VDIzOjU5OjU5WiIsIjEyZjMwZDllLTA0MmUtMTFlZC04ZGRjLWQ4YmJjMTQ2ZDE2NSJd", links["next"]) var prevCreatedAt *time.Time = nil for _, l := range data { @@ -2973,7 +2974,7 @@ func TestLogsEndpoints(t *testing.T) { links := response["links"].(map[string]interface{}) assert.Nil(t, links["prev"]) - assert.Equal(t, "?page[after]=WyIyMDEwLTA3LTAxVDIzOjU5OjU5WiIsIjg1MWZlMGY0LTA0MmUtMTFlZC05MzNlLWQ4YmJjMTQ2ZDE2NSJd", links["next"]) + assert.Equal(t, "?page[after]=WyIyMDEwLTA4LTAxVDIzOjU5OjU5WiIsIjRiNGExYjljLTA0MmUtMTFlZC04MmE4LWQ4YmJjMTQ2ZDE2NSJd", links["next"]) }, }, // TODO @@ -3020,7 +3021,7 @@ func TestLogsEndpoints(t *testing.T) { assert.IsType(t, []interface{}{}, response["data"]) data := response["data"].([]interface{}) - assert.Equal(t, 3, len(data)) + assert.Equal(t, 8, len(data)) links := response["links"].(map[string]interface{}) assert.Nil(t, links["prev"]) @@ -3048,7 +3049,7 @@ func TestLogsEndpoints(t *testing.T) { assert.IsType(t, []interface{}{}, response["data"]) data := response["data"].([]interface{}) - assert.Equal(t, 15, len(data)) + assert.Equal(t, 20, len(data)) }, }, { @@ -3085,6 +3086,18 @@ func TestLogsEndpoints(t *testing.T) { 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]interface{}) { + data := response["data"].([]interface{}) + + assert.Equal(t, 5, len(data)) + }, + }, { description: "Non-existent log", query: "GET /v1/logs/eea19c82-0449-11ed-bd84-d8bbc146d165", diff --git a/test/testdata/fixtures/logs.yml b/test/testdata/fixtures/logs.yml index 4572ad1..1b867bc 100644 --- a/test/testdata/fixtures/logs.yml +++ b/test/testdata/fixtures/logs.yml @@ -110,3 +110,28 @@ created_at: 2010-07-30 23:59:59 updated_at: 2010-12-31 23:59:59 message: A log message + +- id: 06666ecc-042e-11ed-83b0-d8bbc146d165 + created_at: 2010-08-01 23:59:59+00:00 + updated_at: 2010-12-31 23:59:59+00:00 + message: "[github.com/foobar/baz] BAD publiccode.yml" + +- id: 37f904ea-042e-11ed-8176-d8bbc146d165 + created_at: 2010-08-01 23:59:59+00:00 + updated_at: 2010-12-31 23:59:59+00:00 + message: "[github.com/foobar/baz2] BAD publiccode.yml" + +- id: 4b4a1b9c-042e-11ed-82a8-d8bbc146d165 + created_at: 2010-08-01 23:59:59+00:00 + updated_at: 2010-12-31 23:59:59+00:00 + message: "[github.com/foobar/baz3] BAD publiccode.yml" + +- id: 5332f73e-042e-11ed-8e74-d8bbc146d165 + created_at: 2010-08-01 23:59:59+00:00 + updated_at: 2010-12-31 23:59:59+00:00 + message: "[github.com/foobar/baz4] BAD publiccode.yml" + +- id: 5b45111e-042e-11ed-8d60-d8bbc146d165 + created_at: 2010-08-01 23:59:59+00:00 + updated_at: 2010-12-31 23:59:59+00:00 + message: "[github.com/foobar/baz5] BAD publiccode.yml"