2025-03-11 10:46:09 +10:00
|
|
|
|
using Bit.Core.AdminConsole.Enums;
|
|
|
|
|
|
using Bit.Core.AdminConsole.Models.Data.Organizations.Policies;
|
2025-02-14 21:05:49 +10:00
|
|
|
|
using Bit.Core.AdminConsole.OrganizationFeatures.Policies.Implementations;
|
|
|
|
|
|
using Bit.Core.AdminConsole.Repositories;
|
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Test.AdminConsole.OrganizationFeatures.Policies;
|
|
|
|
|
|
|
|
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
|
public class PolicyRequirementQueryTests
|
|
|
|
|
|
{
|
|
|
|
|
|
[Theory, BitAutoData]
|
2025-03-11 10:46:09 +10:00
|
|
|
|
public async Task GetAsync_IgnoresOtherPolicyTypes(Guid userId)
|
2025-02-14 21:05:49 +10:00
|
|
|
|
{
|
2025-03-11 10:46:09 +10:00
|
|
|
|
var thisPolicy = new PolicyDetails { PolicyType = PolicyType.SingleOrg };
|
|
|
|
|
|
var otherPolicy = new PolicyDetails { PolicyType = PolicyType.RequireSso };
|
2025-02-14 21:05:49 +10:00
|
|
|
|
var policyRepository = Substitute.For<IPolicyRepository>();
|
2025-03-11 10:46:09 +10:00
|
|
|
|
policyRepository.GetPolicyDetailsByUserId(userId).Returns([otherPolicy, thisPolicy]);
|
2025-02-14 21:05:49 +10:00
|
|
|
|
|
2025-03-11 10:46:09 +10:00
|
|
|
|
var factory = new TestPolicyRequirementFactory(_ => true);
|
|
|
|
|
|
var sut = new PolicyRequirementQuery(policyRepository, [factory]);
|
|
|
|
|
|
|
|
|
|
|
|
var requirement = await sut.GetAsync<TestPolicyRequirement>(userId);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Contains(thisPolicy, requirement.Policies);
|
|
|
|
|
|
Assert.DoesNotContain(otherPolicy, requirement.Policies);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
|
public async Task GetAsync_CallsEnforceCallback(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange policies
|
|
|
|
|
|
var policyRepository = Substitute.For<IPolicyRepository>();
|
|
|
|
|
|
var thisPolicy = new PolicyDetails { PolicyType = PolicyType.SingleOrg };
|
|
|
|
|
|
var otherPolicy = new PolicyDetails { PolicyType = PolicyType.SingleOrg };
|
|
|
|
|
|
policyRepository.GetPolicyDetailsByUserId(userId).Returns([thisPolicy, otherPolicy]);
|
|
|
|
|
|
|
|
|
|
|
|
// Arrange a substitute Enforce function so that we can inspect the received calls
|
|
|
|
|
|
var callback = Substitute.For<Func<PolicyDetails, bool>>();
|
|
|
|
|
|
callback(Arg.Any<PolicyDetails>()).Returns(x => x.Arg<PolicyDetails>() == thisPolicy);
|
|
|
|
|
|
|
|
|
|
|
|
// Arrange the sut
|
|
|
|
|
|
var factory = new TestPolicyRequirementFactory(callback);
|
|
|
|
|
|
var sut = new PolicyRequirementQuery(policyRepository, [factory]);
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
2025-02-14 21:05:49 +10:00
|
|
|
|
var requirement = await sut.GetAsync<TestPolicyRequirement>(userId);
|
2025-03-11 10:46:09 +10:00
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
Assert.Contains(thisPolicy, requirement.Policies);
|
|
|
|
|
|
Assert.DoesNotContain(otherPolicy, requirement.Policies);
|
|
|
|
|
|
callback.Received()(Arg.Is(thisPolicy));
|
|
|
|
|
|
callback.Received()(Arg.Is(otherPolicy));
|
2025-02-14 21:05:49 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2025-03-11 10:46:09 +10:00
|
|
|
|
public async Task GetAsync_ThrowsIfNoFactoryRegistered(Guid userId)
|
2025-02-14 21:05:49 +10:00
|
|
|
|
{
|
|
|
|
|
|
var policyRepository = Substitute.For<IPolicyRepository>();
|
|
|
|
|
|
var sut = new PolicyRequirementQuery(policyRepository, []);
|
|
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<NotImplementedException>(()
|
|
|
|
|
|
=> sut.GetAsync<TestPolicyRequirement>(userId));
|
2025-03-11 10:46:09 +10:00
|
|
|
|
Assert.Contains("No Requirement Factory found", exception.Message);
|
2025-02-14 21:05:49 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-11 10:46:09 +10:00
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
|
public async Task GetAsync_HandlesNoPolicies(Guid userId)
|
2025-02-14 21:05:49 +10:00
|
|
|
|
{
|
2025-03-11 10:46:09 +10:00
|
|
|
|
var policyRepository = Substitute.For<IPolicyRepository>();
|
|
|
|
|
|
policyRepository.GetPolicyDetailsByUserId(userId).Returns([]);
|
|
|
|
|
|
|
|
|
|
|
|
var factory = new TestPolicyRequirementFactory(x => x.IsProvider);
|
|
|
|
|
|
var sut = new PolicyRequirementQuery(policyRepository, [factory]);
|
|
|
|
|
|
|
|
|
|
|
|
var requirement = await sut.GetAsync<TestPolicyRequirement>(userId);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Empty(requirement.Policies);
|
2025-02-14 21:05:49 +10:00
|
|
|
|
}
|
2025-08-05 15:34:13 +01:00
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2025-08-11 16:36:40 +01:00
|
|
|
|
public async Task GetManyByOrganizationIdAsync_IgnoresOtherPolicyTypes(Guid organizationId)
|
2025-08-05 15:34:13 +01:00
|
|
|
|
{
|
|
|
|
|
|
var policyRepository = Substitute.For<IPolicyRepository>();
|
2025-08-11 16:36:40 +01:00
|
|
|
|
var thisPolicy = new OrganizationPolicyDetails { PolicyType = PolicyType.SingleOrg, OrganizationUserId = Guid.NewGuid() };
|
|
|
|
|
|
var otherPolicy = new OrganizationPolicyDetails { PolicyType = PolicyType.RequireSso, OrganizationUserId = Guid.NewGuid() };
|
2025-08-05 15:34:13 +01:00
|
|
|
|
// Force the repository to return both policies even though that is not the expected result
|
|
|
|
|
|
policyRepository.GetPolicyDetailsByOrganizationIdAsync(organizationId, PolicyType.SingleOrg)
|
|
|
|
|
|
.Returns([thisPolicy, otherPolicy]);
|
|
|
|
|
|
|
|
|
|
|
|
var factory = new TestPolicyRequirementFactory(_ => true);
|
|
|
|
|
|
var sut = new PolicyRequirementQuery(policyRepository, [factory]);
|
|
|
|
|
|
|
2025-08-11 16:36:40 +01:00
|
|
|
|
var organizationUserIds = await sut.GetManyByOrganizationIdAsync<TestPolicyRequirement>(organizationId);
|
2025-08-05 15:34:13 +01:00
|
|
|
|
|
|
|
|
|
|
await policyRepository.Received(1).GetPolicyDetailsByOrganizationIdAsync(organizationId, PolicyType.SingleOrg);
|
|
|
|
|
|
|
2025-08-11 16:36:40 +01:00
|
|
|
|
Assert.Contains(thisPolicy.OrganizationUserId, organizationUserIds);
|
|
|
|
|
|
Assert.DoesNotContain(otherPolicy.OrganizationUserId, organizationUserIds);
|
2025-08-05 15:34:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2025-08-11 16:36:40 +01:00
|
|
|
|
public async Task GetManyByOrganizationIdAsync_CallsEnforceCallback(Guid organizationId)
|
2025-08-05 15:34:13 +01:00
|
|
|
|
{
|
|
|
|
|
|
var policyRepository = Substitute.For<IPolicyRepository>();
|
2025-08-11 16:36:40 +01:00
|
|
|
|
var thisPolicy = new OrganizationPolicyDetails { PolicyType = PolicyType.SingleOrg, OrganizationUserId = Guid.NewGuid() };
|
|
|
|
|
|
var otherPolicy = new OrganizationPolicyDetails { PolicyType = PolicyType.SingleOrg, OrganizationUserId = Guid.NewGuid() };
|
2025-08-05 15:34:13 +01:00
|
|
|
|
policyRepository.GetPolicyDetailsByOrganizationIdAsync(organizationId, PolicyType.SingleOrg).Returns([thisPolicy, otherPolicy]);
|
|
|
|
|
|
|
|
|
|
|
|
var callback = Substitute.For<Func<PolicyDetails, bool>>();
|
|
|
|
|
|
callback(Arg.Any<PolicyDetails>()).Returns(x => x.Arg<PolicyDetails>() == thisPolicy);
|
|
|
|
|
|
|
|
|
|
|
|
var factory = new TestPolicyRequirementFactory(callback);
|
|
|
|
|
|
var sut = new PolicyRequirementQuery(policyRepository, [factory]);
|
|
|
|
|
|
|
2025-08-11 16:36:40 +01:00
|
|
|
|
var organizationUserIds = await sut.GetManyByOrganizationIdAsync<TestPolicyRequirement>(organizationId);
|
2025-08-05 15:34:13 +01:00
|
|
|
|
|
2025-08-11 16:36:40 +01:00
|
|
|
|
Assert.Contains(thisPolicy.OrganizationUserId, organizationUserIds);
|
|
|
|
|
|
Assert.DoesNotContain(otherPolicy.OrganizationUserId, organizationUserIds);
|
2025-08-05 15:34:13 +01:00
|
|
|
|
callback.Received()(Arg.Is<PolicyDetails>(p => p == thisPolicy));
|
|
|
|
|
|
callback.Received()(Arg.Is<PolicyDetails>(p => p == otherPolicy));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2025-08-11 16:36:40 +01:00
|
|
|
|
public async Task GetManyByOrganizationIdAsync_ThrowsIfNoFactoryRegistered(Guid organizationId)
|
2025-08-05 15:34:13 +01:00
|
|
|
|
{
|
|
|
|
|
|
var policyRepository = Substitute.For<IPolicyRepository>();
|
|
|
|
|
|
var sut = new PolicyRequirementQuery(policyRepository, []);
|
|
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<NotImplementedException>(()
|
2025-08-11 16:36:40 +01:00
|
|
|
|
=> sut.GetManyByOrganizationIdAsync<TestPolicyRequirement>(organizationId));
|
2025-08-05 15:34:13 +01:00
|
|
|
|
|
|
|
|
|
|
Assert.Contains("No Requirement Factory found", exception.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2025-08-11 16:36:40 +01:00
|
|
|
|
public async Task GetManyByOrganizationIdAsync_HandlesNoPolicies(Guid organizationId)
|
2025-08-05 15:34:13 +01:00
|
|
|
|
{
|
|
|
|
|
|
var policyRepository = Substitute.For<IPolicyRepository>();
|
|
|
|
|
|
policyRepository.GetPolicyDetailsByOrganizationIdAsync(organizationId, PolicyType.SingleOrg).Returns([]);
|
|
|
|
|
|
|
|
|
|
|
|
var factory = new TestPolicyRequirementFactory(x => x.IsProvider);
|
|
|
|
|
|
var sut = new PolicyRequirementQuery(policyRepository, [factory]);
|
|
|
|
|
|
|
2025-08-11 16:36:40 +01:00
|
|
|
|
var organizationUserIds = await sut.GetManyByOrganizationIdAsync<TestPolicyRequirement>(organizationId);
|
2025-08-05 15:34:13 +01:00
|
|
|
|
|
2025-08-11 16:36:40 +01:00
|
|
|
|
Assert.Empty(organizationUserIds);
|
2025-08-05 15:34:13 +01:00
|
|
|
|
}
|
2025-02-14 21:05:49 +10:00
|
|
|
|
}
|