using Bit.Core.Billing.Constants; using Bit.Core.Billing.Tax.Services.Implementations; using Bit.Core.Services; using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; using NSubstitute; using Stripe; using Xunit; namespace Bit.Core.Test.Billing.Tax.Services; [SutProviderCustomize] public class PersonalUseAutomaticTaxStrategyTests { [Theory] [BitAutoData] public void GetUpdateOptions_ReturnsNull_WhenFeatureFlagAllowingToUpdateSubscriptionsIsDisabled( SutProvider sutProvider) { var subscription = new Subscription(); sutProvider.GetDependency() .IsEnabled(Arg.Is(p => p == FeatureFlagKeys.PM19422_AllowAutomaticTaxUpdates)) .Returns(false); var actual = sutProvider.Sut.GetUpdateOptions(subscription); Assert.Null(actual); } [Theory] [BitAutoData] public void GetUpdateOptions_ReturnsNull_WhenSubscriptionDoesNotNeedUpdating( SutProvider sutProvider) { var subscription = new Subscription { AutomaticTax = new SubscriptionAutomaticTax { Enabled = true }, Customer = new Customer { Address = new Address { Country = "US", }, Tax = new CustomerTax { AutomaticTax = StripeConstants.AutomaticTaxStatus.Supported } } }; sutProvider.GetDependency() .IsEnabled(Arg.Is(p => p == FeatureFlagKeys.PM19422_AllowAutomaticTaxUpdates)) .Returns(true); var actual = sutProvider.Sut.GetUpdateOptions(subscription); Assert.Null(actual); } [Theory] [BitAutoData] public void GetUpdateOptions_SetsAutomaticTaxToFalse_WhenTaxLocationIsUnrecognizedOrInvalid( SutProvider sutProvider) { var subscription = new Subscription { AutomaticTax = new SubscriptionAutomaticTax { Enabled = true }, Customer = new Customer { Tax = new CustomerTax { AutomaticTax = StripeConstants.AutomaticTaxStatus.UnrecognizedLocation } } }; sutProvider.GetDependency() .IsEnabled(Arg.Is(p => p == FeatureFlagKeys.PM19422_AllowAutomaticTaxUpdates)) .Returns(true); var actual = sutProvider.Sut.GetUpdateOptions(subscription); Assert.NotNull(actual); Assert.False(actual.AutomaticTax.Enabled); } [Theory] [BitAutoData("CA")] [BitAutoData("ES")] [BitAutoData("US")] public void GetUpdateOptions_SetsAutomaticTaxToTrue_ForAllCountries( string country, SutProvider sutProvider) { var subscription = new Subscription { AutomaticTax = new SubscriptionAutomaticTax { Enabled = false }, Customer = new Customer { Address = new Address { Country = country }, Tax = new CustomerTax { AutomaticTax = StripeConstants.AutomaticTaxStatus.Supported } } }; sutProvider.GetDependency() .IsEnabled(Arg.Is(p => p == FeatureFlagKeys.PM19422_AllowAutomaticTaxUpdates)) .Returns(true); var actual = sutProvider.Sut.GetUpdateOptions(subscription); Assert.NotNull(actual); Assert.True(actual.AutomaticTax.Enabled); } [Theory] [BitAutoData("CA")] [BitAutoData("ES")] [BitAutoData("US")] public void GetUpdateOptions_SetsAutomaticTaxToTrue_ForGlobalCustomersWithTaxIds( string country, SutProvider sutProvider) { var subscription = new Subscription { AutomaticTax = new SubscriptionAutomaticTax { Enabled = false }, Customer = new Customer { Address = new Address { Country = country, }, Tax = new CustomerTax { AutomaticTax = StripeConstants.AutomaticTaxStatus.Supported }, TaxIds = new StripeList { Data = new List { new() { Country = "ES", Type = "eu_vat", Value = "ESZ8880999Z" } } } } }; sutProvider.GetDependency() .IsEnabled(Arg.Is(p => p == FeatureFlagKeys.PM19422_AllowAutomaticTaxUpdates)) .Returns(true); var actual = sutProvider.Sut.GetUpdateOptions(subscription); Assert.NotNull(actual); Assert.True(actual.AutomaticTax.Enabled); } [Theory] [BitAutoData("CA")] [BitAutoData("ES")] [BitAutoData("US")] public void GetUpdateOptions_SetsAutomaticTaxToTrue_ForGlobalCustomersWithoutTaxIds( string country, SutProvider sutProvider) { var subscription = new Subscription { AutomaticTax = new SubscriptionAutomaticTax { Enabled = false }, Customer = new Customer { Address = new Address { Country = country }, Tax = new CustomerTax { AutomaticTax = StripeConstants.AutomaticTaxStatus.Supported }, TaxIds = new StripeList { Data = new List() } } }; sutProvider.GetDependency() .IsEnabled(Arg.Is(p => p == FeatureFlagKeys.PM19422_AllowAutomaticTaxUpdates)) .Returns(true); var actual = sutProvider.Sut.GetUpdateOptions(subscription); Assert.NotNull(actual); Assert.True(actual.AutomaticTax.Enabled); } }