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

Add happy path test

This commit is contained in:
Thomas Rittson 2024-10-07 10:12:59 +10:00
parent 6c1f369a19
commit 2521a2bcf2
No known key found for this signature in database
GPG Key ID: CDDDA03861C35E27

View File

@ -12,14 +12,39 @@ using Bit.Core.Services;
using Bit.Core.Test.AdminConsole.AutoFixture;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
using AdminConsoleFixtures = Bit.Core.Test.AdminConsole.AutoFixture;
using NSubstitute;
using NSubstitute.ReceivedExtensions;
using Xunit;
using EventType = Bit.Core.Enums.EventType;
namespace Bit.Core.Test.AdminConsole.Services;
public class PolicyServicevNextTests
{
[Theory, BitAutoData]
public async Task SaveAsync_Success([Policy(PolicyType.SingleOrg)] Policy policy)
{
var fakePolicyDefinition = new FakeSingleOrgPolicyDefinition();
fakePolicyDefinition.ValidateAsyncMock(null, policy).Returns((string)null);
var sutProvider = SutProviderFactory([fakePolicyDefinition]);
var originalRevisionDate = policy.RevisionDate;
ArrangeOrganization(sutProvider, policy);
sutProvider.GetDependency<IPolicyRepository>().GetManyByOrganizationIdAsync(policy.OrganizationId).Returns([]);
await sutProvider.Sut.SaveAsync(policy,
Substitute.For<IUserService>(),
Substitute.For<IOrganizationService>(),
Guid.NewGuid());
fakePolicyDefinition.OnSaveSideEffectsAsyncMock.Received(1).Invoke(null, policy);
Assert.NotEqual(originalRevisionDate, policy.RevisionDate);
await sutProvider.GetDependency<IPolicyRepository>().Received(1).UpsertAsync(policy);
await sutProvider.GetDependency<IEventService>().Received(1)
.LogPolicyEventAsync(policy, EventType.Policy_Updated);
}
[Fact]
public void Constructor_DuplicatePolicyDefinitions_Throws()
{
@ -89,26 +114,6 @@ public class PolicyServicevNextTests
await AssertPolicyNotSavedAsync(sutProvider);
}
[Theory, BitAutoData]
public async Task SaveAsync_ThrowsOnValidationError([AdminConsoleFixtures.Policy(PolicyType.SingleOrg)] Policy policy)
{
var fakePolicyDefinition = new FakeSingleOrgPolicyDefinition();
fakePolicyDefinition.ValidateAsyncMock(null, policy).Returns("Validation error!");
var sutProvider = SutProviderFactory([fakePolicyDefinition]);
ArrangeOrganization(sutProvider, policy);
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 AssertPolicyNotSavedAsync(sutProvider);
}
[Theory, BitAutoData]
public async Task SaveAsync_RequiredPolicyIsNull_Throws(
[Policy(PolicyType.RequireSso)] Policy policy)
@ -233,6 +238,26 @@ public class PolicyServicevNextTests
await sutProvider.GetDependency<IPolicyRepository>().Received(1).UpsertAsync(policy);
}
[Theory, BitAutoData]
public async Task SaveAsync_ThrowsOnValidationError([Policy(PolicyType.SingleOrg)] Policy policy)
{
var fakePolicyDefinition = new FakeSingleOrgPolicyDefinition();
fakePolicyDefinition.ValidateAsyncMock(null, policy).Returns("Validation error!");
var sutProvider = SutProviderFactory([fakePolicyDefinition]);
ArrangeOrganization(sutProvider, policy);
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 AssertPolicyNotSavedAsync(sutProvider);
}
/// <summary>
/// Returns a new SutProvider with the PolicyDefinitions registered in the Sut.
/// </summary>