Skip to content

Commit

Permalink
Fix test cases, throw exception when manage not allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
BTreston committed Oct 21, 2024
1 parent 30dbd52 commit 9005f25
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
10 changes: 3 additions & 7 deletions src/Api/AdminConsole/Controllers/PoliciesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,14 @@ public PoliciesController(
[HttpGet("{type}")]
public async Task<PolicyResponseModel> Get(Guid orgId, int type)
{
var policy = new AdminConsoleEntities.Policy();
policy.Type = (PolicyType)type;
policy.Enabled = false;

if (!await _currentContext.ManagePolicies(orgId))
{
return new PolicyResponseModel(policy);
throw new NotFoundException();
}
policy = await _policyRepository.GetByOrganizationIdTypeAsync(orgId, (PolicyType)type);
var policy = await _policyRepository.GetByOrganizationIdTypeAsync(orgId, (PolicyType)type);
if (policy == null)
{
return new PolicyResponseModel(policy);
return new PolicyResponseModel(new AdminConsoleEntities.Policy() { Type = (PolicyType)type, Enabled = false });
}

return new PolicyResponseModel(policy);
Expand Down
28 changes: 20 additions & 8 deletions test/Api.Test/Controllers/PoliciesControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public async Task GetMasterPasswordPolicy_PolicyNotEnabled_ThrowsNotFoundExcepti

[Theory]
[BitAutoData]
public async Task Get_WhenCalled_ReturnsPolicyResponseModel(
public async Task Get_WhenUserCanManagePolicies_WithExistingType_ReturnsExistingPolicy(
SutProvider<PoliciesController> sutProvider, Guid orgId, Policy policy, int type)
{
// Arrange
Expand All @@ -158,16 +158,16 @@ public async Task Get_WhenCalled_ReturnsPolicyResponseModel(

// Assert
Assert.IsType<PolicyResponseModel>(result);
Assert.NotNull(result);
Assert.Equal(policy.Id, result.Id);
Assert.Equal(policy.Type, result.Type);
Assert.Equal(policy.Enabled, result.Enabled);
Assert.Equal(policy.OrganizationId, result.OrganizationId);
}

[Theory]
[BitAutoData]
public async Task Get_PolicyNotFound_ReturnsPolicyResponseModel(
SutProvider<PoliciesController> sutProvider, Guid orgId, Policy policy, int type)
public async Task Get_WhenUserCanManagePolicies_WithNonExistingType_ReturnsDefaultPolicy(
SutProvider<PoliciesController> sutProvider, Guid orgId, int type)
{
// Arrange
sutProvider.GetDependency<ICurrentContext>()
Expand All @@ -176,17 +176,29 @@ public async Task Get_PolicyNotFound_ReturnsPolicyResponseModel(

sutProvider.GetDependency<IPolicyRepository>()
.GetByOrganizationIdTypeAsync(orgId, (PolicyType)type)
.Returns(new Policy { });
.Returns((Policy)null);

// Act
var result = await sutProvider.Sut.Get(orgId, type);

// Assert
Assert.NotNull(result);
Assert.IsType<PolicyResponseModel>(result);
Assert.NotEqual(policy.Id, result.Id);
Assert.Equal(policy.Type, result.Type);
Assert.Equal(result.Type, (PolicyType)type);
Assert.False(result.Enabled);
}

[Theory]
[BitAutoData]
public async Task Get_WhenUserCannotManagePolicies_ThrowsNotFoundException(
SutProvider<PoliciesController> sutProvider, Guid orgId, int type)
{
// Arrange
sutProvider.GetDependency<ICurrentContext>()
.ManagePolicies(orgId)
.Returns(false);

// Act & Assert
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.Get(orgId, type));
}

}

0 comments on commit 9005f25

Please sign in to comment.