mirror of
https://github.com/bitwarden/server.git
synced 2025-01-06 19:28:08 +01:00
Add test for validation, set up fake policy definitions
This commit is contained in:
parent
9d75bcbc86
commit
aa4c126d69
@ -13,6 +13,13 @@ namespace Bit.Core.Test.AdminConsole.AutoFixture;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class PolicyServicevNextBuilder : ISpecimenBuilder
|
public class PolicyServicevNextBuilder : ISpecimenBuilder
|
||||||
{
|
{
|
||||||
|
private readonly IEnumerable<IPolicyDefinition> _policyDefinitions;
|
||||||
|
|
||||||
|
public PolicyServicevNextBuilder(IEnumerable<IPolicyDefinition> policyDefinitions)
|
||||||
|
{
|
||||||
|
_policyDefinitions = policyDefinitions;
|
||||||
|
}
|
||||||
|
|
||||||
public object Create(object request, ISpecimenContext context)
|
public object Create(object request, ISpecimenContext context)
|
||||||
{
|
{
|
||||||
var pi = request as ParameterInfo;
|
var pi = request as ParameterInfo;
|
||||||
@ -21,17 +28,25 @@ public class PolicyServicevNextBuilder : ISpecimenBuilder
|
|||||||
|
|
||||||
if (pi.Member.DeclaringType != typeof(PolicyServicevNext) ||
|
if (pi.Member.DeclaringType != typeof(PolicyServicevNext) ||
|
||||||
pi.ParameterType != typeof(IEnumerable<IPolicyDefinition>))
|
pi.ParameterType != typeof(IEnumerable<IPolicyDefinition>))
|
||||||
|
{
|
||||||
return new NoSpecimen();
|
return new NoSpecimen();
|
||||||
|
}
|
||||||
|
|
||||||
return Array.Empty<IPolicyDefinition>();
|
return _policyDefinitions;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PolicyServicevNextCustomization : ICustomization
|
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)
|
public void Customize(IFixture fixture)
|
||||||
{
|
{
|
||||||
fixture.Customizations.Add(new PolicyServicevNextBuilder());
|
fixture.Customizations.Add(new PolicyServicevNextBuilder(_policyDefinitions));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
using Bit.Core.AdminConsole.Entities;
|
||||||
|
using Bit.Core.AdminConsole.Enums;
|
||||||
|
using Bit.Core.AdminConsole.OrganizationFeatures.Policies;
|
||||||
|
using NSubstitute;
|
||||||
|
using NSubstitute.ExceptionExtensions;
|
||||||
|
|
||||||
|
namespace Bit.Core.Test.AdminConsole.Services;
|
||||||
|
|
||||||
|
public class FakeSingleOrgPolicyDefinition : IPolicyDefinition
|
||||||
|
{
|
||||||
|
public PolicyType Type => PolicyType.SingleOrg;
|
||||||
|
public IEnumerable<PolicyType> RequiredPolicies => Array.Empty<PolicyType>();
|
||||||
|
|
||||||
|
public readonly Func<Policy?, Policy, Task<string?>> ValidateAsyncMock = Substitute.For<Func<Policy, Policy, Task<string>>>();
|
||||||
|
public readonly Action<Policy?, Policy> OnSaveSideEffectsAsyncMock = Substitute.For<Action<Policy, Policy>>();
|
||||||
|
|
||||||
|
public Task<string>? ValidateAsync(Policy? currentPolicy, Policy modifiedPolicy)
|
||||||
|
{
|
||||||
|
return ValidateAsyncMock(currentPolicy, modifiedPolicy);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task OnSaveSideEffectsAsync(Policy? currentPolicy, Policy modifiedPolicy)
|
||||||
|
{
|
||||||
|
OnSaveSideEffectsAsyncMock(currentPolicy, modifiedPolicy);
|
||||||
|
return Task.FromResult(0);
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,7 @@
|
|||||||
using Bit.Core.AdminConsole.Entities;
|
using AutoFixture;
|
||||||
|
using Bit.Core.AdminConsole.Entities;
|
||||||
|
using Bit.Core.AdminConsole.Enums;
|
||||||
|
using Bit.Core.AdminConsole.OrganizationFeatures.Policies;
|
||||||
using Bit.Core.AdminConsole.Repositories;
|
using Bit.Core.AdminConsole.Repositories;
|
||||||
using Bit.Core.AdminConsole.Services.Implementations;
|
using Bit.Core.AdminConsole.Services.Implementations;
|
||||||
using Bit.Core.Exceptions;
|
using Bit.Core.Exceptions;
|
||||||
@ -7,7 +10,9 @@ using Bit.Core.Services;
|
|||||||
using Bit.Core.Test.AdminConsole.AutoFixture;
|
using Bit.Core.Test.AdminConsole.AutoFixture;
|
||||||
using Bit.Test.Common.AutoFixture;
|
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 NSubstitute;
|
using NSubstitute;
|
||||||
|
using NSubstitute.ExceptionExtensions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Bit.Core.Test.AdminConsole.Services;
|
namespace Bit.Core.Test.AdminConsole.Services;
|
||||||
@ -69,4 +74,51 @@ public class PolicyServicevNextTests
|
|||||||
.DidNotReceiveWithAnyArgs()
|
.DidNotReceiveWithAnyArgs()
|
||||||
.LogPolicyEventAsync(default, default, default);
|
.LogPolicyEventAsync(default, default, default);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory, BitAutoData]
|
||||||
|
public async Task SaveAsync_ThrowsOnValidationError(
|
||||||
|
[AdminConsoleFixtures.Policy(PolicyType.SingleOrg)] Policy policy, SutProvider<PolicyServicevNext> sutProvider)
|
||||||
|
{
|
||||||
|
var fakePolicyDefinition = new FakeSingleOrgPolicyDefinition();
|
||||||
|
fakePolicyDefinition.ValidateAsyncMock(null, policy).Returns("Validation error!");
|
||||||
|
sutProvider = RegisterPolicyDefinitions([fakePolicyDefinition]);
|
||||||
|
|
||||||
|
sutProvider.GetDependency<IApplicationCacheService>()
|
||||||
|
.GetOrganizationAbilityAsync(policy.OrganizationId)
|
||||||
|
.Returns(new OrganizationAbility
|
||||||
|
{
|
||||||
|
Id = policy.OrganizationId,
|
||||||
|
UsePolicies = true
|
||||||
|
});
|
||||||
|
|
||||||
|
sutProvider.GetDependency<IPolicyRepository>().GetManyByOrganizationIdAsync(policy.OrganizationId).Returns([]);
|
||||||
|
|
||||||
|
var badRequestException = await Assert.ThrowsAsync<BadRequestException>(
|
||||||
|
() => sutProvider.Sut.SaveAsync(policy,
|
||||||
|
Substitute.For<IUserService>(),
|
||||||
|
Substitute.For<IOrganizationService>(),
|
||||||
|
Guid.NewGuid()));
|
||||||
|
|
||||||
|
Assert.Contains("Validation error!", badRequestException.Message, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
await sutProvider.GetDependency<IPolicyRepository>()
|
||||||
|
.DidNotReceiveWithAnyArgs()
|
||||||
|
.UpsertAsync(default);
|
||||||
|
|
||||||
|
await sutProvider.GetDependency<IEventService>()
|
||||||
|
.DidNotReceiveWithAnyArgs()
|
||||||
|
.LogPolicyEventAsync(default, default, default);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a new SutProvider with the PolicyDefinitions registered in the Sut.
|
||||||
|
/// </summary>
|
||||||
|
private SutProvider<PolicyServicevNext> RegisterPolicyDefinitions(IEnumerable<IPolicyDefinition> policyDefinitions)
|
||||||
|
{
|
||||||
|
var fixture = new Fixture();
|
||||||
|
fixture.Customizations.Add(new PolicyServicevNextBuilder(policyDefinitions));
|
||||||
|
var sutProvider = new SutProvider<PolicyServicevNext>(fixture);
|
||||||
|
sutProvider.Create();
|
||||||
|
return sutProvider;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user