MM-14412: Enables force removals of team/channel members base on group constraints. (#10490)

* MM-14412: Enables force removals of team/channel members base on group constraints.

* Renames some existing methods and variables.

* Change return types to ChannelMembers and TeamMembers for some existing methods.

* Adds option to change LDAP_DATA to either 'qa' or 'test' with env variable.

* Adds methods to retrieve ChannelMembers and TeamMembers that, based on group constraints, should be deleted.

* Adds helper functions to create GroupTeams and GroupChannels.

* MM-14412: Switches to helper methods for GroupSyncable creation in test files.

* MM-14412: Style fix.

* MM-14412: Switches remaining GroupSyncable instances to be created with helper functions.

* MM-14412: Typo fix.

* MM-11412: Build fixes.

* MM-14412: Checks if user is team member before re-adding.

* MM-14412: Update for change of GroupConstrained fields type.
Этот коммит содержится в:
Martin Kraft
2019-04-02 09:22:50 -04:00
коммит произвёл GitHub
родитель cee65379c7
Коммит 25fd962016
23 изменённых файлов: 1212 добавлений и 607 удалений

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

@@ -4,9 +4,10 @@ import (
"testing"
"github.com/mattermost/mattermost-server/model"
"github.com/stretchr/testify/require"
)
func TestPopulateSyncablesSince(t *testing.T) {
func TestCreateDefaultMemberships(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
@@ -70,32 +71,17 @@ func TestPopulateSyncablesSince(t *testing.T) {
t.Errorf("test group not created: %s", err.Error())
}
_, err = th.App.CreateGroupSyncable(&model.GroupSyncable{
AutoAdd: true,
GroupId: gleeGroup.Id,
SyncableId: practiceChannel.Id,
Type: model.GroupSyncableTypeChannel,
})
_, err = th.App.CreateGroupSyncable(model.NewGroupChannel(gleeGroup.Id, practiceChannel.Id, true))
if err != nil {
t.Errorf("test groupchannel not created: %s", err.Error())
}
scienceTeamGroupSyncable, err := th.App.CreateGroupSyncable(&model.GroupSyncable{
AutoAdd: false,
GroupId: scienceGroup.Id,
SyncableId: nerdsTeam.Id,
Type: model.GroupSyncableTypeTeam,
})
scienceTeamGroupSyncable, err := th.App.CreateGroupSyncable(model.NewGroupTeam(scienceGroup.Id, nerdsTeam.Id, false))
if err != nil {
t.Errorf("test groupteam not created: %s", err.Error())
}
scienceChannelGroupSyncable, err := th.App.CreateGroupSyncable(&model.GroupSyncable{
AutoAdd: false,
GroupId: scienceGroup.Id,
SyncableId: experimentsChannel.Id,
Type: model.GroupSyncableTypeChannel,
})
scienceChannelGroupSyncable, err := th.App.CreateGroupSyncable(model.NewGroupChannel(scienceGroup.Id, experimentsChannel.Id, false))
if err != nil {
t.Errorf("test groupchannel not created: %s", err.Error())
}
@@ -113,7 +99,7 @@ func TestPopulateSyncablesSince(t *testing.T) {
t.Errorf("test groupmember not created: %s", err.Error())
}
pErr := th.App.PopulateSyncablesSince(0)
pErr := th.App.CreateDefaultMemberships(0)
if pErr != nil {
t.Errorf("faild to populate syncables: %s", pErr.Error())
}
@@ -183,7 +169,7 @@ func TestPopulateSyncablesSince(t *testing.T) {
}
// Sync everything after syncable was created (proving that team updates trigger re-sync)
pErr = th.App.PopulateSyncablesSince(scientistGroupMember.CreateAt + 1)
pErr = th.App.CreateDefaultMemberships(scientistGroupMember.CreateAt + 1)
if pErr != nil {
t.Errorf("faild to populate syncables: %s", pErr.Error())
}
@@ -226,7 +212,7 @@ func TestPopulateSyncablesSince(t *testing.T) {
}
// Sync everything after syncable was created (proving that channel updates trigger re-sync)
pErr = th.App.PopulateSyncablesSince(scientistGroupMember.CreateAt + 1)
pErr = th.App.CreateDefaultMemberships(scientistGroupMember.CreateAt + 1)
if pErr != nil {
t.Errorf("faild to populate syncables: %s", pErr.Error())
}
@@ -251,7 +237,7 @@ func TestPopulateSyncablesSince(t *testing.T) {
}
// Even re-syncing from the beginning doesn't re-add to channel or team
pErr = th.App.PopulateSyncablesSince(0)
pErr = th.App.CreateDefaultMemberships(0)
if pErr != nil {
t.Errorf("faild to populate syncables: %s", pErr.Error())
}
@@ -292,7 +278,7 @@ func TestPopulateSyncablesSince(t *testing.T) {
t.Errorf("error updating group syncable: %s", err.Error())
}
pErr = th.App.PopulateSyncablesSince(0)
pErr = th.App.CreateDefaultMemberships(0)
if pErr != nil {
t.Errorf("faild to populate syncables: %s", pErr.Error())
}
@@ -313,7 +299,7 @@ func TestPopulateSyncablesSince(t *testing.T) {
t.Errorf("error permanently deleting channelmemberhistory: %s", result.Err.Error())
}
pErr = th.App.PopulateSyncablesSince(scienceChannelGroupSyncable.UpdateAt)
pErr = th.App.CreateDefaultMemberships(scienceChannelGroupSyncable.UpdateAt)
if pErr != nil {
t.Errorf("failed to populate syncables: %s", pErr.Error())
}
@@ -329,7 +315,7 @@ func TestPopulateSyncablesSince(t *testing.T) {
t.Errorf("error permanently deleting channelmemberhistory: %s", result.Err.Error())
}
pErr = th.App.PopulateSyncablesSince(scienceChannelGroupSyncable.UpdateAt)
pErr = th.App.CreateDefaultMemberships(scienceChannelGroupSyncable.UpdateAt)
if pErr != nil {
t.Errorf("failed to populate syncables: %s", pErr.Error())
}
@@ -340,3 +326,70 @@ func TestPopulateSyncablesSince(t *testing.T) {
t.Errorf("expected channel member: %s", err.Error())
}
}
func TestDeleteGroupMemberships(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
group := th.CreateGroup()
userIDs := []string{th.BasicUser.Id, th.BasicUser2.Id, th.SystemAdminUser.Id}
var err *model.AppError
// add users to teams and channels
for _, userID := range userIDs {
_, err = th.App.AddTeamMember(th.BasicTeam.Id, userID)
require.Nil(t, err)
_, err = th.App.AddChannelMember(userID, th.BasicChannel, "", "", "")
require.Nil(t, err)
}
// make team group-constrained
team := th.BasicTeam
team.GroupConstrained = model.NewBool(true)
team, err = th.App.UpdateTeam(team)
require.Nil(t, err)
require.True(t, *team.GroupConstrained)
// make channel group-constrained
channel := th.BasicChannel
channel.GroupConstrained = model.NewBool(true)
channel, err = th.App.UpdateChannel(channel)
require.Nil(t, err)
require.True(t, *channel.GroupConstrained)
// create groupteam and groupchannel
_, err = th.App.CreateGroupSyncable(model.NewGroupTeam(group.Id, team.Id, true))
require.Nil(t, err)
_, err = th.App.CreateGroupSyncable(model.NewGroupChannel(group.Id, channel.Id, true))
require.Nil(t, err)
// verify the member count
tmembers, err := th.App.GetTeamMembers(th.BasicTeam.Id, 0, 100)
require.Nil(t, err)
require.Len(t, tmembers, 3)
cmemberCount, err := th.App.GetChannelMemberCount(th.BasicChannel.Id)
require.Nil(t, err)
require.Equal(t, 3, int(cmemberCount))
// add a user to the group
_, err = th.App.CreateOrRestoreGroupMember(group.Id, th.SystemAdminUser.Id)
require.Nil(t, err)
// run the delete
appErr := th.App.DeleteGroupConstrainedMemberships()
require.Nil(t, appErr)
// verify the new member counts
tmembers, err = th.App.GetTeamMembers(th.BasicTeam.Id, 0, 100)
require.Nil(t, err)
require.Len(t, tmembers, 1)
require.Equal(t, th.SystemAdminUser.Id, tmembers[0].UserId)
cmembers, err := th.App.GetChannelMembersPage(channel.Id, 0, 99)
require.Nil(t, err)
require.Len(t, (*cmembers), 1)
require.Equal(t, th.SystemAdminUser.Id, (*cmembers)[0].UserId)
}