MM-18357: Adds pagination to team search. (#12910)
* MM-18357: Adds pagination to team search. * MM-18357: Adds new client method for paginated requests. * MM-18357: Adds feedback about non-supported pagination-permissions combo. * MM-18357: Removes unnecessary conversion. * MM-18357: Removes paginate parameter and uses nil on page and perpage instead.
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
955f8c4e8e
Коммит
14bcd1f0a1
21
api4/team.go
21
api4/team.go
@@ -789,13 +789,22 @@ func searchTeams(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var teams []*model.Team
|
||||
var totalCount int64
|
||||
var err *model.AppError
|
||||
|
||||
if c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_LIST_PRIVATE_TEAMS) && c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_LIST_PUBLIC_TEAMS) {
|
||||
teams, err = c.App.SearchAllTeams(props.Term)
|
||||
teams, totalCount, err = c.App.SearchAllTeams(props)
|
||||
} else if c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_LIST_PRIVATE_TEAMS) {
|
||||
if props.Page != nil || props.PerPage != nil {
|
||||
c.Err = model.NewAppError("searchTeams", "api.team.search_teams.pagination_not_implemented.private_team_search", nil, "", http.StatusNotImplemented)
|
||||
return
|
||||
}
|
||||
teams, err = c.App.SearchPrivateTeams(props.Term)
|
||||
} else if c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_LIST_PUBLIC_TEAMS) {
|
||||
if props.Page != nil || props.PerPage != nil {
|
||||
c.Err = model.NewAppError("searchTeams", "api.team.search_teams.pagination_not_implemented.public_team_search", nil, "", http.StatusNotImplemented)
|
||||
return
|
||||
}
|
||||
teams, err = c.App.SearchPublicTeams(props.Term)
|
||||
} else {
|
||||
teams = []*model.Team{}
|
||||
@@ -808,7 +817,15 @@ func searchTeams(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
c.App.SanitizeTeams(c.App.Session, teams)
|
||||
|
||||
w.Write([]byte(model.TeamListToJson(teams)))
|
||||
var payload []byte
|
||||
if props.Page != nil && props.PerPage != nil {
|
||||
twc := &model.TeamsWithCount{Teams: teams, TotalCount: totalCount}
|
||||
payload = model.TeamsWithCountToJson(twc)
|
||||
} else {
|
||||
payload = []byte(model.TeamListToJson(teams))
|
||||
}
|
||||
|
||||
w.Write(payload)
|
||||
}
|
||||
|
||||
func teamExists(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -922,6 +922,79 @@ func TestSearchAllTeams(t *testing.T) {
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
}
|
||||
|
||||
func TestSearchAllTeamsPaged(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
commonRandom := model.NewId()
|
||||
teams := [3]*model.Team{}
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
uid := model.NewId()
|
||||
newTeam, err := th.App.CreateTeam(&model.Team{
|
||||
DisplayName: fmt.Sprintf("%s %d %s", commonRandom, i, uid),
|
||||
Name: fmt.Sprintf("%s-%d-%s", commonRandom, i, uid),
|
||||
Type: model.TEAM_OPEN,
|
||||
Email: th.GenerateTestEmail(),
|
||||
})
|
||||
require.Nil(t, err)
|
||||
teams[i] = newTeam
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
Name string
|
||||
Search *model.TeamSearch
|
||||
ExpectedTeams []string
|
||||
ExpectedTotalCount int64
|
||||
}{
|
||||
{
|
||||
Name: "Get all teams on one page",
|
||||
Search: &model.TeamSearch{Term: commonRandom, Page: model.NewInt(0), PerPage: model.NewInt(100)},
|
||||
ExpectedTeams: []string{teams[0].Id, teams[1].Id, teams[2].Id},
|
||||
ExpectedTotalCount: 3,
|
||||
},
|
||||
{
|
||||
Name: "Get 2 teams on the first page",
|
||||
Search: &model.TeamSearch{Term: commonRandom, Page: model.NewInt(0), PerPage: model.NewInt(2)},
|
||||
ExpectedTeams: []string{teams[0].Id, teams[1].Id},
|
||||
ExpectedTotalCount: 3,
|
||||
},
|
||||
{
|
||||
Name: "Get 1 team on the second page",
|
||||
Search: &model.TeamSearch{Term: commonRandom, Page: model.NewInt(1), PerPage: model.NewInt(2)},
|
||||
ExpectedTeams: []string{teams[2].Id},
|
||||
ExpectedTotalCount: 3,
|
||||
},
|
||||
{
|
||||
Name: "SearchTeamsPaged paginates results by default",
|
||||
Search: &model.TeamSearch{Term: commonRandom},
|
||||
ExpectedTeams: []string{teams[0].Id, teams[1].Id, teams[2].Id},
|
||||
ExpectedTotalCount: 3,
|
||||
},
|
||||
{
|
||||
Name: "No results",
|
||||
Search: &model.TeamSearch{Term: model.NewId()},
|
||||
ExpectedTeams: []string{},
|
||||
ExpectedTotalCount: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
teams, count, resp := th.SystemAdminClient.SearchTeamsPaged(tc.Search)
|
||||
require.Nil(t, resp.Error)
|
||||
require.Equal(t, tc.ExpectedTotalCount, count)
|
||||
require.Equal(t, len(tc.ExpectedTeams), len(teams))
|
||||
for i, team := range teams {
|
||||
require.Equal(t, tc.ExpectedTeams[i], team.Id)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
_, _, resp := th.Client.SearchTeamsPaged(&model.TeamSearch{Term: commonRandom, PerPage: model.NewInt(100)})
|
||||
require.Equal(t, "api.team.search_teams.pagination_not_implemented.public_team_search", resp.Error.Id)
|
||||
require.Equal(t, http.StatusNotImplemented, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestSearchAllTeamsSanitization(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
Ссылка в новой задаче
Block a user