-
Notifications
You must be signed in to change notification settings - Fork 60
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
Backend [Integration Test] people.go handlers GetPeopleBySearch, GetListedPeople, & GetPersonByUuid #1531
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AbdulWahab3181 the search should be in the URL query, not param. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed |
||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AbdulWahab3181 the limit and page should be in the URL query, not param. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed |
||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AbdulWahab3181 the limit, page, and search should be in the URL query, not param. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed |
||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AbdulWahab3181 the page, limit, and languages should be in the URL query, not the param. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed |
||
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) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AbdulWahab3181 the search should be in the URL query, not param.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@elraphty Addressed