|
| 1 | +using Bit.Core.Billing; |
| 2 | +using Bit.Core.Billing.Constants; |
| 3 | +using Bit.Core.Billing.Premium.Commands; |
| 4 | +using Bit.Core.Billing.Pricing; |
| 5 | +using Bit.Core.Billing.Services; |
| 6 | +using Bit.Core.Entities; |
| 7 | +using Bit.Core.Exceptions; |
| 8 | +using Bit.Core.Services; |
| 9 | +using Bit.Core.Settings; |
| 10 | +using Bit.Test.Common.AutoFixture.Attributes; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | +using NSubstitute; |
| 13 | +using NSubstitute.ExceptionExtensions; |
| 14 | +using Stripe; |
| 15 | +using Stripe.Checkout; |
| 16 | +using Xunit; |
| 17 | +using PremiumPlan = Bit.Core.Billing.Pricing.Premium.Plan; |
| 18 | +using PremiumPurchasable = Bit.Core.Billing.Pricing.Premium.Purchasable; |
| 19 | + |
| 20 | + |
| 21 | +namespace Bit.Core.Test.Billing.Premium.Commands; |
| 22 | + |
| 23 | +public class CreatePremiumCheckoutSessionCommandTests |
| 24 | +{ |
| 25 | + private readonly IStripeAdapter _stripeAdapter = Substitute.For<IStripeAdapter>(); |
| 26 | + private readonly IPricingClient _pricingClient = Substitute.For<IPricingClient>(); |
| 27 | + private readonly ISubscriberService _subscriberService = Substitute.For<ISubscriberService>(); |
| 28 | + private readonly IUserService _userService= Substitute.For<IUserService>(); |
| 29 | + private readonly IGlobalSettings _globalSettings = Substitute.For<IGlobalSettings>(); |
| 30 | + private readonly ILogger<CreatePremiumCheckoutSessionCommand> _logger = Substitute.For<ILogger<CreatePremiumCheckoutSessionCommand>>(); |
| 31 | + private readonly ICreatePremiumCheckoutSessionCommand _command; |
| 32 | + |
| 33 | + private const string _successUrl = "success/url"; |
| 34 | + private const string _cancelUrl = "cancel/url"; |
| 35 | + |
| 36 | + public CreatePremiumCheckoutSessionCommandTests() |
| 37 | + { |
| 38 | + var stripeSettings = new GlobalSettings.StripeSettings |
| 39 | + { |
| 40 | + PremiumCheckoutSuccessUrl = _successUrl, |
| 41 | + PremiumCheckoutCancelUrl = _cancelUrl |
| 42 | + }; |
| 43 | + _globalSettings.Stripe.Returns(stripeSettings); |
| 44 | + |
| 45 | + var premiumPlan = new PremiumPlan |
| 46 | + { |
| 47 | + Name = "Premium", |
| 48 | + Available = true, |
| 49 | + LegacyYear = null, |
| 50 | + Seat = new PremiumPurchasable { Price = 10M, StripePriceId = StripeConstants.Prices.PremiumAnnually }, |
| 51 | + Storage = new PremiumPurchasable { Price = 4M, StripePriceId = StripeConstants.Prices.StoragePlanPersonal } |
| 52 | + }; |
| 53 | + |
| 54 | + _pricingClient.GetAvailablePremiumPlan().Returns(premiumPlan); |
| 55 | + |
| 56 | + _command = new CreatePremiumCheckoutSessionCommand( |
| 57 | + _stripeAdapter, |
| 58 | + _pricingClient, |
| 59 | + _subscriberService, |
| 60 | + _userService, |
| 61 | + _globalSettings, |
| 62 | + _logger); |
| 63 | + } |
| 64 | + |
| 65 | + [Theory] |
| 66 | + [BitAutoData] |
| 67 | + public async Task Run_UserNotPremium_UserDoesNotHaveExistingStripeCustomer_ReturnsCheckoutSessionUrl(User user) |
| 68 | + { |
| 69 | + // Arrange |
| 70 | + user.Premium = false; |
| 71 | + user.Name = "Test User"; |
| 72 | + user.Email = "test@example.com"; |
| 73 | + user.GatewayCustomerId = null; |
| 74 | + const string appVersion = "1.0.0"; |
| 75 | + const string platform = "iOS"; |
| 76 | + |
| 77 | + const string checkoutSessionUrl = "https://checkout.stripe.com/session/123"; |
| 78 | + _stripeAdapter.CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>()).Returns(new Session { Url = checkoutSessionUrl }); |
| 79 | + _stripeAdapter.CreateCustomerAsync(Arg.Any<CustomerCreateOptions>()).Returns(new Customer { Id = "cus_123" }); |
| 80 | + |
| 81 | + // Act |
| 82 | + var result = await _command.Run(user, appVersion, platform); |
| 83 | + |
| 84 | + // Assert |
| 85 | + Assert.True(result.Success); |
| 86 | + Assert.Equal(checkoutSessionUrl, result.AsT0.CheckoutSessionUrl); |
| 87 | + await _stripeAdapter.Received(1).CreateCheckoutSessionAsync(Arg.Is<SessionCreateOptions>(options => |
| 88 | + options.Customer == user.GatewayCustomerId |
| 89 | + && options.Mode == StripeConstants.CheckoutSession.Modes.Subscription |
| 90 | + && options.LineItems[0].Price == StripeConstants.Prices.PremiumAnnually |
| 91 | + && options.LineItems[0].Quantity == 1 |
| 92 | + && options.AutomaticTax.Enabled == true |
| 93 | + && options.SuccessUrl == _successUrl |
| 94 | + && options.CancelUrl == _cancelUrl |
| 95 | + && options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.UserId] == user.Id.ToString() |
| 96 | + && options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.OriginatingAppVersion] == appVersion |
| 97 | + && options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.OriginatingPlatform] == platform)); |
| 98 | + await _stripeAdapter.Received(1).CreateCustomerAsync(Arg.Is<CustomerCreateOptions>(options => |
| 99 | + options.Email == user.Email |
| 100 | + && options.Description == user.Name |
| 101 | + && options.Metadata[StripeConstants.MetadataKeys.Region] == _globalSettings.BaseServiceUri.CloudRegion)); |
| 102 | + await _userService.Received(1).SaveUserAsync(Arg.Is<User>(savedUser => |
| 103 | + savedUser.GatewayCustomerId == "cus_123")); |
| 104 | + } |
| 105 | + |
| 106 | + [Theory] |
| 107 | + [BitAutoData] |
| 108 | + public async Task Run_UserNotPremium_UserHasExistingStripeCustomer_ReturnsCheckoutSessionUrl(User user) |
| 109 | + { |
| 110 | + // Arrange |
| 111 | + user.Premium = false; |
| 112 | + user.GatewayCustomerId = "cus_existing"; |
| 113 | + const string appVersion = "2.0.0"; |
| 114 | + const string platform = "Android"; |
| 115 | + |
| 116 | + var existingCustomer = new Customer { Id = "cus_existing" }; |
| 117 | + _subscriberService.GetCustomerOrThrow(user).Returns(existingCustomer); |
| 118 | + |
| 119 | + const string checkoutSessionUrl = "https://checkout.stripe.com/session/456"; |
| 120 | + _stripeAdapter.CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>()).Returns(new Session { Url = checkoutSessionUrl }); |
| 121 | + |
| 122 | + // Act |
| 123 | + var result = await _command.Run(user, appVersion, platform); |
| 124 | + |
| 125 | + // Assert |
| 126 | + Assert.True(result.Success); |
| 127 | + Assert.Equal(checkoutSessionUrl, result.AsT0.CheckoutSessionUrl); |
| 128 | + await _stripeAdapter.DidNotReceive().CreateCustomerAsync(Arg.Any<CustomerCreateOptions>()); |
| 129 | + await _userService.DidNotReceive().SaveUserAsync(Arg.Any<User>()); |
| 130 | + await _stripeAdapter.Received(1).CreateCheckoutSessionAsync(Arg.Is<SessionCreateOptions>(options => |
| 131 | + options.Customer == existingCustomer.Id |
| 132 | + && options.Mode == StripeConstants.CheckoutSession.Modes.Subscription |
| 133 | + && options.LineItems[0].Price == StripeConstants.Prices.PremiumAnnually |
| 134 | + && options.LineItems[0].Quantity == 1 |
| 135 | + && options.AutomaticTax.Enabled == true |
| 136 | + && options.SuccessUrl == _successUrl |
| 137 | + && options.CancelUrl == _cancelUrl |
| 138 | + && options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.UserId] == user.Id.ToString() |
| 139 | + && options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.OriginatingAppVersion] == appVersion |
| 140 | + && options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.OriginatingPlatform] == platform)); |
| 141 | + } |
| 142 | + |
| 143 | + [Theory] |
| 144 | + [BitAutoData] |
| 145 | + public async Task Run_UserIsPremium_ReturnsBadRequest(User user) |
| 146 | + { |
| 147 | + // Arrange |
| 148 | + user.Premium = true; |
| 149 | + |
| 150 | + // Act |
| 151 | + var result = await _command.Run(user, "1.0.0", "iOS"); |
| 152 | + |
| 153 | + // Assert |
| 154 | + Assert.True(result.IsT1); |
| 155 | + var badRequest = result.AsT1; |
| 156 | + Assert.Equal("User is already a premium user.", badRequest.Response); |
| 157 | + await _stripeAdapter.DidNotReceive().CreateCustomerAsync(Arg.Any<CustomerCreateOptions>()); |
| 158 | + await _stripeAdapter.DidNotReceive().CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>()); |
| 159 | + } |
| 160 | + |
| 161 | + [Theory] |
| 162 | + [BitAutoData] |
| 163 | + public async Task Run_GetCustomerOrThrowThrows_ReturnsUnhandled(User user) |
| 164 | + { |
| 165 | + // Arrange |
| 166 | + user.Premium = false; |
| 167 | + user.GatewayCustomerId = "cus_existing"; |
| 168 | + |
| 169 | + _subscriberService.GetCustomerOrThrow(user).ThrowsAsync(new BillingException()); |
| 170 | + |
| 171 | + // Act |
| 172 | + var result = await _command.Run(user, "1.0.0", "iOS"); |
| 173 | + |
| 174 | + // Assert |
| 175 | + Assert.True(result.IsT3); |
| 176 | + Assert.IsType<BillingException>(result.AsT3.Exception); |
| 177 | + await _stripeAdapter.DidNotReceive().CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>()); |
| 178 | + } |
| 179 | + |
| 180 | + [Theory] |
| 181 | + [BitAutoData] |
| 182 | + public async Task Run_GetAvailablePremiumPlanThrows_ReturnsUnhandled(User user) |
| 183 | + { |
| 184 | + // Arrange |
| 185 | + user.Premium = false; |
| 186 | + user.GatewayCustomerId = null; |
| 187 | + |
| 188 | + _stripeAdapter.CreateCustomerAsync(Arg.Any<CustomerCreateOptions>()).Returns(new Customer { Id = "cus_123" }); |
| 189 | + _pricingClient.GetAvailablePremiumPlan().ThrowsAsync<NotFoundException>(); |
| 190 | + |
| 191 | + // Act |
| 192 | + var result = await _command.Run(user, "1.0.0", "iOS"); |
| 193 | + |
| 194 | + // Assert |
| 195 | + Assert.True(result.IsT3); // UnhandledException |
| 196 | + Assert.IsType<NotFoundException>(result.AsT3.Exception); |
| 197 | + await _stripeAdapter.DidNotReceive().CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>()); |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments