using Bit.Api.SecretsManager.Controllers; using Bit.Api.SecretsManager.Models.Request; using Bit.Api.Test.SecretsManager.Enums; using Bit.Core.Context; using Bit.Core.Entities; using Bit.Core.Enums; using Bit.Core.Exceptions; using Bit.Core.Repositories; using Bit.Core.SecretsManager.Commands.AccessPolicies.Interfaces; using Bit.Core.SecretsManager.Entities; using Bit.Core.SecretsManager.Repositories; using Bit.Core.Services; using Bit.Core.Test.SecretsManager.AutoFixture.ProjectsFixture; 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] [ProjectCustomize] [JsonDocumentCustomize] public class AccessPoliciesControllerTests { private const int _overMax = 16; private static AccessPoliciesCreateRequest AddRequestsOverMax(AccessPoliciesCreateRequest request) { var newRequests = new List(); for (var i = 0; i < _overMax; i++) { newRequests.Add(new AccessPolicyRequest { GranteeId = new Guid(), Read = true, Write = true }); } request.UserAccessPolicyRequests = newRequests; return request; } private static List AddRequestsOverMax(List request) { for (var i = 0; i < _overMax; i++) { request.Add(new GrantedAccessPolicyRequest { GrantedId = new Guid() }); } return request; } private static void SetupAdmin(SutProvider sutProvider, Guid organizationId) { sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(true); sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); sutProvider.GetDependency().OrganizationAdmin(organizationId).Returns(true); } private static void SetupUserWithPermission(SutProvider sutProvider, Guid organizationId) { sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(true); sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); sutProvider.GetDependency().OrganizationAdmin(organizationId).Returns(false); sutProvider.GetDependency().OrganizationUser(default).ReturnsForAnyArgs(true); } private static void SetupUserWithoutPermission(SutProvider sutProvider, Guid organizationId) { sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(true); sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); sutProvider.GetDependency().OrganizationAdmin(organizationId).Returns(false); sutProvider.GetDependency().OrganizationUser(default).ReturnsForAnyArgs(true); } private static void SetupPermission(SutProvider sutProvider, PermissionType permissionType, Guid orgId) { switch (permissionType) { case PermissionType.RunAsAdmin: SetupAdmin(sutProvider, orgId); break; case PermissionType.RunAsUserWithPermission: SetupUserWithPermission(sutProvider, orgId); break; } } [Theory] [BitAutoData(PermissionType.RunAsAdmin)] [BitAutoData(PermissionType.RunAsUserWithPermission)] public async void GetProjectAccessPolicies_ReturnsEmptyList( PermissionType permissionType, SutProvider sutProvider, Guid id, Project data) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(data); switch (permissionType) { case PermissionType.RunAsAdmin: SetupAdmin(sutProvider, data.OrganizationId); sutProvider.GetDependency().AccessToProjectAsync(Arg.Any(), Arg.Any(), AccessClientType.NoAccessCheck) .Returns((true, true)); break; case PermissionType.RunAsUserWithPermission: SetupUserWithPermission(sutProvider, data.OrganizationId); sutProvider.GetDependency().AccessToProjectAsync(Arg.Any(), Arg.Any(), AccessClientType.User) .Returns((true, true)); break; } var result = await sutProvider.Sut.GetProjectAccessPoliciesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByGrantedProjectIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Any()); Assert.Empty(result.GroupAccessPolicies); Assert.Empty(result.UserAccessPolicies); Assert.Empty(result.ServiceAccountAccessPolicies); } [Theory] [BitAutoData] public async void GetProjectAccessPolicies_UserWithoutPermission_Throws( SutProvider sutProvider, Guid id, Project data) { SetupUserWithoutPermission(sutProvider, data.OrganizationId); sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(data); sutProvider.GetDependency().AccessToProjectAsync(default, default, default) .Returns((false, false)); await Assert.ThrowsAsync(() => sutProvider.Sut.GetProjectAccessPoliciesAsync(id)); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .GetManyByGrantedProjectIdAsync(Arg.Any(), Arg.Any()); } [Theory] [BitAutoData(PermissionType.RunAsAdmin)] [BitAutoData(PermissionType.RunAsUserWithPermission)] public async void GetProjectAccessPolicies_Success( PermissionType permissionType, SutProvider sutProvider, Guid id, Project data, UserProjectAccessPolicy resultAccessPolicy) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(data); switch (permissionType) { case PermissionType.RunAsAdmin: SetupAdmin(sutProvider, data.OrganizationId); sutProvider.GetDependency().AccessToProjectAsync(Arg.Any(), Arg.Any(), AccessClientType.NoAccessCheck) .Returns((true, true)); break; case PermissionType.RunAsUserWithPermission: SetupUserWithPermission(sutProvider, data.OrganizationId); sutProvider.GetDependency().AccessToProjectAsync(Arg.Any(), Arg.Any(), AccessClientType.User) .Returns((true, true)); break; } sutProvider.GetDependency().GetManyByGrantedProjectIdAsync(default, default) .ReturnsForAnyArgs(new List { resultAccessPolicy }); var result = await sutProvider.Sut.GetProjectAccessPoliciesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByGrantedProjectIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Any()); Assert.Empty(result.GroupAccessPolicies); Assert.NotEmpty(result.UserAccessPolicies); Assert.Empty(result.ServiceAccountAccessPolicies); } [Theory] [BitAutoData] public async void GetProjectAccessPolicies_ProjectsExist_UserWithoutPermission_Throws( SutProvider sutProvider, Guid id, Project data, UserProjectAccessPolicy resultAccessPolicy) { SetupUserWithoutPermission(sutProvider, data.OrganizationId); sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(data); sutProvider.GetDependency().AccessToProjectAsync(default, default, default) .Returns((false, false)); sutProvider.GetDependency().GetManyByGrantedProjectIdAsync(default, default) .ReturnsForAnyArgs(new List { resultAccessPolicy }); await Assert.ThrowsAsync(() => sutProvider.Sut.GetProjectAccessPoliciesAsync(id)); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .GetManyByGrantedProjectIdAsync(Arg.Any(), Arg.Any()); } [Theory] [BitAutoData(PermissionType.RunAsAdmin)] [BitAutoData(PermissionType.RunAsUserWithPermission)] public async void GetServiceAccountAccessPolicies_ReturnsEmptyList( PermissionType permissionType, SutProvider sutProvider, Guid id, ServiceAccount data) { sutProvider.GetDependency().GetByIdAsync(data.Id).ReturnsForAnyArgs(data); switch (permissionType) { case PermissionType.RunAsAdmin: SetupAdmin(sutProvider, data.OrganizationId); break; case PermissionType.RunAsUserWithPermission: SetupUserWithPermission(sutProvider, data.OrganizationId); sutProvider.GetDependency() .UserHasWriteAccessToServiceAccount(default, default) .ReturnsForAnyArgs(true); break; } var result = await sutProvider.Sut.GetServiceAccountAccessPoliciesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByGrantedServiceAccountIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Any()); Assert.Empty(result.UserAccessPolicies); Assert.Empty(result.GroupAccessPolicies); } [Theory] [BitAutoData] public async void GetServiceAccountAccessPolicies_UserWithoutPermission_Throws( SutProvider sutProvider, Guid id, ServiceAccount data) { SetupUserWithoutPermission(sutProvider, data.OrganizationId); sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(data); sutProvider.GetDependency().UserHasWriteAccessToServiceAccount(default, default) .ReturnsForAnyArgs(false); await Assert.ThrowsAsync(() => sutProvider.Sut.GetServiceAccountAccessPoliciesAsync(id)); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .GetManyByGrantedServiceAccountIdAsync(Arg.Any(), Arg.Any()); } [Theory] [BitAutoData(PermissionType.RunAsAdmin)] [BitAutoData(PermissionType.RunAsUserWithPermission)] public async void GetServiceAccountAccessPolicies_Success( PermissionType permissionType, SutProvider sutProvider, Guid id, ServiceAccount data, UserServiceAccountAccessPolicy resultAccessPolicy) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(data); switch (permissionType) { case PermissionType.RunAsAdmin: SetupAdmin(sutProvider, data.OrganizationId); break; case PermissionType.RunAsUserWithPermission: SetupUserWithPermission(sutProvider, data.OrganizationId); sutProvider.GetDependency() .UserHasWriteAccessToServiceAccount(default, default) .ReturnsForAnyArgs(true); break; } sutProvider.GetDependency().GetManyByGrantedServiceAccountIdAsync(default, default) .ReturnsForAnyArgs(new List { resultAccessPolicy }); var result = await sutProvider.Sut.GetServiceAccountAccessPoliciesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByGrantedServiceAccountIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Any()); Assert.Empty(result.GroupAccessPolicies); Assert.NotEmpty(result.UserAccessPolicies); } [Theory] [BitAutoData] public async void GetServiceAccountAccessPolicies_ServiceAccountExists_UserWithoutPermission_Throws( SutProvider sutProvider, Guid id, ServiceAccount data, UserServiceAccountAccessPolicy resultAccessPolicy) { SetupUserWithoutPermission(sutProvider, data.OrganizationId); sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(data); sutProvider.GetDependency().UserHasWriteAccessToServiceAccount(default, default) .ReturnsForAnyArgs(false); sutProvider.GetDependency().GetManyByGrantedServiceAccountIdAsync(default, default) .ReturnsForAnyArgs(new List { resultAccessPolicy }); await Assert.ThrowsAsync(() => sutProvider.Sut.GetServiceAccountAccessPoliciesAsync(id)); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .GetManyByGrantedServiceAccountIdAsync(Arg.Any(), Arg.Any()); } [Theory] [BitAutoData(PermissionType.RunAsAdmin)] [BitAutoData(PermissionType.RunAsUserWithPermission)] public async void GetServiceAccountGrantedPolicies_ReturnsEmptyList( PermissionType permissionType, SutProvider sutProvider, Guid id, ServiceAccount data) { sutProvider.GetDependency().GetByIdAsync(data.Id).ReturnsForAnyArgs(data); switch (permissionType) { case PermissionType.RunAsAdmin: SetupAdmin(sutProvider, data.OrganizationId); break; case PermissionType.RunAsUserWithPermission: SetupUserWithPermission(sutProvider, data.OrganizationId); sutProvider.GetDependency() .UserHasWriteAccessToServiceAccount(default, default) .ReturnsForAnyArgs(true); break; } var result = await sutProvider.Sut.GetServiceAccountGrantedPoliciesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByServiceAccountIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Any(), Arg.Any()); Assert.Empty(result.Data); } [Theory] [BitAutoData(PermissionType.RunAsAdmin)] [BitAutoData(PermissionType.RunAsUserWithPermission)] public async void GetServiceAccountGrantedPolicies_Success( PermissionType permissionType, SutProvider sutProvider, Guid id, ServiceAccount data, ServiceAccountProjectAccessPolicy resultAccessPolicy) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(data); switch (permissionType) { case PermissionType.RunAsAdmin: SetupAdmin(sutProvider, data.OrganizationId); break; case PermissionType.RunAsUserWithPermission: SetupUserWithPermission(sutProvider, data.OrganizationId); break; } sutProvider.GetDependency().GetManyByServiceAccountIdAsync(default, default, default) .ReturnsForAnyArgs(new List { resultAccessPolicy }); var result = await sutProvider.Sut.GetServiceAccountGrantedPoliciesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByServiceAccountIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Any(), Arg.Any()); Assert.NotEmpty(result.Data); } [Theory] [BitAutoData] public async void CreateProjectAccessPolicies_RequestMoreThanMax_Throws( SutProvider sutProvider, Guid id, Project mockProject, UserProjectAccessPolicy data, AccessPoliciesCreateRequest request) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(mockProject); sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(true); sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); sutProvider.GetDependency().CreateManyAsync(default, default, default) .ReturnsForAnyArgs(new List { data }); request = AddRequestsOverMax(request); await Assert.ThrowsAsync(() => sutProvider.Sut.CreateProjectAccessPoliciesAsync(id, request)); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .CreateManyAsync(Arg.Any>(), Arg.Any(), Arg.Any()); } [Theory] [BitAutoData] public async void CreateProjectAccessPolicies_Success( SutProvider sutProvider, Guid id, Project mockProject, UserProjectAccessPolicy data, AccessPoliciesCreateRequest request) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(mockProject); sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(true); sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); sutProvider.GetDependency().CreateManyAsync(default, default, default) .ReturnsForAnyArgs(new List { data }); await sutProvider.Sut.CreateProjectAccessPoliciesAsync(id, request); await sutProvider.GetDependency().Received(1) .CreateManyAsync(Arg.Any>(), Arg.Any(), Arg.Any()); } [Theory] [BitAutoData] public async void CreateServiceAccountAccessPolicies_RequestMoreThanMax_Throws( SutProvider sutProvider, Guid id, ServiceAccount serviceAccount, UserServiceAccountAccessPolicy data, AccessPoliciesCreateRequest request) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(serviceAccount); sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(true); sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); sutProvider.GetDependency() .CreateManyAsync(default, default, default) .ReturnsForAnyArgs(new List { data }); request = AddRequestsOverMax(request); await Assert.ThrowsAsync(() => sutProvider.Sut.CreateServiceAccountAccessPoliciesAsync(id, request)); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .CreateManyAsync(Arg.Any>(), Arg.Any(), Arg.Any()); } [Theory] [BitAutoData] public async void CreateServiceAccountAccessPolicies_Success( SutProvider sutProvider, Guid id, ServiceAccount serviceAccount, UserServiceAccountAccessPolicy data, AccessPoliciesCreateRequest request) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(serviceAccount); sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(true); sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); sutProvider.GetDependency() .CreateManyAsync(default, default, default) .ReturnsForAnyArgs(new List { data }); await sutProvider.Sut.CreateServiceAccountAccessPoliciesAsync(id, request); await sutProvider.GetDependency().Received(1) .CreateManyAsync(Arg.Any>(), Arg.Any(), Arg.Any()); } [Theory] [BitAutoData] public async void CreateServiceAccountGrantedPolicies_RequestMoreThanMax_Throws( SutProvider sutProvider, Guid id, ServiceAccount serviceAccount, ServiceAccountProjectAccessPolicy data, List request) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(serviceAccount); sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(true); sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); sutProvider.GetDependency() .CreateManyAsync(default, default, default) .ReturnsForAnyArgs(new List { data }); request = AddRequestsOverMax(request); await Assert.ThrowsAsync(() => sutProvider.Sut.CreateServiceAccountGrantedPoliciesAsync(id, request)); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .CreateManyAsync(Arg.Any>(), Arg.Any(), Arg.Any()); } [Theory] [BitAutoData] public async void CreateServiceAccountGrantedPolicies_Success( SutProvider sutProvider, Guid id, ServiceAccount serviceAccount, ServiceAccountProjectAccessPolicy data, List request) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(serviceAccount); sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(true); sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); sutProvider.GetDependency() .CreateManyAsync(default, default, default) .ReturnsForAnyArgs(new List { data }); await sutProvider.Sut.CreateServiceAccountGrantedPoliciesAsync(id, request); await sutProvider.GetDependency().Received(1) .CreateManyAsync(Arg.Any>(), Arg.Any(), Arg.Any()); } [Theory] [BitAutoData] public async void UpdateAccessPolicies_Success( SutProvider sutProvider, Guid id, UserProjectAccessPolicy data, AccessPolicyUpdateRequest request) { sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); sutProvider.GetDependency().UpdateAsync(default, default, default, default) .ReturnsForAnyArgs(data); await sutProvider.Sut.UpdateAccessPolicyAsync(id, request); await sutProvider.GetDependency().Received(1) .UpdateAsync(Arg.Any(), Arg.Is(request.Read), Arg.Is(request.Write), Arg.Any()); } [Theory] [BitAutoData] public async void DeleteAccessPolicies_Success(SutProvider sutProvider, Guid id) { sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); sutProvider.GetDependency().DeleteAsync(default, default).ReturnsNull(); await sutProvider.Sut.DeleteAccessPolicyAsync(id); await sutProvider.GetDependency().Received(1) .DeleteAsync(Arg.Any(), Arg.Any()); } [Theory] [BitAutoData(PermissionType.RunAsAdmin)] [BitAutoData(PermissionType.RunAsUserWithPermission)] public async void GetPeoplePotentialGrantees_ReturnsEmptyList( PermissionType permissionType, SutProvider sutProvider, Guid id) { SetupPermission(sutProvider, permissionType, id); var result = await sutProvider.Sut.GetPeoplePotentialGranteesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByOrganizationIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id))); await sutProvider.GetDependency().Received(1) .GetManyDetailsByOrganizationAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id))); Assert.Empty(result.Data); } [Theory] [BitAutoData] public async void GetPeoplePotentialGrantees_UserWithoutPermission_Throws( SutProvider sutProvider, Guid id) { sutProvider.GetDependency().OrganizationAdmin(id).Returns(false); sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(false); sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); await Assert.ThrowsAsync(() => sutProvider.Sut.GetPeoplePotentialGranteesAsync(id)); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .GetManyByOrganizationIdAsync(Arg.Any()); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .GetManyDetailsByOrganizationAsync(Arg.Any()); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .GetManyByOrganizationIdWriteAccessAsync(Arg.Any(), Arg.Any(), Arg.Any()); } [Theory] [BitAutoData(PermissionType.RunAsAdmin)] [BitAutoData(PermissionType.RunAsUserWithPermission)] public async void GetPeoplePotentialGrantees_Success( PermissionType permissionType, SutProvider sutProvider, Guid id, Group mockGroup) { SetupPermission(sutProvider, permissionType, id); sutProvider.GetDependency().GetManyByOrganizationIdAsync(default) .ReturnsForAnyArgs(new List { mockGroup }); var result = await sutProvider.Sut.GetPeoplePotentialGranteesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByOrganizationIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id))); await sutProvider.GetDependency().Received(1) .GetManyDetailsByOrganizationAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id))); Assert.NotEmpty(result.Data); } [Theory] [BitAutoData(PermissionType.RunAsAdmin)] [BitAutoData(PermissionType.RunAsUserWithPermission)] public async void GetServiceAccountsPotentialGrantees_ReturnsEmptyList( PermissionType permissionType, SutProvider sutProvider, Guid id) { SetupPermission(sutProvider, permissionType, id); var result = await sutProvider.Sut.GetServiceAccountsPotentialGranteesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByOrganizationIdWriteAccessAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Any()); Assert.Empty(result.Data); } [Theory] [BitAutoData] public async void GetServiceAccountsPotentialGranteesAsync_UserWithoutPermission_Throws( SutProvider sutProvider, Guid id) { sutProvider.GetDependency().OrganizationAdmin(id).Returns(false); sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(false); sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); await Assert.ThrowsAsync(() => sutProvider.Sut.GetServiceAccountsPotentialGranteesAsync(id)); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .GetManyByOrganizationIdWriteAccessAsync(Arg.Any(), Arg.Any(), Arg.Any()); } [Theory] [BitAutoData(PermissionType.RunAsAdmin)] [BitAutoData(PermissionType.RunAsUserWithPermission)] public async void GetServiceAccountsPotentialGranteesAsync_Success( PermissionType permissionType, SutProvider sutProvider, Guid id, ServiceAccount mockServiceAccount) { SetupPermission(sutProvider, permissionType, id); sutProvider.GetDependency() .GetManyByOrganizationIdWriteAccessAsync(default, default, default) .ReturnsForAnyArgs(new List { mockServiceAccount }); var result = await sutProvider.Sut.GetServiceAccountsPotentialGranteesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByOrganizationIdWriteAccessAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Any()); Assert.NotEmpty(result.Data); } [Theory] [BitAutoData(PermissionType.RunAsAdmin)] [BitAutoData(PermissionType.RunAsUserWithPermission)] public async void GetProjectPotentialGrantees_ReturnsEmptyList( PermissionType permissionType, SutProvider sutProvider, Guid id) { SetupPermission(sutProvider, permissionType, id); var result = await sutProvider.Sut.GetProjectPotentialGranteesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByOrganizationIdWriteAccessAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Any()); Assert.Empty(result.Data); } [Theory] [BitAutoData] public async void GetProjectPotentialGrantees_UserWithoutPermission_Throws( SutProvider sutProvider, Guid id) { sutProvider.GetDependency().OrganizationAdmin(id).Returns(false); sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(false); sutProvider.GetDependency().GetProperUserId(default).ReturnsForAnyArgs(Guid.NewGuid()); await Assert.ThrowsAsync(() => sutProvider.Sut.GetProjectPotentialGranteesAsync(id)); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .GetManyByOrganizationIdWriteAccessAsync(Arg.Any(), Arg.Any(), Arg.Any()); } [Theory] [BitAutoData(PermissionType.RunAsAdmin)] [BitAutoData(PermissionType.RunAsUserWithPermission)] public async void GetProjectPotentialGrantees_Success( PermissionType permissionType, SutProvider sutProvider, Guid id, Project mockProject) { SetupPermission(sutProvider, permissionType, id); sutProvider.GetDependency() .GetManyByOrganizationIdWriteAccessAsync(default, default, default) .ReturnsForAnyArgs(new List { mockProject }); var result = await sutProvider.Sut.GetProjectPotentialGranteesAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByOrganizationIdWriteAccessAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Is(AssertHelper.AssertPropertyEqual(id)), Arg.Any()); Assert.NotEmpty(result.Data); } }