2022-06-30 01:46:41 +02:00
|
|
|
|
using System.Security.Claims;
|
2021-11-15 10:46:13 +01:00
|
|
|
|
using AutoFixture.Xunit2;
|
2023-10-18 17:27:56 +02:00
|
|
|
|
using Bit.Api.AdminConsole.Controllers;
|
2024-04-19 16:09:18 +02:00
|
|
|
|
using Bit.Api.Auth.Models.Request.Accounts;
|
|
|
|
|
using Bit.Core;
|
2023-11-29 00:18:08 +01:00
|
|
|
|
using Bit.Core.AdminConsole.Entities;
|
2024-04-19 16:09:18 +02:00
|
|
|
|
using Bit.Core.AdminConsole.Enums.Provider;
|
2024-05-22 18:59:19 +02:00
|
|
|
|
using Bit.Core.AdminConsole.Models.Business.Tokenables;
|
2023-10-26 23:47:44 +02:00
|
|
|
|
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationApiKeys.Interfaces;
|
2024-10-16 11:33:00 +02:00
|
|
|
|
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces;
|
2023-10-26 19:38:29 +02:00
|
|
|
|
using Bit.Core.AdminConsole.Repositories;
|
2023-04-14 19:25:56 +02:00
|
|
|
|
using Bit.Core.Auth.Entities;
|
2023-05-10 21:52:08 +02:00
|
|
|
|
using Bit.Core.Auth.Enums;
|
2023-04-14 19:25:56 +02:00
|
|
|
|
using Bit.Core.Auth.Models.Data;
|
|
|
|
|
using Bit.Core.Auth.Repositories;
|
|
|
|
|
using Bit.Core.Auth.Services;
|
2024-06-14 21:34:47 +02:00
|
|
|
|
using Bit.Core.Billing.Enums;
|
2024-05-23 16:17:00 +02:00
|
|
|
|
using Bit.Core.Billing.Services;
|
2021-11-15 10:46:13 +01:00
|
|
|
|
using Bit.Core.Context;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2023-11-01 16:05:04 +01:00
|
|
|
|
using Bit.Core.Enums;
|
2021-11-15 10:46:13 +01:00
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Core.Services;
|
2024-05-22 18:59:19 +02:00
|
|
|
|
using Bit.Core.Tokens;
|
2024-04-19 16:09:18 +02:00
|
|
|
|
using Bit.Infrastructure.EntityFramework.AdminConsole.Models.Provider;
|
2021-11-15 10:46:13 +01:00
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
2024-01-25 07:57:57 +01:00
|
|
|
|
using GlobalSettings = Bit.Core.Settings.GlobalSettings;
|
2021-11-15 10:46:13 +01:00
|
|
|
|
|
2023-10-18 17:27:56 +02:00
|
|
|
|
namespace Bit.Api.Test.AdminConsole.Controllers;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
|
|
|
|
public class OrganizationsControllerTests : IDisposable
|
2021-11-15 10:46:13 +01:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
|
|
|
|
private readonly ICurrentContext _currentContext;
|
|
|
|
|
private readonly IOrganizationRepository _organizationRepository;
|
|
|
|
|
private readonly IOrganizationService _organizationService;
|
|
|
|
|
private readonly IOrganizationUserRepository _organizationUserRepository;
|
|
|
|
|
private readonly IPolicyRepository _policyRepository;
|
|
|
|
|
private readonly ISsoConfigRepository _ssoConfigRepository;
|
|
|
|
|
private readonly ISsoConfigService _ssoConfigService;
|
|
|
|
|
private readonly IUserService _userService;
|
2022-11-29 01:39:09 +01:00
|
|
|
|
private readonly IGetOrganizationApiKeyQuery _getOrganizationApiKeyQuery;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
private readonly IRotateOrganizationApiKeyCommand _rotateOrganizationApiKeyCommand;
|
|
|
|
|
private readonly IOrganizationApiKeyRepository _organizationApiKeyRepository;
|
2022-11-29 01:39:09 +01:00
|
|
|
|
private readonly ICreateOrganizationApiKeyCommand _createOrganizationApiKeyCommand;
|
2023-05-10 21:52:08 +02:00
|
|
|
|
private readonly IFeatureService _featureService;
|
2024-02-09 01:27:47 +01:00
|
|
|
|
private readonly IPushNotificationService _pushNotificationService;
|
2024-04-19 16:09:18 +02:00
|
|
|
|
private readonly IProviderRepository _providerRepository;
|
2024-05-23 16:17:00 +02:00
|
|
|
|
private readonly IProviderBillingService _providerBillingService;
|
2024-05-22 18:59:19 +02:00
|
|
|
|
private readonly IDataProtectorTokenFactory<OrgDeleteTokenable> _orgDeleteTokenDataFactory;
|
2024-10-16 11:33:00 +02:00
|
|
|
|
private readonly IRemoveOrganizationUserCommand _removeOrganizationUserCommand;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
private readonly OrganizationsController _sut;
|
|
|
|
|
|
|
|
|
|
public OrganizationsControllerTests()
|
2021-11-15 10:46:13 +01:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
_currentContext = Substitute.For<ICurrentContext>();
|
|
|
|
|
_globalSettings = Substitute.For<GlobalSettings>();
|
|
|
|
|
_organizationRepository = Substitute.For<IOrganizationRepository>();
|
|
|
|
|
_organizationService = Substitute.For<IOrganizationService>();
|
|
|
|
|
_organizationUserRepository = Substitute.For<IOrganizationUserRepository>();
|
|
|
|
|
_policyRepository = Substitute.For<IPolicyRepository>();
|
|
|
|
|
_ssoConfigRepository = Substitute.For<ISsoConfigRepository>();
|
|
|
|
|
_ssoConfigService = Substitute.For<ISsoConfigService>();
|
2022-11-29 01:39:09 +01:00
|
|
|
|
_getOrganizationApiKeyQuery = Substitute.For<IGetOrganizationApiKeyQuery>();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
_rotateOrganizationApiKeyCommand = Substitute.For<IRotateOrganizationApiKeyCommand>();
|
|
|
|
|
_organizationApiKeyRepository = Substitute.For<IOrganizationApiKeyRepository>();
|
|
|
|
|
_userService = Substitute.For<IUserService>();
|
2022-11-29 01:39:09 +01:00
|
|
|
|
_createOrganizationApiKeyCommand = Substitute.For<ICreateOrganizationApiKeyCommand>();
|
2023-05-10 21:52:08 +02:00
|
|
|
|
_featureService = Substitute.For<IFeatureService>();
|
2024-02-09 01:27:47 +01:00
|
|
|
|
_pushNotificationService = Substitute.For<IPushNotificationService>();
|
2024-04-19 16:09:18 +02:00
|
|
|
|
_providerRepository = Substitute.For<IProviderRepository>();
|
2024-05-23 16:17:00 +02:00
|
|
|
|
_providerBillingService = Substitute.For<IProviderBillingService>();
|
2024-05-22 18:59:19 +02:00
|
|
|
|
_orgDeleteTokenDataFactory = Substitute.For<IDataProtectorTokenFactory<OrgDeleteTokenable>>();
|
2024-10-16 11:33:00 +02:00
|
|
|
|
_removeOrganizationUserCommand = Substitute.For<IRemoveOrganizationUserCommand>();
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2024-01-12 16:38:47 +01:00
|
|
|
|
_sut = new OrganizationsController(
|
|
|
|
|
_organizationRepository,
|
|
|
|
|
_organizationUserRepository,
|
|
|
|
|
_policyRepository,
|
|
|
|
|
_organizationService,
|
|
|
|
|
_userService,
|
|
|
|
|
_currentContext,
|
|
|
|
|
_ssoConfigRepository,
|
|
|
|
|
_ssoConfigService,
|
|
|
|
|
_getOrganizationApiKeyQuery,
|
|
|
|
|
_rotateOrganizationApiKeyCommand,
|
|
|
|
|
_createOrganizationApiKeyCommand,
|
|
|
|
|
_organizationApiKeyRepository,
|
|
|
|
|
_featureService,
|
|
|
|
|
_globalSettings,
|
2024-02-09 17:58:37 +01:00
|
|
|
|
_pushNotificationService,
|
2024-04-19 16:09:18 +02:00
|
|
|
|
_providerRepository,
|
2024-05-23 16:17:00 +02:00
|
|
|
|
_providerBillingService,
|
2024-10-16 11:33:00 +02:00
|
|
|
|
_orgDeleteTokenDataFactory,
|
|
|
|
|
_removeOrganizationUserCommand);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_sut?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task OrganizationsController_UserCannotLeaveOrganizationThatProvidesKeyConnector(
|
|
|
|
|
Guid orgId, User user)
|
|
|
|
|
{
|
|
|
|
|
var ssoConfig = new SsoConfig
|
2021-11-15 10:46:13 +01:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Id = default,
|
|
|
|
|
Data = new SsoConfigurationData
|
2021-11-15 10:46:13 +01:00
|
|
|
|
{
|
2023-05-10 21:52:08 +02:00
|
|
|
|
MemberDecryptionType = MemberDecryptionType.KeyConnector
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}.Serialize(),
|
|
|
|
|
Enabled = true,
|
|
|
|
|
OrganizationId = orgId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
user.UsesKeyConnector = true;
|
|
|
|
|
|
|
|
|
|
_currentContext.OrganizationUser(orgId).Returns(true);
|
|
|
|
|
_ssoConfigRepository.GetByOrganizationIdAsync(orgId).Returns(ssoConfig);
|
|
|
|
|
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
|
2024-11-07 20:10:00 +01:00
|
|
|
|
_featureService.IsEnabled(FeatureFlagKeys.AccountDeprovisioning).Returns(true);
|
|
|
|
|
_userService.GetOrganizationsManagingUserAsync(user.Id).Returns(new List<Organization> { null });
|
2024-10-16 11:33:00 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(() => _sut.Leave(orgId));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
|
|
|
|
Assert.Contains("Your organization's Single Sign-On settings prevent you from leaving.",
|
|
|
|
|
exception.Message);
|
|
|
|
|
|
2024-10-16 11:33:00 +02:00
|
|
|
|
await _removeOrganizationUserCommand.DidNotReceiveWithAnyArgs().RemoveUserAsync(default, default);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 20:10:00 +01:00
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task OrganizationsController_UserCannotLeaveOrganizationThatManagesUser(
|
|
|
|
|
Guid orgId, User user)
|
|
|
|
|
{
|
|
|
|
|
var ssoConfig = new SsoConfig
|
|
|
|
|
{
|
|
|
|
|
Id = default,
|
|
|
|
|
Data = new SsoConfigurationData
|
|
|
|
|
{
|
|
|
|
|
MemberDecryptionType = MemberDecryptionType.KeyConnector
|
|
|
|
|
}.Serialize(),
|
|
|
|
|
Enabled = true,
|
|
|
|
|
OrganizationId = orgId,
|
|
|
|
|
};
|
|
|
|
|
var foundOrg = new Organization();
|
|
|
|
|
foundOrg.Id = orgId;
|
|
|
|
|
|
|
|
|
|
_currentContext.OrganizationUser(orgId).Returns(true);
|
|
|
|
|
_ssoConfigRepository.GetByOrganizationIdAsync(orgId).Returns(ssoConfig);
|
|
|
|
|
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
|
|
|
|
|
_featureService.IsEnabled(FeatureFlagKeys.AccountDeprovisioning).Returns(true);
|
|
|
|
|
_userService.GetOrganizationsManagingUserAsync(user.Id).Returns(new List<Organization> { { foundOrg } });
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(() => _sut.Leave(orgId));
|
|
|
|
|
|
|
|
|
|
Assert.Contains("Managed user account cannot leave managing organization. Contact your organization administrator for additional details.",
|
|
|
|
|
exception.Message);
|
|
|
|
|
|
|
|
|
|
await _removeOrganizationUserCommand.DidNotReceiveWithAnyArgs().RemoveUserAsync(default, default);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineAutoData(true, false)]
|
|
|
|
|
[InlineAutoData(false, true)]
|
|
|
|
|
[InlineAutoData(false, false)]
|
|
|
|
|
public async Task OrganizationsController_UserCanLeaveOrganizationThatDoesntProvideKeyConnector(
|
|
|
|
|
bool keyConnectorEnabled, bool userUsesKeyConnector, Guid orgId, User user)
|
|
|
|
|
{
|
|
|
|
|
var ssoConfig = new SsoConfig
|
2021-11-18 12:15:22 +01:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Id = default,
|
|
|
|
|
Data = new SsoConfigurationData
|
2021-11-18 12:15:22 +01:00
|
|
|
|
{
|
2023-05-10 21:52:08 +02:00
|
|
|
|
MemberDecryptionType = keyConnectorEnabled
|
|
|
|
|
? MemberDecryptionType.KeyConnector
|
|
|
|
|
: MemberDecryptionType.MasterPassword
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}.Serialize(),
|
|
|
|
|
Enabled = true,
|
|
|
|
|
OrganizationId = orgId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
user.UsesKeyConnector = userUsesKeyConnector;
|
|
|
|
|
|
|
|
|
|
_currentContext.OrganizationUser(orgId).Returns(true);
|
|
|
|
|
_ssoConfigRepository.GetByOrganizationIdAsync(orgId).Returns(ssoConfig);
|
|
|
|
|
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
|
2024-11-07 20:10:00 +01:00
|
|
|
|
_featureService.IsEnabled(FeatureFlagKeys.AccountDeprovisioning).Returns(true);
|
|
|
|
|
_userService.GetOrganizationsManagingUserAsync(user.Id).Returns(new List<Organization>());
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2024-10-16 11:33:00 +02:00
|
|
|
|
await _sut.Leave(orgId);
|
|
|
|
|
|
|
|
|
|
await _removeOrganizationUserCommand.Received(1).RemoveUserAsync(orgId, user.Id);
|
2021-11-15 10:46:13 +01:00
|
|
|
|
}
|
2023-11-01 16:05:04 +01:00
|
|
|
|
|
2024-04-19 16:09:18 +02:00
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task Delete_OrganizationIsConsolidatedBillingClient_ScalesProvidersSeats(
|
|
|
|
|
Provider provider,
|
|
|
|
|
Organization organization,
|
|
|
|
|
User user,
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
SecretVerificationRequestModel requestModel)
|
|
|
|
|
{
|
|
|
|
|
organization.Status = OrganizationStatusType.Managed;
|
|
|
|
|
organization.PlanType = PlanType.TeamsMonthly;
|
|
|
|
|
organization.Seats = 10;
|
|
|
|
|
|
|
|
|
|
provider.Type = ProviderType.Msp;
|
|
|
|
|
provider.Status = ProviderStatusType.Billable;
|
|
|
|
|
|
|
|
|
|
_currentContext.OrganizationOwner(organizationId).Returns(true);
|
|
|
|
|
|
|
|
|
|
_organizationRepository.GetByIdAsync(organizationId).Returns(organization);
|
|
|
|
|
|
|
|
|
|
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
|
|
|
|
|
|
|
|
|
|
_userService.VerifySecretAsync(user, requestModel.Secret).Returns(true);
|
|
|
|
|
|
|
|
|
|
_featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling).Returns(true);
|
|
|
|
|
|
|
|
|
|
_providerRepository.GetByOrganizationIdAsync(organization.Id).Returns(provider);
|
|
|
|
|
|
|
|
|
|
await _sut.Delete(organizationId.ToString(), requestModel);
|
|
|
|
|
|
2024-05-23 16:17:00 +02:00
|
|
|
|
await _providerBillingService.Received(1)
|
|
|
|
|
.ScaleSeats(provider, organization.PlanType, -organization.Seats.Value);
|
2024-04-19 16:09:18 +02:00
|
|
|
|
|
|
|
|
|
await _organizationService.Received(1).DeleteAsync(organization);
|
|
|
|
|
}
|
2021-11-15 10:46:13 +01:00
|
|
|
|
}
|