Skip to content

Commit

Permalink
Refactor TestGetWorkspaceBounties To Use A Real Postgres DB For The Test
Browse files Browse the repository at this point in the history
  • Loading branch information
MahtabBukhari committed Jun 25, 2024
1 parent b0c735b commit 9d7559c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
5 changes: 0 additions & 5 deletions handlers/bots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import (
"github.com/lib/pq"
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
dbMocks "github.com/stakwork/sphinx-tribes/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestGetBotByUniqueName(t *testing.T) {
Expand Down Expand Up @@ -517,9 +515,6 @@ func TestGetBot(t *testing.T) {
err = json.Unmarshal(rr.Body.Bytes(), &returnedBot)
assert.Equal(t, http.StatusOK, rr.Code)

returnedBot.Tsv = ""
fetchedBot.Tsv = ""

assert.Equal(t, bot, returnedBot)
assert.Equal(t, bot, fetchedBot)
})
Expand Down
51 changes: 38 additions & 13 deletions handlers/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -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)
Expand All @@ -379,13 +401,16 @@ 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
assert.Equal(t, http.StatusOK, rr.Code)

// Assert that the response body is an empty array
assert.Equal(t, "[]\n", rr.Body.String())
assert.NotEqual(t, workspace, fetchedWorkspaceWrong)
})
}

Expand Down

0 comments on commit 9d7559c

Please sign in to comment.