diff --git a/src/Heimdall.Infrastructure/DependencyInjection.cs b/src/Heimdall.Infrastructure/DependencyInjection.cs index 1ece446..871edb3 100644 --- a/src/Heimdall.Infrastructure/DependencyInjection.cs +++ b/src/Heimdall.Infrastructure/DependencyInjection.cs @@ -20,16 +20,14 @@ public static class DependencyInjection { public static IServiceCollection AddInfrastructure( this IServiceCollection services, - IConfiguration configuration, - string? environmentName = null) + IConfiguration configuration) { - var env = environmentName ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"; - var isTesting = env == "Testing"; + var dbProvider = configuration.GetValue("Database:Provider") ?? "PostgreSQL"; // EF Core interceptor (registered as singleton — it is stateless) services.AddSingleton(); - if (isTesting) + if (dbProvider == "InMemory") { var dbName = configuration.GetValue("InMemoryDbName") ?? "HeimdallTestDb"; services.AddDbContext(options => @@ -66,7 +64,8 @@ public static class DependencyInjection .AddDefaultTokenProviders(); // Data Protection (for camera credential encryption) - if (isTesting) + var dpMode = configuration.GetValue("DataProtection:Mode") ?? "Persisted"; + if (dpMode == "Ephemeral") { // Tests: use ephemeral keys (no persistence, no certificate encryption) services.AddDataProtection() diff --git a/src/Heimdall.Web/Program.cs b/src/Heimdall.Web/Program.cs index 822ad96..044e2db 100644 --- a/src/Heimdall.Web/Program.cs +++ b/src/Heimdall.Web/Program.cs @@ -175,11 +175,11 @@ builder.Services.AddOpenApi(); builder.Services.AddApplication(); // ── Infrastructure layer ────────────────────────────────────────── -builder.Services.AddInfrastructure(builder.Configuration, - builder.Environment.EnvironmentName); +builder.Services.AddInfrastructure(builder.Configuration); // ── Database connection string (for Hangfire) ───────────────────── -if (!builder.Environment.IsEnvironment("Testing")) +var hangfireEnabled = builder.Configuration.GetValue("Hangfire:Enabled"); +if (hangfireEnabled) { var connectionString = Environment.GetEnvironmentVariable("DATABASE_URL") ?? builder.Configuration.GetConnectionString("Default") @@ -388,8 +388,9 @@ app.MapPost("/_auth/signout", async (HttpContext httpContext, ILogger l // ── Apply pending database migrations ──────────────────────────────── // Retry with exponential backoff to handle container startup races where // PostgreSQL accepts TCP connections but isn't ready to serve queries yet. -// Skipped in Testing — integration tests use InMemory database. -if (!app.Environment.IsEnvironment("Testing")) +// Controlled by Database:MigrateOnStartup config (false in Testing). +var migrateOnStartup = builder.Configuration.GetValue("Database:MigrateOnStartup"); +if (migrateOnStartup) { using (var scope = app.Services.CreateScope()) { diff --git a/src/Heimdall.Web/appsettings.Testing.json b/src/Heimdall.Web/appsettings.Testing.json new file mode 100644 index 0000000..725a975 --- /dev/null +++ b/src/Heimdall.Web/appsettings.Testing.json @@ -0,0 +1,12 @@ +{ + "Database": { + "Provider": "InMemory", + "MigrateOnStartup": false + }, + "DataProtection": { + "Mode": "Ephemeral" + }, + "Hangfire": { + "Enabled": false + } +}