Skip to content

Commit

Permalink
Added UTs for GetPeopleBySearch, GetListedPeople, GetPersonByUuid
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulWahab3181 committed Feb 19, 2024
1 parent f71fe4e commit 3caf894
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 9 deletions.
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
188 changes: 188 additions & 0 deletions handlers/people_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,191 @@ 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()
rctx.URLParams.Add("search", "John")
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/search", 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()
rctx.URLParams.Add("search", "user not matched")
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/search", 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()
rctx.URLParams.Add("page", "1")
rctx.URLParams.Add("limit", "10")
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/", 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()
rctx.URLParams.Add("page", "1")
rctx.URLParams.Add("limit", "10")
rctx.URLParams.Add("search", "John")
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/", 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()
rctx.URLParams.Add("page", "1")
rctx.URLParams.Add("limit", "10")
rctx.URLParams.Add("languages", "typescript")
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/", 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

0 comments on commit 3caf894

Please sign in to comment.