|
| 1 | +using Bit.Core.AdminConsole.Entities; |
| 2 | +using Bit.Core.Billing.Enums; |
| 3 | +using Bit.Core.Entities; |
| 4 | +using Bit.Core.Enums; |
| 5 | +using Bit.Core.Repositories; |
| 6 | +using Bit.Core.Vault.Entities; |
| 7 | +using Bit.Core.Vault.Enums; |
| 8 | +using Bit.Core.Vault.Repositories; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Bit.Infrastructure.IntegrationTest.Vault.Repositories; |
| 12 | + |
| 13 | +public class CollectionCipherRepositoryTests |
| 14 | +{ |
| 15 | + [Theory, DatabaseData] |
| 16 | + public async Task GetManySharedByOrganizationIdAsync_OnlyReturnsSharedCollections( |
| 17 | + IOrganizationRepository organizationRepository, |
| 18 | + ICollectionRepository collectionRepository, |
| 19 | + ICipherRepository cipherRepository, |
| 20 | + ICollectionCipherRepository collectionCipherRepository) |
| 21 | + { |
| 22 | + // Arrange |
| 23 | + var organization = await organizationRepository.CreateAsync(new Organization |
| 24 | + { |
| 25 | + Name = "Test Org", |
| 26 | + PlanType = PlanType.EnterpriseAnnually, |
| 27 | + Plan = "Enterprise", |
| 28 | + BillingEmail = "billing@example.com" |
| 29 | + }); |
| 30 | + |
| 31 | + var sharedCollection = await collectionRepository.CreateAsync(new Collection |
| 32 | + { |
| 33 | + Name = "Shared Collection", |
| 34 | + OrganizationId = organization.Id, |
| 35 | + Type = CollectionType.SharedCollection |
| 36 | + }); |
| 37 | + |
| 38 | + var defaultUserCollection = await collectionRepository.CreateAsync(new Collection |
| 39 | + { |
| 40 | + Name = "Default User Collection", |
| 41 | + OrganizationId = organization.Id, |
| 42 | + Type = CollectionType.DefaultUserCollection |
| 43 | + }); |
| 44 | + |
| 45 | + var sharedCipher = await cipherRepository.CreateAsync(new Cipher |
| 46 | + { |
| 47 | + Type = CipherType.Login, |
| 48 | + OrganizationId = organization.Id, |
| 49 | + Data = "" |
| 50 | + }); |
| 51 | + |
| 52 | + var defaultCipher = await cipherRepository.CreateAsync(new Cipher |
| 53 | + { |
| 54 | + Type = CipherType.Login, |
| 55 | + OrganizationId = organization.Id, |
| 56 | + Data = "" |
| 57 | + }); |
| 58 | + |
| 59 | + await collectionCipherRepository.AddCollectionsForManyCiphersAsync( |
| 60 | + organization.Id, |
| 61 | + new[] { sharedCipher.Id }, |
| 62 | + new[] { sharedCollection.Id }); |
| 63 | + |
| 64 | + await collectionCipherRepository.AddCollectionsForManyCiphersAsync( |
| 65 | + organization.Id, |
| 66 | + new[] { defaultCipher.Id }, |
| 67 | + new[] { defaultUserCollection.Id }); |
| 68 | + |
| 69 | + // Act |
| 70 | + var result = await collectionCipherRepository.GetManySharedByOrganizationIdAsync(organization.Id); |
| 71 | + |
| 72 | + // Assert |
| 73 | + Assert.Single(result); |
| 74 | + Assert.Equal(sharedCollection.Id, result.First().CollectionId); |
| 75 | + Assert.DoesNotContain(result, cc => cc.CollectionId == defaultUserCollection.Id); |
| 76 | + |
| 77 | + // Cleanup |
| 78 | + await cipherRepository.DeleteAsync(sharedCipher); |
| 79 | + await cipherRepository.DeleteAsync(defaultCipher); |
| 80 | + await collectionRepository.DeleteAsync(sharedCollection); |
| 81 | + await collectionRepository.DeleteAsync(defaultUserCollection); |
| 82 | + await organizationRepository.DeleteAsync(organization); |
| 83 | + } |
| 84 | +} |
0 commit comments