MM-16500: Adds ability to retrieve the total count of teams via the API. (#11325)

Этот коммит содержится в:
Martin Kraft
2019-06-24 17:05:34 -04:00
коммит произвёл Miguel de la Cruz
родитель 49d5037a89
Коммит c07b7046ca
6 изменённых файлов: 78 добавлений и 3 удалений

Просмотреть файл

@@ -724,9 +724,14 @@ func updateTeamMemberSchemeRoles(c *Context, w http.ResponseWriter, r *http.Requ
func getAllTeams(c *Context, w http.ResponseWriter, r *http.Request) {
var teams []*model.Team
var err *model.AppError
var teamsWithCount *model.TeamsWithCount
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.GetAllTeamsPage(c.Params.Page*c.Params.PerPage, c.Params.PerPage)
if c.Params.IncludeTotalCount {
teamsWithCount, err = c.App.GetAllTeamsPageWithCount(c.Params.Page*c.Params.PerPage, c.Params.PerPage)
} else {
teams, err = c.App.GetAllTeamsPage(c.Params.Page*c.Params.PerPage, c.Params.PerPage)
}
} else if c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_LIST_PRIVATE_TEAMS) {
teams, err = c.App.GetAllPrivateTeamsPage(c.Params.Page*c.Params.PerPage, c.Params.PerPage)
} else if c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_LIST_PUBLIC_TEAMS) {
@@ -740,7 +745,15 @@ func getAllTeams(c *Context, w http.ResponseWriter, r *http.Request) {
c.App.SanitizeTeams(c.App.Session, teams)
w.Write([]byte(model.TeamListToJson(teams)))
var resBody []byte
if c.Params.IncludeTotalCount {
resBody = model.TeamsWithCountToJson(teamsWithCount)
} else {
resBody = []byte(model.TeamListToJson(teams))
}
w.Write(resBody)
}
func searchTeams(c *Context, w http.ResponseWriter, r *http.Request) {

Просмотреть файл

@@ -627,6 +627,8 @@ func TestGetAllTeams(t *testing.T) {
PerPage int
Permissions []string
ExpectedTeams []string
WithCount bool
ExpectedCount int64
}{
{
Name: "Get 1 team per page",
@@ -677,6 +679,15 @@ func TestGetAllTeams(t *testing.T) {
Permissions: []string{},
ExpectedTeams: []string{},
},
{
Name: "Get all teams with count",
Page: 0,
PerPage: 10,
Permissions: []string{model.PERMISSION_LIST_PUBLIC_TEAMS.Id, model.PERMISSION_LIST_PRIVATE_TEAMS.Id},
ExpectedTeams: []string{th.BasicTeam.Id, team1.Id, team2.Id, team3.Id},
WithCount: true,
ExpectedCount: 4,
},
}
for _, tc := range testCases {
@@ -694,12 +705,18 @@ func TestGetAllTeams(t *testing.T) {
}
var teams []*model.Team
teams, resp = Client.GetAllTeams("", tc.Page, tc.PerPage)
var count int64
if tc.WithCount {
teams, count, resp = Client.GetAllTeamsWithTotalCount("", tc.Page, tc.PerPage)
} else {
teams, resp = Client.GetAllTeams("", tc.Page, tc.PerPage)
}
CheckNoError(t, resp)
require.Equal(t, len(tc.ExpectedTeams), len(teams))
for idx, team := range teams {
assert.Equal(t, tc.ExpectedTeams[idx], team.Id)
}
require.Equal(t, tc.ExpectedCount, count)
})
}