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; using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; using Bit.Test.Common.Helpers; using NSubstitute; using NSubstitute.ReturnsExtensions; using Xunit; namespace Bit.Api.Test.SecretsManager.Controllers; [ControllerCustomize(typeof(AccessPoliciesController))] [SutProviderCustomize] [JsonDocumentCustomize] public class AccessPoliciesControllerTests { [Theory] [BitAutoData] public async void GetAccessPoliciesByProject_ReturnsEmptyList(SutProvider sutProvider, Guid id) { var result = await sutProvider.Sut.GetProjectAccessPoliciesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByGrantedProjectIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id))); Assert.Empty(result.GroupAccessPolicies); Assert.Empty(result.UserAccessPolicies); Assert.Empty(result.ServiceAccountAccessPolicies); } [Theory] [BitAutoData] public async void GetAccessPoliciesByProject_Success(SutProvider sutProvider, Guid id, UserProjectAccessPolicy resultAccessPolicy) { sutProvider.GetDependency().GetManyByGrantedProjectIdAsync(default) .ReturnsForAnyArgs(new List { resultAccessPolicy }); var result = await sutProvider.Sut.GetProjectAccessPoliciesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByGrantedProjectIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id))); Assert.Empty(result.GroupAccessPolicies); Assert.NotEmpty(result.UserAccessPolicies); Assert.Empty(result.ServiceAccountAccessPolicies); } [Theory] [BitAutoData] public async void CreateAccessPolicies_Success(SutProvider sutProvider, Guid id, UserProjectAccessPolicy data, AccessPoliciesCreateRequest request) { sutProvider.GetDependency().CreateAsync(default) .ReturnsForAnyArgs(new List { data }); var result = await sutProvider.Sut.CreateProjectAccessPoliciesAsync(id, request); await sutProvider.GetDependency().Received(1) .CreateAsync(Arg.Any>()); } [Theory] [BitAutoData] public async void UpdateAccessPolicies_Success(SutProvider sutProvider, Guid id, UserProjectAccessPolicy data, AccessPolicyUpdateRequest request) { sutProvider.GetDependency().UpdateAsync(default, default, default) .ReturnsForAnyArgs(data); var result = await sutProvider.Sut.UpdateAccessPolicyAsync(id, request); await sutProvider.GetDependency().Received(1) .UpdateAsync(Arg.Any(), Arg.Is(request.Read), Arg.Is(request.Write)); } [Theory] [BitAutoData] public async void DeleteAccessPolicies_Success(SutProvider sutProvider, Guid id) { sutProvider.GetDependency().DeleteAsync(default).ReturnsNull(); await sutProvider.Sut.DeleteAccessPolicyAsync(id); await sutProvider.GetDependency().Received(1) .DeleteAsync(Arg.Any()); } }