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;
|
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Exceptions;
|
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;
|
|
|
|
|
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;
|
2023-01-24 16:50:04 +01:00
|
|
|
|
using NSubstitute.ReturnsExtensions;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
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]
|
|
|
|
|
public async void GetServiceAccountsByOrganization_ReturnsEmptyList(SutProvider<ServiceAccountsController> sutProvider, Guid id)
|
|
|
|
|
{
|
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
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IServiceAccountRepository>().Received(1)
|
2023-01-24 16:50:04 +01:00
|
|
|
|
.GetManyByOrganizationIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Any<Guid>(), Arg.Any<AccessClientType>());
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
|
|
|
|
Assert.Empty(result.Data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void GetServiceAccountsByOrganization_Success(SutProvider<ServiceAccountsController> sutProvider, ServiceAccount resultServiceAccount)
|
|
|
|
|
{
|
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());
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetManyByOrganizationIdAsync(default, default, default).ReturnsForAnyArgs(new List<ServiceAccount>() { resultServiceAccount });
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
2023-01-31 18:38:53 +01:00
|
|
|
|
var result = await sutProvider.Sut.ListByOrganizationAsync(resultServiceAccount.OrganizationId);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IServiceAccountRepository>().Received(1)
|
2023-01-24 16:50:04 +01:00
|
|
|
|
.GetManyByOrganizationIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(resultServiceAccount.OrganizationId)), Arg.Any<Guid>(), Arg.Any<AccessClientType>());
|
|
|
|
|
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]
|
|
|
|
|
public async void GetServiceAccountsByOrganization_AccessDenied_Throws(SutProvider<ServiceAccountsController> sutProvider, Guid orgId)
|
|
|
|
|
{
|
|
|
|
|
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-05-31 20:49:58 +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-02-02 19:25:14 +01: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-01-24 16:50:04 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2023-05-31 20:49:58 +02:00
|
|
|
|
public async void CreateServiceAccount_Success(SutProvider<ServiceAccountsController> sutProvider, ServiceAccountCreateRequestModel data, Guid organizationId)
|
2023-01-24 16:50:04 +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.Success());
|
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid());
|
2023-01-24 16:50:04 +01:00
|
|
|
|
var resultServiceAccount = data.ToServiceAccount(organizationId);
|
2023-02-02 19:25:14 +01: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)
|
|
|
|
|
.CreateAsync(Arg.Any<ServiceAccount>(), Arg.Any<Guid>());
|
|
|
|
|
}
|
2023-01-24 16:50:04 +01:00
|
|
|
|
|
2023-05-31 20:49:58 +02:00
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void UpdateServiceAccount_NoAccess_Throws(SutProvider<ServiceAccountsController> sutProvider, ServiceAccountUpdateRequestModel data, ServiceAccount existingServiceAccount)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>()
|
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), data.ToServiceAccount(existingServiceAccount.Id),
|
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Failed());
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(existingServiceAccount.Id).ReturnsForAnyArgs(existingServiceAccount);
|
|
|
|
|
var resultServiceAccount = data.ToServiceAccount(existingServiceAccount.Id);
|
|
|
|
|
sutProvider.GetDependency<IUpdateServiceAccountCommand>().UpdateAsync(default).ReturnsForAnyArgs(resultServiceAccount);
|
|
|
|
|
|
|
|
|
|
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-05-31 20:49:58 +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);
|
|
|
|
|
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-05-31 20:49:58 +02:00
|
|
|
|
.UpdateAsync(Arg.Any<ServiceAccount>());
|
2023-01-13 15:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void CreateAccessToken_Success(SutProvider<ServiceAccountsController> sutProvider, AccessTokenCreateRequestModel data, Guid serviceAccountId)
|
|
|
|
|
{
|
2023-01-24 16:50:04 +01:00
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid());
|
2023-01-13 15:02:53 +01:00
|
|
|
|
var resultAccessToken = data.ToApiKey(serviceAccountId);
|
|
|
|
|
|
2023-01-24 16:50:04 +01:00
|
|
|
|
sutProvider.GetDependency<ICreateAccessTokenCommand>().CreateAsync(default, default).ReturnsForAnyArgs(resultAccessToken);
|
2023-01-13 15:02:53 +01:00
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.CreateAccessTokenAsync(serviceAccountId, data);
|
|
|
|
|
await sutProvider.GetDependency<ICreateAccessTokenCommand>().Received(1)
|
2023-01-24 16:50:04 +01:00
|
|
|
|
.CreateAsync(Arg.Any<ApiKey>(), Arg.Any<Guid>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void GetAccessTokens_NoServiceAccount_ThrowsNotFound(SutProvider<ServiceAccountsController> sutProvider, Guid serviceAccountId)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid());
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(default).ReturnsNull();
|
|
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.GetAccessTokens(serviceAccountId));
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IApiKeyRepository>().DidNotReceiveWithAnyArgs().GetManyByServiceAccountIdAsync(Arg.Any<Guid>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void GetAccessTokens_Admin_Success(SutProvider<ServiceAccountsController> sutProvider, ServiceAccount data, Guid userId, ICollection<ApiKey> resultApiKeys)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(userId);
|
2023-01-31 18:38:53 +01:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessSecretsManager(data.OrganizationId).Returns(true);
|
2023-01-24 16:50:04 +01:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationAdmin(data.OrganizationId).Returns(true);
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(default).ReturnsForAnyArgs(data);
|
|
|
|
|
foreach (var apiKey in resultApiKeys)
|
|
|
|
|
{
|
|
|
|
|
apiKey.Scope = "[\"api.secrets\"]";
|
|
|
|
|
}
|
|
|
|
|
sutProvider.GetDependency<IApiKeyRepository>().GetManyByServiceAccountIdAsync(default).ReturnsForAnyArgs(resultApiKeys);
|
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetAccessTokens(data.Id);
|
|
|
|
|
await sutProvider.GetDependency<IApiKeyRepository>().Received(1).GetManyByServiceAccountIdAsync(Arg.Any<Guid>());
|
|
|
|
|
Assert.NotEmpty(result.Data);
|
|
|
|
|
Assert.Equal(resultApiKeys.Count, result.Data.Count());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void GetAccessTokens_User_Success(SutProvider<ServiceAccountsController> sutProvider, ServiceAccount data, Guid userId, ICollection<ApiKey> resultApiKeys)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(userId);
|
2023-01-31 18:38:53 +01:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessSecretsManager(data.OrganizationId).Returns(true);
|
2023-01-24 16:50:04 +01:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationAdmin(data.OrganizationId).Returns(false);
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().UserHasReadAccessToServiceAccount(default, default).ReturnsForAnyArgs(true);
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(default).ReturnsForAnyArgs(data);
|
|
|
|
|
foreach (var apiKey in resultApiKeys)
|
|
|
|
|
{
|
|
|
|
|
apiKey.Scope = "[\"api.secrets\"]";
|
|
|
|
|
}
|
|
|
|
|
sutProvider.GetDependency<IApiKeyRepository>().GetManyByServiceAccountIdAsync(default).ReturnsForAnyArgs(resultApiKeys);
|
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetAccessTokens(data.Id);
|
|
|
|
|
await sutProvider.GetDependency<IApiKeyRepository>().Received(1).GetManyByServiceAccountIdAsync(Arg.Any<Guid>());
|
|
|
|
|
Assert.NotEmpty(result.Data);
|
|
|
|
|
Assert.Equal(resultApiKeys.Count, result.Data.Count());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void GetAccessTokens_UserNoAccess_ThrowsNotFound(SutProvider<ServiceAccountsController> sutProvider, ServiceAccount data, Guid userId, ICollection<ApiKey> resultApiKeys)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(userId);
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationAdmin(data.OrganizationId).Returns(false);
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().UserHasReadAccessToServiceAccount(default, default).ReturnsForAnyArgs(false);
|
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(default).ReturnsForAnyArgs(data);
|
|
|
|
|
foreach (var apiKey in resultApiKeys)
|
|
|
|
|
{
|
|
|
|
|
apiKey.Scope = "[\"api.secrets\"]";
|
|
|
|
|
}
|
|
|
|
|
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-13 15:02:53 +01:00
|
|
|
|
}
|
|
|
|
|
}
|