Skip to content

Commit

Permalink
Merge pull request #1757 from MahtabBukhari/Refactor_TestGetWorkspace…
Browse files Browse the repository at this point in the history
…Bounties_To_Use_A_Real_Postgres_DB_For_The_Test

Refactor TestGetWorkspaceBounties To Use A Real Postgres DB For The Test
  • Loading branch information
elraphty authored Jun 26, 2024
2 parents 20f56e5 + d6624b4 commit bf7f028
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
1 change: 0 additions & 1 deletion handlers/bots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
6 changes: 2 additions & 4 deletions handlers/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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 bf7f028

Please sign in to comment.