1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-25 12:45:18 +01:00
bitwarden-server/test/Infrastructure.EFIntegration.Test/Auth/Repositories/SsoUserRepositoryTests.cs
Jake Fink 88dd745070
[PM-1188] Server owner auth migration (#2825)
* [PM-1188] add sso project to auth

* [PM-1188] move sso api models to auth

* [PM-1188] fix sso api model namespace & imports

* [PM-1188] move core files to auth

* [PM-1188] fix core sso namespace & models

* [PM-1188] move sso repository files to auth

* [PM-1188] fix sso repo files namespace & imports

* [PM-1188] move sso sql files to auth folder

* [PM-1188] move sso test files to auth folders

* [PM-1188] fix sso tests namespace & imports

* [PM-1188] move auth api files to auth folder

* [PM-1188] fix auth api files namespace & imports

* [PM-1188] move auth core files to auth folder

* [PM-1188] fix auth core files namespace & imports

* [PM-1188] move auth email templates to auth folder

* [PM-1188] move auth email folder back into shared directory

* [PM-1188] fix auth email names

* [PM-1188] move auth core models to auth folder

* [PM-1188] fix auth model namespace & imports

* [PM-1188] add entire Identity project to auth codeowners

* [PM-1188] fix auth orm files namespace & imports

* [PM-1188] move auth orm files to auth folder

* [PM-1188] move auth sql files to auth folder

* [PM-1188] move auth tests to auth folder

* [PM-1188] fix auth test files namespace & imports

* [PM-1188] move emergency access api files to auth folder

* [PM-1188] fix emergencyaccess api files namespace & imports

* [PM-1188] move emergency access core files to auth folder

* [PM-1188] fix emergency access core files namespace & imports

* [PM-1188] move emergency access orm files to auth folder

* [PM-1188] fix emergency access orm files namespace & imports

* [PM-1188] move emergency access sql files to auth folder

* [PM-1188] move emergencyaccess test files to auth folder

* [PM-1188] fix emergency access test files namespace & imports

* [PM-1188] move captcha files to auth folder

* [PM-1188] fix captcha files namespace & imports

* [PM-1188] move auth admin files into auth folder

* [PM-1188] fix admin auth files namespace & imports
- configure mvc to look in auth folders for views

* [PM-1188] remove extra imports and formatting

* [PM-1188] fix ef auth model imports

* [PM-1188] fix DatabaseContextModelSnapshot paths

* [PM-1188] fix grant import in ef

* [PM-1188] update sqlproj

* [PM-1188] move missed sqlproj files

* [PM-1188] move auth ef models out of auth folder

* [PM-1188] fix auth ef models namespace

* [PM-1188] remove auth ef models unused imports

* [PM-1188] fix imports for auth ef models

* [PM-1188] fix more ef model imports

* [PM-1188] fix file encodings
2023-04-14 13:25:56 -04:00

189 lines
7.9 KiB
C#

using Bit.Core.Auth.Entities;
using Bit.Core.Entities;
using Bit.Core.Test.AutoFixture.Attributes;
using Bit.Infrastructure.EFIntegration.Test.Auth.AutoFixture;
using Bit.Infrastructure.EFIntegration.Test.Auth.Repositories.EqualityComparers;
using Xunit;
using EfRepo = Bit.Infrastructure.EntityFramework.Repositories;
using SqlAuthRepo = Bit.Infrastructure.Dapper.Auth.Repositories;
using SqlRepo = Bit.Infrastructure.Dapper.Repositories;
namespace Bit.Infrastructure.EFIntegration.Test.Auth.Repositories;
public class SsoUserRepositoryTests
{
[CiSkippedTheory, EfSsoUserAutoData]
public async void CreateAsync_Works_DataMatches(SsoUser ssoUser, User user, Organization org,
SsoUserCompare equalityComparer, List<EfRepo.SsoUserRepository> suts,
List<EfRepo.OrganizationRepository> efOrgRepos, List<EfRepo.UserRepository> efUserRepos,
SqlAuthRepo.SsoUserRepository sqlSsoUserRepo, SqlRepo.OrganizationRepository sqlOrgRepo,
SqlRepo.UserRepository sqlUserRepo)
{
var createdSsoUsers = new List<SsoUser>();
foreach (var sut in suts)
{
var i = suts.IndexOf(sut);
var efUser = await efUserRepos[i].CreateAsync(user);
var efOrg = await efOrgRepos[i].CreateAsync(org);
sut.ClearChangeTracking();
ssoUser.UserId = efUser.Id;
ssoUser.OrganizationId = efOrg.Id;
var postEfSsoUser = await sut.CreateAsync(ssoUser);
sut.ClearChangeTracking();
var savedSsoUser = await sut.GetByIdAsync(ssoUser.Id);
createdSsoUsers.Add(savedSsoUser);
}
var sqlUser = await sqlUserRepo.CreateAsync(user);
var sqlOrganization = await sqlOrgRepo.CreateAsync(org);
ssoUser.UserId = sqlUser.Id;
ssoUser.OrganizationId = sqlOrganization.Id;
var sqlSsoUser = await sqlSsoUserRepo.CreateAsync(ssoUser);
createdSsoUsers.Add(await sqlSsoUserRepo.GetByIdAsync(sqlSsoUser.Id));
var distinctSsoUsers = createdSsoUsers.Distinct(equalityComparer);
Assert.True(!distinctSsoUsers.Skip(1).Any());
}
[CiSkippedTheory, EfSsoUserAutoData]
public async void ReplaceAsync_Works_DataMatches(SsoUser postSsoUser, SsoUser replaceSsoUser,
Organization org, User user, SsoUserCompare equalityComparer,
List<EfRepo.SsoUserRepository> suts, List<EfRepo.UserRepository> efUserRepos,
List<EfRepo.OrganizationRepository> efOrgRepos, SqlAuthRepo.SsoUserRepository sqlSsoUserRepo,
SqlRepo.OrganizationRepository sqlOrgRepo, SqlRepo.UserRepository sqlUserRepo)
{
var savedSsoUsers = new List<SsoUser>();
foreach (var sut in suts)
{
var i = suts.IndexOf(sut);
var efUser = await efUserRepos[i].CreateAsync(user);
var efOrg = await efOrgRepos[i].CreateAsync(org);
sut.ClearChangeTracking();
postSsoUser.UserId = efUser.Id;
postSsoUser.OrganizationId = efOrg.Id;
var postEfSsoUser = await sut.CreateAsync(postSsoUser);
sut.ClearChangeTracking();
replaceSsoUser.Id = postEfSsoUser.Id;
replaceSsoUser.UserId = postEfSsoUser.UserId;
replaceSsoUser.OrganizationId = postEfSsoUser.OrganizationId;
await sut.ReplaceAsync(replaceSsoUser);
sut.ClearChangeTracking();
var replacedSsoUser = await sut.GetByIdAsync(replaceSsoUser.Id);
savedSsoUsers.Add(replacedSsoUser);
}
var sqlUser = await sqlUserRepo.CreateAsync(user);
var sqlOrganization = await sqlOrgRepo.CreateAsync(org);
postSsoUser.UserId = sqlUser.Id;
postSsoUser.OrganizationId = sqlOrganization.Id;
var postSqlSsoUser = await sqlSsoUserRepo.CreateAsync(postSsoUser);
replaceSsoUser.Id = postSqlSsoUser.Id;
replaceSsoUser.UserId = postSqlSsoUser.UserId;
replaceSsoUser.OrganizationId = postSqlSsoUser.OrganizationId;
await sqlSsoUserRepo.ReplaceAsync(replaceSsoUser);
savedSsoUsers.Add(await sqlSsoUserRepo.GetByIdAsync(replaceSsoUser.Id));
var distinctItems = savedSsoUsers.Distinct(equalityComparer);
Assert.True(!distinctItems.Skip(1).Any());
}
[CiSkippedTheory, EfSsoUserAutoData]
public async void DeleteAsync_Works_DataMatches(SsoUser ssoUser, Organization org, User user, List<EfRepo.SsoUserRepository> suts,
List<EfRepo.UserRepository> efUserRepos, List<EfRepo.OrganizationRepository> efOrgRepos,
SqlAuthRepo.SsoUserRepository sqlSsoUserRepo, SqlRepo.UserRepository sqlUserRepo,
SqlRepo.OrganizationRepository sqlOrganizationRepo)
{
foreach (var sut in suts)
{
var i = suts.IndexOf(sut);
var savedEfUser = await efUserRepos[i].CreateAsync(user);
var savedEfOrg = await efOrgRepos[i].CreateAsync(org);
sut.ClearChangeTracking();
ssoUser.UserId = savedEfUser.Id;
ssoUser.OrganizationId = savedEfOrg.Id;
var postEfSsoUser = await sut.CreateAsync(ssoUser);
sut.ClearChangeTracking();
var savedEfSsoUser = await sut.GetByIdAsync(postEfSsoUser.Id);
Assert.True(savedEfSsoUser != null);
sut.ClearChangeTracking();
await sut.DeleteAsync(savedEfSsoUser);
savedEfSsoUser = await sut.GetByIdAsync(savedEfSsoUser.Id);
Assert.True(savedEfSsoUser == null);
}
var sqlUser = await sqlUserRepo.CreateAsync(user);
var sqlOrganization = await sqlOrganizationRepo.CreateAsync(org);
ssoUser.UserId = sqlUser.Id;
ssoUser.OrganizationId = sqlOrganization.Id;
var postSqlSsoUser = await sqlSsoUserRepo.CreateAsync(ssoUser);
var savedSqlSsoUser = await sqlSsoUserRepo.GetByIdAsync(postSqlSsoUser.Id);
Assert.True(savedSqlSsoUser != null);
await sqlSsoUserRepo.DeleteAsync(savedSqlSsoUser);
savedSqlSsoUser = await sqlSsoUserRepo.GetByIdAsync(postSqlSsoUser.Id);
Assert.True(savedSqlSsoUser == null);
}
[CiSkippedTheory, EfSsoUserAutoData]
public async void DeleteAsync_UserIdOrganizationId_Works_DataMatches(SsoUser ssoUser,
User user, Organization org, List<EfRepo.SsoUserRepository> suts,
List<EfRepo.UserRepository> efUserRepos, List<EfRepo.OrganizationRepository> efOrgRepos,
SqlAuthRepo.SsoUserRepository sqlSsoUserRepo, SqlRepo.UserRepository sqlUserRepo, SqlRepo.OrganizationRepository sqlOrgRepo
)
{
foreach (var sut in suts)
{
var i = suts.IndexOf(sut);
var savedEfUser = await efUserRepos[i].CreateAsync(user);
var savedEfOrg = await efOrgRepos[i].CreateAsync(org);
sut.ClearChangeTracking();
ssoUser.UserId = savedEfUser.Id;
ssoUser.OrganizationId = savedEfOrg.Id;
var postEfSsoUser = await sut.CreateAsync(ssoUser);
sut.ClearChangeTracking();
var savedEfSsoUser = await sut.GetByIdAsync(postEfSsoUser.Id);
Assert.True(savedEfSsoUser != null);
sut.ClearChangeTracking();
await sut.DeleteAsync(savedEfSsoUser.UserId, savedEfSsoUser.OrganizationId);
sut.ClearChangeTracking();
savedEfSsoUser = await sut.GetByIdAsync(savedEfSsoUser.Id);
Assert.True(savedEfSsoUser == null);
}
var sqlUser = await sqlUserRepo.CreateAsync(user);
var sqlOrganization = await sqlOrgRepo.CreateAsync(org);
ssoUser.UserId = sqlUser.Id;
ssoUser.OrganizationId = sqlOrganization.Id;
var postSqlSsoUser = await sqlSsoUserRepo.CreateAsync(ssoUser);
var savedSqlSsoUser = await sqlSsoUserRepo.GetByIdAsync(postSqlSsoUser.Id);
Assert.True(savedSqlSsoUser != null);
await sqlSsoUserRepo.DeleteAsync(savedSqlSsoUser.UserId, savedSqlSsoUser.OrganizationId);
savedSqlSsoUser = await sqlSsoUserRepo.GetByIdAsync(postSqlSsoUser.Id);
Assert.True(savedSqlSsoUser == null);
}
}