Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add search parameters in GET /v1/logs #237

Merged
merged 18 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
dist/
tmp/
.idea/
.vscode

test.db
test.db-shm
Expand Down
7 changes: 7 additions & 0 deletions developers-italia.oas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions internal/handlers/general/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
25 changes: 19 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -2905,15 +2906,15 @@ 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
assert.IsType(t, map[string]interface{}{}, response["links"])

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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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))
},
},
{
Expand Down Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions test/testdata/fixtures/logs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading