1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-28 13:15:12 +01:00
bitwarden-server/test/Infrastructure.EFIntegration.Test/Helpers/DatabaseOptionsFactory.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
2.2 KiB
C#
Raw Normal View History

using System.Text;
using Bit.Core;
using Bit.Core.Test.Helpers.Factories;
using Bit.Infrastructure.EntityFramework.Repositories;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
2023-08-10 17:03:42 +02:00
using NSubstitute;
namespace Bit.Infrastructure.EFIntegration.Test.Helpers;
2022-08-29 22:06:55 +02:00
public static class DatabaseOptionsFactory
{
public static List<DbContextOptions<DatabaseContext>> Options { get; } = new();
static DatabaseOptionsFactory()
2022-08-29 22:06:55 +02: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-08-10 17:03:42 +02:00
var dataProtectionProvider = Substitute.For<IDataProtectionProvider>();
dataProtectionProvider.CreateProtector(Constants.DatabaseFieldProtectorPurpose)
.Returns(dataProtector);
2023-08-10 17:03:42 +02:00
return dataProtectionProvider;
})
.BuildServiceProvider();
var globalSettings = GlobalSettingsFactory.GlobalSettings;
if (!string.IsNullOrWhiteSpace(GlobalSettingsFactory.GlobalSettings.PostgreSql?.ConnectionString))
2022-08-29 22:06:55 +02:00
{
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
Options.Add(new DbContextOptionsBuilder<DatabaseContext>()
.UseNpgsql(globalSettings.PostgreSql.ConnectionString)
.UseApplicationServiceProvider(services)
.Options);
2022-08-29 22:06:55 +02:00
}
if (!string.IsNullOrWhiteSpace(GlobalSettingsFactory.GlobalSettings.MySql?.ConnectionString))
{
var mySqlConnectionString = globalSettings.MySql.ConnectionString;
Options.Add(new DbContextOptionsBuilder<DatabaseContext>()
.UseMySql(mySqlConnectionString, ServerVersion.AutoDetect(mySqlConnectionString))
.UseApplicationServiceProvider(services)
.Options);
}
}
}