User Management > User role change not reflected in the UI without a refresh (#13683)

Этот коммит содержится в:
Mario de Frutos Dieguez
2020-01-23 19:30:13 +01:00
коммит произвёл GitHub
родитель 90c0479c2f
Коммит 0581ff7cb8
2 изменённых файлов: 114 добавлений и 2 удалений

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

@@ -15,6 +15,7 @@ import (
"github.com/mattermost/mattermost-server/v5/app"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/services/mailservice"
"github.com/mattermost/mattermost-server/v5/store/localcachelayer"
"github.com/mattermost/mattermost-server/v5/utils/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -4341,3 +4342,114 @@ func TestLoginLockout(t *testing.T) {
_, resp = th.Client.Login(th.BasicUser2.Email, th.BasicUser2.Password)
CheckErrorMessage(t, resp, "api.user.check_user_login_attempts.too_many.app_error")
}
func TestDemoteUserToGuest(t *testing.T) {
t.Run("websocket update user event", func(t *testing.T) {
th := Setup().InitBasic()
th.Server.Store = localcachelayer.NewLocalCacheLayer(th.Server.Store,
th.Server.Metrics, th.Server.Cluster, th.Server.CacheProvider)
defer th.TearDown()
user := th.BasicUser
webSocketClient, err := th.CreateWebSocketClient()
assert.Nil(t, err)
defer webSocketClient.Close()
webSocketClient.Listen()
time.Sleep(300 * time.Millisecond)
resp := <-webSocketClient.ResponseChannel
require.Equal(t, model.STATUS_OK, resp.Status)
adminWebSocketClient, err := th.CreateWebSocketSystemAdminClient()
assert.Nil(t, err)
defer adminWebSocketClient.Close()
adminWebSocketClient.Listen()
time.Sleep(300 * time.Millisecond)
resp = <-adminWebSocketClient.ResponseChannel
require.Equal(t, model.STATUS_OK, resp.Status)
enableGuestAccounts := *th.App.Config().GuestAccountsSettings.Enable
defer func() {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = enableGuestAccounts })
th.App.RemoveLicense()
}()
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = true })
th.App.SetLicense(model.NewTestLicense())
_, respErr := th.SystemAdminClient.GetUser(user.Id, "")
CheckNoError(t, respErr)
_, respErr = th.SystemAdminClient.DemoteUserToGuest(user.Id)
CheckNoError(t, respErr)
assertExpectedWebsocketEvent(t, webSocketClient, model.WEBSOCKET_EVENT_USER_UPDATED, func(event *model.WebSocketEvent) {
eventUser, ok := event.GetData()["user"].(*model.User)
require.True(t, ok, "expected user")
assert.Equal(t, "system_guest", eventUser.Roles)
})
assertExpectedWebsocketEvent(t, adminWebSocketClient, model.WEBSOCKET_EVENT_USER_UPDATED, func(event *model.WebSocketEvent) {
eventUser, ok := event.GetData()["user"].(*model.User)
require.True(t, ok, "expected user")
assert.Equal(t, "system_guest", eventUser.Roles)
})
th.App.InvalidateAllCaches()
})
}
func TestPromoteGuestToUser(t *testing.T) {
t.Run("websocket update user event", func(t *testing.T) {
th := Setup().InitBasic()
th.Server.Store = localcachelayer.NewLocalCacheLayer(th.Server.Store,
th.Server.Metrics, th.Server.Cluster, th.Server.CacheProvider)
defer th.TearDown()
user := th.BasicUser
th.App.UpdateUserRoles(user.Id, model.SYSTEM_GUEST_ROLE_ID, false)
webSocketClient, err := th.CreateWebSocketClient()
assert.Nil(t, err)
defer webSocketClient.Close()
webSocketClient.Listen()
time.Sleep(300 * time.Millisecond)
resp := <-webSocketClient.ResponseChannel
require.Equal(t, model.STATUS_OK, resp.Status)
adminWebSocketClient, err := th.CreateWebSocketSystemAdminClient()
assert.Nil(t, err)
defer adminWebSocketClient.Close()
adminWebSocketClient.Listen()
time.Sleep(300 * time.Millisecond)
resp = <-adminWebSocketClient.ResponseChannel
require.Equal(t, model.STATUS_OK, resp.Status)
enableGuestAccounts := *th.App.Config().GuestAccountsSettings.Enable
defer func() {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = enableGuestAccounts })
th.App.RemoveLicense()
}()
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = true })
th.App.SetLicense(model.NewTestLicense())
_, respErr := th.SystemAdminClient.GetUser(user.Id, "")
CheckNoError(t, respErr)
_, respErr = th.SystemAdminClient.PromoteGuestToUser(user.Id)
CheckNoError(t, respErr)
assertExpectedWebsocketEvent(t, webSocketClient, model.WEBSOCKET_EVENT_USER_UPDATED, func(event *model.WebSocketEvent) {
eventUser, ok := event.GetData()["user"].(*model.User)
require.True(t, ok, "expected user")
assert.Equal(t, "system_user", eventUser.Roles)
})
assertExpectedWebsocketEvent(t, adminWebSocketClient, model.WEBSOCKET_EVENT_USER_UPDATED, func(event *model.WebSocketEvent) {
eventUser, ok := event.GetData()["user"].(*model.User)
require.True(t, ok, "expected user")
assert.Equal(t, "system_user", eventUser.Roles)
})
th.App.InvalidateAllCaches()
})
}

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

@@ -2269,6 +2269,7 @@ func (a *App) getListOfAllowedChannelsForTeam(teamId string, viewRestrictions *m
// guest roles to regular user roles.
func (a *App) PromoteGuestToUser(user *model.User, requestorId string) *model.AppError {
err := a.Srv.Store.User().PromoteGuestToUser(user.Id)
a.InvalidateCacheForUser(user.Id)
if err != nil {
return err
}
@@ -2314,7 +2315,6 @@ func (a *App) PromoteGuestToUser(user *model.User, requestorId string) *model.Ap
}
}
a.InvalidateCacheForUser(user.Id)
a.ClearSessionCacheForUser(user.Id)
return nil
}
@@ -2323,6 +2323,7 @@ func (a *App) PromoteGuestToUser(user *model.User, requestorId string) *model.Ap
// regular user roles to guest roles.
func (a *App) DemoteUserToGuest(user *model.User) *model.AppError {
err := a.Srv.Store.User().DemoteUserToGuest(user.Id)
a.InvalidateCacheForUser(user.Id)
if err != nil {
return err
}
@@ -2357,7 +2358,6 @@ func (a *App) DemoteUserToGuest(user *model.User) *model.AppError {
}
}
a.InvalidateCacheForUser(user.Id)
a.ClearSessionCacheForUser(user.Id)
return nil