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;
|
2023-11-01 16:05:04 +01:00
|
|
|
|
using Bit.Api.AdminConsole.Models.Request.Organizations;
|
|
|
|
|
using Bit.Api.Models.Request.Organizations;
|
2023-11-29 00:18:08 +01:00
|
|
|
|
using Bit.Core.AdminConsole.Entities;
|
2023-10-26 23:47:44 +02:00
|
|
|
|
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationApiKeys.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-02-09 17:58:37 +01:00
|
|
|
|
using Bit.Core.Billing.Commands;
|
|
|
|
|
using Bit.Core.Billing.Queries;
|
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;
|
2023-11-01 16:05:04 +01:00
|
|
|
|
using Bit.Core.Models.Business;
|
|
|
|
|
using Bit.Core.Models.Data.Organizations.OrganizationUsers;
|
2023-01-30 22:42:10 +01:00
|
|
|
|
using Bit.Core.OrganizationFeatures.OrganizationLicenses.Interfaces;
|
2023-07-25 00:05:05 +02:00
|
|
|
|
using Bit.Core.OrganizationFeatures.OrganizationSubscriptions.Interface;
|
2021-11-15 10:46:13 +01:00
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Core.Services;
|
2024-02-09 17:58:37 +01:00
|
|
|
|
using Bit.Core.Tools.Services;
|
2021-11-15 10:46:13 +01:00
|
|
|
|
using NSubstitute;
|
2023-11-01 16:05:04 +01:00
|
|
|
|
using NSubstitute.ReturnsExtensions;
|
2021-11-15 10:46:13 +01:00
|
|
|
|
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
|
|
|
|
|
2021-11-15 10:46:13 +01:00
|
|
|
|
public class OrganizationsControllerTests : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
|
|
|
|
private readonly ICurrentContext _currentContext;
|
|
|
|
|
private readonly IOrganizationRepository _organizationRepository;
|
|
|
|
|
private readonly IOrganizationService _organizationService;
|
|
|
|
|
private readonly IOrganizationUserRepository _organizationUserRepository;
|
|
|
|
|
private readonly IPaymentService _paymentService;
|
|
|
|
|
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-05-10 23:12:09 +02:00
|
|
|
|
private readonly IRotateOrganizationApiKeyCommand _rotateOrganizationApiKeyCommand;
|
|
|
|
|
private readonly IOrganizationApiKeyRepository _organizationApiKeyRepository;
|
2023-01-30 22:42:10 +01:00
|
|
|
|
private readonly ICloudGetOrganizationLicenseQuery _cloudGetOrganizationLicenseQuery;
|
2022-11-29 01:39:09 +01:00
|
|
|
|
private readonly ICreateOrganizationApiKeyCommand _createOrganizationApiKeyCommand;
|
2023-05-10 21:52:08 +02:00
|
|
|
|
private readonly IFeatureService _featureService;
|
2023-05-15 16:38:41 +02:00
|
|
|
|
private readonly ILicensingService _licensingService;
|
2023-07-25 00:05:05 +02:00
|
|
|
|
private readonly IUpdateSecretsManagerSubscriptionCommand _updateSecretsManagerSubscriptionCommand;
|
|
|
|
|
private readonly IUpgradeOrganizationPlanCommand _upgradeOrganizationPlanCommand;
|
2023-08-04 23:51:12 +02:00
|
|
|
|
private readonly IAddSecretsManagerSubscriptionCommand _addSecretsManagerSubscriptionCommand;
|
2024-02-09 01:27:47 +01:00
|
|
|
|
private readonly IPushNotificationService _pushNotificationService;
|
2024-02-09 17:58:37 +01:00
|
|
|
|
private readonly ICancelSubscriptionCommand _cancelSubscriptionCommand;
|
|
|
|
|
private readonly IGetSubscriptionQuery _getSubscriptionQuery;
|
|
|
|
|
private readonly IReferenceEventService _referenceEventService;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-15 10:46:13 +01:00
|
|
|
|
private readonly OrganizationsController _sut;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-15 10:46:13 +01:00
|
|
|
|
public OrganizationsControllerTests()
|
|
|
|
|
{
|
|
|
|
|
_currentContext = Substitute.For<ICurrentContext>();
|
|
|
|
|
_globalSettings = Substitute.For<GlobalSettings>();
|
|
|
|
|
_organizationRepository = Substitute.For<IOrganizationRepository>();
|
|
|
|
|
_organizationService = Substitute.For<IOrganizationService>();
|
|
|
|
|
_organizationUserRepository = Substitute.For<IOrganizationUserRepository>();
|
|
|
|
|
_paymentService = Substitute.For<IPaymentService>();
|
|
|
|
|
_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-05-10 23:12:09 +02:00
|
|
|
|
_rotateOrganizationApiKeyCommand = Substitute.For<IRotateOrganizationApiKeyCommand>();
|
|
|
|
|
_organizationApiKeyRepository = Substitute.For<IOrganizationApiKeyRepository>();
|
2021-11-15 10:46:13 +01:00
|
|
|
|
_userService = Substitute.For<IUserService>();
|
2023-01-30 22:42:10 +01:00
|
|
|
|
_cloudGetOrganizationLicenseQuery = Substitute.For<ICloudGetOrganizationLicenseQuery>();
|
2022-11-29 01:39:09 +01:00
|
|
|
|
_createOrganizationApiKeyCommand = Substitute.For<ICreateOrganizationApiKeyCommand>();
|
2023-05-10 21:52:08 +02:00
|
|
|
|
_featureService = Substitute.For<IFeatureService>();
|
2023-05-15 16:38:41 +02:00
|
|
|
|
_licensingService = Substitute.For<ILicensingService>();
|
2023-07-25 00:05:05 +02:00
|
|
|
|
_updateSecretsManagerSubscriptionCommand = Substitute.For<IUpdateSecretsManagerSubscriptionCommand>();
|
|
|
|
|
_upgradeOrganizationPlanCommand = Substitute.For<IUpgradeOrganizationPlanCommand>();
|
2023-08-04 23:51:12 +02:00
|
|
|
|
_addSecretsManagerSubscriptionCommand = Substitute.For<IAddSecretsManagerSubscriptionCommand>();
|
2024-02-09 01:27:47 +01:00
|
|
|
|
_pushNotificationService = Substitute.For<IPushNotificationService>();
|
2024-02-09 17:58:37 +01:00
|
|
|
|
_cancelSubscriptionCommand = Substitute.For<ICancelSubscriptionCommand>();
|
|
|
|
|
_getSubscriptionQuery = Substitute.For<IGetSubscriptionQuery>();
|
|
|
|
|
_referenceEventService = Substitute.For<IReferenceEventService>();
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2024-01-12 16:38:47 +01:00
|
|
|
|
_sut = new OrganizationsController(
|
|
|
|
|
_organizationRepository,
|
|
|
|
|
_organizationUserRepository,
|
|
|
|
|
_policyRepository,
|
|
|
|
|
_organizationService,
|
|
|
|
|
_userService,
|
|
|
|
|
_paymentService,
|
|
|
|
|
_currentContext,
|
|
|
|
|
_ssoConfigRepository,
|
|
|
|
|
_ssoConfigService,
|
|
|
|
|
_getOrganizationApiKeyQuery,
|
|
|
|
|
_rotateOrganizationApiKeyCommand,
|
|
|
|
|
_createOrganizationApiKeyCommand,
|
|
|
|
|
_organizationApiKeyRepository,
|
|
|
|
|
_cloudGetOrganizationLicenseQuery,
|
|
|
|
|
_featureService,
|
|
|
|
|
_globalSettings,
|
|
|
|
|
_licensingService,
|
|
|
|
|
_updateSecretsManagerSubscriptionCommand,
|
|
|
|
|
_upgradeOrganizationPlanCommand,
|
2024-02-09 01:27:47 +01:00
|
|
|
|
_addSecretsManagerSubscriptionCommand,
|
2024-02-09 17:58:37 +01:00
|
|
|
|
_pushNotificationService,
|
|
|
|
|
_cancelSubscriptionCommand,
|
|
|
|
|
_getSubscriptionQuery,
|
|
|
|
|
_referenceEventService);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 10:46:13 +01:00
|
|
|
|
public void Dispose()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-11-15 10:46:13 +01:00
|
|
|
|
_sut?.Dispose();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 10:46:13 +01:00
|
|
|
|
[Theory, AutoData]
|
2021-11-18 12:15:22 +01:00
|
|
|
|
public async Task OrganizationsController_UserCannotLeaveOrganizationThatProvidesKeyConnector(
|
|
|
|
|
Guid orgId, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-11-15 10:46:13 +01:00
|
|
|
|
var ssoConfig = new SsoConfig
|
|
|
|
|
{
|
|
|
|
|
Id = default,
|
|
|
|
|
Data = new SsoConfigurationData
|
|
|
|
|
{
|
2023-05-10 21:52:08 +02:00
|
|
|
|
MemberDecryptionType = MemberDecryptionType.KeyConnector
|
2021-11-17 11:46:35 +01:00
|
|
|
|
}.Serialize(),
|
2021-11-15 10:46:13 +01:00
|
|
|
|
Enabled = true,
|
|
|
|
|
OrganizationId = orgId,
|
|
|
|
|
};
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-18 12:15:22 +01:00
|
|
|
|
user.UsesKeyConnector = true;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-15 10:46:13 +01:00
|
|
|
|
_currentContext.OrganizationUser(orgId).Returns(true);
|
|
|
|
|
_ssoConfigRepository.GetByOrganizationIdAsync(orgId).Returns(ssoConfig);
|
2021-11-22 10:55:25 +01:00
|
|
|
|
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-15 10:46:13 +01:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => _sut.Leave(orgId.ToString()));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-21 23:22:28 +01:00
|
|
|
|
Assert.Contains("Your organization's Single Sign-On settings prevent you from leaving.",
|
2021-11-15 10:46:13 +01:00
|
|
|
|
exception.Message);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-15 10:46:13 +01:00
|
|
|
|
await _organizationService.DidNotReceiveWithAnyArgs().DeleteUserAsync(default, default);
|
|
|
|
|
}
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-18 12:15:22 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineAutoData(true, false)]
|
|
|
|
|
[InlineAutoData(false, true)]
|
|
|
|
|
[InlineAutoData(false, false)]
|
|
|
|
|
public async Task OrganizationsController_UserCanLeaveOrganizationThatDoesntProvideKeyConnector(
|
|
|
|
|
bool keyConnectorEnabled, bool userUsesKeyConnector, Guid orgId, User user)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-11-18 12:15:22 +01:00
|
|
|
|
var ssoConfig = new SsoConfig
|
|
|
|
|
{
|
|
|
|
|
Id = default,
|
|
|
|
|
Data = new SsoConfigurationData
|
|
|
|
|
{
|
2023-05-10 21:52:08 +02:00
|
|
|
|
MemberDecryptionType = keyConnectorEnabled
|
|
|
|
|
? MemberDecryptionType.KeyConnector
|
|
|
|
|
: MemberDecryptionType.MasterPassword
|
2021-11-18 12:15:22 +01:00
|
|
|
|
}.Serialize(),
|
|
|
|
|
Enabled = true,
|
|
|
|
|
OrganizationId = orgId,
|
|
|
|
|
};
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-18 12:15:22 +01:00
|
|
|
|
user.UsesKeyConnector = userUsesKeyConnector;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-18 12:15:22 +01:00
|
|
|
|
_currentContext.OrganizationUser(orgId).Returns(true);
|
|
|
|
|
_ssoConfigRepository.GetByOrganizationIdAsync(orgId).Returns(ssoConfig);
|
2021-11-22 10:55:25 +01:00
|
|
|
|
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-18 12:15:22 +01:00
|
|
|
|
await _organizationService.DeleteUserAsync(orgId, user.Id);
|
|
|
|
|
await _organizationService.Received(1).DeleteUserAsync(orgId, user.Id);
|
2021-11-15 10:46:13 +01:00
|
|
|
|
}
|
2023-11-01 16:05:04 +01:00
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task OrganizationsController_PostUpgrade_UserCannotEditSubscription_ThrowsNotFoundException(
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
OrganizationUpgradeRequestModel model)
|
|
|
|
|
{
|
|
|
|
|
_currentContext.EditSubscription(organizationId).Returns(false);
|
|
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => _sut.PostUpgrade(organizationId.ToString(), model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task OrganizationsController_PostUpgrade_NonSMUpgrade_ReturnsCorrectResponse(
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
OrganizationUpgradeRequestModel model,
|
|
|
|
|
bool success,
|
|
|
|
|
string paymentIntentClientSecret)
|
|
|
|
|
{
|
|
|
|
|
model.UseSecretsManager = false;
|
|
|
|
|
|
|
|
|
|
_currentContext.EditSubscription(organizationId).Returns(true);
|
|
|
|
|
|
|
|
|
|
_upgradeOrganizationPlanCommand.UpgradePlanAsync(organizationId, Arg.Any<OrganizationUpgrade>())
|
|
|
|
|
.Returns(new Tuple<bool, string>(success, paymentIntentClientSecret));
|
|
|
|
|
|
|
|
|
|
var response = await _sut.PostUpgrade(organizationId.ToString(), model);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(success, response.Success);
|
|
|
|
|
Assert.Equal(paymentIntentClientSecret, response.PaymentIntentClientSecret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task OrganizationsController_PostUpgrade_SMUpgrade_ProvidesAccess_ReturnsCorrectResponse(
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
Guid userId,
|
|
|
|
|
OrganizationUpgradeRequestModel model,
|
|
|
|
|
bool success,
|
|
|
|
|
string paymentIntentClientSecret,
|
|
|
|
|
OrganizationUser organizationUser)
|
|
|
|
|
{
|
|
|
|
|
model.UseSecretsManager = true;
|
|
|
|
|
organizationUser.AccessSecretsManager = false;
|
|
|
|
|
|
|
|
|
|
_currentContext.EditSubscription(organizationId).Returns(true);
|
|
|
|
|
|
|
|
|
|
_upgradeOrganizationPlanCommand.UpgradePlanAsync(organizationId, Arg.Any<OrganizationUpgrade>())
|
|
|
|
|
.Returns(new Tuple<bool, string>(success, paymentIntentClientSecret));
|
|
|
|
|
|
|
|
|
|
_userService.GetProperUserId(Arg.Any<ClaimsPrincipal>()).Returns(userId);
|
|
|
|
|
|
|
|
|
|
_organizationUserRepository.GetByOrganizationAsync(organizationId, userId).Returns(organizationUser);
|
|
|
|
|
|
|
|
|
|
var response = await _sut.PostUpgrade(organizationId.ToString(), model);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(success, response.Success);
|
|
|
|
|
Assert.Equal(paymentIntentClientSecret, response.PaymentIntentClientSecret);
|
|
|
|
|
|
|
|
|
|
await _organizationUserRepository.Received(1).ReplaceAsync(Arg.Is<OrganizationUser>(orgUser =>
|
|
|
|
|
orgUser.Id == organizationUser.Id && orgUser.AccessSecretsManager == true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task OrganizationsController_PostUpgrade_SMUpgrade_NullOrgUser_ReturnsCorrectResponse(
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
Guid userId,
|
|
|
|
|
OrganizationUpgradeRequestModel model,
|
|
|
|
|
bool success,
|
|
|
|
|
string paymentIntentClientSecret)
|
|
|
|
|
{
|
|
|
|
|
model.UseSecretsManager = true;
|
|
|
|
|
|
|
|
|
|
_currentContext.EditSubscription(organizationId).Returns(true);
|
|
|
|
|
|
|
|
|
|
_upgradeOrganizationPlanCommand.UpgradePlanAsync(organizationId, Arg.Any<OrganizationUpgrade>())
|
|
|
|
|
.Returns(new Tuple<bool, string>(success, paymentIntentClientSecret));
|
|
|
|
|
|
|
|
|
|
_userService.GetProperUserId(Arg.Any<ClaimsPrincipal>()).Returns(userId);
|
|
|
|
|
|
|
|
|
|
_organizationUserRepository.GetByOrganizationAsync(organizationId, userId).ReturnsNull();
|
|
|
|
|
|
|
|
|
|
var response = await _sut.PostUpgrade(organizationId.ToString(), model);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(success, response.Success);
|
|
|
|
|
Assert.Equal(paymentIntentClientSecret, response.PaymentIntentClientSecret);
|
|
|
|
|
|
|
|
|
|
await _organizationUserRepository.DidNotReceiveWithAnyArgs().ReplaceAsync(Arg.Any<OrganizationUser>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task OrganizationsController_PostSubscribeSecretsManagerAsync_NullOrg_ThrowsNotFoundException(
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
SecretsManagerSubscribeRequestModel model)
|
|
|
|
|
{
|
|
|
|
|
_organizationRepository.GetByIdAsync(organizationId).ReturnsNull();
|
|
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => _sut.PostSubscribeSecretsManagerAsync(organizationId, model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task OrganizationsController_PostSubscribeSecretsManagerAsync_UserCannotEditSubscription_ThrowsNotFoundException(
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
SecretsManagerSubscribeRequestModel model,
|
|
|
|
|
Organization organization)
|
|
|
|
|
{
|
|
|
|
|
_organizationRepository.GetByIdAsync(organizationId).Returns(organization);
|
|
|
|
|
|
|
|
|
|
_currentContext.EditSubscription(organizationId).Returns(false);
|
|
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => _sut.PostSubscribeSecretsManagerAsync(organizationId, model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task OrganizationsController_PostSubscribeSecretsManagerAsync_ProvidesAccess_ReturnsCorrectResponse(
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
SecretsManagerSubscribeRequestModel model,
|
|
|
|
|
Organization organization,
|
|
|
|
|
Guid userId,
|
|
|
|
|
OrganizationUser organizationUser,
|
|
|
|
|
OrganizationUserOrganizationDetails organizationUserOrganizationDetails)
|
|
|
|
|
{
|
|
|
|
|
organizationUser.AccessSecretsManager = false;
|
|
|
|
|
|
|
|
|
|
var ssoConfigurationData = new SsoConfigurationData
|
|
|
|
|
{
|
|
|
|
|
MemberDecryptionType = MemberDecryptionType.KeyConnector,
|
|
|
|
|
KeyConnectorUrl = "https://example.com"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
organizationUserOrganizationDetails.Permissions = string.Empty;
|
|
|
|
|
organizationUserOrganizationDetails.SsoConfig = ssoConfigurationData.Serialize();
|
|
|
|
|
|
|
|
|
|
_organizationRepository.GetByIdAsync(organizationId).Returns(organization);
|
|
|
|
|
|
|
|
|
|
_currentContext.EditSubscription(organizationId).Returns(true);
|
|
|
|
|
|
|
|
|
|
_userService.GetProperUserId(Arg.Any<ClaimsPrincipal>()).Returns(userId);
|
|
|
|
|
|
|
|
|
|
_organizationUserRepository.GetByOrganizationAsync(organization.Id, userId).Returns(organizationUser);
|
|
|
|
|
|
|
|
|
|
_organizationUserRepository.GetDetailsByUserAsync(userId, organization.Id, OrganizationUserStatusType.Confirmed)
|
|
|
|
|
.Returns(organizationUserOrganizationDetails);
|
|
|
|
|
|
|
|
|
|
var response = await _sut.PostSubscribeSecretsManagerAsync(organizationId, model);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(response.Id, organizationUserOrganizationDetails.OrganizationId);
|
|
|
|
|
Assert.Equal(response.Name, organizationUserOrganizationDetails.Name);
|
|
|
|
|
|
|
|
|
|
await _addSecretsManagerSubscriptionCommand.Received(1)
|
|
|
|
|
.SignUpAsync(organization, model.AdditionalSmSeats, model.AdditionalServiceAccounts);
|
|
|
|
|
await _organizationUserRepository.Received(1).ReplaceAsync(Arg.Is<OrganizationUser>(orgUser =>
|
|
|
|
|
orgUser.Id == organizationUser.Id && orgUser.AccessSecretsManager == true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task OrganizationsController_PostSubscribeSecretsManagerAsync_NullOrgUser_ReturnsCorrectResponse(
|
|
|
|
|
Guid organizationId,
|
|
|
|
|
SecretsManagerSubscribeRequestModel model,
|
|
|
|
|
Organization organization,
|
|
|
|
|
Guid userId,
|
|
|
|
|
OrganizationUserOrganizationDetails organizationUserOrganizationDetails)
|
|
|
|
|
{
|
|
|
|
|
var ssoConfigurationData = new SsoConfigurationData
|
|
|
|
|
{
|
|
|
|
|
MemberDecryptionType = MemberDecryptionType.KeyConnector,
|
|
|
|
|
KeyConnectorUrl = "https://example.com"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
organizationUserOrganizationDetails.Permissions = string.Empty;
|
|
|
|
|
organizationUserOrganizationDetails.SsoConfig = ssoConfigurationData.Serialize();
|
|
|
|
|
|
|
|
|
|
_organizationRepository.GetByIdAsync(organizationId).Returns(organization);
|
|
|
|
|
|
|
|
|
|
_currentContext.EditSubscription(organizationId).Returns(true);
|
|
|
|
|
|
|
|
|
|
_userService.GetProperUserId(Arg.Any<ClaimsPrincipal>()).Returns(userId);
|
|
|
|
|
|
|
|
|
|
_organizationUserRepository.GetByOrganizationAsync(organization.Id, userId).ReturnsNull();
|
|
|
|
|
|
|
|
|
|
_organizationUserRepository.GetDetailsByUserAsync(userId, organization.Id, OrganizationUserStatusType.Confirmed)
|
|
|
|
|
.Returns(organizationUserOrganizationDetails);
|
|
|
|
|
|
|
|
|
|
var response = await _sut.PostSubscribeSecretsManagerAsync(organizationId, model);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(response.Id, organizationUserOrganizationDetails.OrganizationId);
|
|
|
|
|
Assert.Equal(response.Name, organizationUserOrganizationDetails.Name);
|
|
|
|
|
|
|
|
|
|
await _addSecretsManagerSubscriptionCommand.Received(1)
|
|
|
|
|
.SignUpAsync(organization, model.AdditionalSmSeats, model.AdditionalServiceAccounts);
|
|
|
|
|
await _organizationUserRepository.DidNotReceiveWithAnyArgs().ReplaceAsync(Arg.Any<OrganizationUser>());
|
|
|
|
|
}
|
2024-01-25 07:57:57 +01:00
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task EnableCollectionEnhancements_Success(Organization organization)
|
|
|
|
|
{
|
|
|
|
|
organization.FlexibleCollections = false;
|
2024-02-09 01:27:47 +01:00
|
|
|
|
var admin = new OrganizationUser { UserId = Guid.NewGuid(), Type = OrganizationUserType.Admin };
|
|
|
|
|
var owner = new OrganizationUser { UserId = Guid.NewGuid(), Type = OrganizationUserType.Owner };
|
|
|
|
|
var user = new OrganizationUser { UserId = Guid.NewGuid(), Type = OrganizationUserType.User };
|
|
|
|
|
var invited = new OrganizationUser
|
|
|
|
|
{
|
|
|
|
|
UserId = null,
|
|
|
|
|
Type = OrganizationUserType.Admin,
|
|
|
|
|
Email = "invited@example.com"
|
|
|
|
|
};
|
|
|
|
|
var orgUsers = new List<OrganizationUser> { admin, owner, user, invited };
|
|
|
|
|
|
2024-01-25 07:57:57 +01:00
|
|
|
|
_currentContext.OrganizationOwner(organization.Id).Returns(true);
|
|
|
|
|
_organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
2024-02-09 01:27:47 +01:00
|
|
|
|
_organizationUserRepository.GetManyByOrganizationAsync(organization.Id, null).Returns(orgUsers);
|
2024-01-25 07:57:57 +01:00
|
|
|
|
|
|
|
|
|
await _sut.EnableCollectionEnhancements(organization.Id);
|
|
|
|
|
|
|
|
|
|
await _organizationRepository.Received(1).EnableCollectionEnhancements(organization.Id);
|
|
|
|
|
await _organizationService.Received(1).ReplaceAndUpdateCacheAsync(
|
|
|
|
|
Arg.Is<Organization>(o =>
|
|
|
|
|
o.Id == organization.Id &&
|
|
|
|
|
o.FlexibleCollections));
|
2024-02-09 01:27:47 +01:00
|
|
|
|
await _pushNotificationService.Received(1).PushSyncVaultAsync(admin.UserId.Value);
|
|
|
|
|
await _pushNotificationService.Received(1).PushSyncVaultAsync(owner.UserId.Value);
|
|
|
|
|
await _pushNotificationService.DidNotReceive().PushSyncVaultAsync(user.UserId.Value);
|
2024-01-25 07:57:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task EnableCollectionEnhancements_WhenNotOwner_Throws(Organization organization)
|
|
|
|
|
{
|
|
|
|
|
organization.FlexibleCollections = false;
|
|
|
|
|
_currentContext.OrganizationOwner(organization.Id).Returns(false);
|
|
|
|
|
_organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(async () => await _sut.EnableCollectionEnhancements(organization.Id));
|
|
|
|
|
|
|
|
|
|
await _organizationRepository.DidNotReceiveWithAnyArgs().EnableCollectionEnhancements(Arg.Any<Guid>());
|
|
|
|
|
await _organizationService.DidNotReceiveWithAnyArgs().ReplaceAndUpdateCacheAsync(Arg.Any<Organization>());
|
2024-02-09 01:27:47 +01:00
|
|
|
|
await _pushNotificationService.DidNotReceiveWithAnyArgs().PushSyncVaultAsync(Arg.Any<Guid>());
|
2024-01-25 07:57:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public async Task EnableCollectionEnhancements_WhenAlreadyMigrated_Throws(Organization organization)
|
|
|
|
|
{
|
|
|
|
|
organization.FlexibleCollections = true;
|
|
|
|
|
_currentContext.OrganizationOwner(organization.Id).Returns(true);
|
|
|
|
|
_organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(async () => await _sut.EnableCollectionEnhancements(organization.Id));
|
|
|
|
|
Assert.Contains("has already been migrated", exception.Message);
|
|
|
|
|
|
|
|
|
|
await _organizationRepository.DidNotReceiveWithAnyArgs().EnableCollectionEnhancements(Arg.Any<Guid>());
|
|
|
|
|
await _organizationService.DidNotReceiveWithAnyArgs().ReplaceAndUpdateCacheAsync(Arg.Any<Organization>());
|
2024-02-09 01:27:47 +01:00
|
|
|
|
await _pushNotificationService.DidNotReceiveWithAnyArgs().PushSyncVaultAsync(Arg.Any<Guid>());
|
2024-01-25 07:57:57 +01:00
|
|
|
|
}
|
2021-11-15 10:46:13 +01:00
|
|
|
|
}
|