use super::init_garrison_auth;
use crate::bootstrap::error::BootstrapError;
use crate::common::test_helpers::{create_test_db_pool, skip_if_no_test_db};
use crate::config::settings::Settings;
fn make_settings(jwt_secret: &str) -> Settings {
let mut settings = crate::bootstrap::config::load_settings().expect("Failed to load settings");
settings.auth.jwt_secret = jwt_secret.to_string();
settings
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_init_garrison_auth_rejects_empty_secret() {
if skip_if_no_test_db() {
return;
}
let settings = make_settings("");
let pool = create_test_db_pool();
let result = init_garrison_auth(&settings, pool).await;
assert!(
result.is_err(),
"empty jwt_secret must be rejected with Err(BootstrapError::GarrisonConfig)"
);
let err = result.unwrap_err();
assert!(
matches!(err, BootstrapError::GarrisonConfig(_)),
"error should be BootstrapError::GarrisonConfig variant, got: {err}"
);
assert!(
format!("{err}").contains("garrison config error"),
"error Display should contain 'garrison config error' prefix, got: {err}"
);
assert!(
format!("{err}").contains("must not be empty or missing"),
"error Display should contain EmptySecret description, got: {err}"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_init_garrison_auth_rejects_weak_secret() {
if skip_if_no_test_db() {
return;
}
let weak = "too_short"; let settings = make_settings(weak);
let pool = create_test_db_pool();
let result = init_garrison_auth(&settings, pool).await;
assert!(
result.is_err(),
"weak jwt_secret (<32 bytes) must be rejected with Err(BootstrapError::GarrisonConfig)"
);
let err = result.unwrap_err();
assert!(
matches!(err, BootstrapError::GarrisonConfig(_)),
"error should be BootstrapError::GarrisonConfig variant, got: {err}"
);
let display = format!("{err}");
assert!(
display.contains("length 9"),
"error Display should contain 'length 9', got: {display}"
);
assert!(
display.contains("< 32 bytes"),
"error Display should contain '< 32 bytes', got: {display}"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[ignore = "集成测试覆盖:需真实 DB + garrison migrations + GARRISON_MANAGER 单例隔离"]
async fn test_init_garrison_auth_strong_secret_returns_ok() {
let strong = "a-very-strong-secret-key-32-bytes-or-more!!"; let settings = make_settings(strong);
if skip_if_no_test_db() {
return;
}
let pool = create_test_db_pool();
let _garrison_guard = crate::common::test_helpers::acquire_garrison_global_state().await;
let result = init_garrison_auth(&settings, pool).await;
assert!(
result.is_ok(),
"strong jwt_secret + valid DbPool should succeed, got: {:?}",
result.err()
);
}