From d606bb506f4f5f46d29bd034b40640c9b1cc0798 Mon Sep 17 00:00:00 2001 From: Ekrem05 Date: Sun, 2 Jun 2024 11:54:29 +0300 Subject: [PATCH] Stripe service tests added --- .../ServiceTests/Stripe/StripeTests.cs | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 test/CookingApp.UnitTests/ServiceTests/Stripe/StripeTests.cs diff --git a/test/CookingApp.UnitTests/ServiceTests/Stripe/StripeTests.cs b/test/CookingApp.UnitTests/ServiceTests/Stripe/StripeTests.cs new file mode 100644 index 00000000..798c2f2c --- /dev/null +++ b/test/CookingApp.UnitTests/ServiceTests/Stripe/StripeTests.cs @@ -0,0 +1,98 @@ +using CookingApp.Services.Stripe; +using NUnit.Framework; +using Moq; +using Stripe; +using Assert = NUnit.Framework.Assert; +using CookingApp.ViewModels.Stripe.Subscription; +namespace CookingApp.UnitTests.Services.Stripe +{ + internal class StripeTests + { + private StripeService stripeService; + + [OneTimeSetUp] + public void Setup() + { + var customerServiceMoq = new Mock(); + customerServiceMoq + .Setup(x => x.CreateAsync(It.IsAny(), null, It.IsAny())) + .ReturnsAsync(new Customer { Id = "1", Email = "test" }); + var productService = new Mock(); + productService + .Setup(x => x.ListAsync(It.IsAny(), null, It.IsAny())) + .ReturnsAsync(new StripeList() + { + Data = new List() { + new Product() { + Id = "2", Name = "test",DefaultPriceId="test",Description="description",Images=new List{"test"} + } + } + }) ; + var subscriptionService = new Mock(); + subscriptionService.Setup(x => x.CreateAsync(It.IsAny(), null,It.IsAny())) + .ReturnsAsync(new Subscription() + { Id = "sUB123", + LatestInvoice=new Invoice() + { + Id="invoiceId", + PaymentIntent=new PaymentIntent() + { + ClientSecret="secret" + },HostedInvoiceUrl="url" + }, + LatestInvoiceId="invoiceId" }); + subscriptionService.Setup(x=>x.CancelAsync(It.IsAny(),null,null,It.IsAny())) + .ReturnsAsync(new Subscription() { Id="test",CanceledAt=DateTime.Today.AddDays(1) }); + var priceService = new Mock(); + priceService.Setup(x => x.GetAsync(It.IsAny(), null, null, It.IsAny())) + .ReturnsAsync(new Price() { UnitAmount = 1 }); + var options = new CustomerCreateOptions { Email = "AFG" }; + + + stripeService = new StripeService(customerServiceMoq.Object, priceService.Object, productService.Object, subscriptionService.Object); + } + + [Test] + public async Task CreateCustomerAsync_ShouldWork() + { + var result = await stripeService.CreateCustomerAsync("test"); + Assert.That(result, Is.Not.Null); + Assert.That(result.Email,Is.EqualTo("test")); + + } + [Test] + public async Task GetProductsAsync_ShouldWork() + { + var result = await stripeService.GetProductsAsync(); + Assert.That(result, Is.Not.Null); + Assert.That(result.First().Id, Is.EqualTo("2")); + Assert.That(result.First().PriceId, Is.EqualTo("test")); + Assert.That(result.First().Price, Is.EqualTo(1)); + Assert.That(result.First().Name,Is.EqualTo("test")); + Assert.That(result.First().Description, Is.EqualTo("description")); + + + } + [Test] + public async Task CreateSubscriptionAsync_ShouldWork() + { + var result = await stripeService.CreateSubscriptionAsync(new SubscriptionCreation() { CustomerId = "321",PriceId="123" }); + Assert.That(result.InvoiceId, Is.EqualTo("invoiceId")); + Assert.That(result.ClientSecret, Is.EqualTo("secret")); + Assert.That(result.SubscriptionId, Is.EqualTo("sUB123")); + Assert.That(result.InvoiceUrl, Is.EqualTo("url")); + + } + [Test] + public void CreateSubscriptionAsync_ThrowsExceptions() + { + Assert.ThrowsAsync(async () => await stripeService.CreateSubscriptionAsync(new SubscriptionCreation())); + } + [Test] + public async Task CancelSubscriptionAsync_ShouldWork() + { + var result = await stripeService.CancelSubscriptionAsync(new SubscriptionCancellation("testId")); + Assert.That(result.CanceledAt, Is.EqualTo(DateTime.Today.AddDays(1))); + } + } +}