diff --git a/api4/user_local.go b/api4/user_local.go index 58f2c94c90..ca8051c8bd 100644 --- a/api4/user_local.go +++ b/api4/user_local.go @@ -28,6 +28,8 @@ func (api *API) InitUserLocal() { api.BaseRoutes.User.Handle("/password", api.ApiLocal(updatePassword)).Methods("PUT") api.BaseRoutes.User.Handle("/convert_to_bot", api.ApiLocal(convertUserToBot)).Methods("POST") api.BaseRoutes.User.Handle("/email/verify/member", api.ApiLocal(verifyUserEmailWithoutToken)).Methods("POST") + api.BaseRoutes.User.Handle("/promote", api.ApiLocal(promoteGuestToUser)).Methods("POST") + api.BaseRoutes.User.Handle("/demote", api.ApiLocal(demoteUserToGuest)).Methods("POST") api.BaseRoutes.UserByUsername.Handle("", api.ApiLocal(localGetUserByUsername)).Methods("GET") api.BaseRoutes.UserByEmail.Handle("", api.ApiLocal(localGetUserByEmail)).Methods("GET") diff --git a/api4/user_test.go b/api4/user_test.go index f1733bd7f0..9761f4faba 100644 --- a/api4/user_test.go +++ b/api4/user_test.go @@ -4911,12 +4911,30 @@ func TestLoginLockout(t *testing.T) { } func TestDemoteUserToGuest(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + enableGuestAccounts := *th.App.Config().GuestAccountsSettings.Enable + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = enableGuestAccounts }) + th.App.Srv().RemoveLicense() + }() + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = true }) + th.App.Srv().SetLicense(model.NewTestLicense()) + + user := th.BasicUser + + th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { + _, respErr := c.GetUser(user.Id, "") + CheckNoError(t, respErr) + + _, respErr = c.DemoteUserToGuest(user.Id) + CheckNoError(t, respErr) + + defer require.Nil(t, th.App.PromoteGuestToUser(user, "")) + }, "demote a user to guest") + t.Run("websocket update user event", func(t *testing.T) { - th := Setup(t).InitBasic() - defer th.TearDown() - - user := th.BasicUser - webSocketClient, err := th.CreateWebSocketClient() assert.Nil(t, err) defer webSocketClient.Close() @@ -4937,17 +4955,11 @@ func TestDemoteUserToGuest(t *testing.T) { 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.Srv().RemoveLicense() - }() - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = true }) - th.App.Srv().SetLicense(model.NewTestLicense()) _, respErr := th.SystemAdminClient.GetUser(user.Id, "") CheckNoError(t, respErr) _, respErr = th.SystemAdminClient.DemoteUserToGuest(user.Id) CheckNoError(t, respErr) + defer th.SystemAdminClient.PromoteGuestToUser(user.Id) assertExpectedWebsocketEvent(t, webSocketClient, model.WEBSOCKET_EVENT_USER_UPDATED, func(event *model.WebSocketEvent) { eventUser, ok := event.GetData()["user"].(*model.User) @@ -4963,13 +4975,31 @@ func TestDemoteUserToGuest(t *testing.T) { } func TestPromoteGuestToUser(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + enableGuestAccounts := *th.App.Config().GuestAccountsSettings.Enable + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = enableGuestAccounts }) + th.App.Srv().RemoveLicense() + }() + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = true }) + th.App.Srv().SetLicense(model.NewTestLicense()) + + user := th.BasicUser + th.App.UpdateUserRoles(user.Id, model.SYSTEM_GUEST_ROLE_ID, false) + + th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { + _, respErr := c.GetUser(user.Id, "") + CheckNoError(t, respErr) + + _, respErr = c.PromoteGuestToUser(user.Id) + CheckNoError(t, respErr) + + defer require.Nil(t, th.App.DemoteUserToGuest(user)) + }, "promete a guest to user") + t.Run("websocket update user event", func(t *testing.T) { - th := Setup(t).InitBasic() - 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() @@ -4990,17 +5020,11 @@ func TestPromoteGuestToUser(t *testing.T) { 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.Srv().RemoveLicense() - }() - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = true }) - th.App.Srv().SetLicense(model.NewTestLicense()) _, respErr := th.SystemAdminClient.GetUser(user.Id, "") CheckNoError(t, respErr) _, respErr = th.SystemAdminClient.PromoteGuestToUser(user.Id) CheckNoError(t, respErr) + defer th.SystemAdminClient.DemoteUserToGuest(user.Id) assertExpectedWebsocketEvent(t, webSocketClient, model.WEBSOCKET_EVENT_USER_UPDATED, func(event *model.WebSocketEvent) { eventUser, ok := event.GetData()["user"].(*model.User)