Skip to content

Commit

Permalink
Add unit tests, change orgId type to Guid
Browse files Browse the repository at this point in the history
  • Loading branch information
BTreston committed Oct 18, 2024
1 parent 19d398f commit 30dbd52
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/Api/AdminConsole/Controllers/PoliciesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,17 @@ public PoliciesController(
}

[HttpGet("{type}")]
public async Task<PolicyResponseModel> Get(string orgId, int type)
public async Task<PolicyResponseModel> Get(Guid orgId, int type)
{
var orgIdGuid = new Guid(orgId);
var policy = new AdminConsoleEntities.Policy();
policy.Type = (PolicyType)type;
policy.Enabled = false;

if (!await _currentContext.ManagePolicies(orgIdGuid))
if (!await _currentContext.ManagePolicies(orgId))
{
return new PolicyResponseModel(policy);

Check warning on line 70 in src/Api/AdminConsole/Controllers/PoliciesController.cs

View check run for this annotation

Codecov / codecov/patch

src/Api/AdminConsole/Controllers/PoliciesController.cs#L70

Added line #L70 was not covered by tests
}
policy = await _policyRepository.GetByOrganizationIdTypeAsync(orgIdGuid, (PolicyType)type);
policy = await _policyRepository.GetByOrganizationIdTypeAsync(orgId, (PolicyType)type);
if (policy == null)
{
return new PolicyResponseModel(policy);

Check warning on line 75 in src/Api/AdminConsole/Controllers/PoliciesController.cs

View check run for this annotation

Codecov / codecov/patch

src/Api/AdminConsole/Controllers/PoliciesController.cs#L75

Added line #L75 was not covered by tests
Expand Down
57 changes: 57 additions & 0 deletions test/Api.Test/Controllers/PoliciesControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using Bit.Api.AdminConsole.Controllers;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Enums;
using Bit.Core.AdminConsole.Models.Api.Response;
using Bit.Core.AdminConsole.Models.Data.Organizations.Policies;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Context;
using Bit.Core.Entities;
using Bit.Core.Exceptions;
using Bit.Core.Repositories;
Expand Down Expand Up @@ -132,4 +134,59 @@ public async Task GetMasterPasswordPolicy_PolicyNotEnabled_ThrowsNotFoundExcepti
// Act & Assert
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.GetMasterPasswordPolicy(orgId));
}

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

policy.Type = (PolicyType)type;
policy.Enabled = true;
policy.Data = null;

sutProvider.GetDependency<IPolicyRepository>()
.GetByOrganizationIdTypeAsync(orgId, (PolicyType)type)
.Returns(policy);

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

// 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);
}

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

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

// 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.False(result.Enabled);
}

}

0 comments on commit 30dbd52

Please sign in to comment.