using System.Security.Claims; using Bit.Api.Controllers; using Bit.Api.Models.Request; using Bit.Api.Vault.AuthorizationHandlers.Collections; using Bit.Core.Context; using Bit.Core.Entities; using Bit.Core.Exceptions; using Bit.Core.Models.Data; using Bit.Core.Models.Data.Organizations; using Bit.Core.OrganizationFeatures.OrganizationCollections.Interfaces; using Bit.Core.Repositories; using Bit.Core.Services; using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; using Microsoft.AspNetCore.Authorization; using NSubstitute; using Xunit; namespace Bit.Api.Test.Controllers; [ControllerCustomize(typeof(CollectionsController))] [SutProviderCustomize] public class CollectionsControllerTests { [Theory, BitAutoData] public async Task Post_Success(OrganizationAbility organizationAbility, CollectionRequestModel collectionRequest, SutProvider sutProvider) { ArrangeOrganizationAbility(sutProvider, organizationAbility); Collection ExpectedCollection() => Arg.Is(c => c.Name == collectionRequest.Name && c.ExternalId == collectionRequest.ExternalId && c.OrganizationId == organizationAbility.Id); sutProvider.GetDependency() .AuthorizeAsync(Arg.Any(), ExpectedCollection(), Arg.Is>(r => r.Contains(BulkCollectionOperations.Create))) .Returns(AuthorizationResult.Success()); _ = await sutProvider.Sut.Post(organizationAbility.Id, collectionRequest); await sutProvider.GetDependency() .Received(1) .SaveAsync(Arg.Any(), Arg.Any>(), Arg.Any>()); } [Theory, BitAutoData] public async Task Put_Success(Collection collection, CollectionRequestModel collectionRequest, SutProvider sutProvider, OrganizationAbility organizationAbility) { ArrangeOrganizationAbility(sutProvider, organizationAbility); collection.OrganizationId = organizationAbility.Id; Collection ExpectedCollection() => Arg.Is(c => c.Id == collection.Id && c.Name == collectionRequest.Name && c.ExternalId == collectionRequest.ExternalId && c.OrganizationId == collection.OrganizationId); sutProvider.GetDependency() .GetByIdAsync(collection.Id) .Returns(collection); sutProvider.GetDependency() .AuthorizeAsync(Arg.Any(), collection, Arg.Is>(r => r.Contains(BulkCollectionOperations.Update))) .Returns(AuthorizationResult.Success()); _ = await sutProvider.Sut.Put(collection.OrganizationId, collection.Id, collectionRequest); await sutProvider.GetDependency() .Received(1) .SaveAsync(ExpectedCollection(), Arg.Any>(), Arg.Any>()); } [Theory, BitAutoData] public async Task Put_WithNoCollectionPermission_ThrowsNotFound(Collection collection, CollectionRequestModel collectionRequest, SutProvider sutProvider, OrganizationAbility organizationAbility) { ArrangeOrganizationAbility(sutProvider, organizationAbility); collection.OrganizationId = organizationAbility.Id; sutProvider.GetDependency() .AuthorizeAsync(Arg.Any(), collection, Arg.Is>(r => r.Contains(BulkCollectionOperations.Update))) .Returns(AuthorizationResult.Failed()); sutProvider.GetDependency() .GetByIdAsync(collection.Id) .Returns(collection); _ = await Assert.ThrowsAsync(async () => await sutProvider.Sut.Put(collection.OrganizationId, collection.Id, collectionRequest)); } [Theory, BitAutoData] public async Task GetOrganizationCollectionsWithGroups_WithReadAllPermissions_GetsAllCollections(OrganizationAbility organizationAbility, Guid userId, SutProvider sutProvider) { ArrangeOrganizationAbility(sutProvider, organizationAbility); sutProvider.GetDependency().UserId.Returns(userId); sutProvider.GetDependency() .AuthorizeAsync( Arg.Any(), Arg.Any(), Arg.Is>(requirements => requirements.Cast().All(operation => operation.Name == nameof(CollectionOperations.ReadAllWithAccess) && operation.OrganizationId == organizationAbility.Id))) .Returns(AuthorizationResult.Success()); await sutProvider.Sut.GetManyWithDetails(organizationAbility.Id); await sutProvider.GetDependency().Received(1).GetManyByUserIdWithAccessAsync(userId, organizationAbility.Id, Arg.Any()); await sutProvider.GetDependency().Received(1).GetManyByOrganizationIdWithAccessAsync(organizationAbility.Id); } [Theory, BitAutoData] public async Task GetOrganizationCollectionsWithGroups_MissingReadAllPermissions_GetsAssignedCollections( OrganizationAbility organizationAbility, Guid userId, SutProvider sutProvider) { ArrangeOrganizationAbility(sutProvider, organizationAbility); sutProvider.GetDependency().UserId.Returns(userId); sutProvider.GetDependency() .AuthorizeAsync( Arg.Any(), Arg.Any(), Arg.Is>(requirements => requirements.Cast().All(operation => operation.Name == nameof(CollectionOperations.ReadAllWithAccess) && operation.OrganizationId == organizationAbility.Id))) .Returns(AuthorizationResult.Failed()); sutProvider.GetDependency() .AuthorizeAsync( Arg.Any(), Arg.Any(), Arg.Is>(requirements => requirements.Cast().All(operation => operation.Name == nameof(BulkCollectionOperations.ReadWithAccess)))) .Returns(AuthorizationResult.Success()); await sutProvider.Sut.GetManyWithDetails(organizationAbility.Id); await sutProvider.GetDependency().Received(1).GetManyByUserIdWithAccessAsync(userId, organizationAbility.Id, Arg.Any()); await sutProvider.GetDependency().DidNotReceive().GetManyByOrganizationIdWithAccessAsync(organizationAbility.Id); } [Theory, BitAutoData] public async Task GetOrganizationCollections_WithReadAllPermissions_GetsAllCollections( OrganizationAbility organizationAbility, List collections, Guid userId, SutProvider sutProvider) { ArrangeOrganizationAbility(sutProvider, organizationAbility); collections.ForEach(c => c.OrganizationId = organizationAbility.Id); sutProvider.GetDependency().UserId.Returns(userId); sutProvider.GetDependency() .AuthorizeAsync( Arg.Any(), Arg.Any(), Arg.Is>(requirements => requirements.Cast().All(operation => operation.Name == nameof(CollectionOperations.ReadAll) && operation.OrganizationId == organizationAbility.Id))) .Returns(AuthorizationResult.Success()); sutProvider.GetDependency() .GetManyByOrganizationIdAsync(organizationAbility.Id) .Returns(collections); var response = await sutProvider.Sut.Get(organizationAbility.Id); await sutProvider.GetDependency().Received(1).GetManyByOrganizationIdAsync(organizationAbility.Id); Assert.Equal(collections.Count, response.Data.Count()); } [Theory, BitAutoData] public async Task GetOrganizationCollections_MissingReadAllPermissions_GetsManageableCollections( OrganizationAbility organizationAbility, List collections, Guid userId, SutProvider sutProvider) { ArrangeOrganizationAbility(sutProvider, organizationAbility); collections.ForEach(c => c.OrganizationId = organizationAbility.Id); collections.ForEach(c => c.Manage = false); var managedCollection = collections.First(); managedCollection.Manage = true; sutProvider.GetDependency().UserId.Returns(userId); sutProvider.GetDependency() .AuthorizeAsync( Arg.Any(), Arg.Any(), Arg.Is>(requirements => requirements.Cast().All(operation => operation.Name == nameof(CollectionOperations.ReadAll) && operation.OrganizationId == organizationAbility.Id))) .Returns(AuthorizationResult.Failed()); sutProvider.GetDependency() .AuthorizeAsync( Arg.Any(), Arg.Any(), Arg.Is>(requirements => requirements.Cast().All(operation => operation.Name == nameof(BulkCollectionOperations.Read)))) .Returns(AuthorizationResult.Success()); sutProvider.GetDependency() .GetManyByUserIdAsync(userId, false) .Returns(collections); var result = await sutProvider.Sut.Get(organizationAbility.Id); await sutProvider.GetDependency().DidNotReceive().GetManyByOrganizationIdAsync(organizationAbility.Id); await sutProvider.GetDependency().Received(1).GetManyByUserIdAsync(userId, false); Assert.Single(result.Data); Assert.All(result.Data, c => Assert.Equal(organizationAbility.Id, c.OrganizationId)); Assert.All(result.Data, c => Assert.Equal(managedCollection.Id, c.Id)); } [Theory, BitAutoData] public async Task DeleteMany_Success(OrganizationAbility organizationAbility, Collection collection1, Collection collection2, SutProvider sutProvider) { // Arrange var orgId = organizationAbility.Id; ArrangeOrganizationAbility(sutProvider, organizationAbility); var model = new CollectionBulkDeleteRequestModel { Ids = new[] { collection1.Id, collection2.Id } }; var collections = new List { new CollectionDetails { Id = collection1.Id, OrganizationId = orgId, }, new CollectionDetails { Id = collection2.Id, OrganizationId = orgId, }, }; sutProvider.GetDependency().GetManyByManyIdsAsync(Arg.Any>()) .Returns(collections); sutProvider.GetDependency() .AuthorizeAsync(Arg.Any(), collections, Arg.Is>(r => r.Contains(BulkCollectionOperations.Delete))) .Returns(AuthorizationResult.Success()); // Act await sutProvider.Sut.DeleteMany(orgId, model); // Assert await sutProvider.GetDependency() .Received(1) .DeleteManyAsync(Arg.Is>(coll => coll.Select(c => c.Id).SequenceEqual(collections.Select(c => c.Id)))); } [Theory, BitAutoData] public async Task DeleteMany_PermissionDenied_ThrowsNotFound(OrganizationAbility organizationAbility, Collection collection1, Collection collection2, SutProvider sutProvider) { // Arrange var orgId = organizationAbility.Id; ArrangeOrganizationAbility(sutProvider, organizationAbility); var model = new CollectionBulkDeleteRequestModel { Ids = new[] { collection1.Id, collection2.Id } }; var collections = new List { new CollectionDetails { Id = collection1.Id, OrganizationId = orgId, }, new CollectionDetails { Id = collection2.Id, OrganizationId = orgId, }, }; sutProvider.GetDependency().GetManyByManyIdsAsync(Arg.Any>()) .Returns(collections); sutProvider.GetDependency() .AuthorizeAsync(Arg.Any(), collections, Arg.Is>(r => r.Contains(BulkCollectionOperations.Delete))) .Returns(AuthorizationResult.Failed()); // Assert await Assert.ThrowsAsync(() => sutProvider.Sut.DeleteMany(orgId, model)); await sutProvider.GetDependency() .DidNotReceiveWithAnyArgs() .DeleteManyAsync((IEnumerable)default); } [Theory, BitAutoData] public async Task PostBulkCollectionAccess_Success(User actingUser, List collections, OrganizationAbility organizationAbility, SutProvider sutProvider) { // Arrange ArrangeOrganizationAbility(sutProvider, organizationAbility); collections.ForEach(c => c.OrganizationId = organizationAbility.Id); var userId = Guid.NewGuid(); var groupId = Guid.NewGuid(); var model = new BulkCollectionAccessRequestModel { CollectionIds = collections.Select(c => c.Id), Users = new[] { new SelectionReadOnlyRequestModel { Id = userId, Manage = true } }, Groups = new[] { new SelectionReadOnlyRequestModel { Id = groupId, ReadOnly = true } }, }; sutProvider.GetDependency() .GetManyByManyIdsAsync(model.CollectionIds) .Returns(collections); sutProvider.GetDependency() .UserId.Returns(actingUser.Id); sutProvider.GetDependency().AuthorizeAsync( Arg.Any(), ExpectedCollectionAccess(), Arg.Is>( r => r.Contains(BulkCollectionOperations.ModifyAccess) )) .Returns(AuthorizationResult.Success()); IEnumerable ExpectedCollectionAccess() => Arg.Is>(cols => cols.SequenceEqual(collections)); // Act await sutProvider.Sut.PostBulkCollectionAccess(organizationAbility.Id, model); // Assert await sutProvider.GetDependency().Received().AuthorizeAsync( Arg.Any(), ExpectedCollectionAccess(), Arg.Is>( r => r.Contains(BulkCollectionOperations.ModifyAccess)) ); await sutProvider.GetDependency().Received() .AddAccessAsync( Arg.Is>(g => g.SequenceEqual(collections)), Arg.Is>(u => u.All(c => c.Id == userId && c.Manage)), Arg.Is>(g => g.All(c => c.Id == groupId && c.ReadOnly))); } [Theory, BitAutoData] public async Task PostBulkCollectionAccess_CollectionsNotFound_Throws(User actingUser, OrganizationAbility organizationAbility, List collections, SutProvider sutProvider) { ArrangeOrganizationAbility(sutProvider, organizationAbility); collections.ForEach(c => c.OrganizationId = organizationAbility.Id); var userId = Guid.NewGuid(); var groupId = Guid.NewGuid(); var model = new BulkCollectionAccessRequestModel { CollectionIds = collections.Select(c => c.Id), Users = new[] { new SelectionReadOnlyRequestModel { Id = userId, Manage = true } }, Groups = new[] { new SelectionReadOnlyRequestModel { Id = groupId, ReadOnly = true } }, }; sutProvider.GetDependency() .UserId.Returns(actingUser.Id); sutProvider.GetDependency() .GetManyByManyIdsAsync(model.CollectionIds) .Returns(collections.Skip(1).ToList()); var exception = await Assert.ThrowsAsync( () => sutProvider.Sut.PostBulkCollectionAccess(organizationAbility.Id, model)); Assert.Equal("One or more collections not found.", exception.Message); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().AuthorizeAsync( Arg.Any(), Arg.Any>(), Arg.Any>() ); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .AddAccessAsync(default, default, default); } [Theory, BitAutoData] public async Task PostBulkCollectionAccess_CollectionsBelongToDifferentOrganizations_Throws(User actingUser, OrganizationAbility organizationAbility, List collections, SutProvider sutProvider) { ArrangeOrganizationAbility(sutProvider, organizationAbility); // First collection has a different orgId collections.Skip(1).ToList().ForEach(c => c.OrganizationId = organizationAbility.Id); var userId = Guid.NewGuid(); var groupId = Guid.NewGuid(); var model = new BulkCollectionAccessRequestModel { CollectionIds = collections.Select(c => c.Id), Users = new[] { new SelectionReadOnlyRequestModel { Id = userId, Manage = true } }, Groups = new[] { new SelectionReadOnlyRequestModel { Id = groupId, ReadOnly = true } }, }; sutProvider.GetDependency() .UserId.Returns(actingUser.Id); sutProvider.GetDependency() .GetManyByManyIdsAsync(model.CollectionIds) .Returns(collections); var exception = await Assert.ThrowsAsync( () => sutProvider.Sut.PostBulkCollectionAccess(organizationAbility.Id, model)); Assert.Equal("One or more collections not found.", exception.Message); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().AuthorizeAsync( Arg.Any(), Arg.Any>(), Arg.Any>() ); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .AddAccessAsync(default, default, default); } [Theory, BitAutoData] public async Task PostBulkCollectionAccess_FlexibleCollectionsDisabled_Throws(OrganizationAbility organizationAbility, List collections, SutProvider sutProvider) { organizationAbility.FlexibleCollections = false; sutProvider.GetDependency().GetOrganizationAbilityAsync(organizationAbility.Id) .Returns(organizationAbility); var userId = Guid.NewGuid(); var groupId = Guid.NewGuid(); var model = new BulkCollectionAccessRequestModel { CollectionIds = collections.Select(c => c.Id), Users = new[] { new SelectionReadOnlyRequestModel { Id = userId, Manage = true } }, Groups = new[] { new SelectionReadOnlyRequestModel { Id = groupId, ReadOnly = true } }, }; var exception = await Assert.ThrowsAsync( () => sutProvider.Sut.PostBulkCollectionAccess(organizationAbility.Id, model)); Assert.Equal("Feature disabled.", exception.Message); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().AuthorizeAsync( Arg.Any(), Arg.Any>(), Arg.Any>() ); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .AddAccessAsync(default, default, default); } [Theory, BitAutoData] public async Task PostBulkCollectionAccess_AccessDenied_Throws(User actingUser, List collections, OrganizationAbility organizationAbility, SutProvider sutProvider) { ArrangeOrganizationAbility(sutProvider, organizationAbility); collections.ForEach(c => c.OrganizationId = organizationAbility.Id); var userId = Guid.NewGuid(); var groupId = Guid.NewGuid(); var model = new BulkCollectionAccessRequestModel { CollectionIds = collections.Select(c => c.Id), Users = new[] { new SelectionReadOnlyRequestModel { Id = userId, Manage = true } }, Groups = new[] { new SelectionReadOnlyRequestModel { Id = groupId, ReadOnly = true } }, }; sutProvider.GetDependency() .UserId.Returns(actingUser.Id); sutProvider.GetDependency() .GetManyByManyIdsAsync(model.CollectionIds) .Returns(collections); sutProvider.GetDependency().AuthorizeAsync( Arg.Any(), ExpectedCollectionAccess(), Arg.Is>( r => r.Contains(BulkCollectionOperations.ModifyAccess) )) .Returns(AuthorizationResult.Failed()); IEnumerable ExpectedCollectionAccess() => Arg.Is>(cols => cols.SequenceEqual(collections)); await Assert.ThrowsAsync(() => sutProvider.Sut.PostBulkCollectionAccess(organizationAbility.Id, model)); await sutProvider.GetDependency().Received().AuthorizeAsync( Arg.Any(), ExpectedCollectionAccess(), Arg.Is>( r => r.Contains(BulkCollectionOperations.ModifyAccess)) ); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .AddAccessAsync(default, default, default); } private void ArrangeOrganizationAbility(SutProvider sutProvider, OrganizationAbility organizationAbility) { organizationAbility.FlexibleCollections = true; sutProvider.GetDependency().GetOrganizationAbilityAsync(organizationAbility.Id) .Returns(organizationAbility); } }