Files
mostlymatter/app/group_test.go
mkraft 7fa6b321ae Custom groups (#18839)
* WIP

* adding initial creategroup endpoint

* fetching by group source

* fixing startup error

* updating create endpoint to take an array of user_ids, this will allow us to create the group with one request

* adding delete group endpoint and appropriate test

* adding source param for getGroups

* adding add members and delete members endpoints

* locking down crud endpoints to only be allowed for custom groups

* user search stuff

* allowing remoteid be null by changing field to pointer

* code cleanup and store level tests

* adding new tests and removing unused endpoint

* resolving conflicts

* Adds authz check for group.

* Adds authz checks to groups APIs.

* Updated create group authz tests.

* Updates delete group tests.

* Tests create group.

* Adds some tests and validations.

* adding new parameter so I can get users not in a group

* Fixed all lint warnings.

* Fix type.

* fixing search users not in group

* Fixes some lint errors.

* Moves entry in JSON array.

* Fixed SQL query.

* Fixes permission migration test.

* Fixes migration test.

* Fixes some group store tests.

* Fix test.

* Fix test.

* Revert lint change.

* Migrated CreateWithUserIds to sqlx.

* Adds tests for GetMember; migrates implementation to sqlx.

* Tests GetNonMemberUsersPage and hanles wrong group id.

* Fixes test.

* Switches GetMaster to GetMasterX.

* Switches GetReplica to GetReplicaX.

* Fixes logic.

* Fixes shadow declaration.

* Adds include_member_count to get group API endpoint.

* Adds filter_has_member param to getGroups.

* Fixes.

* Removes array of group sources.

* fixing error

* Testing reverting CreateWithUserIds back to gorp.

* Added websocket event for CreateGroupWithUserIds.

* Changed a few response status codes. Switched to correct permission.

* Added member count to ws payload for group when updating or creating.

* Adds feature flag checks for custom groups.

* Added middleware function to require license. Added config to disable custom groups.

* Change for function signature change of executePossiblyEmptyQuery.

* Lint fixes.

* Adds telemetry none comment.

* Adds translations.

* Migrated to sqlx.

* Temp. removal of translation.

* Fixed typo.

* Added an intermediary model to query with a field that is now ignored by sqlx on read queries.

* Re-used existing store struct.

* Inludes member count.

* Fix for merge error.'

* Require license for group endpoints.

* Updates translations.

* Fix shadow declaration.

* Renames permissions. Switches to new method to retrieve remoteid.

* Added WS events for upsert and delete member(s).

* Added new store error type ErrUniqueConstraint.

* Added EnableCustonGroups to the client config.

* Sanitized some user records.

* Added parameter to include_total_count for listing groups.

* Added translations.

* adding deleteAt field to getByUsers query

* Revert sanitize.

* Added uniqueness constraint error to UpdateGroup.

* Removed the FutureFeatures flag so that the feature is not enabled on old Enterprise licenses.

* Renamed function.

* Updates authz check for user search related to groups.

* Removed debug statement.

* Removed unused app method.

* Added telemetry for enable_custom_groups.

* Returns early from nil license.

* Updates test.

* Returned early to avoid nesting in (*SqlGroupStore).checkUserExist. Switched to reading from replica in (*SqlGroupStore).GetMember. Handled JSON marshal error in (*Client4).UpsertGroupMembers

* Switched to SanitizeProfile.

* Switched to model.NewInt.

* Switched from status NotImplemented to Forbidden for missing license.

* Removed deactivated users from 'exists' set.

* Revert gotool update.

* Ignored lint error that I think is invalid.

* Added the approprate access tag for disabling custom groups.

* Revert change to response status.

* Fixed refactor mistake.

* Limited the group member WS events to individual users.

* Removed WS event of deleted groups.

* Updated license check for searchUsers endpoint.

* Switched from license feature to license sku.

* Update app/group.go

Co-authored-by: Claudio Costa <cstcld91@gmail.com>

* Update app/group.go

Co-authored-by: Claudio Costa <cstcld91@gmail.com>

* Remove linter ignore comment.

* Added function to create sku-specific license.

* Fixed typo. Removed comment.

* Fixed for wrong type.

* Added missing param to client. Removed unnecessary props setting. Added test for retrieving groups by source.

* Updated some tests now that we're validating group membership not created for deactivated user.

* Fix for groups endpoint returning all group types by default.

* Changes constant names. Adds migration for all users to manage custom group members.

* Removes requirement for manage_system permission to filter user search by group.

* Added migration mock.

* Removes default permissions from custom_group_user role.

* Fixes migration.

* Fixes emoji migration test.

* fixing issue with member counts

* fixing search issue for deleted members

Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local>
Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MBP.ht.home>
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.fritz.box>
Co-authored-by: Claudio Costa <cstcld91@gmail.com>
2022-02-17 12:34:39 -05:00

411 строки
11 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/v6/model"
)
func TestGetGroup(t *testing.T) {
th := Setup(t)
defer th.TearDown()
group := th.CreateGroup()
group, err := th.App.GetGroup(group.Id, nil)
require.Nil(t, err)
require.NotNil(t, group)
nilGroup, err := th.App.GetGroup(model.NewId(), nil)
require.NotNil(t, err)
require.Nil(t, nilGroup)
group, err = th.App.GetGroup(group.Id, &model.GetGroupOpts{IncludeMemberCount: false})
require.Nil(t, err)
require.Nil(t, group.MemberCount)
group, err = th.App.GetGroup(group.Id, &model.GetGroupOpts{IncludeMemberCount: true})
require.Nil(t, err)
require.NotNil(t, group.MemberCount)
}
func TestGetGroupByRemoteID(t *testing.T) {
th := Setup(t)
defer th.TearDown()
group := th.CreateGroup()
g, err := th.App.GetGroupByRemoteID(*group.RemoteId, model.GroupSourceLdap)
require.Nil(t, err)
require.NotNil(t, g)
g, err = th.App.GetGroupByRemoteID(model.NewId(), model.GroupSourceLdap)
require.NotNil(t, err)
require.Nil(t, g)
}
func TestGetGroupsByType(t *testing.T) {
th := Setup(t)
defer th.TearDown()
th.CreateGroup()
th.CreateGroup()
th.CreateGroup()
groups, err := th.App.GetGroupsBySource(model.GroupSourceLdap)
require.Nil(t, err)
require.NotEmpty(t, groups)
groups, err = th.App.GetGroupsBySource(model.GroupSource("blah"))
require.Nil(t, err)
require.Empty(t, groups)
}
func TestCreateGroup(t *testing.T) {
th := Setup(t)
defer th.TearDown()
id := model.NewId()
group := &model.Group{
DisplayName: "dn_" + id,
Name: model.NewString("name" + id),
Source: model.GroupSourceLdap,
RemoteId: model.NewString(model.NewId()),
}
g, err := th.App.CreateGroup(group)
require.Nil(t, err)
require.NotNil(t, g)
g, err = th.App.CreateGroup(group)
require.NotNil(t, err)
require.Nil(t, g)
}
func TestUpdateGroup(t *testing.T) {
th := Setup(t)
defer th.TearDown()
group := th.CreateGroup()
group.DisplayName = model.NewId()
g, err := th.App.UpdateGroup(group)
require.Nil(t, err)
require.NotNil(t, g)
}
func TestDeleteGroup(t *testing.T) {
th := Setup(t)
defer th.TearDown()
group := th.CreateGroup()
g, err := th.App.DeleteGroup(group.Id)
require.Nil(t, err)
require.NotNil(t, g)
g, err = th.App.DeleteGroup(group.Id)
require.NotNil(t, err)
require.Nil(t, g)
}
func TestCreateOrRestoreGroupMember(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
group := th.CreateGroup()
g, err := th.App.UpsertGroupMember(group.Id, th.BasicUser.Id)
require.Nil(t, err)
require.NotNil(t, g)
g, err = th.App.UpsertGroupMember(group.Id, th.BasicUser.Id)
require.Nil(t, err)
require.NotNil(t, g)
}
func TestDeleteGroupMember(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
group := th.CreateGroup()
groupMember, err := th.App.UpsertGroupMember(group.Id, th.BasicUser.Id)
require.Nil(t, err)
require.NotNil(t, groupMember)
groupMember, err = th.App.DeleteGroupMember(groupMember.GroupId, groupMember.UserId)
require.Nil(t, err)
require.NotNil(t, groupMember)
groupMember, err = th.App.DeleteGroupMember(groupMember.GroupId, groupMember.UserId)
require.NotNil(t, err)
require.Nil(t, groupMember)
}
func TestUpsertGroupSyncable(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
group := th.CreateGroup()
groupSyncable := model.NewGroupTeam(group.Id, th.BasicTeam.Id, false)
gs, err := th.App.UpsertGroupSyncable(groupSyncable)
require.Nil(t, err)
require.NotNil(t, gs)
// can update again without error
gs, err = th.App.UpsertGroupSyncable(groupSyncable)
require.Nil(t, err)
require.NotNil(t, gs)
gs, err = th.App.DeleteGroupSyncable(gs.GroupId, gs.SyncableId, gs.Type)
require.Nil(t, err)
require.NotEqual(t, int64(0), gs.DeleteAt)
// Un-deleting works
gs.DeleteAt = 0
gs, err = th.App.UpsertGroupSyncable(gs)
require.Nil(t, err)
require.Equal(t, int64(0), gs.DeleteAt)
}
func TestUpsertGroupSyncableTeamGroupConstrained(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
group1 := th.CreateGroup()
group2 := th.CreateGroup()
team := th.CreateTeam()
team.GroupConstrained = model.NewBool(true)
team, err := th.App.UpdateTeam(team)
require.Nil(t, err)
_, err = th.App.UpsertGroupSyncable(model.NewGroupTeam(group1.Id, team.Id, false))
require.Nil(t, err)
channel := th.CreateChannel(team)
_, err = th.App.UpsertGroupSyncable(model.NewGroupChannel(group2.Id, channel.Id, false))
require.NotNil(t, err)
require.Equal(t, err.Id, "group_not_associated_to_synced_team")
gs, err := th.App.GetGroupSyncable(group2.Id, channel.Id, model.GroupSyncableTypeChannel)
require.Nil(t, gs)
require.NotNil(t, err)
_, err = th.App.UpsertGroupSyncable(model.NewGroupChannel(group1.Id, channel.Id, false))
require.Nil(t, err)
}
func TestGetGroupSyncable(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
group := th.CreateGroup()
groupSyncable := model.NewGroupTeam(group.Id, th.BasicTeam.Id, false)
gs, err := th.App.UpsertGroupSyncable(groupSyncable)
require.Nil(t, err)
require.NotNil(t, gs)
gs, err = th.App.GetGroupSyncable(group.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam)
require.Nil(t, err)
require.NotNil(t, gs)
}
func TestGetGroupSyncables(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
group := th.CreateGroup()
// Create a group team
groupSyncable := model.NewGroupTeam(group.Id, th.BasicTeam.Id, false)
gs, err := th.App.UpsertGroupSyncable(groupSyncable)
require.Nil(t, err)
require.NotNil(t, gs)
groupTeams, err := th.App.GetGroupSyncables(group.Id, model.GroupSyncableTypeTeam)
require.Nil(t, err)
require.NotEmpty(t, groupTeams)
}
func TestDeleteGroupSyncable(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
group := th.CreateGroup()
groupChannel := model.NewGroupChannel(group.Id, th.BasicChannel.Id, false)
gs, err := th.App.UpsertGroupSyncable(groupChannel)
require.Nil(t, err)
require.NotNil(t, gs)
gs, err = th.App.DeleteGroupSyncable(group.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel)
require.Nil(t, err)
require.NotNil(t, gs)
gs, err = th.App.DeleteGroupSyncable(group.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel)
require.NotNil(t, err)
require.Nil(t, gs)
}
func TestGetGroupsByChannel(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
group := th.CreateGroup()
// Create a group channel
groupSyncable := &model.GroupSyncable{
GroupId: group.Id,
AutoAdd: false,
SyncableId: th.BasicChannel.Id,
Type: model.GroupSyncableTypeChannel,
}
gs, err := th.App.UpsertGroupSyncable(groupSyncable)
require.Nil(t, err)
require.NotNil(t, gs)
opts := model.GroupSearchOpts{
PageOpts: &model.PageOpts{
Page: 0,
PerPage: 60,
},
}
groups, _, err := th.App.GetGroupsByChannel(th.BasicChannel.Id, opts)
require.Nil(t, err)
require.ElementsMatch(t, []*model.GroupWithSchemeAdmin{{Group: *group, SchemeAdmin: model.NewBool(false)}}, groups)
require.NotNil(t, groups[0].SchemeAdmin)
groups, _, err = th.App.GetGroupsByChannel(model.NewId(), opts)
require.Nil(t, err)
require.Empty(t, groups)
}
func TestGetGroupsAssociatedToChannelsByTeam(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
group := th.CreateGroup()
// Create a group channel
groupSyncable := &model.GroupSyncable{
GroupId: group.Id,
AutoAdd: false,
SyncableId: th.BasicChannel.Id,
Type: model.GroupSyncableTypeChannel,
}
gs, err := th.App.UpsertGroupSyncable(groupSyncable)
require.Nil(t, err)
require.NotNil(t, gs)
opts := model.GroupSearchOpts{
PageOpts: &model.PageOpts{
Page: 0,
PerPage: 60,
},
}
groups, err := th.App.GetGroupsAssociatedToChannelsByTeam(th.BasicTeam.Id, opts)
require.Nil(t, err)
assert.Equal(t, map[string][]*model.GroupWithSchemeAdmin{
th.BasicChannel.Id: {
{Group: *group, SchemeAdmin: model.NewBool(false)},
},
}, groups)
require.NotNil(t, groups[th.BasicChannel.Id][0].SchemeAdmin)
groups, err = th.App.GetGroupsAssociatedToChannelsByTeam(model.NewId(), opts)
require.Nil(t, err)
require.Empty(t, groups)
}
func TestGetGroupsByTeam(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
group := th.CreateGroup()
// Create a group team
groupSyncable := &model.GroupSyncable{
GroupId: group.Id,
AutoAdd: false,
SyncableId: th.BasicTeam.Id,
Type: model.GroupSyncableTypeTeam,
}
gs, err := th.App.UpsertGroupSyncable(groupSyncable)
require.Nil(t, err)
require.NotNil(t, gs)
groups, _, err := th.App.GetGroupsByTeam(th.BasicTeam.Id, model.GroupSearchOpts{})
require.Nil(t, err)
require.ElementsMatch(t, []*model.GroupWithSchemeAdmin{{Group: *group, SchemeAdmin: model.NewBool(false)}}, groups)
require.NotNil(t, groups[0].SchemeAdmin)
groups, _, err = th.App.GetGroupsByTeam(model.NewId(), model.GroupSearchOpts{})
require.Nil(t, err)
require.Empty(t, groups)
}
func TestGetGroups(t *testing.T) {
th := Setup(t)
defer th.TearDown()
group := th.CreateGroup()
groups, err := th.App.GetGroups(0, 60, model.GroupSearchOpts{})
require.Nil(t, err)
require.ElementsMatch(t, []*model.Group{group}, groups)
}
func TestUserIsInAdminRoleGroup(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
group1 := th.CreateGroup()
group2 := th.CreateGroup()
g, err := th.App.UpsertGroupMember(group1.Id, th.BasicUser.Id)
require.Nil(t, err)
require.NotNil(t, g)
g, err = th.App.UpsertGroupMember(group2.Id, th.BasicUser.Id)
require.Nil(t, err)
require.NotNil(t, g)
_, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{
GroupId: group1.Id,
AutoAdd: false,
SyncableId: th.BasicTeam.Id,
Type: model.GroupSyncableTypeTeam,
})
require.Nil(t, err)
groupSyncable2, err := th.App.UpsertGroupSyncable(&model.GroupSyncable{
GroupId: group2.Id,
AutoAdd: false,
SyncableId: th.BasicTeam.Id,
Type: model.GroupSyncableTypeTeam,
})
require.Nil(t, err)
// no syncables are set to scheme admin true, so this returns false
actual, err := th.App.UserIsInAdminRoleGroup(th.BasicUser.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam)
require.Nil(t, err)
require.False(t, actual)
// set a syncable to be scheme admins
groupSyncable2.SchemeAdmin = true
_, err = th.App.UpdateGroupSyncable(groupSyncable2)
require.Nil(t, err)
// a syncable is set to scheme admin true, so this returns true
actual, err = th.App.UserIsInAdminRoleGroup(th.BasicUser.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam)
require.Nil(t, err)
require.True(t, actual)
// delete the syncable, should be false again
th.App.DeleteGroupSyncable(group2.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam)
actual, err = th.App.UserIsInAdminRoleGroup(th.BasicUser.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam)
require.Nil(t, err)
require.False(t, actual)
}