diff --git a/src/Api/AdminConsole/Controllers/PoliciesController.cs b/src/Api/AdminConsole/Controllers/PoliciesController.cs index a8104c466ca2..d19f4e92b53d 100644 --- a/src/Api/AdminConsole/Controllers/PoliciesController.cs +++ b/src/Api/AdminConsole/Controllers/PoliciesController.cs @@ -61,18 +61,14 @@ public PoliciesController( [HttpGet("{type}")] public async Task 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); diff --git a/test/Api.Test/Controllers/PoliciesControllerTests.cs b/test/Api.Test/Controllers/PoliciesControllerTests.cs index 5a8163a05295..77cc5ea02c99 100644 --- a/test/Api.Test/Controllers/PoliciesControllerTests.cs +++ b/test/Api.Test/Controllers/PoliciesControllerTests.cs @@ -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 sutProvider, Guid orgId, Policy policy, int type) { // Arrange @@ -158,16 +158,16 @@ public async Task Get_WhenCalled_ReturnsPolicyResponseModel( // Assert Assert.IsType(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 sutProvider, Guid orgId, Policy policy, int type) + public async Task Get_WhenUserCanManagePolicies_WithNonExistingType_ReturnsDefaultPolicy( + SutProvider sutProvider, Guid orgId, int type) { // Arrange sutProvider.GetDependency() @@ -176,17 +176,29 @@ public async Task Get_PolicyNotFound_ReturnsPolicyResponseModel( sutProvider.GetDependency() .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(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 sutProvider, Guid orgId, int type) + { + // Arrange + sutProvider.GetDependency() + .ManagePolicies(orgId) + .Returns(false); + + // Act & Assert + await Assert.ThrowsAsync(() => sutProvider.Sut.Get(orgId, type)); + } + }