diff --git a/test/Core.Test/AdminConsole/AutoFixture/PolicyServicevNextCustomization.cs b/test/Core.Test/AdminConsole/AutoFixture/PolicyServicevNextCustomization.cs index df8b49068..797f4ec60 100644 --- a/test/Core.Test/AdminConsole/AutoFixture/PolicyServicevNextCustomization.cs +++ b/test/Core.Test/AdminConsole/AutoFixture/PolicyServicevNextCustomization.cs @@ -1,4 +1,6 @@ -using System.Reflection; +#nullable enable + +using System.Reflection; using AutoFixture; using AutoFixture.Kernel; using Bit.Core.AdminConsole.OrganizationFeatures.Policies; @@ -8,8 +10,9 @@ 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. +/// Configures Autofixture to inject the provided IPolicyDefinition implementations into the PolicyService constructor. +/// Note that this should usually be used even to inject an empty list, otherwise AutoFixture will create duplicate +/// invalid IPolicyDefinitions. /// public class PolicyServicevNextBuilder : ISpecimenBuilder { @@ -35,26 +38,3 @@ public class PolicyServicevNextBuilder : ISpecimenBuilder return _policyDefinitions; } } - -public class PolicyServicevNextCustomization : ICustomization -{ - private readonly IEnumerable _policyDefinitions; - - public PolicyServicevNextCustomization(IEnumerable policyDefinitions = null) - { - _policyDefinitions = policyDefinitions ?? new List(); - } - public void Customize(IFixture fixture) - { - fixture.Customizations.Add(new PolicyServicevNextBuilder(_policyDefinitions)); - } -} - -/// -/// 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 index 77d75ca44..8401f59e8 100644 --- a/test/Core.Test/AdminConsole/Services/PolicyServicevNextTests.cs +++ b/test/Core.Test/AdminConsole/Services/PolicyServicevNextTests.cs @@ -12,19 +12,17 @@ using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; using AdminConsoleFixtures = Bit.Core.Test.AdminConsole.AutoFixture; using NSubstitute; -using NSubstitute.ExceptionExtensions; 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) + Policy policy) { + var sutProvider = SutProviderFactory(); sutProvider.GetDependency() .GetOrganizationAbilityAsync(policy.OrganizationId) .Returns((OrganizationAbility)null); @@ -48,8 +46,9 @@ public class PolicyServicevNextTests [Theory, BitAutoData] public async Task SaveAsync_OrganizationCannotUsePolicies_ThrowsBadRequest( - Policy policy, SutProvider sutProvider) + Policy policy) { + var sutProvider = SutProviderFactory(); sutProvider.GetDependency() .GetOrganizationAbilityAsync(policy.OrganizationId) .Returns(new OrganizationAbility @@ -76,12 +75,11 @@ public class PolicyServicevNextTests } [Theory, BitAutoData] - public async Task SaveAsync_ThrowsOnValidationError( - [AdminConsoleFixtures.Policy(PolicyType.SingleOrg)] Policy policy, SutProvider sutProvider) + public async Task SaveAsync_ThrowsOnValidationError([AdminConsoleFixtures.Policy(PolicyType.SingleOrg)] Policy policy) { var fakePolicyDefinition = new FakeSingleOrgPolicyDefinition(); fakePolicyDefinition.ValidateAsyncMock(null, policy).Returns("Validation error!"); - sutProvider = RegisterPolicyDefinitions([fakePolicyDefinition]); + var sutProvider = SutProviderFactory([fakePolicyDefinition]); sutProvider.GetDependency() .GetOrganizationAbilityAsync(policy.OrganizationId) @@ -113,10 +111,10 @@ public class PolicyServicevNextTests /// /// Returns a new SutProvider with the PolicyDefinitions registered in the Sut. /// - private SutProvider RegisterPolicyDefinitions(IEnumerable policyDefinitions) + private static SutProvider SutProviderFactory(IEnumerable policyDefinitions = null) { var fixture = new Fixture(); - fixture.Customizations.Add(new PolicyServicevNextBuilder(policyDefinitions)); + fixture.Customizations.Add(new PolicyServicevNextBuilder(policyDefinitions ?? new List())); var sutProvider = new SutProvider(fixture); sutProvider.Create(); return sutProvider;