2022-12-02 20:24:30 +01:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Settings;
|
|
|
|
|
using Bit.Infrastructure.Dapper;
|
2023-03-30 15:37:19 +02:00
|
|
|
|
using Bit.Infrastructure.EntityFramework;
|
2022-12-02 20:24:30 +01:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Xunit.Sdk;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Infrastructure.IntegrationTest;
|
|
|
|
|
|
|
|
|
|
public class DatabaseDataAttribute : DataAttribute
|
|
|
|
|
{
|
|
|
|
|
public bool SelfHosted { get; set; }
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
|
|
|
|
|
{
|
|
|
|
|
var parameters = testMethod.GetParameters();
|
|
|
|
|
|
|
|
|
|
var config = DatabaseTheoryAttribute.GetConfiguration();
|
|
|
|
|
|
|
|
|
|
var serviceProviders = GetDatabaseProviders(config);
|
|
|
|
|
|
|
|
|
|
foreach (var provider in serviceProviders)
|
|
|
|
|
{
|
|
|
|
|
var objects = new object[parameters.Length];
|
|
|
|
|
for (var i = 0; i < parameters.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
objects[i] = provider.GetRequiredService(parameters[i].ParameterType);
|
|
|
|
|
}
|
|
|
|
|
yield return objects;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 11:40:29 +01:00
|
|
|
|
protected virtual IEnumerable<IServiceProvider> GetDatabaseProviders(IConfiguration config)
|
2022-12-02 20:24:30 +01:00
|
|
|
|
{
|
|
|
|
|
var configureLogging = (ILoggingBuilder builder) =>
|
|
|
|
|
{
|
|
|
|
|
if (!config.GetValue<bool>("Quiet"))
|
|
|
|
|
{
|
|
|
|
|
builder.AddConfiguration(config);
|
|
|
|
|
builder.AddConsole();
|
|
|
|
|
builder.AddDebug();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-30 19:25:55 +02:00
|
|
|
|
var databases = config.GetDatabases();
|
|
|
|
|
|
|
|
|
|
foreach (var database in databases)
|
2022-12-02 20:24:30 +01:00
|
|
|
|
{
|
2023-05-30 19:25:55 +02:00
|
|
|
|
if (database.Type == SupportedDatabaseProviders.SqlServer && !database.UseEf)
|
2022-12-02 20:24:30 +01:00
|
|
|
|
{
|
2023-05-30 19:25:55 +02:00
|
|
|
|
var dapperSqlServerCollection = new ServiceCollection();
|
|
|
|
|
dapperSqlServerCollection.AddLogging(configureLogging);
|
|
|
|
|
dapperSqlServerCollection.AddDapperRepositories(SelfHosted);
|
|
|
|
|
var globalSettings = new GlobalSettings
|
2022-12-02 20:24:30 +01:00
|
|
|
|
{
|
2023-05-30 19:25:55 +02:00
|
|
|
|
DatabaseProvider = "sqlServer",
|
|
|
|
|
SqlServer = new GlobalSettings.SqlSettings
|
|
|
|
|
{
|
|
|
|
|
ConnectionString = database.ConnectionString,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
dapperSqlServerCollection.AddSingleton(globalSettings);
|
|
|
|
|
dapperSqlServerCollection.AddSingleton<IGlobalSettings>(globalSettings);
|
2023-10-12 11:15:02 +02:00
|
|
|
|
dapperSqlServerCollection.AddSingleton(database);
|
2023-05-30 19:25:55 +02:00
|
|
|
|
dapperSqlServerCollection.AddDataProtection();
|
|
|
|
|
yield return dapperSqlServerCollection.BuildServiceProvider();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var efCollection = new ServiceCollection();
|
|
|
|
|
efCollection.AddLogging(configureLogging);
|
|
|
|
|
efCollection.SetupEntityFramework(database.ConnectionString, database.Type);
|
|
|
|
|
efCollection.AddPasswordManagerEFRepositories(SelfHosted);
|
2023-10-12 11:15:02 +02:00
|
|
|
|
efCollection.AddSingleton(database);
|
2023-05-30 19:25:55 +02:00
|
|
|
|
efCollection.AddDataProtection();
|
|
|
|
|
yield return efCollection.BuildServiceProvider();
|
|
|
|
|
}
|
2022-12-02 20:24:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|