[MM-21793] Allow bots to be added to group synced channels and teams (#13672)

* MM-21793: Allow bots to be removed and added from group synced teams and channels

* MM-21793: Add tests for adding and removing a team and channel members

* MM-21793 Add punctuation to comments and remove unnecessary variable
Этот коммит содержится в:
Farhan Munshi
2020-01-29 11:01:06 -05:00
коммит произвёл GitHub
родитель ffb3897c8c
Коммит fa769e46d7
10 изменённых файлов: 191 добавлений и 45 удалений

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

@@ -253,6 +253,26 @@ func (me *TestHelper) CreateWebSocketClientWithClient(client *model.Client4) (*m
return model.NewWebSocketClient4(fmt.Sprintf("ws://localhost:%v", me.App.Srv.ListenAddr.Port), client.AuthToken)
}
func (me *TestHelper) CreateBotWithSystemAdminClient() *model.Bot {
return me.CreateBotWithClient((me.SystemAdminClient))
}
func (me *TestHelper) CreateBotWithClient(client *model.Client4) *model.Bot {
bot := &model.Bot{
Username: GenerateTestUsername(),
DisplayName: "a bot",
Description: "bot",
}
utils.DisableDebugLogForTest()
rbot, resp := client.CreateBot(bot)
if resp.Error != nil {
panic(resp.Error)
}
utils.EnableDebugLogForTest()
return rbot
}
func (me *TestHelper) CreateUser() *model.User {
return me.CreateUserWithClient(me.Client)
}

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

@@ -1398,12 +1398,18 @@ func removeChannelMember(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
user, err := c.App.GetUser(c.Params.UserId)
if err != nil {
c.Err = err
return
}
if !(channel.Type == model.CHANNEL_OPEN || channel.Type == model.CHANNEL_PRIVATE) {
c.Err = model.NewAppError("removeChannelMember", "api.channel.remove_channel_member.type.app_error", nil, "", http.StatusBadRequest)
return
}
if channel.IsGroupConstrained() && (c.Params.UserId != c.App.Session.UserId) {
if channel.IsGroupConstrained() && (c.Params.UserId != c.App.Session.UserId) && !user.IsBot {
c.Err = model.NewAppError("removeChannelMember", "api.channel.remove_member.group_constrained.app_error", nil, "", http.StatusBadRequest)
return
}

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

@@ -2387,6 +2387,12 @@ func TestRemoveChannelMember(t *testing.T) {
defer th.TearDown()
Client := th.Client
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.EnableBotAccountCreation = true
})
bot := th.CreateBotWithSystemAdminClient()
th.App.AddUserToTeam(team.Id, bot.UserId, "")
pass, resp := Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser2.Id)
CheckNoError(t, resp)
require.True(t, pass, "should have passed")
@@ -2536,6 +2542,8 @@ func TestRemoveChannelMember(t *testing.T) {
CheckNoError(t, resp)
_, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id)
CheckNoError(t, resp)
_, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, bot.UserId)
CheckNoError(t, resp)
_, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id)
CheckForbiddenStatus(t, resp)
@@ -2564,6 +2572,10 @@ func TestRemoveChannelMember(t *testing.T) {
directChannel, resp := Client.CreateDirectChannel(user1.Id, user2.Id)
CheckNoError(t, resp)
// If the channel is group-constrained a user can remove a bot
_, resp = Client.RemoveUserFromChannel(privateChannel.Id, bot.UserId)
CheckNoError(t, resp)
_, resp = Client.RemoveUserFromChannel(directChannel.Id, user1.Id)
CheckBadRequestStatus(t, resp)

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

@@ -626,7 +626,13 @@ func removeTeamMember(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
if team.IsGroupConstrained() && (c.Params.UserId != c.App.Session.UserId) {
user, err := c.App.GetUser(c.Params.UserId)
if err != nil {
c.Err = err
return
}
if team.IsGroupConstrained() && (c.Params.UserId != c.App.Session.UserId) && !user.IsBot {
c.Err = model.NewAppError("removeTeamMember", "api.team.remove_member.group_constrained.app_error", nil, "", http.StatusBadRequest)
return
}

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

@@ -1721,6 +1721,11 @@ func TestAddTeamMembers(t *testing.T) {
otherUser.Id,
}
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.EnableBotAccountCreation = true
})
bot := th.CreateBotWithSystemAdminClient()
err := th.App.RemoveUserFromTeam(th.BasicTeam.Id, th.BasicUser2.Id, "")
require.Nil(t, err)
@@ -1813,6 +1818,10 @@ func TestAddTeamMembers(t *testing.T) {
_, resp = Client.AddTeamMembers(team.Id, userList)
CheckErrorMessage(t, resp, "api.team.add_members.user_denied")
// Ensure that a group synced team can still add bots
_, resp = Client.AddTeamMembers(team.Id, []string{bot.UserId})
CheckNoError(t, resp)
// Associate group to team
_, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{
GroupId: th.Group.Id,
@@ -1834,6 +1843,11 @@ func TestRemoveTeamMember(t *testing.T) {
defer th.TearDown()
Client := th.Client
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.EnableBotAccountCreation = true
})
bot := th.CreateBotWithSystemAdminClient()
pass, resp := Client.RemoveTeamMember(th.BasicTeam.Id, th.BasicUser.Id)
CheckNoError(t, resp)
@@ -1860,6 +1874,9 @@ func TestRemoveTeamMember(t *testing.T) {
_, resp = th.SystemAdminClient.AddTeamMember(th.BasicTeam.Id, th.SystemAdminUser.Id)
CheckNoError(t, resp)
_, resp = th.SystemAdminClient.AddTeamMember(th.BasicTeam.Id, bot.UserId)
CheckNoError(t, resp)
// If the team is group-constrained the user cannot be removed
th.BasicTeam.GroupConstrained = model.NewBool(true)
_, err := th.App.UpdateTeam(th.BasicTeam)
@@ -1867,6 +1884,10 @@ func TestRemoveTeamMember(t *testing.T) {
_, resp = th.SystemAdminClient.RemoveTeamMember(th.BasicTeam.Id, th.BasicUser.Id)
require.Equal(t, "api.team.remove_member.group_constrained.app_error", resp.Error.Id)
// Can remove a bot even if team is group-constrained
_, resp = th.SystemAdminClient.RemoveTeamMember(th.BasicTeam.Id, bot.UserId)
CheckNoError(t, resp)
// Can remove self even if team is group-constrained
_, resp = th.SystemAdminClient.RemoveTeamMember(th.BasicTeam.Id, th.SystemAdminUser.Id)
CheckNoError(t, resp)