mirror of
https://github.com/bitwarden/server.git
synced 2025-06-28 06:36:15 -05:00
remove redundant code, remove looping db call, refactor tests
This commit is contained in:
parent
2cac1c82d0
commit
be23fe6420
@ -159,10 +159,13 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
|
|||||||
var newUsersEmailsDict = newUsers.ToDictionary(u => u.Email);
|
var newUsersEmailsDict = newUsers.ToDictionary(u => u.Email);
|
||||||
var usersToAttach = existingUsersEmailsDict.Keys.Intersect(newUsersEmailsDict.Keys).ToList();
|
var usersToAttach = existingUsersEmailsDict.Keys.Intersect(newUsersEmailsDict.Keys).ToList();
|
||||||
var usersToUpsert = new List<OrganizationUser>();
|
var usersToUpsert = new List<OrganizationUser>();
|
||||||
|
|
||||||
|
var orgUsers = (await _organizationUserRepository.GetManyAsync(existingUsers.Select(u => u.Id).ToList())).ToDictionary(u => u.Id);
|
||||||
|
|
||||||
foreach (var user in usersToAttach)
|
foreach (var user in usersToAttach)
|
||||||
{
|
{
|
||||||
var orgUserDetails = existingUsersEmailsDict[user];
|
var orgUserDetails = existingUsersEmailsDict[user];
|
||||||
var orgUser = await _organizationUserRepository.GetByIdAsync(orgUserDetails.Id);
|
var orgUser = orgUsers[existingUsersEmailsDict[user].Id];
|
||||||
if (orgUser != null)
|
if (orgUser != null)
|
||||||
{
|
{
|
||||||
orgUser.ExternalId = newUsersEmailsDict[user].ExternalId;
|
orgUser.ExternalId = newUsersEmailsDict[user].ExternalId;
|
||||||
@ -179,38 +182,13 @@ public class ImportOrganizationUserCommand : IImportOrganizationUserCommand
|
|||||||
OrganizationUserImportData importUserData)
|
OrganizationUserImportData importUserData)
|
||||||
{
|
{
|
||||||
|
|
||||||
var existingUsersSet = new HashSet<string>(importUserData.ExistingExternalUsersIdDict.Keys);
|
|
||||||
var usersToAdd = importUserData.NewUsersSet.Except(existingUsersSet).ToList();
|
|
||||||
|
|
||||||
var seatsAvailable = int.MaxValue;
|
|
||||||
var enoughSeatsAvailable = true;
|
|
||||||
if (organization.Seats.HasValue)
|
|
||||||
{
|
|
||||||
var occupiedSeats = await _organizationUserRepository.GetOccupiedSeatCountByOrganizationIdAsync(organization.Id);
|
|
||||||
seatsAvailable = organization.Seats.Value - occupiedSeats;
|
|
||||||
enoughSeatsAvailable = seatsAvailable >= usersToAdd.Count;
|
|
||||||
}
|
|
||||||
|
|
||||||
var hasStandaloneSecretsManager = await _paymentService.HasSecretsManagerStandalone(organization);
|
var hasStandaloneSecretsManager = await _paymentService.HasSecretsManagerStandalone(organization);
|
||||||
|
|
||||||
var userInvites = new List<OrganizationUserInviteCommandModel>();
|
var userInvites = new List<OrganizationUserInviteCommandModel>();
|
||||||
|
|
||||||
foreach (var user in newUsers)
|
foreach (var user in newUsers)
|
||||||
{
|
{
|
||||||
if (!usersToAdd.Contains(user.ExternalId) || string.IsNullOrWhiteSpace(user.Email))
|
var invite = new OrganizationUserInviteCommandModel(user.Email, user.ExternalId);
|
||||||
{
|
userInvites.Add(new OrganizationUserInviteCommandModel(invite, hasStandaloneSecretsManager));
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var invite = new OrganizationUserInviteCommandModel(user.Email, user.ExternalId);
|
|
||||||
userInvites.Add(new OrganizationUserInviteCommandModel(invite, hasStandaloneSecretsManager));
|
|
||||||
}
|
|
||||||
catch (BadRequestException)
|
|
||||||
{
|
|
||||||
// Thrown when the user is already invited to the organization
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var commandResult = await InviteUsersAsync(userInvites, organization);
|
var commandResult = await InviteUsersAsync(userInvites, organization);
|
||||||
|
@ -30,7 +30,7 @@ public class ImportOrganizationUserCommandTests
|
|||||||
private readonly IDataProtectorTokenFactory<OrgUserInviteTokenable> _orgUserInviteTokenDataFactory = new FakeDataProtectorTokenFactory<OrgUserInviteTokenable>();
|
private readonly IDataProtectorTokenFactory<OrgUserInviteTokenable> _orgUserInviteTokenDataFactory = new FakeDataProtectorTokenFactory<OrgUserInviteTokenable>();
|
||||||
|
|
||||||
[Theory, PaidOrganizationCustomize, BitAutoData]
|
[Theory, PaidOrganizationCustomize, BitAutoData]
|
||||||
public async Task OrgImportCreateNewUsers(
|
public async Task OrgImportCallsInviteOrgUserCommand(
|
||||||
SutProvider<ImportOrganizationUserCommand> sutProvider,
|
SutProvider<ImportOrganizationUserCommand> sutProvider,
|
||||||
Organization org,
|
Organization org,
|
||||||
List<OrganizationUserUserDetails> existingUsers,
|
List<OrganizationUserUserDetails> existingUsers,
|
||||||
@ -39,24 +39,17 @@ public class ImportOrganizationUserCommandTests
|
|||||||
{
|
{
|
||||||
SetupOrganizationConfigForImport(sutProvider, org, existingUsers, newUsers);
|
SetupOrganizationConfigForImport(sutProvider, org, existingUsers, newUsers);
|
||||||
|
|
||||||
var expectedNewUsersCount = newUsers.Count - 1;
|
|
||||||
var invitedOrganizationUsers = new List<OrganizationUser>();
|
|
||||||
var invites = new List<OrganizationUserInviteCommandModel>();
|
|
||||||
|
|
||||||
foreach (var u in newUsers)
|
|
||||||
{
|
|
||||||
invitedOrganizationUsers.Add(new OrganizationUser { Email = u.Email + "@test.com", ExternalId = u.ExternalId });
|
|
||||||
invites.Add(new OrganizationUserInviteCommandModel(u.Email + "@test.com", u.ExternalId));
|
|
||||||
}
|
|
||||||
|
|
||||||
var inviteCommandModel = new InviteOrganizationUsersRequest(invites.ToArray(), new InviteOrganization(org, null), org.Id, DateTimeOffset.UtcNow);
|
|
||||||
|
|
||||||
newUsers.Add(new ImportedOrganizationUser
|
newUsers.Add(new ImportedOrganizationUser
|
||||||
{
|
{
|
||||||
Email = existingUsers.First().Email,
|
Email = existingUsers.First().Email,
|
||||||
ExternalId = existingUsers.First().ExternalId
|
ExternalId = existingUsers.First().ExternalId
|
||||||
});
|
});
|
||||||
|
|
||||||
|
foreach (var u in newUsers)
|
||||||
|
{
|
||||||
|
u.Email += "@bitwardentest.com";
|
||||||
|
}
|
||||||
|
|
||||||
existingUsers.First().Type = OrganizationUserType.Owner;
|
existingUsers.First().Type = OrganizationUserType.Owner;
|
||||||
|
|
||||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(org.Id).Returns(org);
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(org.Id).Returns(org);
|
||||||
@ -64,18 +57,13 @@ public class ImportOrganizationUserCommandTests
|
|||||||
sutProvider.GetDependency<IOrganizationUserRepository>().GetManyDetailsByOrganizationAsync(org.Id).Returns(existingUsers);
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetManyDetailsByOrganizationAsync(org.Id).Returns(existingUsers);
|
||||||
sutProvider.GetDependency<IOrganizationUserRepository>().GetCountByOrganizationIdAsync(org.Id).Returns(existingUsers.Count);
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetCountByOrganizationIdAsync(org.Id).Returns(existingUsers.Count);
|
||||||
sutProvider.GetDependency<ICurrentContext>().ManageUsers(org.Id).Returns(true);
|
sutProvider.GetDependency<ICurrentContext>().ManageUsers(org.Id).Returns(true);
|
||||||
// Use the arranged command model
|
sutProvider.GetDependency<IInviteOrganizationUsersCommand>().InviteImportedOrganizationUsersAsync(Arg.Any<InviteOrganizationUsersRequest>(), org.Id)
|
||||||
sutProvider.GetDependency<IInviteOrganizationUsersCommand>().InviteImportedOrganizationUsersAsync(inviteCommandModel, org.Id)
|
.Returns(new Success<InviteOrganizationUsersResponse>(new InviteOrganizationUsersResponse(org.Id)));
|
||||||
// assert against this returned CommandResult response
|
|
||||||
.Returns(new Success<InviteOrganizationUsersResponse>(new InviteOrganizationUsersResponse(invitedOrganizationUsers, org.Id)));
|
|
||||||
|
|
||||||
await sutProvider.Sut.ImportAsync(org.Id, newGroups, newUsers, new List<string>(), false, EventSystemUser.PublicApi);
|
await sutProvider.Sut.ImportAsync(org.Id, newGroups, newUsers, new List<string>(), false, EventSystemUser.PublicApi);
|
||||||
|
|
||||||
await sutProvider.GetDependency<IInviteOrganizationUsersCommand>().Received(1)
|
await sutProvider.GetDependency<IInviteOrganizationUsersCommand>().Received(1)
|
||||||
.InviteImportedOrganizationUsersAsync(Arg.Is<InviteOrganizationUsersRequest>(
|
.InviteImportedOrganizationUsersAsync(Arg.Any<InviteOrganizationUsersRequest>(), org.Id);
|
||||||
// These are the invites that should get populated from the CommandResult response above
|
|
||||||
request => request.Invites.Count() == expectedNewUsersCount
|
|
||||||
), org.Id);
|
|
||||||
await sutProvider.GetDependency<IOrganizationUserRepository>().DidNotReceiveWithAnyArgs()
|
await sutProvider.GetDependency<IOrganizationUserRepository>().DidNotReceiveWithAnyArgs()
|
||||||
.UpsertAsync(default);
|
.UpsertAsync(default);
|
||||||
await sutProvider.GetDependency<IOrganizationUserRepository>().Received(1)
|
await sutProvider.GetDependency<IOrganizationUserRepository>().Received(1)
|
||||||
@ -102,6 +90,15 @@ public class ImportOrganizationUserCommandTests
|
|||||||
|
|
||||||
var reInvitedUser = existingUsers.First();
|
var reInvitedUser = existingUsers.First();
|
||||||
reInvitedUser.ExternalId = null;
|
reInvitedUser.ExternalId = null;
|
||||||
|
foreach (var u in existingUsers)
|
||||||
|
{
|
||||||
|
u.Email += "@bitwardentest.com";
|
||||||
|
}
|
||||||
|
foreach (var u in newUsers)
|
||||||
|
{
|
||||||
|
u.Email += "@bitwardentest.com";
|
||||||
|
}
|
||||||
|
|
||||||
newUsers.Add(new ImportedOrganizationUser
|
newUsers.Add(new ImportedOrganizationUser
|
||||||
{
|
{
|
||||||
Email = reInvitedUser.Email,
|
Email = reInvitedUser.Email,
|
||||||
@ -111,11 +108,10 @@ public class ImportOrganizationUserCommandTests
|
|||||||
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(org.Id).Returns(org);
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(org.Id).Returns(org);
|
||||||
sutProvider.GetDependency<IOrganizationUserRepository>().GetManyDetailsByOrganizationAsync(org.Id).Returns(existingUsers);
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetManyDetailsByOrganizationAsync(org.Id).Returns(existingUsers);
|
||||||
sutProvider.GetDependency<IOrganizationUserRepository>().GetCountByOrganizationIdAsync(org.Id).Returns(existingUsers.Count);
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetCountByOrganizationIdAsync(org.Id).Returns(existingUsers.Count);
|
||||||
sutProvider.GetDependency<IOrganizationUserRepository>().GetByIdAsync(reInvitedUser.Id).Returns(new OrganizationUser { Id = reInvitedUser.Id });
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetManyAsync(Arg.Any<IEnumerable<Guid>>()).Returns(new List<OrganizationUser> { new OrganizationUser { Id = reInvitedUser.Id } });
|
||||||
sutProvider.GetDependency<IInviteOrganizationUsersCommand>().InviteImportedOrganizationUsersAsync(Arg.Any<InviteOrganizationUsersRequest>(), org.Id)
|
sutProvider.GetDependency<IInviteOrganizationUsersCommand>().InviteImportedOrganizationUsersAsync(Arg.Any<InviteOrganizationUsersRequest>(), org.Id)
|
||||||
.Returns(new Success<InviteOrganizationUsersResponse>(new InviteOrganizationUsersResponse(org.Id)));
|
.Returns(new Success<InviteOrganizationUsersResponse>(new InviteOrganizationUsersResponse(org.Id)));
|
||||||
|
|
||||||
|
|
||||||
await sutProvider.Sut.ImportAsync(org.Id, newGroups, newUsers, new List<string>(), false, EventSystemUser.PublicApi);
|
await sutProvider.Sut.ImportAsync(org.Id, newGroups, newUsers, new List<string>(), false, EventSystemUser.PublicApi);
|
||||||
|
|
||||||
await sutProvider.GetDependency<IOrganizationUserRepository>().DidNotReceiveWithAnyArgs()
|
await sutProvider.GetDependency<IOrganizationUserRepository>().DidNotReceiveWithAnyArgs()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user