1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-27 13:05:23 +01:00
bitwarden-server/test/Core.Test/AutoFixture/EntityFrameworkRepositoryFixtures.cs
Thomas Rittson 66629b2f1c
Refactor policy checks (#1536)
* Move policy checking logic inside PolicyService

* Refactor to use currentContext.ManagePolicies

* Make orgUser status check more semantic

* Fix single org user checks

* Use CoreHelper implementation to deserialize json

* Refactor policy checks to use db query

* Use new db query for enforcing 2FA Policy

* Add Policy_ReadByTypeApplicableToUser

* Stub out EF implementations

* Refactor: use PolicyRepository only

* Refactor tests

* Copy SQL queries to proj and update sqlproj file

* Refactor importCiphersAsync to use new method

* Add EF implementations and tests

* Refactor SQL to remove unnecessary operations
2021-09-28 06:54:28 +10:00

124 lines
4.7 KiB
C#

using AutoFixture;
using AutoMapper;
using Bit.Core.Models.EntityFramework;
using Bit.Core.Models.EntityFramework.Provider;
using System.Collections.Generic;
using AutoFixture.Kernel;
using System;
using Moq;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using Bit.Core.Repositories.EntityFramework;
using Bit.Core.Test.Helpers.Factories;
using Microsoft.EntityFrameworkCore;
using Bit.Core.Settings;
namespace Bit.Core.Test.AutoFixture.EntityFrameworkRepositoryFixtures
{
internal class ServiceScopeFactoryBuilder: ISpecimenBuilder
{
private DbContextOptions<DatabaseContext> _options { get; set; }
public ServiceScopeFactoryBuilder(DbContextOptions<DatabaseContext> options) {
_options = options;
}
public object Create(object request, ISpecimenContext context)
{
var fixture = new Fixture();
var serviceProvider = new Mock<IServiceProvider>();
var dbContext = new DatabaseContext(_options);
serviceProvider
.Setup(x => x.GetService(typeof(DatabaseContext)))
.Returns(dbContext);
var serviceScope = new Mock<IServiceScope>();
serviceScope.Setup(x => x.ServiceProvider).Returns(serviceProvider.Object);
var serviceScopeFactory = new Mock<IServiceScopeFactory>();
serviceScopeFactory
.Setup(x => x.CreateScope())
.Returns(serviceScope.Object);
return serviceScopeFactory.Object;
}
}
public class EfRepositoryListBuilder<T>: ISpecimenBuilder where T: BaseEntityFrameworkRepository
{
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var t = request as ParameterInfo;
if (t == null || t.ParameterType != typeof(List<T>))
{
return new NoSpecimen();
}
var list = new List<T>();
foreach (var option in DatabaseOptionsFactory.Options)
{
var fixture = new Fixture();
fixture.Customize<IServiceScopeFactory>(x => x.FromFactory(new ServiceScopeFactoryBuilder(option)));
fixture.Customize<IMapper>(x => x.FromFactory(() =>
new MapperConfiguration(cfg => {
cfg.AddProfile<CipherMapperProfile>();
cfg.AddProfile<CollectionCipherMapperProfile>();
cfg.AddProfile<CollectionMapperProfile>();
cfg.AddProfile<DeviceMapperProfile>();
cfg.AddProfile<EmergencyAccessMapperProfile>();
cfg.AddProfile<EventMapperProfile>();
cfg.AddProfile<FolderMapperProfile>();
cfg.AddProfile<GrantMapperProfile>();
cfg.AddProfile<GroupMapperProfile>();
cfg.AddProfile<GroupUserMapperProfile>();
cfg.AddProfile<InstallationMapperProfile>();
cfg.AddProfile<OrganizationMapperProfile>();
cfg.AddProfile<OrganizationUserMapperProfile>();
cfg.AddProfile<ProviderMapperProfile>();
cfg.AddProfile<ProviderUserMapperProfile>();
cfg.AddProfile<ProviderOrganizationMapperProfile>();
cfg.AddProfile<PolicyMapperProfile>();
cfg.AddProfile<SendMapperProfile>();
cfg.AddProfile<SsoConfigMapperProfile>();
cfg.AddProfile<SsoUserMapperProfile>();
cfg.AddProfile<TaxRateMapperProfile>();
cfg.AddProfile<TransactionMapperProfile>();
cfg.AddProfile<U2fMapperProfile>();
cfg.AddProfile<UserMapperProfile>();
})
.CreateMapper()));
var repo = fixture.Create<T>();
list.Add(repo);
}
return list;
}
}
public class IgnoreVirtualMembersCustomization : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var pi = request as PropertyInfo;
if (pi == null)
{
return new NoSpecimen();
}
if (pi.GetGetMethod().IsVirtual && pi.DeclaringType != typeof(GlobalSettings))
{
return null;
}
return new NoSpecimen();
}
}
}