|
| 1 | +using Bit.Core.Billing.Enums; |
| 2 | +using Bit.Core.Billing.Models.Api.Requests; |
| 3 | +using Bit.Core.Billing.Models.Api.Requests.Organizations; |
| 4 | +using Bit.Core.Billing.Models.StaticStore.Plans; |
| 5 | +using Bit.Core.Billing.Pricing; |
| 6 | +using Bit.Core.Billing.Services; |
| 7 | +using Bit.Core.Billing.Services.Contracts; |
| 8 | +using Bit.Core.Enums; |
| 9 | +using Bit.Core.Services; |
| 10 | +using Bit.Core.Test.Billing.Stubs; |
| 11 | +using Bit.Test.Common.AutoFixture; |
| 12 | +using Bit.Test.Common.AutoFixture.Attributes; |
| 13 | +using NSubstitute; |
| 14 | +using Stripe; |
| 15 | +using Xunit; |
| 16 | + |
| 17 | +namespace Bit.Core.Test.Services; |
| 18 | + |
| 19 | +[SutProviderCustomize] |
| 20 | +public class StripePaymentServiceTests |
| 21 | +{ |
| 22 | + [Theory] |
| 23 | + [BitAutoData] |
| 24 | + public async Task PreviewInvoiceAsync_ForOrganization_CalculatesSalesTaxCorrectlyForFamiliesWithoutAdditionalStorage( |
| 25 | + SutProvider<StripePaymentService> sutProvider) |
| 26 | + { |
| 27 | + sutProvider.GetDependency<IAutomaticTaxFactory>() |
| 28 | + .CreateAsync(Arg.Is<AutomaticTaxFactoryParameters>(p => p.PlanType == PlanType.FamiliesAnnually)) |
| 29 | + .Returns(new FakeAutomaticTaxStrategy(true)); |
| 30 | + |
| 31 | + var familiesPlan = new FamiliesPlan(); |
| 32 | + sutProvider.GetDependency<IPricingClient>() |
| 33 | + .GetPlanOrThrow(Arg.Is<PlanType>(p => p == PlanType.FamiliesAnnually)) |
| 34 | + .Returns(familiesPlan); |
| 35 | + |
| 36 | + var parameters = new PreviewOrganizationInvoiceRequestBody |
| 37 | + { |
| 38 | + PasswordManager = new OrganizationPasswordManagerRequestModel |
| 39 | + { |
| 40 | + Plan = PlanType.FamiliesAnnually, |
| 41 | + AdditionalStorage = 0 |
| 42 | + }, |
| 43 | + TaxInformation = new TaxInformationRequestModel |
| 44 | + { |
| 45 | + Country = "FR", |
| 46 | + PostalCode = "12345" |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + sutProvider.GetDependency<IStripeAdapter>() |
| 51 | + .InvoiceCreatePreviewAsync(Arg.Is<InvoiceCreatePreviewOptions>(p => |
| 52 | + p.Currency == "usd" && |
| 53 | + p.SubscriptionDetails.Items.Any(x => |
| 54 | + x.Plan == familiesPlan.PasswordManager.StripePlanId && |
| 55 | + x.Quantity == 1) && |
| 56 | + p.SubscriptionDetails.Items.Any(x => |
| 57 | + x.Plan == familiesPlan.PasswordManager.StripeStoragePlanId && |
| 58 | + x.Quantity == 0))) |
| 59 | + .Returns(new Invoice |
| 60 | + { |
| 61 | + TotalExcludingTax = 4000, |
| 62 | + Tax = 800, |
| 63 | + Total = 4800 |
| 64 | + }); |
| 65 | + |
| 66 | + var actual = await sutProvider.Sut.PreviewInvoiceAsync(parameters, null, null); |
| 67 | + |
| 68 | + Assert.Equal(8M, actual.TaxAmount); |
| 69 | + Assert.Equal(48M, actual.TotalAmount); |
| 70 | + Assert.Equal(40M, actual.TaxableBaseAmount); |
| 71 | + } |
| 72 | + |
| 73 | + [Theory] |
| 74 | + [BitAutoData] |
| 75 | + public async Task PreviewInvoiceAsync_ForOrganization_CalculatesSalesTaxCorrectlyForFamiliesWithAdditionalStorage( |
| 76 | + SutProvider<StripePaymentService> sutProvider) |
| 77 | + { |
| 78 | + sutProvider.GetDependency<IAutomaticTaxFactory>() |
| 79 | + .CreateAsync(Arg.Is<AutomaticTaxFactoryParameters>(p => p.PlanType == PlanType.FamiliesAnnually)) |
| 80 | + .Returns(new FakeAutomaticTaxStrategy(true)); |
| 81 | + |
| 82 | + var familiesPlan = new FamiliesPlan(); |
| 83 | + sutProvider.GetDependency<IPricingClient>() |
| 84 | + .GetPlanOrThrow(Arg.Is<PlanType>(p => p == PlanType.FamiliesAnnually)) |
| 85 | + .Returns(familiesPlan); |
| 86 | + |
| 87 | + var parameters = new PreviewOrganizationInvoiceRequestBody |
| 88 | + { |
| 89 | + PasswordManager = new OrganizationPasswordManagerRequestModel |
| 90 | + { |
| 91 | + Plan = PlanType.FamiliesAnnually, |
| 92 | + AdditionalStorage = 1 |
| 93 | + }, |
| 94 | + TaxInformation = new TaxInformationRequestModel |
| 95 | + { |
| 96 | + Country = "FR", |
| 97 | + PostalCode = "12345" |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + sutProvider.GetDependency<IStripeAdapter>() |
| 102 | + .InvoiceCreatePreviewAsync(Arg.Is<InvoiceCreatePreviewOptions>(p => |
| 103 | + p.Currency == "usd" && |
| 104 | + p.SubscriptionDetails.Items.Any(x => |
| 105 | + x.Plan == familiesPlan.PasswordManager.StripePlanId && |
| 106 | + x.Quantity == 1) && |
| 107 | + p.SubscriptionDetails.Items.Any(x => |
| 108 | + x.Plan == familiesPlan.PasswordManager.StripeStoragePlanId && |
| 109 | + x.Quantity == 1))) |
| 110 | + .Returns(new Invoice |
| 111 | + { |
| 112 | + TotalExcludingTax = 4000, |
| 113 | + Tax = 800, |
| 114 | + Total = 4800 |
| 115 | + }); |
| 116 | + |
| 117 | + var actual = await sutProvider.Sut.PreviewInvoiceAsync(parameters, null, null); |
| 118 | + |
| 119 | + Assert.Equal(8M, actual.TaxAmount); |
| 120 | + Assert.Equal(48M, actual.TotalAmount); |
| 121 | + Assert.Equal(40M, actual.TaxableBaseAmount); |
| 122 | + } |
| 123 | + |
| 124 | + [Theory] |
| 125 | + [BitAutoData] |
| 126 | + public async Task PreviewInvoiceAsync_ForOrganization_CalculatesSalesTaxCorrectlyForFamiliesForEnterpriseWithoutAdditionalStorage( |
| 127 | + SutProvider<StripePaymentService> sutProvider) |
| 128 | + { |
| 129 | + sutProvider.GetDependency<IAutomaticTaxFactory>() |
| 130 | + .CreateAsync(Arg.Is<AutomaticTaxFactoryParameters>(p => p.PlanType == PlanType.FamiliesAnnually)) |
| 131 | + .Returns(new FakeAutomaticTaxStrategy(true)); |
| 132 | + |
| 133 | + var familiesPlan = new FamiliesPlan(); |
| 134 | + sutProvider.GetDependency<IPricingClient>() |
| 135 | + .GetPlanOrThrow(Arg.Is<PlanType>(p => p == PlanType.FamiliesAnnually)) |
| 136 | + .Returns(familiesPlan); |
| 137 | + |
| 138 | + var parameters = new PreviewOrganizationInvoiceRequestBody |
| 139 | + { |
| 140 | + PasswordManager = new OrganizationPasswordManagerRequestModel |
| 141 | + { |
| 142 | + Plan = PlanType.FamiliesAnnually, |
| 143 | + SponsoredPlan = PlanSponsorshipType.FamiliesForEnterprise, |
| 144 | + AdditionalStorage = 0 |
| 145 | + }, |
| 146 | + TaxInformation = new TaxInformationRequestModel |
| 147 | + { |
| 148 | + Country = "FR", |
| 149 | + PostalCode = "12345" |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + sutProvider.GetDependency<IStripeAdapter>() |
| 154 | + .InvoiceCreatePreviewAsync(Arg.Is<InvoiceCreatePreviewOptions>(p => |
| 155 | + p.Currency == "usd" && |
| 156 | + p.SubscriptionDetails.Items.Any(x => |
| 157 | + x.Plan == "2021-family-for-enterprise-annually" && |
| 158 | + x.Quantity == 1) && |
| 159 | + p.SubscriptionDetails.Items.Any(x => |
| 160 | + x.Plan == familiesPlan.PasswordManager.StripeStoragePlanId && |
| 161 | + x.Quantity == 0))) |
| 162 | + .Returns(new Invoice |
| 163 | + { |
| 164 | + TotalExcludingTax = 0, |
| 165 | + Tax = 0, |
| 166 | + Total = 0 |
| 167 | + }); |
| 168 | + |
| 169 | + var actual = await sutProvider.Sut.PreviewInvoiceAsync(parameters, null, null); |
| 170 | + |
| 171 | + Assert.Equal(0M, actual.TaxAmount); |
| 172 | + Assert.Equal(0M, actual.TotalAmount); |
| 173 | + Assert.Equal(0M, actual.TaxableBaseAmount); |
| 174 | + } |
| 175 | + |
| 176 | + [Theory] |
| 177 | + [BitAutoData] |
| 178 | + public async Task PreviewInvoiceAsync_ForOrganization_CalculatesSalesTaxCorrectlyForFamiliesForEnterpriseWithAdditionalStorage( |
| 179 | + SutProvider<StripePaymentService> sutProvider) |
| 180 | + { |
| 181 | + sutProvider.GetDependency<IAutomaticTaxFactory>() |
| 182 | + .CreateAsync(Arg.Is<AutomaticTaxFactoryParameters>(p => p.PlanType == PlanType.FamiliesAnnually)) |
| 183 | + .Returns(new FakeAutomaticTaxStrategy(true)); |
| 184 | + |
| 185 | + var familiesPlan = new FamiliesPlan(); |
| 186 | + sutProvider.GetDependency<IPricingClient>() |
| 187 | + .GetPlanOrThrow(Arg.Is<PlanType>(p => p == PlanType.FamiliesAnnually)) |
| 188 | + .Returns(familiesPlan); |
| 189 | + |
| 190 | + var parameters = new PreviewOrganizationInvoiceRequestBody |
| 191 | + { |
| 192 | + PasswordManager = new OrganizationPasswordManagerRequestModel |
| 193 | + { |
| 194 | + Plan = PlanType.FamiliesAnnually, |
| 195 | + SponsoredPlan = PlanSponsorshipType.FamiliesForEnterprise, |
| 196 | + AdditionalStorage = 1 |
| 197 | + }, |
| 198 | + TaxInformation = new TaxInformationRequestModel |
| 199 | + { |
| 200 | + Country = "FR", |
| 201 | + PostalCode = "12345" |
| 202 | + } |
| 203 | + }; |
| 204 | + |
| 205 | + sutProvider.GetDependency<IStripeAdapter>() |
| 206 | + .InvoiceCreatePreviewAsync(Arg.Is<InvoiceCreatePreviewOptions>(p => |
| 207 | + p.Currency == "usd" && |
| 208 | + p.SubscriptionDetails.Items.Any(x => |
| 209 | + x.Plan == "2021-family-for-enterprise-annually" && |
| 210 | + x.Quantity == 1) && |
| 211 | + p.SubscriptionDetails.Items.Any(x => |
| 212 | + x.Plan == familiesPlan.PasswordManager.StripeStoragePlanId && |
| 213 | + x.Quantity == 1))) |
| 214 | + .Returns(new Invoice |
| 215 | + { |
| 216 | + TotalExcludingTax = 400, |
| 217 | + Tax = 8, |
| 218 | + Total = 408 |
| 219 | + }); |
| 220 | + |
| 221 | + var actual = await sutProvider.Sut.PreviewInvoiceAsync(parameters, null, null); |
| 222 | + |
| 223 | + Assert.Equal(0.08M, actual.TaxAmount); |
| 224 | + Assert.Equal(4.08M, actual.TotalAmount); |
| 225 | + Assert.Equal(4M, actual.TaxableBaseAmount); |
| 226 | + } |
| 227 | +} |
0 commit comments