2024-04-24 16:29:04 -04:00
|
|
|
|
using Bit.Api.Billing.Controllers;
|
2024-07-17 16:15:28 -04:00
|
|
|
|
using Bit.Core.AdminConsole.Entities;
|
2024-04-24 16:29:04 -04:00
|
|
|
|
using Bit.Core.Billing.Models;
|
2025-07-17 12:02:25 -05:00
|
|
|
|
using Bit.Core.Billing.Organizations.Models;
|
|
|
|
|
|
using Bit.Core.Billing.Organizations.Services;
|
2025-12-12 15:32:43 -06:00
|
|
|
|
using Bit.Core.Billing.Services;
|
2024-07-17 16:15:28 -04:00
|
|
|
|
using Bit.Core.Context;
|
|
|
|
|
|
using Bit.Core.Repositories;
|
2024-04-24 16:29:04 -04:00
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
2024-08-28 10:48:14 -04:00
|
|
|
|
using static Bit.Api.Test.Billing.Utilities;
|
|
|
|
|
|
|
2024-04-24 16:29:04 -04:00
|
|
|
|
namespace Bit.Api.Test.Billing.Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
[ControllerCustomize(typeof(OrganizationBillingController))]
|
|
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
|
public class OrganizationBillingControllerTests
|
|
|
|
|
|
{
|
2024-07-17 16:15:28 -04:00
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
|
public async Task GetMetadataAsync_Unauthorized_ReturnsUnauthorized(
|
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
|
SutProvider<OrganizationBillingController> sutProvider)
|
|
|
|
|
|
{
|
2024-07-19 10:24:48 -04:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessMembersTab(organizationId).Returns(false);
|
2024-07-17 16:15:28 -04:00
|
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetMetadataAsync(organizationId);
|
|
|
|
|
|
|
2024-08-28 10:48:14 -04:00
|
|
|
|
AssertUnauthorized(result);
|
2024-07-17 16:15:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-24 16:29:04 -04:00
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
|
public async Task GetMetadataAsync_MetadataNull_NotFound(
|
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
|
SutProvider<OrganizationBillingController> sutProvider)
|
|
|
|
|
|
{
|
2024-11-04 15:15:27 +01:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationUser(organizationId).Returns(true);
|
2024-08-28 10:48:14 -04:00
|
|
|
|
sutProvider.GetDependency<IOrganizationBillingService>().GetMetadata(organizationId).Returns((OrganizationMetadata)null);
|
2024-07-17 16:15:28 -04:00
|
|
|
|
|
2024-04-24 16:29:04 -04:00
|
|
|
|
var result = await sutProvider.Sut.GetMetadataAsync(organizationId);
|
|
|
|
|
|
|
2024-08-28 10:48:14 -04:00
|
|
|
|
AssertNotFound(result);
|
2024-04-24 16:29:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
|
public async Task GetMetadataAsync_OK(
|
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
|
SutProvider<OrganizationBillingController> sutProvider)
|
|
|
|
|
|
{
|
2024-11-04 15:15:27 +01:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationUser(organizationId).Returns(true);
|
2024-05-23 10:17:00 -04:00
|
|
|
|
sutProvider.GetDependency<IOrganizationBillingService>().GetMetadata(organizationId)
|
2025-10-13 10:49:55 -05:00
|
|
|
|
.Returns(new OrganizationMetadata(true, 10));
|
2024-04-24 16:29:04 -04:00
|
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetMetadataAsync(organizationId);
|
|
|
|
|
|
|
2025-10-13 10:49:55 -05:00
|
|
|
|
Assert.IsType<Ok<OrganizationMetadata>>(result);
|
2024-04-24 16:29:04 -04:00
|
|
|
|
|
2025-10-13 10:49:55 -05:00
|
|
|
|
var response = ((Ok<OrganizationMetadata>)result).Value;
|
2024-04-24 16:29:04 -04:00
|
|
|
|
|
2024-10-31 11:01:37 -04:00
|
|
|
|
Assert.True(response.IsOnSecretsManagerStandalone);
|
2025-10-13 10:49:55 -05:00
|
|
|
|
Assert.Equal(10, response.OrganizationOccupiedSeats);
|
2024-04-24 16:29:04 -04:00
|
|
|
|
}
|
2024-07-17 16:15:28 -04:00
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
|
public async Task GetHistoryAsync_Unauthorized_ReturnsUnauthorized(
|
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
|
SutProvider<OrganizationBillingController> sutProvider)
|
|
|
|
|
|
{
|
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ViewBillingHistory(organizationId).Returns(false);
|
|
|
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetHistoryAsync(organizationId);
|
|
|
|
|
|
|
2024-08-28 10:48:14 -04:00
|
|
|
|
AssertUnauthorized(result);
|
2024-07-17 16:15:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
|
public async Task GetHistoryAsync_OrganizationNotFound_ReturnsNotFound(
|
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
|
SutProvider<OrganizationBillingController> sutProvider)
|
|
|
|
|
|
{
|
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ViewBillingHistory(organizationId).Returns(true);
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organizationId).Returns((Organization)null);
|
|
|
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetHistoryAsync(organizationId);
|
|
|
|
|
|
|
2024-08-28 10:48:14 -04:00
|
|
|
|
AssertNotFound(result);
|
2024-07-17 16:15:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
|
public async Task GetHistoryAsync_OK(
|
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
|
Organization organization,
|
|
|
|
|
|
SutProvider<OrganizationBillingController> sutProvider)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ViewBillingHistory(organizationId).Returns(true);
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organizationId).Returns(organization);
|
|
|
|
|
|
|
|
|
|
|
|
// Manually create a BillingHistoryInfo object to avoid requiring AutoFixture to create HttpResponseHeaders
|
|
|
|
|
|
var billingInfo = new BillingHistoryInfo();
|
|
|
|
|
|
|
2025-12-12 15:32:43 -06:00
|
|
|
|
sutProvider.GetDependency<IStripePaymentService>().GetBillingHistoryAsync(organization).Returns(billingInfo);
|
2024-07-17 16:15:28 -04:00
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
var result = await sutProvider.Sut.GetHistoryAsync(organizationId);
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
var okResult = Assert.IsType<Ok<BillingHistoryInfo>>(result);
|
|
|
|
|
|
Assert.Equal(billingInfo, okResult.Value);
|
|
|
|
|
|
}
|
2024-04-24 16:29:04 -04:00
|
|
|
|
}
|