1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00
bitwarden-server/test/Core.Test/Services/LicensingServiceTests.cs
2022-08-29 16:06:55 -04:00

61 lines
1.9 KiB
C#

using System.Text.Json;
using AutoFixture;
using Bit.Core.Entities;
using Bit.Core.Models.Business;
using Bit.Core.Services;
using Bit.Core.Settings;
using Bit.Core.Test.AutoFixture;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
using Xunit;
namespace Bit.Core.Test.Services;
[SutProviderCustomize]
public class LicensingServiceTests
{
private static string licenseFilePath(Guid orgId) =>
Path.Combine(OrganizationLicenseDirectory.Value, $"{orgId}.json");
private static string LicenseDirectory => Path.GetDirectoryName(OrganizationLicenseDirectory.Value);
private static Lazy<string> OrganizationLicenseDirectory => new(() =>
{
var directory = Path.Combine(Path.GetTempPath(), "organization");
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
return directory;
});
public static SutProvider<LicensingService> GetSutProvider()
{
var fixture = new Fixture().WithAutoNSubstitutions();
var settings = fixture.Create<IGlobalSettings>();
settings.LicenseDirectory = LicenseDirectory;
settings.SelfHosted = true;
return new SutProvider<LicensingService>(fixture)
.SetDependency(settings)
.Create();
}
[Theory, BitAutoData, OrganizationLicenseCustomize]
public async Task ReadOrganizationLicense(Organization organization, OrganizationLicense license)
{
var sutProvider = GetSutProvider();
File.WriteAllText(licenseFilePath(organization.Id), JsonSerializer.Serialize(license));
var actual = await sutProvider.Sut.ReadOrganizationLicenseAsync(organization);
try
{
Assert.Equal(JsonSerializer.Serialize(license), JsonSerializer.Serialize(actual));
}
finally
{
Directory.Delete(OrganizationLicenseDirectory.Value, true);
}
}
}