Skip to content

Commit

Permalink
fix: implement entity field in /{software/,}logs
Browse files Browse the repository at this point in the history
  • Loading branch information
bfabio committed Nov 3, 2022
1 parent de88cdc commit a9fdd39
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
6 changes: 6 additions & 0 deletions developers-italia.oas.yaml
Expand Up @@ -1577,6 +1577,12 @@ components:
type: string
maxLength: 255
pattern: '.*'
description: >
The resource this log is about (fe. a particular Publisher / Software).
It might be absent if the log is not about a specific resource or if
the resource doesn't exist yet, like in case of the error log caused by
failure to create it.
example: /software/7589be36-f046-45c6-9223-b7de9dbf06cd
readOnly: true
required:
- id
Expand Down
6 changes: 4 additions & 2 deletions internal/handlers/logs.go
Expand Up @@ -221,11 +221,13 @@ func (p *Log) PostSoftwareLog(ctx *fiber.Ctx) error {
return common.ErrorWithValidationErrors(fiber.StatusUnprocessableEntity, "can't create Log", "invalid format", err)
}

table := models.Software{}.TableName()

log := models.Log{
ID: utils.UUIDv4(),
Message: logReq.Message,
EntityID: software.ID,
EntityType: models.Software{}.TableName(),
EntityID: &software.ID,
EntityType: &table,
}

if err := p.db.Create(&log).Error; err != nil {
Expand Down
5 changes: 3 additions & 2 deletions internal/models/models.go
Expand Up @@ -25,8 +25,9 @@ type Log struct {
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`

// Entity this Log entry is about (fe. Publisher, Software, etc.)
EntityID string `json:"-"`
EntityType string `json:"-"`
EntityID *string `json:"-"`
EntityType *string `json:"-"`
Entity string `json:"entity,omitempty" gorm:"->;type:GENERATED ALWAYS AS (CASE WHEN entity_id IS NULL THEN NULL ELSE ('/' || entity_type || '/' || entity_id) END);default:(-);"` //nolint:lll
}

type Publisher struct {
Expand Down
24 changes: 20 additions & 4 deletions main_test.go
Expand Up @@ -2101,6 +2101,8 @@ func TestSoftwareEndpoints(t *testing.T) {
_, err = time.Parse(time.RFC3339, log["updatedAt"].(string))
assert.Nil(t, err)

assert.Equal(t, "/software/c353756e-8597-4e46-a99b-7da2e141603b", log["entity"])

for key := range log {
assert.Contains(t, []string{"id", "createdAt", "updatedAt", "message", "entity"}, key)
}
Expand All @@ -2112,8 +2114,6 @@ func TestSoftwareEndpoints(t *testing.T) {

prevCreatedAt = &createdAt
}

// TODO assert.NotEmpty(t, firstLog["entity"])
},
},
{
Expand Down Expand Up @@ -2185,6 +2185,8 @@ func TestSoftwareEndpoints(t *testing.T) {
_, err = time.Parse(time.RFC3339, response["updatedAt"].(string))
assert.Nil(t, err)

assert.Equal(t, "/software/c353756e-8597-4e46-a99b-7da2e141603b", response["entity"])

// TODO: check the record was actually created in the database
},
},
Expand Down Expand Up @@ -2533,13 +2535,25 @@ func TestLogsEndpoints(t *testing.T) {
_, err = time.Parse(time.RFC3339, log["updatedAt"].(string))
assert.Nil(t, err)

// Only certain logs from the fixtures have an associated entity.
//
// FIXME: This is ugly, see the issue about improving tests:
// https://github.com/italia/developers-italia-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"])
}

var prevCreatedAt *time.Time = nil
for key := range log {
assert.Contains(t, []string{"id", "createdAt", "updatedAt", "message", "entity"}, key)
}

// TODO assert.NotEmpty(t, firstLog["entity"])

// Check the logs are ordered by descending createdAt
if prevCreatedAt != nil {
assert.GreaterOrEqual(t, *prevCreatedAt, createdAt)
Expand Down Expand Up @@ -2708,6 +2722,8 @@ func TestLogsEndpoints(t *testing.T) {
_, err = time.Parse(time.RFC3339, response["updatedAt"].(string))
assert.Nil(t, err)

assert.Nil(t, response["entity"])

// TODO: check the record was actually created in the database
},
},
Expand Down
2 changes: 2 additions & 0 deletions test/testdata/fixtures/logs.yml
Expand Up @@ -27,6 +27,8 @@
- id: 53650508-042e-11ed-9b84-d8bbc146d165
created_at: 2010-02-15 23:59:59
updated_at: 2010-12-31 23:59:59
entity_id: 2ded32eb-c45e-4167-9166-a44e18b8adde
entity_type: publishers
message: A log message

- id: 55438aac-042e-11ed-848a-d8bbc146d165
Expand Down

0 comments on commit a9fdd39

Please sign in to comment.