2023-01-24 19:57:28 +01:00
|
|
|
|
using Bit.Api.SecretsManager.Controllers;
|
|
|
|
|
using Bit.Api.SecretsManager.Models.Request;
|
|
|
|
|
using Bit.Core.SecretsManager.Commands.AccessPolicies.Interfaces;
|
|
|
|
|
using Bit.Core.SecretsManager.Entities;
|
|
|
|
|
using Bit.Core.SecretsManager.Repositories;
|
2023-01-20 00:31:19 +01:00
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
using Bit.Test.Common.Helpers;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using NSubstitute.ReturnsExtensions;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2023-01-24 19:57:28 +01:00
|
|
|
|
namespace Bit.Api.Test.SecretsManager.Controllers;
|
2023-01-20 00:31:19 +01:00
|
|
|
|
|
|
|
|
|
[ControllerCustomize(typeof(AccessPoliciesController))]
|
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
[JsonDocumentCustomize]
|
|
|
|
|
public class AccessPoliciesControllerTests
|
|
|
|
|
{
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void GetAccessPoliciesByProject_ReturnsEmptyList(SutProvider<AccessPoliciesController> sutProvider,
|
|
|
|
|
Guid id)
|
|
|
|
|
{
|
|
|
|
|
var result = await sutProvider.Sut.GetProjectAccessPoliciesAsync(id);
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IAccessPolicyRepository>().Received(1)
|
2023-02-02 19:25:14 +01:00
|
|
|
|
.GetManyByGrantedProjectIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)));
|
2023-01-20 00:31:19 +01:00
|
|
|
|
|
|
|
|
|
Assert.Empty(result.GroupAccessPolicies);
|
|
|
|
|
Assert.Empty(result.UserAccessPolicies);
|
|
|
|
|
Assert.Empty(result.ServiceAccountAccessPolicies);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void GetAccessPoliciesByProject_Success(SutProvider<AccessPoliciesController> sutProvider, Guid id,
|
|
|
|
|
UserProjectAccessPolicy resultAccessPolicy)
|
|
|
|
|
{
|
2023-02-02 19:25:14 +01:00
|
|
|
|
sutProvider.GetDependency<IAccessPolicyRepository>().GetManyByGrantedProjectIdAsync(default)
|
2023-01-20 00:31:19 +01:00
|
|
|
|
.ReturnsForAnyArgs(new List<BaseAccessPolicy> { resultAccessPolicy });
|
|
|
|
|
|
|
|
|
|
var result = await sutProvider.Sut.GetProjectAccessPoliciesAsync(id);
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IAccessPolicyRepository>().Received(1)
|
2023-02-02 19:25:14 +01:00
|
|
|
|
.GetManyByGrantedProjectIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)));
|
2023-01-20 00:31:19 +01:00
|
|
|
|
|
|
|
|
|
Assert.Empty(result.GroupAccessPolicies);
|
|
|
|
|
Assert.NotEmpty(result.UserAccessPolicies);
|
|
|
|
|
Assert.Empty(result.ServiceAccountAccessPolicies);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void CreateAccessPolicies_Success(SutProvider<AccessPoliciesController> sutProvider, Guid id,
|
|
|
|
|
UserProjectAccessPolicy data, AccessPoliciesCreateRequest request)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<ICreateAccessPoliciesCommand>().CreateAsync(default)
|
|
|
|
|
.ReturnsForAnyArgs(new List<BaseAccessPolicy> { data });
|
|
|
|
|
var result = await sutProvider.Sut.CreateProjectAccessPoliciesAsync(id, request);
|
|
|
|
|
await sutProvider.GetDependency<ICreateAccessPoliciesCommand>().Received(1)
|
|
|
|
|
.CreateAsync(Arg.Any<List<BaseAccessPolicy>>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void UpdateAccessPolicies_Success(SutProvider<AccessPoliciesController> sutProvider, Guid id,
|
|
|
|
|
UserProjectAccessPolicy data, AccessPolicyUpdateRequest request)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IUpdateAccessPolicyCommand>().UpdateAsync(default, default, default)
|
|
|
|
|
.ReturnsForAnyArgs(data);
|
|
|
|
|
var result = await sutProvider.Sut.UpdateAccessPolicyAsync(id, request);
|
|
|
|
|
await sutProvider.GetDependency<IUpdateAccessPolicyCommand>().Received(1)
|
|
|
|
|
.UpdateAsync(Arg.Any<Guid>(), Arg.Is(request.Read), Arg.Is(request.Write));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async void DeleteAccessPolicies_Success(SutProvider<AccessPoliciesController> sutProvider, Guid id)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IDeleteAccessPolicyCommand>().DeleteAsync(default).ReturnsNull();
|
|
|
|
|
await sutProvider.Sut.DeleteAccessPolicyAsync(id);
|
|
|
|
|
await sutProvider.GetDependency<IDeleteAccessPolicyCommand>().Received(1)
|
|
|
|
|
.DeleteAsync(Arg.Any<Guid>());
|
|
|
|
|
}
|
|
|
|
|
}
|