From c8ca57c103f07f2c3577ee74a0c186b3b5af2b39 Mon Sep 17 00:00:00 2001 From: Abdul Wahab Date: Thu, 22 Feb 2024 16:35:14 +0500 Subject: [PATCH] Fixed spaces issue --- handlers/organization_test.go | 15 +++++++++++++++ handlers/organizations.go | 2 ++ 2 files changed, 17 insertions(+) diff --git a/handlers/organization_test.go b/handlers/organization_test.go index a108ca4ad..4b69712f0 100644 --- a/handlers/organization_test.go +++ b/handlers/organization_test.go @@ -86,6 +86,21 @@ func TestUnitCreateOrEditOrganization(t *testing.T) { assert.Equal(t, http.StatusBadRequest, rr.Code) }) + t.Run("should return error if org name contains only spaces", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(oHandler.CreateOrEditOrganization) + + invalidJson := []byte(`{"name": " "}`) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(invalidJson)) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusBadRequest, rr.Code) + }) + t.Run("should successfully add organization if request is valid", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(oHandler.CreateOrEditOrganization) diff --git a/handlers/organizations.go b/handlers/organizations.go index a9f17afe2..60514dde4 100644 --- a/handlers/organizations.go +++ b/handlers/organizations.go @@ -48,6 +48,8 @@ func (oh *organizationHandler) CreateOrEditOrganization(w http.ResponseWriter, r return } + org.Name = strings.TrimSpace(org.Name) + if len(org.Name) == 0 || len(org.Name) > 20 { fmt.Printf("invalid organization name %s\n", org.Name) w.WriteHeader(http.StatusBadRequest)