mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
[AC-2847] Simplify OrganizationUser and Group PUT methods and tests (#4479)
* refactor controller logic * add additional validation checks to update commands * refactor and improve tests
This commit is contained in:
@ -1,11 +1,14 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.AdminConsole.OrganizationFeatures.Groups;
|
||||
using Bit.Core.AdminConsole.Repositories;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Test.AutoFixture.OrganizationFixtures;
|
||||
using Bit.Core.Utilities;
|
||||
using Bit.Test.Common.AutoFixture;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using Bit.Test.Common.Helpers;
|
||||
@ -18,10 +21,12 @@ namespace Bit.Core.Test.AdminConsole.OrganizationFeatures.Groups;
|
||||
public class UpdateGroupCommandTests
|
||||
{
|
||||
[Theory, OrganizationCustomize(UseGroups = true), BitAutoData]
|
||||
public async Task UpdateGroup_Success(SutProvider<UpdateGroupCommand> sutProvider, Group group, Organization organization)
|
||||
public async Task UpdateGroup_Success(SutProvider<UpdateGroupCommand> sutProvider, Group group, Group oldGroup,
|
||||
Organization organization)
|
||||
{
|
||||
// Deprecated with Flexible Collections
|
||||
group.AccessAll = false;
|
||||
ArrangeGroup(sutProvider, group, oldGroup);
|
||||
ArrangeUsers(sutProvider, group);
|
||||
ArrangeCollections(sutProvider, group);
|
||||
|
||||
await sutProvider.Sut.UpdateGroupAsync(group, organization);
|
||||
|
||||
@ -31,10 +36,12 @@ public class UpdateGroupCommandTests
|
||||
}
|
||||
|
||||
[Theory, OrganizationCustomize(UseGroups = true), BitAutoData]
|
||||
public async Task UpdateGroup_WithCollections_Success(SutProvider<UpdateGroupCommand> sutProvider, Group group, Organization organization, List<CollectionAccessSelection> collections)
|
||||
public async Task UpdateGroup_WithCollections_Success(SutProvider<UpdateGroupCommand> sutProvider, Group group,
|
||||
Group oldGroup, Organization organization, List<CollectionAccessSelection> collections)
|
||||
{
|
||||
// Deprecated with Flexible Collections
|
||||
group.AccessAll = false;
|
||||
ArrangeGroup(sutProvider, group, oldGroup);
|
||||
ArrangeUsers(sutProvider, group);
|
||||
ArrangeCollections(sutProvider, group);
|
||||
|
||||
// Arrange list of collections to make sure Manage is mutually exclusive
|
||||
for (var i = 0; i < collections.Count; i++)
|
||||
@ -53,10 +60,12 @@ public class UpdateGroupCommandTests
|
||||
}
|
||||
|
||||
[Theory, OrganizationCustomize(UseGroups = true), BitAutoData]
|
||||
public async Task UpdateGroup_WithEventSystemUser_Success(SutProvider<UpdateGroupCommand> sutProvider, Group group, Organization organization, EventSystemUser eventSystemUser)
|
||||
public async Task UpdateGroup_WithEventSystemUser_Success(SutProvider<UpdateGroupCommand> sutProvider, Group group,
|
||||
Group oldGroup, Organization organization, EventSystemUser eventSystemUser)
|
||||
{
|
||||
// Deprecated with Flexible Collections
|
||||
group.AccessAll = false;
|
||||
ArrangeGroup(sutProvider, group, oldGroup);
|
||||
ArrangeUsers(sutProvider, group);
|
||||
ArrangeCollections(sutProvider, group);
|
||||
|
||||
await sutProvider.Sut.UpdateGroupAsync(group, organization, eventSystemUser);
|
||||
|
||||
@ -66,19 +75,27 @@ public class UpdateGroupCommandTests
|
||||
}
|
||||
|
||||
[Theory, OrganizationCustomize(UseGroups = true), BitAutoData]
|
||||
public async Task UpdateGroup_WithNullOrganization_Throws(SutProvider<UpdateGroupCommand> sutProvider, Group group, EventSystemUser eventSystemUser)
|
||||
public async Task UpdateGroup_WithNullOrganization_Throws(SutProvider<UpdateGroupCommand> sutProvider, Group group,
|
||||
Group oldGroup, EventSystemUser eventSystemUser)
|
||||
{
|
||||
var exception = await Assert.ThrowsAsync<BadRequestException>(async () => await sutProvider.Sut.UpdateGroupAsync(group, null, eventSystemUser));
|
||||
ArrangeGroup(sutProvider, group, oldGroup);
|
||||
ArrangeUsers(sutProvider, group);
|
||||
ArrangeCollections(sutProvider, group);
|
||||
|
||||
Assert.Contains("Organization not found", exception.Message);
|
||||
await Assert.ThrowsAsync<NotFoundException>(async () => await sutProvider.Sut.UpdateGroupAsync(group, null, eventSystemUser));
|
||||
|
||||
await sutProvider.GetDependency<IGroupRepository>().DidNotReceiveWithAnyArgs().CreateAsync(default);
|
||||
await sutProvider.GetDependency<IEventService>().DidNotReceiveWithAnyArgs().LogGroupEventAsync(default, default, default);
|
||||
}
|
||||
|
||||
[Theory, OrganizationCustomize(UseGroups = false), BitAutoData]
|
||||
public async Task UpdateGroup_WithUseGroupsAsFalse_Throws(SutProvider<UpdateGroupCommand> sutProvider, Organization organization, Group group, EventSystemUser eventSystemUser)
|
||||
public async Task UpdateGroup_WithUseGroupsAsFalse_Throws(SutProvider<UpdateGroupCommand> sutProvider,
|
||||
Organization organization, Group group, Group oldGroup, EventSystemUser eventSystemUser)
|
||||
{
|
||||
ArrangeGroup(sutProvider, group, oldGroup);
|
||||
ArrangeUsers(sutProvider, group);
|
||||
ArrangeCollections(sutProvider, group);
|
||||
|
||||
var exception = await Assert.ThrowsAsync<BadRequestException>(async () => await sutProvider.Sut.UpdateGroupAsync(group, organization, eventSystemUser));
|
||||
|
||||
Assert.Contains("This organization cannot use groups", exception.Message);
|
||||
@ -89,8 +106,12 @@ public class UpdateGroupCommandTests
|
||||
|
||||
[Theory, OrganizationCustomize(UseGroups = true), BitAutoData]
|
||||
public async Task UpdateGroup_WithAccessAll_Throws(
|
||||
SutProvider<UpdateGroupCommand> sutProvider, Organization organization, Group group)
|
||||
SutProvider<UpdateGroupCommand> sutProvider, Organization organization, Group group, Group oldGroup)
|
||||
{
|
||||
ArrangeGroup(sutProvider, group, oldGroup);
|
||||
ArrangeUsers(sutProvider, group);
|
||||
ArrangeCollections(sutProvider, group);
|
||||
|
||||
group.AccessAll = true;
|
||||
|
||||
var exception =
|
||||
@ -100,4 +121,123 @@ public class UpdateGroupCommandTests
|
||||
await sutProvider.GetDependency<IGroupRepository>().DidNotReceiveWithAnyArgs().CreateAsync(default);
|
||||
await sutProvider.GetDependency<IEventService>().DidNotReceiveWithAnyArgs().LogGroupEventAsync(default, default, default);
|
||||
}
|
||||
|
||||
|
||||
[Theory, OrganizationCustomize(UseGroups = true, FlexibleCollections = true), BitAutoData]
|
||||
public async Task UpdateGroup_GroupBelongsToDifferentOrganization_Throws(SutProvider<UpdateGroupCommand> sutProvider,
|
||||
Group group, Group oldGroup, Organization organization)
|
||||
{
|
||||
group.AccessAll = false;
|
||||
ArrangeGroup(sutProvider, group, oldGroup);
|
||||
ArrangeUsers(sutProvider, group);
|
||||
ArrangeCollections(sutProvider, group);
|
||||
|
||||
// Mismatching orgId
|
||||
oldGroup.OrganizationId = CoreHelpers.GenerateComb();
|
||||
|
||||
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.UpdateGroupAsync(group, organization));
|
||||
}
|
||||
|
||||
[Theory, OrganizationCustomize(UseGroups = true, FlexibleCollections = true), BitAutoData]
|
||||
public async Task UpdateGroup_CollectionsBelongsToDifferentOrganization_Throws(SutProvider<UpdateGroupCommand> sutProvider,
|
||||
Group group, Group oldGroup, Organization organization, List<CollectionAccessSelection> collectionAccess)
|
||||
{
|
||||
group.AccessAll = false;
|
||||
ArrangeGroup(sutProvider, group, oldGroup);
|
||||
ArrangeUsers(sutProvider, group);
|
||||
|
||||
sutProvider.GetDependency<ICollectionRepository>()
|
||||
.GetManyByManyIdsAsync(Arg.Any<IEnumerable<Guid>>())
|
||||
.Returns(callInfo => callInfo.Arg<IEnumerable<Guid>>()
|
||||
.Select(guid => new Collection { Id = guid, OrganizationId = CoreHelpers.GenerateComb() }).ToList());
|
||||
|
||||
await Assert.ThrowsAsync<NotFoundException>(
|
||||
() => sutProvider.Sut.UpdateGroupAsync(group, organization, collectionAccess));
|
||||
}
|
||||
|
||||
[Theory, OrganizationCustomize(UseGroups = true, FlexibleCollections = true), BitAutoData]
|
||||
public async Task UpdateGroup_CollectionsDoNotExist_Throws(SutProvider<UpdateGroupCommand> sutProvider,
|
||||
Group group, Group oldGroup, Organization organization, List<CollectionAccessSelection> collectionAccess)
|
||||
{
|
||||
group.AccessAll = false;
|
||||
ArrangeGroup(sutProvider, group, oldGroup);
|
||||
ArrangeUsers(sutProvider, group);
|
||||
|
||||
// Return result is missing a collection
|
||||
sutProvider.GetDependency<ICollectionRepository>()
|
||||
.GetManyByManyIdsAsync(Arg.Any<IEnumerable<Guid>>())
|
||||
.Returns(callInfo =>
|
||||
{
|
||||
var result = callInfo.Arg<IEnumerable<Guid>>()
|
||||
.Select(guid => new Collection { Id = guid, OrganizationId = group.OrganizationId }).ToList();
|
||||
result.RemoveAt(0);
|
||||
return result;
|
||||
});
|
||||
|
||||
await Assert.ThrowsAsync<NotFoundException>(
|
||||
() => sutProvider.Sut.UpdateGroupAsync(group, organization, collectionAccess));
|
||||
}
|
||||
|
||||
[Theory, OrganizationCustomize(UseGroups = true, FlexibleCollections = true), BitAutoData]
|
||||
public async Task UpdateGroup_MemberBelongsToDifferentOrganization_Throws(SutProvider<UpdateGroupCommand> sutProvider,
|
||||
Group group, Group oldGroup, Organization organization, IEnumerable<Guid> userAccess)
|
||||
{
|
||||
group.AccessAll = false;
|
||||
ArrangeGroup(sutProvider, group, oldGroup);
|
||||
ArrangeCollections(sutProvider, group);
|
||||
|
||||
sutProvider.GetDependency<IOrganizationUserRepository>()
|
||||
.GetManyAsync(Arg.Any<IEnumerable<Guid>>())
|
||||
.Returns(callInfo => callInfo.Arg<IEnumerable<Guid>>()
|
||||
.Select(guid => new OrganizationUser { Id = guid, OrganizationId = CoreHelpers.GenerateComb() }).ToList());
|
||||
|
||||
await Assert.ThrowsAsync<NotFoundException>(
|
||||
() => sutProvider.Sut.UpdateGroupAsync(group, organization, null, userAccess));
|
||||
}
|
||||
|
||||
[Theory, OrganizationCustomize(UseGroups = true, FlexibleCollections = true), BitAutoData]
|
||||
public async Task UpdateGroup_MemberDoesNotExist_Throws(SutProvider<UpdateGroupCommand> sutProvider,
|
||||
Group group, Group oldGroup, Organization organization, IEnumerable<Guid> userAccess)
|
||||
{
|
||||
ArrangeGroup(sutProvider, group, oldGroup);
|
||||
ArrangeCollections(sutProvider, group);
|
||||
|
||||
sutProvider.GetDependency<IOrganizationUserRepository>()
|
||||
.GetManyAsync(Arg.Any<IEnumerable<Guid>>())
|
||||
.Returns(callInfo =>
|
||||
{
|
||||
var result = callInfo.Arg<IEnumerable<Guid>>()
|
||||
.Select(guid => new OrganizationUser { Id = guid, OrganizationId = group.OrganizationId })
|
||||
.ToList();
|
||||
result.RemoveAt(0);
|
||||
return result;
|
||||
});
|
||||
|
||||
await Assert.ThrowsAsync<NotFoundException>(
|
||||
() => sutProvider.Sut.UpdateGroupAsync(group, organization, null, userAccess));
|
||||
}
|
||||
|
||||
private void ArrangeGroup(SutProvider<UpdateGroupCommand> sutProvider, Group group, Group oldGroup)
|
||||
{
|
||||
group.AccessAll = false;
|
||||
oldGroup.OrganizationId = group.OrganizationId;
|
||||
oldGroup.Id = group.Id;
|
||||
sutProvider.GetDependency<IGroupRepository>().GetByIdAsync(group.Id).Returns(oldGroup);
|
||||
}
|
||||
|
||||
private void ArrangeCollections(SutProvider<UpdateGroupCommand> sutProvider, Group group)
|
||||
{
|
||||
sutProvider.GetDependency<ICollectionRepository>()
|
||||
.GetManyByManyIdsAsync(Arg.Any<IEnumerable<Guid>>())
|
||||
.Returns(callInfo => callInfo.Arg<IEnumerable<Guid>>()
|
||||
.Select(guid => new Collection() { Id = guid, OrganizationId = group.OrganizationId }).ToList());
|
||||
}
|
||||
|
||||
private void ArrangeUsers(SutProvider<UpdateGroupCommand> sutProvider, Group group)
|
||||
{
|
||||
sutProvider.GetDependency<IOrganizationUserRepository>()
|
||||
.GetManyAsync(Arg.Any<IEnumerable<Guid>>())
|
||||
.Returns(callInfo => callInfo.Arg<IEnumerable<Guid>>()
|
||||
.Select(guid => new OrganizationUser { Id = guid, OrganizationId = group.OrganizationId }).ToList());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user