diff --git a/handlers/bounty.go b/handlers/bounty.go index 3c3f186ae..b8879f0f2 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -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, } } @@ -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) } diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 83f6bb7db..6001ac45b 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -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{{ @@ -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) + + 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) }) } @@ -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) + + 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) + }) +} diff --git a/routes/people.go b/routes/people.go index c21c02938..b65648d99 100644 --- a/routes/people.go +++ b/routes/people.go @@ -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" @@ -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)