From d6624b43e5a75f647f28c7999fcc1c84b7365f50 Mon Sep 17 00:00:00 2001 From: elraphty Date: Tue, 25 Jun 2024 21:49:45 +0100 Subject: [PATCH] rebased --- handlers/bots_test.go | 1 - handlers/channel_test.go | 6 ++--- handlers/workspaces_test.go | 51 +++++++++++++++++++++++++++---------- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/handlers/bots_test.go b/handlers/bots_test.go index c48dec0ab..7ef372806 100644 --- a/handlers/bots_test.go +++ b/handlers/bots_test.go @@ -14,7 +14,6 @@ import ( "github.com/lib/pq" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/db" - "github.com/stretchr/testify/assert" ) diff --git a/handlers/channel_test.go b/handlers/channel_test.go index 9b0b8547b..a9053546f 100644 --- a/handlers/channel_test.go +++ b/handlers/channel_test.go @@ -4,19 +4,17 @@ import ( "bytes" "context" "encoding/json" - "github.com/lib/pq" "net/http" "net/http/httptest" "strconv" "testing" - "github.com/go-chi/chi" "github.com/lib/pq" + + "github.com/go-chi/chi" "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) { diff --git a/handlers/workspaces_test.go b/handlers/workspaces_test.go index 798ebc493..4d67df593 100644 --- a/handlers/workspaces_test.go +++ b/handlers/workspaces_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/google/uuid" "net/http" "net/http/httptest" "strings" @@ -336,41 +337,62 @@ func TestDeleteWorkspace(t *testing.T) { func TestGetWorkspaceBounties(t *testing.T) { ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") - mockDb := mocks.NewDatabase(t) + teardownSuite := SetupSuite(t) + defer teardownSuite(t) mockGenerateBountyHandler := func(bounties []db.NewBounty) []db.BountyResponse { return []db.BountyResponse{} // Mocked response } - oHandler := NewWorkspaceHandler(mockDb) + oHandler := NewWorkspaceHandler(db.TestDB) + + workspace := db.Workspace{ + Uuid: uuid.New().String(), + Name: uuid.New().String(), + OwnerPubKey: "workspace_owner_bounties_pubkey", + Github: "https://github.com/bounties", + Website: "https://www.bountieswebsite.com", + Description: "Workspace Bounties Description", + } + db.TestDB.CreateOrEditWorkspace(workspace) + + bounty := db.NewBounty{ + Type: "coding", + Title: "existing bounty", + Description: "existing bounty description", + WorkspaceUuid: workspace.Uuid, + OwnerID: "workspace-user", + Price: 2000, + } + db.TestDB.CreateOrEditBounty(bounty) t.Run("Should test that a workspace's bounties can be listed without authentication", func(t *testing.T) { - workspaceUUID := "valid-uuid" - oHandler.generateBountyHandler = mockGenerateBountyHandler - - expectedBounties := []db.NewBounty{{}, {}} // Mocked response - mockDb.On("GetWorkspaceBounties", mock.AnythingOfType("*http.Request"), workspaceUUID).Return(expectedBounties).Once() + oHandler.generateBountyHandler = mockGenerateBountyHandler rr := httptest.NewRecorder() handler := http.HandlerFunc(oHandler.GetWorkspaceBounties) rctx := chi.NewRouteContext() - rctx.URLParams.Add("uuid", workspaceUUID) - req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bounties/"+workspaceUUID, nil) + rctx.URLParams.Add("uuid", workspace.Uuid) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bounties/"+workspace.Uuid, nil) if err != nil { t.Fatal(err) } + fetchedWorkspace := db.TestDB.GetWorkspaceByUuid(workspace.Uuid) + workspace.ID = fetchedWorkspace.ID + + fetchedBounty := db.TestDB.GetWorkspaceBounties(req, bounty.WorkspaceUuid) + bounty.ID = fetchedBounty[0].ID handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusOK, rr.Code) + assert.Equal(t, workspace, fetchedWorkspace) + assert.Equal(t, bounty, fetchedBounty[0]) }) t.Run("should return empty array when wrong workspace UUID is passed", func(t *testing.T) { - workspaceUUID := "wrong-uuid" - - mockDb.On("GetWorkspaceBounties", mock.AnythingOfType("*http.Request"), workspaceUUID).Return([]db.NewBounty{}).Once() - rr := httptest.NewRecorder() handler := http.HandlerFunc(oHandler.GetWorkspaceBounties) + workspaceUUID := "wrong-uuid" rctx := chi.NewRouteContext() rctx.URLParams.Add("uuid", workspaceUUID) @@ -379,6 +401,8 @@ func TestGetWorkspaceBounties(t *testing.T) { t.Fatal(err) } + fetchedWorkspaceWrong := db.TestDB.GetWorkspaceByUuid(workspaceUUID) + handler.ServeHTTP(rr, req) // Assert that the response status code is as expected @@ -386,6 +410,7 @@ func TestGetWorkspaceBounties(t *testing.T) { // Assert that the response body is an empty array assert.Equal(t, "[]\n", rr.Body.String()) + assert.NotEqual(t, workspace, fetchedWorkspaceWrong) }) }