Commit f9bfc3c0 authored by MaxGranzow's avatar MaxGranzow Committed by Julien Schröter

Add tests for user recommendation endpoint

parent 4f93c356
Pipeline #2288 failed with stages
in 3 minutes and 7 seconds
......@@ -157,7 +157,7 @@ func (mockUser) SelectUserRecommendations(userID uint32, count uint32) ([]schema
users = append(users, getMockUserInfo(u))
}
}
return users, nil
return users[:count], nil
}
// Users with IDs 1-3 have authentication. Everyone else will be rejected.
......@@ -453,6 +453,142 @@ func Test_searchForUsers_401(t *testing.T) {
assert.Equal(t, 401, res.Code)
}
/*************************
* GET /user/recommended?[c=]
*************************/
func Test_getUserRecommendations_200_noCount(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/recommended",
map[string]string{},
nil)
router.ServeHTTP(res, req)
assert.Equal(t, 200, res.Code)
var mockUserInfos []schemas.InfoUserSchema
for cnt, user := range mockUsers {
if cnt != 1 {
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_getUserRecommendations_200_validCount(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/recommended",
map[string]string{
"c": "1",
},
nil)
router.ServeHTTP(res, req)
assert.Equal(t, 200, res.Code)
mockUserInfos := []schemas.InfoUserSchema{getMockUserInfo(mockUsers[2])}
var actual []schemas.InfoUserSchema
err := json.Unmarshal(res.Body.Bytes(), &actual)
assert.Nil(t, err)
assert.ElementsMatch(t, mockUserInfos, actual)
}
func Test_getUserRecommendations_200_negativeCount(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/recommended",
map[string]string{
"c": "-1",
},
nil)
router.ServeHTTP(res, req)
assert.Equal(t, 200, res.Code)
var mockUserInfos []schemas.InfoUserSchema
for cnt, user := range mockUsers {
if cnt > 1 {
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_getUserRecommendations_400(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/recommended",
map[string]string{
"c": "invalid",
},
nil)
router.ServeHTTP(res, req)
assert.Equal(t, 400, res.Code)
}
func Test_getUserRecommendations_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.NewRequest(
"GET",
"/user/recommended",
nil)
router.ServeHTTP(res, req)
assert.Equal(t, 401, res.Code)
}
/*************************
* 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