From c8974fa7ae28fd85cbb8a4ce3679dd7dba07cfb9 Mon Sep 17 00:00:00 2001 From: AbdulWahab3181 <41957704+AbdulWahab3181@users.noreply.github.com> Date: Thu, 22 Feb 2024 21:37:40 +0500 Subject: [PATCH] lets add another test to create a name that looks like " Abdul " and assert it gets added as "Abdul" (#1553) * Added UT to test trim spaces * Removed unnecessary log --- handlers/organization_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/handlers/organization_test.go b/handlers/organization_test.go index 4b69712f0..933fd7fa0 100644 --- a/handlers/organization_test.go +++ b/handlers/organization_test.go @@ -101,6 +101,35 @@ func TestUnitCreateOrEditOrganization(t *testing.T) { assert.Equal(t, http.StatusBadRequest, rr.Code) }) + t.Run("should trim spaces from organization name", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(oHandler.CreateOrEditOrganization) + + mockDb.On("GetOrganizationByUuid", mock.AnythingOfType("string")).Return(db.Organization{}).Once() + mockDb.On("GetOrganizationByName", "Abdul").Return(db.Organization{}).Once() + mockDb.On("CreateOrEditOrganization", mock.MatchedBy(func(org db.Organization) bool { + return org.Name == "Abdul" && org.Uuid != "" && org.Updated != nil && org.Created != nil + })).Return(db.Organization{Name: "Abdul"}, nil).Once() + + jsonInput := []byte(`{"name": " Abdul ", "owner_pubkey": "test-key" ,"description": "Test"}`) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(jsonInput)) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusOK, rr.Code) + + var responseOrg db.Organization + err = json.Unmarshal(rr.Body.Bytes(), &responseOrg) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, "Abdul", responseOrg.Name) + }) + t.Run("should successfully add organization if request is valid", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(oHandler.CreateOrEditOrganization)