From 3b633fe5269590bb7fb4a6f55819be935d5f49aa Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Wed, 21 Feb 2024 01:12:20 +0530 Subject: [PATCH 01/21] pushed test for Backend [Integration Test] bounty.go handlers GetPersonCreatedBounties, GetPersonAssignedBounties, & GetBountyByCreated #1509 --- handlers/bounty.go | 36 ++++----- handlers/bounty_test.go | 161 ++++++++++++++++++++++++++++++++++++++ handlers/organizations.go | 84 ++++++++++++++++++++ routes/bounty.go | 6 +- routes/people.go | 8 +- 5 files changed, 272 insertions(+), 23 deletions(-) diff --git a/handlers/bounty.go b/handlers/bounty.go index 3019313fb..9d7ade72d 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -32,23 +32,23 @@ func NewBountyHandler(httpClient HttpClient, db db.Database) *bountyHandler { func (h *bountyHandler) GetAllBounties(w http.ResponseWriter, r *http.Request) { bounties := h.db.GetAllBounties(r) - var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties) + var bountyResponse []db.BountyResponse = h.GenerateBountyResponse(bounties) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(bountyResponse) } -func GetBountyById(w http.ResponseWriter, r *http.Request) { +func (h *bountyHandler) GetBountyById(w http.ResponseWriter, r *http.Request) { bountyId := chi.URLParam(r, "bountyId") if bountyId == "" { w.WriteHeader(http.StatusNotFound) } - bounties, err := db.DB.GetBountyById(bountyId) + bounties, err := h.db.GetBountyById(bountyId) 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) } @@ -98,7 +98,7 @@ func GetOrganizationPreviousBountyByCreated(w http.ResponseWriter, r *http.Reque } } -func GetBountyIndexById(w http.ResponseWriter, r *http.Request) { +func (h *bountyHandler) GetBountyIndexById(w http.ResponseWriter, r *http.Request) { bountyId := chi.URLParam(r, "bountyId") if bountyId == "" { w.WriteHeader(http.StatusNotFound) @@ -108,17 +108,17 @@ func GetBountyIndexById(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(bountyIndex) } -func GetBountyByCreated(w http.ResponseWriter, r *http.Request) { +func (h *bountyHandler) GetBountyByCreated(w http.ResponseWriter, r *http.Request) { created := chi.URLParam(r, "created") if created == "" { w.WriteHeader(http.StatusNotFound) } - bounties, err := db.DB.GetBountyDataByCreated(created) + bounties, err := h.db.GetBountyDataByCreated(created) 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) } @@ -143,25 +143,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) } @@ -317,15 +317,15 @@ func UpdatePaymentStatus(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(bounty) } -func GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse { +func (h *bountyHandler) GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse { var bountyResponse []db.BountyResponse for i := 0; i < len(bounties); i++ { bounty := bounties[i] - owner := db.DB.GetPersonByPubkey(bounty.OwnerID) - assignee := db.DB.GetPersonByPubkey(bounty.Assignee) - organization := db.DB.GetOrganizationByUuid(bounty.OrgUuid) + owner := h.db.GetPersonByPubkey(bounty.OwnerID) + assignee := h.db.GetPersonByPubkey(bounty.Assignee) + organization := h.db.GetOrganizationByUuid(bounty.OrgUuid) b := db.BountyResponse{ Bounty: db.Bounty{ diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index d1f87a632..d848d1a0b 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -9,10 +9,13 @@ import ( "io" "net/http" "net/http/httptest" + "strconv" "strings" "testing" + "time" "github.com/go-chi/chi" + "github.com/lib/pq" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/config" "github.com/stakwork/sphinx-tribes/db" @@ -449,3 +452,161 @@ func TestDeleteBounty(t *testing.T) { mockDb.AssertExpectations(t) }) } + +// #1509 +func TestGetBountyByCreated(t *testing.T) { + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("Should return if bounty is present in db", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetBountyByCreated) + bounty := db.Bounty{ + ID: 1, + Type: "coding", + Title: "first bounty", + Description: "first bounty description", + OrgUuid: "org-1", + Assignee: "user1", + Created: 1707991475, + OwnerID: "owner-1", + } + createdStr := strconv.FormatInt(bounty.Created, 10) + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("created", "1707991475") + req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "gobounties/created/1707991475", nil) + mockDb.On("GetBountyDataByCreated", createdStr).Return([]db.Bounty{bounty}, nil).Once() + mockDb.On("GetPersonByPubkey", "owner-1").Return(db.Person{}).Once() + mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}).Once() + mockDb.On("GetOrganizationByUuid", "org-1").Return(db.Organization{}).Once() + handler.ServeHTTP(rr, req) + + var returnedBounty []db.BountyResponse + err := json.Unmarshal(rr.Body.Bytes(), &returnedBounty) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.NotEmpty(t, returnedBounty) + + }) +} + +func TestGetPersonAssignedBounties(t *testing.T) { + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("Should successfull Get Person Assigned Bounties", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetPersonAssignedBounties) + bounty := db.Bounty{ + ID: 1, + Type: "coding", + Title: "first bounty", + Description: "first bounty description", + OrgUuid: "org-1", + Assignee: "user1", + Created: 1707991475, + OwnerID: "owner-1", + } + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("uuid", "clu80datu2rjujsmim40") + rctx.URLParams.Add("sortBy", "paid") + rctx.URLParams.Add("page", "1") + rctx.URLParams.Add("limit", "20") + rctx.URLParams.Add("search", "") + req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "people/wanteds/assigned/clu80datu2rjujsmim40?sortBy=paid&page=1&limit=20&search=", nil) + + mockDb.On("GetAssignedBounties", req).Return([]db.Bounty{bounty}, nil).Once() + mockDb.On("GetPersonByPubkey", "owner-1").Return(db.Person{}, nil).Once() + mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}, nil).Once() + mockDb.On("GetOrganizationByUuid", "org-1").Return(db.Organization{}, nil).Once() + handler.ServeHTTP(rr, req) + + var returnedBounty []db.BountyResponse + err := json.Unmarshal(rr.Body.Bytes(), &returnedBounty) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.NotEmpty(t, returnedBounty) + }) +} + +// #1526 +func TestGetBountyById(t *testing.T) { + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("successful retrieval of bounty by ID", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetBountyById) + + bounty := db.Bounty{ + ID: 1, + OwnerID: "owner123", + Paid: false, + Show: true, + Type: "bug fix", + Award: "500", + AssignedHours: 10, + BountyExpires: "2023-12-31", + CommitmentFee: 1000, + Price: 500, + Title: "Fix critical bug in payment system", + Tribe: "development", + Assignee: "user1", + TicketUrl: "http://example.com/issues/1", + OrgUuid: "org-789", + Description: "This bounty is for fixing a critical bug in the payment system that causes transactions to fail under certain conditions.", + WantedType: "immediate", + Deliverables: "A pull request with a fix, including tests", + GithubDescription: true, + OneSentenceSummary: "Fix a critical payment system bug", + EstimatedSessionLength: "2 hours", + EstimatedCompletionDate: "2023-10-01", + Created: time.Now().Unix(), + Updated: nil, + AssignedDate: nil, + CompletionDate: nil, + MarkAsPaidDate: nil, + PaidDate: nil, + CodingLanguages: pq.StringArray{"Go", "Python"}, + } + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("bountyId", strconv.Itoa(int(bounty.ID))) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bounty/1", nil) + assert.NoError(t, err) + + mockDb.On("GetBountyById", mock.Anything).Return([]db.Bounty{bounty}, nil).Once() + mockDb.On("GetPersonByPubkey", "owner123").Return(db.Person{}).Once() + mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}).Once() + mockDb.On("GetOrganizationByUuid", "org-789").Return(db.Organization{}).Once() + + handler.ServeHTTP(rr, req) + + var returnedBounty []db.BountyResponse + err = json.Unmarshal(rr.Body.Bytes(), &returnedBounty) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + mockDb.AssertExpectations(t) + }) + + t.Run("bounty not found", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetBountyById) + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("bountyId", "999") + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bounty/999", nil) + assert.NoError(t, err) + + mockDb.On("GetBountyById", "999").Return(nil, errors.New("not-found")).Once() + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusBadRequest, rr.Code) + mockDb.AssertExpectations(t) + }) +} diff --git a/handlers/organizations.go b/handlers/organizations.go index d88bd5897..0f8a55211 100644 --- a/handlers/organizations.go +++ b/handlers/organizations.go @@ -709,3 +709,87 @@ func (oh *organizationHandler) DeleteOrganization(w http.ResponseWriter, r *http w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(org) } + +func GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse { + var bountyResponse []db.BountyResponse + + for i := 0; i < len(bounties); i++ { + bounty := bounties[i] + + owner := db.DB.GetPersonByPubkey(bounty.OwnerID) + assignee := db.DB.GetPersonByPubkey(bounty.Assignee) + organization := db.DB.GetOrganizationByUuid(bounty.OrgUuid) + + b := db.BountyResponse{ + Bounty: db.Bounty{ + ID: bounty.ID, + OwnerID: bounty.OwnerID, + Paid: bounty.Paid, + Show: bounty.Show, + Type: bounty.Type, + Award: bounty.Award, + AssignedHours: bounty.AssignedHours, + BountyExpires: bounty.BountyExpires, + CommitmentFee: bounty.CommitmentFee, + Price: bounty.Price, + Title: bounty.Title, + Tribe: bounty.Tribe, + Created: bounty.Created, + Assignee: bounty.Assignee, + TicketUrl: bounty.TicketUrl, + Description: bounty.Description, + WantedType: bounty.WantedType, + Deliverables: bounty.Deliverables, + GithubDescription: bounty.GithubDescription, + OneSentenceSummary: bounty.OneSentenceSummary, + EstimatedSessionLength: bounty.EstimatedSessionLength, + EstimatedCompletionDate: bounty.EstimatedCompletionDate, + OrgUuid: bounty.OrgUuid, + Updated: bounty.Updated, + CodingLanguages: bounty.CodingLanguages, + }, + Assignee: db.Person{ + ID: assignee.ID, + Uuid: assignee.Uuid, + OwnerPubKey: assignee.OwnerPubKey, + OwnerAlias: assignee.OwnerAlias, + UniqueName: assignee.UniqueName, + Description: assignee.Description, + Tags: assignee.Tags, + Img: assignee.Img, + Created: assignee.Created, + Updated: assignee.Updated, + LastLogin: assignee.LastLogin, + OwnerRouteHint: assignee.OwnerRouteHint, + OwnerContactKey: assignee.OwnerContactKey, + PriceToMeet: assignee.PriceToMeet, + TwitterConfirmed: assignee.TwitterConfirmed, + }, + Owner: db.Person{ + ID: owner.ID, + Uuid: owner.Uuid, + OwnerPubKey: owner.OwnerPubKey, + OwnerAlias: owner.OwnerAlias, + UniqueName: owner.UniqueName, + Description: owner.Description, + Tags: owner.Tags, + Img: owner.Img, + Created: owner.Created, + Updated: owner.Updated, + LastLogin: owner.LastLogin, + OwnerRouteHint: owner.OwnerRouteHint, + OwnerContactKey: owner.OwnerContactKey, + PriceToMeet: owner.PriceToMeet, + TwitterConfirmed: owner.TwitterConfirmed, + }, + Organization: db.OrganizationShort{ + Name: organization.Name, + Uuid: organization.Uuid, + Img: organization.Img, + }, + } + bountyResponse = append(bountyResponse, b) + } + + return bountyResponse +} diff --git a/routes/bounty.go b/routes/bounty.go index a169c04ee..ae5a15464 100644 --- a/routes/bounty.go +++ b/routes/bounty.go @@ -14,13 +14,13 @@ func BountyRoutes() chi.Router { bountyHandler := handlers.NewBountyHandler(http.DefaultClient, db.DB) r.Group(func(r chi.Router) { r.Get("/all", bountyHandler.GetAllBounties) - r.Get("/id/{bountyId}", handlers.GetBountyById) - r.Get("/index/{bountyId}", handlers.GetBountyIndexById) + r.Get("/id/{bountyId}", bountyHandler.GetBountyById) + r.Get("/index/{bountyId}", bountyHandler.GetBountyIndexById) r.Get("/next/{created}", handlers.GetNextBountyByCreated) r.Get("/previous/{created}", handlers.GetPreviousBountyByCreated) r.Get("/org/next/{uuid}/{created}", handlers.GetOrganizationNextBountyByCreated) r.Get("/org/previous/{uuid}/{created}", handlers.GetOrganizationPreviousBountyByCreated) - r.Get("/created/{created}", handlers.GetBountyByCreated) + r.Get("/created/{created}", bountyHandler.GetBountyByCreated) r.Get("/count/{personKey}/{tabType}", handlers.GetUserBountyCount) r.Get("/count", handlers.GetBountyCount) r.Get("/invoice/{paymentRequest}", handlers.GetInvoiceData) diff --git a/routes/people.go b/routes/people.go index 2ed0bfa85..19ced0160 100644 --- a/routes/people.go +++ b/routes/people.go @@ -1,18 +1,22 @@ package routes import ( + "net/http" + "github.com/go-chi/chi" + "github.com/stakwork/sphinx-tribes/db" "github.com/stakwork/sphinx-tribes/handlers" ) func PeopleRoutes() chi.Router { r := chi.NewRouter() + bountyHandler := handlers.NewBountyHandler(http.DefaultClient, db.DB) r.Group(func(r chi.Router) { r.Get("/", handlers.GetListedPeople) r.Get("/search", handlers.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) From b4e68b87c089efe7195956588678385e1f5bde0a Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Wed, 21 Feb 2024 01:16:10 +0530 Subject: [PATCH 02/21] Revert "pushed test for Backend [Integration Test] bounty.go handlers GetPersonCreatedBounties, GetPersonAssignedBounties, & GetBountyByCreated #1509" This reverts commit 3b633fe5269590bb7fb4a6f55819be935d5f49aa. --- handlers/bounty.go | 36 ++++----- handlers/bounty_test.go | 161 -------------------------------------- handlers/organizations.go | 84 -------------------- routes/bounty.go | 6 +- routes/people.go | 8 +- 5 files changed, 23 insertions(+), 272 deletions(-) diff --git a/handlers/bounty.go b/handlers/bounty.go index 9d7ade72d..3019313fb 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -32,23 +32,23 @@ func NewBountyHandler(httpClient HttpClient, db db.Database) *bountyHandler { func (h *bountyHandler) GetAllBounties(w http.ResponseWriter, r *http.Request) { bounties := h.db.GetAllBounties(r) - var bountyResponse []db.BountyResponse = h.GenerateBountyResponse(bounties) + var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(bountyResponse) } -func (h *bountyHandler) GetBountyById(w http.ResponseWriter, r *http.Request) { +func GetBountyById(w http.ResponseWriter, r *http.Request) { bountyId := chi.URLParam(r, "bountyId") if bountyId == "" { w.WriteHeader(http.StatusNotFound) } - bounties, err := h.db.GetBountyById(bountyId) + bounties, err := db.DB.GetBountyById(bountyId) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Println("Error", err) } else { - var bountyResponse []db.BountyResponse = h.GenerateBountyResponse(bounties) + var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(bountyResponse) } @@ -98,7 +98,7 @@ func GetOrganizationPreviousBountyByCreated(w http.ResponseWriter, r *http.Reque } } -func (h *bountyHandler) GetBountyIndexById(w http.ResponseWriter, r *http.Request) { +func GetBountyIndexById(w http.ResponseWriter, r *http.Request) { bountyId := chi.URLParam(r, "bountyId") if bountyId == "" { w.WriteHeader(http.StatusNotFound) @@ -108,17 +108,17 @@ func (h *bountyHandler) GetBountyIndexById(w http.ResponseWriter, r *http.Reques json.NewEncoder(w).Encode(bountyIndex) } -func (h *bountyHandler) GetBountyByCreated(w http.ResponseWriter, r *http.Request) { +func GetBountyByCreated(w http.ResponseWriter, r *http.Request) { created := chi.URLParam(r, "created") if created == "" { w.WriteHeader(http.StatusNotFound) } - bounties, err := h.db.GetBountyDataByCreated(created) + bounties, err := db.DB.GetBountyDataByCreated(created) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Println("Error", err) } else { - var bountyResponse []db.BountyResponse = h.GenerateBountyResponse(bounties) + var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(bountyResponse) } @@ -143,25 +143,25 @@ func GetBountyCount(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(bountyCount) } -func (h *bountyHandler) GetPersonCreatedBounties(w http.ResponseWriter, r *http.Request) { - bounties, err := h.db.GetCreatedBounties(r) +func GetPersonCreatedBounties(w http.ResponseWriter, r *http.Request) { + bounties, err := db.DB.GetCreatedBounties(r) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Println("Error", err) } else { - var bountyResponse []db.BountyResponse = h.GenerateBountyResponse(bounties) + var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(bountyResponse) } } -func (h *bountyHandler) GetPersonAssignedBounties(w http.ResponseWriter, r *http.Request) { - bounties, err := h.db.GetAssignedBounties(r) +func GetPersonAssignedBounties(w http.ResponseWriter, r *http.Request) { + bounties, err := db.DB.GetAssignedBounties(r) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Println("Error", err) } else { - var bountyResponse []db.BountyResponse = h.GenerateBountyResponse(bounties) + var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(bountyResponse) } @@ -317,15 +317,15 @@ func UpdatePaymentStatus(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(bounty) } -func (h *bountyHandler) GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse { +func GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse { var bountyResponse []db.BountyResponse for i := 0; i < len(bounties); i++ { bounty := bounties[i] - owner := h.db.GetPersonByPubkey(bounty.OwnerID) - assignee := h.db.GetPersonByPubkey(bounty.Assignee) - organization := h.db.GetOrganizationByUuid(bounty.OrgUuid) + owner := db.DB.GetPersonByPubkey(bounty.OwnerID) + assignee := db.DB.GetPersonByPubkey(bounty.Assignee) + organization := db.DB.GetOrganizationByUuid(bounty.OrgUuid) b := db.BountyResponse{ Bounty: db.Bounty{ diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index d848d1a0b..d1f87a632 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -9,13 +9,10 @@ import ( "io" "net/http" "net/http/httptest" - "strconv" "strings" "testing" - "time" "github.com/go-chi/chi" - "github.com/lib/pq" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/config" "github.com/stakwork/sphinx-tribes/db" @@ -452,161 +449,3 @@ func TestDeleteBounty(t *testing.T) { mockDb.AssertExpectations(t) }) } - -// #1509 -func TestGetBountyByCreated(t *testing.T) { - mockDb := dbMocks.NewDatabase(t) - mockHttpClient := mocks.NewHttpClient(t) - bHandler := NewBountyHandler(mockHttpClient, mockDb) - - t.Run("Should return if bounty is present in db", func(t *testing.T) { - rr := httptest.NewRecorder() - handler := http.HandlerFunc(bHandler.GetBountyByCreated) - bounty := db.Bounty{ - ID: 1, - Type: "coding", - Title: "first bounty", - Description: "first bounty description", - OrgUuid: "org-1", - Assignee: "user1", - Created: 1707991475, - OwnerID: "owner-1", - } - createdStr := strconv.FormatInt(bounty.Created, 10) - - rctx := chi.NewRouteContext() - rctx.URLParams.Add("created", "1707991475") - req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "gobounties/created/1707991475", nil) - mockDb.On("GetBountyDataByCreated", createdStr).Return([]db.Bounty{bounty}, nil).Once() - mockDb.On("GetPersonByPubkey", "owner-1").Return(db.Person{}).Once() - mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}).Once() - mockDb.On("GetOrganizationByUuid", "org-1").Return(db.Organization{}).Once() - handler.ServeHTTP(rr, req) - - var returnedBounty []db.BountyResponse - err := json.Unmarshal(rr.Body.Bytes(), &returnedBounty) - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, rr.Code) - assert.NotEmpty(t, returnedBounty) - - }) -} - -func TestGetPersonAssignedBounties(t *testing.T) { - mockDb := dbMocks.NewDatabase(t) - mockHttpClient := mocks.NewHttpClient(t) - bHandler := NewBountyHandler(mockHttpClient, mockDb) - - t.Run("Should successfull Get Person Assigned Bounties", func(t *testing.T) { - rr := httptest.NewRecorder() - handler := http.HandlerFunc(bHandler.GetPersonAssignedBounties) - bounty := db.Bounty{ - ID: 1, - Type: "coding", - Title: "first bounty", - Description: "first bounty description", - OrgUuid: "org-1", - Assignee: "user1", - Created: 1707991475, - OwnerID: "owner-1", - } - - rctx := chi.NewRouteContext() - rctx.URLParams.Add("uuid", "clu80datu2rjujsmim40") - rctx.URLParams.Add("sortBy", "paid") - rctx.URLParams.Add("page", "1") - rctx.URLParams.Add("limit", "20") - rctx.URLParams.Add("search", "") - req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "people/wanteds/assigned/clu80datu2rjujsmim40?sortBy=paid&page=1&limit=20&search=", nil) - - mockDb.On("GetAssignedBounties", req).Return([]db.Bounty{bounty}, nil).Once() - mockDb.On("GetPersonByPubkey", "owner-1").Return(db.Person{}, nil).Once() - mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}, nil).Once() - mockDb.On("GetOrganizationByUuid", "org-1").Return(db.Organization{}, nil).Once() - handler.ServeHTTP(rr, req) - - var returnedBounty []db.BountyResponse - err := json.Unmarshal(rr.Body.Bytes(), &returnedBounty) - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, rr.Code) - assert.NotEmpty(t, returnedBounty) - }) -} - -// #1526 -func TestGetBountyById(t *testing.T) { - mockDb := dbMocks.NewDatabase(t) - mockHttpClient := mocks.NewHttpClient(t) - bHandler := NewBountyHandler(mockHttpClient, mockDb) - - t.Run("successful retrieval of bounty by ID", func(t *testing.T) { - rr := httptest.NewRecorder() - handler := http.HandlerFunc(bHandler.GetBountyById) - - bounty := db.Bounty{ - ID: 1, - OwnerID: "owner123", - Paid: false, - Show: true, - Type: "bug fix", - Award: "500", - AssignedHours: 10, - BountyExpires: "2023-12-31", - CommitmentFee: 1000, - Price: 500, - Title: "Fix critical bug in payment system", - Tribe: "development", - Assignee: "user1", - TicketUrl: "http://example.com/issues/1", - OrgUuid: "org-789", - Description: "This bounty is for fixing a critical bug in the payment system that causes transactions to fail under certain conditions.", - WantedType: "immediate", - Deliverables: "A pull request with a fix, including tests", - GithubDescription: true, - OneSentenceSummary: "Fix a critical payment system bug", - EstimatedSessionLength: "2 hours", - EstimatedCompletionDate: "2023-10-01", - Created: time.Now().Unix(), - Updated: nil, - AssignedDate: nil, - CompletionDate: nil, - MarkAsPaidDate: nil, - PaidDate: nil, - CodingLanguages: pq.StringArray{"Go", "Python"}, - } - - rctx := chi.NewRouteContext() - rctx.URLParams.Add("bountyId", strconv.Itoa(int(bounty.ID))) - req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bounty/1", nil) - assert.NoError(t, err) - - mockDb.On("GetBountyById", mock.Anything).Return([]db.Bounty{bounty}, nil).Once() - mockDb.On("GetPersonByPubkey", "owner123").Return(db.Person{}).Once() - mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}).Once() - mockDb.On("GetOrganizationByUuid", "org-789").Return(db.Organization{}).Once() - - handler.ServeHTTP(rr, req) - - var returnedBounty []db.BountyResponse - err = json.Unmarshal(rr.Body.Bytes(), &returnedBounty) - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, rr.Code) - mockDb.AssertExpectations(t) - }) - - t.Run("bounty not found", func(t *testing.T) { - rr := httptest.NewRecorder() - handler := http.HandlerFunc(bHandler.GetBountyById) - - rctx := chi.NewRouteContext() - rctx.URLParams.Add("bountyId", "999") - req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bounty/999", nil) - assert.NoError(t, err) - - mockDb.On("GetBountyById", "999").Return(nil, errors.New("not-found")).Once() - handler.ServeHTTP(rr, req) - - assert.Equal(t, http.StatusBadRequest, rr.Code) - mockDb.AssertExpectations(t) - }) -} diff --git a/handlers/organizations.go b/handlers/organizations.go index 0f8a55211..d88bd5897 100644 --- a/handlers/organizations.go +++ b/handlers/organizations.go @@ -709,87 +709,3 @@ func (oh *organizationHandler) DeleteOrganization(w http.ResponseWriter, r *http w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(org) } - -func GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse { - var bountyResponse []db.BountyResponse - - for i := 0; i < len(bounties); i++ { - bounty := bounties[i] - - owner := db.DB.GetPersonByPubkey(bounty.OwnerID) - assignee := db.DB.GetPersonByPubkey(bounty.Assignee) - organization := db.DB.GetOrganizationByUuid(bounty.OrgUuid) - - b := db.BountyResponse{ - Bounty: db.Bounty{ - ID: bounty.ID, - OwnerID: bounty.OwnerID, - Paid: bounty.Paid, - Show: bounty.Show, - Type: bounty.Type, - Award: bounty.Award, - AssignedHours: bounty.AssignedHours, - BountyExpires: bounty.BountyExpires, - CommitmentFee: bounty.CommitmentFee, - Price: bounty.Price, - Title: bounty.Title, - Tribe: bounty.Tribe, - Created: bounty.Created, - Assignee: bounty.Assignee, - TicketUrl: bounty.TicketUrl, - Description: bounty.Description, - WantedType: bounty.WantedType, - Deliverables: bounty.Deliverables, - GithubDescription: bounty.GithubDescription, - OneSentenceSummary: bounty.OneSentenceSummary, - EstimatedSessionLength: bounty.EstimatedSessionLength, - EstimatedCompletionDate: bounty.EstimatedCompletionDate, - OrgUuid: bounty.OrgUuid, - Updated: bounty.Updated, - CodingLanguages: bounty.CodingLanguages, - }, - Assignee: db.Person{ - ID: assignee.ID, - Uuid: assignee.Uuid, - OwnerPubKey: assignee.OwnerPubKey, - OwnerAlias: assignee.OwnerAlias, - UniqueName: assignee.UniqueName, - Description: assignee.Description, - Tags: assignee.Tags, - Img: assignee.Img, - Created: assignee.Created, - Updated: assignee.Updated, - LastLogin: assignee.LastLogin, - OwnerRouteHint: assignee.OwnerRouteHint, - OwnerContactKey: assignee.OwnerContactKey, - PriceToMeet: assignee.PriceToMeet, - TwitterConfirmed: assignee.TwitterConfirmed, - }, - Owner: db.Person{ - ID: owner.ID, - Uuid: owner.Uuid, - OwnerPubKey: owner.OwnerPubKey, - OwnerAlias: owner.OwnerAlias, - UniqueName: owner.UniqueName, - Description: owner.Description, - Tags: owner.Tags, - Img: owner.Img, - Created: owner.Created, - Updated: owner.Updated, - LastLogin: owner.LastLogin, - OwnerRouteHint: owner.OwnerRouteHint, - OwnerContactKey: owner.OwnerContactKey, - PriceToMeet: owner.PriceToMeet, - TwitterConfirmed: owner.TwitterConfirmed, - }, - Organization: db.OrganizationShort{ - Name: organization.Name, - Uuid: organization.Uuid, - Img: organization.Img, - }, - } - bountyResponse = append(bountyResponse, b) - } - - return bountyResponse -} diff --git a/routes/bounty.go b/routes/bounty.go index ae5a15464..a169c04ee 100644 --- a/routes/bounty.go +++ b/routes/bounty.go @@ -14,13 +14,13 @@ func BountyRoutes() chi.Router { bountyHandler := handlers.NewBountyHandler(http.DefaultClient, db.DB) r.Group(func(r chi.Router) { r.Get("/all", bountyHandler.GetAllBounties) - r.Get("/id/{bountyId}", bountyHandler.GetBountyById) - r.Get("/index/{bountyId}", bountyHandler.GetBountyIndexById) + r.Get("/id/{bountyId}", handlers.GetBountyById) + r.Get("/index/{bountyId}", handlers.GetBountyIndexById) r.Get("/next/{created}", handlers.GetNextBountyByCreated) r.Get("/previous/{created}", handlers.GetPreviousBountyByCreated) r.Get("/org/next/{uuid}/{created}", handlers.GetOrganizationNextBountyByCreated) r.Get("/org/previous/{uuid}/{created}", handlers.GetOrganizationPreviousBountyByCreated) - r.Get("/created/{created}", bountyHandler.GetBountyByCreated) + r.Get("/created/{created}", handlers.GetBountyByCreated) r.Get("/count/{personKey}/{tabType}", handlers.GetUserBountyCount) r.Get("/count", handlers.GetBountyCount) r.Get("/invoice/{paymentRequest}", handlers.GetInvoiceData) diff --git a/routes/people.go b/routes/people.go index 19ced0160..2ed0bfa85 100644 --- a/routes/people.go +++ b/routes/people.go @@ -1,22 +1,18 @@ package routes import ( - "net/http" - "github.com/go-chi/chi" - "github.com/stakwork/sphinx-tribes/db" "github.com/stakwork/sphinx-tribes/handlers" ) func PeopleRoutes() chi.Router { r := chi.NewRouter() - bountyHandler := handlers.NewBountyHandler(http.DefaultClient, db.DB) r.Group(func(r chi.Router) { r.Get("/", handlers.GetListedPeople) r.Get("/search", handlers.GetPeopleBySearch) r.Get("/posts", handlers.GetListedPosts) - r.Get("/wanteds/assigned/{uuid}", bountyHandler.GetPersonAssignedBounties) - r.Get("/wanteds/created/{uuid}", bountyHandler.GetPersonCreatedBounties) + r.Get("/wanteds/assigned/{uuid}", handlers.GetPersonAssignedBounties) + r.Get("/wanteds/created/{uuid}", handlers.GetPersonCreatedBounties) r.Get("/wanteds/header", handlers.GetWantedsHeader) r.Get("/short", handlers.GetPeopleShortList) r.Get("/offers", handlers.GetListedOffers) From 43a44edb77003bf7fe00745d09ecef23405fdaf5 Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Wed, 21 Feb 2024 01:28:17 +0530 Subject: [PATCH 03/21] Added tests for GetPersonAssignedBounties, & GetBountyByCreated --- handlers/bounty.go | 38 ++++----- handlers/bounty_test.go | 161 ++++++++++++++++++++++++++++++++++++++ handlers/organizations.go | 84 ++++++++++++++++++++ routes/bounty.go | 6 +- routes/people.go | 8 +- 5 files changed, 273 insertions(+), 24 deletions(-) diff --git a/handlers/bounty.go b/handlers/bounty.go index 3019313fb..33a90fb51 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -32,23 +32,23 @@ func NewBountyHandler(httpClient HttpClient, db db.Database) *bountyHandler { func (h *bountyHandler) GetAllBounties(w http.ResponseWriter, r *http.Request) { bounties := h.db.GetAllBounties(r) - var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties) + var bountyResponse []db.BountyResponse = h.GenerateBountyResponse(bounties) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(bountyResponse) } -func GetBountyById(w http.ResponseWriter, r *http.Request) { +func (h *bountyHandler) GetBountyById(w http.ResponseWriter, r *http.Request) { bountyId := chi.URLParam(r, "bountyId") if bountyId == "" { w.WriteHeader(http.StatusNotFound) } - bounties, err := db.DB.GetBountyById(bountyId) + bounties, err := h.db.GetBountyById(bountyId) 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) } @@ -98,27 +98,27 @@ func GetOrganizationPreviousBountyByCreated(w http.ResponseWriter, r *http.Reque } } -func GetBountyIndexById(w http.ResponseWriter, r *http.Request) { +func (h *bountyHandler) GetBountyIndexById(w http.ResponseWriter, r *http.Request) { bountyId := chi.URLParam(r, "bountyId") if bountyId == "" { w.WriteHeader(http.StatusNotFound) } - bountyIndex := db.DB.GetBountyIndexById(bountyId) + bountyIndex := h.db.GetBountyIndexById(bountyId) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(bountyIndex) } -func GetBountyByCreated(w http.ResponseWriter, r *http.Request) { +func (h *bountyHandler) GetBountyByCreated(w http.ResponseWriter, r *http.Request) { created := chi.URLParam(r, "created") if created == "" { w.WriteHeader(http.StatusNotFound) } - bounties, err := db.DB.GetBountyDataByCreated(created) + bounties, err := h.db.GetBountyDataByCreated(created) 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) } @@ -143,25 +143,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) } @@ -317,15 +317,15 @@ func UpdatePaymentStatus(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(bounty) } -func GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse { +func (h *bountyHandler) GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse { var bountyResponse []db.BountyResponse for i := 0; i < len(bounties); i++ { bounty := bounties[i] - owner := db.DB.GetPersonByPubkey(bounty.OwnerID) - assignee := db.DB.GetPersonByPubkey(bounty.Assignee) - organization := db.DB.GetOrganizationByUuid(bounty.OrgUuid) + owner := h.db.GetPersonByPubkey(bounty.OwnerID) + assignee := h.db.GetPersonByPubkey(bounty.Assignee) + organization := h.db.GetOrganizationByUuid(bounty.OrgUuid) b := db.BountyResponse{ Bounty: db.Bounty{ diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index d1f87a632..d848d1a0b 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -9,10 +9,13 @@ import ( "io" "net/http" "net/http/httptest" + "strconv" "strings" "testing" + "time" "github.com/go-chi/chi" + "github.com/lib/pq" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/config" "github.com/stakwork/sphinx-tribes/db" @@ -449,3 +452,161 @@ func TestDeleteBounty(t *testing.T) { mockDb.AssertExpectations(t) }) } + +// #1509 +func TestGetBountyByCreated(t *testing.T) { + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("Should return if bounty is present in db", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetBountyByCreated) + bounty := db.Bounty{ + ID: 1, + Type: "coding", + Title: "first bounty", + Description: "first bounty description", + OrgUuid: "org-1", + Assignee: "user1", + Created: 1707991475, + OwnerID: "owner-1", + } + createdStr := strconv.FormatInt(bounty.Created, 10) + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("created", "1707991475") + req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "gobounties/created/1707991475", nil) + mockDb.On("GetBountyDataByCreated", createdStr).Return([]db.Bounty{bounty}, nil).Once() + mockDb.On("GetPersonByPubkey", "owner-1").Return(db.Person{}).Once() + mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}).Once() + mockDb.On("GetOrganizationByUuid", "org-1").Return(db.Organization{}).Once() + handler.ServeHTTP(rr, req) + + var returnedBounty []db.BountyResponse + err := json.Unmarshal(rr.Body.Bytes(), &returnedBounty) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.NotEmpty(t, returnedBounty) + + }) +} + +func TestGetPersonAssignedBounties(t *testing.T) { + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("Should successfull Get Person Assigned Bounties", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetPersonAssignedBounties) + bounty := db.Bounty{ + ID: 1, + Type: "coding", + Title: "first bounty", + Description: "first bounty description", + OrgUuid: "org-1", + Assignee: "user1", + Created: 1707991475, + OwnerID: "owner-1", + } + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("uuid", "clu80datu2rjujsmim40") + rctx.URLParams.Add("sortBy", "paid") + rctx.URLParams.Add("page", "1") + rctx.URLParams.Add("limit", "20") + rctx.URLParams.Add("search", "") + req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "people/wanteds/assigned/clu80datu2rjujsmim40?sortBy=paid&page=1&limit=20&search=", nil) + + mockDb.On("GetAssignedBounties", req).Return([]db.Bounty{bounty}, nil).Once() + mockDb.On("GetPersonByPubkey", "owner-1").Return(db.Person{}, nil).Once() + mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}, nil).Once() + mockDb.On("GetOrganizationByUuid", "org-1").Return(db.Organization{}, nil).Once() + handler.ServeHTTP(rr, req) + + var returnedBounty []db.BountyResponse + err := json.Unmarshal(rr.Body.Bytes(), &returnedBounty) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.NotEmpty(t, returnedBounty) + }) +} + +// #1526 +func TestGetBountyById(t *testing.T) { + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("successful retrieval of bounty by ID", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetBountyById) + + bounty := db.Bounty{ + ID: 1, + OwnerID: "owner123", + Paid: false, + Show: true, + Type: "bug fix", + Award: "500", + AssignedHours: 10, + BountyExpires: "2023-12-31", + CommitmentFee: 1000, + Price: 500, + Title: "Fix critical bug in payment system", + Tribe: "development", + Assignee: "user1", + TicketUrl: "http://example.com/issues/1", + OrgUuid: "org-789", + Description: "This bounty is for fixing a critical bug in the payment system that causes transactions to fail under certain conditions.", + WantedType: "immediate", + Deliverables: "A pull request with a fix, including tests", + GithubDescription: true, + OneSentenceSummary: "Fix a critical payment system bug", + EstimatedSessionLength: "2 hours", + EstimatedCompletionDate: "2023-10-01", + Created: time.Now().Unix(), + Updated: nil, + AssignedDate: nil, + CompletionDate: nil, + MarkAsPaidDate: nil, + PaidDate: nil, + CodingLanguages: pq.StringArray{"Go", "Python"}, + } + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("bountyId", strconv.Itoa(int(bounty.ID))) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bounty/1", nil) + assert.NoError(t, err) + + mockDb.On("GetBountyById", mock.Anything).Return([]db.Bounty{bounty}, nil).Once() + mockDb.On("GetPersonByPubkey", "owner123").Return(db.Person{}).Once() + mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}).Once() + mockDb.On("GetOrganizationByUuid", "org-789").Return(db.Organization{}).Once() + + handler.ServeHTTP(rr, req) + + var returnedBounty []db.BountyResponse + err = json.Unmarshal(rr.Body.Bytes(), &returnedBounty) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + mockDb.AssertExpectations(t) + }) + + t.Run("bounty not found", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetBountyById) + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("bountyId", "999") + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bounty/999", nil) + assert.NoError(t, err) + + mockDb.On("GetBountyById", "999").Return(nil, errors.New("not-found")).Once() + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusBadRequest, rr.Code) + mockDb.AssertExpectations(t) + }) +} diff --git a/handlers/organizations.go b/handlers/organizations.go index d88bd5897..0f8a55211 100644 --- a/handlers/organizations.go +++ b/handlers/organizations.go @@ -709,3 +709,87 @@ func (oh *organizationHandler) DeleteOrganization(w http.ResponseWriter, r *http w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(org) } + +func GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse { + var bountyResponse []db.BountyResponse + + for i := 0; i < len(bounties); i++ { + bounty := bounties[i] + + owner := db.DB.GetPersonByPubkey(bounty.OwnerID) + assignee := db.DB.GetPersonByPubkey(bounty.Assignee) + organization := db.DB.GetOrganizationByUuid(bounty.OrgUuid) + + b := db.BountyResponse{ + Bounty: db.Bounty{ + ID: bounty.ID, + OwnerID: bounty.OwnerID, + Paid: bounty.Paid, + Show: bounty.Show, + Type: bounty.Type, + Award: bounty.Award, + AssignedHours: bounty.AssignedHours, + BountyExpires: bounty.BountyExpires, + CommitmentFee: bounty.CommitmentFee, + Price: bounty.Price, + Title: bounty.Title, + Tribe: bounty.Tribe, + Created: bounty.Created, + Assignee: bounty.Assignee, + TicketUrl: bounty.TicketUrl, + Description: bounty.Description, + WantedType: bounty.WantedType, + Deliverables: bounty.Deliverables, + GithubDescription: bounty.GithubDescription, + OneSentenceSummary: bounty.OneSentenceSummary, + EstimatedSessionLength: bounty.EstimatedSessionLength, + EstimatedCompletionDate: bounty.EstimatedCompletionDate, + OrgUuid: bounty.OrgUuid, + Updated: bounty.Updated, + CodingLanguages: bounty.CodingLanguages, + }, + Assignee: db.Person{ + ID: assignee.ID, + Uuid: assignee.Uuid, + OwnerPubKey: assignee.OwnerPubKey, + OwnerAlias: assignee.OwnerAlias, + UniqueName: assignee.UniqueName, + Description: assignee.Description, + Tags: assignee.Tags, + Img: assignee.Img, + Created: assignee.Created, + Updated: assignee.Updated, + LastLogin: assignee.LastLogin, + OwnerRouteHint: assignee.OwnerRouteHint, + OwnerContactKey: assignee.OwnerContactKey, + PriceToMeet: assignee.PriceToMeet, + TwitterConfirmed: assignee.TwitterConfirmed, + }, + Owner: db.Person{ + ID: owner.ID, + Uuid: owner.Uuid, + OwnerPubKey: owner.OwnerPubKey, + OwnerAlias: owner.OwnerAlias, + UniqueName: owner.UniqueName, + Description: owner.Description, + Tags: owner.Tags, + Img: owner.Img, + Created: owner.Created, + Updated: owner.Updated, + LastLogin: owner.LastLogin, + OwnerRouteHint: owner.OwnerRouteHint, + OwnerContactKey: owner.OwnerContactKey, + PriceToMeet: owner.PriceToMeet, + TwitterConfirmed: owner.TwitterConfirmed, + }, + Organization: db.OrganizationShort{ + Name: organization.Name, + Uuid: organization.Uuid, + Img: organization.Img, + }, + } + bountyResponse = append(bountyResponse, b) + } + + return bountyResponse +} diff --git a/routes/bounty.go b/routes/bounty.go index a169c04ee..ae5a15464 100644 --- a/routes/bounty.go +++ b/routes/bounty.go @@ -14,13 +14,13 @@ func BountyRoutes() chi.Router { bountyHandler := handlers.NewBountyHandler(http.DefaultClient, db.DB) r.Group(func(r chi.Router) { r.Get("/all", bountyHandler.GetAllBounties) - r.Get("/id/{bountyId}", handlers.GetBountyById) - r.Get("/index/{bountyId}", handlers.GetBountyIndexById) + r.Get("/id/{bountyId}", bountyHandler.GetBountyById) + r.Get("/index/{bountyId}", bountyHandler.GetBountyIndexById) r.Get("/next/{created}", handlers.GetNextBountyByCreated) r.Get("/previous/{created}", handlers.GetPreviousBountyByCreated) r.Get("/org/next/{uuid}/{created}", handlers.GetOrganizationNextBountyByCreated) r.Get("/org/previous/{uuid}/{created}", handlers.GetOrganizationPreviousBountyByCreated) - r.Get("/created/{created}", handlers.GetBountyByCreated) + r.Get("/created/{created}", bountyHandler.GetBountyByCreated) r.Get("/count/{personKey}/{tabType}", handlers.GetUserBountyCount) r.Get("/count", handlers.GetBountyCount) r.Get("/invoice/{paymentRequest}", handlers.GetInvoiceData) diff --git a/routes/people.go b/routes/people.go index 2ed0bfa85..19ced0160 100644 --- a/routes/people.go +++ b/routes/people.go @@ -1,18 +1,22 @@ package routes import ( + "net/http" + "github.com/go-chi/chi" + "github.com/stakwork/sphinx-tribes/db" "github.com/stakwork/sphinx-tribes/handlers" ) func PeopleRoutes() chi.Router { r := chi.NewRouter() + bountyHandler := handlers.NewBountyHandler(http.DefaultClient, db.DB) r.Group(func(r chi.Router) { r.Get("/", handlers.GetListedPeople) r.Get("/search", handlers.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) From 38220308d2f7f31cee5f4c5583d55a5397c7b2dd Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Wed, 21 Feb 2024 01:47:23 +0530 Subject: [PATCH 04/21] fixed duplicate test --- handlers/bounty_test.go | 47 ++++------------------------------------- 1 file changed, 4 insertions(+), 43 deletions(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 6720c0f73..bfe3300d1 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -12,10 +12,8 @@ import ( "strconv" "strings" "testing" - "time" "github.com/go-chi/chi" - "github.com/lib/pq" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/config" "github.com/stakwork/sphinx-tribes/db" @@ -453,7 +451,6 @@ func TestDeleteBounty(t *testing.T) { }) } - func TestGetBountyByCreated(t *testing.T) { mockDb := dbMocks.NewDatabase(t) mockHttpClient := mocks.NewHttpClient(t) @@ -493,7 +490,10 @@ func TestGetBountyByCreated(t *testing.T) { } func TestGetPersonAssignedBounties(t *testing.T) { - t.Run("Should successfull Get Person Assigned Bounties", func(t *testing.T) { + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + t.Run("Should successfull Get Person Assigned Bounties", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(bHandler.GetPersonAssignedBounties) bounty := db.Bounty{ @@ -529,44 +529,6 @@ func TestGetPersonAssignedBounties(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) { - bHandler.generateBountyResponse = mockGenerateBountyResponse - - expectedBounty := []db.Bounty{{ - ID: 1, - Type: "type1", - Title: "Test Bounty", - Description: "Description", - Created: 123456789, - }} - mockDb.On("GetBountyDataByCreated", "123456789").Return(expectedBounty, nil).Once() - - rr := httptest.NewRecorder() - handler := http.HandlerFunc(bHandler.GetBountyByCreated) - - req, err := http.NewRequestWithContext(ctx, "GET", "/bounty/123456789", nil) - if err != nil { - t.Fatal(err) - } - chiCtx := chi.NewRouteContext() - chiCtx.URLParams.Add("created", "123456789") - req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, chiCtx)) - - handler.ServeHTTP(rr, req) - - assert.Equal(t, http.StatusOK, rr.Code) - }) -} - func TestGetNextBountyByCreated(t *testing.T) { ctx := context.Background() @@ -574,7 +536,6 @@ func TestGetNextBountyByCreated(t *testing.T) { mockHttpClient := mocks.NewHttpClient(t) bHandler := NewBountyHandler(mockHttpClient, mockDb) - t.Run("Should test that the next bounty on the bounties homepage can be gotten by its created value and the selected filters", func(t *testing.T) { mockDb.On("GetNextBountyByCreated", mock.Anything).Return(uint(1), nil).Once() From 19a9624c11855bf36b2fde7ec1e355f8b2402f03 Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Wed, 21 Feb 2024 01:58:21 +0530 Subject: [PATCH 05/21] added TestGetPersonCreatedBounties --- handlers/bounty_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index bfe3300d1..d6dec8f81 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -529,6 +529,46 @@ func TestGetPersonAssignedBounties(t *testing.T) { }) } +func TestGetPersonCreatedBounties(t *testing.T) { + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + t.Run("Should successfull Get Person Created Bounties", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetPersonCreatedBounties) + bounty := db.Bounty{ + ID: 1, + Type: "coding", + Title: "first bounty", + Description: "first bounty description", + OrgUuid: "org-1", + Assignee: "user1", + Created: 1707991475, + OwnerID: "owner-1", + } + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("uuid", "clu80datu2rjujsmim40") + rctx.URLParams.Add("sortBy", "paid") + rctx.URLParams.Add("page", "1") + rctx.URLParams.Add("limit", "20") + rctx.URLParams.Add("search", "") + req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "people/wanteds/created/clu80datu2rjujsmim40?sortBy=paid&page=1&limit=20&search=", nil) + + mockDb.On("GetCreatedBounties", req).Return([]db.Bounty{bounty}, nil).Once() + mockDb.On("GetPersonByPubkey", "owner-1").Return(db.Person{}, nil).Once() + mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}, nil).Once() + mockDb.On("GetOrganizationByUuid", "org-1").Return(db.Organization{}, nil).Once() + handler.ServeHTTP(rr, req) + + var returnedBounty []db.BountyResponse + err := json.Unmarshal(rr.Body.Bytes(), &returnedBounty) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.NotEmpty(t, returnedBounty) + }) +} + func TestGetNextBountyByCreated(t *testing.T) { ctx := context.Background() From b47846dd6fea136642c3b7ce9e8843f79648b536 Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Wed, 21 Feb 2024 02:12:14 +0530 Subject: [PATCH 06/21] initial commit --- handlers/bounty_test.go | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index d6dec8f81..12cfacffc 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -12,8 +12,10 @@ import ( "strconv" "strings" "testing" + "time" "github.com/go-chi/chi" + "github.com/lib/pq" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/config" "github.com/stakwork/sphinx-tribes/db" @@ -647,3 +649,80 @@ func TestGetOrganizationPreviousBountyByCreated(t *testing.T) { mockDb.AssertExpectations(t) }) } + +func TestGetBountyById(t *testing.T) { + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("successful retrieval of bounty by ID", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetBountyById) + + bounty := db.Bounty{ + ID: 1, + OwnerID: "owner123", + Paid: false, + Show: true, + Type: "bug fix", + Award: "500", + AssignedHours: 10, + BountyExpires: "2023-12-31", + CommitmentFee: 1000, + Price: 500, + Title: "Fix critical bug in payment system", + Tribe: "development", + Assignee: "user1", + TicketUrl: "http://example.com/issues/1", + OrgUuid: "org-789", + Description: "This bounty is for fixing a critical bug in the payment system that causes transactions to fail under certain conditions.", + WantedType: "immediate", + Deliverables: "A pull request with a fix, including tests", + GithubDescription: true, + OneSentenceSummary: "Fix a critical payment system bug", + EstimatedSessionLength: "2 hours", + EstimatedCompletionDate: "2023-10-01", + Created: time.Now().Unix(), + Updated: nil, + AssignedDate: nil, + CompletionDate: nil, + MarkAsPaidDate: nil, + PaidDate: nil, + CodingLanguages: pq.StringArray{"Go", "Python"}, + } + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("bountyId", strconv.Itoa(int(bounty.ID))) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bounty/1", nil) + assert.NoError(t, err) + + mockDb.On("GetBountyById", mock.Anything).Return([]db.Bounty{bounty}, nil).Once() + mockDb.On("GetPersonByPubkey", "owner123").Return(db.Person{}).Once() + mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}).Once() + mockDb.On("GetOrganizationByUuid", "org-789").Return(db.Organization{}).Once() + + handler.ServeHTTP(rr, req) + + var returnedBounty []db.BountyResponse + err = json.Unmarshal(rr.Body.Bytes(), &returnedBounty) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + mockDb.AssertExpectations(t) + }) + + t.Run("bounty not found", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetBountyById) + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("bountyId", "999") + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bounty/999", nil) + assert.NoError(t, err) + + mockDb.On("GetBountyById", "999").Return(nil, errors.New("not-found")).Once() + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusBadRequest, rr.Code) + mockDb.AssertExpectations(t) + }) +} From 78c7f3b9e21476c8cb3920a6098e4f4d8050c680 Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Wed, 21 Feb 2024 04:33:19 +0530 Subject: [PATCH 07/21] routes --- routes/bounty.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/bounty.go b/routes/bounty.go index 74fbba8b7..5101515d6 100644 --- a/routes/bounty.go +++ b/routes/bounty.go @@ -15,8 +15,8 @@ func BountyRoutes() chi.Router { r.Group(func(r chi.Router) { r.Get("/all", bountyHandler.GetAllBounties) - r.Get("/id/{bountyId}", handlers.GetBountyById) - r.Get("/index/{bountyId}", handlers.GetBountyIndexById) + r.Get("/id/{bountyId}", bountyHandler.GetBountyById) + r.Get("/index/{bountyId}", bountyHandler.GetBountyIndexById) r.Get("/next/{created}", bountyHandler.GetNextBountyByCreated) r.Get("/previous/{created}", bountyHandler.GetPreviousBountyByCreated) r.Get("/org/next/{uuid}/{created}", bountyHandler.GetOrganizationNextBountyByCreated) From 372491a124abb271b3faf50ff345700449de38f7 Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Wed, 21 Feb 2024 04:49:55 +0530 Subject: [PATCH 08/21] TestGetAllBounties --- handlers/bounty_test.go | 59 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 12cfacffc..c89668ad7 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -726,3 +726,62 @@ func TestGetBountyById(t *testing.T) { mockDb.AssertExpectations(t) }) } + +func TestGetAllBounties(t *testing.T) { + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("successful retrieval of bounties", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetAllBounties) + bounty := db.Bounty{ + ID: 1, + OwnerID: "owner123", + Paid: false, + Show: true, + Type: "bug fix", + Award: "500", + AssignedHours: 10, + BountyExpires: "2023-12-31", + CommitmentFee: 1000, + Price: 500, + Title: "Fix critical bug in payment system", + Tribe: "development", + Assignee: "user1", + TicketUrl: "http://example.com/issues/1", + OrgUuid: "org-789", + Description: "This bounty is for fixing a critical bug in the payment system that causes transactions to fail under certain conditions.", + WantedType: "immediate", + Deliverables: "A pull request with a fix, including tests", + GithubDescription: true, + OneSentenceSummary: "Fix a critical payment system bug", + EstimatedSessionLength: "2 hours", + EstimatedCompletionDate: "2023-10-01", + Created: time.Now().Unix(), + Updated: nil, + AssignedDate: nil, + CompletionDate: nil, + MarkAsPaidDate: nil, + PaidDate: nil, + CodingLanguages: pq.StringArray{"Go", "Python"}, + } + + rctx := chi.NewRouteContext() + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/all", nil) + assert.NoError(t, err) + mockDb.On("GetAllBounties", mock.Anything).Return([]db.Bounty{bounty}, nil).Once() + mockDb.On("GetPersonByPubkey", "owner123").Return(db.Person{}).Once() + mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}).Once() + mockDb.On("GetOrganizationByUuid", "org-789").Return(db.Organization{}).Once() + + handler.ServeHTTP(rr, req) + + var returnedBounty []db.BountyResponse + err = json.Unmarshal(rr.Body.Bytes(), &returnedBounty) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + mockDb.AssertExpectations(t) + }) + +} From 2c7a75e2bda372cdb12259622fd50a58a70082cf Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Wed, 21 Feb 2024 05:54:57 +0530 Subject: [PATCH 09/21] TestGetBountyIndexById --- handlers/bounty_test.go | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index c89668ad7..fe9e663c8 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -785,3 +785,55 @@ func TestGetAllBounties(t *testing.T) { }) } + +func TestGetBountyIndexById(t *testing.T) { + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("successful retrieval of bounty by Index ID", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetBountyIndexById) + + bounty := db.Bounty{ + ID: 1, + } + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("bountyId", strconv.Itoa(int(bounty.ID))) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/index/1", nil) + assert.NoError(t, err) + + mockDb.On("GetBountyIndexById", "1").Return(int64(12), nil).Once() + handler.ServeHTTP(rr, req) + + responseBody := rr.Body.Bytes() + responseString := strings.TrimSpace(string(responseBody)) + returnedIndex, err := strconv.Atoi(responseString) + assert.NoError(t, err) + assert.Equal(t, 12, returnedIndex) + + assert.Equal(t, http.StatusOK, rr.Code) + + mockDb.AssertExpectations(t) + }) + + t.Run("bounty index by ID not found", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetBountyIndexById) + + bountyID := "" + rctx := chi.NewRouteContext() + rctx.URLParams.Add("bountyId", bountyID) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/index/"+bountyID, nil) + assert.NoError(t, err) + + mockDb.On("GetBountyIndexById", bountyID).Return(int64(0), fmt.Errorf("bounty not found")).Once() + + handler.ServeHTTP(rr, req) + assert.Equal(t, http.StatusNotFound, rr.Code) + + mockDb.AssertExpectations(t) + }) + +} From 376079d9630b0e7fa342bfdc2cb8ee80d4654744 Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Wed, 21 Feb 2024 06:06:56 +0530 Subject: [PATCH 10/21] TestGetBountyByCreated --- handlers/bounty_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index fe9e663c8..b29e81c7d 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -489,6 +489,23 @@ func TestGetBountyByCreated(t *testing.T) { assert.NotEmpty(t, returnedBounty) }) + t.Run("Should return 404 if bounty is not present in db", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetBountyByCreated) + createdStr := "" + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("created", createdStr) + req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/gobounties/created/"+createdStr, nil) + + mockDb.On("GetBountyDataByCreated", createdStr).Return([]db.Bounty{}, nil).Once() + + handler.ServeHTTP(rr, req) + assert.Equal(t, http.StatusNotFound, rr.Code, "Expected 404 Not Found for nonexistent bounty") + + mockDb.AssertExpectations(t) + }) + } func TestGetPersonAssignedBounties(t *testing.T) { From 3b8745a4855ab6c517c020f70c41919c96f78514 Mon Sep 17 00:00:00 2001 From: Gourav Kadu Date: Thu, 22 Feb 2024 02:41:02 +0530 Subject: [PATCH 11/21] Update organizations.go --- handlers/organizations.go | 82 --------------------------------------- 1 file changed, 82 deletions(-) diff --git a/handlers/organizations.go b/handlers/organizations.go index 0f8a55211..72cb070fa 100644 --- a/handlers/organizations.go +++ b/handlers/organizations.go @@ -710,86 +710,4 @@ func (oh *organizationHandler) DeleteOrganization(w http.ResponseWriter, r *http json.NewEncoder(w).Encode(org) } -func GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse { - var bountyResponse []db.BountyResponse - - for i := 0; i < len(bounties); i++ { - bounty := bounties[i] - - owner := db.DB.GetPersonByPubkey(bounty.OwnerID) - assignee := db.DB.GetPersonByPubkey(bounty.Assignee) - organization := db.DB.GetOrganizationByUuid(bounty.OrgUuid) - - b := db.BountyResponse{ - Bounty: db.Bounty{ - ID: bounty.ID, - OwnerID: bounty.OwnerID, - Paid: bounty.Paid, - Show: bounty.Show, - Type: bounty.Type, - Award: bounty.Award, - AssignedHours: bounty.AssignedHours, - BountyExpires: bounty.BountyExpires, - CommitmentFee: bounty.CommitmentFee, - Price: bounty.Price, - Title: bounty.Title, - Tribe: bounty.Tribe, - Created: bounty.Created, - Assignee: bounty.Assignee, - TicketUrl: bounty.TicketUrl, - Description: bounty.Description, - WantedType: bounty.WantedType, - Deliverables: bounty.Deliverables, - GithubDescription: bounty.GithubDescription, - OneSentenceSummary: bounty.OneSentenceSummary, - EstimatedSessionLength: bounty.EstimatedSessionLength, - EstimatedCompletionDate: bounty.EstimatedCompletionDate, - OrgUuid: bounty.OrgUuid, - Updated: bounty.Updated, - CodingLanguages: bounty.CodingLanguages, - }, - Assignee: db.Person{ - ID: assignee.ID, - Uuid: assignee.Uuid, - OwnerPubKey: assignee.OwnerPubKey, - OwnerAlias: assignee.OwnerAlias, - UniqueName: assignee.UniqueName, - Description: assignee.Description, - Tags: assignee.Tags, - Img: assignee.Img, - Created: assignee.Created, - Updated: assignee.Updated, - LastLogin: assignee.LastLogin, - OwnerRouteHint: assignee.OwnerRouteHint, - OwnerContactKey: assignee.OwnerContactKey, - PriceToMeet: assignee.PriceToMeet, - TwitterConfirmed: assignee.TwitterConfirmed, - }, - Owner: db.Person{ - ID: owner.ID, - Uuid: owner.Uuid, - OwnerPubKey: owner.OwnerPubKey, - OwnerAlias: owner.OwnerAlias, - UniqueName: owner.UniqueName, - Description: owner.Description, - Tags: owner.Tags, - Img: owner.Img, - Created: owner.Created, - Updated: owner.Updated, - LastLogin: owner.LastLogin, - OwnerRouteHint: owner.OwnerRouteHint, - OwnerContactKey: owner.OwnerContactKey, - PriceToMeet: owner.PriceToMeet, - TwitterConfirmed: owner.TwitterConfirmed, - }, - Organization: db.OrganizationShort{ - Name: organization.Name, - Uuid: organization.Uuid, - Img: organization.Img, - }, - } - bountyResponse = append(bountyResponse, b) - } - return bountyResponse -} From 911fccb9d14248c4107c1657f5fa7998a80ea039 Mon Sep 17 00:00:00 2001 From: Gourav Kadu Date: Thu, 22 Feb 2024 02:47:22 +0530 Subject: [PATCH 12/21] Update bounty.go --- handlers/bounty.go | 1 - 1 file changed, 1 deletion(-) diff --git a/handlers/bounty.go b/handlers/bounty.go index 5ef1087ec..cbec13f6d 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -28,7 +28,6 @@ func NewBountyHandler(httpClient HttpClient, db db.Database) *bountyHandler { return &bountyHandler{ httpClient: httpClient, db: db, - generateBountyResponse: GenerateBountyResponse, } } From 8e9dec4fe1d95d9ed1d284c5201f191ca3222b6d Mon Sep 17 00:00:00 2001 From: Gourav Kadu Date: Thu, 22 Feb 2024 03:41:55 +0530 Subject: [PATCH 13/21] Update organizations.go --- handlers/organizations.go | 83 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/handlers/organizations.go b/handlers/organizations.go index f575da187..2346ef230 100644 --- a/handlers/organizations.go +++ b/handlers/organizations.go @@ -720,3 +720,86 @@ func (oh *organizationHandler) DeleteOrganization(w http.ResponseWriter, r *http } +func GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse { + var bountyResponse []db.BountyResponse + + for i := 0; i < len(bounties); i++ { + bounty := bounties[i] + + owner := db.DB.GetPersonByPubkey(bounty.OwnerID) + assignee := db.DB.GetPersonByPubkey(bounty.Assignee) + organization := db.DB.GetOrganizationByUuid(bounty.OrgUuid) + + b := db.BountyResponse{ + Bounty: db.Bounty{ + ID: bounty.ID, + OwnerID: bounty.OwnerID, + Paid: bounty.Paid, + Show: bounty.Show, + Type: bounty.Type, + Award: bounty.Award, + AssignedHours: bounty.AssignedHours, + BountyExpires: bounty.BountyExpires, + CommitmentFee: bounty.CommitmentFee, + Price: bounty.Price, + Title: bounty.Title, + Tribe: bounty.Tribe, + Created: bounty.Created, + Assignee: bounty.Assignee, + TicketUrl: bounty.TicketUrl, + Description: bounty.Description, + WantedType: bounty.WantedType, + Deliverables: bounty.Deliverables, + GithubDescription: bounty.GithubDescription, + OneSentenceSummary: bounty.OneSentenceSummary, + EstimatedSessionLength: bounty.EstimatedSessionLength, + EstimatedCompletionDate: bounty.EstimatedCompletionDate, + OrgUuid: bounty.OrgUuid, + Updated: bounty.Updated, + CodingLanguages: bounty.CodingLanguages, + }, + Assignee: db.Person{ + ID: assignee.ID, + Uuid: assignee.Uuid, + OwnerPubKey: assignee.OwnerPubKey, + OwnerAlias: assignee.OwnerAlias, + UniqueName: assignee.UniqueName, + Description: assignee.Description, + Tags: assignee.Tags, + Img: assignee.Img, + Created: assignee.Created, + Updated: assignee.Updated, + LastLogin: assignee.LastLogin, + OwnerRouteHint: assignee.OwnerRouteHint, + OwnerContactKey: assignee.OwnerContactKey, + PriceToMeet: assignee.PriceToMeet, + TwitterConfirmed: assignee.TwitterConfirmed, + }, + Owner: db.Person{ + ID: owner.ID, + Uuid: owner.Uuid, + OwnerPubKey: owner.OwnerPubKey, + OwnerAlias: owner.OwnerAlias, + UniqueName: owner.UniqueName, + Description: owner.Description, + Tags: owner.Tags, + Img: owner.Img, + Created: owner.Created, + Updated: owner.Updated, + LastLogin: owner.LastLogin, + OwnerRouteHint: owner.OwnerRouteHint, + OwnerContactKey: owner.OwnerContactKey, + PriceToMeet: owner.PriceToMeet, + TwitterConfirmed: owner.TwitterConfirmed, + }, + Organization: db.OrganizationShort{ + Name: organization.Name, + Uuid: organization.Uuid, + Img: organization.Img, + }, + } + bountyResponse = append(bountyResponse, b) + } + + return bountyResponse +} From a686e9ce8ee08f44e4e876ea8c287ee4f435ff9d Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Sun, 25 Feb 2024 19:16:31 +0530 Subject: [PATCH 14/21] some changes and conflicts fix --- handlers/bounty_test.go | 7 +++- handlers/organizations.go | 88 +-------------------------------------- 2 files changed, 7 insertions(+), 88 deletions(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index b3e5a467d..9a4157522 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -924,7 +924,8 @@ func TestGetAllBounties(t *testing.T) { } mockDb.On("GetAssignedBounties", mock.Anything).Return(expectedBounties, nil).Once() - + mockDb.On("GetPersonByPubkey", mock.Anything).Return(db.Person{}, nil) + mockDb.On("GetOrganizationByUuid", mock.Anything).Return(db.Organization{}, nil) rr := httptest.NewRecorder() req, err := http.NewRequest("GET", "/wanteds/assigned/uuid", nil) req = req.WithContext(ctx) @@ -1001,6 +1002,7 @@ func TestGetBountyIndexById(t *testing.T) { assert.NoError(t, err) mockDb.On("GetBountyIndexById", "1").Return(int64(12), nil).Once() + handler.ServeHTTP(rr, req) responseBody := rr.Body.Bytes() @@ -1066,7 +1068,8 @@ func TestGetBountyIndexById(t *testing.T) { } mockDb.On("GetCreatedBounties", mock.Anything).Return(expectedBounties, nil).Once() - + mockDb.On("GetPersonByPubkey", mock.Anything).Return(db.Person{}, nil) + mockDb.On("GetOrganizationByUuid", mock.Anything).Return(db.Organization{}, nil) rr := httptest.NewRecorder() req, err := http.NewRequest("GET", "/wanteds/created/uuid", nil) req = req.WithContext(ctx) diff --git a/handlers/organizations.go b/handlers/organizations.go index fec499c21..87a5dcdfe 100644 --- a/handlers/organizations.go +++ b/handlers/organizations.go @@ -21,9 +21,10 @@ type organizationHandler struct { } func NewOrganizationHandler(db db.Database) *organizationHandler { + bHandler := NewBountyHandler(http.DefaultClient, db) return &organizationHandler{ db: db, - generateBountyHandler: GenerateBountyResponse, + generateBountyHandler: bHandler.GenerateBountyResponse, } } @@ -720,88 +721,3 @@ func (oh *organizationHandler) DeleteOrganization(w http.ResponseWriter, r *http w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(org) } - - -func GenerateBountyResponse(bounties []db.Bounty) []db.BountyResponse { - var bountyResponse []db.BountyResponse - - for i := 0; i < len(bounties); i++ { - bounty := bounties[i] - - owner := db.DB.GetPersonByPubkey(bounty.OwnerID) - assignee := db.DB.GetPersonByPubkey(bounty.Assignee) - organization := db.DB.GetOrganizationByUuid(bounty.OrgUuid) - - b := db.BountyResponse{ - Bounty: db.Bounty{ - ID: bounty.ID, - OwnerID: bounty.OwnerID, - Paid: bounty.Paid, - Show: bounty.Show, - Type: bounty.Type, - Award: bounty.Award, - AssignedHours: bounty.AssignedHours, - BountyExpires: bounty.BountyExpires, - CommitmentFee: bounty.CommitmentFee, - Price: bounty.Price, - Title: bounty.Title, - Tribe: bounty.Tribe, - Created: bounty.Created, - Assignee: bounty.Assignee, - TicketUrl: bounty.TicketUrl, - Description: bounty.Description, - WantedType: bounty.WantedType, - Deliverables: bounty.Deliverables, - GithubDescription: bounty.GithubDescription, - OneSentenceSummary: bounty.OneSentenceSummary, - EstimatedSessionLength: bounty.EstimatedSessionLength, - EstimatedCompletionDate: bounty.EstimatedCompletionDate, - OrgUuid: bounty.OrgUuid, - Updated: bounty.Updated, - CodingLanguages: bounty.CodingLanguages, - }, - Assignee: db.Person{ - ID: assignee.ID, - Uuid: assignee.Uuid, - OwnerPubKey: assignee.OwnerPubKey, - OwnerAlias: assignee.OwnerAlias, - UniqueName: assignee.UniqueName, - Description: assignee.Description, - Tags: assignee.Tags, - Img: assignee.Img, - Created: assignee.Created, - Updated: assignee.Updated, - LastLogin: assignee.LastLogin, - OwnerRouteHint: assignee.OwnerRouteHint, - OwnerContactKey: assignee.OwnerContactKey, - PriceToMeet: assignee.PriceToMeet, - TwitterConfirmed: assignee.TwitterConfirmed, - }, - Owner: db.Person{ - ID: owner.ID, - Uuid: owner.Uuid, - OwnerPubKey: owner.OwnerPubKey, - OwnerAlias: owner.OwnerAlias, - UniqueName: owner.UniqueName, - Description: owner.Description, - Tags: owner.Tags, - Img: owner.Img, - Created: owner.Created, - Updated: owner.Updated, - LastLogin: owner.LastLogin, - OwnerRouteHint: owner.OwnerRouteHint, - OwnerContactKey: owner.OwnerContactKey, - PriceToMeet: owner.PriceToMeet, - TwitterConfirmed: owner.TwitterConfirmed, - }, - Organization: db.OrganizationShort{ - Name: organization.Name, - Uuid: organization.Uuid, - Img: organization.Img, - }, - } - bountyResponse = append(bountyResponse, b) - } - - return bountyResponse -} From 55f66622b1141d0577c2e2ae5e1edb7d002b610e Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Mon, 26 Feb 2024 01:20:27 +0530 Subject: [PATCH 15/21] test fix --- handlers/bounty_test.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 9a4157522..d41cb0c72 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -527,15 +527,6 @@ func TestGetBountyByCreated(t *testing.T) { } bHandler.generateBountyResponse = mockGenerateBountyResponse - expectedBounty := []db.Bounty{{ - ID: 1, - Type: "type1", - Title: "Test Bounty", - Description: "Description", - Created: 123456789, - }} - mockDb.On("GetBountyDataByCreated", "123456789").Return(expectedBounty, nil).Once() - rr := httptest.NewRecorder() handler := http.HandlerFunc(bHandler.GetBountyByCreated) bounty := db.Bounty{ From 2fd91c349bbec40a3ffe4b6fc6593ac1098d5b50 Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Mon, 26 Feb 2024 16:45:23 +0530 Subject: [PATCH 16/21] fixed --- handlers/bounty_test.go | 52 ----------------------------------------- 1 file changed, 52 deletions(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index d41cb0c72..15c683f2b 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -620,58 +620,6 @@ func TestGetPersonCreatedBounties(t *testing.T) { mockDb := dbMocks.NewDatabase(t) mockHttpClient := mocks.NewHttpClient(t) bHandler := NewBountyHandler(mockHttpClient, mockDb) - - t.Run("successful retrieval of bounties", func(t *testing.T) { - rr := httptest.NewRecorder() - handler := http.HandlerFunc(bHandler.GetAllBounties) - bounty := db.Bounty{ - ID: 1, - OwnerID: "owner123", - Paid: false, - Show: true, - Type: "bug fix", - Award: "500", - AssignedHours: 10, - BountyExpires: "2023-12-31", - CommitmentFee: 1000, - Price: 500, - Title: "Fix critical bug in payment system", - Tribe: "development", - Assignee: "user1", - TicketUrl: "http://example.com/issues/1", - OrgUuid: "org-789", - Description: "This bounty is for fixing a critical bug in the payment system that causes transactions to fail under certain conditions.", - WantedType: "immediate", - Deliverables: "A pull request with a fix, including tests", - GithubDescription: true, - OneSentenceSummary: "Fix a critical payment system bug", - EstimatedSessionLength: "2 hours", - EstimatedCompletionDate: "2023-10-01", - Created: time.Now().Unix(), - Updated: nil, - AssignedDate: nil, - CompletionDate: nil, - MarkAsPaidDate: nil, - PaidDate: nil, - CodingLanguages: pq.StringArray{"Go", "Python"}, - } - - rctx := chi.NewRouteContext() - req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/all", nil) - assert.NoError(t, err) - mockDb.On("GetAllBounties", mock.Anything).Return([]db.Bounty{bounty}, nil).Once() - mockDb.On("GetPersonByPubkey", "owner123").Return(db.Person{}).Once() - mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}).Once() - mockDb.On("GetOrganizationByUuid", "org-789").Return(db.Organization{}).Once() - - handler.ServeHTTP(rr, req) - - var returnedBounty []db.BountyResponse - err = json.Unmarshal(rr.Body.Bytes(), &returnedBounty) - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, rr.Code) - mockDb.AssertExpectations(t) - }) t.Run("Should successfull Get Person Created Bounties", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(bHandler.GetPersonCreatedBounties) From 31b518ac9b60262fba32b7097faa41220cb09431 Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Mon, 26 Feb 2024 16:47:54 +0530 Subject: [PATCH 17/21] removed get person created bounties --- handlers/bounty_test.go | 50 ----------------------------------------- 1 file changed, 50 deletions(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 15c683f2b..66908c2e4 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -616,56 +616,6 @@ func TestGetPersonAssignedBounties(t *testing.T) { }) } -func TestGetPersonCreatedBounties(t *testing.T) { - mockDb := dbMocks.NewDatabase(t) - mockHttpClient := mocks.NewHttpClient(t) - bHandler := NewBountyHandler(mockHttpClient, mockDb) - t.Run("Should successfull Get Person Created Bounties", func(t *testing.T) { - rr := httptest.NewRecorder() - handler := http.HandlerFunc(bHandler.GetPersonCreatedBounties) - bounty := db.Bounty{ - ID: 1, - Type: "coding", - Title: "first bounty", - Description: "first bounty description", - OrgUuid: "org-1", - Assignee: "user1", - Created: 1707991475, - OwnerID: "owner-1", - } - - rctx := chi.NewRouteContext() - rctx.URLParams.Add("uuid", "clu80datu2rjujsmim40") - rctx.URLParams.Add("sortBy", "paid") - rctx.URLParams.Add("page", "1") - rctx.URLParams.Add("limit", "20") - rctx.URLParams.Add("search", "") - req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "people/wanteds/created/clu80datu2rjujsmim40?sortBy=paid&page=1&limit=20&search=", nil) - - mockDb.On("GetCreatedBounties", req).Return([]db.Bounty{bounty}, nil).Once() - mockDb.On("GetPersonByPubkey", "owner-1").Return(db.Person{}, nil).Once() - mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}, nil).Once() - mockDb.On("GetOrganizationByUuid", "org-1").Return(db.Organization{}, nil).Once() - handler.ServeHTTP(rr, req) - - var returnedBounty []db.BountyResponse - err := json.Unmarshal(rr.Body.Bytes(), &returnedBounty) - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, rr.Code) - assert.NotEmpty(t, returnedBounty) - - 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) - - }) -} - func TestGetNextBountyByCreated(t *testing.T) { ctx := context.Background() From 0b56f3a976ca3d1582906ca94a9fdedd02883287 Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Mon, 26 Feb 2024 20:48:58 +0530 Subject: [PATCH 18/21] renamed the test --- handlers/bounty_test.go | 52 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 66908c2e4..4d4b01e47 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -773,7 +773,7 @@ func TestGetBountyById(t *testing.T) { }) } -func TestGetAllBounties(t *testing.T) { +func GetPersonAssigned(t *testing.T) { ctx := context.Background() mockDb := dbMocks.NewDatabase(t) @@ -1014,3 +1014,53 @@ func TestGetBountyIndexById(t *testing.T) { assert.Len(t, responseData, 0) }) } + +func GetAllBounties(t *testing.T) { + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + t.Run("Should successfull Get Person Created Bounties", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetPersonCreatedBounties) + bounty := db.Bounty{ + ID: 1, + Type: "coding", + Title: "first bounty", + Description: "first bounty description", + OrgUuid: "org-1", + Assignee: "user1", + Created: 1707991475, + OwnerID: "owner-1", + } + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("uuid", "clu80datu2rjujsmim40") + rctx.URLParams.Add("sortBy", "paid") + rctx.URLParams.Add("page", "1") + rctx.URLParams.Add("limit", "20") + rctx.URLParams.Add("search", "") + req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "people/wanteds/created/clu80datu2rjujsmim40?sortBy=paid&page=1&limit=20&search=", nil) + + mockDb.On("GetCreatedBounties", req).Return([]db.Bounty{bounty}, nil).Once() + mockDb.On("GetPersonByPubkey", "owner-1").Return(db.Person{}, nil).Once() + mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}, nil).Once() + mockDb.On("GetOrganizationByUuid", "org-1").Return(db.Organization{}, nil).Once() + handler.ServeHTTP(rr, req) + + var returnedBounty []db.BountyResponse + err := json.Unmarshal(rr.Body.Bytes(), &returnedBounty) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.NotEmpty(t, returnedBounty) + + 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) + + }) +} From 99eea4a162e99697d4e05ea2462e1e09721df49f Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Mon, 26 Feb 2024 22:27:01 +0530 Subject: [PATCH 19/21] added TestGetAllBounties --- handlers/bounty_test.go | 48 +++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 4d4b01e47..40df7ac2a 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -1015,36 +1015,31 @@ func TestGetBountyIndexById(t *testing.T) { }) } -func GetAllBounties(t *testing.T) { +func TestGetAllBounties(t *testing.T) { mockDb := dbMocks.NewDatabase(t) mockHttpClient := mocks.NewHttpClient(t) bHandler := NewBountyHandler(mockHttpClient, mockDb) - t.Run("Should successfull Get Person Created Bounties", func(t *testing.T) { + t.Run("Should successfull All Bounties", func(t *testing.T) { rr := httptest.NewRecorder() - handler := http.HandlerFunc(bHandler.GetPersonCreatedBounties) - bounty := db.Bounty{ - ID: 1, - Type: "coding", - Title: "first bounty", - Description: "first bounty description", - OrgUuid: "org-1", - Assignee: "user1", - Created: 1707991475, - OwnerID: "owner-1", + handler := http.HandlerFunc(bHandler.GetAllBounties) + bounties := []db.Bounty{ + {ID: 1, + Type: "coding", + Title: "first bounty", + Description: "first bounty description", + OrgUuid: "org-1", + Assignee: "user1", + Created: 1707991475, + OwnerID: "owner-1", + }, } rctx := chi.NewRouteContext() - rctx.URLParams.Add("uuid", "clu80datu2rjujsmim40") - rctx.URLParams.Add("sortBy", "paid") - rctx.URLParams.Add("page", "1") - rctx.URLParams.Add("limit", "20") - rctx.URLParams.Add("search", "") - req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "people/wanteds/created/clu80datu2rjujsmim40?sortBy=paid&page=1&limit=20&search=", nil) + req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/all", nil) - mockDb.On("GetCreatedBounties", req).Return([]db.Bounty{bounty}, nil).Once() - mockDb.On("GetPersonByPubkey", "owner-1").Return(db.Person{}, nil).Once() - mockDb.On("GetPersonByPubkey", "user1").Return(db.Person{}, nil).Once() - mockDb.On("GetOrganizationByUuid", "org-1").Return(db.Organization{}, nil).Once() + mockDb.On("GetAllBounties", req).Return(bounties) + mockDb.On("GetPersonByPubkey", mock.Anything).Return(db.Person{}, nil) + mockDb.On("GetOrganizationByUuid", mock.Anything).Return(db.Organization{}, nil) handler.ServeHTTP(rr, req) var returnedBounty []db.BountyResponse @@ -1053,14 +1048,5 @@ func GetAllBounties(t *testing.T) { assert.Equal(t, http.StatusOK, rr.Code) assert.NotEmpty(t, returnedBounty) - 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) - }) } From 87775ac0c64d4d1e527da8803190855b5a0c5bee Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Tue, 27 Feb 2024 14:50:24 +0530 Subject: [PATCH 20/21] change http url req --- handlers/bounty_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 40df7ac2a..ca3617c18 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -887,7 +887,7 @@ func TestGetBountyIndexById(t *testing.T) { rctx := chi.NewRouteContext() rctx.URLParams.Add("bountyId", strconv.Itoa(int(bounty.ID))) - req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/index/1", nil) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "gobounties/index/1", nil) assert.NoError(t, err) mockDb.On("GetBountyIndexById", "1").Return(int64(12), nil).Once() @@ -912,7 +912,7 @@ func TestGetBountyIndexById(t *testing.T) { bountyID := "" rctx := chi.NewRouteContext() rctx.URLParams.Add("bountyId", bountyID) - req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/index/"+bountyID, nil) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "gobounties/index/"+bountyID, nil) assert.NoError(t, err) mockDb.On("GetBountyIndexById", bountyID).Return(int64(0), fmt.Errorf("bounty not found")).Once() @@ -1035,7 +1035,7 @@ func TestGetAllBounties(t *testing.T) { } rctx := chi.NewRouteContext() - req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/all", nil) + req, _ := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "gobounties/all", nil) mockDb.On("GetAllBounties", req).Return(bounties) mockDb.On("GetPersonByPubkey", mock.Anything).Return(db.Person{}, nil) From d43b4dd07dda17004ba0d3b530df7a63140d34fd Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Tue, 27 Feb 2024 14:53:23 +0530 Subject: [PATCH 21/21] more url req changes --- handlers/bounty_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index ca3617c18..778a3d53c 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -739,7 +739,7 @@ func TestGetBountyById(t *testing.T) { rctx := chi.NewRouteContext() rctx.URLParams.Add("bountyId", strconv.Itoa(int(bounty.ID))) - req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bounty/1", nil) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "gobounties/bounty/1", nil) assert.NoError(t, err) mockDb.On("GetBountyById", mock.Anything).Return([]db.Bounty{bounty}, nil).Once() @@ -762,7 +762,7 @@ func TestGetBountyById(t *testing.T) { rctx := chi.NewRouteContext() rctx.URLParams.Add("bountyId", "999") - req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bounty/999", nil) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "gobounties/bounty/999", nil) assert.NoError(t, err) mockDb.On("GetBountyById", "999").Return(nil, errors.New("not-found")).Once()