MM-24692: Add a since parameter to getGroups api (#14444)

* add a since parameter to getGroups api

* update for lint error

* when using since, return deleted groups as well.

* update flaky test, groups have same create time

Co-authored-by: mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Scott Bishel
2020-05-07 14:35:09 -06:00
коммит произвёл GitHub
родитель 1031e27fd8
Коммит 80c846412d
6 изменённых файлов: 87 добавлений и 2 удалений

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

@@ -8,6 +8,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/mattermost/mattermost-server/v5/audit"
@@ -730,6 +731,16 @@ func getGroups(c *Context, w http.ResponseWriter, r *http.Request) {
opts.NotAssociatedToChannel = channelID
}
sinceString := r.URL.Query().Get("since")
if len(sinceString) > 0 {
since, parseError := strconv.ParseInt(sinceString, 10, 64)
if parseError != nil {
c.SetInvalidParam("since")
return
}
opts.Since = since
}
groups, err := c.App.GetGroups(c.Params.Page, c.Params.PerPage, opts)
if err != nil {
c.Err = err

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

@@ -851,6 +851,8 @@ func TestGetGroups(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
// make sure "createdDate" for next group is after one created in InitBasic()
time.Sleep(2 * time.Millisecond)
id := model.NewId()
group, err := th.App.CreateGroup(&model.Group{
DisplayName: "dn-foo_" + id,
@@ -860,6 +862,7 @@ func TestGetGroups(t *testing.T) {
RemoteId: model.NewId(),
})
assert.Nil(t, err)
start := group.UpdateAt - 1
opts := model.GroupSearchOpts{
PageOpts: &model.PageOpts{
@@ -911,6 +914,36 @@ func TestGetGroups(t *testing.T) {
_, response = th.Client.GetGroups(opts)
assert.Nil(t, response.Error)
// test "since", should only return group created in this test, not th.Group
opts.Since = start
groups, response = th.Client.GetGroups(opts)
assert.Nil(t, response.Error)
assert.Len(t, groups, 1)
// test correct group returned
assert.Equal(t, groups[0].Id, group.Id)
// delete group, should still return
th.App.DeleteGroup(group.Id)
groups, response = th.Client.GetGroups(opts)
assert.Nil(t, response.Error)
assert.Len(t, groups, 1)
assert.Equal(t, groups[0].Id, group.Id)
// test with current since value, return none
opts.Since = model.GetMillis()
groups, response = th.Client.GetGroups(opts)
assert.Nil(t, response.Error)
assert.Empty(t, groups)
// make sure delete group is not returned without Since
opts.Since = 0
groups, response = th.Client.GetGroups(opts)
assert.Nil(t, response.Error)
//'Normal getGroups should not return delete groups
assert.Len(t, groups, 1)
// make sure it returned th.Group,not group
assert.Equal(t, groups[0].Id, th.Group.Id)
}
func TestGetGroupsByUserId(t *testing.T) {

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

@@ -3781,6 +3781,9 @@ func (c *Client4) GetGroups(opts GroupSearchOpts) ([]*Group, *Response) {
opts.FilterAllowReference,
opts.Q,
)
if opts.Since > 0 {
path = fmt.Sprintf("%s&since=%v", path, opts.Since)
}
if opts.PageOpts != nil {
path = fmt.Sprintf("%s&page=%v&per_page=%v", path, opts.PageOpts.Page, opts.PageOpts.PerPage)
}

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

@@ -79,6 +79,7 @@ type GroupSearchOpts struct {
IncludeMemberCount bool
FilterAllowReference bool
PageOpts *PageOpts
Since int64
}
type PageOpts struct {

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

@@ -1144,9 +1144,16 @@ func (s *SqlGroupStore) GetGroups(page, perPage int, opts model.GroupSearchOpts)
groupsQuery = groupsQuery.
From("UserGroups g").
Where("g.DeleteAt = 0").
OrderBy("g.DisplayName")
if opts.Since > 0 {
groupsQuery = groupsQuery.Where(sq.Gt{
"g.UpdateAt": opts.Since,
})
} else {
groupsQuery = groupsQuery.Where("g.DeleteAt = 0")
}
if perPage != 0 {
groupsQuery = groupsQuery.
Limit(uint64(perPage)).

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

@@ -3010,6 +3010,8 @@ func testGetGroups(t *testing.T, ss store.Store) {
team1, err := ss.Team().Save(team1)
require.Nil(t, err)
startCreateTime := team1.UpdateAt - 1
// Create Channel1
channel1 := &model.Channel{
TeamId: model.NewId(),
@@ -3148,10 +3150,12 @@ func testGetGroups(t *testing.T, ss store.Store) {
require.Nil(t, err)
user2.DeleteAt = 1
ss.User().Update(user2, true)
u2Update, _ := ss.User().Update(user2, true)
group2NameSubstring := "group-2"
endCreateTime := u2Update.New.UpdateAt + 1
testCases := []struct {
Name string
Page int
@@ -3309,6 +3313,32 @@ func testGetGroups(t *testing.T, ss store.Store) {
return true
},
},
{
Name: "Use Since return all",
Opts: model.GroupSearchOpts{FilterAllowReference: true, Since: startCreateTime},
Page: 0,
PerPage: 100,
Resultf: func(groups []*model.Group) bool {
if len(groups) == 0 {
return false
}
for _, g := range groups {
if g.DeleteAt != 0 {
return false
}
}
return true
},
},
{
Name: "Use Since return none",
Opts: model.GroupSearchOpts{FilterAllowReference: true, Since: endCreateTime},
Page: 0,
PerPage: 100,
Resultf: func(groups []*model.Group) bool {
return len(groups) == 0
},
},
}
for _, tc := range testCases {