From 4578429478a6851610e59f1cfc93810a422073a1 Mon Sep 17 00:00:00 2001 From: Piotr Pabis Date: Wed, 3 Jan 2024 13:05:52 +0100 Subject: [PATCH 1/5] chore: work in progress commit started advanced scenario --- Tests/SubscriptionTests.cs | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Tests/SubscriptionTests.cs b/Tests/SubscriptionTests.cs index f4b103f..9320439 100644 --- a/Tests/SubscriptionTests.cs +++ b/Tests/SubscriptionTests.cs @@ -65,6 +65,46 @@ await _client.SubscriptionsController.PurgeSubscriptionAsync((int)subscriptionRe } } + [Fact] + public async Task CreateSubscription_RestrictedCouponMeteredComponentData_ShouldHaveAwaitingSignupStatus() + { + var productFamilyId = await CreateOrGetProductFamily(); + + var restrictedProduct = await CreateProduct(productFamilyId); + + var randomChar = _fixture.Create(); + + var quantityComponent = new QuantityBasedComponent($"widget{randomChar}", $"widget {randomChar}", + PricingScheme.PerUnit, unitPrice: QuantityBasedComponentUnitPrice.FromPrecision(1)); + + var restrictedComponentResponse = await _client.ComponentsController.CreateComponentAsync((int)productFamilyId, + ComponentKindPath.QuantityBasedComponents, + CreateComponentBody.FromCreateQuantityBasedComponent(new CreateQuantityBasedComponent(quantityComponent))); + + restrictedComponentResponse.Component.Id.Should().NotBeNull(); + + var meteredComponent = new MeteredComponent($"ApiCalls{randomChar}", $"api call {randomChar}", + PricingScheme.PerUnit, unitPrice: MeteredComponentUnitPrice.FromString("1")); + + var componentResponse = await _client.ComponentsController.CreateComponentAsync((int)productFamilyId, + ComponentKindPath.MeteredComponents, + CreateComponentBody.FromCreateMeteredComponent(new CreateMeteredComponent(meteredComponent))); + + componentResponse.Component.Id.Should().NotBeNull(); + + var createOrUpdatePercentageCoupon = new CreateOrUpdatePercentageCoupon("100% off first month of usage", + "100OFF", CreateOrUpdatePercentageCouponPercentage.FromPrecision(100), "100% off one-time", "false", + "false", "2024-08-29T12:00:00-04:00", productFamilyId.ToString(), "false", excludeMidPeriodAllocations: true, applyOnCancelAtEndOfPeriod: true); + + var restrictedProductDictionary = new Dictionary { { restrictedProduct.Product.Id.ToString()!, false } }; + var restrictedComponentsDictionary = new Dictionary { { restrictedComponentResponse.Component.Id.ToString()!, false }, { componentResponse.Component.Id.ToString()!, true} }; + + var test = await _client.CouponsController.CreateCouponAsync((int)productFamilyId, + new CreateOrUpdateCoupon( + CreateOrUpdateCouponCoupon.FromCreateOrUpdatePercentageCoupon(createOrUpdatePercentageCoupon), + restrictedProductDictionary, restrictedComponentsDictionary)); + } + [Fact] public async Task CreateSubscription_WithMinimalData_ShouldFailWithAnyErrorMessage() { From 280760c266355daef86e759da8f2124deb1f48e3 Mon Sep 17 00:00:00 2001 From: Piotr Pabis Date: Thu, 4 Jan 2024 13:26:29 +0100 Subject: [PATCH 2/5] chore: dirty version of completed test 1 --- Tests/SubscriptionTests.cs | 102 ++++++++++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 14 deletions(-) diff --git a/Tests/SubscriptionTests.cs b/Tests/SubscriptionTests.cs index 9320439..0a24b82 100644 --- a/Tests/SubscriptionTests.cs +++ b/Tests/SubscriptionTests.cs @@ -70,20 +70,22 @@ public async Task CreateSubscription_RestrictedCouponMeteredComponentData_Should { var productFamilyId = await CreateOrGetProductFamily(); - var restrictedProduct = await CreateProduct(productFamilyId); + var product = await CreateProduct(productFamilyId); - var randomChar = _fixture.Create(); + var randomString = GenerateRandomString(4); - var quantityComponent = new QuantityBasedComponent($"widget{randomChar}", $"widget {randomChar}", + var quantityComponent = new QuantityBasedComponent($"widget{randomString}", $"widget {randomString}", PricingScheme.PerUnit, unitPrice: QuantityBasedComponentUnitPrice.FromPrecision(1)); - var restrictedComponentResponse = await _client.ComponentsController.CreateComponentAsync((int)productFamilyId, + var restrictedComponentResponse = await _client.ComponentsController.CreateComponentAsync( + (int)productFamilyId, ComponentKindPath.QuantityBasedComponents, - CreateComponentBody.FromCreateQuantityBasedComponent(new CreateQuantityBasedComponent(quantityComponent))); + CreateComponentBody.FromCreateQuantityBasedComponent( + new CreateQuantityBasedComponent(quantityComponent))); restrictedComponentResponse.Component.Id.Should().NotBeNull(); - var meteredComponent = new MeteredComponent($"ApiCalls{randomChar}", $"api call {randomChar}", + var meteredComponent = new MeteredComponent($"ApiCalls{randomString}", $"api call {randomString}", PricingScheme.PerUnit, unitPrice: MeteredComponentUnitPrice.FromString("1")); var componentResponse = await _client.ComponentsController.CreateComponentAsync((int)productFamilyId, @@ -92,17 +94,82 @@ public async Task CreateSubscription_RestrictedCouponMeteredComponentData_Should componentResponse.Component.Id.Should().NotBeNull(); + var couponCode = $"100{randomString}OFF"; + var createOrUpdatePercentageCoupon = new CreateOrUpdatePercentageCoupon("100% off first month of usage", - "100OFF", CreateOrUpdatePercentageCouponPercentage.FromPrecision(100), "100% off one-time", "false", - "false", "2024-08-29T12:00:00-04:00", productFamilyId.ToString(), "false", excludeMidPeriodAllocations: true, applyOnCancelAtEndOfPeriod: true); + couponCode, CreateOrUpdatePercentageCouponPercentage.FromPrecision(100), "100% off one-time", "false", + "false", "2024-08-29T12:00:00-04:00", productFamilyId.ToString(), "false", + excludeMidPeriodAllocations: true, applyOnCancelAtEndOfPeriod: true); + + var restrictedProductDictionary = new Dictionary { { product.Product.Id.ToString()!, true } }; + var restrictedComponentsDictionary = new Dictionary + { + { restrictedComponentResponse.Component.Id.ToString()!, false }, + { componentResponse.Component.Id.ToString()!, true } + }; + + try + { + var couponResponse = await _client.CouponsController.CreateCouponAsync((int)productFamilyId, + new CreateOrUpdateCoupon( + CreateOrUpdateCouponCoupon.FromCreateOrUpdatePercentageCoupon(createOrUpdatePercentageCoupon), + restrictedProductDictionary, restrictedComponentsDictionary)); + couponResponse.Coupon.Id.Should().NotBeNull(); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + + var customer = await CreateCustomer(); + + var paymentProfile = await CreatePaymentProfile(customer.Customer.Id); + + var initialBillingDate = DateTime.Now.AddDays(20); + + var createdSubscription = new CreateSubscription + { + CustomerId = customer.Customer.Id, + ProductId = product.Product.Id, + PaymentCollectionMethod = PaymentCollectionMethod.Automatic, + PaymentProfileId = paymentProfile.PaymentProfile.Id, + DunningCommunicationDelayEnabled = false, + SkipBillingManifestTaxes = false, + Components = new List() + { + new CreateSubscriptionComponent( + CreateSubscriptionComponentComponentId.FromNumber((int)componentResponse.Component.Id), quantity: 10) + }, + CouponCode = couponCode, + InitialBillingAt = initialBillingDate.ToString("yyyy-MM-dd") + }; + var subscriptionResponse = + await _client.SubscriptionsController.CreateSubscriptionAsync( + new CreateSubscriptionRequest(createdSubscription)); + + subscriptionResponse.Subscription.Id.Should().NotBeNull(); + subscriptionResponse.Subscription.State.Should().Be(SubscriptionState.AwaitingSignup); + + try + { + await _client.SubscriptionsController.PurgeSubscriptionAsync((int)subscriptionResponse.Subscription.Id, + (int)customer.Customer.Id); - var restrictedProductDictionary = new Dictionary { { restrictedProduct.Product.Id.ToString()!, false } }; - var restrictedComponentsDictionary = new Dictionary { { restrictedComponentResponse.Component.Id.ToString()!, false }, { componentResponse.Component.Id.ToString()!, true} }; + await _client.PaymentProfilesController.DeleteUnusedPaymentProfileAsync(paymentProfile.ToString()); - var test = await _client.CouponsController.CreateCouponAsync((int)productFamilyId, - new CreateOrUpdateCoupon( - CreateOrUpdateCouponCoupon.FromCreateOrUpdatePercentageCoupon(createOrUpdatePercentageCoupon), - restrictedProductDictionary, restrictedComponentsDictionary)); + await _client.CustomersController.DeleteCustomerAsync((int)customer.Customer.Id); + // await _client.CouponsController.ArchiveCouponAsync((int)productFamilyId, (int)couponResponse.Coupon.Id); + await _client.ComponentsController.ArchiveComponentAsync((int)productFamilyId, + componentResponse.Component.Id.ToString()); + await _client.ComponentsController.ArchiveComponentAsync((int)productFamilyId, + restrictedComponentResponse.Component.Id.ToString()); + await _client.ProductsController.ArchiveProductAsync((int)product.Product.Id); + } + catch (ApiException e) + { + // Suppress Errors on Cleanup + } } [Fact] @@ -243,5 +310,12 @@ await _client.ProductsController.CreateProductAsync((int)productFamilyId, return productFamilyId; } + static string GenerateRandomString(int length) + { + const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+"; + var random = new Random(); + return new string(Enumerable.Repeat(chars, length) + .Select(s => s[random.Next(s.Length)]).ToArray()); + } } } From 2e1a963d7814ded4780821e1c4a0914b700fd929 Mon Sep 17 00:00:00 2001 From: Piotr Pabis Date: Mon, 8 Jan 2024 14:54:15 +0100 Subject: [PATCH 3/5] chore: dirty update of tests --- Tests/SubscriptionTests.cs | 149 ++++++++++++++++++++++++++++++++----- 1 file changed, 132 insertions(+), 17 deletions(-) diff --git a/Tests/SubscriptionTests.cs b/Tests/SubscriptionTests.cs index 0a24b82..1a2fbe8 100644 --- a/Tests/SubscriptionTests.cs +++ b/Tests/SubscriptionTests.cs @@ -4,6 +4,7 @@ using AdvancedBilling.Standard.Models.Containers; using AutoFixture; using FluentAssertions; +using Environment = AdvancedBilling.Standard.Environment; namespace AdvancedBillingTests { @@ -65,6 +66,127 @@ await _client.SubscriptionsController.PurgeSubscriptionAsync((int)subscriptionRe } } + [Fact] + public async Task + CreateSubscription_WrongCouponCodeProvidedToSubscription_ShouldHaveUnprocessableStatusWithMeaningfulError() + { + var productFamilyId = await CreateOrGetProductFamily(); + + var product = await CreateProduct(productFamilyId); + + var randomString = GenerateRandomString(8); + + var meteredComponent = new MeteredComponent($"ApiCalls{randomString}", $"api call {randomString}", + PricingScheme.PerUnit, unitPrice: MeteredComponentUnitPrice.FromString("1")); + + var componentResponse = await _client.ComponentsController.CreateComponentAsync((int)productFamilyId, + ComponentKindPath.MeteredComponents, + CreateComponentBody.FromCreateMeteredComponent(new CreateMeteredComponent(meteredComponent))); + + componentResponse.Component.Id.Should().NotBeNull(); + + var couponCode = $"100{randomString}OFF"; + + var createOrUpdatePercentageCoupon = new CreateOrUpdatePercentageCoupon("100% off first month of usage", + couponCode, CreateOrUpdatePercentageCouponPercentage.FromPrecision(100), "100% off one-time", "false", + "false", "2024-08-29T12:00:00-04:00", productFamilyId.ToString(), "false", + excludeMidPeriodAllocations: true, applyOnCancelAtEndOfPeriod: true); + + var restrictedProductDictionary = new Dictionary { { product.Product.Id.ToString()!, true } }; + var restrictedComponentsDictionary = new Dictionary + { + { componentResponse.Component.Id.ToString()!, true } + }; + + var couponResponse = await _client.CouponsController.CreateCouponAsync((int)productFamilyId, + new CreateOrUpdateCoupon( + CreateOrUpdateCouponCoupon.FromCreateOrUpdatePercentageCoupon(createOrUpdatePercentageCoupon), + restrictedProductDictionary, restrictedComponentsDictionary)); + couponResponse.Coupon.Id.Should().NotBeNull(); + + var customer = await CreateCustomer(); + + var paymentProfile = await CreatePaymentProfile(customer.Customer.Id); + + var initialBillingDate = DateTime.Now.AddDays(20); + + var wrongCouponCode = $"WrongCode{randomString}"; + + var createdSubscription = new CreateSubscription + { + CustomerId = customer.Customer.Id, + ProductId = product.Product.Id, + PaymentCollectionMethod = PaymentCollectionMethod.Automatic, + PaymentProfileId = paymentProfile.PaymentProfile.Id, + DunningCommunicationDelayEnabled = false, + SkipBillingManifestTaxes = false, + Components = new List() + { + new CreateSubscriptionComponent( + CreateSubscriptionComponentComponentId.FromNumber((int)componentResponse.Component.Id), + quantity: 10) + }, + CouponCode = wrongCouponCode, + InitialBillingAt = initialBillingDate.ToString("yyyy-MM-dd") + }; + + await _client.Invoking(c => c.SubscriptionsController.CreateSubscriptionAsync( + new CreateSubscriptionRequest(createdSubscription))).Should() + .ThrowAsync() + .Where(e => e.ResponseCode == 422 && e.Errors.Any(a => a.Contains("Coupon code could not be found"))); + + try + { + await _client.PaymentProfilesController.DeleteUnusedPaymentProfileAsync(paymentProfile.ToString()); + + await _client.CustomersController.DeleteCustomerAsync((int)customer.Customer.Id); + await _client.CouponsController.ArchiveCouponAsync((int)productFamilyId, (int)couponResponse.Coupon.Id); + await _client.ComponentsController.ArchiveComponentAsync((int)productFamilyId, + componentResponse.Component.Id.ToString()); + + await _client.ProductsController.ArchiveProductAsync((int)product.Product.Id); + } + catch (ApiException e) + { + // Suppress Errors on Cleanup + } + } + + [Fact] + public async Task + CreateSubscription_UnauthorizedAccessCreateSubscription_ShouldHaveReturns401Unauthorized() + { + var builder = new AdvancedBillingClient.Builder(); + builder.Environment(Environment.Production); + builder.Domain("staging-chargify.com"); + builder.Subdomain("dotnet-sdk"); + builder.BasicAuthCredentials("abc", "123"); + var invalidClient = builder.Build(); + + var createdSubscription = new CreateSubscription + { + CustomerId = _fixture.Create(), + ProductId = _fixture.Create(), + PaymentCollectionMethod = PaymentCollectionMethod.Automatic, + PaymentProfileId = _fixture.Create(), + DunningCommunicationDelayEnabled = false, + SkipBillingManifestTaxes = false, + Components = new List() + { + new CreateSubscriptionComponent( + CreateSubscriptionComponentComponentId.FromNumber(_fixture.Create()), + quantity: 10) + }, + CouponCode = _fixture.Create(), + InitialBillingAt = _fixture.Create().ToString("yyyy-MM-dd") + }; + + await invalidClient.Invoking(c => c.SubscriptionsController.CreateSubscriptionAsync( + new CreateSubscriptionRequest(createdSubscription))).Should() + .ThrowAsync() + .Where(e => e.ResponseCode == 401); + } + [Fact] public async Task CreateSubscription_RestrictedCouponMeteredComponentData_ShouldHaveAwaitingSignupStatus() { @@ -108,20 +230,12 @@ public async Task CreateSubscription_RestrictedCouponMeteredComponentData_Should { componentResponse.Component.Id.ToString()!, true } }; - try - { - var couponResponse = await _client.CouponsController.CreateCouponAsync((int)productFamilyId, - new CreateOrUpdateCoupon( - CreateOrUpdateCouponCoupon.FromCreateOrUpdatePercentageCoupon(createOrUpdatePercentageCoupon), - restrictedProductDictionary, restrictedComponentsDictionary)); - couponResponse.Coupon.Id.Should().NotBeNull(); - } - catch (Exception e) - { - Console.WriteLine(e); - throw; - } - + var couponResponse = await _client.CouponsController.CreateCouponAsync((int)productFamilyId, + new CreateOrUpdateCoupon( + CreateOrUpdateCouponCoupon.FromCreateOrUpdatePercentageCoupon(createOrUpdatePercentageCoupon), + restrictedProductDictionary, restrictedComponentsDictionary)); + couponResponse.Coupon.Id.Should().NotBeNull(); + var customer = await CreateCustomer(); var paymentProfile = await CreatePaymentProfile(customer.Customer.Id); @@ -139,7 +253,8 @@ public async Task CreateSubscription_RestrictedCouponMeteredComponentData_Should Components = new List() { new CreateSubscriptionComponent( - CreateSubscriptionComponentComponentId.FromNumber((int)componentResponse.Component.Id), quantity: 10) + CreateSubscriptionComponentComponentId.FromNumber((int)componentResponse.Component.Id), + quantity: 10) }, CouponCode = couponCode, InitialBillingAt = initialBillingDate.ToString("yyyy-MM-dd") @@ -159,7 +274,7 @@ await _client.SubscriptionsController.PurgeSubscriptionAsync((int)subscriptionRe await _client.PaymentProfilesController.DeleteUnusedPaymentProfileAsync(paymentProfile.ToString()); await _client.CustomersController.DeleteCustomerAsync((int)customer.Customer.Id); - // await _client.CouponsController.ArchiveCouponAsync((int)productFamilyId, (int)couponResponse.Coupon.Id); + // await _client.CouponsController.ArchiveCouponAsync((int)productFamilyId, (int)couponResponse.Coupon.Id); await _client.ComponentsController.ArchiveComponentAsync((int)productFamilyId, componentResponse.Component.Id.ToString()); await _client.ComponentsController.ArchiveComponentAsync((int)productFamilyId, @@ -312,7 +427,7 @@ await _client.ProductsController.CreateProductAsync((int)productFamilyId, static string GenerateRandomString(int length) { - const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+"; + const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var random = new Random(); return new string(Enumerable.Repeat(chars, length) .Select(s => s[random.Next(s.Length)]).ToArray()); From 5227024e72d56b5510370f64f1e7d6e5dbdbdcae Mon Sep 17 00:00:00 2001 From: Piotr Pabis Date: Tue, 9 Jan 2024 14:20:04 +0100 Subject: [PATCH 4/5] chore: minor improvement --- Tests/InvalidClient.cs | 19 +++++++++++++++++++ Tests/SiteTests.cs | 9 +-------- Tests/SubscriptionTests.cs | 8 +------- 3 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 Tests/InvalidClient.cs diff --git a/Tests/InvalidClient.cs b/Tests/InvalidClient.cs new file mode 100644 index 0000000..2022604 --- /dev/null +++ b/Tests/InvalidClient.cs @@ -0,0 +1,19 @@ +using AdvancedBilling.Standard; +using Environment = AdvancedBilling.Standard.Environment; + +namespace AdvancedBillingTests +{ + internal static class InvalidClient + { + public static AdvancedBillingClient GetInvalidClient() + { + var builder = new AdvancedBillingClient.Builder(); + builder.Environment(Environment.Production); + builder.Domain("staging-chargify.com"); + builder.Subdomain("dotnet-sdk"); + builder.BasicAuthCredentials("abc", "123"); + + return builder.Build(); + } + } +} diff --git a/Tests/SiteTests.cs b/Tests/SiteTests.cs index a4f742b..8179831 100644 --- a/Tests/SiteTests.cs +++ b/Tests/SiteTests.cs @@ -1,7 +1,5 @@ -using AdvancedBilling.Standard; using AdvancedBilling.Standard.Exceptions; using FluentAssertions; -using Environment = AdvancedBilling.Standard.Environment; namespace AdvancedBillingTests { @@ -26,12 +24,7 @@ public async Task ValidSiteReading_SuccessfulRequest_ReturnsSiteObject() [Fact] public async Task UnauthorizedSiteReading_InvalidCredentials_Returns401Unauthorized() { - var builder = new AdvancedBillingClient.Builder(); - builder.Environment(Environment.Production); - builder.Domain("staging-chargify.com"); - builder.Subdomain("dotnet-sdk"); - builder.BasicAuthCredentials("abc", "123"); - var invalidClient = builder.Build(); + var invalidClient = InvalidClient.GetInvalidClient(); await invalidClient.Invoking(i => i.SitesController.ReadSiteAsync()).Should().ThrowAsync().Where(e => e.ResponseCode == 401); } diff --git a/Tests/SubscriptionTests.cs b/Tests/SubscriptionTests.cs index 1a2fbe8..a9f5db4 100644 --- a/Tests/SubscriptionTests.cs +++ b/Tests/SubscriptionTests.cs @@ -4,7 +4,6 @@ using AdvancedBilling.Standard.Models.Containers; using AutoFixture; using FluentAssertions; -using Environment = AdvancedBilling.Standard.Environment; namespace AdvancedBillingTests { @@ -156,12 +155,7 @@ await _client.ComponentsController.ArchiveComponentAsync((int)productFamilyId, public async Task CreateSubscription_UnauthorizedAccessCreateSubscription_ShouldHaveReturns401Unauthorized() { - var builder = new AdvancedBillingClient.Builder(); - builder.Environment(Environment.Production); - builder.Domain("staging-chargify.com"); - builder.Subdomain("dotnet-sdk"); - builder.BasicAuthCredentials("abc", "123"); - var invalidClient = builder.Build(); + var invalidClient = InvalidClient.GetInvalidClient(); var createdSubscription = new CreateSubscription { From e6de5b21cca9652342d2bf2b96173c84dff91572 Mon Sep 17 00:00:00 2001 From: Piotr Pabis Date: Wed, 10 Jan 2024 12:05:13 +0100 Subject: [PATCH 5/5] chore: refactoring with cleanup part --- Tests/ErrorSuppressionWrapper.cs | 19 ++++++++ Tests/SubscriptionTests.cs | 84 +++++++++++++++----------------- 2 files changed, 59 insertions(+), 44 deletions(-) create mode 100644 Tests/ErrorSuppressionWrapper.cs diff --git a/Tests/ErrorSuppressionWrapper.cs b/Tests/ErrorSuppressionWrapper.cs new file mode 100644 index 0000000..cec7e5b --- /dev/null +++ b/Tests/ErrorSuppressionWrapper.cs @@ -0,0 +1,19 @@ +using AdvancedBilling.Standard.Exceptions; + +namespace AdvancedBillingTests +{ + public static class ErrorSuppressionWrapper + { + public static async Task RunAsync(Func action) + { + try + { + await action.Invoke(); + } + catch (ApiException e) + { + // Suppress Errors on Cleanup + } + } + } +} diff --git a/Tests/SubscriptionTests.cs b/Tests/SubscriptionTests.cs index a9f5db4..c98405f 100644 --- a/Tests/SubscriptionTests.cs +++ b/Tests/SubscriptionTests.cs @@ -49,20 +49,7 @@ await _client.SubscriptionsController.CreateSubscriptionAsync( new CreateSubscriptionRequest(subscription)); subscriptionResponse.Subscription.Id.Should().NotBeNull(); - try - { - await _client.SubscriptionsController.PurgeSubscriptionAsync((int)subscriptionResponse.Subscription.Id, - (int)customerResponse.Customer.Id); - - await _client.PaymentProfilesController.DeleteUnusedPaymentProfileAsync(paymentProfile.ToString()); - - await _client.CustomersController.DeleteCustomerAsync((int)customerResponse.Customer.Id); - await _client.ProductsController.ArchiveProductAsync((int)productResponse.Product.Id); - } - catch (ApiException e) - { - // Suppress Errors on Cleanup - } + await ExecuteBasicSubscriptionCleanup(subscriptionResponse, customerResponse, paymentProfile, productResponse); } [Fact] @@ -134,21 +121,18 @@ await _client.Invoking(c => c.SubscriptionsController.CreateSubscriptionAsync( .ThrowAsync() .Where(e => e.ResponseCode == 422 && e.Errors.Any(a => a.Contains("Coupon code could not be found"))); - try - { - await _client.PaymentProfilesController.DeleteUnusedPaymentProfileAsync(paymentProfile.ToString()); + await ExecuteCleanupForPaymentProfileProductCustomer(customer, paymentProfile, product); - await _client.CustomersController.DeleteCustomerAsync((int)customer.Customer.Id); - await _client.CouponsController.ArchiveCouponAsync((int)productFamilyId, (int)couponResponse.Coupon.Id); + await ErrorSuppressionWrapper.RunAsync(async () => + { await _client.ComponentsController.ArchiveComponentAsync((int)productFamilyId, componentResponse.Component.Id.ToString()); + }); - await _client.ProductsController.ArchiveProductAsync((int)product.Product.Id); - } - catch (ApiException e) + await ErrorSuppressionWrapper.RunAsync(async () => { - // Suppress Errors on Cleanup - } + await _client.CouponsController.ArchiveCouponAsync((int)productFamilyId, (int)couponResponse.Coupon.Id); + }); } [Fact] @@ -260,25 +244,19 @@ await _client.SubscriptionsController.CreateSubscriptionAsync( subscriptionResponse.Subscription.Id.Should().NotBeNull(); subscriptionResponse.Subscription.State.Should().Be(SubscriptionState.AwaitingSignup); - try - { - await _client.SubscriptionsController.PurgeSubscriptionAsync((int)subscriptionResponse.Subscription.Id, - (int)customer.Customer.Id); + await ExecuteBasicSubscriptionCleanup(subscriptionResponse, customer, paymentProfile, product); - await _client.PaymentProfilesController.DeleteUnusedPaymentProfileAsync(paymentProfile.ToString()); - - await _client.CustomersController.DeleteCustomerAsync((int)customer.Customer.Id); - // await _client.CouponsController.ArchiveCouponAsync((int)productFamilyId, (int)couponResponse.Coupon.Id); + await ErrorSuppressionWrapper.RunAsync(async () => + { await _client.ComponentsController.ArchiveComponentAsync((int)productFamilyId, componentResponse.Component.Id.ToString()); + }); + + await ErrorSuppressionWrapper.RunAsync(async () => + { await _client.ComponentsController.ArchiveComponentAsync((int)productFamilyId, restrictedComponentResponse.Component.Id.ToString()); - await _client.ProductsController.ArchiveProductAsync((int)product.Product.Id); - } - catch (ApiException e) - { - // Suppress Errors on Cleanup - } + }); } [Fact] @@ -325,20 +303,38 @@ await _client.SubscriptionsController.CreateSubscriptionAsync( subscription.Subscription.Customer.Id.Should().Be(customerResponse.Customer.Id); subscription.Subscription.PaymentCollectionMethod.Should().Be(PaymentCollectionMethod.Automatic); - try + await ExecuteBasicSubscriptionCleanup(subscriptionResponse, customerResponse, paymentProfile, productResponse); + } + + private async Task ExecuteBasicSubscriptionCleanup(SubscriptionResponse subscriptionResponse, + CustomerResponse customerResponse, CreatePaymentProfileResponse paymentProfile, ProductResponse productResponse) + { + await ErrorSuppressionWrapper.RunAsync(async () => { await _client.SubscriptionsController.PurgeSubscriptionAsync((int)subscriptionResponse.Subscription.Id, (int)customerResponse.Customer.Id); + }); + await ExecuteCleanupForPaymentProfileProductCustomer(customerResponse, paymentProfile, productResponse); + } + + private async Task ExecuteCleanupForPaymentProfileProductCustomer(CustomerResponse customerResponse, + CreatePaymentProfileResponse paymentProfile, ProductResponse productResponse) + { + await ErrorSuppressionWrapper.RunAsync(async () => + { await _client.PaymentProfilesController.DeleteUnusedPaymentProfileAsync(paymentProfile.ToString()); + }); + await ErrorSuppressionWrapper.RunAsync(async () => + { await _client.CustomersController.DeleteCustomerAsync((int)customerResponse.Customer.Id); - await _client.ProductsController.ArchiveProductAsync((int)productResponse.Product.Id); - } - catch (ApiException e) + }); + + await ErrorSuppressionWrapper.RunAsync(async () => { - // Suppress Errors on Cleanup - } + await _client.ProductsController.ArchiveProductAsync((int)productResponse.Product.Id); + }); } private async Task CreatePaymentProfile(int? customerId)