Commit 26efb2e0 authored by MaxGranzow's avatar MaxGranzow

Add tests for user-search endpoint

parent 0a753cd8
Pipeline #2217 passed with stage
in 4 minutes and 28 seconds
...@@ -144,7 +144,7 @@ func (mockUser) SelectByUsernameLike(username string) ([]schemas.InfoUserSchema, ...@@ -144,7 +144,7 @@ func (mockUser) SelectByUsernameLike(username string) ([]schemas.InfoUserSchema,
var users []schemas.InfoUserSchema var users []schemas.InfoUserSchema
for _, u := range mockUsers { for _, u := range mockUsers {
if strings.HasPrefix(u.Username, username) { if strings.HasPrefix(u.Username, username) {
users = append(users, schemas.InfoUserFromFullUser(u)) users = append(users, getMockUserInfo(u))
} }
} }
return users, nil return users, nil
...@@ -286,6 +286,163 @@ func Test_getAllUsers_401(t *testing.T) { ...@@ -286,6 +286,163 @@ func Test_getAllUsers_401(t *testing.T) {
assert.Equal(t, 401, res.Code) assert.Equal(t, 401, res.Code)
} }
/*************************
* GET /user/search?s=
*************************/
func Test_searchForUsers_200_multipleMatchingUsernames(t *testing.T) {
router := setup.SetupRoutes(nil, nil, nil, &mockTitle{}, nil, &mockUser{}, nil, nil, nil, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
env.Login("user1", "password1", t)
res := httptest.NewRecorder()
req, _ := env.NewParameterRequest(
"GET",
"/user/search",
map[string]string{
"s": "user",
},
nil)
router.ServeHTTP(res, req)
assert.Equal(t, 200, res.Code)
var mockUserInfos []schemas.InfoUserSchema
for _, user := range mockUsers {
mockUserInfos = append(mockUserInfos, getMockUserInfo(user))
}
var actual []schemas.InfoUserSchema
err := json.Unmarshal(res.Body.Bytes(), &actual)
assert.Nil(t, err)
assert.ElementsMatch(t, mockUserInfos, actual)
}
func Test_searchForUsers_200_singleMatchingUsername(t *testing.T) {
router := setup.SetupRoutes(nil, nil, nil, &mockTitle{}, nil, &mockUser{}, nil, nil, nil, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
env.Login("user1", "password1", t)
res := httptest.NewRecorder()
req, _ := env.NewParameterRequest(
"GET",
"/user/search",
map[string]string{
"s": "user1",
},
nil)
router.ServeHTTP(res, req)
assert.Equal(t, 200, res.Code)
var mockUserInfos []schemas.InfoUserSchema
mockUserInfos = append(mockUserInfos, getMockUserInfo(mockUsers[1]))
var actual []schemas.InfoUserSchema
err := json.Unmarshal(res.Body.Bytes(), &actual)
assert.Nil(t, err)
assert.ElementsMatch(t, mockUserInfos, actual)
}
func Test_searchForUsers_200_noMatchingUsernames(t *testing.T) {
router := setup.SetupRoutes(nil, nil, nil, &mockTitle{}, nil, &mockUser{}, nil, nil, nil, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
env.Login("user1", "password1", t)
res := httptest.NewRecorder()
req, _ := env.NewParameterRequest(
"GET",
"/user/search",
map[string]string{
"s": "nonexistent",
},
nil)
router.ServeHTTP(res, req)
assert.Equal(t, 200, res.Code)
mockUserInfos := make([]schemas.InfoUserSchema, 0)
var actual []schemas.InfoUserSchema
err := json.Unmarshal(res.Body.Bytes(), &actual)
assert.Nil(t, err)
assert.ElementsMatch(t, mockUserInfos, actual)
}
func Test_searchForUsers_400_noSearchQuery(t *testing.T) {
router := setup.SetupRoutes(nil, nil, nil, &mockTitle{}, nil, &mockUser{}, nil, nil, nil, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
env.Login("user1", "password1", t)
res := httptest.NewRecorder()
req, _ := env.NewParameterRequest(
"GET",
"/user/search",
map[string]string{},
nil)
router.ServeHTTP(res, req)
assert.Equal(t, 400, res.Code)
}
func Test_searchForUsers_400_tooShortSearchQuery(t *testing.T) {
router := setup.SetupRoutes(nil, nil, nil, &mockTitle{}, nil, &mockUser{}, nil, nil, nil, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
env.Login("user1", "password1", t)
res := httptest.NewRecorder()
req, _ := env.NewParameterRequest(
"GET",
"/user/search",
map[string]string{
"s": "u",
},
nil)
router.ServeHTTP(res, req)
assert.Equal(t, 400, res.Code)
}
func Test_searchForUsers_401(t *testing.T) {
router := setup.SetupRoutes(nil, nil, nil, &mockTitle{}, nil, &mockUser{}, nil, nil, nil, nil, newSession(), nil, nil, nil, nil)
env := new(TestEnv)
env.Router = router
res := httptest.NewRecorder()
req, _ := env.NewParameterRequest(
"GET",
"/user/search",
map[string]string{
"s": "user",
},
nil)
router.ServeHTTP(res, req)
assert.Equal(t, 401, res.Code)
}
/************************* /*************************
* GET /user * GET /user
*************************/ *************************/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment