1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-13 15:36:45 +01:00

[PM-14984] Use provider subscription for MSP managed enterprise license (#5102)

* Use provider subscription when creating license for MSP managed enterprise organization

* Run dotnet format
This commit is contained in:
Alex Morask 2024-12-12 07:08:17 -05:00 committed by GitHub
parent 2d891b396a
commit c852575a9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 2 deletions

View File

@ -1,4 +1,6 @@
using Bit.Core.AdminConsole.Entities; using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Enums;
using Bit.Core.Exceptions; using Bit.Core.Exceptions;
using Bit.Core.Models.Business; using Bit.Core.Models.Business;
using Bit.Core.OrganizationFeatures.OrganizationLicenses.Interfaces; using Bit.Core.OrganizationFeatures.OrganizationLicenses.Interfaces;
@ -12,15 +14,18 @@ public class CloudGetOrganizationLicenseQuery : ICloudGetOrganizationLicenseQuer
private readonly IInstallationRepository _installationRepository; private readonly IInstallationRepository _installationRepository;
private readonly IPaymentService _paymentService; private readonly IPaymentService _paymentService;
private readonly ILicensingService _licensingService; private readonly ILicensingService _licensingService;
private readonly IProviderRepository _providerRepository;
public CloudGetOrganizationLicenseQuery( public CloudGetOrganizationLicenseQuery(
IInstallationRepository installationRepository, IInstallationRepository installationRepository,
IPaymentService paymentService, IPaymentService paymentService,
ILicensingService licensingService) ILicensingService licensingService,
IProviderRepository providerRepository)
{ {
_installationRepository = installationRepository; _installationRepository = installationRepository;
_paymentService = paymentService; _paymentService = paymentService;
_licensingService = licensingService; _licensingService = licensingService;
_providerRepository = providerRepository;
} }
public async Task<OrganizationLicense> GetLicenseAsync(Organization organization, Guid installationId, public async Task<OrganizationLicense> GetLicenseAsync(Organization organization, Guid installationId,
@ -32,11 +37,22 @@ public class CloudGetOrganizationLicenseQuery : ICloudGetOrganizationLicenseQuer
throw new BadRequestException("Invalid installation id"); throw new BadRequestException("Invalid installation id");
} }
var subscriptionInfo = await _paymentService.GetSubscriptionAsync(organization); var subscriptionInfo = await GetSubscriptionAsync(organization);
return new OrganizationLicense(organization, subscriptionInfo, installationId, _licensingService, version) return new OrganizationLicense(organization, subscriptionInfo, installationId, _licensingService, version)
{ {
Token = await _licensingService.CreateOrganizationTokenAsync(organization, installationId, subscriptionInfo) Token = await _licensingService.CreateOrganizationTokenAsync(organization, installationId, subscriptionInfo)
}; };
} }
private async Task<SubscriptionInfo> GetSubscriptionAsync(Organization organization)
{
if (organization is not { Status: OrganizationStatusType.Managed })
{
return await _paymentService.GetSubscriptionAsync(organization);
}
var provider = await _providerRepository.GetByOrganizationIdAsync(organization.Id);
return await _paymentService.GetSubscriptionAsync(provider);
}
} }

View File

@ -1,4 +1,6 @@
using Bit.Core.AdminConsole.Entities; using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Entities.Provider;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Entities; using Bit.Core.Entities;
using Bit.Core.Enums; using Bit.Core.Enums;
using Bit.Core.Exceptions; using Bit.Core.Exceptions;
@ -11,6 +13,7 @@ using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes; using Bit.Test.Common.AutoFixture.Attributes;
using NSubstitute; using NSubstitute;
using NSubstitute.ReturnsExtensions; using NSubstitute.ReturnsExtensions;
using Stripe;
using Xunit; using Xunit;
namespace Bit.Core.Test.OrganizationFeatures.OrganizationLicenses; namespace Bit.Core.Test.OrganizationFeatures.OrganizationLicenses;
@ -62,4 +65,34 @@ public class CloudGetOrganizationLicenseQueryTests
Assert.Equal(installationId, result.InstallationId); Assert.Equal(installationId, result.InstallationId);
Assert.Equal(licenseSignature, result.SignatureBytes); Assert.Equal(licenseSignature, result.SignatureBytes);
} }
[Theory]
[BitAutoData]
public async Task GetLicenseAsync_MSPManagedOrganization_UsesProviderSubscription(SutProvider<CloudGetOrganizationLicenseQuery> sutProvider,
Organization organization, Guid installationId, Installation installation, SubscriptionInfo subInfo,
byte[] licenseSignature, Provider provider)
{
organization.Status = OrganizationStatusType.Managed;
organization.ExpirationDate = null;
subInfo.Subscription = new SubscriptionInfo.BillingSubscription(new Subscription
{
CurrentPeriodStart = DateTime.UtcNow,
CurrentPeriodEnd = DateTime.UtcNow.AddMonths(1)
});
installation.Enabled = true;
sutProvider.GetDependency<IInstallationRepository>().GetByIdAsync(installationId).Returns(installation);
sutProvider.GetDependency<IProviderRepository>().GetByOrganizationIdAsync(organization.Id).Returns(provider);
sutProvider.GetDependency<IPaymentService>().GetSubscriptionAsync(provider).Returns(subInfo);
sutProvider.GetDependency<ILicensingService>().SignLicense(Arg.Any<ILicense>()).Returns(licenseSignature);
var result = await sutProvider.Sut.GetLicenseAsync(organization, installationId);
Assert.Equal(LicenseType.Organization, result.LicenseType);
Assert.Equal(organization.Id, result.Id);
Assert.Equal(installationId, result.InstallationId);
Assert.Equal(licenseSignature, result.SignatureBytes);
Assert.Equal(DateTime.UtcNow.AddYears(1).Date, result.Expires!.Value.Date);
}
} }