diff --git a/Cargo.lock b/Cargo.lock index 7f21cbcff..b5e17a6fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2663,6 +2663,16 @@ dependencies = [ "memchr", ] +[[package]] +name = "ctor" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a2785755761f3ddc1492979ce1e48d2c00d09311c39e4466429188f3dd6501" +dependencies = [ + "quote", + "syn 2.0.106", +] + [[package]] name = "ctr" version = "0.9.2" @@ -4710,6 +4720,8 @@ version = "1.0.0" dependencies = [ "anyhow", "chrono", + "ctor", + "dotenvy", "futures", "jsonwebtoken", "prost 0.14.1", diff --git a/services/integration_tests/Cargo.toml b/services/integration_tests/Cargo.toml index a45ef27ef..35c52a392 100644 --- a/services/integration_tests/Cargo.toml +++ b/services/integration_tests/Cargo.toml @@ -41,6 +41,9 @@ chrono = { workspace = true } # Environment variables dotenvy = "0.15" +# Constructor hooks for test initialization +ctor = "0.2" + [dev-dependencies] # Test utilities serial_test.workspace = true diff --git a/services/integration_tests/tests/common/auth_helpers.rs b/services/integration_tests/tests/common/auth_helpers.rs index 7c5ba2d09..8ac471000 100644 --- a/services/integration_tests/tests/common/auth_helpers.rs +++ b/services/integration_tests/tests/common/auth_helpers.rs @@ -23,6 +23,19 @@ //! } //! ``` +/// Initialize test environment by loading .env file +/// This runs BEFORE module initialization, ensuring JWT_SECRET is available +#[ctor::ctor] +fn init_test_env() { + // Load .env file at module initialization time + let _ = dotenvy::dotenv(); + + // Verify JWT_SECRET is available + if std::env::var("JWT_SECRET").is_err() { + eprintln!("WARNING: JWT_SECRET not found in .env file"); + } +} + use anyhow::{Context, Result}; use chrono::{Duration, Utc}; use jsonwebtoken::{encode, Algorithm, EncodingKey, Header}; @@ -200,9 +213,6 @@ impl TestAuthConfig { /// 2. Set JWT_SECRET in .env file /// 3. Load .env before running tests (e.g., `source .env` or use dotenv) pub fn get_test_jwt_secret() -> String { - // Load .env file for JWT_SECRET (idempotent, safe to call multiple times) - let _ = dotenvy::dotenv(); - std::env::var("JWT_SECRET").expect( "FATAL: JWT_SECRET must be set in .env file for E2E tests\n\ \n\