From a17a9cef9acf018a94422543ee0c5bd95bf31e9c Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Fri, 25 Sep 2026 15:11:00 +0100 Subject: [PATCH] ApiWebServerFixture : connexion admin Npgsql depuis l'env, plus de mot de passe en source MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Applique le même traitement que ServerServicesFixture : la chaîne de connexion admin PostgreSQL se lit depuis YAVSC_API_TEST_NPGSQL_ADMIN_CONNECTION au lieu d'être codée en dur (mot de passe dev 8*5idas retiré). Sans cette variable, le chemin Npgsql échoue avec un message explicite plutôt que de retomber silencieusement sur la base de dev locale. Co-Authored-By: Claude Code --- .../Fixtures/ApiWebServerFixture.cs | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Yavsc.Api.Test/Fixtures/ApiWebServerFixture.cs b/src/Yavsc.Api.Test/Fixtures/ApiWebServerFixture.cs index 51e939ceb..5d040fd27 100644 --- a/src/Yavsc.Api.Test/Fixtures/ApiWebServerFixture.cs +++ b/src/Yavsc.Api.Test/Fixtures/ApiWebServerFixture.cs @@ -24,7 +24,6 @@ public sealed class ApiWebServerFixture : WebHostFixture private const string DbProviderEnvVar = "YAVSC_API_TEST_DB_PROVIDER"; private const string NpgsqlAdminConnectionEnvVar = "YAVSC_API_TEST_NPGSQL_ADMIN_CONNECTION"; private const string DedicatedNpgsqlDatabaseName = "yavscTestDb"; - private const string DefaultDevelopmentConnectionString = "Server=localhost;Port=5432;Database=yavscdev;Username=yavscdev;Password=8*5idas;Include Error Detail=true"; protected override int HttpsPort => 5104; @@ -168,25 +167,32 @@ public sealed class ApiWebServerFixture : WebHostFixture private static string BuildAdminConnectionString() { + // The admin connection string (host + credentials) is supplied + // via environment, never embedded in source — committing a + // real dev password would leak it into git history. The Npgsql + // path is opt-in, so requiring the env var is fine: a missing + // value produces a clear failure rather than a silent fallback + // onto someone's local dev database. var configured = Environment.GetEnvironmentVariable(NpgsqlAdminConnectionEnvVar); - var source = string.IsNullOrWhiteSpace(configured) - ? DefaultDevelopmentConnectionString - : configured; + if (string.IsNullOrWhiteSpace(configured)) + throw new InvalidOperationException( + $"{NpgsqlAdminConnectionEnvVar} is not set. The Npgsql test " + + "provider requires an admin PostgreSQL connection string " + + "in that environment variable (e.g. " + + "'Server=localhost;Port=5432;Database=postgres;" + + "Username=yavscdev;Password=...;Include Error Detail=true')."); - var builder = new NpgsqlConnectionStringBuilder(source) + var builder = new NpgsqlConnectionStringBuilder(configured) { Pooling = false, IncludeErrorDetail = true }; - if (string.IsNullOrWhiteSpace(configured)) - { + // The admin connection targets the maintenance database + // (postgres) so we can CREATE/DROP the dedicated test database; + // whatever database the env var names is overridden here. + if (string.IsNullOrWhiteSpace(builder.Database)) builder.Database = "postgres"; - } - else if (string.IsNullOrWhiteSpace(builder.Database)) - { - builder.Database = "postgres"; - } return builder.ToString(); }