mirror of
https://github.com/bitwarden/server.git
synced 2024-12-01 13:43:23 +01:00
96f9fbb951
* Update optionality to use org.FlexibleCollections Also break old feature flag key to ensure it's never enabled * Add logic to set defaults for collection management setting * Update optionality logic to use org property * Add comments * Add helper method for getting individual orgAbility * Fix validate user update permissions interface * Fix tests * dotnet format * Fix more tests * Simplify self-hosted update logic * Fix mapping * Use new getOrganizationAbility method * Refactor invite and save orgUser methods Pass in whole organization object instead of using OrganizationAbility * fix CipherService tests * dotnet format * Remove manager check to simplify this set of changes * Misc cleanup before review * Fix undefined variable * Refactor bulk-access endpoint to avoid early repo call * Restore manager check * Add tests for UpdateOrganizationLicenseCommand * Add nullable regions * Delete unused dependency * dotnet format * Fix test
101 lines
4.3 KiB
C#
101 lines
4.3 KiB
C#
using Bit.Core.AdminConsole.Entities;
|
|
using Bit.Core.Enums;
|
|
using Bit.Core.Models.Business;
|
|
using Bit.Core.Models.Data.Organizations;
|
|
using Bit.Core.OrganizationFeatures.OrganizationLicenses;
|
|
using Bit.Core.Services;
|
|
using Bit.Core.Settings;
|
|
using Bit.Test.Common.AutoFixture;
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
using Bit.Test.Common.Helpers;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
|
|
|
namespace Bit.Core.Test.OrganizationFeatures.OrganizationLicenses;
|
|
|
|
[SutProviderCustomize]
|
|
public class UpdateOrganizationLicenseCommandTests
|
|
{
|
|
private static string LicenseDirectory => Path.GetDirectoryName(OrganizationLicenseDirectory.Value);
|
|
private static Lazy<string> OrganizationLicenseDirectory => new(() =>
|
|
{
|
|
// Create a temporary directory to write the license file to
|
|
var directory = Path.Combine(Path.GetTempPath(), "bitwarden/");
|
|
if (!Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
return directory;
|
|
});
|
|
|
|
[Theory, BitAutoData]
|
|
public async Task UpdateLicenseAsync_UpdatesLicenseFileAndOrganization(
|
|
SelfHostedOrganizationDetails selfHostedOrg,
|
|
OrganizationLicense license,
|
|
SutProvider<UpdateOrganizationLicenseCommand> sutProvider)
|
|
{
|
|
var globalSettings = sutProvider.GetDependency<IGlobalSettings>();
|
|
globalSettings.LicenseDirectory = LicenseDirectory;
|
|
globalSettings.SelfHosted = true;
|
|
|
|
// Passing values for OrganizationLicense.CanUse
|
|
// NSubstitute cannot override non-virtual members so we have to ensure the real method passes
|
|
license.Enabled = true;
|
|
license.Issued = DateTime.Now.AddDays(-1);
|
|
license.Expires = DateTime.Now.AddDays(1);
|
|
license.Version = OrganizationLicense.CurrentLicenseFileVersion;
|
|
license.InstallationId = globalSettings.Installation.Id;
|
|
license.LicenseType = LicenseType.Organization;
|
|
sutProvider.GetDependency<ILicensingService>().VerifyLicense(license).Returns(true);
|
|
|
|
// Passing values for SelfHostedOrganizationDetails.CanUseLicense
|
|
// NSubstitute cannot override non-virtual members so we have to ensure the real method passes
|
|
license.Seats = null;
|
|
license.MaxCollections = null;
|
|
license.UseGroups = true;
|
|
license.UsePolicies = true;
|
|
license.UseSso = true;
|
|
license.UseKeyConnector = true;
|
|
license.UseScim = true;
|
|
license.UseCustomPermissions = true;
|
|
license.UseResetPassword = true;
|
|
|
|
try
|
|
{
|
|
await sutProvider.Sut.UpdateLicenseAsync(selfHostedOrg, license, null);
|
|
|
|
// Assertion: should have saved the license file to disk
|
|
var filePath = Path.Combine(LicenseDirectory, "organization", $"{selfHostedOrg.Id}.json");
|
|
await using var fs = File.OpenRead(filePath);
|
|
var licenseFromFile = await JsonSerializer.DeserializeAsync<OrganizationLicense>(fs);
|
|
|
|
AssertHelper.AssertPropertyEqual(license, licenseFromFile, "SignatureBytes");
|
|
|
|
// Assertion: should have updated and saved the organization
|
|
// Properties excluded from the comparison below are exceptions to the rule that the Organization mirrors
|
|
// the OrganizationLicense
|
|
await sutProvider.GetDependency<IOrganizationService>()
|
|
.Received(1)
|
|
.ReplaceAndUpdateCacheAsync(Arg.Is<Organization>(
|
|
org => AssertPropertyEqual(license, org,
|
|
"Id", "MaxStorageGb", "Issued", "Refresh", "Version", "Trial", "LicenseType",
|
|
"Hash", "Signature", "SignatureBytes", "InstallationId", "Expires", "ExpirationWithoutGracePeriod") &&
|
|
// Same property but different name, use explicit mapping
|
|
org.ExpirationDate == license.Expires));
|
|
}
|
|
finally
|
|
{
|
|
// Clean up temporary directory
|
|
Directory.Delete(OrganizationLicenseDirectory.Value, true);
|
|
}
|
|
}
|
|
|
|
// Wrapper to compare 2 objects that are different types
|
|
private bool AssertPropertyEqual(OrganizationLicense expected, Organization actual, params string[] excludedPropertyStrings)
|
|
{
|
|
AssertHelper.AssertPropertyEqual(expected, actual, excludedPropertyStrings);
|
|
return true;
|
|
}
|
|
}
|