|
| 1 | +using Bit.Core.AdminConsole.Entities; |
| 2 | +using Bit.Core.AdminConsole.Enums; |
| 3 | +using Bit.Core.AdminConsole.Repositories; |
| 4 | +using Bit.Core.Entities; |
| 5 | +using Bit.Core.Enums; |
| 6 | +using Bit.Core.Repositories; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Bit.Infrastructure.IntegrationTest.AdminConsole.Repositories.PolicyRepository; |
| 10 | + |
| 11 | +public class GetManyConfirmedAndAcceptedByUserAsyncTests |
| 12 | +{ |
| 13 | + [Theory, DatabaseData] |
| 14 | + public async Task ReturnsPolicies_WhenUserIsConfirmed( |
| 15 | + IUserRepository userRepository, |
| 16 | + IOrganizationRepository organizationRepository, |
| 17 | + IOrganizationUserRepository organizationUserRepository, |
| 18 | + IPolicyRepository policyRepository) |
| 19 | + { |
| 20 | + // Arrange |
| 21 | + var user = await userRepository.CreateTestUserAsync(); |
| 22 | + var organization = await organizationRepository.CreateTestOrganizationAsync(); |
| 23 | + await organizationUserRepository.CreateConfirmedTestOrganizationUserAsync(organization, user); |
| 24 | + var policy = await policyRepository.CreateAsync(new Policy |
| 25 | + { |
| 26 | + OrganizationId = organization.Id, |
| 27 | + Type = PolicyType.TwoFactorAuthentication, |
| 28 | + Enabled = true |
| 29 | + }); |
| 30 | + |
| 31 | + // Act |
| 32 | + var results = await policyRepository.GetManyConfirmedAndAcceptedByUserAsync(user.Id); |
| 33 | + |
| 34 | + // Assert |
| 35 | + Assert.Contains(results, p => p.Id == policy.Id); |
| 36 | + |
| 37 | + // Annul |
| 38 | + await organizationRepository.DeleteAsync(organization); |
| 39 | + await userRepository.DeleteAsync(user); |
| 40 | + } |
| 41 | + |
| 42 | + [Theory, DatabaseData] |
| 43 | + public async Task ReturnsPolicies_WhenUserIsAccepted( |
| 44 | + IUserRepository userRepository, |
| 45 | + IOrganizationRepository organizationRepository, |
| 46 | + IOrganizationUserRepository organizationUserRepository, |
| 47 | + IPolicyRepository policyRepository) |
| 48 | + { |
| 49 | + // Arrange |
| 50 | + var user = await userRepository.CreateTestUserAsync(); |
| 51 | + var organization = await organizationRepository.CreateTestOrganizationAsync(); |
| 52 | + await organizationUserRepository.CreateAcceptedTestOrganizationUserAsync(organization, user); |
| 53 | + var policy = await policyRepository.CreateAsync(new Policy |
| 54 | + { |
| 55 | + OrganizationId = organization.Id, |
| 56 | + Type = PolicyType.TwoFactorAuthentication, |
| 57 | + Enabled = true |
| 58 | + }); |
| 59 | + |
| 60 | + // Act |
| 61 | + var results = await policyRepository.GetManyConfirmedAndAcceptedByUserAsync(user.Id); |
| 62 | + |
| 63 | + // Assert |
| 64 | + Assert.Contains(results, p => p.Id == policy.Id); |
| 65 | + |
| 66 | + // Annul |
| 67 | + await organizationRepository.DeleteAsync(organization); |
| 68 | + await userRepository.DeleteAsync(user); |
| 69 | + } |
| 70 | + |
| 71 | + [Theory, DatabaseData] |
| 72 | + public async Task ReturnsPoliciesAcrossMultipleOrganizations_WhenUserIsConfirmedOrAccepted( |
| 73 | + IUserRepository userRepository, |
| 74 | + IOrganizationRepository organizationRepository, |
| 75 | + IOrganizationUserRepository organizationUserRepository, |
| 76 | + IPolicyRepository policyRepository) |
| 77 | + { |
| 78 | + // Arrange |
| 79 | + var user = await userRepository.CreateTestUserAsync(); |
| 80 | + |
| 81 | + var confirmedOrg = await organizationRepository.CreateTestOrganizationAsync(); |
| 82 | + await organizationUserRepository.CreateConfirmedTestOrganizationUserAsync(confirmedOrg, user); |
| 83 | + var confirmedPolicy = await policyRepository.CreateAsync(new Policy |
| 84 | + { |
| 85 | + OrganizationId = confirmedOrg.Id, |
| 86 | + Type = PolicyType.TwoFactorAuthentication, |
| 87 | + Enabled = true |
| 88 | + }); |
| 89 | + |
| 90 | + var acceptedOrg = await organizationRepository.CreateTestOrganizationAsync(); |
| 91 | + await organizationUserRepository.CreateAcceptedTestOrganizationUserAsync(acceptedOrg, user); |
| 92 | + var acceptedPolicy = await policyRepository.CreateAsync(new Policy |
| 93 | + { |
| 94 | + OrganizationId = acceptedOrg.Id, |
| 95 | + Type = PolicyType.TwoFactorAuthentication, |
| 96 | + Enabled = true |
| 97 | + }); |
| 98 | + |
| 99 | + // Act |
| 100 | + var results = await policyRepository.GetManyConfirmedAndAcceptedByUserAsync(user.Id); |
| 101 | + |
| 102 | + // Assert |
| 103 | + Assert.Contains(results, p => p.Id == confirmedPolicy.Id); |
| 104 | + Assert.Contains(results, p => p.Id == acceptedPolicy.Id); |
| 105 | + |
| 106 | + // Annul |
| 107 | + await organizationRepository.DeleteAsync(confirmedOrg); |
| 108 | + await organizationRepository.DeleteAsync(acceptedOrg); |
| 109 | + await userRepository.DeleteAsync(user); |
| 110 | + } |
| 111 | + |
| 112 | + [Theory, DatabaseData] |
| 113 | + public async Task DoesNotReturnPolicies_WhenUserIsInvited( |
| 114 | + IUserRepository userRepository, |
| 115 | + IOrganizationRepository organizationRepository, |
| 116 | + IOrganizationUserRepository organizationUserRepository, |
| 117 | + IPolicyRepository policyRepository) |
| 118 | + { |
| 119 | + // Arrange |
| 120 | + var user = await userRepository.CreateTestUserAsync(); |
| 121 | + var organization = await organizationRepository.CreateTestOrganizationAsync(); |
| 122 | + await organizationUserRepository.CreateAsync(new OrganizationUser |
| 123 | + { |
| 124 | + OrganizationId = organization.Id, |
| 125 | + UserId = null, |
| 126 | + Email = user.Email, |
| 127 | + Status = OrganizationUserStatusType.Invited, |
| 128 | + Type = OrganizationUserType.User |
| 129 | + }); |
| 130 | + var policy = await policyRepository.CreateAsync(new Policy |
| 131 | + { |
| 132 | + OrganizationId = organization.Id, |
| 133 | + Type = PolicyType.TwoFactorAuthentication, |
| 134 | + Enabled = true |
| 135 | + }); |
| 136 | + |
| 137 | + // Act |
| 138 | + var results = await policyRepository.GetManyConfirmedAndAcceptedByUserAsync(user.Id); |
| 139 | + |
| 140 | + // Assert |
| 141 | + Assert.DoesNotContain(results, p => p.Id == policy.Id); |
| 142 | + |
| 143 | + // Annul |
| 144 | + await organizationRepository.DeleteAsync(organization); |
| 145 | + await userRepository.DeleteAsync(user); |
| 146 | + } |
| 147 | + |
| 148 | + [Theory, DatabaseData] |
| 149 | + public async Task DoesNotReturnPolicies_WhenUserIsRevoked( |
| 150 | + IUserRepository userRepository, |
| 151 | + IOrganizationRepository organizationRepository, |
| 152 | + IOrganizationUserRepository organizationUserRepository, |
| 153 | + IPolicyRepository policyRepository) |
| 154 | + { |
| 155 | + // Arrange |
| 156 | + var user = await userRepository.CreateTestUserAsync(); |
| 157 | + var organization = await organizationRepository.CreateTestOrganizationAsync(); |
| 158 | + await organizationUserRepository.CreateRevokedTestOrganizationUserAsync(organization, user); |
| 159 | + var policy = await policyRepository.CreateAsync(new Policy |
| 160 | + { |
| 161 | + OrganizationId = organization.Id, |
| 162 | + Type = PolicyType.TwoFactorAuthentication, |
| 163 | + Enabled = true |
| 164 | + }); |
| 165 | + |
| 166 | + // Act |
| 167 | + var results = await policyRepository.GetManyConfirmedAndAcceptedByUserAsync(user.Id); |
| 168 | + |
| 169 | + // Assert |
| 170 | + Assert.DoesNotContain(results, p => p.Id == policy.Id); |
| 171 | + |
| 172 | + // Annul |
| 173 | + await organizationRepository.DeleteAsync(organization); |
| 174 | + await userRepository.DeleteAsync(user); |
| 175 | + } |
| 176 | + |
| 177 | + [Theory, DatabaseData] |
| 178 | + public async Task DoesNotReturnPolicies_ForOtherUsers( |
| 179 | + IUserRepository userRepository, |
| 180 | + IOrganizationRepository organizationRepository, |
| 181 | + IOrganizationUserRepository organizationUserRepository, |
| 182 | + IPolicyRepository policyRepository) |
| 183 | + { |
| 184 | + // Arrange |
| 185 | + var targetUser = await userRepository.CreateTestUserAsync(); |
| 186 | + var otherUser = await userRepository.CreateTestUserAsync(); |
| 187 | + |
| 188 | + var organization = await organizationRepository.CreateTestOrganizationAsync(); |
| 189 | + await organizationUserRepository.CreateConfirmedTestOrganizationUserAsync(organization, otherUser); |
| 190 | + var policy = await policyRepository.CreateAsync(new Policy |
| 191 | + { |
| 192 | + OrganizationId = organization.Id, |
| 193 | + Type = PolicyType.TwoFactorAuthentication, |
| 194 | + Enabled = true |
| 195 | + }); |
| 196 | + |
| 197 | + // Act |
| 198 | + var results = await policyRepository.GetManyConfirmedAndAcceptedByUserAsync(targetUser.Id); |
| 199 | + |
| 200 | + // Assert |
| 201 | + Assert.DoesNotContain(results, p => p.Id == policy.Id); |
| 202 | + |
| 203 | + // Annul |
| 204 | + await organizationRepository.DeleteAsync(organization); |
| 205 | + await userRepository.DeleteManyAsync([targetUser, otherUser]); |
| 206 | + } |
| 207 | +} |
0 commit comments