From 0563de964be941123f2b25be94f9b1c95f61a85b Mon Sep 17 00:00:00 2001 From: Fabio Bonelli Date: Mon, 7 Nov 2022 14:12:43 +0100 Subject: [PATCH] fix: implement entity field in /{software/,}logs (#177) --- developers-italia.oas.yaml | 6 ++++++ go.mod | 6 +++--- go.sum | 15 ++++++++------- internal/handlers/logs.go | 6 ++++-- internal/models/models.go | 5 +++-- main_test.go | 24 ++++++++++++++++++++---- test/testdata/fixtures/logs.yml | 2 ++ 7 files changed, 46 insertions(+), 18 deletions(-) diff --git a/developers-italia.oas.yaml b/developers-italia.oas.yaml index fc2c074..3c1ab46 100644 --- a/developers-italia.oas.yaml +++ b/developers-italia.oas.yaml @@ -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 diff --git a/go.mod b/go.mod index cdb6895..85c51cd 100644 --- a/go.mod +++ b/go.mod @@ -12,8 +12,8 @@ require ( github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa github.com/stretchr/testify v1.7.5 gorm.io/driver/postgres v1.3.7 - gorm.io/driver/sqlite v1.3.4 - gorm.io/gorm v1.23.6 + gorm.io/driver/sqlite v1.4.3 + gorm.io/gorm v1.24.1 ) require ( @@ -26,7 +26,7 @@ require ( github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/gofiber/adaptor/v2 v2.1.25 // indirect github.com/golang/protobuf v1.5.2 // indirect - github.com/mattn/go-sqlite3 v1.14.13 // indirect + github.com/mattn/go-sqlite3 v1.14.16 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/prometheus/client_golang v1.12.2 // indirect github.com/prometheus/client_model v0.2.0 // indirect diff --git a/go.sum b/go.sum index aa944a8..0667323 100644 --- a/go.sum +++ b/go.sum @@ -291,9 +291,9 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI= -github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= -github.com/mattn/go-sqlite3 v1.14.13 h1:1tj15ngiFfcZzii7yd82foL+ks+ouQcj8j/TPq3fk1I= -github.com/mattn/go-sqlite3 v1.14.13/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= +github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -707,14 +707,15 @@ gorm.io/driver/postgres v1.0.8/go.mod h1:4eOzrI1MUfm6ObJU/UcmbXyiHSs8jSwH95G5P5d gorm.io/driver/postgres v1.3.7 h1:FKF6sIMDHDEvvMF/XJvbnCl0nu6KSKUaPXevJ4r+VYQ= gorm.io/driver/postgres v1.3.7/go.mod h1:f02ympjIcgtHEGFMZvdgTxODZ9snAHDb4hXfigBVuNI= gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw= -gorm.io/driver/sqlite v1.3.4 h1:NnFOPVfzi4CPsJPH4wXr6rMkPb4ElHEqKMvrsx9c9Fk= -gorm.io/driver/sqlite v1.3.4/go.mod h1:B+8GyC9K7VgzJAcrcXMRPdnMcck+8FgJynEehEPM16U= +gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU= +gorm.io/driver/sqlite v1.4.3/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI= gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw= gorm.io/gorm v1.20.12/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw= gorm.io/gorm v1.21.7/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= -gorm.io/gorm v1.23.6 h1:KFLdNgri4ExFFGTRGGFWON2P1ZN28+9SJRN8voOoYe0= -gorm.io/gorm v1.23.6/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= +gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= +gorm.io/gorm v1.24.1 h1:CgvzRniUdG67hBAzsxDGOAuq4Te1osVMYsa1eQbd4fs= +gorm.io/gorm v1.24.1/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/handlers/logs.go b/internal/handlers/logs.go index 357146b..6d4871b 100644 --- a/internal/handlers/logs.go +++ b/internal/handlers/logs.go @@ -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 { diff --git a/internal/models/models.go b/internal/models/models.go index 4d50e4d..b0c32c8 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -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:text GENERATED ALWAYS AS (CASE WHEN entity_id IS NULL THEN NULL ELSE ('/' || entity_type || '/' || entity_id) END) STORED;default:(-);"` //nolint:lll } type Publisher struct { diff --git a/main_test.go b/main_test.go index c05ac79..87d851a 100644 --- a/main_test.go +++ b/main_test.go @@ -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) } @@ -2112,8 +2114,6 @@ func TestSoftwareEndpoints(t *testing.T) { prevCreatedAt = &createdAt } - - // TODO assert.NotEmpty(t, firstLog["entity"]) }, }, { @@ -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 }, }, @@ -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) @@ -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 }, }, diff --git a/test/testdata/fixtures/logs.yml b/test/testdata/fixtures/logs.yml index 64e9486..4572ad1 100644 --- a/test/testdata/fixtures/logs.yml +++ b/test/testdata/fixtures/logs.yml @@ -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