Skip to content

Commit

Permalink
Added UTs for GetPersonCreatedBounties, GetPersonAssignedBounties, Ge…
Browse files Browse the repository at this point in the history
…tBountyByCreated
  • Loading branch information
AbdulWahab3181 committed Feb 21, 2024
1 parent f247c84 commit defc38d
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 15 deletions.
20 changes: 10 additions & 10 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (
)

type bountyHandler struct {
httpClient HttpClient
db db.Database
httpClient HttpClient
db db.Database
generateBountyResponse func(bounties []db.Bounty) []db.BountyResponse
}

func NewBountyHandler(httpClient HttpClient, db db.Database) *bountyHandler {
return &bountyHandler{
httpClient: httpClient,
db: db,
httpClient: httpClient,
db: db,
generateBountyResponse: GenerateBountyResponse,
}
}
Expand Down Expand Up @@ -145,25 +145,25 @@ func GetBountyCount(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(bountyCount)
}

func GetPersonCreatedBounties(w http.ResponseWriter, r *http.Request) {
bounties, err := db.DB.GetCreatedBounties(r)
func (h *bountyHandler) GetPersonCreatedBounties(w http.ResponseWriter, r *http.Request) {
bounties, err := h.db.GetCreatedBounties(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
} else {
var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties)
var bountyResponse []db.BountyResponse = h.generateBountyResponse(bounties)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(bountyResponse)
}
}

