2024-03-29 16:18:10 +01:00
|
|
|
|
using Bit.Api.Billing.Controllers;
|
2024-06-03 17:00:52 +02:00
|
|
|
|
using Bit.Api.Billing.Models.Requests;
|
2024-04-16 19:55:00 +02:00
|
|
|
|
using Bit.Api.Billing.Models.Responses;
|
2024-03-29 16:18:10 +01:00
|
|
|
|
using Bit.Core;
|
2024-06-03 17:00:52 +02:00
|
|
|
|
using Bit.Core.AdminConsole.Entities.Provider;
|
|
|
|
|
using Bit.Core.AdminConsole.Enums.Provider;
|
|
|
|
|
using Bit.Core.AdminConsole.Repositories;
|
|
|
|
|
using Bit.Core.Billing.Constants;
|
2024-07-31 15:26:44 +02:00
|
|
|
|
using Bit.Core.Billing.Entities;
|
2024-06-14 21:34:47 +02:00
|
|
|
|
using Bit.Core.Billing.Enums;
|
2024-03-29 16:18:10 +01:00
|
|
|
|
using Bit.Core.Billing.Models;
|
2024-07-31 15:26:44 +02:00
|
|
|
|
using Bit.Core.Billing.Repositories;
|
2024-05-23 16:17:00 +02:00
|
|
|
|
using Bit.Core.Billing.Services;
|
2024-03-29 16:18:10 +01:00
|
|
|
|
using Bit.Core.Context;
|
2024-07-31 15:26:44 +02:00
|
|
|
|
using Bit.Core.Models.Api;
|
|
|
|
|
using Bit.Core.Models.BitStripe;
|
2024-03-29 16:18:10 +01:00
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
2024-07-31 15:26:44 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2024-03-29 16:18:10 +01:00
|
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using NSubstitute.ReturnsExtensions;
|
|
|
|
|
using Stripe;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2024-06-24 17:15:47 +02:00
|
|
|
|
using static Bit.Api.Test.Billing.Utilities;
|
|
|
|
|
|
2024-03-29 16:18:10 +01:00
|
|
|
|
namespace Bit.Api.Test.Billing.Controllers;
|
|
|
|
|
|
|
|
|
|
[ControllerCustomize(typeof(ProviderBillingController))]
|
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
public class ProviderBillingControllerTests
|
|
|
|
|
{
|
2024-07-31 15:26:44 +02:00
|
|
|
|
#region GetInvoicesAsync & TryGetBillableProviderForAdminOperations
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task GetInvoicesAsync_FFDisabled_NotFound(
|
|
|
|
|
Guid providerId,
|
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling)
|
|
|
|
|
.Returns(false);
|
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetInvoicesAsync(providerId);
|
|
|
|
|
|
|
|
|
|
AssertNotFound(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task GetInvoicesAsync_NullProvider_NotFound(
|
|
|
|
|
Guid providerId,
|
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling)
|
|
|
|
|
.Returns(true);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(providerId).ReturnsNull();
|
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetInvoicesAsync(providerId);
|
|
|
|
|
|
|
|
|
|
AssertNotFound(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task GetInvoicesAsync_NotProviderUser_Unauthorized(
|
|
|
|
|
Provider provider,
|
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling)
|
|
|
|
|
.Returns(true);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ProviderProviderAdmin(provider.Id)
|
|
|
|
|
.Returns(false);
|
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetInvoicesAsync(provider.Id);
|
|
|
|
|
|
|
|
|
|
AssertUnauthorized(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task GetInvoicesAsync_ProviderNotBillable_Unauthorized(
|
|
|
|
|
Provider provider,
|
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling)
|
|
|
|
|
.Returns(true);
|
|
|
|
|
|
|
|
|
|
provider.Type = ProviderType.Reseller;
|
|
|
|
|
provider.Status = ProviderStatusType.Created;
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ProviderProviderAdmin(provider.Id)
|
|
|
|
|
.Returns(true);
|
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetInvoicesAsync(provider.Id);
|
|
|
|
|
|
|
|
|
|
AssertUnauthorized(result);
|
|
|
|
|
}
|
2024-06-05 19:33:28 +02:00
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task GetInvoices_Ok(
|
|
|
|
|
Provider provider,
|
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
2024-06-24 17:15:47 +02:00
|
|
|
|
ConfigureStableAdminInputs(provider, sutProvider);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
|
|
|
|
|
var invoices = new List<Invoice>
|
|
|
|
|
{
|
|
|
|
|
new ()
|
|
|
|
|
{
|
2024-06-14 18:26:49 +02:00
|
|
|
|
Id = "3",
|
2024-06-05 19:33:28 +02:00
|
|
|
|
Created = new DateTime(2024, 7, 1),
|
|
|
|
|
Status = "draft",
|
|
|
|
|
Total = 100000,
|
|
|
|
|
HostedInvoiceUrl = "https://example.com/invoice/3",
|
|
|
|
|
InvoicePdf = "https://example.com/invoice/3/pdf"
|
|
|
|
|
},
|
|
|
|
|
new ()
|
|
|
|
|
{
|
2024-06-14 18:26:49 +02:00
|
|
|
|
Id = "2",
|
2024-06-05 19:33:28 +02:00
|
|
|
|
Created = new DateTime(2024, 6, 1),
|
2024-06-14 18:26:49 +02:00
|
|
|
|
Number = "B",
|
2024-06-05 19:33:28 +02:00
|
|
|
|
Status = "open",
|
|
|
|
|
Total = 100000,
|
2024-06-24 17:15:47 +02:00
|
|
|
|
DueDate = new DateTime(2024, 7, 1),
|
2024-06-05 19:33:28 +02:00
|
|
|
|
HostedInvoiceUrl = "https://example.com/invoice/2",
|
|
|
|
|
InvoicePdf = "https://example.com/invoice/2/pdf"
|
|
|
|
|
},
|
|
|
|
|
new ()
|
|
|
|
|
{
|
2024-06-14 18:26:49 +02:00
|
|
|
|
Id = "1",
|
2024-06-05 19:33:28 +02:00
|
|
|
|
Created = new DateTime(2024, 5, 1),
|
2024-06-14 18:26:49 +02:00
|
|
|
|
Number = "A",
|
2024-06-05 19:33:28 +02:00
|
|
|
|
Status = "paid",
|
|
|
|
|
Total = 100000,
|
2024-06-24 17:15:47 +02:00
|
|
|
|
DueDate = new DateTime(2024, 6, 1),
|
2024-06-05 19:33:28 +02:00
|
|
|
|
HostedInvoiceUrl = "https://example.com/invoice/1",
|
|
|
|
|
InvoicePdf = "https://example.com/invoice/1/pdf"
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
sutProvider.GetDependency<IStripeAdapter>().InvoiceListAsync(Arg.Is<StripeInvoiceListOptions>(
|
|
|
|
|
options =>
|
|
|
|
|
options.Customer == provider.GatewayCustomerId)).Returns(invoices);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetInvoicesAsync(provider.Id);
|
|
|
|
|
|
|
|
|
|
Assert.IsType<Ok<InvoicesResponse>>(result);
|
|
|
|
|
|
|
|
|
|
var response = ((Ok<InvoicesResponse>)result).Value;
|
|
|
|
|
|
|
|
|
|
Assert.Equal(2, response.Invoices.Count);
|
|
|
|
|
|
|
|
|
|
var openInvoice = response.Invoices.FirstOrDefault(i => i.Status == "open");
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(openInvoice);
|
2024-06-14 18:26:49 +02:00
|
|
|
|
Assert.Equal("2", openInvoice.Id);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
Assert.Equal(new DateTime(2024, 6, 1), openInvoice.Date);
|
2024-06-14 18:26:49 +02:00
|
|
|
|
Assert.Equal("B", openInvoice.Number);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
Assert.Equal(1000, openInvoice.Total);
|
2024-06-24 17:15:47 +02:00
|
|
|
|
Assert.Equal(new DateTime(2024, 7, 1), openInvoice.DueDate);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
Assert.Equal("https://example.com/invoice/2", openInvoice.Url);
|
|
|
|
|
|
|
|
|
|
var paidInvoice = response.Invoices.FirstOrDefault(i => i.Status == "paid");
|
2024-06-14 18:26:49 +02:00
|
|
|
|
|
2024-06-05 19:33:28 +02:00
|
|
|
|
Assert.NotNull(paidInvoice);
|
2024-06-14 18:26:49 +02:00
|
|
|
|
Assert.Equal("1", paidInvoice.Id);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
Assert.Equal(new DateTime(2024, 5, 1), paidInvoice.Date);
|
2024-06-14 18:26:49 +02:00
|
|
|
|
Assert.Equal("A", paidInvoice.Number);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
Assert.Equal(1000, paidInvoice.Total);
|
2024-06-24 17:15:47 +02:00
|
|
|
|
Assert.Equal(new DateTime(2024, 6, 1), paidInvoice.DueDate);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
Assert.Equal("https://example.com/invoice/1", paidInvoice.Url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2024-06-14 18:26:49 +02:00
|
|
|
|
#region GenerateClientInvoiceReportAsync
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2024-07-31 15:26:44 +02:00
|
|
|
|
public async Task GenerateClientInvoiceReportAsync_NullReportContent_ServerError(
|
2024-06-14 18:26:49 +02:00
|
|
|
|
Provider provider,
|
|
|
|
|
string invoiceId,
|
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
2024-06-24 17:15:47 +02:00
|
|
|
|
ConfigureStableAdminInputs(provider, sutProvider);
|
2024-06-14 18:26:49 +02:00
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IProviderBillingService>().GenerateClientInvoiceReport(invoiceId)
|
2024-07-31 15:26:44 +02:00
|
|
|
|
.ReturnsNull();
|
2024-06-14 18:26:49 +02:00
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GenerateClientInvoiceReportAsync(provider.Id, invoiceId);
|
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
Assert.IsType<JsonHttpResult<ErrorResponseModel>>(result);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
var response = (JsonHttpResult<ErrorResponseModel>)result;
|
2024-06-05 19:33:28 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
Assert.Equal(StatusCodes.Status500InternalServerError, response.StatusCode);
|
|
|
|
|
Assert.Equal("We had a problem generating your invoice CSV. Please contact support.", response.Value.Message);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2024-07-31 15:26:44 +02:00
|
|
|
|
public async Task GenerateClientInvoiceReportAsync_Ok(
|
2024-06-05 19:33:28 +02:00
|
|
|
|
Provider provider,
|
2024-07-31 15:26:44 +02:00
|
|
|
|
string invoiceId,
|
2024-06-05 19:33:28 +02:00
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
2024-06-24 17:15:47 +02:00
|
|
|
|
ConfigureStableAdminInputs(provider, sutProvider);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
var reportContent = "Report"u8.ToArray();
|
2024-06-05 19:33:28 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
sutProvider.GetDependency<IProviderBillingService>().GenerateClientInvoiceReport(invoiceId)
|
|
|
|
|
.Returns(reportContent);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
var result = await sutProvider.Sut.GenerateClientInvoiceReportAsync(provider.Id, invoiceId);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
Assert.IsType<FileContentHttpResult>(result);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
var response = (FileContentHttpResult)result;
|
2024-06-05 19:33:28 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
Assert.Equal("text/csv", response.ContentType);
|
|
|
|
|
Assert.Equal(reportContent, response.FileContents);
|
2024-06-05 19:33:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2024-06-24 17:15:47 +02:00
|
|
|
|
#region GetSubscriptionAsync & TryGetBillableProviderForServiceUserOperation
|
|
|
|
|
|
2024-03-29 16:18:10 +01:00
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task GetSubscriptionAsync_FFDisabled_NotFound(
|
|
|
|
|
Guid providerId,
|
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling)
|
|
|
|
|
.Returns(false);
|
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetSubscriptionAsync(providerId);
|
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
AssertNotFound(result);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2024-06-03 17:00:52 +02:00
|
|
|
|
public async Task GetSubscriptionAsync_NullProvider_NotFound(
|
2024-03-29 16:18:10 +01:00
|
|
|
|
Guid providerId,
|
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling)
|
|
|
|
|
.Returns(true);
|
|
|
|
|
|
2024-06-03 17:00:52 +02:00
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(providerId).ReturnsNull();
|
2024-03-29 16:18:10 +01:00
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetSubscriptionAsync(providerId);
|
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
AssertNotFound(result);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2024-06-24 17:15:47 +02:00
|
|
|
|
public async Task GetSubscriptionAsync_NotProviderUser_Unauthorized(
|
2024-06-03 17:00:52 +02:00
|
|
|
|
Provider provider,
|
2024-03-29 16:18:10 +01:00
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling)
|
|
|
|
|
.Returns(true);
|
|
|
|
|
|
2024-06-03 17:00:52 +02:00
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
|
2024-06-24 17:15:47 +02:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ProviderUser(provider.Id)
|
2024-06-03 17:00:52 +02:00
|
|
|
|
.Returns(false);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
|
2024-06-03 17:00:52 +02:00
|
|
|
|
var result = await sutProvider.Sut.GetSubscriptionAsync(provider.Id);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
AssertUnauthorized(result);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2024-06-03 17:00:52 +02:00
|
|
|
|
public async Task GetSubscriptionAsync_ProviderNotBillable_Unauthorized(
|
|
|
|
|
Provider provider,
|
2024-03-29 16:18:10 +01:00
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IFeatureService>().IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling)
|
|
|
|
|
.Returns(true);
|
|
|
|
|
|
2024-06-03 17:00:52 +02:00
|
|
|
|
provider.Type = ProviderType.Reseller;
|
|
|
|
|
provider.Status = ProviderStatusType.Created;
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
|
2024-06-24 17:15:47 +02:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ProviderUser(provider.Id)
|
|
|
|
|
.Returns(true);
|
2024-06-03 17:00:52 +02:00
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetSubscriptionAsync(provider.Id);
|
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
AssertUnauthorized(result);
|
2024-06-03 17:00:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task GetSubscriptionAsync_Ok(
|
|
|
|
|
Provider provider,
|
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
2024-06-24 17:15:47 +02:00
|
|
|
|
ConfigureStableServiceUserInputs(provider, sutProvider);
|
2024-06-03 17:00:52 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
var stripeAdapter = sutProvider.GetDependency<IStripeAdapter>();
|
|
|
|
|
|
|
|
|
|
var (thisYear, thisMonth, _) = DateTime.UtcNow;
|
|
|
|
|
var daysInThisMonth = DateTime.DaysInMonth(thisYear, thisMonth);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
|
|
|
|
|
var subscription = new Subscription
|
|
|
|
|
{
|
2024-07-31 15:26:44 +02:00
|
|
|
|
CollectionMethod = StripeConstants.CollectionMethod.ChargeAutomatically,
|
|
|
|
|
CurrentPeriodEnd = new DateTime(thisYear, thisMonth, daysInThisMonth),
|
2024-06-26 15:08:18 +02:00
|
|
|
|
Customer = new Customer
|
|
|
|
|
{
|
2024-07-31 15:26:44 +02:00
|
|
|
|
Address = new Address
|
2024-06-26 15:08:18 +02:00
|
|
|
|
{
|
2024-07-31 15:26:44 +02:00
|
|
|
|
Country = "US",
|
|
|
|
|
PostalCode = "12345",
|
|
|
|
|
Line1 = "123 Example St.",
|
|
|
|
|
Line2 = "Unit 1",
|
|
|
|
|
City = "Example Town",
|
|
|
|
|
State = "NY"
|
|
|
|
|
},
|
2024-08-05 17:02:17 +02:00
|
|
|
|
Balance = -100000,
|
2024-07-31 15:26:44 +02:00
|
|
|
|
Discount = new Discount { Coupon = new Coupon { PercentOff = 10 } },
|
|
|
|
|
TaxIds = new StripeList<TaxId> { Data = [new TaxId { Value = "123456789" }] }
|
|
|
|
|
},
|
|
|
|
|
Status = "unpaid",
|
2024-03-29 16:18:10 +01:00
|
|
|
|
};
|
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
stripeAdapter.SubscriptionGetAsync(provider.GatewaySubscriptionId, Arg.Is<SubscriptionGetOptions>(
|
|
|
|
|
options =>
|
|
|
|
|
options.Expand.Contains("customer.tax_ids") &&
|
|
|
|
|
options.Expand.Contains("test_clock"))).Returns(subscription);
|
2024-06-26 15:08:18 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
var lastMonth = thisMonth - 1;
|
|
|
|
|
var daysInLastMonth = DateTime.DaysInMonth(thisYear, lastMonth);
|
2024-06-26 15:08:18 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
var overdueInvoice = new Invoice
|
|
|
|
|
{
|
|
|
|
|
Id = "invoice_id",
|
|
|
|
|
Status = "open",
|
|
|
|
|
Created = new DateTime(thisYear, lastMonth, 1),
|
|
|
|
|
PeriodEnd = new DateTime(thisYear, lastMonth, daysInLastMonth),
|
|
|
|
|
Attempted = true
|
|
|
|
|
};
|
2024-03-29 16:18:10 +01:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
stripeAdapter.InvoiceSearchAsync(Arg.Is<InvoiceSearchOptions>(
|
|
|
|
|
options => options.Query == $"subscription:'{subscription.Id}' status:'open'"))
|
|
|
|
|
.Returns([overdueInvoice]);
|
|
|
|
|
|
|
|
|
|
var providerPlans = new List<ProviderPlan>
|
|
|
|
|
{
|
|
|
|
|
new ()
|
|
|
|
|
{
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
ProviderId = provider.Id,
|
|
|
|
|
PlanType = PlanType.TeamsMonthly,
|
|
|
|
|
SeatMinimum = 50,
|
|
|
|
|
PurchasedSeats = 10,
|
|
|
|
|
AllocatedSeats = 60
|
|
|
|
|
},
|
|
|
|
|
new ()
|
|
|
|
|
{
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
ProviderId = provider.Id,
|
|
|
|
|
PlanType = PlanType.EnterpriseMonthly,
|
|
|
|
|
SeatMinimum = 100,
|
|
|
|
|
PurchasedSeats = 0,
|
|
|
|
|
AllocatedSeats = 90
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IProviderPlanRepository>().GetByProviderId(provider.Id).Returns(providerPlans);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
|
2024-06-03 17:00:52 +02:00
|
|
|
|
var result = await sutProvider.Sut.GetSubscriptionAsync(provider.Id);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
Assert.IsType<Ok<ProviderSubscriptionResponse>>(result);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
var response = ((Ok<ProviderSubscriptionResponse>)result).Value;
|
2024-03-29 16:18:10 +01:00
|
|
|
|
|
2024-06-26 15:08:18 +02:00
|
|
|
|
Assert.Equal(subscription.Status, response.Status);
|
|
|
|
|
Assert.Equal(subscription.CurrentPeriodEnd, response.CurrentPeriodEndDate);
|
|
|
|
|
Assert.Equal(subscription.Customer!.Discount!.Coupon!.PercentOff, response.DiscountPercentage);
|
|
|
|
|
Assert.Equal(subscription.CollectionMethod, response.CollectionMethod);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
|
|
|
|
|
var teamsPlan = StaticStore.GetPlan(PlanType.TeamsMonthly);
|
2024-06-03 17:00:52 +02:00
|
|
|
|
var providerTeamsPlan = response.Plans.FirstOrDefault(plan => plan.PlanName == teamsPlan.Name);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
Assert.NotNull(providerTeamsPlan);
|
|
|
|
|
Assert.Equal(50, providerTeamsPlan.SeatMinimum);
|
|
|
|
|
Assert.Equal(10, providerTeamsPlan.PurchasedSeats);
|
2024-07-31 15:26:44 +02:00
|
|
|
|
Assert.Equal(60, providerTeamsPlan.AssignedSeats);
|
2024-06-24 17:16:57 +02:00
|
|
|
|
Assert.Equal(60 * teamsPlan.PasswordManager.ProviderPortalSeatPrice, providerTeamsPlan.Cost);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
Assert.Equal("Monthly", providerTeamsPlan.Cadence);
|
|
|
|
|
|
|
|
|
|
var enterprisePlan = StaticStore.GetPlan(PlanType.EnterpriseMonthly);
|
2024-06-03 17:00:52 +02:00
|
|
|
|
var providerEnterprisePlan = response.Plans.FirstOrDefault(plan => plan.PlanName == enterprisePlan.Name);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
Assert.NotNull(providerEnterprisePlan);
|
|
|
|
|
Assert.Equal(100, providerEnterprisePlan.SeatMinimum);
|
|
|
|
|
Assert.Equal(0, providerEnterprisePlan.PurchasedSeats);
|
|
|
|
|
Assert.Equal(90, providerEnterprisePlan.AssignedSeats);
|
2024-06-24 17:16:57 +02:00
|
|
|
|
Assert.Equal(100 * enterprisePlan.PasswordManager.ProviderPortalSeatPrice, providerEnterprisePlan.Cost);
|
2024-03-29 16:18:10 +01:00
|
|
|
|
Assert.Equal("Monthly", providerEnterprisePlan.Cadence);
|
2024-06-26 15:08:18 +02:00
|
|
|
|
|
2024-08-05 17:02:17 +02:00
|
|
|
|
Assert.Equal(1000.00M, response.AccountCredit);
|
2024-06-26 15:08:18 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
var customer = subscription.Customer;
|
|
|
|
|
Assert.Equal(customer.Address.Country, response.TaxInformation.Country);
|
|
|
|
|
Assert.Equal(customer.Address.PostalCode, response.TaxInformation.PostalCode);
|
|
|
|
|
Assert.Equal(customer.TaxIds.First().Value, response.TaxInformation.TaxId);
|
|
|
|
|
Assert.Equal(customer.Address.Line1, response.TaxInformation.Line1);
|
|
|
|
|
Assert.Equal(customer.Address.Line2, response.TaxInformation.Line2);
|
|
|
|
|
Assert.Equal(customer.Address.City, response.TaxInformation.City);
|
|
|
|
|
Assert.Equal(customer.Address.State, response.TaxInformation.State);
|
2024-06-03 17:00:52 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
Assert.Null(response.CancelAt);
|
2024-06-03 17:00:52 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
Assert.Equal(overdueInvoice.Created.AddDays(14), response.Suspension.SuspensionDate);
|
|
|
|
|
Assert.Equal(overdueInvoice.PeriodEnd, response.Suspension.UnpaidPeriodEndDate);
|
|
|
|
|
Assert.Equal(14, response.Suspension.GracePeriod);
|
2024-06-03 17:00:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
#region UpdateTaxInformationAsync
|
2024-06-03 17:00:52 +02:00
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2024-07-31 15:26:44 +02:00
|
|
|
|
public async Task UpdateTaxInformation_NoCountry_BadRequest(
|
2024-06-03 17:00:52 +02:00
|
|
|
|
Provider provider,
|
2024-07-31 15:26:44 +02:00
|
|
|
|
TaxInformationRequestBody requestBody,
|
2024-06-03 17:00:52 +02:00
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
2024-06-24 17:15:47 +02:00
|
|
|
|
ConfigureStableAdminInputs(provider, sutProvider);
|
2024-06-03 17:00:52 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
requestBody.Country = null;
|
2024-06-03 17:00:52 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
var result = await sutProvider.Sut.UpdateTaxInformationAsync(provider.Id, requestBody);
|
2024-06-03 17:00:52 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
Assert.IsType<BadRequest<ErrorResponseModel>>(result);
|
2024-06-03 17:00:52 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
var response = (BadRequest<ErrorResponseModel>)result;
|
2024-06-03 17:00:52 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
Assert.Equal("Country and postal code are required to update your tax information.", response.Value.Message);
|
|
|
|
|
}
|
2024-06-03 17:00:52 +02:00
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task UpdateTaxInformation_Ok(
|
|
|
|
|
Provider provider,
|
|
|
|
|
TaxInformationRequestBody requestBody,
|
|
|
|
|
SutProvider<ProviderBillingController> sutProvider)
|
|
|
|
|
{
|
2024-06-24 17:15:47 +02:00
|
|
|
|
ConfigureStableAdminInputs(provider, sutProvider);
|
2024-06-03 17:00:52 +02:00
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.UpdateTaxInformationAsync(provider.Id, requestBody);
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<ISubscriberService>().Received(1).UpdateTaxInformation(
|
2024-07-31 15:26:44 +02:00
|
|
|
|
provider, Arg.Is<TaxInformation>(
|
2024-06-03 17:00:52 +02:00
|
|
|
|
options =>
|
|
|
|
|
options.Country == requestBody.Country &&
|
|
|
|
|
options.PostalCode == requestBody.PostalCode &&
|
|
|
|
|
options.TaxId == requestBody.TaxId &&
|
|
|
|
|
options.Line1 == requestBody.Line1 &&
|
|
|
|
|
options.Line2 == requestBody.Line2 &&
|
|
|
|
|
options.City == requestBody.City &&
|
|
|
|
|
options.State == requestBody.State));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2024-03-29 16:18:10 +01:00
|
|
|
|
}
|