mirror of
https://github.com/bitwarden/server.git
synced 2024-11-28 13:15:12 +01:00
f5caecc6d6
* Add SQL script to migrate custom users with specific permissions to User type Remove 'editAssignedCollections' and 'deleteAssignedCollections' properties from Permissions in OrganizationUser table. Migrate custom users who only have these permissions to the User type. * Add MySQL migration to migrate custom users with specific permissions to User type * Add Postgres migration to migrate custom users with specific permissions to User type * Add Sqlite migration to migrate custom users with specific permissions to User type * Update AutoFixture usage in tests to resolve creating ILogger mock instances * Update EF integration tests database contexts to use each respective Migrations assembly. Configure Sqlite instance * Add RunMigration method to BaseEntityFrameworkRepository * Add FinalFlexibleCollectionsDataMigrationsTests * Improve data migration efficiency by using OPENJSON instead of multiple JSON_EXTRACT * Add batching to the sql data migrations * Update DbMigrator to run a specific script based on its name * Update DatabaseDataAttribute to be able to test a specific migration * Add reference to the migration projects to Infrastructure.IntegrationTest * Add integration test to test the migration FinalFlexibleCollectionsDataMigrations * Remove EFIntegration tests and remove RunMigration method from BaseEntityFrameworkRepository * Add IMigrationTesterService and implementations for SQL and EF migrations * Add FinalFlexibleCollectionsDataMigrationsTests and remove test from OrganizationUserRepositoryTests * Update sql data migration script based on performance feedback * Bump date on EF migration scripts * Add xmldoc comments to IMigrationTesterService and each implementation * Bump up the date on the EF migration scripts * Bump up dates on EF migrations * Added tests to assert no unwanted changes are made to the permissions json. Refactor tests. * Revert changes made to DbMigrator and refactor SqlMigrationTesterService to not use it. * Add method description * Fix test to assert no changes are made to custom user * Remove unnecessary COALESCE and SELECT CASE * Unident lines on SQL script * Update DatabaseDataAttribute MigrationName property to be nullable * Fix null reference checks * Remove unnecessary COALESCE from Postgres script * Bump dates on migration scripts * Bump up dates on EF migrations * Add migration tests for handling null * Add test for non json values * Fix test * Remove migrations * Recreate EF migrations * Update Postgres data migration script to check for valid JSON in Permissions column --------- Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Co-authored-by: Thomas Rittson <trittson@bitwarden.com>
126 lines
4.7 KiB
C#
126 lines
4.7 KiB
C#
using System.Reflection;
|
|
using AutoFixture;
|
|
using AutoFixture.Kernel;
|
|
using AutoMapper;
|
|
using Bit.Core.Settings;
|
|
using Bit.Infrastructure.EFIntegration.Test.Helpers;
|
|
using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
|
|
using Bit.Infrastructure.EntityFramework.AdminConsole.Models.Provider;
|
|
using Bit.Infrastructure.EntityFramework.Auth.Models;
|
|
using Bit.Infrastructure.EntityFramework.Models;
|
|
using Bit.Infrastructure.EntityFramework.Repositories;
|
|
using Bit.Infrastructure.EntityFramework.Vault.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
|
|
namespace Bit.Infrastructure.EFIntegration.Test.AutoFixture;
|
|
|
|
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 = Substitute.For<IServiceProvider>();
|
|
var dbContext = new DatabaseContext(_options);
|
|
serviceProvider.GetService(typeof(DatabaseContext)).Returns(dbContext);
|
|
|
|
var serviceScope = Substitute.For<IServiceScope>();
|
|
serviceScope.ServiceProvider.Returns(serviceProvider);
|
|
|
|
var serviceScopeFactory = Substitute.For<IServiceScopeFactory>();
|
|
serviceScopeFactory.CreateScope().Returns(serviceScope);
|
|
return serviceScopeFactory;
|
|
}
|
|
}
|
|
|
|
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<AuthRequestMapperProfile>();
|
|
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<OrganizationSponsorshipMapperProfile>();
|
|
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<UserMapperProfile>();
|
|
})
|
|
.CreateMapper()));
|
|
|
|
fixture.Customize<ILogger<T>>(x => x.FromFactory(() => Substitute.For<ILogger<T>>()));
|
|
|
|
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();
|
|
}
|
|
}
|