|
|
|
|
@ -14,7 +14,6 @@ using Bit.Test.Common.AutoFixture.Attributes;
@@ -14,7 +14,6 @@ using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
using Bit.Test.Common.Helpers; |
|
|
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
|
|
using NSubstitute; |
|
|
|
|
using NSubstitute.ReturnsExtensions; |
|
|
|
|
using Xunit; |
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Test.SecretsManager.Controllers; |
|
|
|
|
@ -26,37 +25,43 @@ public class ServiceAccountsControllerTests
@@ -26,37 +25,43 @@ public class ServiceAccountsControllerTests
|
|
|
|
|
{ |
|
|
|
|
[Theory] |
|
|
|
|
[BitAutoData] |
|
|
|
|
public async void GetServiceAccountsByOrganization_ReturnsEmptyList(SutProvider<ServiceAccountsController> sutProvider, Guid id) |
|
|
|
|
public async void GetServiceAccountsByOrganization_ReturnsEmptyList( |
|
|
|
|
SutProvider<ServiceAccountsController> sutProvider, Guid id) |
|
|
|
|
{ |
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessSecretsManager(id).Returns(true); |
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); |
|
|
|
|
var result = await sutProvider.Sut.ListByOrganizationAsync(id); |
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IServiceAccountRepository>().Received(1) |
|
|
|
|
.GetManyByOrganizationIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Any<Guid>(), Arg.Any<AccessClientType>()); |
|
|
|
|
.GetManyByOrganizationIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Any<Guid>(), |
|
|
|
|
Arg.Any<AccessClientType>()); |
|
|
|
|
|
|
|
|
|
Assert.Empty(result.Data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Theory] |
|
|
|
|
[BitAutoData] |
|
|
|
|
public async void GetServiceAccountsByOrganization_Success(SutProvider<ServiceAccountsController> sutProvider, ServiceAccount resultServiceAccount) |
|
|
|
|
public async void GetServiceAccountsByOrganization_Success(SutProvider<ServiceAccountsController> sutProvider, |
|
|
|
|
ServiceAccount resultServiceAccount) |
|
|
|
|
{ |
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessSecretsManager(default).ReturnsForAnyArgs(true); |
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); |
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetManyByOrganizationIdAsync(default, default, default).ReturnsForAnyArgs(new List<ServiceAccount>() { resultServiceAccount }); |
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetManyByOrganizationIdAsync(default, default, default) |
|
|
|
|
.ReturnsForAnyArgs(new List<ServiceAccount> { resultServiceAccount }); |
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.ListByOrganizationAsync(resultServiceAccount.OrganizationId); |
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IServiceAccountRepository>().Received(1) |
|
|
|
|
.GetManyByOrganizationIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(resultServiceAccount.OrganizationId)), Arg.Any<Guid>(), Arg.Any<AccessClientType>()); |
|
|
|
|
.GetManyByOrganizationIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(resultServiceAccount.OrganizationId)), |
|
|
|
|
Arg.Any<Guid>(), Arg.Any<AccessClientType>()); |
|
|
|
|
Assert.NotEmpty(result.Data); |
|
|
|
|
Assert.Single(result.Data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Theory] |
|
|
|
|
[BitAutoData] |
|
|
|
|
public async void GetServiceAccountsByOrganization_AccessDenied_Throws(SutProvider<ServiceAccountsController> sutProvider, Guid orgId) |
|
|
|
|
public async void GetServiceAccountsByOrganization_AccessDenied_Throws( |
|
|
|
|
SutProvider<ServiceAccountsController> sutProvider, Guid orgId) |
|
|
|
|
{ |
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessSecretsManager(default).ReturnsForAnyArgs(false); |
|
|
|
|
|
|
|
|
|
@ -66,14 +71,16 @@ public class ServiceAccountsControllerTests
@@ -66,14 +71,16 @@ public class ServiceAccountsControllerTests
|
|
|
|
|
|
|
|
|
|
[Theory] |
|
|
|
|
[BitAutoData] |
|
|
|
|
public async void CreateServiceAccount_NoAccess_Throws(SutProvider<ServiceAccountsController> sutProvider, ServiceAccountCreateRequestModel data, Guid organizationId) |
|
|
|
|
public async void CreateServiceAccount_NoAccess_Throws(SutProvider<ServiceAccountsController> sutProvider, |
|
|
|
|
ServiceAccountCreateRequestModel data, Guid organizationId) |
|
|
|
|
{ |
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>() |
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), data.ToServiceAccount(organizationId), |
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Failed()); |
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); |
|
|
|
|
var resultServiceAccount = data.ToServiceAccount(organizationId); |
|
|
|
|
sutProvider.GetDependency<ICreateServiceAccountCommand>().CreateAsync(default, default).ReturnsForAnyArgs(resultServiceAccount); |
|
|
|
|
sutProvider.GetDependency<ICreateServiceAccountCommand>().CreateAsync(default, default) |
|
|
|
|
.ReturnsForAnyArgs(resultServiceAccount); |
|
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.CreateAsync(organizationId, data)); |
|
|
|
|
await sutProvider.GetDependency<ICreateServiceAccountCommand>().DidNotReceiveWithAnyArgs() |
|
|
|
|
@ -82,30 +89,35 @@ public class ServiceAccountsControllerTests
@@ -82,30 +89,35 @@ public class ServiceAccountsControllerTests
|
|
|
|
|
|
|
|
|
|
[Theory] |
|
|
|
|
[BitAutoData] |
|
|
|
|
public async void CreateServiceAccount_Success(SutProvider<ServiceAccountsController> sutProvider, ServiceAccountCreateRequestModel data, Guid organizationId) |
|
|
|
|
public async void CreateServiceAccount_Success(SutProvider<ServiceAccountsController> sutProvider, |
|
|
|
|
ServiceAccountCreateRequestModel data, Guid organizationId) |
|
|
|
|
{ |
|
|
|
|
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()); |
|
|
|
|
var resultServiceAccount = data.ToServiceAccount(organizationId); |
|
|
|
|
sutProvider.GetDependency<ICreateServiceAccountCommand>().CreateAsync(default, default).ReturnsForAnyArgs(resultServiceAccount); |
|
|
|
|
sutProvider.GetDependency<ICreateServiceAccountCommand>().CreateAsync(default, default) |
|
|
|
|
.ReturnsForAnyArgs(resultServiceAccount); |
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.CreateAsync(organizationId, data); |
|
|
|
|
await sutProvider.GetDependency<ICreateServiceAccountCommand>().Received(1) |
|
|
|
|
.CreateAsync(Arg.Any<ServiceAccount>(), Arg.Any<Guid>()); |
|
|
|
|
.CreateAsync(Arg.Any<ServiceAccount>(), Arg.Any<Guid>()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Theory] |
|
|
|
|
[BitAutoData] |
|
|
|
|
public async void UpdateServiceAccount_NoAccess_Throws(SutProvider<ServiceAccountsController> sutProvider, ServiceAccountUpdateRequestModel data, ServiceAccount existingServiceAccount) |
|
|
|
|
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); |
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(existingServiceAccount.Id) |
|
|
|
|
.ReturnsForAnyArgs(existingServiceAccount); |
|
|
|
|
var resultServiceAccount = data.ToServiceAccount(existingServiceAccount.Id); |
|
|
|
|
sutProvider.GetDependency<IUpdateServiceAccountCommand>().UpdateAsync(default).ReturnsForAnyArgs(resultServiceAccount); |
|
|
|
|
sutProvider.GetDependency<IUpdateServiceAccountCommand>().UpdateAsync(default) |
|
|
|
|
.ReturnsForAnyArgs(resultServiceAccount); |
|
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.UpdateAsync(existingServiceAccount.Id, data)); |
|
|
|
|
await sutProvider.GetDependency<IUpdateServiceAccountCommand>().DidNotReceiveWithAnyArgs() |
|
|
|
|
@ -114,102 +126,136 @@ public class ServiceAccountsControllerTests
@@ -114,102 +126,136 @@ public class ServiceAccountsControllerTests
|
|
|
|
|
|
|
|
|
|
[Theory] |
|
|
|
|
[BitAutoData] |
|
|
|
|
public async void UpdateServiceAccount_Success(SutProvider<ServiceAccountsController> sutProvider, ServiceAccountUpdateRequestModel data, ServiceAccount existingServiceAccount) |
|
|
|
|
public async void UpdateServiceAccount_Success(SutProvider<ServiceAccountsController> sutProvider, |
|
|
|
|
ServiceAccountUpdateRequestModel data, ServiceAccount existingServiceAccount) |
|
|
|
|
{ |
|
|
|
|
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); |
|
|
|
|
sutProvider.GetDependency<IUpdateServiceAccountCommand>().UpdateAsync(default) |
|
|
|
|
.ReturnsForAnyArgs(resultServiceAccount); |
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.UpdateAsync(existingServiceAccount.Id, data); |
|
|
|
|
await sutProvider.GetDependency<IUpdateServiceAccountCommand>().Received(1) |
|
|
|
|
.UpdateAsync(Arg.Any<ServiceAccount>()); |
|
|
|
|
.UpdateAsync(Arg.Any<ServiceAccount>()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Theory] |
|
|
|
|
[BitAutoData] |
|
|
|
|
public async void CreateAccessToken_Success(SutProvider<ServiceAccountsController> sutProvider, AccessTokenCreateRequestModel data, Guid serviceAccountId) |
|
|
|
|
public async void CreateAccessToken_NoAccess_Throws(SutProvider<ServiceAccountsController> sutProvider, |
|
|
|
|
AccessTokenCreateRequestModel data, ServiceAccount serviceAccount) |
|
|
|
|
{ |
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); |
|
|
|
|
var resultAccessToken = data.ToApiKey(serviceAccountId); |
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<ICreateAccessTokenCommand>().CreateAsync(default, default).ReturnsForAnyArgs(resultAccessToken); |
|
|
|
|
sutProvider.GetDependency<ICreateAccessTokenCommand>().CreateAsync(default) |
|
|
|
|
.ReturnsForAnyArgs(resultAccessToken); |
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.CreateAccessTokenAsync(serviceAccountId, data); |
|
|
|
|
await sutProvider.GetDependency<ICreateAccessTokenCommand>().Received(1) |
|
|
|
|
.CreateAsync(Arg.Any<ApiKey>(), Arg.Any<Guid>()); |
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => |
|
|
|
|
sutProvider.Sut.CreateAccessTokenAsync(serviceAccount.Id, data)); |
|
|
|
|
await sutProvider.GetDependency<ICreateAccessTokenCommand>().DidNotReceiveWithAnyArgs() |
|
|
|
|
.CreateAsync(Arg.Any<ApiKey>()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Theory] |
|
|
|
|
[BitAutoData] |
|
|
|
|
public async void GetAccessTokens_NoServiceAccount_ThrowsNotFound(SutProvider<ServiceAccountsController> sutProvider, Guid serviceAccountId) |
|
|
|
|
public async void CreateAccessToken_Success(SutProvider<ServiceAccountsController> sutProvider, |
|
|
|
|
AccessTokenCreateRequestModel data, ServiceAccount serviceAccount) |
|
|
|
|
{ |
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); |
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(default).ReturnsNull(); |
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.GetAccessTokens(serviceAccountId)); |
|
|
|
|
sutProvider.GetDependency<ICreateAccessTokenCommand>().CreateAsync(default) |
|
|
|
|
.ReturnsForAnyArgs(resultAccessToken); |
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IApiKeyRepository>().DidNotReceiveWithAnyArgs().GetManyByServiceAccountIdAsync(Arg.Any<Guid>()); |
|
|
|
|
await sutProvider.Sut.CreateAccessTokenAsync(serviceAccount.Id, data); |
|
|
|
|
await sutProvider.GetDependency<ICreateAccessTokenCommand>().Received(1) |
|
|
|
|
.CreateAsync(Arg.Any<ApiKey>()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Theory] |
|
|
|
|
[BitAutoData] |
|
|
|
|
public async void GetAccessTokens_Admin_Success(SutProvider<ServiceAccountsController> sutProvider, ServiceAccount data, Guid userId, ICollection<ApiKey> resultApiKeys) |
|
|
|
|
public async void GetAccessTokens_NoAccess_Throws(SutProvider<ServiceAccountsController> sutProvider, |
|
|
|
|
ServiceAccount data, ICollection<ApiKey> resultApiKeys) |
|
|
|
|
{ |
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(userId); |
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessSecretsManager(data.OrganizationId).Returns(true); |
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationAdmin(data.OrganizationId).Returns(true); |
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(default).ReturnsForAnyArgs(data); |
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>() |
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), data, |
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Failed()); |
|
|
|
|
|
|
|
|
|
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()); |
|
|
|
|
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>()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Theory] |
|
|
|
|
[BitAutoData] |
|
|
|
|
public async void GetAccessTokens_User_Success(SutProvider<ServiceAccountsController> sutProvider, ServiceAccount data, Guid userId, ICollection<ApiKey> resultApiKeys) |
|
|
|
|
public async void GetAccessTokens_Success(SutProvider<ServiceAccountsController> sutProvider, ServiceAccount data, |
|
|
|
|
ICollection<ApiKey> resultApiKeys) |
|
|
|
|
{ |
|
|
|
|
sutProvider.GetDependency<IUserService>().GetProperUserId(default).ReturnsForAnyArgs(userId); |
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().AccessSecretsManager(data.OrganizationId).Returns(true); |
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationAdmin(data.OrganizationId).Returns(false); |
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().UserHasReadAccessToServiceAccount(default, default).ReturnsForAnyArgs(true); |
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(default).ReturnsForAnyArgs(data); |
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>() |
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), data, |
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Success()); |
|
|
|
|
|
|
|
|
|
foreach (var apiKey in resultApiKeys) |
|
|
|
|
{ |
|
|
|
|
apiKey.Scope = "[\"api.secrets\"]"; |
|
|
|
|
} |
|
|
|
|
sutProvider.GetDependency<IApiKeyRepository>().GetManyByServiceAccountIdAsync(default).ReturnsForAnyArgs(resultApiKeys); |
|
|
|
|
|
|
|
|
|
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>()); |
|
|
|
|
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) |
|
|
|
|
public async void RevokeAccessTokens_NoAccess_Throws(SutProvider<ServiceAccountsController> sutProvider, |
|
|
|
|
RevokeAccessTokensRequest data, ServiceAccount serviceAccount) |
|
|
|
|
{ |
|
|
|
|
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); |
|
|
|
|
sutProvider.GetDependency<IServiceAccountRepository>().GetByIdAsync(serviceAccount.Id).Returns(serviceAccount); |
|
|
|
|
sutProvider.GetDependency<IAuthorizationService>() |
|
|
|
|
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), serviceAccount, |
|
|
|
|
Arg.Any<IEnumerable<IAuthorizationRequirement>>()).ReturnsForAnyArgs(AuthorizationResult.Failed()); |
|
|
|
|
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.GetAccessTokens(data.Id)); |
|
|
|
|
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()); |
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IApiKeyRepository>().DidNotReceiveWithAnyArgs().GetManyByServiceAccountIdAsync(Arg.Any<Guid>()); |
|
|
|
|
await sutProvider.Sut.RevokeAccessTokensAsync(serviceAccount.Id, data); |
|
|
|
|
await sutProvider.GetDependency<IRevokeAccessTokensCommand>().Received(1) |
|
|
|
|
.RevokeAsync(Arg.Any<ServiceAccount>(), Arg.Any<Guid[]>()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|