2024-04-16 19:55:00 +02:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using Bit.Api.Billing.Controllers;
|
|
|
|
|
using Bit.Api.Billing.Models.Requests;
|
|
|
|
|
using Bit.Core.AdminConsole.Entities;
|
|
|
|
|
using Bit.Core.AdminConsole.Entities.Provider;
|
|
|
|
|
using Bit.Core.AdminConsole.Repositories;
|
|
|
|
|
using Bit.Core.AdminConsole.Services;
|
2024-05-23 16:17:00 +02:00
|
|
|
|
using Bit.Core.Billing.Services;
|
2024-04-16 19:55:00 +02:00
|
|
|
|
using Bit.Core.Entities;
|
|
|
|
|
using Bit.Core.Models.Business;
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using NSubstitute.ReturnsExtensions;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2024-06-24 17:15:47 +02:00
|
|
|
|
using static Bit.Api.Test.Billing.Utilities;
|
|
|
|
|
|
2024-04-16 19:55:00 +02:00
|
|
|
|
namespace Bit.Api.Test.Billing.Controllers;
|
|
|
|
|
|
|
|
|
|
[ControllerCustomize(typeof(ProviderClientsController))]
|
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
public class ProviderClientsControllerTests
|
|
|
|
|
{
|
|
|
|
|
#region CreateAsync
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task CreateAsync_NoPrincipalUser_Unauthorized(
|
2024-06-24 17:15:47 +02:00
|
|
|
|
Provider provider,
|
2024-04-16 19:55:00 +02:00
|
|
|
|
CreateClientOrganizationRequestBody requestBody,
|
|
|
|
|
SutProvider<ProviderClientsController> sutProvider)
|
|
|
|
|
{
|
2024-08-28 16:48:14 +02:00
|
|
|
|
ConfigureStableProviderAdminInputs(provider, sutProvider);
|
2024-04-16 19:55:00 +02:00
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).ReturnsNull();
|
|
|
|
|
|
2024-06-24 17:15:47 +02:00
|
|
|
|
var result = await sutProvider.Sut.CreateAsync(provider.Id, requestBody);
|
2024-04-16 19:55:00 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
AssertUnauthorized(result);
|
2024-04-16 19:55:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task CreateAsync_OK(
|
2024-06-24 17:15:47 +02:00
|
|
|
|
Provider provider,
|
2024-04-16 19:55:00 +02:00
|
|
|
|
CreateClientOrganizationRequestBody requestBody,
|
|
|
|
|
SutProvider<ProviderClientsController> sutProvider)
|
|
|
|
|
{
|
2024-08-28 16:48:14 +02:00
|
|
|
|
ConfigureStableProviderAdminInputs(provider, sutProvider);
|
2024-04-16 19:55:00 +02:00
|
|
|
|
|
|
|
|
|
var user = new User();
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>())
|
|
|
|
|
.Returns(user);
|
|
|
|
|
|
|
|
|
|
var clientOrganizationId = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IProviderService>().CreateOrganizationAsync(
|
2024-06-24 17:15:47 +02:00
|
|
|
|
provider.Id,
|
2024-04-16 19:55:00 +02:00
|
|
|
|
Arg.Is<OrganizationSignup>(signup =>
|
|
|
|
|
signup.Name == requestBody.Name &&
|
|
|
|
|
signup.Plan == requestBody.PlanType &&
|
|
|
|
|
signup.AdditionalSeats == requestBody.Seats &&
|
|
|
|
|
signup.OwnerKey == requestBody.Key &&
|
|
|
|
|
signup.PublicKey == requestBody.KeyPair.PublicKey &&
|
|
|
|
|
signup.PrivateKey == requestBody.KeyPair.EncryptedPrivateKey &&
|
|
|
|
|
signup.CollectionName == requestBody.CollectionName),
|
|
|
|
|
requestBody.OwnerEmail,
|
|
|
|
|
user)
|
|
|
|
|
.Returns(new ProviderOrganization
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = clientOrganizationId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var clientOrganization = new Organization { Id = clientOrganizationId };
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(clientOrganizationId)
|
|
|
|
|
.Returns(clientOrganization);
|
|
|
|
|
|
2024-06-24 17:15:47 +02:00
|
|
|
|
var result = await sutProvider.Sut.CreateAsync(provider.Id, requestBody);
|
2024-04-16 19:55:00 +02:00
|
|
|
|
|
|
|
|
|
Assert.IsType<Ok>(result);
|
|
|
|
|
|
2024-05-23 16:17:00 +02:00
|
|
|
|
await sutProvider.GetDependency<IProviderBillingService>().Received(1).CreateCustomerForClientOrganization(
|
2024-04-16 19:55:00 +02:00
|
|
|
|
provider,
|
|
|
|
|
clientOrganization);
|
|
|
|
|
}
|
2024-06-24 17:15:47 +02:00
|
|
|
|
|
2024-04-16 19:55:00 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region UpdateAsync
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task UpdateAsync_NoProviderOrganization_NotFound(
|
2024-06-24 17:15:47 +02:00
|
|
|
|
Provider provider,
|
2024-04-16 19:55:00 +02:00
|
|
|
|
Guid providerOrganizationId,
|
|
|
|
|
UpdateClientOrganizationRequestBody requestBody,
|
|
|
|
|
SutProvider<ProviderClientsController> sutProvider)
|
|
|
|
|
{
|
2024-08-28 16:48:14 +02:00
|
|
|
|
ConfigureStableProviderServiceUserInputs(provider, sutProvider);
|
2024-04-16 19:55:00 +02:00
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IProviderOrganizationRepository>().GetByIdAsync(providerOrganizationId)
|
|
|
|
|
.ReturnsNull();
|
|
|
|
|
|
2024-06-24 17:15:47 +02:00
|
|
|
|
var result = await sutProvider.Sut.UpdateAsync(provider.Id, providerOrganizationId, requestBody);
|
2024-04-16 19:55:00 +02:00
|
|
|
|
|
2024-07-31 15:26:44 +02:00
|
|
|
|
AssertNotFound(result);
|
2024-04-16 19:55:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2024-07-31 15:26:44 +02:00
|
|
|
|
public async Task UpdateAsync_AssignedSeats_Ok(
|
2024-06-24 17:15:47 +02:00
|
|
|
|
Provider provider,
|
2024-04-16 19:55:00 +02:00
|
|
|
|
Guid providerOrganizationId,
|
|
|
|
|
UpdateClientOrganizationRequestBody requestBody,
|
|
|
|
|
ProviderOrganization providerOrganization,
|
|
|
|
|
Organization organization,
|
|
|
|
|
SutProvider<ProviderClientsController> sutProvider)
|
|
|
|
|
{
|
2024-08-28 16:48:14 +02:00
|
|
|
|
ConfigureStableProviderServiceUserInputs(provider, sutProvider);
|
2024-04-16 19:55:00 +02:00
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IProviderOrganizationRepository>().GetByIdAsync(providerOrganizationId)
|
|
|
|
|
.Returns(providerOrganization);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(providerOrganization.OrganizationId)
|
|
|
|
|
.Returns(organization);
|
|
|
|
|
|
2024-06-24 17:15:47 +02:00
|
|
|
|
var result = await sutProvider.Sut.UpdateAsync(provider.Id, providerOrganizationId, requestBody);
|
2024-04-16 19:55:00 +02:00
|
|
|
|
|
2024-05-23 16:17:00 +02:00
|
|
|
|
await sutProvider.GetDependency<IProviderBillingService>().Received(1)
|
2024-04-16 19:55:00 +02:00
|
|
|
|
.AssignSeatsToClientOrganization(
|
|
|
|
|
provider,
|
|
|
|
|
organization,
|
|
|
|
|
requestBody.AssignedSeats);
|
|
|
|
|
|
2024-05-14 17:26:08 +02:00
|
|
|
|
await sutProvider.GetDependency<IOrganizationRepository>().Received(1)
|
|
|
|
|
.ReplaceAsync(Arg.Is<Organization>(org => org.Name == requestBody.Name));
|
|
|
|
|
|
|
|
|
|
Assert.IsType<Ok>(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2024-07-31 15:26:44 +02:00
|
|
|
|
public async Task UpdateAsync_Name_Ok(
|
2024-06-24 17:15:47 +02:00
|
|
|
|
Provider provider,
|
2024-05-14 17:26:08 +02:00
|
|
|
|
Guid providerOrganizationId,
|
|
|
|
|
UpdateClientOrganizationRequestBody requestBody,
|
|
|
|
|
ProviderOrganization providerOrganization,
|
|
|
|
|
Organization organization,
|
|
|
|
|
SutProvider<ProviderClientsController> sutProvider)
|
|
|
|
|
{
|
2024-08-28 16:48:14 +02:00
|
|
|
|
ConfigureStableProviderServiceUserInputs(provider, sutProvider);
|
2024-05-14 17:26:08 +02:00
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IProviderOrganizationRepository>().GetByIdAsync(providerOrganizationId)
|
|
|
|
|
.Returns(providerOrganization);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(providerOrganization.OrganizationId)
|
|
|
|
|
.Returns(organization);
|
|
|
|
|
|
|
|
|
|
requestBody.AssignedSeats = organization.Seats!.Value;
|
|
|
|
|
|
2024-06-24 17:15:47 +02:00
|
|
|
|
var result = await sutProvider.Sut.UpdateAsync(provider.Id, providerOrganizationId, requestBody);
|
2024-05-14 17:26:08 +02:00
|
|
|
|
|
2024-05-23 16:17:00 +02:00
|
|
|
|
await sutProvider.GetDependency<IProviderBillingService>().DidNotReceiveWithAnyArgs()
|
2024-05-14 17:26:08 +02:00
|
|
|
|
.AssignSeatsToClientOrganization(
|
|
|
|
|
Arg.Any<Provider>(),
|
|
|
|
|
Arg.Any<Organization>(),
|
|
|
|
|
Arg.Any<int>());
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IOrganizationRepository>().Received(1)
|
|
|
|
|
.ReplaceAsync(Arg.Is<Organization>(org => org.Name == requestBody.Name));
|
|
|
|
|
|
2024-04-16 19:55:00 +02:00
|
|
|
|
Assert.IsType<Ok>(result);
|
|
|
|
|
}
|
2024-06-24 17:15:47 +02:00
|
|
|
|
|
2024-04-16 19:55:00 +02:00
|
|
|
|
#endregion
|
|
|
|
|
}
|