1
0
mirror of https://github.com/bitwarden/server synced 2026-01-14 14:33:51 +00:00

[PM-29611] Decouple License from Subscription Response (#6768)

* implement the ticket request

* resolve the build lint error

* Resolve the build lint error

* Address review comments

* Fixt the lint and failing unit test

* Fix NSubstitute mock - use concrete ClaimsPrincipal instead of Arg.Any in Returns()

* resolve InjectUser issues

* Fix the failing testing

* Fix the failing unit test
This commit is contained in:
cyprain-okeke
2025-12-31 17:30:41 +01:00
committed by GitHub
parent bf08bd279c
commit 665be6bfb0
7 changed files with 164 additions and 0 deletions

View File

@@ -85,6 +85,7 @@ public class AccountsControllerTests : IDisposable
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe; // User has payment gateway
@@ -124,6 +125,7 @@ public class AccountsControllerTests : IDisposable
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(false);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe; // User has payment gateway
@@ -161,6 +163,7 @@ public class AccountsControllerTests : IDisposable
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe; // User has payment gateway
@@ -207,6 +210,7 @@ public class AccountsControllerTests : IDisposable
};
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
_userService.GenerateLicenseAsync(user).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
// Act
var result = await _sut.GetSubscriptionAsync(_globalSettings, _paymentService);
@@ -243,6 +247,7 @@ public class AccountsControllerTests : IDisposable
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe; // User has payment gateway
@@ -293,6 +298,7 @@ public class AccountsControllerTests : IDisposable
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;
@@ -349,6 +355,7 @@ public class AccountsControllerTests : IDisposable
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;
// Act & Assert - Feature flag ENABLED
@@ -413,6 +420,7 @@ public class AccountsControllerTests : IDisposable
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;
// Act - Step 4: Call AccountsController.GetSubscriptionAsync
@@ -507,6 +515,7 @@ public class AccountsControllerTests : IDisposable
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;
// Act
@@ -558,6 +567,7 @@ public class AccountsControllerTests : IDisposable
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;
// Act
@@ -611,6 +621,7 @@ public class AccountsControllerTests : IDisposable
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;
// Act
@@ -658,6 +669,7 @@ public class AccountsControllerTests : IDisposable
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;
// Act
@@ -726,6 +738,7 @@ public class AccountsControllerTests : IDisposable
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true);
_paymentService.GetSubscriptionAsync(user).Returns(subscriptionInfo);
_userService.GenerateLicenseAsync(user, subscriptionInfo).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
user.Gateway = GatewayType.Stripe;
// Act - Full pipeline: Stripe → SubscriptionInfo → SubscriptionResponseModel → API response
@@ -791,6 +804,7 @@ public class AccountsControllerTests : IDisposable
};
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
_userService.GenerateLicenseAsync(user).Returns(license);
_licensingService.GetClaimsPrincipalFromLicense(license).Returns(new ClaimsPrincipal());
_featureService.IsEnabled(FeatureFlagKeys.PM23341_Milestone_2).Returns(true); // Flag enabled
// Act

View File

@@ -0,0 +1,57 @@
using Bit.Api.Billing.Controllers.VNext;
using Bit.Core.Billing.Licenses.Queries;
using Bit.Core.Billing.Payment.Commands;
using Bit.Core.Billing.Payment.Queries;
using Bit.Core.Billing.Premium.Commands;
using Bit.Core.Entities;
using Bit.Test.Common.AutoFixture.Attributes;
using Microsoft.AspNetCore.Http;
using NSubstitute;
using Xunit;
namespace Bit.Api.Test.Billing.Controllers.VNext;
public class AccountBillingVNextControllerTests
{
private readonly ICreateBitPayInvoiceForCreditCommand _createBitPayInvoiceForCreditCommand;
private readonly ICreatePremiumCloudHostedSubscriptionCommand _createPremiumCloudHostedSubscriptionCommand;
private readonly IGetCreditQuery _getCreditQuery;
private readonly IGetPaymentMethodQuery _getPaymentMethodQuery;
private readonly IGetUserLicenseQuery _getUserLicenseQuery;
private readonly IUpdatePaymentMethodCommand _updatePaymentMethodCommand;
private readonly AccountBillingVNextController _sut;
public AccountBillingVNextControllerTests()
{
_createBitPayInvoiceForCreditCommand = Substitute.For<ICreateBitPayInvoiceForCreditCommand>();
_createPremiumCloudHostedSubscriptionCommand = Substitute.For<ICreatePremiumCloudHostedSubscriptionCommand>();
_getCreditQuery = Substitute.For<IGetCreditQuery>();
_getPaymentMethodQuery = Substitute.For<IGetPaymentMethodQuery>();
_getUserLicenseQuery = Substitute.For<IGetUserLicenseQuery>();
_updatePaymentMethodCommand = Substitute.For<IUpdatePaymentMethodCommand>();
_sut = new AccountBillingVNextController(
_createBitPayInvoiceForCreditCommand,
_createPremiumCloudHostedSubscriptionCommand,
_getCreditQuery,
_getPaymentMethodQuery,
_getUserLicenseQuery,
_updatePaymentMethodCommand);
}
[Theory, BitAutoData]
public async Task GetLicenseAsync_ValidUser_ReturnsLicenseResponse(User user,
Core.Billing.Licenses.Models.Api.Response.LicenseResponseModel licenseResponse)
{
// Arrange
_getUserLicenseQuery.Run(user).Returns(licenseResponse);
// Act
var result = await _sut.GetLicenseAsync(user);
// Assert
var okResult = Assert.IsAssignableFrom<IResult>(result);
await _getUserLicenseQuery.Received(1).Run(user);
}
}