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/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/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"` 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/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/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/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/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/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) 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 } 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,