using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Heimdall.Application.Services; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using System.Globalization; namespace Heimdall.IntegrationTests.Factories; /// /// WebApplicationFactory for integration tests. /// Requires a PostgreSQL connection string from . /// public class HeimdallWebApplicationFactory : WebApplicationFactory { private readonly string _connectionString; private readonly long? _recordingQuotaMaxBytes; private readonly Action? _configureTestServices; /// Creates a factory connected to a real PostgreSQL instance via Testcontainers. public HeimdallWebApplicationFactory(string connectionString, long? recordingQuotaMaxBytes = null, Action? configureTestServices = null) { _connectionString = connectionString; _recordingQuotaMaxBytes = recordingQuotaMaxBytes; _configureTestServices = configureTestServices; } protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.UseEnvironment("Testing"); builder.UseSetting("Cors:AllowedOrigins", "*"); builder.UseSetting("ConnectionStrings:Default", _connectionString); builder.UseSetting("Database:MigrateOnStartup", "true"); if (_recordingQuotaMaxBytes is long maxBytes) builder.UseSetting("Recordings:StorageQuota:MaxBytes", maxBytes.ToString(CultureInfo.InvariantCulture)); // JWT config builder.UseSetting("Jwt:Key", "ThisIsATestKeyThatIsAtLeast32BytesLong!"); builder.UseSetting("Jwt:RefreshKey", "ThisIsATestRefreshKeyThatIsAlso32Bytes!"); builder.UseSetting("Jwt:Issuer", "heimdall-test"); builder.UseSetting("Jwt:Audience", "heimdall-test"); // Stub HIBP: integration tests must never depend on an external breach-check // API nor on a test password that happens not to be in the HIBP database // (that made the registration command return 400: "password has appeared in a // data breach"). Force "not breached" in tests only — production code path // (HaveIBeenPwnedBreachChecker) is untouched. builder.ConfigureServices(services => { services.RemoveAll(); services.AddScoped(); _configureTestServices?.Invoke(services); }); } } /// Test-only stub: never flags a password as breached. Avoids network + HIBP flakiness. internal sealed class NoopPasswordBreachChecker : IPasswordBreachChecker { public Task IsPasswordBreachedAsync(string password, CancellationToken ct = default) => Task.FromResult(false); }