2023-01-18 19:16:57 +01:00
|
|
|
|
using System.Text;
|
|
|
|
|
using Bit.Core;
|
|
|
|
|
using Bit.Core.Test.Helpers.Factories;
|
2022-08-29 15:40:59 +02:00
|
|
|
|
using Bit.Infrastructure.EntityFramework.Repositories;
|
2023-01-18 19:16:57 +01:00
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
2022-08-29 15:40:59 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-01-18 19:16:57 +01:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-08-10 17:03:42 +02:00
|
|
|
|
using NSubstitute;
|
2022-08-29 15:40:59 +02:00
|
|
|
|
|
|
|
|
|
namespace Bit.Infrastructure.EFIntegration.Test.Helpers;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-08-29 15:40:59 +02:00
|
|
|
|
public static class DatabaseOptionsFactory
|
|
|
|
|
{
|
|
|
|
|
public static List<DbContextOptions<DatabaseContext>> Options { get; } = new();
|
2022-08-29 21:53:48 +02:00
|
|
|
|
|
2022-08-29 15:40:59 +02:00
|
|
|
|
static DatabaseOptionsFactory()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2023-01-18 19:16:57 +01:00
|
|
|
|
var services = new ServiceCollection()
|
|
|
|
|
.AddSingleton(sp =>
|
|
|
|
|
{
|
2023-08-10 17:03:42 +02:00
|
|
|
|
var dataProtector = Substitute.For<IDataProtector>();
|
|
|
|
|
dataProtector.Unprotect(Arg.Any<byte[]>())
|
|
|
|
|
.Returns<byte[]>(data =>
|
|
|
|
|
Encoding.UTF8.GetBytes(Constants.DatabaseFieldProtectedPrefix +
|
|
|
|
|
Encoding.UTF8.GetString((byte[])data[0])));
|
2023-01-18 19:16:57 +01:00
|
|
|
|
|
2023-08-10 17:03:42 +02:00
|
|
|
|
var dataProtectionProvider = Substitute.For<IDataProtectionProvider>();
|
|
|
|
|
dataProtectionProvider.CreateProtector(Constants.DatabaseFieldProtectorPurpose)
|
|
|
|
|
.Returns(dataProtector);
|
2023-01-18 19:16:57 +01:00
|
|
|
|
|
2023-08-10 17:03:42 +02:00
|
|
|
|
return dataProtectionProvider;
|
2023-01-18 19:16:57 +01:00
|
|
|
|
})
|
|
|
|
|
.BuildServiceProvider();
|
|
|
|
|
|
2022-08-29 15:40:59 +02:00
|
|
|
|
var globalSettings = GlobalSettingsFactory.GlobalSettings;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(GlobalSettingsFactory.GlobalSettings.PostgreSql?.ConnectionString))
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-08-29 15:40:59 +02:00
|
|
|
|
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
2023-01-18 19:16:57 +01:00
|
|
|
|
Options.Add(new DbContextOptionsBuilder<DatabaseContext>()
|
2024-09-02 12:04:55 +02:00
|
|
|
|
.UseNpgsql(globalSettings.PostgreSql.ConnectionString, npgsqlDbContextOptionsBuilder =>
|
|
|
|
|
{
|
|
|
|
|
npgsqlDbContextOptionsBuilder.MigrationsAssembly(nameof(PostgresMigrations));
|
|
|
|
|
})
|
2023-01-18 19:16:57 +01:00
|
|
|
|
.UseApplicationServiceProvider(services)
|
|
|
|
|
.Options);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-08-29 15:40:59 +02:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(GlobalSettingsFactory.GlobalSettings.MySql?.ConnectionString))
|
|
|
|
|
{
|
|
|
|
|
var mySqlConnectionString = globalSettings.MySql.ConnectionString;
|
2023-01-18 19:16:57 +01:00
|
|
|
|
Options.Add(new DbContextOptionsBuilder<DatabaseContext>()
|
2024-09-02 12:04:55 +02:00
|
|
|
|
.UseMySql(mySqlConnectionString, ServerVersion.AutoDetect(mySqlConnectionString), mySqlDbContextOptionsBuilder =>
|
|
|
|
|
{
|
|
|
|
|
mySqlDbContextOptionsBuilder.MigrationsAssembly(nameof(MySqlMigrations));
|
|
|
|
|
})
|
|
|
|
|
.UseApplicationServiceProvider(services)
|
|
|
|
|
.Options);
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(GlobalSettingsFactory.GlobalSettings.Sqlite?.ConnectionString))
|
|
|
|
|
{
|
|
|
|
|
var sqliteConnectionString = globalSettings.Sqlite.ConnectionString;
|
|
|
|
|
Options.Add(new DbContextOptionsBuilder<DatabaseContext>()
|
|
|
|
|
.UseSqlite(sqliteConnectionString, mySqlDbContextOptionsBuilder =>
|
|
|
|
|
{
|
|
|
|
|
mySqlDbContextOptionsBuilder.MigrationsAssembly(nameof(SqliteMigrations));
|
|
|
|
|
})
|
2023-01-18 19:16:57 +01:00
|
|
|
|
.UseApplicationServiceProvider(services)
|
|
|
|
|
.Options);
|
2022-08-29 15:40:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|