From 11059b02511d3b53954edf7f7feb873546b3bfc5 Mon Sep 17 00:00:00 2001 From: akhilanandbv003 Date: Wed, 12 Dec 2018 09:18:41 -0600 Subject: [PATCH] Gh 9610 add plugin api for update user active method (#9854) * wip * wip * After running make plugin-mocks * Add TestUpdateUserActive and run make pluginapi * Adding plugin_api_test.go * Better formatting of code using gofmt * Fix tests and run make pluginapi * Specify the minimum server version on the comments * Include more tests as per the CR * Fix tests * Checking err.Id intsead of err & Removed comments and trailing spaces * wip * Fix tests as per CR and spaces * Make changes to tests as per CR --- app/plugin_api.go | 4 ++++ app/plugin_api_test.go | 27 +++++++++++++++++++++++++++ app/user.go | 13 +++++++++++++ app/user_test.go | 18 ++++++++++++++++++ plugin/api.go | 5 +++++ plugin/client_rpc_generated.go | 29 +++++++++++++++++++++++++++++ plugin/plugintest/api.go | 16 ++++++++++++++++ 7 files changed, 112 insertions(+) diff --git a/app/plugin_api.go b/app/plugin_api.go index 79b0126bfc..51fd2de89f 100644 --- a/app/plugin_api.go +++ b/app/plugin_api.go @@ -195,6 +195,10 @@ func (api *PluginAPI) UpdateUser(user *model.User) (*model.User, *model.AppError return api.app.UpdateUser(user, true) } +func (api *PluginAPI) UpdateUserActive(userId string, active bool) *model.AppError { + return api.app.UpdateUserActive(userId, active) +} + func (api *PluginAPI) GetUserStatus(userId string) (*model.Status, *model.AppError) { return api.app.GetStatus(userId) } diff --git a/app/plugin_api_test.go b/app/plugin_api_test.go index afb5ca46e8..1b9977f35d 100644 --- a/app/plugin_api_test.go +++ b/app/plugin_api_test.go @@ -620,6 +620,33 @@ func TestPluginAPIRemoveTeamIcon(t *testing.T) { require.Nil(t, err) } +func TestPluginAPIUpdateUserActive(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + api := th.SetupPluginAPI() + + err := api.UpdateUserActive(th.BasicUser.Id, true) + require.Nil(t, err) + user, err := api.GetUser(th.BasicUser.Id) + require.Nil(t, err) + require.Equal(t, int64(0), user.DeleteAt) + + err = api.UpdateUserActive(th.BasicUser.Id, false) + require.Nil(t, err) + user, err = api.GetUser(th.BasicUser.Id) + require.Nil(t, err) + require.NotNil(t, user) + require.NotEqual(t, int64(0), user.DeleteAt) + + err = api.UpdateUserActive(th.BasicUser.Id, true) + require.Nil(t, err) + err = api.UpdateUserActive(th.BasicUser.Id, true) + require.Nil(t, err) + user, err = api.GetUser(th.BasicUser.Id) + require.Nil(t, err) + require.Equal(t, int64(0), user.DeleteAt) +} + func TestPluginAPIGetDirectChannel(t *testing.T) { th := Setup().InitBasic() defer th.TearDown() diff --git a/app/user.go b/app/user.go index d0e7b56935..4f8a89e457 100644 --- a/app/user.go +++ b/app/user.go @@ -1056,6 +1056,19 @@ func (a *App) UpdateUser(user *model.User, sendNotifications bool) (*model.User, return rusers[0], nil } +func (a *App) UpdateUserActive(userId string, active bool) *model.AppError { + user, err := a.GetUser(userId) + + if err != nil { + return err + } + if _, err = a.UpdateActive(user, active); err != nil { + return err + } + + return nil +} + func (a *App) UpdateUserNotifyProps(userId string, props map[string]string) (*model.User, *model.AppError) { user, err := a.GetUser(userId) if err != nil { diff --git a/app/user_test.go b/app/user_test.go index 2aeed6fc84..99020dce92 100644 --- a/app/user_test.go +++ b/app/user_test.go @@ -154,6 +154,24 @@ func TestUpdateUserToRestrictedDomain(t *testing.T) { assert.False(t, err == nil) } +func TestUpdateUserActive(t *testing.T) { + th := Setup() + defer th.TearDown() + + user := th.CreateUser() + + EnableUserDeactivation := th.App.Config().TeamSettings.EnableUserDeactivation + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableUserDeactivation = EnableUserDeactivation }) + }() + + th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.TeamSettings.EnableUserDeactivation = true + }) + err := th.App.UpdateUserActive(user.Id, false) + assert.Nil(t, err) +} + func TestUpdateOAuthUserAttrs(t *testing.T) { th := Setup() defer th.TearDown() diff --git a/plugin/api.go b/plugin/api.go index 0f166a0429..cff931c65d 100644 --- a/plugin/api.go +++ b/plugin/api.go @@ -102,6 +102,11 @@ type API interface { // The status parameter can be: "online", "away", "dnd", or "offline". UpdateUserStatus(userId, status string) (*model.Status, *model.AppError) + // UpdateUserActive deactivates or reactivates an user. + // + // Minimum server version: 5.8 + UpdateUserActive(userId string, active bool) *model.AppError + // GetUsersInChannel returns a page of users in a channel. Page counting starts at 0. // The sortBy parameter can be: "username" or "status". // diff --git a/plugin/client_rpc_generated.go b/plugin/client_rpc_generated.go index 1209456572..9a5c831454 100644 --- a/plugin/client_rpc_generated.go +++ b/plugin/client_rpc_generated.go @@ -1145,6 +1145,35 @@ func (s *apiRPCServer) UpdateUserStatus(args *Z_UpdateUserStatusArgs, returns *Z return nil } +type Z_UpdateUserActiveArgs struct { + A string + B bool +} + +type Z_UpdateUserActiveReturns struct { + A *model.AppError +} + +func (g *apiRPCClient) UpdateUserActive(userId string, active bool) *model.AppError { + _args := &Z_UpdateUserActiveArgs{userId, active} + _returns := &Z_UpdateUserActiveReturns{} + if err := g.client.Call("Plugin.UpdateUserActive", _args, _returns); err != nil { + log.Printf("RPC call to UpdateUserActive API failed: %s", err.Error()) + } + return _returns.A +} + +func (s *apiRPCServer) UpdateUserActive(args *Z_UpdateUserActiveArgs, returns *Z_UpdateUserActiveReturns) error { + if hook, ok := s.impl.(interface { + UpdateUserActive(userId string, active bool) *model.AppError + }); ok { + returns.A = hook.UpdateUserActive(args.A, args.B) + } else { + return encodableError(fmt.Errorf("API UpdateUserActive called but not implemented.")) + } + return nil +} + type Z_GetUsersInChannelArgs struct { A string B string diff --git a/plugin/plugintest/api.go b/plugin/plugintest/api.go index 5fee237235..04e284f34a 100644 --- a/plugin/plugintest/api.go +++ b/plugin/plugintest/api.go @@ -2170,6 +2170,22 @@ func (_m *API) UpdateUser(user *model.User) (*model.User, *model.AppError) { return r0, r1 } +// UpdateUserActive provides a mock function with given fields: userId, active +func (_m *API) UpdateUserActive(userId string, active bool) *model.AppError { + ret := _m.Called(userId, active) + + var r0 *model.AppError + if rf, ok := ret.Get(0).(func(string, bool) *model.AppError); ok { + r0 = rf(userId, active) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*model.AppError) + } + } + + return r0 +} + // UpdateUserStatus provides a mock function with given fields: userId, status func (_m *API) UpdateUserStatus(userId string, status string) (*model.Status, *model.AppError) { ret := _m.Called(userId, status)