From de85dbc67c2ff53c8bb40aebea4d6d39f9fb787e Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 19 Feb 2019 16:27:05 -0500 Subject: [PATCH] tests for org import user marrying --- .../Services/OrganizationServiceTests.cs | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 test/Core.Test/Services/OrganizationServiceTests.cs diff --git a/test/Core.Test/Services/OrganizationServiceTests.cs b/test/Core.Test/Services/OrganizationServiceTests.cs new file mode 100644 index 000000000..ebf10f75c --- /dev/null +++ b/test/Core.Test/Services/OrganizationServiceTests.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Bit.Core.Models.Data; +using Bit.Core.Models.Table; +using Bit.Core.Repositories; +using Bit.Core.Services; +using Microsoft.AspNetCore.DataProtection; +using NSubstitute; +using Xunit; + +namespace Bit.Core.Test.Services +{ + public class OrganizationServiceTests + { + [Fact] + public async Task OrgImportCreateNewUsers() + { + var orgRepo = Substitute.For(); + var orgUserRepo = Substitute.For(); + var collectionRepo = Substitute.For(); + var userRepo = Substitute.For(); + var groupRepo = Substitute.For(); + var dataProtector = Substitute.For(); + var mailService = Substitute.For(); + var pushNotService = Substitute.For(); + var pushRegService = Substitute.For(); + var deviceRepo = Substitute.For(); + var licenseService = Substitute.For(); + var eventService = Substitute.For(); + var installationRepo = Substitute.For(); + var appCacheService = Substitute.For(); + var paymentService = Substitute.For(); + var globalSettings = Substitute.For(); + + var orgService = new OrganizationService(orgRepo, orgUserRepo, collectionRepo, userRepo, + groupRepo, dataProtector, mailService, pushNotService, pushRegService, deviceRepo, + licenseService, eventService, installationRepo, appCacheService, paymentService, globalSettings); + + var id = Guid.NewGuid(); + var userId = Guid.NewGuid(); + var org = new Organization + { + Id = id, + Name = "Test Org", + UseDirectory = true, + UseGroups = true, + Seats = 3 + }; + orgRepo.GetByIdAsync(id).Returns(org); + + var existingUsers = new List(); + existingUsers.Add(new OrganizationUserUserDetails + { + Id = Guid.NewGuid(), + ExternalId = "a", + Email = "a@test.com" + }); + orgUserRepo.GetManyDetailsByOrganizationAsync(id).Returns(existingUsers); + orgUserRepo.GetCountByOrganizationIdAsync(id).Returns(1); + + var newUsers = new List(); + newUsers.Add(new Models.Business.ImportedOrganizationUser { Email = "a@test.com", ExternalId = "a" }); + newUsers.Add(new Models.Business.ImportedOrganizationUser { Email = "b@test.com", ExternalId = "b" }); + newUsers.Add(new Models.Business.ImportedOrganizationUser { Email = "c@test.com", ExternalId = "c" }); + await orgService.ImportAsync(id, userId, null, newUsers, null); + + await orgUserRepo.DidNotReceive().UpsertAsync(Arg.Any()); + await orgUserRepo.Received(2).CreateAsync(Arg.Any()); + } + + [Fact] + public async Task OrgImportCreateNewUsersAndMarryExistingUser() + { + var orgRepo = Substitute.For(); + var orgUserRepo = Substitute.For(); + var collectionRepo = Substitute.For(); + var userRepo = Substitute.For(); + var groupRepo = Substitute.For(); + var dataProtector = Substitute.For(); + var mailService = Substitute.For(); + var pushNotService = Substitute.For(); + var pushRegService = Substitute.For(); + var deviceRepo = Substitute.For(); + var licenseService = Substitute.For(); + var eventService = Substitute.For(); + var installationRepo = Substitute.For(); + var appCacheService = Substitute.For(); + var paymentService = Substitute.For(); + var globalSettings = Substitute.For(); + + var orgService = new OrganizationService(orgRepo, orgUserRepo, collectionRepo, userRepo, + groupRepo, dataProtector, mailService, pushNotService, pushRegService, deviceRepo, + licenseService, eventService, installationRepo, appCacheService, paymentService, globalSettings); + + var id = Guid.NewGuid(); + var userId = Guid.NewGuid(); + var org = new Organization + { + Id = id, + Name = "Test Org", + UseDirectory = true, + UseGroups = true, + Seats = 3 + }; + orgRepo.GetByIdAsync(id).Returns(org); + + var existingUserAId = Guid.NewGuid(); + var existingUsers = new List(); + existingUsers.Add(new OrganizationUserUserDetails + { + Id = existingUserAId, + // No external id here + Email = "a@test.com" + }); + orgUserRepo.GetManyDetailsByOrganizationAsync(id).Returns(existingUsers); + orgUserRepo.GetCountByOrganizationIdAsync(id).Returns(1); + orgUserRepo.GetByIdAsync(existingUserAId).Returns(new OrganizationUser { Id = existingUserAId }); + + var newUsers = new List(); + newUsers.Add(new Models.Business.ImportedOrganizationUser { Email = "a@test.com", ExternalId = "a" }); + newUsers.Add(new Models.Business.ImportedOrganizationUser { Email = "b@test.com", ExternalId = "b" }); + newUsers.Add(new Models.Business.ImportedOrganizationUser { Email = "c@test.com", ExternalId = "c" }); + await orgService.ImportAsync(id, userId, null, newUsers, null); + + await orgUserRepo.Received(1).UpsertAsync(Arg.Any()); + await orgUserRepo.Received(2).CreateAsync(Arg.Any()); + } + } +}