1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-28 13:15:12 +01:00
bitwarden-server/bitwarden_license/test/Scim.Test/Groups/PostGroupCommandTests.cs
Thomas Rittson 9021236d61
AC Team code ownership moves: Organization pt. 1 (#3472)
* move Organization.cs files to AC Team code ownership
2023-11-28 17:18:08 -06:00

124 lines
4.9 KiB
C#

using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Enums;
using Bit.Core.AdminConsole.OrganizationFeatures.Groups.Interfaces;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Enums;
using Bit.Core.Exceptions;
using Bit.Scim.Context;
using Bit.Scim.Groups;
using Bit.Scim.Models;
using Bit.Scim.Utilities;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
using Bit.Test.Common.Helpers;
using NSubstitute;
using Xunit;
namespace Bit.Scim.Test.Groups;
[SutProviderCustomize]
public class PostGroupCommandTests
{
[Theory]
[BitAutoData]
public async Task PostGroup_Success(SutProvider<PostGroupCommand> sutProvider, string displayName, string externalId, Organization organization, ICollection<Group> groups)
{
var scimGroupRequestModel = new ScimGroupRequestModel
{
DisplayName = displayName,
ExternalId = externalId,
Members = new List<ScimGroupRequestModel.GroupMembersModel>(),
Schemas = new List<string> { ScimConstants.Scim2SchemaUser }
};
var expectedResult = new Group
{
OrganizationId = organization.Id,
Name = displayName,
ExternalId = externalId,
};
sutProvider.GetDependency<IGroupRepository>()
.GetManyByOrganizationIdAsync(organization.Id)
.Returns(groups);
var group = await sutProvider.Sut.PostGroupAsync(organization, scimGroupRequestModel);
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, Organization organization, ICollection<Group> groups, IEnumerable<Guid> membersUserIds)
{
var scimGroupRequestModel = new ScimGroupRequestModel
{
DisplayName = displayName,
ExternalId = externalId,
Members = membersUserIds.Select(uid => new ScimGroupRequestModel.GroupMembersModel { Value = uid.ToString() }).ToList(),
Schemas = new List<string> { ScimConstants.Scim2SchemaUser }
};
var expectedResult = new Group
{
OrganizationId = organization.Id,
Name = displayName,
ExternalId = externalId
};
sutProvider.GetDependency<IGroupRepository>()
.GetManyByOrganizationIdAsync(organization.Id)
.Returns(groups);
sutProvider.GetDependency<IScimContext>()
.RequestScimProvider
.Returns(ScimProviderType.Okta);
var group = await sutProvider.Sut.PostGroupAsync(organization, scimGroupRequestModel);
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");
}
[Theory]
[BitAutoData((string)null)]
[BitAutoData("")]
[BitAutoData(" ")]
public async Task PostGroup_NullDisplayName_Throws(string displayName, SutProvider<PostGroupCommand> sutProvider, Organization organization)
{
var scimGroupRequestModel = new ScimGroupRequestModel
{
DisplayName = displayName,
ExternalId = Guid.NewGuid().ToString(),
Members = new List<ScimGroupRequestModel.GroupMembersModel>(),
Schemas = new List<string> { ScimConstants.Scim2SchemaUser }
};
await Assert.ThrowsAsync<BadRequestException>(async () => await sutProvider.Sut.PostGroupAsync(organization, scimGroupRequestModel));
}
[Theory]
[BitAutoData]
public async Task PostGroup_ExistingExternalId_Throws(string displayName, SutProvider<PostGroupCommand> sutProvider, Organization organization, ICollection<Group> groups)
{
var scimGroupRequestModel = new ScimGroupRequestModel
{
DisplayName = displayName,
ExternalId = groups.First().ExternalId,
Members = new List<ScimGroupRequestModel.GroupMembersModel>(),
Schemas = new List<string> { ScimConstants.Scim2SchemaUser }
};
sutProvider.GetDependency<IGroupRepository>()
.GetManyByOrganizationIdAsync(organization.Id)
.Returns(groups);
await Assert.ThrowsAsync<ConflictException>(async () => await sutProvider.Sut.PostGroupAsync(organization, scimGroupRequestModel));
}
}