mirror of
https://github.com/bitwarden/server.git
synced 2024-12-01 13:43:23 +01:00
Simplify autofixture
This commit is contained in:
parent
aa4c126d69
commit
70139ab97d
@ -1,4 +1,6 @@
|
|||||||
using System.Reflection;
|
#nullable enable
|
||||||
|
|
||||||
|
using System.Reflection;
|
||||||
using AutoFixture;
|
using AutoFixture;
|
||||||
using AutoFixture.Kernel;
|
using AutoFixture.Kernel;
|
||||||
using Bit.Core.AdminConsole.OrganizationFeatures.Policies;
|
using Bit.Core.AdminConsole.OrganizationFeatures.Policies;
|
||||||
@ -8,8 +10,9 @@ using Bit.Test.Common.AutoFixture.Attributes;
|
|||||||
namespace Bit.Core.Test.AdminConsole.AutoFixture;
|
namespace Bit.Core.Test.AdminConsole.AutoFixture;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Override autofixture and set the injected PolicyDefinitions to an empty array.
|
/// Configures Autofixture to inject the provided IPolicyDefinition implementations into the PolicyService constructor.
|
||||||
/// This prevents Autofixture from creating duplicate PolicyDefinitions which will throw an error.
|
/// Note that this should usually be used even to inject an empty list, otherwise AutoFixture will create duplicate
|
||||||
|
/// invalid IPolicyDefinitions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class PolicyServicevNextBuilder : ISpecimenBuilder
|
public class PolicyServicevNextBuilder : ISpecimenBuilder
|
||||||
{
|
{
|
||||||
@ -35,26 +38,3 @@ public class PolicyServicevNextBuilder : ISpecimenBuilder
|
|||||||
return _policyDefinitions;
|
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();
|
|
||||||
}
|
|
||||||
|
@ -12,19 +12,17 @@ using Bit.Test.Common.AutoFixture;
|
|||||||
using Bit.Test.Common.AutoFixture.Attributes;
|
using Bit.Test.Common.AutoFixture.Attributes;
|
||||||
using AdminConsoleFixtures = Bit.Core.Test.AdminConsole.AutoFixture;
|
using AdminConsoleFixtures = Bit.Core.Test.AdminConsole.AutoFixture;
|
||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using NSubstitute.ExceptionExtensions;
|
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Bit.Core.Test.AdminConsole.Services;
|
namespace Bit.Core.Test.AdminConsole.Services;
|
||||||
|
|
||||||
[SutProviderCustomize]
|
|
||||||
[PolicyServicevNextCustomize]
|
|
||||||
public class PolicyServicevNextTests
|
public class PolicyServicevNextTests
|
||||||
{
|
{
|
||||||
[Theory, BitAutoData]
|
[Theory, BitAutoData]
|
||||||
public async Task SaveAsync_OrganizationDoesNotExist_ThrowsBadRequest(
|
public async Task SaveAsync_OrganizationDoesNotExist_ThrowsBadRequest(
|
||||||
Policy policy, SutProvider<PolicyServicevNext> sutProvider)
|
Policy policy)
|
||||||
{
|
{
|
||||||
|
var sutProvider = SutProviderFactory();
|
||||||
sutProvider.GetDependency<IApplicationCacheService>()
|
sutProvider.GetDependency<IApplicationCacheService>()
|
||||||
.GetOrganizationAbilityAsync(policy.OrganizationId)
|
.GetOrganizationAbilityAsync(policy.OrganizationId)
|
||||||
.Returns((OrganizationAbility)null);
|
.Returns((OrganizationAbility)null);
|
||||||
@ -48,8 +46,9 @@ public class PolicyServicevNextTests
|
|||||||
|
|
||||||
[Theory, BitAutoData]
|
[Theory, BitAutoData]
|
||||||
public async Task SaveAsync_OrganizationCannotUsePolicies_ThrowsBadRequest(
|
public async Task SaveAsync_OrganizationCannotUsePolicies_ThrowsBadRequest(
|
||||||
Policy policy, SutProvider<PolicyServicevNext> sutProvider)
|
Policy policy)
|
||||||
{
|
{
|
||||||
|
var sutProvider = SutProviderFactory();
|
||||||
sutProvider.GetDependency<IApplicationCacheService>()
|
sutProvider.GetDependency<IApplicationCacheService>()
|
||||||
.GetOrganizationAbilityAsync(policy.OrganizationId)
|
.GetOrganizationAbilityAsync(policy.OrganizationId)
|
||||||
.Returns(new OrganizationAbility
|
.Returns(new OrganizationAbility
|
||||||
@ -76,12 +75,11 @@ public class PolicyServicevNextTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Theory, BitAutoData]
|
[Theory, BitAutoData]
|
||||||
public async Task SaveAsync_ThrowsOnValidationError(
|
public async Task SaveAsync_ThrowsOnValidationError([AdminConsoleFixtures.Policy(PolicyType.SingleOrg)] Policy policy)
|
||||||
[AdminConsoleFixtures.Policy(PolicyType.SingleOrg)] Policy policy, SutProvider<PolicyServicevNext> sutProvider)
|
|
||||||
{
|
{
|
||||||
var fakePolicyDefinition = new FakeSingleOrgPolicyDefinition();
|
var fakePolicyDefinition = new FakeSingleOrgPolicyDefinition();
|
||||||
fakePolicyDefinition.ValidateAsyncMock(null, policy).Returns("Validation error!");
|
fakePolicyDefinition.ValidateAsyncMock(null, policy).Returns("Validation error!");
|
||||||
sutProvider = RegisterPolicyDefinitions([fakePolicyDefinition]);
|
var sutProvider = SutProviderFactory([fakePolicyDefinition]);
|
||||||
|
|
||||||
sutProvider.GetDependency<IApplicationCacheService>()
|
sutProvider.GetDependency<IApplicationCacheService>()
|
||||||
.GetOrganizationAbilityAsync(policy.OrganizationId)
|
.GetOrganizationAbilityAsync(policy.OrganizationId)
|
||||||
@ -113,10 +111,10 @@ public class PolicyServicevNextTests
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns a new SutProvider with the PolicyDefinitions registered in the Sut.
|
/// Returns a new SutProvider with the PolicyDefinitions registered in the Sut.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private SutProvider<PolicyServicevNext> RegisterPolicyDefinitions(IEnumerable<IPolicyDefinition> policyDefinitions)
|
private static SutProvider<PolicyServicevNext> SutProviderFactory(IEnumerable<IPolicyDefinition> policyDefinitions = null)
|
||||||
{
|
{
|
||||||
var fixture = new Fixture();
|
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);
|
var sutProvider = new SutProvider<PolicyServicevNext>(fixture);
|
||||||
sutProvider.Create();
|
sutProvider.Create();
|
||||||
return sutProvider;
|
return sutProvider;
|
||||||
|
Loading…
Reference in New Issue
Block a user