diff --git a/src/Core/AdminConsole/Services/IPolicyServicevNext.cs b/src/Core/AdminConsole/Services/IPolicyServicevNext.cs index 6366bc552..a1b03436a 100644 --- a/src/Core/AdminConsole/Services/IPolicyServicevNext.cs +++ b/src/Core/AdminConsole/Services/IPolicyServicevNext.cs @@ -1,9 +1,4 @@ using Bit.Core.AdminConsole.Entities; -using Bit.Core.AdminConsole.Enums; -using Bit.Core.AdminConsole.Models.Data.Organizations.Policies; -using Bit.Core.Entities; -using Bit.Core.Enums; -using Bit.Core.Models.Data.Organizations.OrganizationUsers; using Bit.Core.Services; namespace Bit.Core.AdminConsole.Services; diff --git a/test/Core.Test/AdminConsole/AutoFixture/PolicyServicevNextCustomization.cs b/test/Core.Test/AdminConsole/AutoFixture/PolicyServicevNextCustomization.cs new file mode 100644 index 000000000..8a7ea9d8a --- /dev/null +++ b/test/Core.Test/AdminConsole/AutoFixture/PolicyServicevNextCustomization.cs @@ -0,0 +1,45 @@ +using System.Reflection; +using AutoFixture; +using AutoFixture.Kernel; +using Bit.Core.AdminConsole.OrganizationFeatures.Policies; +using Bit.Core.AdminConsole.Services.Implementations; +using Bit.Test.Common.AutoFixture.Attributes; + +namespace Bit.Core.Test.AdminConsole.AutoFixture; + +/// +/// Override autofixture and set the injected PolicyDefinitions to an empty array. +/// This prevents Autofixture from creating duplicate PolicyDefinitions which will throw an error. +/// +public class PolicyServicevNextBuilder : ISpecimenBuilder +{ + public object Create(object request, ISpecimenContext context) + { + var pi = request as ParameterInfo; + if (pi == null) + return new NoSpecimen(); + + if (pi.Member.DeclaringType != typeof(PolicyServicevNext) || + pi.ParameterType != typeof(IEnumerable)) + return new NoSpecimen(); + + return Array.Empty(); + } +} + +public class PolicyServicevNextCustomization : ICustomization +{ + public void Customize(IFixture fixture) + { + fixture.Customizations.Add(new PolicyServicevNextBuilder()); + } +} + +/// +/// A customization for PolicyService that sets the injected PolicyDefinitions to an empty array. +/// This prevents Autofixture from creating duplicate PolicyDefinitions which will throw an error. +/// +public class PolicyServicevNextCustomizeAttribute : BitCustomizeAttribute +{ + public override ICustomization GetCustomization() => new PolicyServicevNextCustomization(); +} diff --git a/test/Core.Test/AdminConsole/Services/PolicyServicevNextTests.cs b/test/Core.Test/AdminConsole/Services/PolicyServicevNextTests.cs new file mode 100644 index 000000000..0b8c31c81 --- /dev/null +++ b/test/Core.Test/AdminConsole/Services/PolicyServicevNextTests.cs @@ -0,0 +1,72 @@ +using Bit.Core.AdminConsole.Entities; +using Bit.Core.AdminConsole.Repositories; +using Bit.Core.AdminConsole.Services.Implementations; +using Bit.Core.Exceptions; +using Bit.Core.Models.Data.Organizations; +using Bit.Core.Services; +using Bit.Core.Test.AdminConsole.AutoFixture; +using Bit.Test.Common.AutoFixture; +using Bit.Test.Common.AutoFixture.Attributes; +using NSubstitute; +using Xunit; + +namespace Bit.Core.Test.AdminConsole.Services; + +[SutProviderCustomize] +[PolicyServicevNextCustomize] +public class PolicyServicevNextTests +{ + [Theory, BitAutoData] + public async Task SaveAsync_OrganizationDoesNotExist_ThrowsBadRequest( + Policy policy, SutProvider sutProvider) + { + sutProvider.GetDependency() + .GetOrganizationAbilityAsync(policy.OrganizationId) + .Returns((OrganizationAbility)null); + + var badRequestException = await Assert.ThrowsAsync( + () => sutProvider.Sut.SaveAsync(policy, + Substitute.For(), + Substitute.For(), + Guid.NewGuid())); + + Assert.Contains("Organization not found", badRequestException.Message, StringComparison.OrdinalIgnoreCase); + + await sutProvider.GetDependency() + .DidNotReceiveWithAnyArgs() + .UpsertAsync(default); + + await sutProvider.GetDependency() + .DidNotReceiveWithAnyArgs() + .LogPolicyEventAsync(default, default, default); + } + + [Theory, BitAutoData] + public async Task SaveAsync_OrganizationCannotUsePolicies_ThrowsBadRequest( + Policy policy, SutProvider sutProvider) + { + sutProvider.GetDependency() + .GetOrganizationAbilityAsync(policy.OrganizationId) + .Returns(new OrganizationAbility + { + Id = policy.OrganizationId, + UsePolicies = false + }); + + var badRequestException = await Assert.ThrowsAsync( + () => sutProvider.Sut.SaveAsync(policy, + Substitute.For(), + Substitute.For(), + Guid.NewGuid())); + + Assert.Contains("cannot use policies", badRequestException.Message, StringComparison.OrdinalIgnoreCase); + + await sutProvider.GetDependency() + .DidNotReceiveWithAnyArgs() + .UpsertAsync(default); + + await sutProvider.GetDependency() + .DidNotReceiveWithAnyArgs() + .LogPolicyEventAsync(default, default, default); + } +}