Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend [Integration Test] bounty.go handlers GetPersonCreatedBounties, GetPersonAssignedBounties, & GetBountyByCreated #1544

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
238 changes: 235 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,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)
Comment on lines +517 to +518
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noticed that we dont assert the exact value here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevkevinpal Added additional assertions to validate the response


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 @@ -563,3 +601,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)
Comment on lines +663 to +664
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just notice that we're not asserting the exact response but just the length and that we have a value coming back

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevkevinpal Added additional assertions to validate the exact response.


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)
Comment on lines +760 to +761
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same with just then lenght and a value being passed back

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevkevinpal Added additional assertions to validate the response


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)
Comment on lines +794 to +795
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same with this assertion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is not need as the response data is already empty

})
}
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
Loading