From 646640f4002a54b9372b218702d89604032803e7 Mon Sep 17 00:00:00 2001 From: Fabio Bonelli Date: Tue, 4 Oct 2022 20:07:42 +0200 Subject: [PATCH] fix: publisher's description must be unique and not NULL (#154) Also, fix tests for #149. --- internal/handlers/publishers.go | 4 +-- internal/models/models.go | 2 +- internal/models/models_test.go | 4 +-- main_test.go | 48 ++++++++++++++++++++++++--------- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/internal/handlers/publishers.go b/internal/handlers/publishers.go index 07f74ba..f5f903e 100644 --- a/internal/handlers/publishers.go +++ b/internal/handlers/publishers.go @@ -96,7 +96,7 @@ func (p *Publisher) PostPublisher(ctx *fiber.Ctx) error { publisher := &models.Publisher{ ID: utils.UUIDv4(), - Description: &request.Description, + Description: request.Description, Email: normalizedEmail, Active: request.Active, ExternalCode: &request.ExternalCode, @@ -171,7 +171,7 @@ func (p *Publisher) updatePublisherTrx( } if request.Description != "" { - publisher.Description = &request.Description + publisher.Description = request.Description } if request.Email != "" { diff --git a/internal/models/models.go b/internal/models/models.go index 52b86d6..1d9f826 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -32,7 +32,7 @@ type Log struct { type Publisher struct { ID string `json:"id" gorm:"primaryKey"` Email string `json:"email"` - Description *string `json:"description" gorm:"uniqueIndex;default:null"` + Description string `json:"description" gorm:"uniqueIndex;not null"` CodeHosting []CodeHosting `json:"codeHosting" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;unique"` Active *bool `json:"active" gorm:"default:true;not null"` ExternalCode *string `json:"externalCode,omitempty" gorm:"uniqueIndex"` diff --git a/internal/models/models_test.go b/internal/models/models_test.go index 42d295d..efa61b3 100644 --- a/internal/models/models_test.go +++ b/internal/models/models_test.go @@ -128,7 +128,7 @@ func TestPublisherCreate(t *testing.T) { err := db.Create( &Publisher{ ID: utils.UUIDv4(), - Description: &description, + Description: description, Email: "new-publisher@example.org", }, ).Error @@ -138,7 +138,7 @@ func TestPublisherCreate(t *testing.T) { err = db.Create( &Publisher{ ID: "2ded32eb-c45e-4167-9166-a44e18b8adde", - Description: &description, + Description: description, Email: "new-publisher@example.org", }, ).Error diff --git a/main_test.go b/main_test.go index e5aca4f..f10e7b3 100644 --- a/main_test.go +++ b/main_test.go @@ -420,7 +420,7 @@ func TestPublishersEndpoints(t *testing.T) { // POST /publishers { query: "POST /v1/publishers", - body: `{"codeHosting": [{"url" : "https://www.example-testcase-1.com"}], "email":"example-testcase-1@example.com", "description" : "Description example test case 1"}`, + body: `{"description": "new description", "codeHosting": [{"url" : "https://www.example-testcase-1.com"}], "email":"example-testcase-1@example.com"}`, headers: map[string][]string{ "Authorization": {goodToken}, "Content-Type": {"application/json"}, @@ -456,8 +456,8 @@ func TestPublishersEndpoints(t *testing.T) { { description: "POST publishers - with externalCode example", query: "POST /v1/publishers", + body: `{"description":"new description", "codeHosting": [{"url" : "https://www.example-testcase-2.com"}], "email":"example-testcase-2@example.com", "externalCode":"example-testcase-2"}`, fixtures: []string{"publishers.yml", "publishers_code_hosting.yml"}, - body: `{"codeHosting": [{"url" : "https://www.example-testcase-2.com"}], "email":"example-testcase-2@example.com", "externalCode":"example-testcase-2", "description" : "Test example testcase 2"}`, headers: map[string][]string{ "Authorization": {goodToken}, "Content-Type": {"application/json"}, @@ -485,8 +485,8 @@ func TestPublishersEndpoints(t *testing.T) { }, }, { - query: "POST /v1/publishers - NOT normalized URL validation passed", - body: `{"codeHosting": [{"url" : "https://WwW.example-testcase-3.com"}], "email":"example-testcase-3@example.com", "externalCode":"example-testcase-3", "description" : "Test example testcase 2"}`, + query: "POST /v1/publishers - NOT normalized URL validation passed", + body: `{"description":"new description", "codeHosting": [{"url" : "https://WwW.example-testcase-3.com"}], "email":"example-testcase-3@example.com", "externalCode":"example-testcase-3"}`, fixtures: []string{"publishers.yml", "publishers_code_hosting.yml"}, headers: map[string][]string{ "Authorization": {goodToken}, @@ -515,9 +515,10 @@ func TestPublishersEndpoints(t *testing.T) { }, }, { - query: "POST /v1/publishers - NOT normalized URL already exist", + description: "POST publishers with duplicate URL (when normalized)", + query: "POST /v1/publishers", + body: `{"codeHosting": [{"url" : "https://1-a.exAMple.org/code/repo"}], "email":"example-testcase-3@example.com", "description":"new description"}`, fixtures: []string{"publishers.yml", "publishers_code_hosting.yml"}, - body: `{"codeHosting": [{"url" : "hTtPs://1-A.example.org/code/repo/"}], "email":"example-testcase-3@example.com", "description" : "New POST description example testcase 2"}`, headers: map[string][]string{ "Authorization": {goodToken}, "Content-Type": {"application/json"}, @@ -569,9 +570,32 @@ func TestPublishersEndpoints(t *testing.T) { expectedBody: "{\"title\":\"can't create Publisher\",\"detail\":\"Publisher with provided description, email, external_code or CodeHosting URL already exists\",\"status\":409}", }, { - query: "POST /v1/publishers - ExternalCode already exist", - fixtures: []string{"publishers.yml", "publishers_code_hosting.yml"}, - body: `{"codeHosting": [{"url" : "https://example-testcase-xx3.com"}], "email":"example-testcase-3-pass@example.com", "externalCode":"external-code-27", "description": "New POST description"}`, + description: "POST new publisher with no description", + query: "POST /v1/publishers", + body: `{"codeHosting": [{"url" : "https://WwW.example-testcase-3.com"}], "email":"example-testcase-3@example.com"}`, + headers: map[string][]string{ + "Authorization": {goodToken}, + "Content-Type": {"application/json"}, + }, + expectedCode: 422, + expectedContentType: "application/problem+json", + expectedBody: "{\"title\":\"can't create Publisher\",\"detail\":\"invalid format\",\"status\":422,\"validationErrors\":[{\"field\":\"description\",\"rule\":\"required\"}]}", + }, + { + description: "POST new publisher with empty description", + query: "POST /v1/publishers", + body: `{"description":"", "codeHosting": [{"url" : "https://WwW.example-testcase-3.com"}], "email":"example-testcase-3@example.com"}`, + headers: map[string][]string{ + "Authorization": {goodToken}, + "Content-Type": {"application/json"}, + }, + expectedCode: 422, + expectedContentType: "application/problem+json", + expectedBody: "{\"title\":\"can't create Publisher\",\"detail\":\"invalid format\",\"status\":422,\"validationErrors\":[{\"field\":\"description\",\"rule\":\"required\"}]}", + }, + { + query: "POST /v1/publishers - ExternalCode already exist", + body: `{"description":"new description", "codeHosting": [{"url" : "https://example-testcase-xx3.com"}], "email":"example-testcase-3-pass@example.com", "externalCode":"external-code-27"}`, headers: map[string][]string{ "Authorization": {goodToken}, "Content-Type": {"application/json"}, @@ -597,7 +621,7 @@ func TestPublishersEndpoints(t *testing.T) { description: "POST publishers - wrong token", fixtures: []string{"publishers.yml", "publishers_code_hosting.yml"}, query: "POST /v1/publishers", - body: `{"codeHosting": [{"url" : "https://www.example-5.com"}], "email":"example@example.com", "description": "Publisher description 1--x"}`, + body: `{"description":"new description", "codeHosting": [{"url" : "https://www.example-5.com"}], "email":"example@example.com"}`, headers: map[string][]string{ "Authorization": {badToken}, "Content-Type": {"application/json"}, @@ -624,7 +648,7 @@ func TestPublishersEndpoints(t *testing.T) { description: "POST publishers with optional boolean field set to false", fixtures: []string{"publishers.yml", "publishers_code_hosting.yml"}, query: "POST /v1/publishers", - body: `{"active": false, "codeHosting": [{"url" : "https://www.example.com"}], "email":"example-optional-boolean@example.com", "description": "Publisher description 1--x"}`, + body: `{"active": false, "description": "new description", "codeHosting": [{"url" : "https://www.example.com"}], "email":"example-optional-boolean@example.com"}`, headers: map[string][]string{ "Authorization": {goodToken}, "Content-Type": {"application/json"}, @@ -638,8 +662,8 @@ func TestPublishersEndpoints(t *testing.T) { { description: "POST publishers with codeHosting optional boolean field (group) set to false", query: "POST /v1/publishers", + body: `{"description":"new description", "codeHosting": [{"url" : "https://www.example.com", "group": false}], "email":"example-optional-group@example.com"}`, fixtures: []string{"publishers.yml", "publishers_code_hosting.yml"}, - body: `{"codeHosting": [{"url" : "https://www.example.com", "group": false}], "email":"example-optional-group@example.com", "description": "Publisher description 1--x"}`, headers: map[string][]string{ "Authorization": {goodToken}, "Content-Type": {"application/json"},