1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-01 13:43:23 +01:00

Simplify autofixture

This commit is contained in:
Thomas Rittson 2024-10-07 07:56:16 +10:00
parent aa4c126d69
commit 70139ab97d
No known key found for this signature in database
GPG Key ID: CDDDA03861C35E27
2 changed files with 14 additions and 36 deletions

View File

@ -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;
/// <summary>
/// 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.
/// </summary>
public class PolicyServicevNextBuilder : ISpecimenBuilder
{
@ -35,26 +38,3 @@ public class PolicyServicevNextBuilder : ISpecimenBuilder
return _policyDefinitions;
}
}
public class PolicyServicevNextCustomization : ICustomization
{
private readonly IEnumerable<IPolicyDefinition> _policyDefinitions;
public PolicyServicevNextCustomization(IEnumerable<IPolicyDefinition> policyDefinitions = null)
{
_policyDefinitions = policyDefinitions ?? new List<IPolicyDefinition>();
}
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new PolicyServicevNextBuilder(_policyDefinitions));
}
}
/// <summary>
/// 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.
/// </summary>
public class PolicyServicevNextCustomizeAttribute : BitCustomizeAttribute
{
public override ICustomization GetCustomization() => new PolicyServicevNextCustomization();
}

View File

@ -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<PolicyServicevNext> sutProvider)
Policy policy)
{
var sutProvider = SutProviderFactory();
sutProvider.GetDependency<IApplicationCacheService>()
.GetOrganizationAbilityAsync(policy.OrganizationId)
.Returns((OrganizationAbility)null);
@ -48,8 +46,9 @@ public class PolicyServicevNextTests
[Theory, BitAutoData]
public async Task SaveAsync_OrganizationCannotUsePolicies_ThrowsBadRequest(
Policy policy, SutProvider<PolicyServicevNext> sutProvider)
Policy policy)
{
var sutProvider = SutProviderFactory();
sutProvider.GetDependency<IApplicationCacheService>()
.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<PolicyServicevNext> 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<IApplicationCacheService>()
.GetOrganizationAbilityAsync(policy.OrganizationId)
@ -113,10 +111,10 @@ public class PolicyServicevNextTests
/// <summary>
/// Returns a new SutProvider with the PolicyDefinitions registered in the Sut.
/// </summary>
private SutProvider<PolicyServicevNext> RegisterPolicyDefinitions(IEnumerable<IPolicyDefinition> policyDefinitions)
private static SutProvider<PolicyServicevNext> SutProviderFactory(IEnumerable<IPolicyDefinition> policyDefinitions = null)
{
var fixture = new Fixture();
fixture.Customizations.Add(new PolicyServicevNextBuilder(policyDefinitions));
fixture.Customizations.Add(new PolicyServicevNextBuilder(policyDefinitions ?? new List<IPolicyDefinition>()));
var sutProvider = new SutProvider<PolicyServicevNext>(fixture);
sutProvider.Create();
return sutProvider;