MM-16477 Add api to get users modified since a given time (#11406)

* MM-16477 Add api to get users modified since a given time

* Address feedback
Этот коммит содержится в:
Harrison Healey
2019-06-27 09:37:03 -04:00
коммит произвёл GitHub
родитель 89a41dc381
Коммит 4b96437370
12 изменённых файлов: 176 добавлений и 47 удалений

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

@@ -15,6 +15,7 @@ import (
"github.com/mattermost/mattermost-server/app"
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
"github.com/mattermost/mattermost-server/utils"
)
@@ -615,13 +616,29 @@ func getUsersByIds(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
sinceString := r.URL.Query().Get("since")
options := &store.UserGetByIdsOpts{
IsAdmin: c.IsSystemAdmin(),
}
if len(sinceString) > 0 {
since, parseError := strconv.ParseInt(sinceString, 10, 64)
if parseError != nil {
c.SetInvalidParam("since")
return
}
options.Since = since
}
restrictions, err := c.App.GetViewUsersRestrictions(c.App.Session.UserId)
if err != nil {
c.Err = err
return
}
options.ViewRestrictions = restrictions
users, err := c.App.GetUsersByIds(userIds, c.IsSystemAdmin(), restrictions)
users, err := c.App.GetUsersByIds(userIds, options)
if err != nil {
c.Err = err
return

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

@@ -1099,32 +1099,71 @@ func TestGetUsersByIds(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
users, resp := th.Client.GetUsersByIds([]string{th.BasicUser.Id})
CheckNoError(t, resp)
t.Run("should return the user", func(t *testing.T) {
users, resp := th.Client.GetUsersByIds([]string{th.BasicUser.Id})
if users[0].Id != th.BasicUser.Id {
t.Fatal("returned wrong user")
}
CheckUserSanitization(t, users[0])
CheckNoError(t, resp)
_, resp = th.Client.GetUsersByIds([]string{})
CheckBadRequestStatus(t, resp)
assert.Equal(t, th.BasicUser.Id, users[0].Id)
CheckUserSanitization(t, users[0])
})
users, resp = th.Client.GetUsersByIds([]string{"junk"})
CheckNoError(t, resp)
if len(users) > 0 {
t.Fatal("no users should be returned")
}
t.Run("should return error when no IDs are specified", func(t *testing.T) {
_, resp := th.Client.GetUsersByIds([]string{})
users, resp = th.Client.GetUsersByIds([]string{"junk", th.BasicUser.Id})
CheckNoError(t, resp)
if len(users) != 1 {
t.Fatal("1 user should be returned")
}
CheckBadRequestStatus(t, resp)
})
th.Client.Logout()
_, resp = th.Client.GetUsersByIds([]string{th.BasicUser.Id})
CheckUnauthorizedStatus(t, resp)
t.Run("should not return an error for invalid IDs", func(t *testing.T) {
users, resp := th.Client.GetUsersByIds([]string{"junk"})
CheckNoError(t, resp)
if len(users) > 0 {
t.Fatal("no users should be returned")
}
})
t.Run("should still return users for valid IDs when invalid IDs are specified", func(t *testing.T) {
users, resp := th.Client.GetUsersByIds([]string{"junk", th.BasicUser.Id})
CheckNoError(t, resp)
if len(users) != 1 {
t.Fatal("1 user should be returned")
}
})
t.Run("should return error when not logged in", func(t *testing.T) {
th.Client.Logout()
_, resp := th.Client.GetUsersByIds([]string{th.BasicUser.Id})
CheckUnauthorizedStatus(t, resp)
})
}
func TestGetUsersByIdsWithOptions(t *testing.T) {
t.Run("should only return specified users that have been updated since the given time", func(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
// Users before the timestamp shouldn't be returned
user1, err := th.App.CreateUser(&model.User{Email: th.GenerateTestEmail(), Username: model.NewId(), Password: model.NewId()})
require.Nil(t, err)
user2, err := th.App.CreateUser(&model.User{Email: th.GenerateTestEmail(), Username: model.NewId(), Password: model.NewId()})
require.Nil(t, err)
// Users not in the list of IDs shouldn't be returned
_, err = th.App.CreateUser(&model.User{Email: th.GenerateTestEmail(), Username: model.NewId(), Password: model.NewId()})
require.Nil(t, err)
users, resp := th.Client.GetUsersByIdsWithOptions([]string{user1.Id, user2.Id}, &model.UserGetByIdsOptions{
Since: user2.UpdateAt - 1,
})
assert.Nil(t, resp.Error)
assert.Len(t, users, 1)
assert.Equal(t, users[0].Id, user2.Id)
})
}
func TestGetUsersByGroupChannelIds(t *testing.T) {