[MM-21464] api4/channel: add ability to force move channel by removing non-members (#14887)

* api4/channel: add ability to force move channel by removing non-members

* app/channel: add log to move channel for non taget team members

* app/channel: add tets for remove non-team members for channel

* Update api4/channel_test.go

Co-authored-by: Eli Yukelzon <reflog@gmail.com>

Co-authored-by: Eli Yukelzon <reflog@gmail.com>
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2020-07-16 11:26:19 +03:00
коммит произвёл GitHub
родитель b916bae0f7
Коммит bf664997a7
7 изменённых файлов: 111 добавлений и 11 удалений

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

@@ -1810,6 +1810,12 @@ func moveChannel(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
force, ok := props["force"].(bool)
if !ok {
c.SetInvalidParam("force")
return
}
team, err := c.App.GetTeam(teamId)
if err != nil {
c.Err = err
@@ -1845,6 +1851,14 @@ func moveChannel(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
if force {
err = c.App.RemoveUsersFromChannelNotMemberOfTeam(user, channel, team)
if err != nil {
c.Err = err
return
}
}
err = c.App.MoveChannel(team, channel, user)
if err != nil {
c.Err = err

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

@@ -3839,14 +3839,14 @@ func TestMoveChannel(t *testing.T) {
t.Run("Should move channel", func(t *testing.T) {
publicChannel := th.CreatePublicChannel()
ch, resp := th.SystemAdminClient.MoveChannel(publicChannel.Id, team2.Id)
ch, resp := th.SystemAdminClient.MoveChannel(publicChannel.Id, team2.Id, false)
require.Nil(t, resp.Error)
require.Equal(t, team2.Id, ch.TeamId)
})
t.Run("Should fail when trying to move a private channel", func(t *testing.T) {
channel := th.CreatePrivateChannel()
_, resp := Client.MoveChannel(channel.Id, team1.Id)
_, resp := Client.MoveChannel(channel.Id, team1.Id, false)
require.NotNil(t, resp.Error)
CheckErrorMessage(t, resp, "api.channel.move_channel.type.invalid")
})
@@ -3854,7 +3854,7 @@ func TestMoveChannel(t *testing.T) {
t.Run("Should fail when trying to move a DM channel", func(t *testing.T) {
user := th.CreateUser()
dmChannel := th.CreateDmChannel(user)
_, resp := Client.MoveChannel(dmChannel.Id, team1.Id)
_, resp := Client.MoveChannel(dmChannel.Id, team1.Id, false)
require.NotNil(t, resp.Error)
CheckErrorMessage(t, resp, "api.channel.move_channel.type.invalid")
})
@@ -3864,14 +3864,14 @@ func TestMoveChannel(t *testing.T) {
gmChannel, err := th.App.CreateGroupChannel([]string{th.BasicUser.Id, th.SystemAdminUser.Id, th.TeamAdminUser.Id}, user.Id)
require.Nil(t, err)
_, resp := Client.MoveChannel(gmChannel.Id, team1.Id)
_, resp := Client.MoveChannel(gmChannel.Id, team1.Id, false)
require.NotNil(t, resp.Error)
CheckErrorMessage(t, resp, "api.channel.move_channel.type.invalid")
})
t.Run("Should fail due to permissions", func(t *testing.T) {
publicChannel := th.CreatePublicChannel()
_, resp := Client.MoveChannel(publicChannel.Id, team1.Id)
_, resp := Client.MoveChannel(publicChannel.Id, team1.Id, false)
require.NotNil(t, resp.Error)
CheckErrorMessage(t, resp, "api.context.permissions.app_error")
})
@@ -3886,10 +3886,25 @@ func TestMoveChannel(t *testing.T) {
_, resp = th.SystemAdminClient.AddChannelMember(publicChannel.Id, user.Id)
CheckNoError(t, resp)
_, resp = th.SystemAdminClient.MoveChannel(publicChannel.Id, team2.Id)
_, resp = th.SystemAdminClient.MoveChannel(publicChannel.Id, team2.Id, false)
require.NotNil(t, resp.Error)
CheckErrorMessage(t, resp, "app.channel.move_channel.members_do_not_match.error")
})
t.Run("Should be able to (force) move channel by a member that is not member of target team", func(t *testing.T) {
publicChannel := th.CreatePublicChannel()
user := th.BasicUser
_, resp := th.SystemAdminClient.RemoveTeamMember(team2.Id, user.Id)
CheckNoError(t, resp)
_, resp = th.SystemAdminClient.AddChannelMember(publicChannel.Id, user.Id)
CheckNoError(t, resp)
newChannel, resp := th.SystemAdminClient.MoveChannel(publicChannel.Id, team2.Id, true)
require.Nil(t, resp.Error)
require.Equal(t, team2.Id, newChannel.TeamId)
})
}
func TestUpdateCategoryForTeamForUser(t *testing.T) {