diff --git a/test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/ImportOrganizationUserCommandTests.cs b/test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/ImportOrganizationUserCommandTests.cs index 8c682258ff..4abbd2b8af 100644 --- a/test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/ImportOrganizationUserCommandTests.cs +++ b/test/Core.Test/AdminConsole/OrganizationFeatures/OrganizationUsers/ImportOrganizationUserCommandTests.cs @@ -1,11 +1,13 @@ using Bit.Core.AdminConsole.Models.Business; using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers; +using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers; +using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers.Models; +using Bit.Core.AdminConsole.Utilities.Commands; using Bit.Core.Auth.Models.Business.Tokenables; using Bit.Core.Context; using Bit.Core.Entities; using Bit.Core.Enums; using Bit.Core.Models.Business; -using Bit.Core.Models.Data; using Bit.Core.Models.Data.Organizations.OrganizationUsers; using Bit.Core.Repositories; using Bit.Core.Services; @@ -30,46 +32,36 @@ public class ImportOrganizationUserCommandTests SutProvider sutProvider, Organization org, List existingUsers, - List importedUsers, + List newUsers, List newGroups) { - SetupOrganizationConfigForImport(sutProvider, org, existingUsers, importedUsers); + SetupOrganizationConfigForImport(sutProvider, org, existingUsers, newUsers); - var orgUsers = new List(); - - // fix mocked email format, mock OrganizationUsers. - foreach (var u in importedUsers) - { - u.Email += "@bitwardentest.com"; - orgUsers.Add(new OrganizationUser { Email = u.Email, ExternalId = u.ExternalId }); - } - - importedUsers.Add(new ImportedOrganizationUser + newUsers.Add(new ImportedOrganizationUser { Email = existingUsers.First().Email, ExternalId = existingUsers.First().ExternalId }); + foreach (var u in newUsers) + { + u.Email += "@bitwardentest.com"; + } existingUsers.First().Type = OrganizationUserType.Owner; sutProvider.GetDependency().GetByIdAsync(org.Id).Returns(org); - - var organizationUserRepository = sutProvider.GetDependency(); - SetupOrgUserRepositoryCreateManyAsyncMock(organizationUserRepository); - - sutProvider.GetDependency().HasSecretsManagerStandalone(org).Returns(true); + sutProvider.GetDependency().HasSecretsManagerStandalone(org).Returns(false); sutProvider.GetDependency().GetManyDetailsByOrganizationAsync(org.Id).Returns(existingUsers); sutProvider.GetDependency().GetCountByOrganizationIdAsync(org.Id).Returns(existingUsers.Count); sutProvider.GetDependency().ManageUsers(org.Id).Returns(true); - sutProvider.GetDependency().InviteUsersAsync(org.Id, Guid.Empty, EventSystemUser.PublicApi, - Arg.Any>()) - .Returns(orgUsers); + sutProvider.GetDependency().InviteImportedOrganizationUsersAsync(Arg.Any()) + .Returns(new Success(new InviteOrganizationUsersResponse(org.Id))); - await sutProvider.Sut.ImportAsync(org.Id, newGroups, importedUsers, new List(), false); - - var expectedNewUsersCount = importedUsers.Count - 1; + await sutProvider.Sut.ImportAsync(org.Id, newGroups, newUsers, new List(), false); + await sutProvider.GetDependency().Received(1) + .InviteImportedOrganizationUsersAsync(Arg.Any()); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .UpsertAsync(default); await sutProvider.GetDependency().Received(1) @@ -77,11 +69,6 @@ public class ImportOrganizationUserCommandTests await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .CreateAsync(default); - // Send Invites - await sutProvider.GetDependency().Received(1). - InviteUsersAsync(org.Id, Guid.Empty, EventSystemUser.PublicApi, - Arg.Is>(invites => invites.Count() == expectedNewUsersCount)); - // Send events await sutProvider.GetDependency().Received(1) .LogOrganizationUserEventsAsync(Arg.Any>()); @@ -92,55 +79,36 @@ public class ImportOrganizationUserCommandTests SutProvider sutProvider, Organization org, List existingUsers, - List importedUsers, + List newUsers, List newGroups) { - SetupOrganizationConfigForImport(sutProvider, org, existingUsers, importedUsers); + SetupOrganizationConfigForImport(sutProvider, org, existingUsers, newUsers); - var orgUsers = new List(); var reInvitedUser = existingUsers.First(); - // Existing user has no external ID. This will make the SUT call UpsertManyAsync - reInvitedUser.ExternalId = ""; - - // Mock an existing org user for this "existing" user - var reInvitedOrgUser = new OrganizationUser { Email = reInvitedUser.Email, Id = reInvitedUser.Id }; - - // fix email formatting, mock orgUsers to be returned + reInvitedUser.ExternalId = null; foreach (var u in existingUsers) { u.Email += "@bitwardentest.com"; - orgUsers.Add(new OrganizationUser { Email = u.Email, ExternalId = u.ExternalId }); } - foreach (var u in importedUsers) + foreach (var u in newUsers) { u.Email += "@bitwardentest.com"; - orgUsers.Add(new OrganizationUser { Email = u.Email, ExternalId = u.ExternalId }); } - // add the existing user to be re-imported - importedUsers.Add(new ImportedOrganizationUser + newUsers.Add(new ImportedOrganizationUser { Email = reInvitedUser.Email, ExternalId = reInvitedUser.Email, }); - var expectedNewUsersCount = importedUsers.Count - 1; - sutProvider.GetDependency().GetByIdAsync(org.Id).Returns(org); - - var organizationUserRepository = sutProvider.GetDependency(); - SetupOrgUserRepositoryCreateManyAsyncMock(organizationUserRepository); - - sutProvider.GetDependency().GetManyAsync(Arg.Any>()) - .Returns(new List([reInvitedOrgUser])); sutProvider.GetDependency().GetManyDetailsByOrganizationAsync(org.Id).Returns(existingUsers); sutProvider.GetDependency().GetCountByOrganizationIdAsync(org.Id).Returns(existingUsers.Count); + sutProvider.GetDependency().GetManyAsync(Arg.Any>()).Returns(new List { new OrganizationUser { Id = reInvitedUser.Id } }); + sutProvider.GetDependency().InviteImportedOrganizationUsersAsync(Arg.Any()) + .Returns(new Success(new InviteOrganizationUsersResponse(org.Id))); - sutProvider.GetDependency().InviteUsersAsync(org.Id, Guid.Empty, EventSystemUser.PublicApi, - Arg.Any>()) - .Returns(orgUsers); - - await sutProvider.Sut.ImportAsync(org.Id, newGroups, importedUsers, new List(), false); + await sutProvider.Sut.ImportAsync(org.Id, newGroups, newUsers, new List(), false); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .UpsertAsync(default); @@ -151,55 +119,28 @@ public class ImportOrganizationUserCommandTests // Upserted existing user await sutProvider.GetDependency().Received(1) - .UpsertManyAsync(Arg.Is>(users => users.Count() == 1 && users.First() == reInvitedOrgUser)); + .UpsertManyAsync(Arg.Is>(users => users.Count() == 1)); - // Send Invites - await sutProvider.GetDependency().Received(1). - InviteUsersAsync(org.Id, Guid.Empty, EventSystemUser.PublicApi, - Arg.Is>(invites => invites.Count() == expectedNewUsersCount)); + await sutProvider.GetDependency().Received(1) + .InviteImportedOrganizationUsersAsync(Arg.Any()); // Send events await sutProvider.GetDependency().Received(1) .LogOrganizationUserEventsAsync(Arg.Any>()); + } private void SetupOrganizationConfigForImport( SutProvider sutProvider, Organization org, List existingUsers, - List importedUsers) + List newUsers) { // Setup FakeDataProtectorTokenFactory for creating new tokens - this must come first in order to avoid resetting mocks sutProvider.SetDependency(_orgUserInviteTokenDataFactory, "orgUserInviteTokenDataFactory"); sutProvider.Create(); org.UseDirectory = true; - org.Seats = importedUsers.Count + existingUsers.Count + 1; - } - - // Must set real guids in order for dictionary of guids to not throw aggregate exceptions - private void SetupOrgUserRepositoryCreateManyAsyncMock(IOrganizationUserRepository organizationUserRepository) - { - organizationUserRepository.CreateManyAsync(Arg.Any>()).Returns( - info => - { - var orgUsers = info.Arg>(); - foreach (var orgUser in orgUsers) - { - orgUser.Id = Guid.NewGuid(); - } - - return Task.FromResult>(orgUsers.Select(u => u.Id).ToList()); - } - ); - - organizationUserRepository.CreateAsync(Arg.Any(), Arg.Any>()).Returns( - info => - { - var orgUser = info.Arg(); - orgUser.Id = Guid.NewGuid(); - return Task.FromResult(orgUser.Id); - } - ); + org.Seats = newUsers.Count + existingUsers.Count + 1; } }