2024-04-16 03:39:51 +02:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using Bit.Api.AdminConsole.Controllers;
|
2023-10-18 17:27:56 +02:00
|
|
|
|
using Bit.Api.AdminConsole.Models.Request;
|
2024-05-02 01:55:16 +02:00
|
|
|
|
using Bit.Api.Models.Request;
|
|
|
|
|
using Bit.Api.Vault.AuthorizationHandlers.Collections;
|
2024-04-16 03:39:51 +02:00
|
|
|
|
using Bit.Core;
|
2023-10-19 22:37:46 +02:00
|
|
|
|
using Bit.Core.AdminConsole.Entities;
|
|
|
|
|
using Bit.Core.AdminConsole.OrganizationFeatures.Groups.Interfaces;
|
|
|
|
|
using Bit.Core.AdminConsole.Repositories;
|
2022-12-12 10:59:48 +01:00
|
|
|
|
using Bit.Core.Context;
|
2024-04-16 03:39:51 +02:00
|
|
|
|
using Bit.Core.Entities;
|
|
|
|
|
using Bit.Core.Exceptions;
|
2022-12-12 10:59:48 +01:00
|
|
|
|
using Bit.Core.Models.Data;
|
2024-04-16 03:39:51 +02:00
|
|
|
|
using Bit.Core.Models.Data.Organizations;
|
2022-12-12 10:59:48 +01:00
|
|
|
|
using Bit.Core.Repositories;
|
2024-04-16 03:39:51 +02:00
|
|
|
|
using Bit.Core.Services;
|
2024-05-02 01:55:16 +02:00
|
|
|
|
using Bit.Core.Utilities;
|
2022-12-12 10:59:48 +01:00
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
2024-05-02 01:55:16 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2022-12-12 10:59:48 +01:00
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2023-10-18 17:27:56 +02:00
|
|
|
|
namespace Bit.Api.Test.AdminConsole.Controllers;
|
2022-12-12 10:59:48 +01:00
|
|
|
|
|
|
|
|
|
[ControllerCustomize(typeof(GroupsController))]
|
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
public class GroupsControllerTests
|
|
|
|
|
{
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2024-05-02 01:55:16 +02:00
|
|
|
|
public async Task Post_PreFCv1_Success(Organization organization, GroupRequestModel groupRequestModel, SutProvider<GroupsController> sutProvider)
|
2022-12-12 10:59:48 +01:00
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageGroups(organization.Id).Returns(true);
|
|
|
|
|
|
2024-05-02 01:55:16 +02:00
|
|
|
|
var response = await sutProvider.Sut.Post(organization.Id, groupRequestModel);
|
2022-12-12 10:59:48 +01:00
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<ICurrentContext>().Received(1).ManageGroups(organization.Id);
|
|
|
|
|
await sutProvider.GetDependency<ICreateGroupCommand>().Received(1).CreateGroupAsync(
|
|
|
|
|
Arg.Is<Group>(g =>
|
|
|
|
|
g.OrganizationId == organization.Id && g.Name == groupRequestModel.Name &&
|
2023-02-06 10:27:40 +01:00
|
|
|
|
g.AccessAll == groupRequestModel.AccessAll),
|
2022-12-12 10:59:48 +01:00
|
|
|
|
organization,
|
2024-02-08 14:44:36 +01:00
|
|
|
|
Arg.Any<ICollection<CollectionAccessSelection>>(),
|
2023-01-19 17:00:54 +01:00
|
|
|
|
Arg.Any<IEnumerable<Guid>>());
|
2022-12-12 10:59:48 +01:00
|
|
|
|
Assert.Equal(groupRequestModel.Name, response.Name);
|
2023-07-14 17:18:26 +02:00
|
|
|
|
Assert.Equal(organization.Id, response.OrganizationId);
|
2022-12-12 10:59:48 +01:00
|
|
|
|
Assert.Equal(groupRequestModel.AccessAll, response.AccessAll);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2024-05-02 01:55:16 +02:00
|
|
|
|
public async Task Post_AuthorizedToGiveAccessToCollections_Success(Organization organization,
|
|
|
|
|
GroupRequestModel groupRequestModel, SutProvider<GroupsController> sutProvider)
|
2022-12-12 10:59:48 +01:00
|
|
|
|
{
|
2024-05-02 01:55:16 +02:00
|
|
|
|
// Enable FC and v1
|
|
|
|
|
sutProvider.GetDependency<IApplicationCacheService>().GetOrganizationAbilityAsync(organization.Id).Returns(
|
2024-06-04 00:46:48 +02:00
|
|
|
|
new OrganizationAbility { Id = organization.Id, AllowAdminAccessToAllCollectionItems = false });
|
2024-05-02 01:55:16 +02:00
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.FlexibleCollectionsV1).Returns(true);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(),
|
|
|
|
|
Arg.Any<IEnumerable<Collection>>(),
|
|
|
|
|
Arg.Is<IEnumerable<IAuthorizationRequirement>>(reqs => reqs.Contains(BulkCollectionOperations.ModifyGroupAccess)))
|
|
|
|
|
.Returns(AuthorizationResult.Success());
|
2022-12-12 10:59:48 +01:00
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageGroups(organization.Id).Returns(true);
|
2024-05-02 01:55:16 +02:00
|
|
|
|
|
|
|
|
|
var response = await sutProvider.Sut.Post(organization.Id, groupRequestModel);
|
|
|
|
|
|
|
|
|
|
var requestModelCollectionIds = groupRequestModel.Collections.Select(c => c.Id).ToHashSet();
|
|
|
|
|
|
|
|
|
|
// Assert that it checked permissions
|
|
|
|
|
await sutProvider.GetDependency<ICurrentContext>().Received(1).ManageGroups(organization.Id);
|
|
|
|
|
await sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.Received(1)
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(),
|
|
|
|
|
Arg.Is<IEnumerable<Collection>>(collections =>
|
|
|
|
|
collections.All(c => requestModelCollectionIds.Contains(c.Id))),
|
|
|
|
|
Arg.Is<IEnumerable<IAuthorizationRequirement>>(reqs =>
|
|
|
|
|
reqs.Single() == BulkCollectionOperations.ModifyGroupAccess));
|
|
|
|
|
|
|
|
|
|
// Assert that it saved the data
|
|
|
|
|
await sutProvider.GetDependency<ICreateGroupCommand>().Received(1).CreateGroupAsync(
|
|
|
|
|
Arg.Is<Group>(g =>
|
|
|
|
|
g.OrganizationId == organization.Id && g.Name == groupRequestModel.Name &&
|
|
|
|
|
g.AccessAll == groupRequestModel.AccessAll),
|
|
|
|
|
organization,
|
|
|
|
|
Arg.Is<ICollection<CollectionAccessSelection>>(access =>
|
|
|
|
|
access.All(c => requestModelCollectionIds.Contains(c.Id))),
|
|
|
|
|
Arg.Any<IEnumerable<Guid>>());
|
|
|
|
|
Assert.Equal(groupRequestModel.Name, response.Name);
|
|
|
|
|
Assert.Equal(organization.Id, response.OrganizationId);
|
|
|
|
|
Assert.Equal(groupRequestModel.AccessAll, response.AccessAll);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task Post_NotAuthorizedToGiveAccessToCollections_Throws(Organization organization, GroupRequestModel groupRequestModel, SutProvider<GroupsController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
// Enable FC and v1
|
2024-04-16 03:39:51 +02:00
|
|
|
|
sutProvider.GetDependency<IApplicationCacheService>().GetOrganizationAbilityAsync(organization.Id).Returns(
|
2024-06-04 00:46:48 +02:00
|
|
|
|
new OrganizationAbility { Id = organization.Id, AllowAdminAccessToAllCollectionItems = false });
|
2024-05-02 01:55:16 +02:00
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.FlexibleCollectionsV1).Returns(true);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageGroups(organization.Id).Returns(true);
|
|
|
|
|
|
|
|
|
|
var requestModelCollectionIds = groupRequestModel.Collections.Select(c => c.Id).ToHashSet();
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(),
|
|
|
|
|
Arg.Is<IEnumerable<Collection>>(collections => collections.All(c => requestModelCollectionIds.Contains(c.Id))),
|
|
|
|
|
Arg.Is<IEnumerable<IAuthorizationRequirement>>(reqs => reqs.Contains(BulkCollectionOperations.ModifyGroupAccess)))
|
|
|
|
|
.Returns(AuthorizationResult.Failed());
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.Post(organization.Id, groupRequestModel));
|
|
|
|
|
|
|
|
|
|
Assert.Contains("You are not authorized to grant access to these collections.", exception.Message);
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<ICreateGroupCommand>().DidNotReceiveWithAnyArgs()
|
|
|
|
|
.CreateGroupAsync(default, default, default, default);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task Put_AdminsCanAccessAllCollections_Success(Organization organization, Group group,
|
|
|
|
|
GroupRequestModel groupRequestModel, List<CollectionAccessSelection> existingCollectionAccess,
|
|
|
|
|
SutProvider<GroupsController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
group.OrganizationId = organization.Id;
|
|
|
|
|
|
|
|
|
|
// Enable FC and v1, set Collection Management Setting
|
|
|
|
|
sutProvider.GetDependency<IApplicationCacheService>().GetOrganizationAbilityAsync(organization.Id).Returns(
|
2024-06-04 00:46:48 +02:00
|
|
|
|
new OrganizationAbility { Id = organization.Id, AllowAdminAccessToAllCollectionItems = true });
|
2024-05-02 01:55:16 +02:00
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.FlexibleCollectionsV1).Returns(true);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
sutProvider.GetDependency<IGroupRepository>().GetByIdWithCollectionsAsync(group.Id)
|
|
|
|
|
.Returns(new Tuple<Group, ICollection<CollectionAccessSelection>>(group, existingCollectionAccess));
|
|
|
|
|
sutProvider.GetDependency<ICollectionRepository>()
|
|
|
|
|
.GetManyByManyIdsAsync(existingCollectionAccess.Select(c => c.Id))
|
|
|
|
|
.Returns(existingCollectionAccess.Select(c => new Collection { Id = c.Id }).ToList());
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageGroups(organization.Id).Returns(true);
|
|
|
|
|
|
|
|
|
|
var requestModelCollectionIds = groupRequestModel.Collections.Select(c => c.Id).ToHashSet();
|
2022-12-12 10:59:48 +01:00
|
|
|
|
|
2024-04-16 03:39:51 +02:00
|
|
|
|
var response = await sutProvider.Sut.Put(organization.Id, group.Id, groupRequestModel);
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<ICurrentContext>().Received(1).ManageGroups(organization.Id);
|
|
|
|
|
await sutProvider.GetDependency<IUpdateGroupCommand>().Received(1).UpdateGroupAsync(
|
|
|
|
|
Arg.Is<Group>(g =>
|
|
|
|
|
g.OrganizationId == organization.Id && g.Name == groupRequestModel.Name &&
|
|
|
|
|
g.AccessAll == groupRequestModel.AccessAll),
|
|
|
|
|
Arg.Is<Organization>(o => o.Id == organization.Id),
|
2024-05-02 01:55:16 +02:00
|
|
|
|
// Should overwrite any existing collections
|
|
|
|
|
Arg.Is<ICollection<CollectionAccessSelection>>(access =>
|
|
|
|
|
access.All(c => requestModelCollectionIds.Contains(c.Id))),
|
2024-04-16 03:39:51 +02:00
|
|
|
|
Arg.Any<IEnumerable<Guid>>());
|
|
|
|
|
Assert.Equal(groupRequestModel.Name, response.Name);
|
|
|
|
|
Assert.Equal(organization.Id, response.OrganizationId);
|
|
|
|
|
Assert.Equal(groupRequestModel.AccessAll, response.AccessAll);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2024-05-02 01:55:16 +02:00
|
|
|
|
public async Task Put_UpdateMembers_AdminsCannotAccessAllCollections_CannotAddSelfToGroup(Organization organization, Group group,
|
2024-04-16 03:39:51 +02:00
|
|
|
|
GroupRequestModel groupRequestModel, OrganizationUser savingOrganizationUser, List<Guid> currentGroupUsers,
|
|
|
|
|
SutProvider<GroupsController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
group.OrganizationId = organization.Id;
|
|
|
|
|
|
2024-05-02 01:55:16 +02:00
|
|
|
|
// Enable FC and v1
|
2024-04-16 03:39:51 +02:00
|
|
|
|
sutProvider.GetDependency<IApplicationCacheService>().GetOrganizationAbilityAsync(organization.Id).Returns(
|
|
|
|
|
new OrganizationAbility
|
|
|
|
|
{
|
|
|
|
|
Id = organization.Id,
|
|
|
|
|
AllowAdminAccessToAllCollectionItems = false,
|
|
|
|
|
});
|
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.FlexibleCollectionsV1).Returns(true);
|
2024-05-02 01:55:16 +02:00
|
|
|
|
|
|
|
|
|
// Saving user is trying to add themselves to the group
|
|
|
|
|
var updatedUsers = groupRequestModel.Users.ToList();
|
|
|
|
|
updatedUsers.Add(savingOrganizationUser.Id);
|
|
|
|
|
groupRequestModel.Users = updatedUsers;
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
sutProvider.GetDependency<IGroupRepository>().GetByIdWithCollectionsAsync(group.Id)
|
|
|
|
|
.Returns(new Tuple<Group, ICollection<CollectionAccessSelection>>(group, new List<CollectionAccessSelection>()));
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageGroups(organization.Id).Returns(true);
|
2024-04-16 03:39:51 +02:00
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetByOrganizationAsync(organization.Id, Arg.Any<Guid>())
|
|
|
|
|
.Returns(savingOrganizationUser);
|
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(Arg.Any<ClaimsPrincipal>())
|
|
|
|
|
.Returns(savingOrganizationUser.UserId);
|
|
|
|
|
sutProvider.GetDependency<IGroupRepository>().GetManyUserIdsByIdAsync(group.Id)
|
|
|
|
|
.Returns(currentGroupUsers);
|
|
|
|
|
|
|
|
|
|
var exception = await
|
|
|
|
|
Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.Put(organization.Id, group.Id, groupRequestModel));
|
|
|
|
|
|
|
|
|
|
Assert.Contains("You cannot add yourself to groups", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2024-05-02 01:55:16 +02:00
|
|
|
|
public async Task Put_UpdateMembers_AdminsCannotAccessAllCollections_AlreadyInGroup_Success(Organization organization, Group group,
|
2024-04-16 03:39:51 +02:00
|
|
|
|
GroupRequestModel groupRequestModel, OrganizationUser savingOrganizationUser, List<Guid> currentGroupUsers,
|
|
|
|
|
SutProvider<GroupsController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
group.OrganizationId = organization.Id;
|
|
|
|
|
|
2024-05-02 01:55:16 +02:00
|
|
|
|
// Enable FC and v1
|
|
|
|
|
sutProvider.GetDependency<IApplicationCacheService>().GetOrganizationAbilityAsync(organization.Id).Returns(
|
|
|
|
|
new OrganizationAbility
|
|
|
|
|
{
|
|
|
|
|
Id = organization.Id,
|
|
|
|
|
AllowAdminAccessToAllCollectionItems = false,
|
|
|
|
|
});
|
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.FlexibleCollectionsV1).Returns(true);
|
|
|
|
|
|
2024-04-16 03:39:51 +02:00
|
|
|
|
// Saving user is trying to add themselves to the group
|
|
|
|
|
var updatedUsers = groupRequestModel.Users.ToList();
|
|
|
|
|
updatedUsers.Add(savingOrganizationUser.Id);
|
|
|
|
|
groupRequestModel.Users = updatedUsers;
|
|
|
|
|
|
|
|
|
|
// But! they are already a member of the group
|
|
|
|
|
currentGroupUsers.Add(savingOrganizationUser.Id);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
2024-05-02 01:55:16 +02:00
|
|
|
|
sutProvider.GetDependency<IGroupRepository>().GetByIdWithCollectionsAsync(group.Id)
|
|
|
|
|
.Returns(new Tuple<Group, ICollection<CollectionAccessSelection>>(group, new List<CollectionAccessSelection>()));
|
2024-04-16 03:39:51 +02:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageGroups(organization.Id).Returns(true);
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetByOrganizationAsync(organization.Id, Arg.Any<Guid>())
|
|
|
|
|
.Returns(savingOrganizationUser);
|
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(Arg.Any<ClaimsPrincipal>())
|
|
|
|
|
.Returns(savingOrganizationUser.UserId);
|
|
|
|
|
sutProvider.GetDependency<IGroupRepository>().GetManyUserIdsByIdAsync(group.Id)
|
2024-05-15 16:17:15 +02:00
|
|
|
|
.Returns(currentGroupUsers);
|
|
|
|
|
|
|
|
|
|
// Make collection authorization pass, it's not being tested here
|
|
|
|
|
groupRequestModel.Collections = Array.Empty<SelectionReadOnlyRequestModel>();
|
|
|
|
|
|
|
|
|
|
var response = await sutProvider.Sut.Put(organization.Id, group.Id, groupRequestModel);
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<ICurrentContext>().Received(1).ManageGroups(organization.Id);
|
|
|
|
|
await sutProvider.GetDependency<IUpdateGroupCommand>().Received(1).UpdateGroupAsync(
|
|
|
|
|
Arg.Is<Group>(g =>
|
|
|
|
|
g.OrganizationId == organization.Id && g.Name == groupRequestModel.Name &&
|
|
|
|
|
g.AccessAll == groupRequestModel.AccessAll),
|
|
|
|
|
Arg.Is<Organization>(o => o.Id == organization.Id),
|
|
|
|
|
Arg.Any<ICollection<CollectionAccessSelection>>(),
|
|
|
|
|
Arg.Any<IEnumerable<Guid>>());
|
|
|
|
|
Assert.Equal(groupRequestModel.Name, response.Name);
|
|
|
|
|
Assert.Equal(organization.Id, response.OrganizationId);
|
|
|
|
|
Assert.Equal(groupRequestModel.AccessAll, response.AccessAll);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task Put_UpdateMembers_AdminsCannotAccessAllCollections_ProviderUser_Success(Organization organization, Group group,
|
|
|
|
|
GroupRequestModel groupRequestModel, List<Guid> currentGroupUsers, Guid savingUserId,
|
|
|
|
|
SutProvider<GroupsController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
group.OrganizationId = organization.Id;
|
|
|
|
|
|
|
|
|
|
// Enable FC and v1
|
|
|
|
|
sutProvider.GetDependency<IApplicationCacheService>().GetOrganizationAbilityAsync(organization.Id).Returns(
|
|
|
|
|
new OrganizationAbility
|
|
|
|
|
{
|
|
|
|
|
Id = organization.Id,
|
|
|
|
|
AllowAdminAccessToAllCollectionItems = false,
|
|
|
|
|
});
|
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.FlexibleCollectionsV1).Returns(true);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
sutProvider.GetDependency<IGroupRepository>().GetByIdWithCollectionsAsync(group.Id)
|
|
|
|
|
.Returns(new Tuple<Group, ICollection<CollectionAccessSelection>>(group, new List<CollectionAccessSelection>()));
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageGroups(organization.Id).Returns(true);
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetByOrganizationAsync(organization.Id, Arg.Any<Guid>())
|
|
|
|
|
.Returns((OrganizationUser)null); // Provider is not an OrganizationUser, so it will always return null
|
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(Arg.Any<ClaimsPrincipal>())
|
|
|
|
|
.Returns(savingUserId);
|
|
|
|
|
sutProvider.GetDependency<IGroupRepository>().GetManyUserIdsByIdAsync(group.Id)
|
2024-04-16 03:39:51 +02:00
|
|
|
|
.Returns(currentGroupUsers);
|
|
|
|
|
|
2024-05-02 01:55:16 +02:00
|
|
|
|
// Make collection authorization pass, it's not being tested here
|
|
|
|
|
groupRequestModel.Collections = Array.Empty<SelectionReadOnlyRequestModel>();
|
|
|
|
|
|
2024-04-16 03:39:51 +02:00
|
|
|
|
var response = await sutProvider.Sut.Put(organization.Id, group.Id, groupRequestModel);
|
2022-12-12 10:59:48 +01:00
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<ICurrentContext>().Received(1).ManageGroups(organization.Id);
|
|
|
|
|
await sutProvider.GetDependency<IUpdateGroupCommand>().Received(1).UpdateGroupAsync(
|
|
|
|
|
Arg.Is<Group>(g =>
|
|
|
|
|
g.OrganizationId == organization.Id && g.Name == groupRequestModel.Name &&
|
2023-02-06 10:27:40 +01:00
|
|
|
|
g.AccessAll == groupRequestModel.AccessAll),
|
2022-12-12 10:59:48 +01:00
|
|
|
|
Arg.Is<Organization>(o => o.Id == organization.Id),
|
2024-02-08 14:44:36 +01:00
|
|
|
|
Arg.Any<ICollection<CollectionAccessSelection>>(),
|
2023-01-19 17:00:54 +01:00
|
|
|
|
Arg.Any<IEnumerable<Guid>>());
|
2022-12-12 10:59:48 +01:00
|
|
|
|
Assert.Equal(groupRequestModel.Name, response.Name);
|
2023-07-14 17:18:26 +02:00
|
|
|
|
Assert.Equal(organization.Id, response.OrganizationId);
|
2022-12-12 10:59:48 +01:00
|
|
|
|
Assert.Equal(groupRequestModel.AccessAll, response.AccessAll);
|
|
|
|
|
}
|
2024-05-02 01:55:16 +02:00
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task Put_UpdateCollections_OnlyUpdatesCollectionsTheSavingUserCanUpdate(GroupRequestModel groupRequestModel,
|
|
|
|
|
Group group, Organization organization,
|
|
|
|
|
SutProvider<GroupsController> sutProvider, Guid savingUserId)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.FlexibleCollectionsV1).Returns(true);
|
|
|
|
|
Put_Setup(sutProvider, organization, group, savingUserId);
|
|
|
|
|
|
|
|
|
|
var editedCollectionId = CoreHelpers.GenerateComb();
|
|
|
|
|
var readonlyCollectionId1 = CoreHelpers.GenerateComb();
|
|
|
|
|
var readonlyCollectionId2 = CoreHelpers.GenerateComb();
|
|
|
|
|
|
|
|
|
|
var currentCollectionAccess = new List<CollectionAccessSelection>
|
|
|
|
|
{
|
|
|
|
|
new()
|
|
|
|
|
{
|
|
|
|
|
Id = editedCollectionId,
|
|
|
|
|
HidePasswords = true,
|
|
|
|
|
Manage = false,
|
|
|
|
|
ReadOnly = true
|
|
|
|
|
},
|
|
|
|
|
new()
|
|
|
|
|
{
|
|
|
|
|
Id = readonlyCollectionId1,
|
|
|
|
|
HidePasswords = false,
|
|
|
|
|
Manage = true,
|
|
|
|
|
ReadOnly = false
|
|
|
|
|
},
|
|
|
|
|
new()
|
|
|
|
|
{
|
|
|
|
|
Id = readonlyCollectionId2,
|
|
|
|
|
HidePasswords = false,
|
|
|
|
|
Manage = false,
|
|
|
|
|
ReadOnly = false
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// User is upgrading editedCollectionId to manage
|
|
|
|
|
groupRequestModel.Collections = new List<SelectionReadOnlyRequestModel>
|
|
|
|
|
{
|
|
|
|
|
new() { Id = editedCollectionId, HidePasswords = false, Manage = true, ReadOnly = false }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IGroupRepository>()
|
|
|
|
|
.GetByIdWithCollectionsAsync(group.Id)
|
|
|
|
|
.Returns(new Tuple<Group, ICollection<CollectionAccessSelection>>(group,
|
|
|
|
|
currentCollectionAccess));
|
|
|
|
|
|
|
|
|
|
var currentCollections = currentCollectionAccess
|
|
|
|
|
.Select(cas => new Collection { Id = cas.Id }).ToList();
|
|
|
|
|
sutProvider.GetDependency<ICollectionRepository>()
|
|
|
|
|
.GetManyByManyIdsAsync(Arg.Any<IEnumerable<Guid>>())
|
|
|
|
|
.Returns(currentCollections);
|
|
|
|
|
|
|
|
|
|
// Authorize the editedCollection
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), Arg.Is<Collection>(c => c.Id == editedCollectionId),
|
|
|
|
|
Arg.Is<IEnumerable<IAuthorizationRequirement>>(reqs => reqs.Contains(BulkCollectionOperations.ModifyGroupAccess)))
|
|
|
|
|
.Returns(AuthorizationResult.Success());
|
|
|
|
|
|
|
|
|
|
// Do not authorize the readonly collections
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), Arg.Is<Collection>(c => c.Id == readonlyCollectionId1 || c.Id == readonlyCollectionId2),
|
|
|
|
|
Arg.Is<IEnumerable<IAuthorizationRequirement>>(reqs => reqs.Contains(BulkCollectionOperations.ModifyGroupAccess)))
|
|
|
|
|
.Returns(AuthorizationResult.Failed());
|
|
|
|
|
|
|
|
|
|
var response = await sutProvider.Sut.Put(organization.Id, group.Id, groupRequestModel);
|
|
|
|
|
|
|
|
|
|
// Expect all collection access (modified and unmodified) to be saved
|
|
|
|
|
await sutProvider.GetDependency<ICurrentContext>().Received(1).ManageGroups(organization.Id);
|
|
|
|
|
await sutProvider.GetDependency<IUpdateGroupCommand>().Received(1).UpdateGroupAsync(
|
|
|
|
|
Arg.Is<Group>(g =>
|
|
|
|
|
g.OrganizationId == organization.Id && g.Name == groupRequestModel.Name &&
|
|
|
|
|
g.AccessAll == groupRequestModel.AccessAll),
|
|
|
|
|
Arg.Is<Organization>(o => o.Id == organization.Id),
|
|
|
|
|
Arg.Is<List<CollectionAccessSelection>>(cas =>
|
|
|
|
|
cas.Select(c => c.Id).SequenceEqual(currentCollectionAccess.Select(c => c.Id)) &&
|
|
|
|
|
cas.First(c => c.Id == editedCollectionId).Manage == true &&
|
|
|
|
|
cas.First(c => c.Id == editedCollectionId).ReadOnly == false &&
|
|
|
|
|
cas.First(c => c.Id == editedCollectionId).HidePasswords == false),
|
|
|
|
|
Arg.Any<IEnumerable<Guid>>());
|
|
|
|
|
Assert.Equal(groupRequestModel.Name, response.Name);
|
|
|
|
|
Assert.Equal(organization.Id, response.OrganizationId);
|
|
|
|
|
Assert.Equal(groupRequestModel.AccessAll, response.AccessAll);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task Put_UpdateCollections_ThrowsIfSavingUserCannotUpdateCollections(GroupRequestModel groupRequestModel,
|
|
|
|
|
Group group, Organization organization,
|
|
|
|
|
SutProvider<GroupsController> sutProvider, Guid savingUserId)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.FlexibleCollectionsV1).Returns(true);
|
|
|
|
|
Put_Setup(sutProvider, organization, group, savingUserId);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IGroupRepository>()
|
|
|
|
|
.GetByIdWithCollectionsAsync(group.Id)
|
|
|
|
|
.Returns(new Tuple<Group, ICollection<CollectionAccessSelection>>(group,
|
|
|
|
|
groupRequestModel.Collections.Select(cas => cas.ToSelectionReadOnly()).ToList()));
|
|
|
|
|
var collections = groupRequestModel.Collections.Select(cas => new Collection { Id = cas.Id }).ToList();
|
|
|
|
|
sutProvider.GetDependency<ICollectionRepository>()
|
|
|
|
|
.GetManyByManyIdsAsync(Arg.Is<IEnumerable<Guid>>(guids => guids.SequenceEqual(collections.Select(c => c.Id))))
|
|
|
|
|
.Returns(collections);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), Arg.Is<Collection>(c => collections.Contains(c)),
|
|
|
|
|
Arg.Is<IEnumerable<IAuthorizationRequirement>>(reqs => reqs.Contains(BulkCollectionOperations.ModifyGroupAccess)))
|
|
|
|
|
.Returns(AuthorizationResult.Failed());
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.Put(organization.Id, group.Id, groupRequestModel));
|
|
|
|
|
Assert.Contains("You must have Can Manage permission", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Put_Setup(SutProvider<GroupsController> sutProvider, Organization organization,
|
|
|
|
|
Group group, Guid savingUserId)
|
|
|
|
|
{
|
|
|
|
|
var orgId = organization.Id = group.OrganizationId;
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageGroups(orgId).Returns(true);
|
|
|
|
|
sutProvider.GetDependency<IApplicationCacheService>().GetOrganizationAbilityAsync(orgId)
|
|
|
|
|
.Returns(new OrganizationAbility
|
|
|
|
|
{
|
|
|
|
|
Id = organization.Id,
|
|
|
|
|
AllowAdminAccessToAllCollectionItems = false
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IGroupRepository>().GetManyUserIdsByIdAsync(group.Id).Returns(new List<Guid>());
|
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(Arg.Any<ClaimsPrincipal>()).Returns(savingUserId);
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetByOrganizationAsync(orgId, savingUserId).Returns(new OrganizationUser
|
|
|
|
|
{
|
|
|
|
|
Id = savingUserId
|
|
|
|
|
});
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
}
|
2022-12-12 10:59:48 +01:00
|
|
|
|
}
|