Skip to content

Commit

Permalink
fix: publisher's description must be unique and not NULL (#154)
Browse files Browse the repository at this point in the history
Also, fix tests for #149.
  • Loading branch information
bfabio committed Oct 4, 2022
1 parent d07ffb2 commit 646640f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
4 changes: 2 additions & 2 deletions internal/handlers/publishers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -171,7 +171,7 @@ func (p *Publisher) updatePublisherTrx(
}

if request.Description != "" {
publisher.Description = &request.Description
publisher.Description = request.Description
}

if request.Email != "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
4 changes: 2 additions & 2 deletions internal/models/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestPublisherCreate(t *testing.T) {
err := db.Create(
&Publisher{
ID: utils.UUIDv4(),
Description: &description,
Description: description,
Email: "[email protected]",
},
).Error
Expand All @@ -138,7 +138,7 @@ func TestPublisherCreate(t *testing.T) {
err = db.Create(
&Publisher{
ID: "2ded32eb-c45e-4167-9166-a44e18b8adde",
Description: &description,
Description: description,
Email: "[email protected]",
},
).Error
Expand Down
48 changes: 36 additions & 12 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":"[email protected]", "description" : "Description example test case 1"}`,
body: `{"description": "new description", "codeHosting": [{"url" : "https://www.example-testcase-1.com"}], "email":"[email protected]"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
Expand Down Expand Up @@ -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":"[email protected]", "externalCode":"example-testcase-2"}`,
fixtures: []string{"publishers.yml", "publishers_code_hosting.yml"},
body: `{"codeHosting": [{"url" : "https://www.example-testcase-2.com"}], "email":"[email protected]", "externalCode":"example-testcase-2", "description" : "Test example testcase 2"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
Expand Down Expand Up @@ -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":"[email protected]", "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":"[email protected]", "externalCode":"example-testcase-3"}`,
fixtures: []string{"publishers.yml", "publishers_code_hosting.yml"},
headers: map[string][]string{
"Authorization": {goodToken},
Expand Down Expand Up @@ -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":"[email protected]", "description":"new description"}`,
fixtures: []string{"publishers.yml", "publishers_code_hosting.yml"},
body: `{"codeHosting": [{"url" : "hTtPs://1-A.example.org/code/repo/"}], "email":"[email protected]", "description" : "New POST description example testcase 2"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
Expand Down Expand Up @@ -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":"[email protected]", "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":"[email protected]"}`,
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":"[email protected]"}`,
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":"[email protected]", "externalCode":"external-code-27"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
Expand All @@ -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":"[email protected]", "description": "Publisher description 1--x"}`,
body: `{"description":"new description", "codeHosting": [{"url" : "https://www.example-5.com"}], "email":"[email protected]"}`,
headers: map[string][]string{
"Authorization": {badToken},
"Content-Type": {"application/json"},
Expand All @@ -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":"[email protected]", "description": "Publisher description 1--x"}`,
body: `{"active": false, "description": "new description", "codeHosting": [{"url" : "https://www.example.com"}], "email":"[email protected]"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
Expand All @@ -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":"[email protected]"}`,
fixtures: []string{"publishers.yml", "publishers_code_hosting.yml"},
body: `{"codeHosting": [{"url" : "https://www.example.com", "group": false}], "email":"[email protected]", "description": "Publisher description 1--x"}`,
headers: map[string][]string{
"Authorization": {goodToken},
"Content-Type": {"application/json"},
Expand Down

0 comments on commit 646640f

Please sign in to comment.