mirror of
https://github.com/bitwarden/server.git
synced 2026-01-31 14:13:18 +08:00
[PM-27123] Account Credit not Showing for Premium Upgrade Payment (#6484)
* feat(billing): add PaymentMethod union * feat(billing): add nontokenized payment method * feat(billing): add validation for tokinized and nontokenized payments * feat(billing): update and add payment method requests * feat(billing): update command with new union object * test(billing): add tests for account credit for user. * feat(billing): update premium cloud hosted subscription request * fix(billing): dotnet format * tests(billing): include payment method tests * fix(billing): clean up tests and converter method
This commit is contained in:
112
test/Core.Test/Billing/Payment/Models/PaymentMethodTests.cs
Normal file
112
test/Core.Test/Billing/Payment/Models/PaymentMethodTests.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using System.Text.Json;
|
||||
using Bit.Core.Billing.Payment.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.Billing.Payment.Models;
|
||||
|
||||
public class PaymentMethodTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("{\"cardNumber\":\"1234\"}")]
|
||||
[InlineData("{\"type\":\"unknown_type\",\"data\":\"value\"}")]
|
||||
[InlineData("{\"type\":\"invalid\",\"token\":\"test-token\"}")]
|
||||
[InlineData("{\"type\":\"invalid\"}")]
|
||||
public void Read_ShouldThrowJsonException_OnInvalidOrMissingType(string json)
|
||||
{
|
||||
// Arrange
|
||||
var options = new JsonSerializerOptions { Converters = { new PaymentMethodJsonConverter() } };
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<PaymentMethod>(json, options));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("{\"type\":\"card\"}")]
|
||||
[InlineData("{\"type\":\"card\",\"token\":\"\"}")]
|
||||
[InlineData("{\"type\":\"card\",\"token\":null}")]
|
||||
public void Read_ShouldThrowJsonException_OnInvalidTokenizedPaymentMethodToken(string json)
|
||||
{
|
||||
// Arrange
|
||||
var options = new JsonSerializerOptions { Converters = { new PaymentMethodJsonConverter() } };
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<PaymentMethod>(json, options));
|
||||
}
|
||||
|
||||
// Tokenized payment method deserialization
|
||||
[Theory]
|
||||
[InlineData("bankAccount", TokenizablePaymentMethodType.BankAccount)]
|
||||
[InlineData("card", TokenizablePaymentMethodType.Card)]
|
||||
[InlineData("payPal", TokenizablePaymentMethodType.PayPal)]
|
||||
public void Read_ShouldDeserializeTokenizedPaymentMethods(string typeString, TokenizablePaymentMethodType expectedType)
|
||||
{
|
||||
// Arrange
|
||||
var json = $"{{\"type\":\"{typeString}\",\"token\":\"test-token\"}}";
|
||||
var options = new JsonSerializerOptions { Converters = { new PaymentMethodJsonConverter() } };
|
||||
|
||||
// Act
|
||||
var result = JsonSerializer.Deserialize<PaymentMethod>(json, options);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsTokenized);
|
||||
Assert.Equal(expectedType, result.AsT0.Type);
|
||||
Assert.Equal("test-token", result.AsT0.Token);
|
||||
}
|
||||
|
||||
// Non-tokenized payment method deserialization
|
||||
[Theory]
|
||||
[InlineData("accountcredit", NonTokenizablePaymentMethodType.AccountCredit)]
|
||||
public void Read_ShouldDeserializeNonTokenizedPaymentMethods(string typeString, NonTokenizablePaymentMethodType expectedType)
|
||||
{
|
||||
// Arrange
|
||||
var json = $"{{\"type\":\"{typeString}\"}}";
|
||||
var options = new JsonSerializerOptions { Converters = { new PaymentMethodJsonConverter() } };
|
||||
|
||||
// Act
|
||||
var result = JsonSerializer.Deserialize<PaymentMethod>(json, options);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsNonTokenized);
|
||||
Assert.Equal(expectedType, result.AsT1.Type);
|
||||
}
|
||||
|
||||
// Tokenized payment method serialization
|
||||
[Theory]
|
||||
[InlineData(TokenizablePaymentMethodType.BankAccount, "bankaccount")]
|
||||
[InlineData(TokenizablePaymentMethodType.Card, "card")]
|
||||
[InlineData(TokenizablePaymentMethodType.PayPal, "paypal")]
|
||||
public void Write_ShouldSerializeTokenizedPaymentMethods(TokenizablePaymentMethodType type, string expectedTypeString)
|
||||
{
|
||||
// Arrange
|
||||
var paymentMethod = new PaymentMethod(new TokenizedPaymentMethod
|
||||
{
|
||||
Type = type,
|
||||
Token = "test-token"
|
||||
});
|
||||
var options = new JsonSerializerOptions { Converters = { new PaymentMethodJsonConverter() } };
|
||||
|
||||
// Act
|
||||
var json = JsonSerializer.Serialize(paymentMethod, options);
|
||||
|
||||
// Assert
|
||||
Assert.Contains($"\"type\":\"{expectedTypeString}\"", json);
|
||||
Assert.Contains("\"token\":\"test-token\"", json);
|
||||
}
|
||||
|
||||
// Non-tokenized payment method serialization
|
||||
[Theory]
|
||||
[InlineData(NonTokenizablePaymentMethodType.AccountCredit, "accountcredit")]
|
||||
public void Write_ShouldSerializeNonTokenizedPaymentMethods(NonTokenizablePaymentMethodType type, string expectedTypeString)
|
||||
{
|
||||
// Arrange
|
||||
var paymentMethod = new PaymentMethod(new NonTokenizedPaymentMethod { Type = type });
|
||||
var options = new JsonSerializerOptions { Converters = { new PaymentMethodJsonConverter() } };
|
||||
|
||||
// Act
|
||||
var json = JsonSerializer.Serialize(paymentMethod, options);
|
||||
|
||||
// Assert
|
||||
Assert.Contains($"\"type\":\"{expectedTypeString}\"", json);
|
||||
Assert.DoesNotContain("token", json);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Bit.Core.Billing.Caches;
|
||||
using Bit.Core.Billing;
|
||||
using Bit.Core.Billing.Caches;
|
||||
using Bit.Core.Billing.Constants;
|
||||
using Bit.Core.Billing.Extensions;
|
||||
using Bit.Core.Billing.Payment.Models;
|
||||
@@ -567,4 +568,79 @@ public class CreatePremiumCloudHostedSubscriptionCommandTests
|
||||
var unhandled = result.AsT3;
|
||||
Assert.Equal("Something went wrong with your request. Please contact support for assistance.", unhandled.Response);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task Run_AccountCredit_WithExistingCustomer_Success(
|
||||
User user,
|
||||
NonTokenizedPaymentMethod paymentMethod,
|
||||
BillingAddress billingAddress)
|
||||
{
|
||||
// Arrange
|
||||
user.Premium = false;
|
||||
user.GatewayCustomerId = "existing_customer_123";
|
||||
paymentMethod.Type = NonTokenizablePaymentMethodType.AccountCredit;
|
||||
billingAddress.Country = "US";
|
||||
billingAddress.PostalCode = "12345";
|
||||
|
||||
var mockCustomer = Substitute.For<StripeCustomer>();
|
||||
mockCustomer.Id = "existing_customer_123";
|
||||
mockCustomer.Address = new Address { Country = "US", PostalCode = "12345" };
|
||||
mockCustomer.Metadata = new Dictionary<string, string>();
|
||||
|
||||
var mockSubscription = Substitute.For<StripeSubscription>();
|
||||
mockSubscription.Id = "sub_123";
|
||||
mockSubscription.Status = "active";
|
||||
mockSubscription.Items = new StripeList<SubscriptionItem>
|
||||
{
|
||||
Data =
|
||||
[
|
||||
new SubscriptionItem
|
||||
{
|
||||
CurrentPeriodEnd = DateTime.UtcNow.AddDays(30)
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var mockInvoice = Substitute.For<Invoice>();
|
||||
|
||||
_subscriberService.GetCustomerOrThrow(Arg.Any<User>(), Arg.Any<CustomerGetOptions>()).Returns(mockCustomer);
|
||||
_stripeAdapter.SubscriptionCreateAsync(Arg.Any<SubscriptionCreateOptions>()).Returns(mockSubscription);
|
||||
_stripeAdapter.InvoiceUpdateAsync(Arg.Any<string>(), Arg.Any<InvoiceUpdateOptions>()).Returns(mockInvoice);
|
||||
|
||||
// Act
|
||||
var result = await _command.Run(user, paymentMethod, billingAddress, 0);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsT0);
|
||||
await _subscriberService.Received(1).GetCustomerOrThrow(Arg.Any<User>(), Arg.Any<CustomerGetOptions>());
|
||||
await _stripeAdapter.DidNotReceive().CustomerCreateAsync(Arg.Any<CustomerCreateOptions>());
|
||||
Assert.True(user.Premium);
|
||||
Assert.Equal(mockSubscription.GetCurrentPeriodEnd(), user.PremiumExpirationDate);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task Run_NonTokenizedPaymentWithoutExistingCustomer_ThrowsBillingException(
|
||||
User user,
|
||||
NonTokenizedPaymentMethod paymentMethod,
|
||||
BillingAddress billingAddress)
|
||||
{
|
||||
// Arrange
|
||||
user.Premium = false;
|
||||
// No existing gateway customer ID
|
||||
user.GatewayCustomerId = null;
|
||||
paymentMethod.Type = NonTokenizablePaymentMethodType.AccountCredit;
|
||||
billingAddress.Country = "US";
|
||||
billingAddress.PostalCode = "12345";
|
||||
|
||||
// Act
|
||||
var result = await _command.Run(user, paymentMethod, billingAddress, 0);
|
||||
|
||||
//Assert
|
||||
Assert.True(result.IsT3); // Assuming T3 is the Unhandled result
|
||||
Assert.IsType<BillingException>(result.AsT3.Exception);
|
||||
// Verify no customer was created or subscription attempted
|
||||
await _stripeAdapter.DidNotReceive().CustomerCreateAsync(Arg.Any<CustomerCreateOptions>());
|
||||
await _stripeAdapter.DidNotReceive().SubscriptionCreateAsync(Arg.Any<SubscriptionCreateOptions>());
|
||||
await _userService.DidNotReceive().SaveUserAsync(Arg.Any<User>());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user