Skip to content

Commit

Permalink
[PM-14984] Use provider subscription for MSP managed enterprise licen…
Browse files Browse the repository at this point in the history
…se (#5102)

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

* Run dotnet format
  • Loading branch information
amorask-bitwarden authored Dec 12, 2024
1 parent 2d891b3 commit c852575
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Enums;
using Bit.Core.Exceptions;
using Bit.Core.Models.Business;
using Bit.Core.OrganizationFeatures.OrganizationLicenses.Interfaces;
Expand All @@ -12,15 +14,18 @@ public class CloudGetOrganizationLicenseQuery : ICloudGetOrganizationLicenseQuer
private readonly IInstallationRepository _installationRepository;
private readonly IPaymentService _paymentService;
private readonly ILicensingService _licensingService;
private readonly IProviderRepository _providerRepository;

public CloudGetOrganizationLicenseQuery(
IInstallationRepository installationRepository,
IPaymentService paymentService,
ILicensingService licensingService)
ILicensingService licensingService,
IProviderRepository providerRepository)
{
_installationRepository = installationRepository;
_paymentService = paymentService;
_licensingService = licensingService;
_providerRepository = providerRepository;
}

public async Task<OrganizationLicense> GetLicenseAsync(Organization organization, Guid installationId,
Expand All @@ -32,11 +37,22 @@ public async Task<OrganizationLicense> GetLicenseAsync(Organization organization
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)
{
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Entities.Provider;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.Exceptions;
Expand All @@ -11,6 +13,7 @@
using Bit.Test.Common.AutoFixture.Attributes;
using NSubstitute;
using NSubstitute.ReturnsExtensions;
using Stripe;
using Xunit;

namespace Bit.Core.Test.OrganizationFeatures.OrganizationLicenses;
Expand Down Expand Up @@ -62,4 +65,34 @@ public async Task GetLicenseAsync_CreatesAndReturns(SutProvider<CloudGetOrganiza
Assert.Equal(installationId, result.InstallationId);
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);
}
}

0 comments on commit c852575

Please sign in to comment.