diff --git a/handlers/people.go b/handlers/people.go index 4d2a92db7..f0b0df1d6 100644 --- a/handlers/people.go +++ b/handlers/people.go @@ -359,9 +359,9 @@ func (ph *peopleHandler) GetPersonById(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(person) } -func GetPersonByUuid(w http.ResponseWriter, r *http.Request) { +func (ph *peopleHandler) GetPersonByUuid(w http.ResponseWriter, r *http.Request) { uuid := chi.URLParam(r, "uuid") - person := db.DB.GetPersonByUuid(uuid) + person := ph.db.GetPersonByUuid(uuid) assetBalanceData, err := GetAssetByPubkey(person.OwnerPubKey) personResponse := make(map[string]interface{}) @@ -623,14 +623,14 @@ func GetPeopleShortList(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(people) } -func GetPeopleBySearch(w http.ResponseWriter, r *http.Request) { - people := db.DB.GetPeopleBySearch(r) +func (ph *peopleHandler) GetPeopleBySearch(w http.ResponseWriter, r *http.Request) { + people := ph.db.GetPeopleBySearch(r) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(people) } -func GetListedPeople(w http.ResponseWriter, r *http.Request) { - people := db.DB.GetListedPeople(r) +func (ph *peopleHandler) GetListedPeople(w http.ResponseWriter, r *http.Request) { + people := ph.db.GetListedPeople(r) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(people) } diff --git a/handlers/people_test.go b/handlers/people_test.go index 155ec0ef1..c0fe4d8c7 100644 --- a/handlers/people_test.go +++ b/handlers/people_test.go @@ -258,3 +258,181 @@ func TestDeletePerson(t *testing.T) { mockDb.AssertExpectations(t) }) } + +func TestGetPeopleBySearch(t *testing.T) { + mockDb := mocks.NewDatabase(t) + pHandler := NewPeopleHandler(mockDb) + + t.Run("should return users that match the search text", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(pHandler.GetPeopleBySearch) + expectedPeople := []db.Person{ + {ID: 1, Uuid: "uuid1", OwnerPubKey: "pubkey1", OwnerAlias: "John Doe"}, + {ID: 2, Uuid: "uuid2", OwnerPubKey: "pubkey2", OwnerAlias: "John Smith"}, + } + + rctx := chi.NewRouteContext() + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/search?search=John", nil) + assert.NoError(t, err) + + mockDb.On("GetPeopleBySearch", mock.Anything).Return(expectedPeople) + handler.ServeHTTP(rr, req) + + var returnedPeople []db.Person + err = json.Unmarshal(rr.Body.Bytes(), &returnedPeople) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.EqualValues(t, expectedPeople, returnedPeople) + mockDb.AssertExpectations(t) + }) + + t.Run("should return an empty search result when no user matches the search text", func(t *testing.T) { + mockDb.ExpectedCalls = nil + rr := httptest.NewRecorder() + handler := http.HandlerFunc(pHandler.GetPeopleBySearch) + expectedPeople := []db.Person{} + + rctx := chi.NewRouteContext() + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/search?search=user not matched", nil) + assert.NoError(t, err) + + mockDb.On("GetPeopleBySearch", mock.Anything).Return(expectedPeople) + handler.ServeHTTP(rr, req) + + var returnedPeople []db.Person + err = json.Unmarshal(rr.Body.Bytes(), &returnedPeople) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.EqualValues(t, expectedPeople, returnedPeople) + mockDb.AssertExpectations(t) + }) +} + +func TestGetListedPeople(t *testing.T) { + mockDb := mocks.NewDatabase(t) + pHandler := NewPeopleHandler(mockDb) + + t.Run("should return all listed users", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(pHandler.GetListedPeople) + expectedPeople := []db.Person{ + {ID: 1, Uuid: "uuid1", OwnerPubKey: "pubkey1", OwnerAlias: "John Doe"}, + {ID: 2, Uuid: "uuid2", OwnerPubKey: "pubkey2", OwnerAlias: "John Smith"}, + } + + rctx := chi.NewRouteContext() + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/?page=1&limit=10", nil) + assert.NoError(t, err) + + mockDb.On("GetListedPeople", mock.Anything).Return(expectedPeople) + handler.ServeHTTP(rr, req) + + var returnedPeople []db.Person + err = json.Unmarshal(rr.Body.Bytes(), &returnedPeople) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.EqualValues(t, expectedPeople, returnedPeople) + mockDb.AssertExpectations(t) + }) + + t.Run("should return only users that match a search text when a search is added to the URL query", func(t *testing.T) { + mockDb.ExpectedCalls = nil + rr := httptest.NewRecorder() + handler := http.HandlerFunc(pHandler.GetListedPeople) + expectedPeople := []db.Person{ + {ID: 1, Uuid: "uuid1", OwnerPubKey: "pubkey1", OwnerAlias: "John Doe"}, + {ID: 2, Uuid: "uuid2", OwnerPubKey: "pubkey2", OwnerAlias: "John Smith"}, + } + + rctx := chi.NewRouteContext() + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/?page=1&limit=10&search=John", nil) + assert.NoError(t, err) + + mockDb.On("GetListedPeople", mock.Anything).Return(expectedPeople) + handler.ServeHTTP(rr, req) + + var returnedPeople []db.Person + err = json.Unmarshal(rr.Body.Bytes(), &returnedPeople) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.EqualValues(t, expectedPeople, returnedPeople) + mockDb.AssertExpectations(t) + }) + + t.Run("should return only users that match a skill set when languages are passed to the URL query", func(t *testing.T) { + mockDb.ExpectedCalls = nil + rr := httptest.NewRecorder() + handler := http.HandlerFunc(pHandler.GetListedPeople) + expectedPeople := []db.Person{ + {ID: 1, Uuid: "uuid1", OwnerPubKey: "pubkey1", OwnerAlias: "John Doe"}, + } + + rctx := chi.NewRouteContext() + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/?page=1&limit=10&languages=typescript", nil) + assert.NoError(t, err) + + mockDb.On("GetListedPeople", mock.Anything).Return(expectedPeople) + handler.ServeHTTP(rr, req) + + var returnedPeople []db.Person + err = json.Unmarshal(rr.Body.Bytes(), &returnedPeople) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.EqualValues(t, expectedPeople, returnedPeople) + mockDb.AssertExpectations(t) + }) +} + +func TestGetPersonByUuid(t *testing.T) { + mockDb := mocks.NewDatabase(t) + pHandler := NewPeopleHandler(mockDb) + + t.Run("should return a user with the right UUID", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(pHandler.GetPersonByUuid) + expectedPerson := db.Person{ + ID: 1, + Uuid: uuid.New().String(), + OwnerPubKey: "person-pub-key", + OwnerAlias: "owner", + UniqueName: "test_user", + Description: "test user", + } + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("uuid", "uuid") + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/uuid", nil) + assert.NoError(t, err) + + mockDb.On("GetPersonByUuid", mock.Anything).Return(expectedPerson) + handler.ServeHTTP(rr, req) + + var returnedPerson db.Person + err = json.Unmarshal(rr.Body.Bytes(), &returnedPerson) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rr.Code) + assert.EqualValues(t, expectedPerson, returnedPerson) + mockDb.AssertExpectations(t) + }) + + t.Run("should return no user for a wrong UUID", func(t *testing.T) { + mockDb.ExpectedCalls = nil + rr := httptest.NewRecorder() + handler := http.HandlerFunc(pHandler.GetPersonByUuid) + expectedPerson := db.Person{} + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("uuid", "wrong-uuid") + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/uuid", nil) + assert.NoError(t, err) + + mockDb.On("GetPersonByUuid", mock.Anything).Return(expectedPerson) + handler.ServeHTTP(rr, req) + + var returnedPerson db.Person + err = json.Unmarshal(rr.Body.Bytes(), &returnedPerson) + assert.NoError(t, err) + assert.EqualValues(t, expectedPerson, returnedPerson) + mockDb.AssertExpectations(t) + }) +} diff --git a/routes/people.go b/routes/people.go index 2ed0bfa85..c21c02938 100644 --- a/routes/people.go +++ b/routes/people.go @@ -2,14 +2,16 @@ package routes import ( "github.com/go-chi/chi" + "github.com/stakwork/sphinx-tribes/db" "github.com/stakwork/sphinx-tribes/handlers" ) func PeopleRoutes() chi.Router { r := chi.NewRouter() + peopleHandler := handlers.NewPeopleHandler(db.DB) r.Group(func(r chi.Router) { - r.Get("/", handlers.GetListedPeople) - r.Get("/search", handlers.GetPeopleBySearch) + 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) diff --git a/routes/person.go b/routes/person.go index 99ada5967..6a07d37df 100644 --- a/routes/person.go +++ b/routes/person.go @@ -13,7 +13,7 @@ func PersonRoutes() chi.Router { r.Group(func(r chi.Router) { r.Get("/{pubkey}", peopleHandler.GetPersonByPubkey) r.Get("/id/{id}", peopleHandler.GetPersonById) - r.Get("/uuid/{uuid}", handlers.GetPersonByUuid) + r.Get("/uuid/{uuid}", peopleHandler.GetPersonByUuid) r.Get("/uuid/{uuid}/assets", handlers.GetPersonAssetsByUuid) r.Get("/githubname/{github}", handlers.GetPersonByGithubName) })