From 13775d0902a875be2bf5aff3fb6d278d837359e7 Mon Sep 17 00:00:00 2001 From: Cy Okeke Date: Wed, 20 Nov 2024 17:59:01 +0100 Subject: [PATCH] Add some unit test for the policy status Signed-off-by: Cy Okeke --- .../Controllers/PoliciesControllerTests.cs | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/test/Api.Test/Controllers/PoliciesControllerTests.cs b/test/Api.Test/Controllers/PoliciesControllerTests.cs index 1b96ace5d..8236074ee 100644 --- a/test/Api.Test/Controllers/PoliciesControllerTests.cs +++ b/test/Api.Test/Controllers/PoliciesControllerTests.cs @@ -201,4 +201,85 @@ public class PoliciesControllerTests await Assert.ThrowsAsync(() => sutProvider.Sut.Get(orgId, type)); } + [Theory] + [BitAutoData] + public async Task GetPolicyStatusAsync_UserUnauthorized_ThrowsUnauthorizedAccessException( + SutProvider sutProvider, Guid orgId, PolicyType type) + { + // Arrange + sutProvider.GetDependency() + .GetUserByPrincipalAsync(Arg.Any()) + .Returns(Task.FromResult(null)); + + // Act & Assert + await Assert.ThrowsAsync(() => sutProvider.Sut.GetPolicyStatusAsync(orgId, type)); + } + + [Theory] + [BitAutoData] + public async Task GetPolicyStatusAsync_PolicyNotFound_ReturnsFalse( + SutProvider sutProvider, Guid orgId, PolicyType type, User user) + { + // Arrange + sutProvider.GetDependency() + .GetUserByPrincipalAsync(Arg.Any()) + .Returns(user); + + sutProvider.GetDependency() + .GetByOrganizationIdTypeAsync(orgId, type) + .Returns(Task.FromResult(null)); + + // Act + var result = await sutProvider.Sut.GetPolicyStatusAsync(orgId, type); + + // Assert + Assert.False(result); + } + + [Theory] + [BitAutoData] + public async Task GetPolicyStatusAsync_PolicyDisabled_ReturnsFalse( + SutProvider sutProvider, Guid orgId, PolicyType type, User user, Policy policy) + { + // Arrange + policy.Enabled = false; + + sutProvider.GetDependency() + .GetUserByPrincipalAsync(Arg.Any()) + .Returns(user); + + sutProvider.GetDependency() + .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 sutProvider, Guid orgId, PolicyType type, User user, Policy policy) + { + // Arrange + policy.Enabled = true; + + sutProvider.GetDependency() + .GetUserByPrincipalAsync(Arg.Any()) + .Returns(user); + + sutProvider.GetDependency() + .GetByOrganizationIdTypeAsync(orgId, type) + .Returns(policy); + + // Act + var result = await sutProvider.Sut.GetPolicyStatusAsync(orgId, type); + + // Assert + Assert.True(result); + } + }