Skip to content

Commit

Permalink
Fixed spaces issue (#1550)
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulWahab3181 authored Feb 22, 2024
1 parent 54731b8 commit af7d6c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions handlers/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions handlers/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit af7d6c8

Please sign in to comment.