mirror of
https://github.com/bitwarden/server
synced 2025-12-06 00:03:34 +00:00
[PM-25088] - refactor premium purchase endpoint (#6262)
* [PM-25088] add feature flag for new premium subscription flow * [PM-25088] refactor premium endpoint * forgot the punctuation change in the test * [PM-25088] - pr feedback * [PM-25088] - pr feedback round two
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
using System.Text.Json;
|
||||
using AutoFixture;
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Billing.Models.Business;
|
||||
using Bit.Core.Billing.Organizations.Models;
|
||||
using Bit.Core.Billing.Services;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Test.Billing.AutoFixture;
|
||||
using Bit.Test.Common.AutoFixture;
|
||||
@@ -16,6 +18,8 @@ public class LicensingServiceTests
|
||||
{
|
||||
private static string licenseFilePath(Guid orgId) =>
|
||||
Path.Combine(OrganizationLicenseDirectory.Value, $"{orgId}.json");
|
||||
private static string userLicenseFilePath(Guid userId) =>
|
||||
Path.Combine(UserLicenseDirectory.Value, $"{userId}.json");
|
||||
private static string LicenseDirectory => Path.GetDirectoryName(OrganizationLicenseDirectory.Value);
|
||||
private static Lazy<string> OrganizationLicenseDirectory => new(() =>
|
||||
{
|
||||
@@ -26,6 +30,15 @@ public class LicensingServiceTests
|
||||
}
|
||||
return directory;
|
||||
});
|
||||
private static Lazy<string> UserLicenseDirectory => new(() =>
|
||||
{
|
||||
var directory = Path.Combine(Path.GetTempPath(), "user");
|
||||
if (!Directory.Exists(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
return directory;
|
||||
});
|
||||
|
||||
public static SutProvider<LicensingService> GetSutProvider()
|
||||
{
|
||||
@@ -57,4 +70,66 @@ public class LicensingServiceTests
|
||||
Directory.Delete(OrganizationLicenseDirectory.Value, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task WriteUserLicense_CreatesFileWithCorrectContent(User user, UserLicense license)
|
||||
{
|
||||
// Arrange
|
||||
var sutProvider = GetSutProvider();
|
||||
var expectedFilePath = userLicenseFilePath(user.Id);
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
await sutProvider.Sut.WriteUserLicenseAsync(user, license);
|
||||
|
||||
// Assert
|
||||
Assert.True(File.Exists(expectedFilePath));
|
||||
var fileContent = await File.ReadAllTextAsync(expectedFilePath);
|
||||
var actualLicense = JsonSerializer.Deserialize<UserLicense>(fileContent);
|
||||
|
||||
Assert.Equal(license.LicenseKey, actualLicense.LicenseKey);
|
||||
Assert.Equal(license.Id, actualLicense.Id);
|
||||
Assert.Equal(license.Expires, actualLicense.Expires);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Cleanup
|
||||
if (Directory.Exists(UserLicenseDirectory.Value))
|
||||
{
|
||||
Directory.Delete(UserLicenseDirectory.Value, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task WriteUserLicense_CreatesDirectoryIfNotExists(User user, UserLicense license)
|
||||
{
|
||||
// Arrange
|
||||
var sutProvider = GetSutProvider();
|
||||
|
||||
// Ensure directory doesn't exist
|
||||
if (Directory.Exists(UserLicenseDirectory.Value))
|
||||
{
|
||||
Directory.Delete(UserLicenseDirectory.Value, true);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
await sutProvider.Sut.WriteUserLicenseAsync(user, license);
|
||||
|
||||
// Assert
|
||||
Assert.True(Directory.Exists(UserLicenseDirectory.Value));
|
||||
Assert.True(File.Exists(userLicenseFilePath(user.Id)));
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Cleanup
|
||||
if (Directory.Exists(UserLicenseDirectory.Value))
|
||||
{
|
||||
Directory.Delete(UserLicenseDirectory.Value, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user