test(host): supply Central test ApiKeyPepper so StartupValidator preflight passes (fix pre-existing 1fcc4f5 red); lock pepper-required behavior
Commit1fcc4f5added a Central-only Require for ScadaBridge:InboundApi:ApiKeyPepper (>=16 chars) to StartupValidator. That Require fires in Program.cs before WebApplicationFactory can apply any WithWebHostBuilder config overlays, so it must be satisfied via environment variables (which ARE in the pre-host AddEnvironmentVariables() pass). Fix (test-only, no src/ changes): - CentralDbTestEnvironment: add ScadaBridge__InboundApi__ApiKeyPepper env var (TestPepper constant, 23 chars) alongside the existing db connection string; restore on Dispose. Fixes HealthCheckTests, MetricsEndpointTests, and HostStartupTests.CentralRole_StartsWithoutError which all use CentralDbTestEnvironment. - CentralActorPathTests.InitializeAsync: set the pepper env var before WebApplicationFactory is constructed (the class uses IAsyncLifetime directly, not CentralDbTestEnvironment). - CentralCompositionRootTests ctor + Dispose: same env-var pattern; those tests already had the pepper in AddInMemoryCollection (DI-layer only, too late for pre-host validation). - CentralAuditWiringTests ctor + Dispose: same env-var pattern for the same reason. - StartupValidatorTests.ValidCentralConfig(): add pepper so the unit tests that call StartupValidator.Validate() directly with a Central config stop failing. - Add guard tests: Central_MissingApiKeyPepper_FailsValidation, Central_ShortApiKeyPepper_FailsValidation, Site_ApiKeyPepper_NotRequired — these lock the production behavior introduced by1fcc4f5.
This commit is contained in:
@@ -8,7 +8,10 @@ namespace ZB.MOM.WW.ScadaBridge.Host.Tests;
|
||||
/// environment variable (<c>Program</c>'s configuration builder calls
|
||||
/// <c>AddEnvironmentVariables()</c>).
|
||||
///
|
||||
/// Dispose restores the previous value so tests stay isolated.
|
||||
/// Also supplies <c>ScadaBridge__InboundApi__ApiKeyPepper</c> so the Central-role
|
||||
/// StartupValidator preflight (added in 1fcc4f5) does not fail for tests that set
|
||||
/// <c>DOTNET_ENVIRONMENT=Central</c> without an explicit pepper env var. Both vars
|
||||
/// are restored on Dispose so tests stay isolated.
|
||||
/// </summary>
|
||||
internal sealed class CentralDbTestEnvironment : IDisposable
|
||||
{
|
||||
@@ -19,16 +22,27 @@ internal sealed class CentralDbTestEnvironment : IDisposable
|
||||
|
||||
private const string ConfigKey = "ScadaBridge__Database__ConfigurationDb";
|
||||
|
||||
// Test-only pepper — satisfies the ≥16-char StartupValidator requirement without
|
||||
// committing a real secret. The env-var name uses the double-underscore delimiter
|
||||
// so AddEnvironmentVariables() maps it to ScadaBridge:InboundApi:ApiKeyPepper.
|
||||
internal const string TestPepper = "test-pepper-01234567890";
|
||||
private const string PepperKey = "ScadaBridge__InboundApi__ApiKeyPepper";
|
||||
|
||||
private readonly string? _previousConfig;
|
||||
private readonly string? _previousPepper;
|
||||
|
||||
public CentralDbTestEnvironment()
|
||||
{
|
||||
_previousConfig = Environment.GetEnvironmentVariable(ConfigKey);
|
||||
Environment.SetEnvironmentVariable(ConfigKey, ConfigurationDb);
|
||||
|
||||
_previousPepper = Environment.GetEnvironmentVariable(PepperKey);
|
||||
Environment.SetEnvironmentVariable(PepperKey, TestPepper);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Environment.SetEnvironmentVariable(ConfigKey, _previousConfig);
|
||||
Environment.SetEnvironmentVariable(PepperKey, _previousPepper);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user