-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PM-13014] - Add CanToggleStatus property to PolicyRepsonseModel base…
…d on Policy Validators (#4940) * Adding CanToggleState to PoliciesControllers (api/public) endpoints. Added mappings wrapped in feature flag. * Updated logic for determining CanToggle. Removed setting of toggle from List endpoint. Added new details model for single policy response. Validator now returns after first error.
- Loading branch information
1 parent
2e635c9
commit 1dec51b
Showing
14 changed files
with
167 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Api/AdminConsole/Models/Response/Helpers/PolicyDetailResponses.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Bit.Api.AdminConsole.Models.Response.Organizations; | ||
using Bit.Core.AdminConsole.Entities; | ||
using Bit.Core.AdminConsole.Enums; | ||
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationDomains.Interfaces; | ||
|
||
namespace Bit.Api.AdminConsole.Models.Response.Helpers; | ||
|
||
public static class PolicyDetailResponses | ||
{ | ||
public static async Task<PolicyDetailResponseModel> GetSingleOrgPolicyDetailResponseAsync(this Policy policy, IOrganizationHasVerifiedDomainsQuery hasVerifiedDomainsQuery) | ||
{ | ||
if (policy.Type is not PolicyType.SingleOrg) | ||
{ | ||
throw new ArgumentException($"'{nameof(policy)}' must be of type '{nameof(PolicyType.SingleOrg)}'.", nameof(policy)); | ||
} | ||
|
||
return new PolicyDetailResponseModel(policy, !await hasVerifiedDomainsQuery.HasVerifiedDomainsAsync(policy.OrganizationId)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Api/AdminConsole/Models/Response/Organizations/PolicyDetailResponseModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Bit.Core.AdminConsole.Entities; | ||
|
||
namespace Bit.Api.AdminConsole.Models.Response.Organizations; | ||
|
||
public class PolicyDetailResponseModel : PolicyResponseModel | ||
{ | ||
public PolicyDetailResponseModel(Policy policy, string obj = "policy") : base(policy, obj) | ||
{ | ||
} | ||
|
||
public PolicyDetailResponseModel(Policy policy, bool canToggleState) : base(policy) | ||
{ | ||
CanToggleState = canToggleState; | ||
} | ||
|
||
/// <summary> | ||
/// Indicates whether the Policy can be enabled/disabled | ||
/// </summary> | ||
public bool CanToggleState { get; set; } = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
test/Api.Test/AdminConsole/Models/Response/Helpers/PolicyDetailResponsesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using AutoFixture; | ||
using Bit.Api.AdminConsole.Models.Response.Helpers; | ||
using Bit.Core.AdminConsole.Entities; | ||
using Bit.Core.AdminConsole.Enums; | ||
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationDomains.Interfaces; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace Bit.Api.Test.AdminConsole.Models.Response.Helpers; | ||
|
||
public class PolicyDetailResponsesTests | ||
{ | ||
[Fact] | ||
public async Task GetSingleOrgPolicyDetailResponseAsync_GivenPolicyEntity_WhenIsSingleOrgTypeAndHasVerifiedDomains_ThenShouldNotBeAbleToToggle() | ||
{ | ||
var fixture = new Fixture(); | ||
|
||
var policy = fixture.Build<Policy>() | ||
.Without(p => p.Data) | ||
.With(p => p.Type, PolicyType.SingleOrg) | ||
.Create(); | ||
|
||
var querySub = Substitute.For<IOrganizationHasVerifiedDomainsQuery>(); | ||
querySub.HasVerifiedDomainsAsync(policy.OrganizationId) | ||
.Returns(true); | ||
|
||
var result = await policy.GetSingleOrgPolicyDetailResponseAsync(querySub); | ||
|
||
Assert.False(result.CanToggleState); | ||
} | ||
|
||
[Fact] | ||
public async Task GetSingleOrgPolicyDetailResponseAsync_GivenPolicyEntity_WhenIsNotSingleOrgType_ThenShouldThrowArgumentException() | ||
{ | ||
var fixture = new Fixture(); | ||
|
||
var policy = fixture.Build<Policy>() | ||
.Without(p => p.Data) | ||
.With(p => p.Type, PolicyType.TwoFactorAuthentication) | ||
.Create(); | ||
|
||
var querySub = Substitute.For<IOrganizationHasVerifiedDomainsQuery>(); | ||
querySub.HasVerifiedDomainsAsync(policy.OrganizationId) | ||
.Returns(true); | ||
|
||
var action = async () => await policy.GetSingleOrgPolicyDetailResponseAsync(querySub); | ||
|
||
await Assert.ThrowsAsync<ArgumentException>("policy", action); | ||
} | ||
|
||
[Fact] | ||
public async Task GetSingleOrgPolicyDetailResponseAsync_GivenPolicyEntity_WhenIsSingleOrgTypeAndDoesNotHaveVerifiedDomains_ThenShouldBeAbleToToggle() | ||
{ | ||
var fixture = new Fixture(); | ||
|
||
var policy = fixture.Build<Policy>() | ||
.Without(p => p.Data) | ||
.With(p => p.Type, PolicyType.SingleOrg) | ||
.Create(); | ||
|
||
var querySub = Substitute.For<IOrganizationHasVerifiedDomainsQuery>(); | ||
querySub.HasVerifiedDomainsAsync(policy.OrganizationId) | ||
.Returns(false); | ||
|
||
var result = await policy.GetSingleOrgPolicyDetailResponseAsync(querySub); | ||
|
||
Assert.True(result.CanToggleState); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters