1
0
mirror of https://github.com/bitwarden/server.git synced 2025-01-06 19:28:08 +01:00

Add test helper methods

This commit is contained in:
Thomas Rittson 2024-10-07 08:44:53 +10:00
parent 9844ba8512
commit b3006ef64e
No known key found for this signature in database
GPG Key ID: CDDDA03861C35E27

View File

@ -46,14 +46,7 @@ public class PolicyServicevNextTests
Guid.NewGuid())); Guid.NewGuid()));
Assert.Contains("Organization not found", badRequestException.Message, StringComparison.OrdinalIgnoreCase); Assert.Contains("Organization not found", badRequestException.Message, StringComparison.OrdinalIgnoreCase);
await AssertPolicyNotSavedAsync(sutProvider);
await sutProvider.GetDependency<IPolicyRepository>()
.DidNotReceiveWithAnyArgs()
.UpsertAsync(default);
await sutProvider.GetDependency<IEventService>()
.DidNotReceiveWithAnyArgs()
.LogPolicyEventAsync(default, default, default);
} }
[Theory, BitAutoData] [Theory, BitAutoData]
@ -75,27 +68,14 @@ public class PolicyServicevNextTests
Guid.NewGuid())); Guid.NewGuid()));
Assert.Contains("cannot use policies", badRequestException.Message, StringComparison.OrdinalIgnoreCase); Assert.Contains("cannot use policies", badRequestException.Message, StringComparison.OrdinalIgnoreCase);
await AssertPolicyNotSavedAsync(sutProvider);
await sutProvider.GetDependency<IPolicyRepository>()
.DidNotReceiveWithAnyArgs()
.UpsertAsync(default);
await sutProvider.GetDependency<IEventService>()
.DidNotReceiveWithAnyArgs()
.LogPolicyEventAsync(default, default, default);
} }
[Theory, BitAutoData] [Theory, BitAutoData]
public async Task SaveAsync_PolicyDefinitionNotFound_Throws([Policy(PolicyType.SingleOrg)]Policy policy) public async Task SaveAsync_PolicyDefinitionNotFound_Throws([Policy(PolicyType.SingleOrg)]Policy policy)
{ {
var sutProvider = SutProviderFactory(); var sutProvider = SutProviderFactory();
sutProvider.GetDependency<IApplicationCacheService>() ArrangeOrganization(sutProvider, policy);
.GetOrganizationAbilityAsync(policy.OrganizationId)
.Returns(new OrganizationAbility
{
Id = policy.OrganizationId,
UsePolicies = true
});
var exception = await Assert.ThrowsAsync<Exception>( var exception = await Assert.ThrowsAsync<Exception>(
() => sutProvider.Sut.SaveAsync(policy, () => sutProvider.Sut.SaveAsync(policy,
@ -104,14 +84,7 @@ public class PolicyServicevNextTests
Guid.NewGuid())); Guid.NewGuid()));
Assert.Contains("No PolicyDefinition found for SingleOrg policy", exception.Message, StringComparison.OrdinalIgnoreCase); Assert.Contains("No PolicyDefinition found for SingleOrg policy", exception.Message, StringComparison.OrdinalIgnoreCase);
await AssertPolicyNotSavedAsync(sutProvider);
await sutProvider.GetDependency<IPolicyRepository>()
.DidNotReceiveWithAnyArgs()
.UpsertAsync(default);
await sutProvider.GetDependency<IEventService>()
.DidNotReceiveWithAnyArgs()
.LogPolicyEventAsync(default, default, default);
} }
[Theory, BitAutoData] [Theory, BitAutoData]
@ -121,14 +94,7 @@ public class PolicyServicevNextTests
fakePolicyDefinition.ValidateAsyncMock(null, policy).Returns("Validation error!"); fakePolicyDefinition.ValidateAsyncMock(null, policy).Returns("Validation error!");
var sutProvider = SutProviderFactory([fakePolicyDefinition]); var sutProvider = SutProviderFactory([fakePolicyDefinition]);
sutProvider.GetDependency<IApplicationCacheService>() ArrangeOrganization(sutProvider, policy);
.GetOrganizationAbilityAsync(policy.OrganizationId)
.Returns(new OrganizationAbility
{
Id = policy.OrganizationId,
UsePolicies = true
});
sutProvider.GetDependency<IPolicyRepository>().GetManyByOrganizationIdAsync(policy.OrganizationId).Returns([]); sutProvider.GetDependency<IPolicyRepository>().GetManyByOrganizationIdAsync(policy.OrganizationId).Returns([]);
var badRequestException = await Assert.ThrowsAsync<BadRequestException>( var badRequestException = await Assert.ThrowsAsync<BadRequestException>(
@ -138,14 +104,7 @@ public class PolicyServicevNextTests
Guid.NewGuid())); Guid.NewGuid()));
Assert.Contains("Validation error!", badRequestException.Message, StringComparison.OrdinalIgnoreCase); Assert.Contains("Validation error!", badRequestException.Message, StringComparison.OrdinalIgnoreCase);
await AssertPolicyNotSavedAsync(sutProvider);
await sutProvider.GetDependency<IPolicyRepository>()
.DidNotReceiveWithAnyArgs()
.UpsertAsync(default);
await sutProvider.GetDependency<IEventService>()
.DidNotReceiveWithAnyArgs()
.LogPolicyEventAsync(default, default, default);
} }
/// <summary> /// <summary>
@ -159,4 +118,26 @@ public class PolicyServicevNextTests
sutProvider.Create(); sutProvider.Create();
return sutProvider; return sutProvider;
} }
private static void ArrangeOrganization(SutProvider<PolicyServicevNext> sutProvider, Policy policy)
{
sutProvider.GetDependency<IApplicationCacheService>()
.GetOrganizationAbilityAsync(policy.OrganizationId)
.Returns(new OrganizationAbility
{
Id = policy.OrganizationId,
UsePolicies = true
});
}
private static async Task AssertPolicyNotSavedAsync(SutProvider<PolicyServicevNext> sutProvider)
{
await sutProvider.GetDependency<IPolicyRepository>()
.DidNotReceiveWithAnyArgs()
.UpsertAsync(default);
await sutProvider.GetDependency<IEventService>()
.DidNotReceiveWithAnyArgs()
.LogPolicyEventAsync(default, default, default);
}
} }