From dd5f068d77b204f309e04fe796ff1c41ae31514d Mon Sep 17 00:00:00 2001 From: hkarani Date: Fri, 23 Feb 2024 01:24:46 +0300 Subject: [PATCH 01/10] Added UTs for GetAllTribes, GetTotalTribes & GetListedTribes --- handlers/tribes.go | 12 ++-- handlers/tribes_test.go | 124 ++++++++++++++++++++++++++++++++++++++++ routes/tribes.go | 5 +- 3 files changed, 133 insertions(+), 8 deletions(-) diff --git a/handlers/tribes.go b/handlers/tribes.go index 11a319a16..a23a9716c 100644 --- a/handlers/tribes.go +++ b/handlers/tribes.go @@ -31,20 +31,20 @@ func NewTribeHandler(db db.Database) *tribeHandler { } } -func GetAllTribes(w http.ResponseWriter, r *http.Request) { - tribes := db.DB.GetAllTribes() +func (th *tribeHandler) GetAllTribes(w http.ResponseWriter, r *http.Request) { + tribes := th.db.GetAllTribes() w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(tribes) } -func GetTotalribes(w http.ResponseWriter, r *http.Request) { - tribesTotal := db.DB.GetTribesTotal() +func (th *tribeHandler) GetTotalTribes(w http.ResponseWriter, r *http.Request) { + tribesTotal := th.db.GetTribesTotal() w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(tribesTotal) } -func GetListedTribes(w http.ResponseWriter, r *http.Request) { - tribes := db.DB.GetListedTribes(r) +func (th *tribeHandler) GetListedTribes(w http.ResponseWriter, r *http.Request) { + tribes := th.db.GetListedTribes(r) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(tribes) } diff --git a/handlers/tribes_test.go b/handlers/tribes_test.go index 4602ecc11..b64bb51c8 100644 --- a/handlers/tribes_test.go +++ b/handlers/tribes_test.go @@ -6,9 +6,11 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "strings" "testing" "github.com/go-chi/chi" + "github.com/lib/pq" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/db" mocks "github.com/stakwork/sphinx-tribes/mocks" @@ -471,3 +473,125 @@ func TestGetTribeByUniqueName(t *testing.T) { assert.Equal(t, mockUniqueName, responseData["unique_name"]) }) } + +func TestGetAllTribes(t *testing.T) { + mockDb := mocks.NewDatabase(t) + tHandler := NewTribeHandler(mockDb) + t.Run("should return all tribes", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(tHandler.GetAllTribes) + + expectedTribes := []db.Tribe{ + {UUID: "uuid", Name: "Tribe1"}, + {UUID: "uuid", Name: "Tribe2"}, + {UUID: "uuid", Name: "Tribe3"}, + } + + rctx := chi.NewRouteContext() + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/", nil) + assert.NoError(t, err) + + mockDb.On("GetAllTribes", mock.Anything).Return(expectedTribes) + handler.ServeHTTP(rr, req) + var returnedTribes []db.Tribe + err = json.Unmarshal(rr.Body.Bytes(), &returnedTribes) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.EqualValues(t, expectedTribes, returnedTribes) + mockDb.AssertExpectations(t) + + }) +} + +func TestGetTotalTribes(t *testing.T) { + mockDb := mocks.NewDatabase(t) + tHandler := NewTribeHandler(mockDb) + t.Run("should return the total number of tribes", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(tHandler.GetTotalTribes) + + expectedTribes := []db.Tribe{ + {UUID: "uuid", Name: "Tribe1"}, + {UUID: "uuid", Name: "Tribe2"}, + {UUID: "uuid", Name: "Tribe3"}, + } + + expectedTribesCount := int64(len(expectedTribes)) + + rctx := chi.NewRouteContext() + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/total", nil) + assert.NoError(t, err) + + mockDb.On("GetTribesTotal", mock.Anything).Return(expectedTribesCount) + + handler.ServeHTTP(rr, req) + var returnedTribesCount int64 + err = json.Unmarshal(rr.Body.Bytes(), &returnedTribesCount) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.EqualValues(t, expectedTribesCount, returnedTribesCount) + mockDb.AssertExpectations(t) + + }) +} + +func TestGetListedTribes(t *testing.T) { + mockDb := mocks.NewDatabase(t) + tHandler := NewTribeHandler(mockDb) + + t.Run("should only return tribes associated with a passed tag query", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(tHandler.GetListedTribes) + expectedTribes := []db.Tribe{ + {UUID: "1", Name: "Tribe 1", Tags: pq.StringArray{"tag1", "tag2", "tag3"}}, + {UUID: "2", Name: "Tribe 2", Tags: pq.StringArray{"tag4", "tag5"}}, + {UUID: "3", Name: "Tribe 3", Tags: pq.StringArray{"tag6", "tag7", "tag8"}}, + } + rctx := chi.NewRouteContext() + tagVals := pq.StringArray{"tag1", "tag4", "tag7"} + tags := strings.Join(tagVals, ",") + rctx.URLParams.Add("tags", tags) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/", nil) + if err != nil { + t.Fatal(err) + } + + mockDb.On("GetListedTribes", req).Return(expectedTribes) + handler.ServeHTTP(rr, req) + var returnedTribes []db.Tribe + err = json.Unmarshal(rr.Body.Bytes(), &returnedTribes) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.EqualValues(t, expectedTribes, returnedTribes) + + }) + + t.Run("should return all tribes when no tag queries are passed", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(tHandler.GetListedTribes) + expectedTribes := []db.Tribe{ + {UUID: "1", Name: "Tribe 1", Tags: pq.StringArray{"tag1", "tag2", "tag3"}}, + {UUID: "2", Name: "Tribe 2", Tags: pq.StringArray{"tag4", "tag5"}}, + {UUID: "3", Name: "Tribe 3", Tags: pq.StringArray{"tag6", "tag7", "tag8"}}, + } + rctx := chi.NewRouteContext() + tagVals := pq.StringArray{} + tags := strings.Join(tagVals, ",") + rctx.URLParams.Add("tags", tags) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/", nil) + if err != nil { + t.Fatal(err) + } + + mockDb.On("GetListedTribes", req).Return(expectedTribes) + handler.ServeHTTP(rr, req) + + var returnedTribes []db.Tribe + err = json.Unmarshal(rr.Body.Bytes(), &returnedTribes) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.EqualValues(t, expectedTribes, returnedTribes) + + }) + +} diff --git a/routes/tribes.go b/routes/tribes.go index 7e8e68439..ed4ad2368 100644 --- a/routes/tribes.go +++ b/routes/tribes.go @@ -10,12 +10,13 @@ func TribeRoutes() chi.Router { r := chi.NewRouter() tribeHandlers := handlers.NewTribeHandler(db.DB) r.Group(func(r chi.Router) { - r.Get("/", handlers.GetListedTribes) + r.Get("/{tags}", tribeHandlers.GetListedTribes) r.Get("/app_url/{app_url}", tribeHandlers.GetTribesByAppUrl) r.Get("/app_urls/{app_urls}", handlers.GetTribesByAppUrls) r.Get("/{uuid}", tribeHandlers.GetTribe) - r.Get("/total", handlers.GetTotalribes) + r.Get("/total", tribeHandlers.GetTotalTribes) r.Post("/", tribeHandlers.CreateOrEditTribe) + r.Get("/", tribeHandlers.GetAllTribes) }) return r } From b51ef8a5abca9ff15799ab539f35238879ba0c8a Mon Sep 17 00:00:00 2001 From: elraphty Date: Thu, 22 Feb 2024 17:52:58 +0100 Subject: [PATCH 02/10] added second brain url field --- db/structs.go | 1 + 1 file changed, 1 insertion(+) diff --git a/db/structs.go b/db/structs.go index 479548717..3e71fe64f 100644 --- a/db/structs.go +++ b/db/structs.go @@ -36,6 +36,7 @@ type Tribe struct { Deleted bool `json:"deleted"` AppURL string `json:"app_url"` FeedURL string `json:"feed_url"` + SecondBrainUrl string `json:"second_brain_url"` FeedType uint64 `json:"feed_type"` LastActive int64 `json:"last_active"` Bots string `json:"bots"` From 2c18e52a2c7d921db3bf4d29e76c0a6a9b73204a Mon Sep 17 00:00:00 2001 From: elraphty Date: Thu, 22 Feb 2024 22:45:17 +0100 Subject: [PATCH 03/10] added second_brain_url --- db/config.go | 1 + tribes.sql | 1 + 2 files changed, 2 insertions(+) diff --git a/db/config.go b/db/config.go index 957b4f89d..19b948254 100644 --- a/db/config.go +++ b/db/config.go @@ -50,6 +50,7 @@ func InitDB() { fmt.Println("db connected") // migrate table changes + db.AutoMigrate(&Tribe{}) db.AutoMigrate(&Person{}) db.AutoMigrate(&Channel{}) db.AutoMigrate(&LeaderBoard{}) diff --git a/tribes.sql b/tribes.sql index 8f389ded5..a3a0f6abd 100644 --- a/tribes.sql +++ b/tribes.sql @@ -22,6 +22,7 @@ CREATE TABLE tribes ( deleted boolean, app_url TEXT, feed_url TEXT, + second_brain_url TEXT, feed_type INT, last_active BIGINT, bots TEXT, From 9979ff3c9f1358c4d6dd93a984217787e4e1f6af Mon Sep 17 00:00:00 2001 From: Abdul Wahab Date: Wed, 21 Feb 2024 16:21:59 +0500 Subject: [PATCH 04/10] Added UTs for GetPersonCreatedBounties, GetPersonAssignedBounties, GetBountyByCreated --- handlers/bounty.go | 12 +-- handlers/bounty_test.go | 217 +++++++++++++++++++++++++++++++++++++++- routes/people.go | 7 +- 3 files changed, 225 insertions(+), 11 deletions(-) diff --git a/handlers/bounty.go b/handlers/bounty.go index 60d9f501c..eb23bfb26 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -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 86e856ce4..b7b68782e 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -495,13 +495,35 @@ func TestDeleteBounty(t *testing.T) { func TestGetBountyByCreated(t *testing.T) { ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") mockDb := dbMocks.NewDatabase(t) - mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse { - return []db.BountyResponse{} // Mocked response - } mockHttpClient := mocks.NewHttpClient(t) bHandler := NewBountyHandler(mockHttpClient, mockDb) t.Run("Should return bounty by its created value", func(t *testing.T) { + mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse { + var bountyResponses []db.BountyResponse + + for _, bounty := range bounties { + owner := db.Person{ + ID: 1, + } + assignee := db.Person{ + ID: 1, + } + organization := db.OrganizationShort{ + Uuid: "uuid", + } + + bountyResponse := db.BountyResponse{ + Bounty: bounty, + Assignee: assignee, + Owner: owner, + Organization: organization, + } + bountyResponses = append(bountyResponses, bountyResponse) + } + + return bountyResponses + } bHandler.generateBountyResponse = mockGenerateBountyResponse expectedBounty := []db.Bounty{{ @@ -527,6 +549,15 @@ func TestGetBountyByCreated(t *testing.T) { handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusOK, rr.Code) + + var responseData []db.BountyResponse + err = json.Unmarshal(rr.Body.Bytes(), &responseData) + if err != nil { + t.Fatalf("Error decoding JSON response: %s", err) + } + + assert.NotEmpty(t, responseData) + assert.Len(t, responseData, 1) }) } @@ -605,3 +636,183 @@ func TestGetOrganizationPreviousBountyByCreated(t *testing.T) { mockDb.AssertExpectations(t) }) } + +func TestGetPersonAssignedBounties(t *testing.T) { + ctx := context.Background() + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("should return bounties assigned to the user", func(t *testing.T) { + mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse { + var bountyResponses []db.BountyResponse + + for _, bounty := range bounties { + owner := db.Person{ + ID: 1, + } + assignee := db.Person{ + ID: 1, + } + organization := db.OrganizationShort{ + Uuid: "uuid", + } + + bountyResponse := db.BountyResponse{ + Bounty: bounty, + Assignee: assignee, + Owner: owner, + Organization: organization, + } + bountyResponses = append(bountyResponses, bountyResponse) + } + + return bountyResponses + } + bHandler.generateBountyResponse = mockGenerateBountyResponse + + mockDb.On("GetAssignedBounties", mock.Anything).Return([]db.Bounty{ + {ID: 1, Assignee: "user1"}, + {ID: 2, Assignee: "user1"}, + }, nil).Once() + + rr := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/wanteds/assigned/uuid", nil) + req = req.WithContext(ctx) + if err != nil { + t.Fatal(err) + } + + bHandler.GetPersonAssignedBounties(rr, req) + + assert.Equal(t, http.StatusOK, rr.Code) + + var responseData []db.BountyResponse + err = json.Unmarshal(rr.Body.Bytes(), &responseData) + if err != nil { + t.Fatalf("Error decoding JSON response: %s", err) + } + + assert.NotEmpty(t, responseData) + assert.Len(t, responseData, 2) + }) + + t.Run("should not return bounties assigned to other users", func(t *testing.T) { + mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse { + return []db.BountyResponse{} + } + bHandler.generateBountyResponse = mockGenerateBountyResponse + + mockDb.On("GetAssignedBounties", mock.Anything).Return([]db.Bounty{}, nil).Once() + + rr := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/wanteds/assigned/uuid", nil) + req = req.WithContext(ctx) + if err != nil { + t.Fatal(err) + } + + bHandler.GetPersonAssignedBounties(rr, req) + + assert.Equal(t, http.StatusOK, rr.Code) + + var responseData []db.BountyResponse + err = json.Unmarshal(rr.Body.Bytes(), &responseData) + if err != nil { + t.Fatalf("Error decoding JSON response: %s", err) + } + + assert.Empty(t, responseData) + assert.Len(t, responseData, 0) + }) +} + +func TestGetPersonCreatedBounties(t *testing.T) { + ctx := context.Background() + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("should return bounties created by the user", func(t *testing.T) { + mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse { + var bountyResponses []db.BountyResponse + + for _, bounty := range bounties { + owner := db.Person{ + ID: 1, + } + assignee := db.Person{ + ID: 1, + } + organization := db.OrganizationShort{ + Uuid: "uuid", + } + + bountyResponse := db.BountyResponse{ + Bounty: bounty, + Assignee: assignee, + Owner: owner, + Organization: organization, + } + bountyResponses = append(bountyResponses, bountyResponse) + } + + return bountyResponses + } + bHandler.generateBountyResponse = mockGenerateBountyResponse + + mockDb.On("GetCreatedBounties", mock.Anything).Return([]db.Bounty{ + {ID: 1, OwnerID: "user1"}, + {ID: 2, OwnerID: "user1"}, + }, nil).Once() + + rr := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/wanteds/created/uuid", nil) + req = req.WithContext(ctx) + if err != nil { + t.Fatal(err) + } + + bHandler.GetPersonCreatedBounties(rr, req) + + assert.Equal(t, http.StatusOK, rr.Code) + + var responseData []db.BountyResponse + err = json.Unmarshal(rr.Body.Bytes(), &responseData) + if err != nil { + t.Fatalf("Error decoding JSON response: %s", err) + } + + assert.NotEmpty(t, responseData) + assert.Len(t, responseData, 2) + }) + + t.Run("should not return bounties created by other users", func(t *testing.T) { + mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse { + return []db.BountyResponse{} + } + bHandler.generateBountyResponse = mockGenerateBountyResponse + + mockDb.On("GetCreatedBounties", mock.Anything).Return([]db.Bounty{}, nil).Once() + + rr := httptest.NewRecorder() + req, err := http.NewRequest("GET", "/wanteds/created/uuid", nil) + req = req.WithContext(ctx) + if err != nil { + t.Fatal(err) + } + + bHandler.GetPersonCreatedBounties(rr, req) + + assert.Equal(t, http.StatusOK, rr.Code) + + var responseData []db.BountyResponse + err = json.Unmarshal(rr.Body.Bytes(), &responseData) + if err != nil { + t.Fatalf("Error decoding JSON response: %s", err) + } + + assert.Empty(t, responseData) + assert.Len(t, responseData, 0) + }) +} 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) From e46bef24093fef00c4e0a83596608e7c829d1c53 Mon Sep 17 00:00:00 2001 From: Abdul Wahab Date: Fri, 23 Feb 2024 00:54:18 +0500 Subject: [PATCH 05/10] Addressed changes --- handlers/bounty_test.go | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index b7b68782e..448d55231 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -558,6 +558,13 @@ func TestGetBountyByCreated(t *testing.T) { 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) }) } @@ -671,10 +678,12 @@ func TestGetPersonAssignedBounties(t *testing.T) { } bHandler.generateBountyResponse = mockGenerateBountyResponse - mockDb.On("GetAssignedBounties", mock.Anything).Return([]db.Bounty{ + expectedBounties := []db.Bounty{ {ID: 1, Assignee: "user1"}, {ID: 2, Assignee: "user1"}, - }, nil).Once() + } + + mockDb.On("GetAssignedBounties", mock.Anything).Return(expectedBounties, nil).Once() rr := httptest.NewRecorder() req, err := http.NewRequest("GET", "/wanteds/assigned/uuid", nil) @@ -695,6 +704,11 @@ func TestGetPersonAssignedBounties(t *testing.T) { 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) { @@ -761,10 +775,12 @@ func TestGetPersonCreatedBounties(t *testing.T) { } bHandler.generateBountyResponse = mockGenerateBountyResponse - mockDb.On("GetCreatedBounties", mock.Anything).Return([]db.Bounty{ + expectedBounties := []db.Bounty{ {ID: 1, OwnerID: "user1"}, {ID: 2, OwnerID: "user1"}, - }, nil).Once() + } + + mockDb.On("GetCreatedBounties", mock.Anything).Return(expectedBounties, nil).Once() rr := httptest.NewRecorder() req, err := http.NewRequest("GET", "/wanteds/created/uuid", nil) @@ -785,6 +801,11 @@ func TestGetPersonCreatedBounties(t *testing.T) { 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) { From b5777d3ad862be507090a23db111fe43c335a86b Mon Sep 17 00:00:00 2001 From: Khalid Sulyman <84248666+Khaliid-oh@users.noreply.github.com> Date: Fri, 23 Feb 2024 08:05:18 +0100 Subject: [PATCH 06/10] fix/ioutil to io --- db/store.go | 6 +++--- feeds/generic.go | 4 ++-- feeds/podcastindex.go | 8 ++++---- handlers/bots.go | 4 ++-- handlers/bounties.go | 4 ++-- handlers/channel.go | 4 ++-- handlers/feed.go | 12 ++++++------ handlers/people.go | 7 +++---- routes/index.go | 4 ++-- yarn.lock | 4 ++++ 10 files changed, 30 insertions(+), 27 deletions(-) create mode 100644 yarn.lock diff --git a/db/store.go b/db/store.go index edd64ffe3..f27874682 100644 --- a/db/store.go +++ b/db/store.go @@ -5,7 +5,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "strconv" "time" @@ -175,7 +175,7 @@ func Verify(w http.ResponseWriter, r *http.Request) { } payload := VerifyPayload{} - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) r.Body.Close() err = json.Unmarshal(body, &payload) if err != nil { @@ -263,7 +263,7 @@ type Save struct { func PostSave(w http.ResponseWriter, r *http.Request) { save := Save{} - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) r.Body.Close() err = json.Unmarshal(body, &save) if err != nil { diff --git a/feeds/generic.go b/feeds/generic.go index 30df499d3..7bab0caa5 100644 --- a/feeds/generic.go +++ b/feeds/generic.go @@ -2,7 +2,7 @@ package feeds import ( "encoding/json" - "io/ioutil" + "io" "net/http" "strings" ) @@ -161,6 +161,6 @@ func httpget(url string) ([]byte, error) { } defer response.Body.Close() - body, _ := ioutil.ReadAll(response.Body) + body, _ := io.ReadAll(response.Body) return body, nil } diff --git a/feeds/podcastindex.go b/feeds/podcastindex.go index de9f6d552..0f0eedcc5 100644 --- a/feeds/podcastindex.go +++ b/feeds/podcastindex.go @@ -5,7 +5,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "os" "strconv" @@ -122,7 +122,7 @@ func PodcastFeed(url string, fulltext bool) (*Podcast, error) { defer resp.Body.Close() var r PodcastResponse - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) err = json.Unmarshal(body, &r) if err != nil { fmt.Println("=> json unmarshall error", err) @@ -158,7 +158,7 @@ func PodcastEpisodes(url string, fulltext bool) ([]Episode, error) { defer resp.Body.Close() var r EpisodeResponse - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) err = json.Unmarshal(body, &r) if err != nil { fmt.Println("json unmarshall error", err) @@ -234,7 +234,7 @@ func PodcastEpisodesByPerson(query string, fulltext bool) ([]Episode, error) { defer resp.Body.Close() var r EpisodeResponse - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) err = json.Unmarshal(body, &r) if err != nil { fmt.Println("json unmarshall error", err) diff --git a/handlers/bots.go b/handlers/bots.go index 935ff29ab..e1700bbfc 100644 --- a/handlers/bots.go +++ b/handlers/bots.go @@ -3,7 +3,7 @@ package handlers import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "regexp" "strconv" @@ -20,7 +20,7 @@ func CreateOrEditBot(w http.ResponseWriter, r *http.Request) { pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) bot := db.Bot{} - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) r.Body.Close() err = json.Unmarshal(body, &bot) if err != nil { diff --git a/handlers/bounties.go b/handlers/bounties.go index f2efb4e70..6177442ab 100644 --- a/handlers/bounties.go +++ b/handlers/bounties.go @@ -3,7 +3,7 @@ package handlers import ( "encoding/json" "fmt" - "io/ioutil" + "io" "log" "net/http" "reflect" @@ -46,7 +46,7 @@ func GetBountiesLeaderboard(w http.ResponseWriter, _ *http.Request) { func DeleteBountyAssignee(w http.ResponseWriter, r *http.Request) { invoice := db.DeleteBountyAssignee{} - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) var deletedAssignee bool r.Body.Close() diff --git a/handlers/channel.go b/handlers/channel.go index fa9ca5f77..627fe1e97 100644 --- a/handlers/channel.go +++ b/handlers/channel.go @@ -3,7 +3,7 @@ package handlers import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strconv" @@ -56,7 +56,7 @@ func CreateChannel(w http.ResponseWriter, r *http.Request) { pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) channel := db.Channel{} - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) r.Body.Close() err = json.Unmarshal(body, &channel) if err != nil { diff --git a/handlers/feed.go b/handlers/feed.go index f1eaae3d8..9507c2e38 100644 --- a/handlers/feed.go +++ b/handlers/feed.go @@ -6,7 +6,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "os" "strings" @@ -53,7 +53,7 @@ func DownloadYoutubeFeed(w http.ResponseWriter, r *http.Request) { tube, err := youtube.NewService(ctx, option.WithAPIKey(apiKey)) youtube_download := db.YoutubeDownload{} - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) r.Body.Close() err = json.Unmarshal(body, &youtube_download) @@ -143,7 +143,7 @@ func processYoutubeDownload(data []string) { fmt.Println("Youtube Download Request Error ===", err) } defer response.Body.Close() - res, err := ioutil.ReadAll(response.Body) + res, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Youtube Download Request Error ==", err) } @@ -272,7 +272,7 @@ func getFeed(feedURL string, feedID string) (*feeds.Podcast, error) { defer resp.Body.Close() var r feeds.PodcastResponse - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) err = json.Unmarshal(body, &r) if err != nil { fmt.Println("json unmarshall error", err) @@ -314,7 +314,7 @@ func getEpisodes(feedURL string, feedID string) ([]feeds.Episode, error) { defer resp.Body.Close() var r feeds.EpisodeResponse - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) err = json.Unmarshal(body, &r) if err != nil { fmt.Println("json unmarshall error", err) @@ -348,7 +348,7 @@ func searchPodcastIndex(term string) ([]feeds.Podcast, error) { defer resp.Body.Close() var r feeds.PodcastSearchResponse - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) err = json.Unmarshal(body, &r) if err != nil { fmt.Println("json unmarshall error", err) diff --git a/handlers/people.go b/handlers/people.go index f0b0df1d6..1bac21c1a 100644 --- a/handlers/people.go +++ b/handlers/people.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "log" "net/http" "os" @@ -479,7 +478,7 @@ func GetAssetByPubkey(pubkey string) ([]db.AssetBalanceData, error) { defer resp.Body.Close() var r db.AssetResponse - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) err = json.Unmarshal(body, &r) if err != nil { fmt.Println("json unmarshall error", err) @@ -512,7 +511,7 @@ func GetAssetList(pubkey string) ([]db.AssetListData, error) { defer resp.Body.Close() var r []db.AssetListData - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) err = json.Unmarshal(body, &r) if err != nil { fmt.Println("json unmarshall error", err) @@ -527,7 +526,7 @@ func AddOrRemoveBadge(w http.ResponseWriter, r *http.Request) { pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string) badgeCreationData := db.BadgeCreationData{} - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) r.Body.Close() err = json.Unmarshal(body, &badgeCreationData) if err != nil { diff --git a/routes/index.go b/routes/index.go index ba2cf7233..d489f4923 100644 --- a/routes/index.go +++ b/routes/index.go @@ -3,7 +3,7 @@ package routes import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "os" "time" @@ -116,7 +116,7 @@ func getFromAuth(path string) (*extractResponse, error) { return nil, err } defer resp.Body.Close() - body2, err := ioutil.ReadAll(resp.Body) + body2, err := io.ReadAll(resp.Body) if err != nil { return nil, err } diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 000000000..fb57ccd13 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,4 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + From b2b81290a0784d8d0dc84337092f032ba4b12632 Mon Sep 17 00:00:00 2001 From: Khalid Sulyman <84248666+Khaalid-oh@users.noreply.github.com> Date: Fri, 23 Feb 2024 15:54:05 +0100 Subject: [PATCH 07/10] Delete yarn.lock --- yarn.lock | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 yarn.lock diff --git a/yarn.lock b/yarn.lock deleted file mode 100644 index fb57ccd13..000000000 --- a/yarn.lock +++ /dev/null @@ -1,4 +0,0 @@ -# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. -# yarn lockfile v1 - - From 6b131ef4ca266bc563bbfec99c417065bef5a7a9 Mon Sep 17 00:00:00 2001 From: Abdul Wahab Date: Wed, 21 Feb 2024 16:21:59 +0500 Subject: [PATCH 08/10] Added UTs for GetPersonCreatedBounties, GetPersonAssignedBounties, GetBountyByCreated --- handlers/bounty_test.go | 29 ++++------------------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 448d55231..b7b68782e 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -558,13 +558,6 @@ func TestGetBountyByCreated(t *testing.T) { 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) }) } @@ -678,12 +671,10 @@ func TestGetPersonAssignedBounties(t *testing.T) { } bHandler.generateBountyResponse = mockGenerateBountyResponse - expectedBounties := []db.Bounty{ + mockDb.On("GetAssignedBounties", mock.Anything).Return([]db.Bounty{ {ID: 1, Assignee: "user1"}, {ID: 2, Assignee: "user1"}, - } - - mockDb.On("GetAssignedBounties", mock.Anything).Return(expectedBounties, nil).Once() + }, nil).Once() rr := httptest.NewRecorder() req, err := http.NewRequest("GET", "/wanteds/assigned/uuid", nil) @@ -704,11 +695,6 @@ func TestGetPersonAssignedBounties(t *testing.T) { 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) { @@ -775,12 +761,10 @@ func TestGetPersonCreatedBounties(t *testing.T) { } bHandler.generateBountyResponse = mockGenerateBountyResponse - expectedBounties := []db.Bounty{ + mockDb.On("GetCreatedBounties", mock.Anything).Return([]db.Bounty{ {ID: 1, OwnerID: "user1"}, {ID: 2, OwnerID: "user1"}, - } - - mockDb.On("GetCreatedBounties", mock.Anything).Return(expectedBounties, nil).Once() + }, nil).Once() rr := httptest.NewRecorder() req, err := http.NewRequest("GET", "/wanteds/created/uuid", nil) @@ -801,11 +785,6 @@ func TestGetPersonCreatedBounties(t *testing.T) { 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) { From 9c0ca295dc20d5bdc69beeb03c747305d67820b7 Mon Sep 17 00:00:00 2001 From: hkarani Date: Sat, 24 Feb 2024 04:28:19 +0300 Subject: [PATCH 09/10] Remove tags from params to URL Query --- handlers/tribes_test.go | 26 +++++++++++++++++++------- routes/tribes.go | 2 +- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/handlers/tribes_test.go b/handlers/tribes_test.go index b64bb51c8..fb254232b 100644 --- a/handlers/tribes_test.go +++ b/handlers/tribes_test.go @@ -547,11 +547,17 @@ func TestGetListedTribes(t *testing.T) { {UUID: "2", Name: "Tribe 2", Tags: pq.StringArray{"tag4", "tag5"}}, {UUID: "3", Name: "Tribe 3", Tags: pq.StringArray{"tag6", "tag7", "tag8"}}, } - rctx := chi.NewRouteContext() - tagVals := pq.StringArray{"tag1", "tag4", "tag7"} + + req, err := http.NewRequest("GET", "/tribes", nil) + if err != nil { + t.Fatal(err) + } + + query := req.URL.Query() + tagVals := pq.StringArray{} tags := strings.Join(tagVals, ",") - rctx.URLParams.Add("tags", tags) - req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/", nil) + query.Set("tags", tags) + req.URL.RawQuery = query.Encode() if err != nil { t.Fatal(err) } @@ -574,11 +580,17 @@ func TestGetListedTribes(t *testing.T) { {UUID: "2", Name: "Tribe 2", Tags: pq.StringArray{"tag4", "tag5"}}, {UUID: "3", Name: "Tribe 3", Tags: pq.StringArray{"tag6", "tag7", "tag8"}}, } - rctx := chi.NewRouteContext() + + req, err := http.NewRequest("GET", "/tribes", nil) + if err != nil { + t.Fatal(err) + } + + query := req.URL.Query() tagVals := pq.StringArray{} tags := strings.Join(tagVals, ",") - rctx.URLParams.Add("tags", tags) - req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/", nil) + query.Set("tags", tags) + req.URL.RawQuery = query.Encode() if err != nil { t.Fatal(err) } diff --git a/routes/tribes.go b/routes/tribes.go index ed4ad2368..cccd74f2d 100644 --- a/routes/tribes.go +++ b/routes/tribes.go @@ -10,7 +10,7 @@ func TribeRoutes() chi.Router { r := chi.NewRouter() tribeHandlers := handlers.NewTribeHandler(db.DB) r.Group(func(r chi.Router) { - r.Get("/{tags}", tribeHandlers.GetListedTribes) + r.Get("/", tribeHandlers.GetListedTribes) r.Get("/app_url/{app_url}", tribeHandlers.GetTribesByAppUrl) r.Get("/app_urls/{app_urls}", handlers.GetTribesByAppUrls) r.Get("/{uuid}", tribeHandlers.GetTribe) From 1e9d93c843f3e7d0a1dd1d4172304a761959f85f Mon Sep 17 00:00:00 2001 From: hkarani Date: Sat, 24 Feb 2024 04:33:11 +0300 Subject: [PATCH 10/10] Revert "Remove tags from params to URL Query" This reverts commit 9c0ca295dc20d5bdc69beeb03c747305d67820b7. --- handlers/tribes_test.go | 26 +++++++------------------- routes/tribes.go | 2 +- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/handlers/tribes_test.go b/handlers/tribes_test.go index fb254232b..b64bb51c8 100644 --- a/handlers/tribes_test.go +++ b/handlers/tribes_test.go @@ -547,17 +547,11 @@ func TestGetListedTribes(t *testing.T) { {UUID: "2", Name: "Tribe 2", Tags: pq.StringArray{"tag4", "tag5"}}, {UUID: "3", Name: "Tribe 3", Tags: pq.StringArray{"tag6", "tag7", "tag8"}}, } - - req, err := http.NewRequest("GET", "/tribes", nil) - if err != nil { - t.Fatal(err) - } - - query := req.URL.Query() - tagVals := pq.StringArray{} + rctx := chi.NewRouteContext() + tagVals := pq.StringArray{"tag1", "tag4", "tag7"} tags := strings.Join(tagVals, ",") - query.Set("tags", tags) - req.URL.RawQuery = query.Encode() + rctx.URLParams.Add("tags", tags) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/", nil) if err != nil { t.Fatal(err) } @@ -580,17 +574,11 @@ func TestGetListedTribes(t *testing.T) { {UUID: "2", Name: "Tribe 2", Tags: pq.StringArray{"tag4", "tag5"}}, {UUID: "3", Name: "Tribe 3", Tags: pq.StringArray{"tag6", "tag7", "tag8"}}, } - - req, err := http.NewRequest("GET", "/tribes", nil) - if err != nil { - t.Fatal(err) - } - - query := req.URL.Query() + rctx := chi.NewRouteContext() tagVals := pq.StringArray{} tags := strings.Join(tagVals, ",") - query.Set("tags", tags) - req.URL.RawQuery = query.Encode() + rctx.URLParams.Add("tags", tags) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/", nil) if err != nil { t.Fatal(err) } diff --git a/routes/tribes.go b/routes/tribes.go index cccd74f2d..ed4ad2368 100644 --- a/routes/tribes.go +++ b/routes/tribes.go @@ -10,7 +10,7 @@ func TribeRoutes() chi.Router { r := chi.NewRouter() tribeHandlers := handlers.NewTribeHandler(db.DB) r.Group(func(r chi.Router) { - r.Get("/", tribeHandlers.GetListedTribes) + r.Get("/{tags}", tribeHandlers.GetListedTribes) r.Get("/app_url/{app_url}", tribeHandlers.GetTribesByAppUrl) r.Get("/app_urls/{app_urls}", handlers.GetTribesByAppUrls) r.Get("/{uuid}", tribeHandlers.GetTribe)