1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-23 12:25:16 +01:00
bitwarden-server/test/Core.Test/Services/OrganizationServiceTests.cs
Addison Beck 59f8467f7c
Create sso user api (#886)
* facilitate linking/unlinking existing users from an sso enabled org

* added user_identifier to identity methods for sso

* moved sso user delete method to account controller

* fixed a broken test

* Update AccountsController.cs

* facilitate linking/unlinking existing users from an sso enabled org

* added user_identifier to identity methods for sso

* moved sso user delete method to account controller

* fixed a broken test

* added a token to the existing user sso link flow

* added a token to the existing user sso link flow

* fixed a typo

* added an event log for unlink ssoUser records

* fixed a merge issue

* fixed a busted test

* fixed a busted test

* ran a formatter over everything & changed .vscode settings in .gitignore

* chagned a variable to use string interpolation

* removed a blank line

* Changed TokenPurpose enum to a static class of strings

* code review cleanups

* formatting fix

* Changed parameters & logging for delete sso user

* changed th method used to get organization user for deleting sso user records

Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com>
2020-08-26 14:12:04 -04:00

141 lines
7.1 KiB
C#

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<IOrganizationRepository>();
var orgUserRepo = Substitute.For<IOrganizationUserRepository>();
var collectionRepo = Substitute.For<ICollectionRepository>();
var userRepo = Substitute.For<IUserRepository>();
var groupRepo = Substitute.For<IGroupRepository>();
var dataProtector = Substitute.For<IDataProtector>();
var mailService = Substitute.For<IMailService>();
var pushNotService = Substitute.For<IPushNotificationService>();
var pushRegService = Substitute.For<IPushRegistrationService>();
var deviceRepo = Substitute.For<IDeviceRepository>();
var licenseService = Substitute.For<ILicensingService>();
var eventService = Substitute.For<IEventService>();
var installationRepo = Substitute.For<IInstallationRepository>();
var appCacheService = Substitute.For<IApplicationCacheService>();
var paymentService = Substitute.For<IPaymentService>();
var policyRepo = Substitute.For<IPolicyRepository>();
var ssoConfigRepo = Substitute.For<ISsoConfigRepository>();
var ssoUserRepo = Substitute.For<ISsoUserRepository>();
var referenceEventService = Substitute.For<IReferenceEventService>();
var globalSettings = Substitute.For<GlobalSettings>();
var orgService = new OrganizationService(orgRepo, orgUserRepo, collectionRepo, userRepo,
groupRepo, dataProtector, mailService, pushNotService, pushRegService, deviceRepo,
licenseService, eventService, installationRepo, appCacheService, paymentService, policyRepo,
ssoConfigRepo, ssoUserRepo, referenceEventService, 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<OrganizationUserUserDetails>();
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<Models.Business.ImportedOrganizationUser>();
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, false);
await orgUserRepo.DidNotReceive().UpsertAsync(Arg.Any<OrganizationUser>());
await orgUserRepo.Received(2).CreateAsync(Arg.Any<OrganizationUser>());
}
[Fact]
public async Task OrgImportCreateNewUsersAndMarryExistingUser()
{
var orgRepo = Substitute.For<IOrganizationRepository>();
var orgUserRepo = Substitute.For<IOrganizationUserRepository>();
var collectionRepo = Substitute.For<ICollectionRepository>();
var userRepo = Substitute.For<IUserRepository>();
var groupRepo = Substitute.For<IGroupRepository>();
var dataProtector = Substitute.For<IDataProtector>();
var mailService = Substitute.For<IMailService>();
var pushNotService = Substitute.For<IPushNotificationService>();
var pushRegService = Substitute.For<IPushRegistrationService>();
var deviceRepo = Substitute.For<IDeviceRepository>();
var licenseService = Substitute.For<ILicensingService>();
var eventService = Substitute.For<IEventService>();
var installationRepo = Substitute.For<IInstallationRepository>();
var appCacheService = Substitute.For<IApplicationCacheService>();
var paymentService = Substitute.For<IPaymentService>();
var policyRepo = Substitute.For<IPolicyRepository>();
var ssoConfigRepo = Substitute.For<ISsoConfigRepository>();
var ssoUserRepo = Substitute.For<ISsoUserRepository>();
var referenceEventService = Substitute.For<IReferenceEventService>();
var globalSettings = Substitute.For<GlobalSettings>();
var orgService = new OrganizationService(orgRepo, orgUserRepo, collectionRepo, userRepo,
groupRepo, dataProtector, mailService, pushNotService, pushRegService, deviceRepo,
licenseService, eventService, installationRepo, appCacheService, paymentService, policyRepo,
ssoConfigRepo, ssoUserRepo, referenceEventService, 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<OrganizationUserUserDetails>();
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<Models.Business.ImportedOrganizationUser>();
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, false);
await orgUserRepo.Received(1).UpsertAsync(Arg.Any<OrganizationUser>());
await orgUserRepo.Received(2).CreateAsync(Arg.Any<OrganizationUser>());
}
}
}