using Bit.Core.AdminConsole.Entities; using Bit.Core.Billing.Queries.Implementations; 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 GetCustomer [Theory, BitAutoData] public async Task GetCustomer_NullSubscriber_ThrowsArgumentNullException( SutProvider sutProvider) => await Assert.ThrowsAsync( async () => await sutProvider.Sut.GetCustomer(null)); [Theory, BitAutoData] public async Task GetCustomer_NoGatewayCustomerId_ReturnsNull( Organization organization, SutProvider sutProvider) { organization.GatewayCustomerId = null; var customer = await sutProvider.Sut.GetCustomer(organization); Assert.Null(customer); } [Theory, BitAutoData] public async Task GetCustomer_NoCustomer_ReturnsNull( Organization organization, SutProvider sutProvider) { sutProvider.GetDependency() .CustomerGetAsync(organization.GatewayCustomerId) .ReturnsNull(); var customer = await sutProvider.Sut.GetCustomer(organization); Assert.Null(customer); } [Theory, BitAutoData] public async Task GetCustomer_Succeeds( Organization organization, SutProvider sutProvider) { var customer = new Customer(); sutProvider.GetDependency() .CustomerGetAsync(organization.GatewayCustomerId) .Returns(customer); var gotCustomer = await sutProvider.Sut.GetCustomer(organization); Assert.Equivalent(customer, gotCustomer); } #endregion #region GetSubscription [Theory, BitAutoData] public async Task GetSubscription_NullSubscriber_ThrowsArgumentNullException( SutProvider sutProvider) => await Assert.ThrowsAsync( async () => await sutProvider.Sut.GetSubscription(null)); [Theory, BitAutoData] public async Task GetSubscription_NoGatewaySubscriptionId_ReturnsNull( Organization organization, SutProvider sutProvider) { organization.GatewaySubscriptionId = null; var subscription = await sutProvider.Sut.GetSubscription(organization); Assert.Null(subscription); } [Theory, BitAutoData] public async Task GetSubscription_NoSubscription_ReturnsNull( Organization organization, SutProvider sutProvider) { sutProvider.GetDependency() .SubscriptionGetAsync(organization.GatewaySubscriptionId) .ReturnsNull(); var subscription = await sutProvider.Sut.GetSubscription(organization); Assert.Null(subscription); } [Theory, BitAutoData] public async Task GetSubscription_Succeeds( Organization organization, SutProvider sutProvider) { var subscription = new Subscription(); sutProvider.GetDependency() .SubscriptionGetAsync(organization.GatewaySubscriptionId) .Returns(subscription); var gotSubscription = await sutProvider.Sut.GetSubscription(organization); Assert.Equivalent(subscription, gotSubscription); } #endregion #region GetCustomerOrThrow [Theory, BitAutoData] public async Task GetCustomerOrThrow_NullSubscriber_ThrowsArgumentNullException( SutProvider sutProvider) => await Assert.ThrowsAsync( async () => await sutProvider.Sut.GetCustomerOrThrow(null)); [Theory, BitAutoData] public async Task GetCustomerOrThrow_NoGatewaySubscriptionId_ThrowsGatewayException( Organization organization, SutProvider sutProvider) { organization.GatewayCustomerId = null; await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetCustomerOrThrow(organization)); } [Theory, BitAutoData] public async Task GetSubscriptionOrThrow_NoCustomer_ThrowsGatewayException( Organization organization, SutProvider sutProvider) { sutProvider.GetDependency() .CustomerGetAsync(organization.GatewayCustomerId) .ReturnsNull(); await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetCustomerOrThrow(organization)); } [Theory, BitAutoData] public async Task GetCustomerOrThrow_Succeeds( Organization organization, SutProvider sutProvider) { var customer = new Customer(); sutProvider.GetDependency() .CustomerGetAsync(organization.GatewayCustomerId) .Returns(customer); var gotCustomer = await sutProvider.Sut.GetCustomerOrThrow(organization); Assert.Equivalent(customer, gotCustomer); } #endregion #region GetSubscriptionOrThrow [Theory, BitAutoData] public async Task GetSubscriptionOrThrow_NullSubscriber_ThrowsArgumentNullException( SutProvider sutProvider) => await Assert.ThrowsAsync( async () => await sutProvider.Sut.GetSubscriptionOrThrow(null)); [Theory, BitAutoData] public async Task GetSubscriptionOrThrow_NoGatewaySubscriptionId_ThrowsGatewayException( Organization organization, SutProvider sutProvider) { organization.GatewaySubscriptionId = null; await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetSubscriptionOrThrow(organization)); } [Theory, BitAutoData] public async Task GetSubscriptionOrThrow_NoSubscription_ThrowsGatewayException( Organization organization, SutProvider sutProvider) { sutProvider.GetDependency() .SubscriptionGetAsync(organization.GatewaySubscriptionId) .ReturnsNull(); await ThrowsContactSupportAsync(async () => await sutProvider.Sut.GetSubscriptionOrThrow(organization)); } [Theory, BitAutoData] public async Task GetSubscriptionOrThrow_Succeeds( Organization organization, SutProvider sutProvider) { var subscription = new Subscription(); sutProvider.GetDependency() .SubscriptionGetAsync(organization.GatewaySubscriptionId) .Returns(subscription); var gotSubscription = await sutProvider.Sut.GetSubscriptionOrThrow(organization); Assert.Equivalent(subscription, gotSubscription); } #endregion }