mirror of
https://github.com/bitwarden/server.git
synced 2025-06-04 02:00:32 -05:00
Dynamically build connection string
This commit is contained in:
parent
e844c9c773
commit
3b52c59cf9
@ -21,7 +21,7 @@ public class ApiApplicationFactory : WebApplicationFactoryBase<Startup>
|
|||||||
public ApiApplicationFactory(ITestDatabase db)
|
public ApiApplicationFactory(ITestDatabase db)
|
||||||
{
|
{
|
||||||
TestDatabase = db;
|
TestDatabase = db;
|
||||||
_handleDbDisposal = true;
|
HandleDbDisposal = true;
|
||||||
|
|
||||||
_identityApplicationFactory = new IdentityApplicationFactory();
|
_identityApplicationFactory = new IdentityApplicationFactory();
|
||||||
_identityApplicationFactory.TestDatabase = TestDatabase;
|
_identityApplicationFactory.TestDatabase = TestDatabase;
|
||||||
|
@ -41,7 +41,7 @@ public abstract class WebApplicationFactoryBase<T> : WebApplicationFactory<T>
|
|||||||
private readonly List<Action<IServiceCollection>> _configureTestServices = new();
|
private readonly List<Action<IServiceCollection>> _configureTestServices = new();
|
||||||
private readonly List<Action<IConfigurationBuilder>> _configureAppConfiguration = new();
|
private readonly List<Action<IConfigurationBuilder>> _configureAppConfiguration = new();
|
||||||
|
|
||||||
private bool _handleDbDisposal { get; set; }
|
public bool HandleDbDisposal { get; set; }
|
||||||
|
|
||||||
|
|
||||||
public void SubstituteService<TService>(Action<TService> mockService)
|
public void SubstituteService<TService>(Action<TService> mockService)
|
||||||
@ -121,7 +121,7 @@ public abstract class WebApplicationFactoryBase<T> : WebApplicationFactory<T>
|
|||||||
if (TestDatabase == null)
|
if (TestDatabase == null)
|
||||||
{
|
{
|
||||||
TestDatabase = new SqliteTestDatabase();
|
TestDatabase = new SqliteTestDatabase();
|
||||||
_handleDbDisposal = true;
|
HandleDbDisposal = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
var config = new Dictionary<string, string?>
|
var config = new Dictionary<string, string?>
|
||||||
@ -185,7 +185,7 @@ public abstract class WebApplicationFactoryBase<T> : WebApplicationFactory<T>
|
|||||||
|
|
||||||
// Add database to the service collection
|
// Add database to the service collection
|
||||||
TestDatabase.AddDatabase(services);
|
TestDatabase.AddDatabase(services);
|
||||||
if (_handleDbDisposal)
|
if (HandleDbDisposal)
|
||||||
{
|
{
|
||||||
TestDatabase.Migrate(services);
|
TestDatabase.Migrate(services);
|
||||||
}
|
}
|
||||||
@ -287,9 +287,9 @@ public abstract class WebApplicationFactoryBase<T> : WebApplicationFactory<T>
|
|||||||
protected override void Dispose(bool disposing)
|
protected override void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
base.Dispose(disposing);
|
base.Dispose(disposing);
|
||||||
if (_handleDbDisposal)
|
if (HandleDbDisposal)
|
||||||
{
|
{
|
||||||
_handleDbDisposal = false;
|
HandleDbDisposal = false;
|
||||||
|
|
||||||
if (TestDatabase != null)
|
if (TestDatabase != null)
|
||||||
{
|
{
|
||||||
|
@ -3,6 +3,7 @@ using Bit.Infrastructure.EntityFramework.Repositories;
|
|||||||
using Bit.Migrator;
|
using Bit.Migrator;
|
||||||
using Microsoft.Data.SqlClient;
|
using Microsoft.Data.SqlClient;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
@ -10,23 +11,35 @@ namespace Bit.IntegrationTestCommon;
|
|||||||
|
|
||||||
public class SqlServerTestDatabase : ITestDatabase
|
public class SqlServerTestDatabase : ITestDatabase
|
||||||
{
|
{
|
||||||
public string SqlServerConnection { get; set; }
|
private string _sqlServerConnection { get; set; }
|
||||||
|
|
||||||
public SqlServerTestDatabase()
|
public SqlServerTestDatabase()
|
||||||
{
|
{
|
||||||
SqlServerConnection = "Server=localhost;Database=vault_test;User Id=SA;Password=SET_A_PASSWORD_HERE_123;Encrypt=True;TrustServerCertificate=True;";
|
// Grab the connection string from the Identity project user secrets
|
||||||
|
var identityBuilder = new ConfigurationBuilder();
|
||||||
|
identityBuilder.AddUserSecrets(typeof(Identity.Startup).Assembly, optional: true);
|
||||||
|
var identityConfig = identityBuilder.Build();
|
||||||
|
var identityConnectionString = identityConfig.GetSection("globalSettings:sqlServer:connectionString").Value;
|
||||||
|
|
||||||
|
// Replace the database name in the connection string to use a test database
|
||||||
|
var testConnectionString = new SqlConnectionStringBuilder(identityConnectionString)
|
||||||
|
{
|
||||||
|
InitialCatalog = "vault_test"
|
||||||
|
}.ConnectionString;
|
||||||
|
|
||||||
|
_sqlServerConnection = testConnectionString;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ModifyGlobalSettings(Dictionary<string, string> config)
|
public void ModifyGlobalSettings(Dictionary<string, string> config)
|
||||||
{
|
{
|
||||||
config["globalSettings:databaseProvider"] = "sqlserver";
|
config["globalSettings:databaseProvider"] = "sqlserver";
|
||||||
config["globalSettings:sqlServer:connectionString"] = SqlServerConnection;
|
config["globalSettings:sqlServer:connectionString"] = _sqlServerConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddDatabase(IServiceCollection serviceCollection)
|
public void AddDatabase(IServiceCollection serviceCollection)
|
||||||
{
|
{
|
||||||
serviceCollection.AddScoped(s => new DbContextOptionsBuilder<DatabaseContext>()
|
serviceCollection.AddScoped(s => new DbContextOptionsBuilder<DatabaseContext>()
|
||||||
.UseSqlServer(SqlServerConnection)
|
.UseSqlServer(_sqlServerConnection)
|
||||||
.UseApplicationServiceProvider(s)
|
.UseApplicationServiceProvider(s)
|
||||||
.Options);
|
.Options);
|
||||||
}
|
}
|
||||||
@ -45,13 +58,13 @@ public class SqlServerTestDatabase : ITestDatabase
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
var masterConnectionString = new SqlConnectionStringBuilder(SqlServerConnection)
|
var masterConnectionString = new SqlConnectionStringBuilder(_sqlServerConnection)
|
||||||
{
|
{
|
||||||
InitialCatalog = "master"
|
InitialCatalog = "master"
|
||||||
}.ConnectionString;
|
}.ConnectionString;
|
||||||
|
|
||||||
using var connection = new SqlConnection(masterConnectionString);
|
using var connection = new SqlConnection(masterConnectionString);
|
||||||
var databaseName = new SqlConnectionStringBuilder(SqlServerConnection).InitialCatalog;
|
var databaseName = new SqlConnectionStringBuilder(_sqlServerConnection).InitialCatalog;
|
||||||
|
|
||||||
connection.Open();
|
connection.Open();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user