ApiWebServerFixture : connexion admin Npgsql depuis l'env, plus de mot de passe en source
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 <noreply@anthropic.com>
This commit is contained in:
parent
ed3dc733d3
commit
a17a9cef9a
1 changed files with 19 additions and 13 deletions
|
|
@ -24,7 +24,6 @@ public sealed class ApiWebServerFixture : WebHostFixture
|
||||||
private const string DbProviderEnvVar = "YAVSC_API_TEST_DB_PROVIDER";
|
private const string DbProviderEnvVar = "YAVSC_API_TEST_DB_PROVIDER";
|
||||||
private const string NpgsqlAdminConnectionEnvVar = "YAVSC_API_TEST_NPGSQL_ADMIN_CONNECTION";
|
private const string NpgsqlAdminConnectionEnvVar = "YAVSC_API_TEST_NPGSQL_ADMIN_CONNECTION";
|
||||||
private const string DedicatedNpgsqlDatabaseName = "yavscTestDb";
|
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;
|
protected override int HttpsPort => 5104;
|
||||||
|
|
||||||
|
|
@ -168,25 +167,32 @@ public sealed class ApiWebServerFixture : WebHostFixture
|
||||||
|
|
||||||
private static string BuildAdminConnectionString()
|
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 configured = Environment.GetEnvironmentVariable(NpgsqlAdminConnectionEnvVar);
|
||||||
var source = string.IsNullOrWhiteSpace(configured)
|
if (string.IsNullOrWhiteSpace(configured))
|
||||||
? DefaultDevelopmentConnectionString
|
throw new InvalidOperationException(
|
||||||
: configured;
|
$"{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,
|
Pooling = false,
|
||||||
IncludeErrorDetail = true
|
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";
|
builder.Database = "postgres";
|
||||||
}
|
|
||||||
else if (string.IsNullOrWhiteSpace(builder.Database))
|
|
||||||
{
|
|
||||||
builder.Database = "postgres";
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.ToString();
|
return builder.ToString();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue