2023-05-31 20:49:58 +02:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using Bit.Api.SecretsManager.Controllers;
|
2023-01-24 19:57:28 +01:00
|
|
|
|
using Bit.Api.SecretsManager.Models.Request;
|
2023-01-24 16:50:04 +01:00
|
|
|
|
using Bit.Core.Context;
|
2023-08-04 23:51:12 +02:00
|
|
|
|
using Bit.Core.Entities;
|
2023-01-24 16:50:04 +01:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Exceptions;
|
2023-08-04 23:51:12 +02:00
|
|
|
|
using Bit.Core.OrganizationFeatures.OrganizationSubscriptions.Interface;
|
|
|
|
|
using Bit.Core.Repositories;
|
2023-01-24 19:57:28 +01:00
|
|
|
|
using Bit.Core.SecretsManager.Commands.AccessTokens.Interfaces;
|
|
|
|
|
using Bit.Core.SecretsManager.Commands.ServiceAccounts.Interfaces;
|
|
|
|
|
using Bit.Core.SecretsManager.Entities;
|
2023-06-21 20:16:06 +02:00
|
|
|
|
using Bit.Core.SecretsManager.Models.Data;
|
2023-08-03 17:06:34 +02:00
|
|
|
|
using Bit.Core.SecretsManager.Queries.ServiceAccounts.Interfaces;
|
2023-01-24 19:57:28 +01:00
|
|
|
|
using Bit.Core.SecretsManager.Repositories;
|
2023-01-24 16:50:04 +01:00
|
|
|
|
using Bit.Core.Services;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
using Bit.Test.Common.Helpers;
|
2023-05-31 20:49:58 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2023-01-24 19:57:28 +01:00
|
|
|
|
namespace Bit.Api.Test.SecretsManager.Controllers;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
|
|
|
|
[ControllerCustomize(typeof(ServiceAccountsController))]
|
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
[JsonDocumentCustomize]
|
|
|
|
|
public class ServiceAccountsControllerTests
|
|
|
|
|
{
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2023-06-13 22:30:44 +02:00
|
|
|
|
public async void GetServiceAccountsByOrganization_ReturnsEmptyList(
|
|
|
|
|
SutProvider<ServiceAccountsController> sutProvider, Guid id)
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessSecretsManager(id).Returns(true);
|
2023-01-24 16:50:04 +01:00
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid());
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var result = await sutProvider.Sut.ListByOrganizationAsync(id);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-08-03 17:06:34 +02:00
|
|
|
|
await sutProvider.GetDependency<IServiceAccountSecretsDetailsQuery>().Received(1)
|
|
|
|
|
.GetManyByOrganizationIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)),
|
|
|
|
|
Arg.Any<Guid>(), Arg.Any<AccessClientType>(), Arg.Any<bool>());
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
|
|
|
|
Assert.Empty(result.Data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2023-06-13 22:30:44 +02:00
|
|
|
|
public async void GetServiceAccountsByOrganization_Success(SutProvider<ServiceAccountsController> sutProvider,
|
2023-08-03 17:06:34 +02:00
|
|
|
|
ServiceAccountSecretsDetails resultServiceAccount)
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
2023-01-31 18:38:53 +01:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessSecretsManager(default).ReturnsForAnyArgs(true);
|
2023-01-24 16:50:04 +01:00
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid());
|
2023-08-03 17:06:34 +02:00
|
|
|
|
sutProvider.GetDependency<IServiceAccountSecretsDetailsQuery>().GetManyByOrganizationIdAsync(default, default, default, default)
|
|
|
|
|
.ReturnsForAnyArgs(new List<ServiceAccountSecretsDetails> { resultServiceAccount });
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-08-03 17:06:34 +02:00
|
|
|
|
var result = await sutProvider.Sut.ListByOrganizationAsync(resultServiceAccount.ServiceAccount.OrganizationId);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-08-03 17:06:34 +02:00
|
|
|
|
await sutProvider.GetDependency<IServiceAccountSecretsDetailsQuery>().Received(1)
|
|
|
|
|
.GetManyByOrganizationIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(resultServiceAccount.ServiceAccount.OrganizationId)),
|
|
|
|
|
Arg.Any<Guid>(), Arg.Any<AccessClientType>(), Arg.Any<bool>());
|
2023-01-24 16:50:04 +01:00
|
|
|
|
Assert.NotEmpty(result.Data);
|
|
|
|
|
Assert.Single(result.Data);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-31 18:38:53 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2023-06-13 22:30:44 +02:00
|
|
|
|
public async void GetServiceAccountsByOrganization_AccessDenied_Throws(
|
|
|
|
|
SutProvider<ServiceAccountsController> sutProvider, Guid orgId)
|
2023-01-31 18:38:53 +01:00
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessSecretsManager(default).ReturnsForAnyArgs(false);
|
|
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
|
|
|
sutProvider.Sut.ListByOrganizationAsync(orgId));
|
|
|
|
|
}
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2023-06-13 22:30:44 +02:00
|
|
|
|
public async void CreateServiceAccount_NoAccess_Throws(SutProvider<ServiceAccountsController> sutProvider,
|
|
|
|
|
ServiceAccountCreateRequestModel data, Guid organizationId)
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
2023-05-31 20:49:58 +02:00
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), data.ToServiceAccount(organizationId),
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Failed());
|
2023-02-02 19:25:14 +01:00
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid());
|
2023-01-13 15:02:53 +01:00
|
|
|
|
var resultServiceAccount = data.ToServiceAccount(organizationId);
|
2023-06-13 22:30:44 +02:00
|
|
|
|
sutProvider.GetDependency<ICreateServiceAccountCommand>().CreateAsync(default, default)
|
|
|
|
|
.ReturnsForAnyArgs(resultServiceAccount);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-05-31 20:49:58 +02:00
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.CreateAsync(organizationId, data));
|
|
|
|
|
await sutProvider.GetDependency<ICreateServiceAccountCommand>().DidNotReceiveWithAnyArgs()
|
|
|
|
|
.CreateAsync(Arg.Any<ServiceAccount>(), Arg.Any<Guid>());
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 23:51:12 +02:00
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData(0)]
|
|
|
|
|
public async void CreateServiceAccount_WhenAutoscalingNotRequired_DoesNotCallUpdateSubscription(
|
|
|
|
|
int newSlotsRequired, SutProvider<ServiceAccountsController> sutProvider,
|
|
|
|
|
ServiceAccountCreateRequestModel data, Organization organization)
|
|
|
|
|
{
|
|
|
|
|
ArrangeCreateServiceAccountAutoScalingTest(newSlotsRequired, sutProvider, data, organization);
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.CreateAsync(organization.Id, data);
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<ICreateServiceAccountCommand>().Received(1)
|
|
|
|
|
.CreateAsync(Arg.Is<ServiceAccount>(sa => sa.Name == data.Name), Arg.Any<Guid>());
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IUpdateSecretsManagerSubscriptionCommand>().DidNotReceiveWithAnyArgs()
|
|
|
|
|
.AdjustServiceAccountsAsync(Arg.Any<Organization>(), Arg.Any<int>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData(1)]
|
|
|
|
|
[BitAutoData(2)]
|
|
|
|
|
public async void CreateServiceAccount_WhenAutoscalingRequired_CallsUpdateSubscription(int newSlotsRequired,
|
|
|
|
|
SutProvider<ServiceAccountsController> sutProvider,
|
|
|
|
|
ServiceAccountCreateRequestModel data, Organization organization)
|
|
|
|
|
{
|
|
|
|
|
ArrangeCreateServiceAccountAutoScalingTest(newSlotsRequired, sutProvider, data, organization);
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.CreateAsync(organization.Id, data);
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<ICreateServiceAccountCommand>().Received(1)
|
|
|
|
|
.CreateAsync(Arg.Is<ServiceAccount>(sa => sa.Name == data.Name), Arg.Any<Guid>());
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IUpdateSecretsManagerSubscriptionCommand>().Received(1)
|
|
|
|
|
.AdjustServiceAccountsAsync(Arg.Is(organization), Arg.Is(newSlotsRequired));
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-24 16:50:04 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2023-06-13 22:30:44 +02:00
|
|
|
|
public async void CreateServiceAccount_Success(SutProvider<ServiceAccountsController> sutProvider,
|
2023-08-04 23:51:12 +02:00
|
|
|
|
ServiceAccountCreateRequestModel data, Guid organizationId, Organization mockOrg)
|
2023-01-24 16:50:04 +01:00
|
|
|
|
{
|
2023-08-04 23:51:12 +02:00
|
|
|
|
mockOrg.Id = organizationId;
|
2023-05-31 20:49:58 +02:00
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), data.ToServiceAccount(organizationId),
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Success());
|
2023-08-04 23:51:12 +02:00
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(Arg.Is(organizationId)).Returns(mockOrg);
|
2023-05-31 20:49:58 +02:00
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid());
|
2023-01-24 16:50:04 +01:00
|
|
|
|
var resultServiceAccount = data.ToServiceAccount(organizationId);
|
2023-06-13 22:30:44 +02:00
|
|
|
|
sutProvider.GetDependency<ICreateServiceAccountCommand>().CreateAsync(default, default)
|
|
|
|
|
.ReturnsForAnyArgs(resultServiceAccount);
|
2023-01-24 16:50:04 +01:00
|
|
|
|
|
2023-05-31 20:49:58 +02:00
|
|
|
|
await sutProvider.Sut.CreateAsync(organizationId, data);
|
|
|
|
|
await sutProvider.GetDependency<ICreateServiceAccountCommand>().Received(1)
|
2023-06-13 22:30:44 +02:00
|
|
|
|
.CreateAsync(Arg.Any<ServiceAccount>(), Arg.Any<Guid>());
|
2023-05-31 20:49:58 +02:00
|
|
|
|
}
|
2023-01-24 16:50:04 +01:00
|
|
|
|
|
2023-05-31 20:49:58 +02:00
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2023-06-13 22:30:44 +02:00
|
|
|
|
public async void UpdateServiceAccount_NoAccess_Throws(SutProvider<ServiceAccountsController> sutProvider,
|
|
|
|
|
ServiceAccountUpdateRequestModel data, ServiceAccount existingServiceAccount)
|
2023-05-31 20:49:58 +02:00
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), data.ToServiceAccount(existingServiceAccount.Id),
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Failed());
|
2023-06-13 22:30:44 +02:00
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(existingServiceAccount.Id)
|
|
|
|
|
.ReturnsForAnyArgs(existingServiceAccount);
|
2023-05-31 20:49:58 +02:00
|
|
|
|
var resultServiceAccount = data.ToServiceAccount(existingServiceAccount.Id);
|
2023-06-13 22:30:44 +02:00
|
|
|
|
sutProvider.GetDependency<IUpdateServiceAccountCommand>().UpdateAsync(default)
|
|
|
|
|
.ReturnsForAnyArgs(resultServiceAccount);
|
2023-05-31 20:49:58 +02:00
|
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.UpdateAsync(existingServiceAccount.Id, data));
|
|
|
|
|
await sutProvider.GetDependency<IUpdateServiceAccountCommand>().DidNotReceiveWithAnyArgs()
|
|
|
|
|
.UpdateAsync(Arg.Any<ServiceAccount>());
|
2023-01-24 16:50:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 15:02:53 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2023-06-13 22:30:44 +02:00
|
|
|
|
public async void UpdateServiceAccount_Success(SutProvider<ServiceAccountsController> sutProvider,
|
|
|
|
|
ServiceAccountUpdateRequestModel data, ServiceAccount existingServiceAccount)
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
2023-05-31 20:49:58 +02:00
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), data.ToServiceAccount(existingServiceAccount.Id),
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Success());
|
|
|
|
|
var resultServiceAccount = data.ToServiceAccount(existingServiceAccount.Id);
|
2023-06-13 22:30:44 +02:00
|
|
|
|
sutProvider.GetDependency<IUpdateServiceAccountCommand>().UpdateAsync(default)
|
|
|
|
|
.ReturnsForAnyArgs(resultServiceAccount);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-05-31 20:49:58 +02:00
|
|
|
|
var result = await sutProvider.Sut.UpdateAsync(existingServiceAccount.Id, data);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
await sutProvider.GetDependency<IUpdateServiceAccountCommand>().Received(1)
|
2023-06-13 22:30:44 +02:00
|
|
|
|
.UpdateAsync(Arg.Any<ServiceAccount>());
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2023-06-13 22:30:44 +02:00
|
|
|
|
public async void CreateAccessToken_NoAccess_Throws(SutProvider<ServiceAccountsController> sutProvider,
|
2023-06-21 20:16:06 +02:00
|
|
|
|
AccessTokenCreateRequestModel data, ServiceAccount serviceAccount, string mockClientSecret)
|
2023-01-13 15:02:53 +01:00
|
|
|
|
{
|
2023-06-13 22:30:44 +02:00
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(serviceAccount.Id).Returns(serviceAccount);
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), serviceAccount,
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Failed());
|
|
|
|
|
var resultAccessToken = data.ToApiKey(serviceAccount.Id);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-06-21 20:16:06 +02:00
|
|
|
|
sutProvider.GetDependency<ICreateAccessTokenCommand>()
|
|
|
|
|
.CreateAsync(default)
|
|
|
|
|
.ReturnsForAnyArgs(new ApiKeyClientSecretDetails { ApiKey = resultAccessToken, ClientSecret = mockClientSecret });
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-06-13 22:30:44 +02:00
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
|
|
|
sutProvider.Sut.CreateAccessTokenAsync(serviceAccount.Id, data));
|
|
|
|
|
await sutProvider.GetDependency<ICreateAccessTokenCommand>().DidNotReceiveWithAnyArgs()
|
|
|
|
|
.CreateAsync(Arg.Any<ApiKey>());
|
2023-01-24 16:50:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2023-06-13 22:30:44 +02:00
|
|
|
|
public async void CreateAccessToken_Success(SutProvider<ServiceAccountsController> sutProvider,
|
2023-06-21 20:16:06 +02:00
|
|
|
|
AccessTokenCreateRequestModel data, ServiceAccount serviceAccount, string mockClientSecret)
|
2023-01-24 16:50:04 +01:00
|
|
|
|
{
|
2023-06-13 22:30:44 +02:00
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(serviceAccount.Id).Returns(serviceAccount);
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), serviceAccount,
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Success());
|
|
|
|
|
var resultAccessToken = data.ToApiKey(serviceAccount.Id);
|
2023-01-24 16:50:04 +01:00
|
|
|
|
|
2023-06-13 22:30:44 +02:00
|
|
|
|
sutProvider.GetDependency<ICreateAccessTokenCommand>().CreateAsync(default)
|
2023-06-21 20:16:06 +02:00
|
|
|
|
.ReturnsForAnyArgs(new ApiKeyClientSecretDetails { ApiKey = resultAccessToken, ClientSecret = mockClientSecret });
|
2023-01-24 16:50:04 +01:00
|
|
|
|
|
2023-06-13 22:30:44 +02:00
|
|
|
|
await sutProvider.Sut.CreateAccessTokenAsync(serviceAccount.Id, data);
|
|
|
|
|
await sutProvider.GetDependency<ICreateAccessTokenCommand>().Received(1)
|
|
|
|
|
.CreateAsync(Arg.Any<ApiKey>());
|
2023-01-24 16:50:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2023-06-13 22:30:44 +02:00
|
|
|
|
public async void GetAccessTokens_NoAccess_Throws(SutProvider<ServiceAccountsController> sutProvider,
|
|
|
|
|
ServiceAccount data, ICollection<ApiKey> resultApiKeys)
|
2023-01-24 16:50:04 +01:00
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(default).ReturnsForAnyArgs(data);
|
2023-06-13 22:30:44 +02:00
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), data,
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Failed());
|
|
|
|
|
|
2023-01-24 16:50:04 +01:00
|
|
|
|
foreach (var apiKey in resultApiKeys)
|
|
|
|
|
{
|
|
|
|
|
apiKey.Scope = "[\"api.secrets\"]";
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-13 22:30:44 +02:00
|
|
|
|
sutProvider.GetDependency<IApiKeyRepository>().GetManyByServiceAccountIdAsync(default)
|
|
|
|
|
.ReturnsForAnyArgs(resultApiKeys);
|
|
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.GetAccessTokens(data.Id));
|
|
|
|
|
await sutProvider.GetDependency<IApiKeyRepository>().DidNotReceiveWithAnyArgs()
|
|
|
|
|
.GetManyByServiceAccountIdAsync(Arg.Any<Guid>());
|
2023-01-24 16:50:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2023-06-13 22:30:44 +02:00
|
|
|
|
public async void GetAccessTokens_Success(SutProvider<ServiceAccountsController> sutProvider, ServiceAccount data,
|
|
|
|
|
ICollection<ApiKey> resultApiKeys)
|
2023-01-24 16:50:04 +01:00
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(default).ReturnsForAnyArgs(data);
|
2023-06-13 22:30:44 +02:00
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), data,
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Success());
|
|
|
|
|
|
2023-01-24 16:50:04 +01:00
|
|
|
|
foreach (var apiKey in resultApiKeys)
|
|
|
|
|
{
|
|
|
|
|
apiKey.Scope = "[\"api.secrets\"]";
|
|
|
|
|
}
|
2023-06-13 22:30:44 +02:00
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IApiKeyRepository>().GetManyByServiceAccountIdAsync(default)
|
|
|
|
|
.ReturnsForAnyArgs(resultApiKeys);
|
2023-01-24 16:50:04 +01:00
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetAccessTokens(data.Id);
|
2023-06-13 22:30:44 +02:00
|
|
|
|
await sutProvider.GetDependency<IApiKeyRepository>().Received(1)
|
|
|
|
|
.GetManyByServiceAccountIdAsync(Arg.Any<Guid>());
|
2023-01-24 16:50:04 +01:00
|
|
|
|
Assert.NotEmpty(result.Data);
|
|
|
|
|
Assert.Equal(resultApiKeys.Count, result.Data.Count());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2023-06-13 22:30:44 +02:00
|
|
|
|
public async void RevokeAccessTokens_NoAccess_Throws(SutProvider<ServiceAccountsController> sutProvider,
|
|
|
|
|
RevokeAccessTokensRequest data, ServiceAccount serviceAccount)
|
2023-01-24 16:50:04 +01:00
|
|
|
|
{
|
2023-06-13 22:30:44 +02:00
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(serviceAccount.Id).Returns(serviceAccount);
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), serviceAccount,
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Failed());
|
2023-01-24 16:50:04 +01:00
|
|
|
|
|
2023-06-13 22:30:44 +02:00
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
|
|
|
sutProvider.Sut.RevokeAccessTokensAsync(serviceAccount.Id, data));
|
|
|
|
|
await sutProvider.GetDependency<IRevokeAccessTokensCommand>().DidNotReceiveWithAnyArgs()
|
|
|
|
|
.RevokeAsync(Arg.Any<ServiceAccount>(), Arg.Any<Guid[]>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void RevokeAccessTokens_Success(SutProvider<ServiceAccountsController> sutProvider,
|
|
|
|
|
RevokeAccessTokensRequest data, ServiceAccount serviceAccount)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(serviceAccount.Id).Returns(serviceAccount);
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), serviceAccount,
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Success());
|
2023-01-24 16:50:04 +01:00
|
|
|
|
|
2023-06-13 22:30:44 +02:00
|
|
|
|
await sutProvider.Sut.RevokeAccessTokensAsync(serviceAccount.Id, data);
|
|
|
|
|
await sutProvider.GetDependency<IRevokeAccessTokensCommand>().Received(1)
|
|
|
|
|
.RevokeAsync(Arg.Any<ServiceAccount>(), Arg.Any<Guid[]>());
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
2023-07-12 22:32:40 +02:00
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void BulkDelete_NoServiceAccountsFound_ThrowsNotFound(SutProvider<ServiceAccountsController> sutProvider, List<ServiceAccount> data)
|
|
|
|
|
{
|
|
|
|
|
var ids = data.Select(sa => sa.Id).ToList();
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetManyByIds(Arg.Is(ids)).ReturnsForAnyArgs(new List<ServiceAccount>());
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.BulkDeleteAsync(ids));
|
|
|
|
|
await sutProvider.GetDependency<IDeleteServiceAccountsCommand>().DidNotReceiveWithAnyArgs().DeleteServiceAccounts(Arg.Any<List<ServiceAccount>>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void BulkDelete_ServiceAccountsFoundMisMatch_ThrowsNotFound(SutProvider<ServiceAccountsController> sutProvider, List<ServiceAccount> data, ServiceAccount mockSa)
|
|
|
|
|
{
|
|
|
|
|
data.Add(mockSa);
|
|
|
|
|
var ids = data.Select(sa => sa.Id).ToList();
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetManyByIds(Arg.Is(ids)).ReturnsForAnyArgs(new List<ServiceAccount> { mockSa });
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.BulkDeleteAsync(ids));
|
|
|
|
|
await sutProvider.GetDependency<IDeleteServiceAccountsCommand>().DidNotReceiveWithAnyArgs().DeleteServiceAccounts(Arg.Any<List<ServiceAccount>>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void BulkDelete_OrganizationMistMatch_ThrowsNotFound(SutProvider<ServiceAccountsController> sutProvider, List<ServiceAccount> data)
|
|
|
|
|
{
|
|
|
|
|
var ids = data.Select(sa => sa.Id).ToList();
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetManyByIds(Arg.Is(ids)).ReturnsForAnyArgs(data);
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.BulkDeleteAsync(ids));
|
|
|
|
|
await sutProvider.GetDependency<IDeleteServiceAccountsCommand>().DidNotReceiveWithAnyArgs().DeleteServiceAccounts(Arg.Any<List<ServiceAccount>>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void BulkDelete_NoAccessToSecretsManager_ThrowsNotFound(SutProvider<ServiceAccountsController> sutProvider, List<ServiceAccount> data)
|
|
|
|
|
{
|
|
|
|
|
var ids = data.Select(sa => sa.Id).ToList();
|
|
|
|
|
var organizationId = data.First().OrganizationId;
|
|
|
|
|
foreach (var sa in data)
|
|
|
|
|
{
|
|
|
|
|
sa.OrganizationId = organizationId;
|
|
|
|
|
}
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessSecretsManager(Arg.Is(organizationId)).ReturnsForAnyArgs(false);
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetManyByIds(Arg.Is(ids)).ReturnsForAnyArgs(data);
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.BulkDeleteAsync(ids));
|
|
|
|
|
await sutProvider.GetDependency<IDeleteServiceAccountsCommand>().DidNotReceiveWithAnyArgs().DeleteServiceAccounts(Arg.Any<List<ServiceAccount>>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void BulkDelete_ReturnsAccessDeniedForProjectsWithoutAccess_Success(SutProvider<ServiceAccountsController> sutProvider, List<ServiceAccount> data)
|
|
|
|
|
{
|
|
|
|
|
var ids = data.Select(sa => sa.Id).ToList();
|
|
|
|
|
var organizationId = data.First().OrganizationId;
|
|
|
|
|
foreach (var sa in data)
|
|
|
|
|
{
|
|
|
|
|
sa.OrganizationId = organizationId;
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), sa,
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Success());
|
|
|
|
|
}
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), data.First(),
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).Returns(AuthorizationResult.Failed());
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessSecretsManager(Arg.Is(organizationId)).ReturnsForAnyArgs(true);
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetManyByIds(Arg.Is(ids)).ReturnsForAnyArgs(data);
|
|
|
|
|
|
|
|
|
|
var results = await sutProvider.Sut.BulkDeleteAsync(ids);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(data.Count, results.Data.Count());
|
|
|
|
|
Assert.Equal("access denied", results.Data.First().Error);
|
|
|
|
|
|
|
|
|
|
data.Remove(data.First());
|
|
|
|
|
await sutProvider.GetDependency<IDeleteServiceAccountsCommand>().Received(1)
|
|
|
|
|
.DeleteServiceAccounts(Arg.Is(AssertHelper.AssertPropertyEqual(data)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void BulkDelete_Success(SutProvider<ServiceAccountsController> sutProvider, List<ServiceAccount> data)
|
|
|
|
|
{
|
|
|
|
|
var ids = data.Select(sa => sa.Id).ToList();
|
|
|
|
|
var organizationId = data.First().OrganizationId;
|
|
|
|
|
foreach (var sa in data)
|
|
|
|
|
{
|
|
|
|
|
sa.OrganizationId = organizationId;
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), sa,
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Success());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessSecretsManager(Arg.Is(organizationId)).ReturnsForAnyArgs(true);
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetManyByIds(Arg.Is(ids)).ReturnsForAnyArgs(data);
|
|
|
|
|
|
|
|
|
|
var results = await sutProvider.Sut.BulkDeleteAsync(ids);
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IDeleteServiceAccountsCommand>().Received(1)
|
|
|
|
|
.DeleteServiceAccounts(Arg.Is(AssertHelper.AssertPropertyEqual(data)));
|
|
|
|
|
Assert.Equal(data.Count, results.Data.Count());
|
|
|
|
|
foreach (var result in results.Data)
|
|
|
|
|
{
|
|
|
|
|
Assert.Null(result.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-04 23:51:12 +02:00
|
|
|
|
|
|
|
|
|
private static void ArrangeCreateServiceAccountAutoScalingTest(int newSlotsRequired, SutProvider<ServiceAccountsController> sutProvider,
|
|
|
|
|
ServiceAccountCreateRequestModel data, Organization organization)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), data.ToServiceAccount(organization.Id),
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Success());
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(Arg.Is(organization.Id)).Returns(organization);
|
|
|
|
|
sutProvider.GetDependency<ICountNewServiceAccountSlotsRequiredQuery>()
|
|
|
|
|
.CountNewServiceAccountSlotsRequiredAsync(organization.Id, 1)
|
|
|
|
|
.ReturnsForAnyArgs(newSlotsRequired);
|
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid());
|
|
|
|
|
var resultServiceAccount = data.ToServiceAccount(organization.Id);
|
|
|
|
|
sutProvider.GetDependency<ICreateServiceAccountCommand>().CreateAsync(default, default)
|
|
|
|
|
.ReturnsForAnyArgs(resultServiceAccount);
|
|
|
|
|
}
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|