using Bit.Core.AdminConsole.Entities; using Bit.Core.Billing.Services.Implementations; using Bit.Core.Entities; using Bit.Core.Models.BitStripe; using Bit.Core.Repositories; using Bit.Core.Services; using Microsoft.Extensions.Logging; using NSubstitute; using Stripe; using Xunit; namespace Bit.Core.Test.Billing.Services; public class PaymentHistoryServiceTests { [Fact] public async Task GetInvoiceHistoryAsync_Succeeds() { // Arrange var subscriber = new Organization { GatewayCustomerId = "cus_id", GatewaySubscriptionId = "sub_id" }; var invoices = new List { new() { Id = "in_id" } }; var stripeAdapter = Substitute.For(); stripeAdapter.InvoiceListAsync(Arg.Any()).Returns(invoices); var transactionRepository = Substitute.For(); var logger = Substitute.For>(); var paymentHistoryService = new PaymentHistoryService(stripeAdapter, transactionRepository, logger); // Act var result = await paymentHistoryService.GetInvoiceHistoryAsync(subscriber); // Assert Assert.NotNull(result); Assert.Single(result); await stripeAdapter.Received(1).InvoiceListAsync(Arg.Any()); } [Fact] public async Task GetInvoiceHistoryAsync_SubscriberNull_ReturnsNull() { // Arrange var paymentHistoryService = new PaymentHistoryService( Substitute.For(), Substitute.For(), Substitute.For>()); // Act var result = await paymentHistoryService.GetInvoiceHistoryAsync(null); // Assert Assert.Null(result); } [Fact] public async Task GetTransactionHistoryAsync_Succeeds() { // Arrange var subscriber = new Organization { Id = Guid.NewGuid() }; var transactions = new List { new() { Id = Guid.NewGuid() } }; var transactionRepository = Substitute.For(); transactionRepository.GetManyByOrganizationIdAsync(subscriber.Id, Arg.Any(), Arg.Any()).Returns(transactions); var stripeAdapter = Substitute.For(); var logger = Substitute.For>(); var paymentHistoryService = new PaymentHistoryService(stripeAdapter, transactionRepository, logger); // Act var result = await paymentHistoryService.GetTransactionHistoryAsync(subscriber); // Assert Assert.NotNull(result); Assert.Single(result); await transactionRepository.Received(1).GetManyByOrganizationIdAsync(subscriber.Id, Arg.Any(), Arg.Any()); } [Fact] public async Task GetTransactionHistoryAsync_SubscriberNull_ReturnsNull() { // Arrange var paymentHistoryService = new PaymentHistoryService( Substitute.For(), Substitute.For(), Substitute.For>()); // Act var result = await paymentHistoryService.GetTransactionHistoryAsync(null); // Assert Assert.Null(result); } }