1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-21 12:05:42 +01:00

Add some unit test for the policy status

Signed-off-by: Cy Okeke <cokeke@bitwarden.com>
This commit is contained in:
Cy Okeke 2024-11-20 17:59:01 +01:00
parent 238662d9da
commit 13775d0902
No known key found for this signature in database
GPG Key ID: 88B341B55C84B45C

View File

@ -201,4 +201,85 @@ public class PoliciesControllerTests
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.Get(orgId, type));
}
[Theory]
[BitAutoData]
public async Task GetPolicyStatusAsync_UserUnauthorized_ThrowsUnauthorizedAccessException(
SutProvider<PoliciesController> sutProvider, Guid orgId, PolicyType type)
{
// Arrange
sutProvider.GetDependency<IUserService>()
.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>())
.Returns(Task.FromResult<User>(null));
// Act & Assert
await Assert.ThrowsAsync<UnauthorizedAccessException>(() => sutProvider.Sut.GetPolicyStatusAsync(orgId, type));
}
[Theory]
[BitAutoData]
public async Task GetPolicyStatusAsync_PolicyNotFound_ReturnsFalse(
SutProvider<PoliciesController> sutProvider, Guid orgId, PolicyType type, User user)
{
// Arrange
sutProvider.GetDependency<IUserService>()
.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>())
.Returns(user);
sutProvider.GetDependency<IPolicyRepository>()
.GetByOrganizationIdTypeAsync(orgId, type)
.Returns(Task.FromResult<Policy>(null));
// Act
var result = await sutProvider.Sut.GetPolicyStatusAsync(orgId, type);
// Assert
Assert.False(result);
}
[Theory]
[BitAutoData]
public async Task GetPolicyStatusAsync_PolicyDisabled_ReturnsFalse(
SutProvider<PoliciesController> sutProvider, Guid orgId, PolicyType type, User user, Policy policy)
{
// Arrange
policy.Enabled = false;
sutProvider.GetDependency<IUserService>()
.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>())
.Returns(user);
sutProvider.GetDependency<IPolicyRepository>()
.GetByOrganizationIdTypeAsync(orgId, type)
.Returns(policy);
// Act
var result = await sutProvider.Sut.GetPolicyStatusAsync(orgId, type);
// Assert
Assert.False(result);
}
[Theory]
[BitAutoData]
public async Task GetPolicyStatusAsync_PolicyEnabled_ReturnsTrue(
SutProvider<PoliciesController> sutProvider, Guid orgId, PolicyType type, User user, Policy policy)
{
// Arrange
policy.Enabled = true;
sutProvider.GetDependency<IUserService>()
.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>())
.Returns(user);
sutProvider.GetDependency<IPolicyRepository>()
.GetByOrganizationIdTypeAsync(orgId, type)
.Returns(policy);
// Act
var result = await sutProvider.Sut.GetPolicyStatusAsync(orgId, type);
// Assert
Assert.True(result);
}
}