* Add support for timed DND status - accept a date time value in api query when dnd mode for user needs to be unset - Create a new function to handle SetDNDStatus calls - Create a scheduled task to unset dnd mode to wahtever mode was before setting it to DND * update schema version * Model changes to make fields more intuitive - move dndendtime to status model - add new field prev status in status to keep track of previous status of user - update db migration function - make use of prevstatus and dndendtime from status model * set prev status and dndendtime appropriately after unsetting dnd mode * add json tag for dndendtime * unset dnd status only if not changed manually by user * update dnd statuses after server restart * make app-layers * fix failing tests * don't create sched task when setting status to DND * get only expired statuses from db - convert end time from any timezone to utc - store dnd end time in unix format for usability reasons * run update dnd status only on leader * make mocks * fix tests * run UpdateDNDStatusOfUsers as recurring task * save all statuses at once in db and update UpdateDNDStatusOfUsers logic * add app method to get timezone of user * store dnd end time in context.Params * set max size of prevstatus * update status model to take endtime input as string and store in db as unix time(int64) * Add tests for SetStatusDoNotDisturbTimed * if dnd_end_time is not passed the call old api to set dnd mode * fix tests * new plugin api to use new timed dnd mode * get and update rows in a single db query * dnd end time will be stored in request body and not route param * exclude statuses which has dndendtimeunix < 0 * update and get the updated dnd statuses in single db query * add updated status to cache * DNDEndTimeUnix and PrevStatus need not to be visible to users * update db schema version for migration * Keep Status and PrevStatus varchar size same * add test to verify status is restored after dnd end time expires * expect endtime in utc from client - remove store method GetTimezone as no longer needed - add documentation for SetStatusDoNotDisturbTimed * reduce sleep time for dnd timed restore test * more appropriate name for new api to update user status * update db migration function * parse and validate time before potentially triggering db query to get status of user * add migration changes in to existing upgrade function * not supporting un-timed dnd status via api * don't call Srv.Store directly, call via app layer * rename dndendtime to statuscleartime to make it suitable for custom status usage as well * Revert "rename dndendtime to statuscleartime to make it suitable for custom status usage as well" This reverts commit fa69152d9a3db18f1c59b34c878fb7ce494440b5. * mysql doesn't support RETURNING clause so add tx to get and update statuses * add UpdateDNDStatusOfUsers mock in tests * update store mock import path * add mock in storelib * Add status mocks to empty store * Close the task during server shutdown * Do not cancel a nil task * update squirrel queries * remove untimed dnd test * start recurring task to unset statuses on leadership change * set dndTask to nil after cancelling it upon server shutdown * new recurring task which starts at nearest rounded time of the interval * mock Get() call for status * return updated statuses in case of mysql * remove unneccessary code * add Get() mock to empty store * fix mocking for once and all * address review comments fix mysql updateStatus fn protect dndTask with mutex minor refactors * move runDNDStatusExpireJob to server.go and pass App as arg instead of method receiver * frontend will send endtime in unix epoch format so get rid of double representation * scan for all fields and not just two * add some tests and fix review comments * remove extra sql query and create needed result in go * add storetest for UpdateExpiredDNDStatuses * add migrations to latest version * update min supported version * add comment to fix a bug in future * update test to expect 1 status in return * rename UpdateUserStatusWithDNDTimeout to SetUserStatusTimedDND * rename DNDEndTimeUnix to DNDEndTime * cast int to int64 for equality * fix tests and error handling * move updating values to retrieved statuses fields outside sql transaction * move migrations to 5.36 Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
236 строки
8.0 KiB
Go
236 строки
8.0 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package api4
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/mattermost/mattermost-server/v5/model"
|
|
)
|
|
|
|
func TestGetUserStatus(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
Client := th.Client
|
|
|
|
t.Run("offline status", func(t *testing.T) {
|
|
userStatus, resp := Client.GetUserStatus(th.BasicUser.Id, "")
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "offline", userStatus.Status)
|
|
})
|
|
|
|
t.Run("online status", func(t *testing.T) {
|
|
th.App.SetStatusOnline(th.BasicUser.Id, true)
|
|
userStatus, resp := Client.GetUserStatus(th.BasicUser.Id, "")
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "online", userStatus.Status)
|
|
})
|
|
|
|
t.Run("away status", func(t *testing.T) {
|
|
th.App.SetStatusAwayIfNeeded(th.BasicUser.Id, true)
|
|
userStatus, resp := Client.GetUserStatus(th.BasicUser.Id, "")
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "away", userStatus.Status)
|
|
})
|
|
|
|
t.Run("dnd status", func(t *testing.T) {
|
|
th.App.SetStatusDoNotDisturb(th.BasicUser.Id)
|
|
userStatus, resp := Client.GetUserStatus(th.BasicUser.Id, "")
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "dnd", userStatus.Status)
|
|
})
|
|
|
|
t.Run("dnd status timed", func(t *testing.T) {
|
|
th.App.SetStatusDoNotDisturbTimed(th.BasicUser.Id, time.Now().Add(10*time.Minute).Unix())
|
|
userStatus, resp := Client.GetUserStatus(th.BasicUser.Id, "")
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "dnd", userStatus.Status)
|
|
})
|
|
|
|
t.Run("dnd status timed restore after time interval", func(t *testing.T) {
|
|
task := model.CreateRecurringTaskFromNextIntervalTime("Unset DND Statuses From Test", th.App.UpdateDNDStatusOfUsers, 1*time.Second)
|
|
defer task.Cancel()
|
|
th.App.SetStatusOnline(th.BasicUser.Id, true)
|
|
userStatus, resp := Client.GetUserStatus(th.BasicUser.Id, "")
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "online", userStatus.Status)
|
|
th.App.SetStatusDoNotDisturbTimed(th.BasicUser.Id, time.Now().Add(2*time.Second).Unix())
|
|
userStatus, resp = Client.GetUserStatus(th.BasicUser.Id, "")
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "dnd", userStatus.Status)
|
|
time.Sleep(3 * time.Second)
|
|
userStatus, resp = Client.GetUserStatus(th.BasicUser.Id, "")
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "online", userStatus.Status)
|
|
})
|
|
|
|
t.Run("back to offline status", func(t *testing.T) {
|
|
th.App.SetStatusOffline(th.BasicUser.Id, true)
|
|
userStatus, resp := Client.GetUserStatus(th.BasicUser.Id, "")
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "offline", userStatus.Status)
|
|
})
|
|
|
|
t.Run("get other user status", func(t *testing.T) {
|
|
//Get user2 status logged as user1
|
|
userStatus, resp := Client.GetUserStatus(th.BasicUser2.Id, "")
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "offline", userStatus.Status)
|
|
})
|
|
|
|
t.Run("get status from logged out user", func(t *testing.T) {
|
|
Client.Logout()
|
|
_, resp := Client.GetUserStatus(th.BasicUser2.Id, "")
|
|
CheckUnauthorizedStatus(t, resp)
|
|
})
|
|
|
|
t.Run("get status from other user", func(t *testing.T) {
|
|
th.LoginBasic2()
|
|
userStatus, resp := Client.GetUserStatus(th.BasicUser2.Id, "")
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "offline", userStatus.Status)
|
|
})
|
|
}
|
|
|
|
func TestGetUsersStatusesByIds(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
Client := th.Client
|
|
|
|
usersIds := []string{th.BasicUser.Id, th.BasicUser2.Id}
|
|
|
|
t.Run("empty userIds list", func(t *testing.T) {
|
|
_, resp := Client.GetUsersStatusesByIds([]string{})
|
|
CheckBadRequestStatus(t, resp)
|
|
})
|
|
|
|
t.Run("completely invalid userIds list", func(t *testing.T) {
|
|
_, resp := Client.GetUsersStatusesByIds([]string{"invalid_user_id", "invalid_user_id"})
|
|
CheckBadRequestStatus(t, resp)
|
|
})
|
|
|
|
t.Run("partly invalid userIds list", func(t *testing.T) {
|
|
_, resp := Client.GetUsersStatusesByIds([]string{th.BasicUser.Id, "invalid_user_id"})
|
|
CheckBadRequestStatus(t, resp)
|
|
})
|
|
|
|
t.Run("offline status", func(t *testing.T) {
|
|
usersStatuses, resp := Client.GetUsersStatusesByIds(usersIds)
|
|
CheckNoError(t, resp)
|
|
for _, userStatus := range usersStatuses {
|
|
assert.Equal(t, "offline", userStatus.Status)
|
|
}
|
|
})
|
|
|
|
t.Run("online status", func(t *testing.T) {
|
|
th.App.SetStatusOnline(th.BasicUser.Id, true)
|
|
th.App.SetStatusOnline(th.BasicUser2.Id, true)
|
|
usersStatuses, resp := Client.GetUsersStatusesByIds(usersIds)
|
|
CheckNoError(t, resp)
|
|
for _, userStatus := range usersStatuses {
|
|
assert.Equal(t, "online", userStatus.Status)
|
|
}
|
|
})
|
|
|
|
t.Run("away status", func(t *testing.T) {
|
|
th.App.SetStatusAwayIfNeeded(th.BasicUser.Id, true)
|
|
th.App.SetStatusAwayIfNeeded(th.BasicUser2.Id, true)
|
|
usersStatuses, resp := Client.GetUsersStatusesByIds(usersIds)
|
|
CheckNoError(t, resp)
|
|
for _, userStatus := range usersStatuses {
|
|
assert.Equal(t, "away", userStatus.Status)
|
|
}
|
|
})
|
|
|
|
t.Run("dnd status", func(t *testing.T) {
|
|
th.App.SetStatusDoNotDisturb(th.BasicUser.Id)
|
|
th.App.SetStatusDoNotDisturb(th.BasicUser2.Id)
|
|
usersStatuses, resp := Client.GetUsersStatusesByIds(usersIds)
|
|
CheckNoError(t, resp)
|
|
for _, userStatus := range usersStatuses {
|
|
assert.Equal(t, "dnd", userStatus.Status)
|
|
}
|
|
})
|
|
|
|
t.Run("dnd status", func(t *testing.T) {
|
|
th.App.SetStatusDoNotDisturbTimed(th.BasicUser.Id, time.Now().Add(10*time.Minute).Unix())
|
|
th.App.SetStatusDoNotDisturbTimed(th.BasicUser2.Id, time.Now().Add(15*time.Minute).Unix())
|
|
usersStatuses, resp := Client.GetUsersStatusesByIds(usersIds)
|
|
CheckNoError(t, resp)
|
|
for _, userStatus := range usersStatuses {
|
|
assert.Equal(t, "dnd", userStatus.Status)
|
|
}
|
|
})
|
|
|
|
t.Run("get statuses from logged out user", func(t *testing.T) {
|
|
Client.Logout()
|
|
|
|
_, resp := Client.GetUsersStatusesByIds(usersIds)
|
|
CheckUnauthorizedStatus(t, resp)
|
|
})
|
|
}
|
|
|
|
func TestUpdateUserStatus(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
Client := th.Client
|
|
|
|
t.Run("set online status", func(t *testing.T) {
|
|
toUpdateUserStatus := &model.Status{Status: "online", UserId: th.BasicUser.Id}
|
|
updateUserStatus, resp := Client.UpdateUserStatus(th.BasicUser.Id, toUpdateUserStatus)
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "online", updateUserStatus.Status)
|
|
})
|
|
|
|
t.Run("set away status", func(t *testing.T) {
|
|
toUpdateUserStatus := &model.Status{Status: "away", UserId: th.BasicUser.Id}
|
|
updateUserStatus, resp := Client.UpdateUserStatus(th.BasicUser.Id, toUpdateUserStatus)
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "away", updateUserStatus.Status)
|
|
})
|
|
|
|
t.Run("set dnd status timed", func(t *testing.T) {
|
|
toUpdateUserStatus := &model.Status{Status: "dnd", UserId: th.BasicUser.Id, DNDEndTime: time.Now().Add(10 * time.Minute).Unix()}
|
|
updateUserStatus, resp := Client.UpdateUserStatus(th.BasicUser.Id, toUpdateUserStatus)
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "dnd", updateUserStatus.Status)
|
|
})
|
|
|
|
t.Run("set offline status", func(t *testing.T) {
|
|
toUpdateUserStatus := &model.Status{Status: "offline", UserId: th.BasicUser.Id}
|
|
updateUserStatus, resp := Client.UpdateUserStatus(th.BasicUser.Id, toUpdateUserStatus)
|
|
CheckNoError(t, resp)
|
|
assert.Equal(t, "offline", updateUserStatus.Status)
|
|
})
|
|
|
|
t.Run("set status for other user as regular user", func(t *testing.T) {
|
|
toUpdateUserStatus := &model.Status{Status: "online", UserId: th.BasicUser2.Id}
|
|
_, resp := Client.UpdateUserStatus(th.BasicUser2.Id, toUpdateUserStatus)
|
|
CheckForbiddenStatus(t, resp)
|
|
})
|
|
|
|
t.Run("set status for other user as admin user", func(t *testing.T) {
|
|
toUpdateUserStatus := &model.Status{Status: "online", UserId: th.BasicUser2.Id}
|
|
updateUserStatus, _ := th.SystemAdminClient.UpdateUserStatus(th.BasicUser2.Id, toUpdateUserStatus)
|
|
assert.Equal(t, "online", updateUserStatus.Status)
|
|
})
|
|
|
|
t.Run("not matching status user id and the user id passed in the function", func(t *testing.T) {
|
|
toUpdateUserStatus := &model.Status{Status: "online", UserId: th.BasicUser2.Id}
|
|
_, resp := Client.UpdateUserStatus(th.BasicUser.Id, toUpdateUserStatus)
|
|
CheckBadRequestStatus(t, resp)
|
|
})
|
|
|
|
t.Run("get statuses from logged out user", func(t *testing.T) {
|
|
toUpdateUserStatus := &model.Status{Status: "online", UserId: th.BasicUser2.Id}
|
|
Client.Logout()
|
|
|
|
_, resp := Client.UpdateUserStatus(th.BasicUser2.Id, toUpdateUserStatus)
|
|
CheckUnauthorizedStatus(t, resp)
|
|
})
|
|
}
|