func GetPersonAssignedBounties(w http.ResponseWriter, r *http.Request) {
bounties, err := db.DB.GetAssignedBounties(r)
func (h *bountyHandler) GetPersonAssignedBounties(w http.ResponseWriter, r *http.Request) {
bounties, err := h.db.GetAssignedBounties(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("Error", err)
} else {
var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties)
var bountyResponse []db.BountyResponse = h.generateBountyResponse(bounties)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(bountyResponse)
}
Expand Down
217 changes: 214 additions & 3 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,35 @@ func TestDeleteBounty(t *testing.T) {
func TestGetBountyByCreated(t *testing.T) {
ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key")
mockDb := dbMocks.NewDatabase(t)
mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse {
return []db.BountyResponse{} // Mocked response
}
mockHttpClient := mocks.NewHttpClient(t)
bHandler := NewBountyHandler(mockHttpClient, mockDb)

t.Run("Should return bounty by its created value", func(t *testing.T) {
mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse {
var bountyResponses []db.BountyResponse

for _, bounty := range bounties {
owner := db.Person{
ID: 1,
}
assignee := db.Person{
ID: 1,
}
organization := db.OrganizationShort{
Uuid: "uuid",
}

bountyResponse := db.BountyResponse{
Bounty: bounty,
Assignee: assignee,
Owner: owner,
Organization: organization,
}
bountyResponses = append(bountyResponses, bountyResponse)
}

return bountyResponses
}
bHandler.generateBountyResponse = mockGenerateBountyResponse

expectedBounty := []db.Bounty{{
Expand All @@ -485,6 +507,15 @@ func TestGetBountyByCreated(t *testing.T) {
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)

var responseData []db.BountyResponse
err = json.Unmarshal(rr.Body.Bytes(), &responseData)
if err != nil {
t.Fatalf("Error decoding JSON response: %s", err)
}

assert.NotEmpty(t, responseData)
assert.Len(t, responseData, 1)
})
}

Expand Down Expand Up @@ -563,3 +594,183 @@ func TestGetOrganizationPreviousBountyByCreated(t *testing.T) {
mockDb.AssertExpectations(t)
})
}

func TestGetPersonAssignedBounties(t *testing.T) {
ctx := context.Background()
mockDb := dbMocks.NewDatabase(t)
mockHttpClient := mocks.NewHttpClient(t)
bHandler := NewBountyHandler(mockHttpClient, mockDb)

t.Run("should return bounties assigned to the user", func(t *testing.T) {
mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse {
var bountyResponses []db.BountyResponse

for _, bounty := range bounties {
owner := db.Person{
ID: 1,
}
assignee := db.Person{
ID: 1,
}
organization := db.OrganizationShort{
Uuid: "uuid",
}

bountyResponse := db.BountyResponse{
Bounty: bounty,
Assignee: assignee,
Owner: owner,
Organization: organization,
}
bountyResponses = append(bountyResponses, bountyResponse)
}

return bountyResponses
}
bHandler.generateBountyResponse = mockGenerateBountyResponse

mockDb.On("GetAssignedBounties", mock.Anything).Return([]db.Bounty{
{ID: 1, Assignee: "user1"},
{ID: 2, Assignee: "user1"},
}, nil).Once()

rr := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/wanteds/assigned/uuid", nil)
req = req.WithContext(ctx)
if err != nil {
t.Fatal(err)
}

bHandler.GetPersonAssignedBounties(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)

var responseData []db.BountyResponse
err = json.Unmarshal(rr.Body.Bytes(), &responseData)
if err != nil {
t.Fatalf("Error decoding JSON response: %s", err)
}

assert.NotEmpty(t, responseData)
assert.Len(t, responseData, 2)
})

t.Run("should not return bounties assigned to other users", func(t *testing.T) {
mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse {
return []db.BountyResponse{}
}
bHandler.generateBountyResponse = mockGenerateBountyResponse

mockDb.On("GetAssignedBounties", mock.Anything).Return([]db.Bounty{}, nil).Once()

rr := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/wanteds/assigned/uuid", nil)
req = req.WithContext(ctx)
if err != nil {
t.Fatal(err)
}

bHandler.GetPersonAssignedBounties(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)

var responseData []db.BountyResponse
err = json.Unmarshal(rr.Body.Bytes(), &responseData)
if err != nil {
t.Fatalf("Error decoding JSON response: %s", err)
}

assert.Empty(t, responseData)
assert.Len(t, responseData, 0)
})
}

func TestGetPersonCreatedBounties(t *testing.T) {
ctx := context.Background()
mockDb := dbMocks.NewDatabase(t)
mockHttpClient := mocks.NewHttpClient(t)
bHandler := NewBountyHandler(mockHttpClient, mockDb)

t.Run("should return bounties created by the user", func(t *testing.T) {
mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse {
var bountyResponses []db.BountyResponse

for _, bounty := range bounties {
owner := db.Person{
ID: 1,
}
assignee := db.Person{
ID: 1,
}
organization := db.OrganizationShort{
Uuid: "uuid",
}

bountyResponse := db.BountyResponse{
Bounty: bounty,
Assignee: assignee,
Owner: owner,
Organization: organization,
}
bountyResponses = append(bountyResponses, bountyResponse)
}

return bountyResponses
}
bHandler.generateBountyResponse = mockGenerateBountyResponse

mockDb.On("GetCreatedBounties", mock.Anything).Return([]db.Bounty{
{ID: 1, OwnerID: "user1"},
{ID: 2, OwnerID: "user1"},
}, nil).Once()

rr := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/wanteds/created/uuid", nil)
req = req.WithContext(ctx)
if err != nil {
t.Fatal(err)
}

bHandler.GetPersonCreatedBounties(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)

var responseData []db.BountyResponse
err = json.Unmarshal(rr.Body.Bytes(), &responseData)
if err != nil {
t.Fatalf("Error decoding JSON response: %s", err)
}

assert.NotEmpty(t, responseData)
assert.Len(t, responseData, 2)
})

t.Run("should not return bounties created by other users", func(t *testing.T) {
mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse {
return []db.BountyResponse{}
}
bHandler.generateBountyResponse = mockGenerateBountyResponse

mockDb.On("GetCreatedBounties", mock.Anything).Return([]db.Bounty{}, nil).Once()

rr := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/wanteds/created/uuid", nil)
req = req.WithContext(ctx)
if err != nil {
t.Fatal(err)
}

bHandler.GetPersonCreatedBounties(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)

var responseData []db.BountyResponse
err = json.Unmarshal(rr.Body.Bytes(), &responseData)
if err != nil {
t.Fatalf("Error decoding JSON response: %s", err)
}

assert.Empty(t, responseData)
assert.Len(t, responseData, 0)
})
}
7 changes: 5 additions & 2 deletions routes/people.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package routes

import (
"net/http"

"github.com/go-chi/chi"
"github.com/stakwork/sphinx-tribes/db"
"github.com/stakwork/sphinx-tribes/handlers"
Expand All @@ -9,12 +11,13 @@ import (
func PeopleRoutes() chi.Router {
r := chi.NewRouter()
peopleHandler := handlers.NewPeopleHandler(db.DB)
bountyHandler := handlers.NewBountyHandler(http.DefaultClient, db.DB)
r.Group(func(r chi.Router) {
r.Get("/", peopleHandler.GetListedPeople)
r.Get("/search", peopleHandler.GetPeopleBySearch)
r.Get("/posts", handlers.GetListedPosts)
r.Get("/wanteds/assigned/{uuid}", handlers.GetPersonAssignedBounties)
r.Get("/wanteds/created/{uuid}", handlers.GetPersonCreatedBounties)
r.Get("/wanteds/assigned/{uuid}", bountyHandler.GetPersonAssignedBounties)
r.Get("/wanteds/created/{uuid}", bountyHandler.GetPersonCreatedBounties)
r.Get("/wanteds/header", handlers.GetWantedsHeader)
r.Get("/short", handlers.GetPeopleShortList)
r.Get("/offers", handlers.GetListedOffers)
Expand Down

0 comments on commit defc38d

Please sign in to comment.