-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathCreatePremiumCheckoutSessionCommandTests.cs
More file actions
310 lines (262 loc) · 13.4 KB
/
CreatePremiumCheckoutSessionCommandTests.cs
File metadata and controls
310 lines (262 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using Bit.Core.Billing;
using Bit.Core.Billing.Constants;
using Bit.Core.Billing.Premium.Commands;
using Bit.Core.Billing.Pricing;
using Bit.Core.Billing.Services;
using Bit.Core.Entities;
using Bit.Core.Exceptions;
using Bit.Core.Settings;
using Bit.Test.Common.AutoFixture.Attributes;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Stripe;
using Stripe.Checkout;
using Xunit;
using PremiumPlan = Bit.Core.Billing.Pricing.Premium.Plan;
using PremiumPurchasable = Bit.Core.Billing.Pricing.Premium.Purchasable;
namespace Bit.Core.Test.Billing.Premium.Commands;
public class CreatePremiumCheckoutSessionCommandTests
{
private readonly IStripeAdapter _stripeAdapter = Substitute.For<IStripeAdapter>();
private readonly IPricingClient _pricingClient = Substitute.For<IPricingClient>();
private readonly ISubscriberService _subscriberService = Substitute.For<ISubscriberService>();
private readonly IGlobalSettings _globalSettings = Substitute.For<IGlobalSettings>();
private readonly ILogger<CreatePremiumCheckoutSessionCommand> _logger = Substitute.For<ILogger<CreatePremiumCheckoutSessionCommand>>();
private readonly ICreatePremiumCheckoutSessionCommand _command;
private const string _successUrl = "success/url";
private const string _cancelUrl = "cancel/url";
private const string _browserSuccessUrl = "browser/success/url";
private const string _browserCancelUrl = "browser/cancel/url";
private const string _desktopSuccessUrl = "desktop/success/url";
private const string _desktopCancelUrl = "desktop/cancel/url";
public CreatePremiumCheckoutSessionCommandTests()
{
var stripeSettings = new GlobalSettings.StripeSettings
{
PremiumCheckoutSuccessUrl = _successUrl,
PremiumCheckoutCancelUrl = _cancelUrl,
BrowserPremiumCheckoutSuccessUrl = _browserSuccessUrl,
BrowserPremiumCheckoutCancelUrl = _browserCancelUrl,
DesktopPremiumCheckoutSuccessUrl = _desktopSuccessUrl,
DesktopPremiumCheckoutCancelUrl = _desktopCancelUrl
};
_globalSettings.Stripe.Returns(stripeSettings);
var premiumPlan = new PremiumPlan
{
Name = "Premium",
Available = true,
LegacyYear = null,
Seat = new PremiumPurchasable { Price = 10M, StripePriceId = StripeConstants.Prices.PremiumAnnually },
Storage = new PremiumPurchasable { Price = 4M, StripePriceId = StripeConstants.Prices.StoragePlanPersonal }
};
_pricingClient.GetAvailablePremiumPlan().Returns(premiumPlan);
_command = new CreatePremiumCheckoutSessionCommand(
_stripeAdapter,
_pricingClient,
_subscriberService,
_globalSettings,
_logger);
}
[Theory]
[BitAutoData]
public async Task Run_UserNotPremium_UserDoesNotHaveExistingStripeCustomer_ReturnsCheckoutSessionUrl(User user)
{
// Arrange
user.Premium = false;
user.GatewayCustomerId = null;
const string appVersion = "1.0.0";
var platform = StripeConstants.CheckoutSession.Platforms.Ios;
var newCustomer = new Customer { Id = "cus_123" };
_subscriberService.CreateStripeCustomer(user).Returns(newCustomer);
const string checkoutSessionUrl = "https://checkout.stripe.com/session/123";
_stripeAdapter.CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>()).Returns(new Session { Url = checkoutSessionUrl });
// Act
var result = await _command.Run(user, appVersion, platform);
// Assert
Assert.True(result.Success);
Assert.Equal(checkoutSessionUrl, result.AsT0.CheckoutSessionUrl);
await _subscriberService.Received(1).CreateStripeCustomer(user);
await _stripeAdapter.Received(1).CreateCheckoutSessionAsync(Arg.Is<SessionCreateOptions>(options =>
options.Customer == "cus_123"
&& options.Mode == StripeConstants.CheckoutSession.Modes.Subscription
&& options.LineItems[0].Price == StripeConstants.Prices.PremiumAnnually
&& options.LineItems[0].Quantity == 1
&& options.AutomaticTax.Enabled == true
&& options.SuccessUrl == _successUrl
&& options.CancelUrl == _cancelUrl
&& options.PaymentMethodTypes.Contains(StripeConstants.PaymentMethodTypes.Card)
&& options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.UserId] == user.Id.ToString()
&& options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.OriginatingAppVersion] == appVersion
&& options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.OriginatingPlatform] == platform));
}
[Theory]
[BitAutoData]
public async Task Run_UserNotPremium_UserHasExistingStripeCustomer_ReturnsCheckoutSessionUrl(User user)
{
// Arrange
user.Premium = false;
user.GatewayCustomerId = "cus_existing";
const string appVersion = "2.0.0";
var platform = StripeConstants.CheckoutSession.Platforms.Android;
var existingCustomer = new Customer { Id = "cus_existing" };
_subscriberService.GetCustomerOrThrow(user).Returns(existingCustomer);
const string checkoutSessionUrl = "https://checkout.stripe.com/session/456";
_stripeAdapter.CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>()).Returns(new Session { Url = checkoutSessionUrl });
// Act
var result = await _command.Run(user, appVersion, platform);
// Assert
Assert.True(result.Success);
Assert.Equal(checkoutSessionUrl, result.AsT0.CheckoutSessionUrl);
await _stripeAdapter.Received(1).CreateCheckoutSessionAsync(Arg.Is<SessionCreateOptions>(options =>
options.Customer == existingCustomer.Id
&& options.Mode == StripeConstants.CheckoutSession.Modes.Subscription
&& options.LineItems[0].Price == StripeConstants.Prices.PremiumAnnually
&& options.LineItems[0].Quantity == 1
&& options.AutomaticTax.Enabled == true
&& options.SuccessUrl == _successUrl
&& options.CancelUrl == _cancelUrl
&& options.PaymentMethodTypes.Contains(StripeConstants.PaymentMethodTypes.Card)
&& options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.UserId] == user.Id.ToString()
&& options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.OriginatingAppVersion] == appVersion
&& options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.OriginatingPlatform] == platform));
}
[Theory]
[BitAutoData]
public async Task Run_UserIsPremium_ReturnsBadRequest(User user)
{
// Arrange
user.Premium = true;
// Act
var result = await _command.Run(user, "1.0.0", StripeConstants.CheckoutSession.Platforms.Ios);
// Assert
Assert.True(result.IsT1);
var badRequest = result.AsT1;
Assert.Equal("User is already a premium user.", badRequest.Response);
await _subscriberService.DidNotReceive().CreateStripeCustomer(Arg.Any<User>());
await _stripeAdapter.DidNotReceive().CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>());
}
[Theory]
[BitAutoData]
public async Task Run_CreateStripeCustomerThrows_ReturnsUnhandled(User user)
{
// Arrange
user.Premium = false;
user.GatewayCustomerId = null;
_subscriberService.CreateStripeCustomer(user).ThrowsAsync(new BillingException());
// Act
var result = await _command.Run(user, "1.0.0", StripeConstants.CheckoutSession.Platforms.Ios);
// Assert
Assert.True(result.IsT3);
Assert.IsType<BillingException>(result.AsT3.Exception);
await _stripeAdapter.DidNotReceive().CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>());
}
[Theory]
[BitAutoData]
public async Task Run_GetCustomerOrThrowThrows_ReturnsUnhandled(User user)
{
// Arrange
user.Premium = false;
user.GatewayCustomerId = "cus_existing";
_subscriberService.GetCustomerOrThrow(user).ThrowsAsync(new BillingException());
// Act
var result = await _command.Run(user, "1.0.0", StripeConstants.CheckoutSession.Platforms.Ios);
// Assert
Assert.True(result.IsT3);
Assert.IsType<BillingException>(result.AsT3.Exception);
await _stripeAdapter.DidNotReceive().CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>());
}
[Theory]
[BitAutoData]
public async Task Run_GetAvailablePremiumPlanThrows_ReturnsUnhandled(User user)
{
// Arrange
user.Premium = false;
user.GatewayCustomerId = null;
_subscriberService.CreateStripeCustomer(user).Returns(new Customer { Id = "cus_123" });
_pricingClient.GetAvailablePremiumPlan().ThrowsAsync<NotFoundException>();
// Act
var result = await _command.Run(user, "1.0.0", StripeConstants.CheckoutSession.Platforms.Ios);
// Assert
Assert.True(result.IsT3); // UnhandledException
Assert.IsType<NotFoundException>(result.AsT3.Exception);
await _stripeAdapter.DidNotReceive().CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>());
}
[Theory]
[BitAutoData]
public async Task Run_UserNotPremium_BrowserPlatform_UsesCorrectUrls(User user)
{
// Arrange
user.Premium = false;
user.GatewayCustomerId = null;
const string appVersion = "1.0.0";
var platform = StripeConstants.CheckoutSession.Platforms.Browser;
var newCustomer = new Customer { Id = "cus_123" };
_subscriberService.CreateStripeCustomer(user).Returns(newCustomer);
const string checkoutSessionUrl = "https://checkout.stripe.com/session/789";
_stripeAdapter.CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>()).Returns(new Session { Url = checkoutSessionUrl });
// Act
var result = await _command.Run(user, appVersion, platform);
// Assert
Assert.True(result.Success);
Assert.Equal(checkoutSessionUrl, result.AsT0.CheckoutSessionUrl);
await _stripeAdapter.Received(1).CreateCheckoutSessionAsync(Arg.Is<SessionCreateOptions>(options =>
options.Customer == "cus_123"
&& options.Mode == StripeConstants.CheckoutSession.Modes.Subscription
&& options.LineItems[0].Price == StripeConstants.Prices.PremiumAnnually
&& options.LineItems[0].Quantity == 1
&& options.AutomaticTax.Enabled == true
&& options.SuccessUrl == _browserSuccessUrl
&& options.CancelUrl == _browserCancelUrl
&& options.PaymentMethodTypes.Contains(StripeConstants.PaymentMethodTypes.Card)
&& options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.UserId] == user.Id.ToString()
&& options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.OriginatingAppVersion] == appVersion
&& options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.OriginatingPlatform] == platform));
}
[Theory]
[BitAutoData]
public async Task Run_UserNotPremium_DesktopPlatform_UsesCorrectUrls(User user)
{
// Arrange
user.Premium = false;
user.GatewayCustomerId = null;
const string appVersion = "1.0.0";
var platform = StripeConstants.CheckoutSession.Platforms.Desktop;
var newCustomer = new Customer { Id = "cus_123" };
_subscriberService.CreateStripeCustomer(user).Returns(newCustomer);
const string checkoutSessionUrl = "https://checkout.stripe.com/session/101";
_stripeAdapter.CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>()).Returns(new Session { Url = checkoutSessionUrl });
// Act
var result = await _command.Run(user, appVersion, platform);
// Assert
Assert.True(result.Success);
Assert.Equal(checkoutSessionUrl, result.AsT0.CheckoutSessionUrl);
await _stripeAdapter.Received(1).CreateCheckoutSessionAsync(Arg.Is<SessionCreateOptions>(options =>
options.Customer == "cus_123"
&& options.Mode == StripeConstants.CheckoutSession.Modes.Subscription
&& options.LineItems[0].Price == StripeConstants.Prices.PremiumAnnually
&& options.LineItems[0].Quantity == 1
&& options.AutomaticTax.Enabled == true
&& options.SuccessUrl == _desktopSuccessUrl
&& options.CancelUrl == _desktopCancelUrl
&& options.PaymentMethodTypes.Contains(StripeConstants.PaymentMethodTypes.Card)
&& options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.UserId] == user.Id.ToString()
&& options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.OriginatingAppVersion] == appVersion
&& options.SubscriptionData.Metadata[StripeConstants.MetadataKeys.OriginatingPlatform] == platform));
}
[Theory]
[BitAutoData]
public async Task Run_UnsupportedPlatform_ReturnsUnhandledException(User user)
{
// Arrange
user.Premium = false;
user.GatewayCustomerId = null;
_subscriberService.CreateStripeCustomer(user).Returns(new Customer { Id = "cus_123" });
// Act
var result = await _command.Run(user, "1.0.0", "web");
// Assert
Assert.True(result.IsT3);
Assert.IsType<InvalidOperationException>(result.AsT3.Exception);
await _stripeAdapter.DidNotReceive().CreateCheckoutSessionAsync(Arg.Any<SessionCreateOptions>());
}
}