Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend [Integration Test] people.go handlers GetPeopleBySearch, GetListedPeople, & GetPersonByUuid #1531

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions handlers/people.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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)
}
Expand Down
178 changes: 178 additions & 0 deletions handlers/people_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
6 changes: 4 additions & 2 deletions routes/people.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion routes/person.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
Loading