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;
|
2024-07-03 18:48:23 +02:00
|
|
|
|
using Microsoft.Extensions.Caching.Distributed;
|
2022-12-02 20:24:30 +01:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-07-03 18:48:23 +02:00
|
|
|
|
using Microsoft.Extensions.Time.Testing;
|
2022-12-02 20:24:30 +01:00
|
|
|
|
using Xunit.Sdk;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Infrastructure.IntegrationTest;
|
|
|
|
|
|
|
|
|
|
public class DatabaseDataAttribute : DataAttribute
|
|
|
|
|
{
|
|
|
|
|
public bool SelfHosted { get; set; }
|
2024-07-03 18:48:23 +02:00
|
|
|
|
public bool UseFakeTimeProvider { get; set; }
|
2022-12-02 20:24:30 +01:00
|
|
|
|
|
|
|
|
|
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();
|
2024-07-03 18:48:23 +02:00
|
|
|
|
AddCommonServices(dapperSqlServerCollection, configureLogging);
|
2023-05-30 19:25:55 +02:00
|
|
|
|
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);
|
2024-07-03 18:48:23 +02:00
|
|
|
|
dapperSqlServerCollection.AddDistributedSqlServerCache((o) =>
|
|
|
|
|
{
|
|
|
|
|
o.ConnectionString = database.ConnectionString;
|
|
|
|
|
o.SchemaName = "dbo";
|
|
|
|
|
o.TableName = "Cache";
|
|
|
|
|
});
|
2023-05-30 19:25:55 +02:00
|
|
|
|
yield return dapperSqlServerCollection.BuildServiceProvider();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var efCollection = new ServiceCollection();
|
2024-07-03 18:48:23 +02:00
|
|
|
|
AddCommonServices(efCollection, configureLogging);
|
2023-05-30 19:25:55 +02:00
|
|
|
|
efCollection.SetupEntityFramework(database.ConnectionString, database.Type);
|
|
|
|
|
efCollection.AddPasswordManagerEFRepositories(SelfHosted);
|
2023-10-12 11:15:02 +02:00
|
|
|
|
efCollection.AddSingleton(database);
|
2024-07-03 18:48:23 +02:00
|
|
|
|
efCollection.AddSingleton<IDistributedCache, EntityFrameworkCache>();
|
2023-05-30 19:25:55 +02:00
|
|
|
|
yield return efCollection.BuildServiceProvider();
|
|
|
|
|
}
|
2022-12-02 20:24:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-03 18:48:23 +02:00
|
|
|
|
|
|
|
|
|
private void AddCommonServices(IServiceCollection services, Action<ILoggingBuilder> configureLogging)
|
|
|
|
|
{
|
|
|
|
|
services.AddLogging(configureLogging);
|
|
|
|
|
services.AddDataProtection();
|
|
|
|
|
|
|
|
|
|
if (UseFakeTimeProvider)
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<TimeProvider, FakeTimeProvider>();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-02 20:24:30 +01:00
|
|
|
|
}
|