|
| 1 | +using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers; |
| 2 | +using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces; |
| 3 | +using Bit.Core.Entities; |
| 4 | +using Bit.Core.Exceptions; |
| 5 | +using Bit.Core.Repositories; |
| 6 | +using Bit.Test.Common.AutoFixture; |
| 7 | +using Bit.Test.Common.AutoFixture.Attributes; |
| 8 | +using NSubstitute; |
| 9 | +using NSubstitute.ExceptionExtensions; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Bit.Core.Test.AdminConsole.OrganizationFeatures.OrganizationUsers; |
| 13 | + |
| 14 | +[SutProviderCustomize] |
| 15 | +public class BulkChangeEmailForPasswordlessOrgUserCommandTests |
| 16 | +{ |
| 17 | + [Theory, BitAutoData] |
| 18 | + public async Task BulkChangeOrganizationUserEmailAsync_AllSucceed_ReturnsEmptyErrors( |
| 19 | + Guid organizationId, |
| 20 | + OrganizationUser orgUser1, |
| 21 | + OrganizationUser orgUser2, |
| 22 | + SutProvider<BulkChangeEmailForPasswordlessOrgUserCommand> sutProvider) |
| 23 | + { |
| 24 | + orgUser1.OrganizationId = organizationId; |
| 25 | + orgUser2.OrganizationId = organizationId; |
| 26 | + |
| 27 | + sutProvider.GetDependency<IOrganizationUserRepository>() |
| 28 | + .GetByIdAsync(orgUser1.Id).Returns(orgUser1); |
| 29 | + sutProvider.GetDependency<IOrganizationUserRepository>() |
| 30 | + .GetByIdAsync(orgUser2.Id).Returns(orgUser2); |
| 31 | + |
| 32 | + var requests = new[] |
| 33 | + { |
| 34 | + (orgUser1.Id, "user1@example.com"), |
| 35 | + (orgUser2.Id, "user2@example.com"), |
| 36 | + }; |
| 37 | + |
| 38 | + var results = (await sutProvider.Sut.BulkChangeOrganizationUserEmailAsync(organizationId, requests)).ToList(); |
| 39 | + |
| 40 | + Assert.Equal(2, results.Count); |
| 41 | + Assert.All(results, r => Assert.Equal(string.Empty, r.ErrorMessage)); |
| 42 | + await sutProvider.GetDependency<IChangeEmailForPasswordlessOrgUserCommand>() |
| 43 | + .Received(2) |
| 44 | + .ChangeOrganizationUserEmailAsync(organizationId, Arg.Any<OrganizationUser>(), Arg.Any<string>()); |
| 45 | + } |
| 46 | + |
| 47 | + [Theory, BitAutoData] |
| 48 | + public async Task BulkChangeOrganizationUserEmailAsync_PartialFailure_ReturnsPerItemErrors( |
| 49 | + Guid organizationId, |
| 50 | + OrganizationUser orgUser1, |
| 51 | + OrganizationUser orgUser2, |
| 52 | + SutProvider<BulkChangeEmailForPasswordlessOrgUserCommand> sutProvider) |
| 53 | + { |
| 54 | + orgUser1.OrganizationId = organizationId; |
| 55 | + orgUser2.OrganizationId = organizationId; |
| 56 | + |
| 57 | + sutProvider.GetDependency<IOrganizationUserRepository>() |
| 58 | + .GetByIdAsync(orgUser1.Id).Returns(orgUser1); |
| 59 | + sutProvider.GetDependency<IOrganizationUserRepository>() |
| 60 | + .GetByIdAsync(orgUser2.Id).Returns(orgUser2); |
| 61 | + |
| 62 | + sutProvider.GetDependency<IChangeEmailForPasswordlessOrgUserCommand>() |
| 63 | + .ChangeOrganizationUserEmailAsync(organizationId, orgUser1, Arg.Any<string>()) |
| 64 | + .ThrowsAsync(new BadRequestException("User has a master password.")); |
| 65 | + |
| 66 | + var requests = new[] |
| 67 | + { |
| 68 | + (orgUser1.Id, "user1@example.com"), |
| 69 | + (orgUser2.Id, "user2@example.com"), |
| 70 | + }; |
| 71 | + |
| 72 | + var results = (await sutProvider.Sut.BulkChangeOrganizationUserEmailAsync(organizationId, requests)).ToList(); |
| 73 | + |
| 74 | + Assert.Equal(2, results.Count); |
| 75 | + Assert.Equal("User has a master password.", results[0].ErrorMessage); |
| 76 | + Assert.Equal(string.Empty, results[1].ErrorMessage); |
| 77 | + } |
| 78 | + |
| 79 | + [Theory, BitAutoData] |
| 80 | + public async Task BulkChangeOrganizationUserEmailAsync_OrgUserNotFound_ReturnsError( |
| 81 | + Guid organizationId, |
| 82 | + Guid unknownOrgUserId, |
| 83 | + SutProvider<BulkChangeEmailForPasswordlessOrgUserCommand> sutProvider) |
| 84 | + { |
| 85 | + sutProvider.GetDependency<IOrganizationUserRepository>() |
| 86 | + .GetByIdAsync(unknownOrgUserId).Returns((OrganizationUser)null); |
| 87 | + |
| 88 | + var requests = new[] { (unknownOrgUserId, "user@example.com") }; |
| 89 | + |
| 90 | + var results = (await sutProvider.Sut.BulkChangeOrganizationUserEmailAsync(organizationId, requests)).ToList(); |
| 91 | + |
| 92 | + Assert.Single(results); |
| 93 | + Assert.NotEmpty(results[0].ErrorMessage); |
| 94 | + await sutProvider.GetDependency<IChangeEmailForPasswordlessOrgUserCommand>() |
| 95 | + .DidNotReceive() |
| 96 | + .ChangeOrganizationUserEmailAsync(Arg.Any<Guid>(), Arg.Any<OrganizationUser>(), Arg.Any<string>()); |
| 97 | + } |
| 98 | + |
| 99 | + [Theory, BitAutoData] |
| 100 | + public async Task BulkChangeOrganizationUserEmailAsync_OrgUserBelongsToDifferentOrg_ReturnsError( |
| 101 | + Guid organizationId, |
| 102 | + OrganizationUser orgUserFromOtherOrg, |
| 103 | + SutProvider<BulkChangeEmailForPasswordlessOrgUserCommand> sutProvider) |
| 104 | + { |
| 105 | + // OrganizationId on the fetched user does not match the requested org. |
| 106 | + orgUserFromOtherOrg.OrganizationId = Guid.NewGuid(); |
| 107 | + |
| 108 | + sutProvider.GetDependency<IOrganizationUserRepository>() |
| 109 | + .GetByIdAsync(orgUserFromOtherOrg.Id).Returns(orgUserFromOtherOrg); |
| 110 | + |
| 111 | + var requests = new[] { (orgUserFromOtherOrg.Id, "user@example.com") }; |
| 112 | + |
| 113 | + var results = (await sutProvider.Sut.BulkChangeOrganizationUserEmailAsync(organizationId, requests)).ToList(); |
| 114 | + |
| 115 | + Assert.Single(results); |
| 116 | + Assert.NotEmpty(results[0].ErrorMessage); |
| 117 | + await sutProvider.GetDependency<IChangeEmailForPasswordlessOrgUserCommand>() |
| 118 | + .DidNotReceive() |
| 119 | + .ChangeOrganizationUserEmailAsync(Arg.Any<Guid>(), Arg.Any<OrganizationUser>(), Arg.Any<string>()); |
| 120 | + } |
| 121 | +} |
0 commit comments