mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 00:22:50 -05:00
Revert "Merge branch 'main' into ac/ac-1682/ef-migrations"
This reverts commitf98646a722
, reversing changes made to7dfd2821f1
.
This commit is contained in:
@ -1,339 +0,0 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.AdminConsole.Entities.Provider;
|
||||
using Bit.Core.Billing;
|
||||
using Bit.Core.Billing.Commands.Implementations;
|
||||
using Bit.Core.Billing.Entities;
|
||||
using Bit.Core.Billing.Queries;
|
||||
using Bit.Core.Billing.Repositories;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.StaticStore;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Utilities;
|
||||
using Bit.Test.Common.AutoFixture;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
using static Bit.Core.Test.Billing.Utilities;
|
||||
|
||||
namespace Bit.Core.Test.Billing.Commands;
|
||||
|
||||
[SutProviderCustomize]
|
||||
public class AssignSeatsToClientOrganizationCommandTests
|
||||
{
|
||||
[Theory, BitAutoData]
|
||||
public Task AssignSeatsToClientOrganization_NullProvider_ArgumentNullException(
|
||||
Organization organization,
|
||||
int seats,
|
||||
SutProvider<AssignSeatsToClientOrganizationCommand> sutProvider)
|
||||
=> Assert.ThrowsAsync<ArgumentNullException>(() =>
|
||||
sutProvider.Sut.AssignSeatsToClientOrganization(null, organization, seats));
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public Task AssignSeatsToClientOrganization_NullOrganization_ArgumentNullException(
|
||||
Provider provider,
|
||||
int seats,
|
||||
SutProvider<AssignSeatsToClientOrganizationCommand> sutProvider)
|
||||
=> Assert.ThrowsAsync<ArgumentNullException>(() =>
|
||||
sutProvider.Sut.AssignSeatsToClientOrganization(provider, null, seats));
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public Task AssignSeatsToClientOrganization_NegativeSeats_BillingException(
|
||||
Provider provider,
|
||||
Organization organization,
|
||||
SutProvider<AssignSeatsToClientOrganizationCommand> sutProvider)
|
||||
=> Assert.ThrowsAsync<BillingException>(() =>
|
||||
sutProvider.Sut.AssignSeatsToClientOrganization(provider, organization, -5));
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task AssignSeatsToClientOrganization_CurrentSeatsMatchesNewSeats_NoOp(
|
||||
Provider provider,
|
||||
Organization organization,
|
||||
int seats,
|
||||
SutProvider<AssignSeatsToClientOrganizationCommand> sutProvider)
|
||||
{
|
||||
organization.PlanType = PlanType.TeamsMonthly;
|
||||
|
||||
organization.Seats = seats;
|
||||
|
||||
await sutProvider.Sut.AssignSeatsToClientOrganization(provider, organization, seats);
|
||||
|
||||
await sutProvider.GetDependency<IProviderPlanRepository>().DidNotReceive().GetByProviderId(provider.Id);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task AssignSeatsToClientOrganization_OrganizationPlanTypeDoesNotSupportConsolidatedBilling_ContactSupport(
|
||||
Provider provider,
|
||||
Organization organization,
|
||||
int seats,
|
||||
SutProvider<AssignSeatsToClientOrganizationCommand> sutProvider)
|
||||
{
|
||||
organization.PlanType = PlanType.FamiliesAnnually;
|
||||
|
||||
await ThrowsContactSupportAsync(() => sutProvider.Sut.AssignSeatsToClientOrganization(provider, organization, seats));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task AssignSeatsToClientOrganization_ProviderPlanIsNotConfigured_ContactSupport(
|
||||
Provider provider,
|
||||
Organization organization,
|
||||
int seats,
|
||||
SutProvider<AssignSeatsToClientOrganizationCommand> sutProvider)
|
||||
{
|
||||
organization.PlanType = PlanType.TeamsMonthly;
|
||||
|
||||
sutProvider.GetDependency<IProviderPlanRepository>().GetByProviderId(provider.Id).Returns(new List<ProviderPlan>
|
||||
{
|
||||
new ()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
PlanType = PlanType.TeamsMonthly,
|
||||
ProviderId = provider.Id
|
||||
}
|
||||
});
|
||||
|
||||
await ThrowsContactSupportAsync(() => sutProvider.Sut.AssignSeatsToClientOrganization(provider, organization, seats));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task AssignSeatsToClientOrganization_BelowToBelow_Succeeds(
|
||||
Provider provider,
|
||||
Organization organization,
|
||||
SutProvider<AssignSeatsToClientOrganizationCommand> sutProvider)
|
||||
{
|
||||
organization.Seats = 10;
|
||||
|
||||
organization.PlanType = PlanType.TeamsMonthly;
|
||||
|
||||
// Scale up 10 seats
|
||||
const int seats = 20;
|
||||
|
||||
var providerPlans = new List<ProviderPlan>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
PlanType = PlanType.TeamsMonthly,
|
||||
ProviderId = provider.Id,
|
||||
PurchasedSeats = 0,
|
||||
// 100 minimum
|
||||
SeatMinimum = 100,
|
||||
AllocatedSeats = 50
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
PlanType = PlanType.EnterpriseMonthly,
|
||||
ProviderId = provider.Id,
|
||||
PurchasedSeats = 0,
|
||||
SeatMinimum = 500,
|
||||
AllocatedSeats = 0
|
||||
}
|
||||
};
|
||||
|
||||
var providerPlan = providerPlans.First();
|
||||
|
||||
sutProvider.GetDependency<IProviderPlanRepository>().GetByProviderId(provider.Id).Returns(providerPlans);
|
||||
|
||||
// 50 seats currently assigned with a seat minimum of 100
|
||||
sutProvider.GetDependency<IProviderBillingQueries>().GetAssignedSeatTotalForPlanOrThrow(provider.Id, providerPlan.PlanType).Returns(50);
|
||||
|
||||
await sutProvider.Sut.AssignSeatsToClientOrganization(provider, organization, seats);
|
||||
|
||||
// 50 assigned seats + 10 seat scale up = 60 seats, well below the 100 minimum
|
||||
await sutProvider.GetDependency<IPaymentService>().DidNotReceiveWithAnyArgs().AdjustSeats(
|
||||
Arg.Any<Provider>(),
|
||||
Arg.Any<Plan>(),
|
||||
Arg.Any<int>(),
|
||||
Arg.Any<int>());
|
||||
|
||||
await sutProvider.GetDependency<IOrganizationRepository>().Received(1).ReplaceAsync(Arg.Is<Organization>(
|
||||
org => org.Id == organization.Id && org.Seats == seats));
|
||||
|
||||
await sutProvider.GetDependency<IProviderPlanRepository>().Received(1).ReplaceAsync(Arg.Is<ProviderPlan>(
|
||||
pPlan => pPlan.AllocatedSeats == 60));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task AssignSeatsToClientOrganization_BelowToAbove_Succeeds(
|
||||
Provider provider,
|
||||
Organization organization,
|
||||
SutProvider<AssignSeatsToClientOrganizationCommand> sutProvider)
|
||||
{
|
||||
organization.Seats = 10;
|
||||
|
||||
organization.PlanType = PlanType.TeamsMonthly;
|
||||
|
||||
// Scale up 10 seats
|
||||
const int seats = 20;
|
||||
|
||||
var providerPlans = new List<ProviderPlan>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
PlanType = PlanType.TeamsMonthly,
|
||||
ProviderId = provider.Id,
|
||||
PurchasedSeats = 0,
|
||||
// 100 minimum
|
||||
SeatMinimum = 100,
|
||||
AllocatedSeats = 95
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
PlanType = PlanType.EnterpriseMonthly,
|
||||
ProviderId = provider.Id,
|
||||
PurchasedSeats = 0,
|
||||
SeatMinimum = 500,
|
||||
AllocatedSeats = 0
|
||||
}
|
||||
};
|
||||
|
||||
var providerPlan = providerPlans.First();
|
||||
|
||||
sutProvider.GetDependency<IProviderPlanRepository>().GetByProviderId(provider.Id).Returns(providerPlans);
|
||||
|
||||
// 95 seats currently assigned with a seat minimum of 100
|
||||
sutProvider.GetDependency<IProviderBillingQueries>().GetAssignedSeatTotalForPlanOrThrow(provider.Id, providerPlan.PlanType).Returns(95);
|
||||
|
||||
await sutProvider.Sut.AssignSeatsToClientOrganization(provider, organization, seats);
|
||||
|
||||
// 95 current + 10 seat scale = 105 seats, 5 above the minimum
|
||||
await sutProvider.GetDependency<IPaymentService>().Received(1).AdjustSeats(
|
||||
provider,
|
||||
StaticStore.GetPlan(providerPlan.PlanType),
|
||||
providerPlan.SeatMinimum!.Value,
|
||||
105);
|
||||
|
||||
await sutProvider.GetDependency<IOrganizationRepository>().Received(1).ReplaceAsync(Arg.Is<Organization>(
|
||||
org => org.Id == organization.Id && org.Seats == seats));
|
||||
|
||||
// 105 total seats - 100 minimum = 5 purchased seats
|
||||
await sutProvider.GetDependency<IProviderPlanRepository>().Received(1).ReplaceAsync(Arg.Is<ProviderPlan>(
|
||||
pPlan => pPlan.Id == providerPlan.Id && pPlan.PurchasedSeats == 5 && pPlan.AllocatedSeats == 105));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task AssignSeatsToClientOrganization_AboveToAbove_Succeeds(
|
||||
Provider provider,
|
||||
Organization organization,
|
||||
SutProvider<AssignSeatsToClientOrganizationCommand> sutProvider)
|
||||
{
|
||||
organization.Seats = 10;
|
||||
|
||||
organization.PlanType = PlanType.TeamsMonthly;
|
||||
|
||||
// Scale up 10 seats
|
||||
const int seats = 20;
|
||||
|
||||
var providerPlans = new List<ProviderPlan>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
PlanType = PlanType.TeamsMonthly,
|
||||
ProviderId = provider.Id,
|
||||
// 10 additional purchased seats
|
||||
PurchasedSeats = 10,
|
||||
// 100 seat minimum
|
||||
SeatMinimum = 100,
|
||||
AllocatedSeats = 110
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
PlanType = PlanType.EnterpriseMonthly,
|
||||
ProviderId = provider.Id,
|
||||
PurchasedSeats = 0,
|
||||
SeatMinimum = 500,
|
||||
AllocatedSeats = 0
|
||||
}
|
||||
};
|
||||
|
||||
var providerPlan = providerPlans.First();
|
||||
|
||||
sutProvider.GetDependency<IProviderPlanRepository>().GetByProviderId(provider.Id).Returns(providerPlans);
|
||||
|
||||
// 110 seats currently assigned with a seat minimum of 100
|
||||
sutProvider.GetDependency<IProviderBillingQueries>().GetAssignedSeatTotalForPlanOrThrow(provider.Id, providerPlan.PlanType).Returns(110);
|
||||
|
||||
await sutProvider.Sut.AssignSeatsToClientOrganization(provider, organization, seats);
|
||||
|
||||
// 110 current + 10 seat scale up = 120 seats
|
||||
await sutProvider.GetDependency<IPaymentService>().Received(1).AdjustSeats(
|
||||
provider,
|
||||
StaticStore.GetPlan(providerPlan.PlanType),
|
||||
110,
|
||||
120);
|
||||
|
||||
await sutProvider.GetDependency<IOrganizationRepository>().Received(1).ReplaceAsync(Arg.Is<Organization>(
|
||||
org => org.Id == organization.Id && org.Seats == seats));
|
||||
|
||||
// 120 total seats - 100 seat minimum = 20 purchased seats
|
||||
await sutProvider.GetDependency<IProviderPlanRepository>().Received(1).ReplaceAsync(Arg.Is<ProviderPlan>(
|
||||
pPlan => pPlan.Id == providerPlan.Id && pPlan.PurchasedSeats == 20 && pPlan.AllocatedSeats == 120));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task AssignSeatsToClientOrganization_AboveToBelow_Succeeds(
|
||||
Provider provider,
|
||||
Organization organization,
|
||||
SutProvider<AssignSeatsToClientOrganizationCommand> sutProvider)
|
||||
{
|
||||
organization.Seats = 50;
|
||||
|
||||
organization.PlanType = PlanType.TeamsMonthly;
|
||||
|
||||
// Scale down 30 seats
|
||||
const int seats = 20;
|
||||
|
||||
var providerPlans = new List<ProviderPlan>
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
PlanType = PlanType.TeamsMonthly,
|
||||
ProviderId = provider.Id,
|
||||
// 10 additional purchased seats
|
||||
PurchasedSeats = 10,
|
||||
// 100 seat minimum
|
||||
SeatMinimum = 100,
|
||||
AllocatedSeats = 110
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
PlanType = PlanType.EnterpriseMonthly,
|
||||
ProviderId = provider.Id,
|
||||
PurchasedSeats = 0,
|
||||
SeatMinimum = 500,
|
||||
AllocatedSeats = 0
|
||||
}
|
||||
};
|
||||
|
||||
var providerPlan = providerPlans.First();
|
||||
|
||||
sutProvider.GetDependency<IProviderPlanRepository>().GetByProviderId(provider.Id).Returns(providerPlans);
|
||||
|
||||
// 110 seats currently assigned with a seat minimum of 100
|
||||
sutProvider.GetDependency<IProviderBillingQueries>().GetAssignedSeatTotalForPlanOrThrow(provider.Id, providerPlan.PlanType).Returns(110);
|
||||
|
||||
await sutProvider.Sut.AssignSeatsToClientOrganization(provider, organization, seats);
|
||||
|
||||
// 110 seats - 30 scale down seats = 80 seats, below the 100 seat minimum.
|
||||
await sutProvider.GetDependency<IPaymentService>().Received(1).AdjustSeats(
|
||||
provider,
|
||||
StaticStore.GetPlan(providerPlan.PlanType),
|
||||
110,
|
||||
providerPlan.SeatMinimum!.Value);
|
||||
|
||||
await sutProvider.GetDependency<IOrganizationRepository>().Received(1).ReplaceAsync(Arg.Is<Organization>(
|
||||
org => org.Id == organization.Id && org.Seats == seats));
|
||||
|
||||
// Being below the seat minimum means no purchased seats.
|
||||
await sutProvider.GetDependency<IProviderPlanRepository>().Received(1).ReplaceAsync(Arg.Is<ProviderPlan>(
|
||||
pPlan => pPlan.Id == providerPlan.Id && pPlan.PurchasedSeats == 0 && pPlan.AllocatedSeats == 80));
|
||||
}
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Billing.Commands.Implementations;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Test.Common.AutoFixture;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ReturnsExtensions;
|
||||
using Xunit;
|
||||
using static Bit.Core.Test.Billing.Utilities;
|
||||
using BT = Braintree;
|
||||
using S = Stripe;
|
||||
|
||||
@ -355,4 +355,13 @@ public class RemovePaymentMethodCommandTests
|
||||
|
||||
return (braintreeGateway, customerGateway, paymentMethodGateway);
|
||||
}
|
||||
|
||||
private static async Task ThrowsContactSupportAsync(Func<Task> function)
|
||||
{
|
||||
const string message = "Could not remove your payment method. Please contact support for assistance.";
|
||||
|
||||
var exception = await Assert.ThrowsAsync<GatewayException>(function);
|
||||
|
||||
Assert.Equal(message, exception.Message);
|
||||
}
|
||||
}
|
||||
|
104
test/Core.Test/Billing/Queries/GetSubscriptionQueryTests.cs
Normal file
104
test/Core.Test/Billing/Queries/GetSubscriptionQueryTests.cs
Normal file
@ -0,0 +1,104 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Billing.Queries.Implementations;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Test.Common.AutoFixture;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ReturnsExtensions;
|
||||
using Stripe;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.Billing.Queries;
|
||||
|
||||
[SutProviderCustomize]
|
||||
public class GetSubscriptionQueryTests
|
||||
{
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_NullSubscriber_ThrowsArgumentNullException(
|
||||
SutProvider<GetSubscriptionQuery> sutProvider)
|
||||
=> await Assert.ThrowsAsync<ArgumentNullException>(
|
||||
async () => await sutProvider.Sut.GetSubscription(null));
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_Organization_NoGatewaySubscriptionId_ThrowsGatewayException(
|
||||
Organization organization,
|
||||
SutProvider<GetSubscriptionQuery> sutProvider)
|
||||
{
|
||||
organization.GatewaySubscriptionId = null;
|
||||
|
||||
await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetSubscription(organization));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_Organization_NoSubscription_ThrowsGatewayException(
|
||||
Organization organization,
|
||||
SutProvider<GetSubscriptionQuery> sutProvider)
|
||||
{
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(organization.GatewaySubscriptionId)
|
||||
.ReturnsNull();
|
||||
|
||||
await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetSubscription(organization));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_Organization_Succeeds(
|
||||
Organization organization,
|
||||
SutProvider<GetSubscriptionQuery> sutProvider)
|
||||
{
|
||||
var subscription = new Subscription();
|
||||
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(organization.GatewaySubscriptionId)
|
||||
.Returns(subscription);
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscription(organization);
|
||||
|
||||
Assert.Equivalent(subscription, gotSubscription);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_User_NoGatewaySubscriptionId_ThrowsGatewayException(
|
||||
User user,
|
||||
SutProvider<GetSubscriptionQuery> sutProvider)
|
||||
{
|
||||
user.GatewaySubscriptionId = null;
|
||||
|
||||
await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetSubscription(user));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_User_NoSubscription_ThrowsGatewayException(
|
||||
User user,
|
||||
SutProvider<GetSubscriptionQuery> sutProvider)
|
||||
{
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(user.GatewaySubscriptionId)
|
||||
.ReturnsNull();
|
||||
|
||||
await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetSubscription(user));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_User_Succeeds(
|
||||
User user,
|
||||
SutProvider<GetSubscriptionQuery> sutProvider)
|
||||
{
|
||||
var subscription = new Subscription();
|
||||
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(user.GatewaySubscriptionId)
|
||||
.Returns(subscription);
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscription(user);
|
||||
|
||||
Assert.Equivalent(subscription, gotSubscription);
|
||||
}
|
||||
|
||||
private static async Task ThrowsContactSupportAsync(Func<Task> function)
|
||||
{
|
||||
const string message = "Something went wrong with your request. Please contact support.";
|
||||
|
||||
var exception = await Assert.ThrowsAsync<GatewayException>(function);
|
||||
|
||||
Assert.Equal(message, exception.Message);
|
||||
}
|
||||
}
|
@ -1,154 +0,0 @@
|
||||
using Bit.Core.AdminConsole.Entities.Provider;
|
||||
using Bit.Core.AdminConsole.Repositories;
|
||||
using Bit.Core.Billing.Entities;
|
||||
using Bit.Core.Billing.Models;
|
||||
using Bit.Core.Billing.Queries;
|
||||
using Bit.Core.Billing.Queries.Implementations;
|
||||
using Bit.Core.Billing.Repositories;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Test.Common.AutoFixture;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ReturnsExtensions;
|
||||
using Stripe;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.Billing.Queries;
|
||||
|
||||
[SutProviderCustomize]
|
||||
public class ProviderBillingQueriesTests
|
||||
{
|
||||
#region GetSubscriptionData
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscriptionData_NullProvider_ReturnsNull(
|
||||
SutProvider<ProviderBillingQueries> sutProvider,
|
||||
Guid providerId)
|
||||
{
|
||||
var providerRepository = sutProvider.GetDependency<IProviderRepository>();
|
||||
|
||||
providerRepository.GetByIdAsync(providerId).ReturnsNull();
|
||||
|
||||
var subscriptionData = await sutProvider.Sut.GetSubscriptionData(providerId);
|
||||
|
||||
Assert.Null(subscriptionData);
|
||||
|
||||
await providerRepository.Received(1).GetByIdAsync(providerId);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscriptionData_NullSubscription_ReturnsNull(
|
||||
SutProvider<ProviderBillingQueries> sutProvider,
|
||||
Guid providerId,
|
||||
Provider provider)
|
||||
{
|
||||
var providerRepository = sutProvider.GetDependency<IProviderRepository>();
|
||||
|
||||
providerRepository.GetByIdAsync(providerId).Returns(provider);
|
||||
|
||||
var subscriberQueries = sutProvider.GetDependency<ISubscriberQueries>();
|
||||
|
||||
subscriberQueries.GetSubscription(provider).ReturnsNull();
|
||||
|
||||
var subscriptionData = await sutProvider.Sut.GetSubscriptionData(providerId);
|
||||
|
||||
Assert.Null(subscriptionData);
|
||||
|
||||
await providerRepository.Received(1).GetByIdAsync(providerId);
|
||||
|
||||
await subscriberQueries.Received(1).GetSubscription(
|
||||
provider,
|
||||
Arg.Is<SubscriptionGetOptions>(
|
||||
options => options.Expand.Count == 1 && options.Expand.First() == "customer"));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscriptionData_Success(
|
||||
SutProvider<ProviderBillingQueries> sutProvider,
|
||||
Guid providerId,
|
||||
Provider provider)
|
||||
{
|
||||
var providerRepository = sutProvider.GetDependency<IProviderRepository>();
|
||||
|
||||
providerRepository.GetByIdAsync(providerId).Returns(provider);
|
||||
|
||||
var subscriberQueries = sutProvider.GetDependency<ISubscriberQueries>();
|
||||
|
||||
var subscription = new Subscription();
|
||||
|
||||
subscriberQueries.GetSubscription(provider, Arg.Is<SubscriptionGetOptions>(
|
||||
options => options.Expand.Count == 1 && options.Expand.First() == "customer")).Returns(subscription);
|
||||
|
||||
var providerPlanRepository = sutProvider.GetDependency<IProviderPlanRepository>();
|
||||
|
||||
var enterprisePlan = new ProviderPlan
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
ProviderId = providerId,
|
||||
PlanType = PlanType.EnterpriseMonthly,
|
||||
SeatMinimum = 100,
|
||||
PurchasedSeats = 0,
|
||||
AllocatedSeats = 0
|
||||
};
|
||||
|
||||
var teamsPlan = new ProviderPlan
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
ProviderId = providerId,
|
||||
PlanType = PlanType.TeamsMonthly,
|
||||
SeatMinimum = 50,
|
||||
PurchasedSeats = 10,
|
||||
AllocatedSeats = 60
|
||||
};
|
||||
|
||||
var providerPlans = new List<ProviderPlan>
|
||||
{
|
||||
enterprisePlan,
|
||||
teamsPlan,
|
||||
};
|
||||
|
||||
providerPlanRepository.GetByProviderId(providerId).Returns(providerPlans);
|
||||
|
||||
var subscriptionData = await sutProvider.Sut.GetSubscriptionData(providerId);
|
||||
|
||||
Assert.NotNull(subscriptionData);
|
||||
|
||||
Assert.Equivalent(subscriptionData.Subscription, subscription);
|
||||
|
||||
Assert.Equal(2, subscriptionData.ProviderPlans.Count);
|
||||
|
||||
var configuredEnterprisePlan =
|
||||
subscriptionData.ProviderPlans.FirstOrDefault(configuredPlan =>
|
||||
configuredPlan.PlanType == PlanType.EnterpriseMonthly);
|
||||
|
||||
var configuredTeamsPlan =
|
||||
subscriptionData.ProviderPlans.FirstOrDefault(configuredPlan =>
|
||||
configuredPlan.PlanType == PlanType.TeamsMonthly);
|
||||
|
||||
Compare(enterprisePlan, configuredEnterprisePlan);
|
||||
|
||||
Compare(teamsPlan, configuredTeamsPlan);
|
||||
|
||||
await providerRepository.Received(1).GetByIdAsync(providerId);
|
||||
|
||||
await subscriberQueries.Received(1).GetSubscription(
|
||||
provider,
|
||||
Arg.Is<SubscriptionGetOptions>(
|
||||
options => options.Expand.Count == 1 && options.Expand.First() == "customer"));
|
||||
|
||||
await providerPlanRepository.Received(1).GetByProviderId(providerId);
|
||||
|
||||
return;
|
||||
|
||||
void Compare(ProviderPlan providerPlan, ConfiguredProviderPlan configuredProviderPlan)
|
||||
{
|
||||
Assert.NotNull(configuredProviderPlan);
|
||||
Assert.Equal(providerPlan.Id, configuredProviderPlan.Id);
|
||||
Assert.Equal(providerPlan.ProviderId, configuredProviderPlan.ProviderId);
|
||||
Assert.Equal(providerPlan.SeatMinimum!.Value, configuredProviderPlan.SeatMinimum);
|
||||
Assert.Equal(providerPlan.PurchasedSeats!.Value, configuredProviderPlan.PurchasedSeats);
|
||||
Assert.Equal(providerPlan.AllocatedSeats!.Value, configuredProviderPlan.AssignedSeats);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
@ -1,263 +0,0 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.AdminConsole.Entities.Provider;
|
||||
using Bit.Core.Billing.Queries.Implementations;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Test.Common.AutoFixture;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ReturnsExtensions;
|
||||
using Stripe;
|
||||
using Xunit;
|
||||
|
||||
using static Bit.Core.Test.Billing.Utilities;
|
||||
|
||||
namespace Bit.Core.Test.Billing.Queries;
|
||||
|
||||
[SutProviderCustomize]
|
||||
public class SubscriberQueriesTests
|
||||
{
|
||||
#region GetSubscription
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_NullSubscriber_ThrowsArgumentNullException(
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
=> await Assert.ThrowsAsync<ArgumentNullException>(
|
||||
async () => await sutProvider.Sut.GetSubscription(null));
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_Organization_NoGatewaySubscriptionId_ReturnsNull(
|
||||
Organization organization,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
organization.GatewaySubscriptionId = null;
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscription(organization);
|
||||
|
||||
Assert.Null(gotSubscription);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_Organization_NoSubscription_ReturnsNull(
|
||||
Organization organization,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(organization.GatewaySubscriptionId)
|
||||
.ReturnsNull();
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscription(organization);
|
||||
|
||||
Assert.Null(gotSubscription);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_Organization_Succeeds(
|
||||
Organization organization,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
var subscription = new Subscription();
|
||||
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(organization.GatewaySubscriptionId)
|
||||
.Returns(subscription);
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscription(organization);
|
||||
|
||||
Assert.Equivalent(subscription, gotSubscription);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_User_NoGatewaySubscriptionId_ReturnsNull(
|
||||
User user,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
user.GatewaySubscriptionId = null;
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscription(user);
|
||||
|
||||
Assert.Null(gotSubscription);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_User_NoSubscription_ReturnsNull(
|
||||
User user,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(user.GatewaySubscriptionId)
|
||||
.ReturnsNull();
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscription(user);
|
||||
|
||||
Assert.Null(gotSubscription);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_User_Succeeds(
|
||||
User user,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
var subscription = new Subscription();
|
||||
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(user.GatewaySubscriptionId)
|
||||
.Returns(subscription);
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscription(user);
|
||||
|
||||
Assert.Equivalent(subscription, gotSubscription);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_Provider_NoGatewaySubscriptionId_ReturnsNull(
|
||||
Provider provider,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
provider.GatewaySubscriptionId = null;
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscription(provider);
|
||||
|
||||
Assert.Null(gotSubscription);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_Provider_NoSubscription_ReturnsNull(
|
||||
Provider provider,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(provider.GatewaySubscriptionId)
|
||||
.ReturnsNull();
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscription(provider);
|
||||
|
||||
Assert.Null(gotSubscription);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscription_Provider_Succeeds(
|
||||
Provider provider,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
var subscription = new Subscription();
|
||||
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(provider.GatewaySubscriptionId)
|
||||
.Returns(subscription);
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscription(provider);
|
||||
|
||||
Assert.Equivalent(subscription, gotSubscription);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region GetSubscriptionOrThrow
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscriptionOrThrow_NullSubscriber_ThrowsArgumentNullException(
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
=> await Assert.ThrowsAsync<ArgumentNullException>(
|
||||
async () => await sutProvider.Sut.GetSubscriptionOrThrow(null));
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscriptionOrThrow_Organization_NoGatewaySubscriptionId_ThrowsGatewayException(
|
||||
Organization organization,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
organization.GatewaySubscriptionId = null;
|
||||
|
||||
await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetSubscriptionOrThrow(organization));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscriptionOrThrow_Organization_NoSubscription_ThrowsGatewayException(
|
||||
Organization organization,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(organization.GatewaySubscriptionId)
|
||||
.ReturnsNull();
|
||||
|
||||
await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetSubscriptionOrThrow(organization));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscriptionOrThrow_Organization_Succeeds(
|
||||
Organization organization,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
var subscription = new Subscription();
|
||||
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(organization.GatewaySubscriptionId)
|
||||
.Returns(subscription);
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscriptionOrThrow(organization);
|
||||
|
||||
Assert.Equivalent(subscription, gotSubscription);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscriptionOrThrow_User_NoGatewaySubscriptionId_ThrowsGatewayException(
|
||||
User user,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
user.GatewaySubscriptionId = null;
|
||||
|
||||
await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetSubscriptionOrThrow(user));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscriptionOrThrow_User_NoSubscription_ThrowsGatewayException(
|
||||
User user,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(user.GatewaySubscriptionId)
|
||||
.ReturnsNull();
|
||||
|
||||
await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetSubscriptionOrThrow(user));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscriptionOrThrow_User_Succeeds(
|
||||
User user,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
var subscription = new Subscription();
|
||||
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(user.GatewaySubscriptionId)
|
||||
.Returns(subscription);
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscriptionOrThrow(user);
|
||||
|
||||
Assert.Equivalent(subscription, gotSubscription);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscriptionOrThrow_Provider_NoGatewaySubscriptionId_ThrowsGatewayException(
|
||||
Provider provider,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
provider.GatewaySubscriptionId = null;
|
||||
|
||||
await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetSubscriptionOrThrow(provider));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscriptionOrThrow_Provider_NoSubscription_ThrowsGatewayException(
|
||||
Provider provider,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(provider.GatewaySubscriptionId)
|
||||
.ReturnsNull();
|
||||
|
||||
await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetSubscriptionOrThrow(provider));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetSubscriptionOrThrow_Provider_Succeeds(
|
||||
Provider provider,
|
||||
SutProvider<SubscriberQueries> sutProvider)
|
||||
{
|
||||
var subscription = new Subscription();
|
||||
|
||||
sutProvider.GetDependency<IStripeAdapter>().SubscriptionGetAsync(provider.GatewaySubscriptionId)
|
||||
.Returns(subscription);
|
||||
|
||||
var gotSubscription = await sutProvider.Sut.GetSubscriptionOrThrow(provider);
|
||||
|
||||
Assert.Equivalent(subscription, gotSubscription);
|
||||
}
|
||||
#endregion
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
using Bit.Core.Billing;
|
||||
using Bit.Core.Exceptions;
|
||||
using Xunit;
|
||||
|
||||
using static Bit.Core.Billing.Utilities;
|
||||
@ -11,7 +11,7 @@ public static class Utilities
|
||||
{
|
||||
var contactSupport = ContactSupport();
|
||||
|
||||
var exception = await Assert.ThrowsAsync<BillingException>(function);
|
||||
var exception = await Assert.ThrowsAsync<GatewayException>(function);
|
||||
|
||||
Assert.Equal(contactSupport.Message, exception.Message);
|
||||
}
|
||||
|
Reference in New Issue
Block a user