|
| 1 | +using Bit.Core.AdminConsole.Entities; |
| 2 | +using Bit.Core.AdminConsole.Enums; |
| 3 | +using Bit.Core.AdminConsole.Models.Data.Organizations.Policies; |
| 4 | +using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.AutoConfirmUser; |
| 5 | +using Bit.Core.AdminConsole.OrganizationFeatures.Policies; |
| 6 | +using Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyRequirements; |
| 7 | +using Bit.Core.Billing.Enums; |
| 8 | +using Bit.Core.Entities; |
| 9 | +using Bit.Core.Enums; |
| 10 | +using Bit.Core.Repositories; |
| 11 | +using Bit.Core.Test.AdminConsole.AutoFixture; |
| 12 | +using Bit.Core.Test.AutoFixture.OrganizationUserFixtures; |
| 13 | +using Bit.Test.Common.AutoFixture; |
| 14 | +using Bit.Test.Common.AutoFixture.Attributes; |
| 15 | +using NSubstitute; |
| 16 | +using Xunit; |
| 17 | +using static Bit.Core.AdminConsole.Utilities.v2.Validation.ValidationResultHelpers; |
| 18 | + |
| 19 | +namespace Bit.Core.Test.AdminConsole.OrganizationFeatures.OrganizationUsers.AutoConfirmUser; |
| 20 | + |
| 21 | +[SutProviderCustomize] |
| 22 | +public class AutomaticallyConfirmOrganizationUserCommandTests |
| 23 | +{ |
| 24 | + [Theory, BitAutoData] |
| 25 | + public async Task AutomaticallyConfirmOrganizationUserAsync_UseMyItemsDisabled_DoesNotCreateCollection( |
| 26 | + Organization organization, |
| 27 | + [OrganizationUser(OrganizationUserStatusType.Accepted)] OrganizationUser orgUser, |
| 28 | + string key, |
| 29 | + string collectionName, |
| 30 | + SutProvider<AutomaticallyConfirmOrganizationUserCommand> sutProvider) |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + organization.PlanType = PlanType.EnterpriseAnnually; |
| 34 | + organization.UseMyItems = false; |
| 35 | + orgUser.OrganizationId = organization.Id; |
| 36 | + |
| 37 | + var request = new AutomaticallyConfirmOrganizationUserRequest |
| 38 | + { |
| 39 | + OrganizationUserId = orgUser.Id, |
| 40 | + OrganizationId = organization.Id, |
| 41 | + Key = key, |
| 42 | + DefaultUserCollectionName = collectionName, |
| 43 | + PerformedBy = null |
| 44 | + }; |
| 45 | + |
| 46 | + var validationRequest = new AutomaticallyConfirmOrganizationUserValidationRequest |
| 47 | + { |
| 48 | + OrganizationUserId = orgUser.Id, |
| 49 | + OrganizationId = organization.Id, |
| 50 | + Key = key, |
| 51 | + DefaultUserCollectionName = collectionName, |
| 52 | + PerformedBy = null, |
| 53 | + OrganizationUser = orgUser, |
| 54 | + Organization = organization |
| 55 | + }; |
| 56 | + |
| 57 | + sutProvider.GetDependency<IOrganizationUserRepository>() |
| 58 | + .GetByIdAsync(orgUser.Id) |
| 59 | + .Returns(orgUser); |
| 60 | + sutProvider.GetDependency<IOrganizationRepository>() |
| 61 | + .GetByIdAsync(organization.Id) |
| 62 | + .Returns(organization); |
| 63 | + sutProvider.GetDependency<IOrganizationUserRepository>() |
| 64 | + .ConfirmOrganizationUserAsync(Arg.Any<Bit.Core.AdminConsole.Models.Data.OrganizationUsers.AcceptedOrganizationUserToConfirm>()) |
| 65 | + .Returns(true); |
| 66 | + |
| 67 | + var policyDetails = new PolicyDetails |
| 68 | + { |
| 69 | + OrganizationId = organization.Id, |
| 70 | + OrganizationUserId = orgUser.Id, |
| 71 | + IsProvider = false, |
| 72 | + OrganizationUserStatus = orgUser.Status, |
| 73 | + OrganizationUserType = orgUser.Type, |
| 74 | + PolicyType = PolicyType.OrganizationDataOwnership |
| 75 | + }; |
| 76 | + sutProvider.GetDependency<IPolicyRequirementQuery>() |
| 77 | + .GetAsync<OrganizationDataOwnershipPolicyRequirement>(orgUser.UserId!.Value) |
| 78 | + .Returns(new OrganizationDataOwnershipPolicyRequirement(OrganizationDataOwnershipState.Enabled, [policyDetails])); |
| 79 | + |
| 80 | + sutProvider.GetDependency<IAutomaticallyConfirmOrganizationUsersValidator>() |
| 81 | + .ValidateAsync(Arg.Any<AutomaticallyConfirmOrganizationUserValidationRequest>()) |
| 82 | + .Returns(Valid(validationRequest)); |
| 83 | + |
| 84 | + // Act |
| 85 | + await sutProvider.Sut.AutomaticallyConfirmOrganizationUserAsync(request); |
| 86 | + |
| 87 | + // Assert - Collection repository should NOT be called |
| 88 | + await sutProvider.GetDependency<ICollectionRepository>() |
| 89 | + .DidNotReceive() |
| 90 | + .CreateDefaultCollectionsAsync(Arg.Any<Guid>(), Arg.Any<IEnumerable<Guid>>(), Arg.Any<string>()); |
| 91 | + } |
| 92 | + |
| 93 | + [Theory, BitAutoData] |
| 94 | + public async Task AutomaticallyConfirmOrganizationUserAsync_UseMyItemsEnabled_CreatesCollection( |
| 95 | + Organization organization, |
| 96 | + [OrganizationUser(OrganizationUserStatusType.Accepted)] OrganizationUser orgUser, |
| 97 | + string key, |
| 98 | + string collectionName, |
| 99 | + SutProvider<AutomaticallyConfirmOrganizationUserCommand> sutProvider) |
| 100 | + { |
| 101 | + // Arrange |
| 102 | + organization.PlanType = PlanType.EnterpriseAnnually; |
| 103 | + organization.UseMyItems = true; |
| 104 | + orgUser.OrganizationId = organization.Id; |
| 105 | + |
| 106 | + var request = new AutomaticallyConfirmOrganizationUserRequest |
| 107 | + { |
| 108 | + OrganizationUserId = orgUser.Id, |
| 109 | + OrganizationId = organization.Id, |
| 110 | + Key = key, |
| 111 | + DefaultUserCollectionName = collectionName, |
| 112 | + PerformedBy = null |
| 113 | + }; |
| 114 | + |
| 115 | + var validationRequest = new AutomaticallyConfirmOrganizationUserValidationRequest |
| 116 | + { |
| 117 | + OrganizationUserId = orgUser.Id, |
| 118 | + OrganizationId = organization.Id, |
| 119 | + Key = key, |
| 120 | + DefaultUserCollectionName = collectionName, |
| 121 | + PerformedBy = null, |
| 122 | + OrganizationUser = orgUser, |
| 123 | + Organization = organization |
| 124 | + }; |
| 125 | + |
| 126 | + sutProvider.GetDependency<IOrganizationUserRepository>() |
| 127 | + .GetByIdAsync(orgUser.Id) |
| 128 | + .Returns(orgUser); |
| 129 | + sutProvider.GetDependency<IOrganizationRepository>() |
| 130 | + .GetByIdAsync(organization.Id) |
| 131 | + .Returns(organization); |
| 132 | + sutProvider.GetDependency<IOrganizationUserRepository>() |
| 133 | + .ConfirmOrganizationUserAsync(Arg.Any<Bit.Core.AdminConsole.Models.Data.OrganizationUsers.AcceptedOrganizationUserToConfirm>()) |
| 134 | + .Returns(true); |
| 135 | + |
| 136 | + var policyDetails = new PolicyDetails |
| 137 | + { |
| 138 | + OrganizationId = organization.Id, |
| 139 | + OrganizationUserId = orgUser.Id, |
| 140 | + IsProvider = false, |
| 141 | + OrganizationUserStatus = orgUser.Status, |
| 142 | + OrganizationUserType = orgUser.Type, |
| 143 | + PolicyType = PolicyType.OrganizationDataOwnership |
| 144 | + }; |
| 145 | + sutProvider.GetDependency<IPolicyRequirementQuery>() |
| 146 | + .GetAsync<OrganizationDataOwnershipPolicyRequirement>(orgUser.UserId!.Value) |
| 147 | + .Returns(new OrganizationDataOwnershipPolicyRequirement(OrganizationDataOwnershipState.Enabled, [policyDetails])); |
| 148 | + |
| 149 | + sutProvider.GetDependency<IAutomaticallyConfirmOrganizationUsersValidator>() |
| 150 | + .ValidateAsync(Arg.Any<AutomaticallyConfirmOrganizationUserValidationRequest>()) |
| 151 | + .Returns(Valid(validationRequest)); |
| 152 | + |
| 153 | + // Act |
| 154 | + await sutProvider.Sut.AutomaticallyConfirmOrganizationUserAsync(request); |
| 155 | + |
| 156 | + // Assert - Collection repository should be called |
| 157 | + await sutProvider.GetDependency<ICollectionRepository>() |
| 158 | + .Received(1) |
| 159 | + .CreateDefaultCollectionsAsync( |
| 160 | + organization.Id, |
| 161 | + Arg.Is<IEnumerable<Guid>>(ids => ids.Single() == orgUser.Id), |
| 162 | + collectionName); |
| 163 | + } |
| 164 | + |
| 165 | + [Theory, BitAutoData] |
| 166 | + public async Task AutomaticallyConfirmOrganizationUserAsync_UseMyItemsEnabled_PolicyDisabled_DoesNotCreateCollection( |
| 167 | + Organization organization, |
| 168 | + [OrganizationUser(OrganizationUserStatusType.Accepted)] OrganizationUser orgUser, |
| 169 | + string key, |
| 170 | + string collectionName, |
| 171 | + SutProvider<AutomaticallyConfirmOrganizationUserCommand> sutProvider) |
| 172 | + { |
| 173 | + // Arrange |
| 174 | + organization.PlanType = PlanType.EnterpriseAnnually; |
| 175 | + organization.UseMyItems = true; |
| 176 | + orgUser.OrganizationId = organization.Id; |
| 177 | + |
| 178 | + var request = new AutomaticallyConfirmOrganizationUserRequest |
| 179 | + { |
| 180 | + OrganizationUserId = orgUser.Id, |
| 181 | + OrganizationId = organization.Id, |
| 182 | + Key = key, |
| 183 | + DefaultUserCollectionName = collectionName, |
| 184 | + PerformedBy = null |
| 185 | + }; |
| 186 | + |
| 187 | + var validationRequest = new AutomaticallyConfirmOrganizationUserValidationRequest |
| 188 | + { |
| 189 | + OrganizationUserId = orgUser.Id, |
| 190 | + OrganizationId = organization.Id, |
| 191 | + Key = key, |
| 192 | + DefaultUserCollectionName = collectionName, |
| 193 | + PerformedBy = null, |
| 194 | + OrganizationUser = orgUser, |
| 195 | + Organization = organization |
| 196 | + }; |
| 197 | + |
| 198 | + sutProvider.GetDependency<IOrganizationUserRepository>() |
| 199 | + .GetByIdAsync(orgUser.Id) |
| 200 | + .Returns(orgUser); |
| 201 | + sutProvider.GetDependency<IOrganizationRepository>() |
| 202 | + .GetByIdAsync(organization.Id) |
| 203 | + .Returns(organization); |
| 204 | + sutProvider.GetDependency<IOrganizationUserRepository>() |
| 205 | + .ConfirmOrganizationUserAsync(Arg.Any<Bit.Core.AdminConsole.Models.Data.OrganizationUsers.AcceptedOrganizationUserToConfirm>()) |
| 206 | + .Returns(true); |
| 207 | + |
| 208 | + // Policy is disabled |
| 209 | + sutProvider.GetDependency<IPolicyRequirementQuery>() |
| 210 | + .GetAsync<OrganizationDataOwnershipPolicyRequirement>(orgUser.UserId!.Value) |
| 211 | + .Returns(new OrganizationDataOwnershipPolicyRequirement(OrganizationDataOwnershipState.Disabled, [])); |
| 212 | + |
| 213 | + sutProvider.GetDependency<IAutomaticallyConfirmOrganizationUsersValidator>() |
| 214 | + .ValidateAsync(Arg.Any<AutomaticallyConfirmOrganizationUserValidationRequest>()) |
| 215 | + .Returns(Valid(validationRequest)); |
| 216 | + |
| 217 | + // Act |
| 218 | + await sutProvider.Sut.AutomaticallyConfirmOrganizationUserAsync(request); |
| 219 | + |
| 220 | + // Assert - Collection repository should NOT be called when policy is disabled |
| 221 | + await sutProvider.GetDependency<ICollectionRepository>() |
| 222 | + .DidNotReceive() |
| 223 | + .CreateDefaultCollectionsAsync(Arg.Any<Guid>(), Arg.Any<IEnumerable<Guid>>(), Arg.Any<string>()); |
| 224 | + } |
| 225 | +} |
0 commit comments