Skip to content

Commit

Permalink
Merge pull request #1544 from AbdulWahab3181/bounty-handler-UTs-V2
Browse files Browse the repository at this point in the history
Backend [Integration Test] bounty.go handlers GetPersonCreatedBounties, GetPersonAssignedBounties, & GetBountyByCreated
  • Loading branch information
elraphty authored Feb 23, 2024
2 parents 816482e + 9305d72 commit 204eb81
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 11 deletions.
12 changes: 6 additions & 6 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
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
238 changes: 235 additions & 3 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,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 @@ -527,6 +549,22 @@ 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)

returnedBounty := responseData[0].Bounty
assert.Equal(t, expectedBounty[0].ID, returnedBounty.ID)
assert.Equal(t, expectedBounty[0].Type, returnedBounty.Type)
assert.Equal(t, expectedBounty[0].Title, returnedBounty.Title)
assert.Equal(t, expectedBounty[0].Description, returnedBounty.Description)
assert.Equal(t, expectedBounty[0].Created, returnedBounty.Created)
})
}

Expand Down Expand Up @@ -605,3 +643,197 @@ 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

expectedBounties := []db.Bounty{
{ID: 1, Assignee: "user1"},
{ID: 2, Assignee: "user1"},
}

mockDb.On("GetAssignedBounties", mock.Anything).Return(expectedBounties, 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)

for i, expectedBounty := range expectedBounties {
assert.Equal(t, expectedBounty.ID, responseData[i].Bounty.ID)
assert.Equal(t, expectedBounty.Assignee, responseData[i].Bounty.Assignee)
}
})

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

expectedBounties := []db.Bounty{
{ID: 1, OwnerID: "user1"},
{ID: 2, OwnerID: "user1"},
}

mockDb.On("GetCreatedBounties", mock.Anything).Return(expectedBounties, 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)

for i, expectedBounty := range expectedBounties {
assert.Equal(t, expectedBounty.ID, responseData[i].Bounty.ID)
assert.Equal(t, expectedBounty.Assignee, responseData[i].Bounty.Assignee)
}
})

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 204eb81

Please sign in to comment.