2024-04-24 22:29:04 +02:00
|
|
|
|
using Bit.Api.Billing.Controllers;
|
|
|
|
|
using Bit.Api.Billing.Models.Responses;
|
2024-07-17 22:15:28 +02:00
|
|
|
|
using Bit.Core.AdminConsole.Entities;
|
2024-04-24 22:29:04 +02:00
|
|
|
|
using Bit.Core.Billing.Models;
|
2024-05-23 16:17:00 +02:00
|
|
|
|
using Bit.Core.Billing.Services;
|
2024-07-17 22:15:28 +02:00
|
|
|
|
using Bit.Core.Context;
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Core.Services;
|
2024-04-24 22:29:04 +02:00
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2024-08-28 16:48:14 +02:00
|
|
|
|
using static Bit.Api.Test.Billing.Utilities;
|
|
|
|
|
|
2024-04-24 22:29:04 +02:00
|
|
|
|
namespace Bit.Api.Test.Billing.Controllers;
|
|
|
|
|
|
|
|
|
|
[ControllerCustomize(typeof(OrganizationBillingController))]
|
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
public class OrganizationBillingControllerTests
|
|
|
|
|
{
|
2024-07-17 22:15:28 +02:00
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task GetMetadataAsync_Unauthorized_ReturnsUnauthorized(
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
SutProvider<OrganizationBillingController> sutProvider)
|
|
|
|
|
{
|
2024-07-19 16:24:48 +02:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessMembersTab(organizationId).Returns(false);
|
2024-07-17 22:15:28 +02:00
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetMetadataAsync(organizationId);
|
|
|
|
|
|
2024-08-28 16:48:14 +02:00
|
|
|
|
AssertUnauthorized(result);
|
2024-07-17 22:15:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-24 22:29:04 +02: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 16:48:14 +02:00
|
|
|
|
sutProvider.GetDependency<IOrganizationBillingService>().GetMetadata(organizationId).Returns((OrganizationMetadata)null);
|
2024-07-17 22:15:28 +02:00
|
|
|
|
|
2024-04-24 22:29:04 +02:00
|
|
|
|
var result = await sutProvider.Sut.GetMetadataAsync(organizationId);
|
|
|
|
|
|
2024-08-28 16:48:14 +02:00
|
|
|
|
AssertNotFound(result);
|
2024-04-24 22:29:04 +02: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 16:17:00 +02:00
|
|
|
|
sutProvider.GetDependency<IOrganizationBillingService>().GetMetadata(organizationId)
|
2024-11-13 13:19:16 +01:00
|
|
|
|
.Returns(new OrganizationMetadata(true, true, true, true, true));
|
2024-04-24 22:29:04 +02:00
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetMetadataAsync(organizationId);
|
|
|
|
|
|
|
|
|
|
Assert.IsType<Ok<OrganizationMetadataResponse>>(result);
|
|
|
|
|
|
2024-10-31 16:01:37 +01:00
|
|
|
|
var response = ((Ok<OrganizationMetadataResponse>)result).Value;
|
2024-04-24 22:29:04 +02:00
|
|
|
|
|
2024-10-31 16:01:37 +01:00
|
|
|
|
Assert.True(response.IsEligibleForSelfHost);
|
|
|
|
|
Assert.True(response.IsManaged);
|
|
|
|
|
Assert.True(response.IsOnSecretsManagerStandalone);
|
2024-11-04 15:15:27 +01:00
|
|
|
|
Assert.True(response.IsSubscriptionUnpaid);
|
2024-11-13 13:19:16 +01:00
|
|
|
|
Assert.True(response.HasSubscription);
|
2024-04-24 22:29:04 +02:00
|
|
|
|
}
|
2024-07-17 22:15:28 +02: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 16:48:14 +02:00
|
|
|
|
AssertUnauthorized(result);
|
2024-07-17 22:15:28 +02: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 16:48:14 +02:00
|
|
|
|
AssertNotFound(result);
|
2024-07-17 22:15:28 +02: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();
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IPaymentService>().GetBillingHistoryAsync(organization).Returns(billingInfo);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await sutProvider.Sut.GetHistoryAsync(organizationId);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
var okResult = Assert.IsType<Ok<BillingHistoryInfo>>(result);
|
|
|
|
|
Assert.Equal(billingInfo, okResult.Value);
|
|
|
|
|
}
|
2024-04-24 22:29:04 +02:00
|
|
|
|
}
|