mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 07:36:14 -05:00
[EC-654] Create commands for Group Create and Group Update (#2442)
* [EC-654] Add CreateGroupCommand and UpdateGroupCommand Added new CQRS commands CreateGroupCommand and UpdateGroupCommand Updated GroupService to use new commands Edited existing GroupServiceTests and added new tests for the new commands * [EC-654] dotnet format * [EC-654] Replace GroupService.SaveAsync with CreateGroup and UpdateGroup commands * [EC-654] Add assertions to check calls on IReferenceEventService * [EC-654] Use AssertHelper.AssertRecent for DateTime properties * [EC-654] Extracted database reads from CreateGroupCommand and UpdateGroupCommand. Added unit tests. * [EC-654] Changed CreateGroupCommand and UpdateGroupCommand Validate method to private
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.OrganizationFeatures.Groups.Interfaces;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Scim.Context;
|
||||
using Bit.Scim.Groups;
|
||||
using Bit.Scim.Models;
|
||||
@ -20,7 +20,7 @@ public class PostGroupCommandTests
|
||||
{
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task PostGroup_Success(SutProvider<PostGroupCommand> sutProvider, string displayName, string externalId, Guid organizationId, ICollection<Group> groups)
|
||||
public async Task PostGroup_Success(SutProvider<PostGroupCommand> sutProvider, string displayName, string externalId, Organization organization, ICollection<Group> groups)
|
||||
{
|
||||
var scimGroupRequestModel = new ScimGroupRequestModel
|
||||
{
|
||||
@ -32,26 +32,26 @@ public class PostGroupCommandTests
|
||||
|
||||
var expectedResult = new Group
|
||||
{
|
||||
OrganizationId = organizationId,
|
||||
OrganizationId = organization.Id,
|
||||
Name = displayName,
|
||||
ExternalId = externalId,
|
||||
};
|
||||
|
||||
sutProvider.GetDependency<IGroupRepository>()
|
||||
.GetManyByOrganizationIdAsync(organizationId)
|
||||
.GetManyByOrganizationIdAsync(organization.Id)
|
||||
.Returns(groups);
|
||||
|
||||
var group = await sutProvider.Sut.PostGroupAsync(organizationId, scimGroupRequestModel);
|
||||
var group = await sutProvider.Sut.PostGroupAsync(organization, scimGroupRequestModel);
|
||||
|
||||
await sutProvider.GetDependency<IGroupService>().Received(1).SaveAsync(group, EventSystemUser.SCIM, null);
|
||||
await sutProvider.GetDependency<IGroupRepository>().Received(0).UpdateUsersAsync(Arg.Any<Guid>(), Arg.Any<IEnumerable<Guid>>());
|
||||
await sutProvider.GetDependency<ICreateGroupCommand>().Received(1).CreateGroupAsync(group, organization, EventSystemUser.SCIM, null);
|
||||
await sutProvider.GetDependency<IGroupRepository>().DidNotReceiveWithAnyArgs().UpdateUsersAsync(default, default);
|
||||
|
||||
AssertHelper.AssertPropertyEqual(expectedResult, group, "Id", "CreationDate", "RevisionDate");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task PostGroup_WithMembers_Success(SutProvider<PostGroupCommand> sutProvider, string displayName, string externalId, Guid organizationId, ICollection<Group> groups, IEnumerable<Guid> membersUserIds)
|
||||
public async Task PostGroup_WithMembers_Success(SutProvider<PostGroupCommand> sutProvider, string displayName, string externalId, Organization organization, ICollection<Group> groups, IEnumerable<Guid> membersUserIds)
|
||||
{
|
||||
var scimGroupRequestModel = new ScimGroupRequestModel
|
||||
{
|
||||
@ -63,22 +63,22 @@ public class PostGroupCommandTests
|
||||
|
||||
var expectedResult = new Group
|
||||
{
|
||||
OrganizationId = organizationId,
|
||||
OrganizationId = organization.Id,
|
||||
Name = displayName,
|
||||
ExternalId = externalId
|
||||
};
|
||||
|
||||
sutProvider.GetDependency<IGroupRepository>()
|
||||
.GetManyByOrganizationIdAsync(organizationId)
|
||||
.GetManyByOrganizationIdAsync(organization.Id)
|
||||
.Returns(groups);
|
||||
|
||||
sutProvider.GetDependency<IScimContext>()
|
||||
.RequestScimProvider
|
||||
.Returns(Core.Enums.ScimProviderType.Okta);
|
||||
|
||||
var group = await sutProvider.Sut.PostGroupAsync(organizationId, scimGroupRequestModel);
|
||||
var group = await sutProvider.Sut.PostGroupAsync(organization, scimGroupRequestModel);
|
||||
|
||||
await sutProvider.GetDependency<IGroupService>().Received(1).SaveAsync(group, EventSystemUser.SCIM, null);
|
||||
await sutProvider.GetDependency<ICreateGroupCommand>().Received(1).CreateGroupAsync(group, organization, EventSystemUser.SCIM, null);
|
||||
await sutProvider.GetDependency<IGroupRepository>().Received(1).UpdateUsersAsync(Arg.Any<Guid>(), Arg.Is<IEnumerable<Guid>>(arg => arg.All(id => membersUserIds.Contains(id))));
|
||||
|
||||
AssertHelper.AssertPropertyEqual(expectedResult, group, "Id", "CreationDate", "RevisionDate");
|
||||
@ -88,7 +88,7 @@ public class PostGroupCommandTests
|
||||
[BitAutoData((string)null)]
|
||||
[BitAutoData("")]
|
||||
[BitAutoData(" ")]
|
||||
public async Task PostGroup_NullDisplayName_Throws(string displayName, SutProvider<PostGroupCommand> sutProvider, Guid organizationId)
|
||||
public async Task PostGroup_NullDisplayName_Throws(string displayName, SutProvider<PostGroupCommand> sutProvider, Organization organization)
|
||||
{
|
||||
var scimGroupRequestModel = new ScimGroupRequestModel
|
||||
{
|
||||
@ -98,12 +98,12 @@ public class PostGroupCommandTests
|
||||
Schemas = new List<string> { ScimConstants.Scim2SchemaUser }
|
||||
};
|
||||
|
||||
await Assert.ThrowsAsync<BadRequestException>(async () => await sutProvider.Sut.PostGroupAsync(organizationId, scimGroupRequestModel));
|
||||
await Assert.ThrowsAsync<BadRequestException>(async () => await sutProvider.Sut.PostGroupAsync(organization, scimGroupRequestModel));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task PostGroup_ExistingExternalId_Throws(string displayName, SutProvider<PostGroupCommand> sutProvider, Guid organizationId, ICollection<Group> groups)
|
||||
public async Task PostGroup_ExistingExternalId_Throws(string displayName, SutProvider<PostGroupCommand> sutProvider, Organization organization, ICollection<Group> groups)
|
||||
{
|
||||
var scimGroupRequestModel = new ScimGroupRequestModel
|
||||
{
|
||||
@ -114,9 +114,9 @@ public class PostGroupCommandTests
|
||||
};
|
||||
|
||||
sutProvider.GetDependency<IGroupRepository>()
|
||||
.GetManyByOrganizationIdAsync(organizationId)
|
||||
.GetManyByOrganizationIdAsync(organization.Id)
|
||||
.Returns(groups);
|
||||
|
||||
await Assert.ThrowsAsync<ConflictException>(async () => await sutProvider.Sut.PostGroupAsync(organizationId, scimGroupRequestModel));
|
||||
await Assert.ThrowsAsync<ConflictException>(async () => await sutProvider.Sut.PostGroupAsync(organization, scimGroupRequestModel));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user