From efb5502244b950865e5abd3b3d3422eaeaea9fcc Mon Sep 17 00:00:00 2001 From: AhsanFarooqDev Date: Wed, 26 Jun 2024 09:34:52 +0500 Subject: [PATCH] Refactor TestUnitCreateOrEditWorkspace UT --- handlers/channel_test.go | 7 ++-- handlers/workspaces_test.go | 67 +++++++++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/handlers/channel_test.go b/handlers/channel_test.go index 9b0b8547b..a3a411924 100644 --- a/handlers/channel_test.go +++ b/handlers/channel_test.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "encoding/json" - "github.com/lib/pq" + "github.com/google/uuid" "net/http" "net/http/httptest" "strconv" @@ -14,9 +14,8 @@ import ( "github.com/lib/pq" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/db" - mocks "github.com/stakwork/sphinx-tribes/mocks" + "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" ) func TestCreateChannel(t *testing.T) { @@ -73,7 +72,7 @@ func TestCreateChannel(t *testing.T) { }) t.Run("Should test that an authenticated user can create a channel", func(t *testing.T) { - person, tribe := createTestPersonAndTribe("person_chan_pubkey", "tribe_uuid", "New Tribe") + person, tribe := createTestPersonAndTribe("person_chan_pubkey", uuid.New().String(), "New Tribe") requestBody := map[string]interface{}{ "tribe_uuid": tribe.UUID, diff --git a/handlers/workspaces_test.go b/handlers/workspaces_test.go index 798ebc493..5e2fb1d34 100644 --- a/handlers/workspaces_test.go +++ b/handlers/workspaces_test.go @@ -6,10 +6,13 @@ import ( "encoding/json" "errors" "fmt" + "github.com/google/uuid" + "golang.org/x/exp/rand" "net/http" "net/http/httptest" "strings" "testing" + "time" "github.com/go-chi/chi" "github.com/stakwork/sphinx-tribes/auth" @@ -20,9 +23,9 @@ import ( ) func TestUnitCreateOrEditWorkspace(t *testing.T) { - ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") - mockDb := mocks.NewDatabase(t) - oHandler := NewWorkspaceHandler(mockDb) + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + oHandler := NewWorkspaceHandler(db.TestDB) t.Run("should return error if body is not a valid json", func(t *testing.T) { rr := httptest.NewRecorder() @@ -61,6 +64,7 @@ func TestUnitCreateOrEditWorkspace(t *testing.T) { handler := http.HandlerFunc(oHandler.CreateOrEditWorkspace) invalidJson := []byte(`{"name": ""}`) + ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(invalidJson)) if err != nil { t.Fatal(err) @@ -76,6 +80,7 @@ func TestUnitCreateOrEditWorkspace(t *testing.T) { handler := http.HandlerFunc(oHandler.CreateOrEditWorkspace) invalidJson := []byte(`{"name": "DemoTestingNewWorkspace"}`) + ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(invalidJson)) if err != nil { t.Fatal(err) @@ -91,6 +96,7 @@ func TestUnitCreateOrEditWorkspace(t *testing.T) { handler := http.HandlerFunc(oHandler.CreateOrEditWorkspace) invalidJson := []byte(`{"name": " "}`) + ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(invalidJson)) if err != nil { t.Fatal(err) @@ -105,13 +111,20 @@ func TestUnitCreateOrEditWorkspace(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(oHandler.CreateOrEditWorkspace) - mockDb.On("GetWorkspaceByUuid", mock.AnythingOfType("string")).Return(db.Workspace{}).Once() - mockDb.On("GetWorkspaceByName", "Abdul").Return(db.Workspace{}).Once() - mockDb.On("CreateOrEditWorkspace", mock.MatchedBy(func(org db.Workspace) bool { - return org.Name == "Abdul" && org.Uuid != "" && org.Updated != nil && org.Created != nil - })).Return(db.Workspace{Name: "Abdul"}, nil).Once() + const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + rand.Seed(uint64(time.Now().UnixNano())) + + b := make([]byte, 10) + for i := range b { + b[i] = letters[rand.Intn(len(letters))] + } + name := string(b) + + spacedName := " " + name + " " + + jsonInput := []byte(fmt.Sprintf(`{"name": "%s", "owner_pubkey": "test-key", "description": "Workspace Bounties Description"}`, spacedName)) - jsonInput := []byte(`{"name": " Abdul ", "owner_pubkey": "test-key" ,"description": "Test"}`) + ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(jsonInput)) if err != nil { t.Fatal(err) @@ -127,21 +140,38 @@ func TestUnitCreateOrEditWorkspace(t *testing.T) { t.Fatal(err) } - assert.Equal(t, "Abdul", responseOrg.Name) + assert.Equal(t, name, responseOrg.Name) }) t.Run("should successfully add workspace if request is valid", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(oHandler.CreateOrEditWorkspace) - mockDb.On("GetWorkspaceByUuid", mock.AnythingOfType("string")).Return(db.Workspace{}).Once() - mockDb.On("GetWorkspaceByName", "TestWorkspace").Return(db.Workspace{}).Once() - mockDb.On("CreateOrEditWorkspace", mock.MatchedBy(func(org db.Workspace) bool { - return org.Name == "TestWorkspace" && org.Uuid != "" && org.Updated != nil && org.Created != nil - })).Return(db.Workspace{}, nil).Once() + const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + rand.Seed(uint64(time.Now().UnixNano())) - invalidJson := []byte(`{"name": "TestWorkspace", "owner_pubkey": "test-key" ,"description": "Test"}`) - req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(invalidJson)) + b := make([]byte, 10) + for i := range b { + b[i] = letters[rand.Intn(len(letters))] + } + name := string(b) + + workspace := db.Workspace{ + Uuid: uuid.New().String(), + Name: name, + OwnerPubKey: uuid.New().String(), + Github: "https://github.com/bounties", + Website: "https://www.bountieswebsite.com", + Description: "Workspace Bounties Description", + } + db.TestDB.CreateOrEditWorkspace(workspace) + + Workspace := db.TestDB.GetWorkspaceByUuid(workspace.Uuid) + workspace.ID = Workspace.ID + + requestBody, _ := json.Marshal(workspace) + ctx := context.WithValue(context.Background(), auth.ContextKey, workspace.OwnerPubKey) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(requestBody)) if err != nil { t.Fatal(err) } @@ -149,6 +179,7 @@ func TestUnitCreateOrEditWorkspace(t *testing.T) { handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusOK, rr.Code) + assert.Equal(t, workspace, Workspace) }) t.Run("should return error if org description is empty or too long", func(t *testing.T) { tests := []struct { @@ -164,7 +195,7 @@ func TestUnitCreateOrEditWorkspace(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(oHandler.CreateOrEditWorkspace) invalidJson := []byte(fmt.Sprintf(`{"name": "TestWorkspace", "owner_pubkey": "test-key", "description": "%s"}`, tc.description)) - + ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/", bytes.NewReader(invalidJson)) if err != nil { t.Fatal(err)