mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
Work on PolicyServiceTests
This commit is contained in:
29
test/Core.Test/AutoFixture/PolicyFixtures.cs
Normal file
29
test/Core.Test/AutoFixture/PolicyFixtures.cs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using AutoFixture;
|
||||||
|
using Bit.Core.Test.AutoFixture.Attributes;
|
||||||
|
using Bit.Core.Test.AutoFixture.OrganizationFixtures;
|
||||||
|
|
||||||
|
namespace Bit.Core.Test.AutoFixture
|
||||||
|
{
|
||||||
|
internal class PolicyCustomization : ICustomization
|
||||||
|
{
|
||||||
|
public void Customize(IFixture fixture)
|
||||||
|
{
|
||||||
|
fixture.Customize<Core.Models.Table.Policy>(composer => composer
|
||||||
|
.With(p => p.Id, Guid.NewGuid())
|
||||||
|
.With(p => p.OrganizationId, Guid.NewGuid())
|
||||||
|
.With(p => p.Type, Enums.PolicyType.DisableSend)
|
||||||
|
.With(p => p.Data, "")
|
||||||
|
.With(p => p.Enabled, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class PolicyAutoDataAttribute : CustomAutoDataAttribute
|
||||||
|
{
|
||||||
|
public PolicyAutoDataAttribute() : base(
|
||||||
|
new SutProviderCustomization(), new PolicyCustomization(), new Organization())
|
||||||
|
{ }
|
||||||
|
}
|
||||||
|
}
|
149
test/Core.Test/Services/PolicyServiceTests.cs
Normal file
149
test/Core.Test/Services/PolicyServiceTests.cs
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Bit.Core.Exceptions;
|
||||||
|
using Bit.Core.Models.Table;
|
||||||
|
using Bit.Core.Repositories;
|
||||||
|
using Bit.Core.Services;
|
||||||
|
using Bit.Core.Test.AutoFixture;
|
||||||
|
using Bit.Core.Test.AutoFixture.Attributes;
|
||||||
|
using Braintree.Test;
|
||||||
|
using NSubstitute;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Bit.Core.Test.Services
|
||||||
|
{
|
||||||
|
public class PolicyServiceTests
|
||||||
|
{
|
||||||
|
[Theory, PolicyAutoData]
|
||||||
|
public async Task SaveAsync_OrganizationDoesNotExist_ThrowsBadRequest(Policy policy, SutProvider<PolicyService> sutProvider)
|
||||||
|
{
|
||||||
|
SetupOrg(sutProvider, policy.OrganizationId, null);
|
||||||
|
|
||||||
|
var badRequestException = await Assert.ThrowsAsync<BadRequestException>(
|
||||||
|
() => sutProvider.Sut.SaveAsync(policy,
|
||||||
|
Substitute.For<IUserService>(),
|
||||||
|
Substitute.For<IOrganizationService>(),
|
||||||
|
Guid.NewGuid()));
|
||||||
|
|
||||||
|
Assert.Contains("Organization not found", badRequestException.Message, StringComparison.OrdinalIgnoreCase);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory, PolicyAutoData]
|
||||||
|
public async Task SaveAsync_OrganizationCannotUsePolicies_ThrowsBadRequest(Policy policy, SutProvider<PolicyService> sutProvider)
|
||||||
|
{
|
||||||
|
var orgId = Guid.NewGuid();
|
||||||
|
|
||||||
|
SetupOrg(sutProvider, policy.OrganizationId, new Organization
|
||||||
|
{
|
||||||
|
UsePolicies = false,
|
||||||
|
});
|
||||||
|
|
||||||
|
var badRequestException = await Assert.ThrowsAsync<BadRequestException>(
|
||||||
|
() => sutProvider.Sut.SaveAsync(policy,
|
||||||
|
Substitute.For<IUserService>(),
|
||||||
|
Substitute.For<IOrganizationService>(),
|
||||||
|
Guid.NewGuid()));
|
||||||
|
|
||||||
|
Assert.Contains("cannot use policies", badRequestException.Message, StringComparison.OrdinalIgnoreCase);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory, PolicyAutoData]
|
||||||
|
public async Task SaveAsync_SingleOrg_RequireSsoEnabled_ThrowsBadRequest(Policy policy, SutProvider<PolicyService> sutProvider)
|
||||||
|
{
|
||||||
|
policy.Type = Enums.PolicyType.SingleOrg;
|
||||||
|
policy.Enabled = false;
|
||||||
|
|
||||||
|
SetupOrg(sutProvider, policy.OrganizationId, new Organization
|
||||||
|
{
|
||||||
|
Id = policy.OrganizationId,
|
||||||
|
UsePolicies = true,
|
||||||
|
});
|
||||||
|
|
||||||
|
sutProvider.GetDependency<IPolicyRepository>()
|
||||||
|
.GetByOrganizationIdTypeAsync(policy.OrganizationId, Enums.PolicyType.RequireSso)
|
||||||
|
.Returns(Task.FromResult(new Policy { Enabled = true }));
|
||||||
|
|
||||||
|
var badRequestException = await Assert.ThrowsAsync<BadRequestException>(
|
||||||
|
() => sutProvider.Sut.SaveAsync(policy,
|
||||||
|
Substitute.For<IUserService>(),
|
||||||
|
Substitute.For<IOrganizationService>(),
|
||||||
|
Guid.NewGuid()));
|
||||||
|
|
||||||
|
Assert.Contains("Single Sign-On Authentication policy is enabled.", badRequestException.Message, StringComparison.OrdinalIgnoreCase);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory, PolicyAutoData]
|
||||||
|
public async Task SaveAsync_RequireSsoPolicy_NotEnabled_ThrowsBadRequestAsync(Policy policy, SutProvider<PolicyService> sutProvider)
|
||||||
|
{
|
||||||
|
policy.Type = Enums.PolicyType.RequireSso;
|
||||||
|
policy.Enabled = true;
|
||||||
|
|
||||||
|
SetupOrg(sutProvider, policy.OrganizationId, new Organization
|
||||||
|
{
|
||||||
|
Id = policy.OrganizationId,
|
||||||
|
UsePolicies = true,
|
||||||
|
});
|
||||||
|
|
||||||
|
sutProvider.GetDependency<IPolicyRepository>()
|
||||||
|
.GetByOrganizationIdTypeAsync(policy.OrganizationId, Enums.PolicyType.SingleOrg)
|
||||||
|
.Returns(Task.FromResult(new Policy { Enabled = false }));
|
||||||
|
|
||||||
|
var badRequestException = await Assert.ThrowsAsync<BadRequestException>(
|
||||||
|
() => sutProvider.Sut.SaveAsync(policy,
|
||||||
|
Substitute.For<IUserService>(),
|
||||||
|
Substitute.For<IOrganizationService>(),
|
||||||
|
Guid.NewGuid()));
|
||||||
|
|
||||||
|
Assert.Contains("Single Organization policy not enabled.", badRequestException.Message, StringComparison.OrdinalIgnoreCase);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory, PolicyAutoData]
|
||||||
|
public async Task SaveAsync_NewPolicy_Created(Policy policy, SutProvider<PolicyService> sutProvider)
|
||||||
|
{
|
||||||
|
policy.Id = default;
|
||||||
|
policy.Type = Enums.PolicyType.MasterPassword;
|
||||||
|
|
||||||
|
SetupOrg(sutProvider, policy.OrganizationId, new Organization
|
||||||
|
{
|
||||||
|
Id = policy.OrganizationId,
|
||||||
|
UsePolicies = true,
|
||||||
|
});
|
||||||
|
|
||||||
|
var utcNow = DateTime.UtcNow;
|
||||||
|
|
||||||
|
await sutProvider.Sut.SaveAsync(policy, Substitute.For<IUserService>(), Substitute.For<IOrganizationService>(), Guid.NewGuid());
|
||||||
|
|
||||||
|
await sutProvider.GetDependency<IEventService>().Received()
|
||||||
|
.LogPolicyEventAsync(policy, Enums.EventType.Policy_Updated);
|
||||||
|
|
||||||
|
await sutProvider.GetDependency<IPolicyRepository>().Received()
|
||||||
|
.UpsertAsync(policy);
|
||||||
|
|
||||||
|
Assert.True(policy.CreationDate - utcNow < TimeSpan.FromSeconds(1));
|
||||||
|
Assert.True(policy.RevisionDate - utcNow < TimeSpan.FromSeconds(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory, PolicyAutoData]
|
||||||
|
public async Task SaveAsync_ExistingPolicy_Updated(Policy policy, SutProvider<PolicyService> sutProvider)
|
||||||
|
{
|
||||||
|
policy.Type = Enums.PolicyType.MasterPassword;
|
||||||
|
|
||||||
|
SetupOrg(sutProvider, policy.OrganizationId, new Organization
|
||||||
|
{
|
||||||
|
Id = policy.OrganizationId,
|
||||||
|
UsePolicies = true,
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void SetupOrg(SutProvider<PolicyService> sutProvider, Guid organizationId, Organization organization)
|
||||||
|
{
|
||||||
|
sutProvider.GetDependency<IOrganizationRepository>()
|
||||||
|
.GetByIdAsync(organizationId)
|
||||||
|
.Returns(Task.FromResult(organization));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user