mod common;
#[test]
fn health_public_with_api_key_set() {
let (port, _guard) = common::start_server_with(&["--api-key", "test-secret"], &[]);
let resp = reqwest::blocking::get(format!("http://127.0.0.1:{port}/health"))
.expect("GET /health must succeed");
assert_eq!(resp.status().as_u16(), 200, "GET /health must return 200");
}
#[test]
fn scenarios_without_auth_returns_401() {
let (port, _guard) = common::start_server_with(&["--api-key", "test-secret"], &[]);
let resp = reqwest::blocking::get(format!("http://127.0.0.1:{port}/scenarios"))
.expect("GET /scenarios must succeed at HTTP level");
assert_eq!(
resp.status().as_u16(),
401,
"GET /scenarios without auth must return 401"
);
let body: serde_json::Value = resp.json().expect("body must be valid JSON");
assert_eq!(body["error"], "unauthorized");
}
#[test]
fn scenarios_wrong_key_returns_401() {
let (port, _guard) = common::start_server_with(&["--api-key", "correct-key"], &[]);
let client = reqwest::blocking::Client::new();
let resp = client
.get(format!("http://127.0.0.1:{port}/scenarios"))
.header("Authorization", "Bearer wrong-key")
.send()
.expect("request must succeed at HTTP level");
assert_eq!(
resp.status().as_u16(),
401,
"GET /scenarios with wrong key must return 401"
);
let body: serde_json::Value = resp.json().expect("body must be valid JSON");
assert_eq!(body["detail"], "invalid API key");
}
#[test]
fn scenarios_correct_key_returns_200() {
let (port, _guard) = common::start_server_with(&["--api-key", "my-secret-key"], &[]);
let client = reqwest::blocking::Client::new();
let resp = client
.get(format!("http://127.0.0.1:{port}/scenarios"))
.header("Authorization", "Bearer my-secret-key")
.send()
.expect("request must succeed");
assert_eq!(
resp.status().as_u16(),
200,
"GET /scenarios with correct key must return 200"
);
}
#[test]
fn env_var_enables_auth() {
let (port, _guard) = common::start_server_with(&[], &[("SONDA_API_KEY", "env-secret")]);
let resp = reqwest::blocking::get(format!("http://127.0.0.1:{port}/scenarios"))
.expect("request must succeed at HTTP level");
assert_eq!(
resp.status().as_u16(),
401,
"GET /scenarios without auth must return 401 when SONDA_API_KEY is set"
);
let client = reqwest::blocking::Client::new();
let resp = client
.get(format!("http://127.0.0.1:{port}/scenarios"))
.header("Authorization", "Bearer env-secret")
.send()
.expect("request must succeed");
assert_eq!(
resp.status().as_u16(),
200,
"GET /scenarios with correct env-based key must return 200"
);
}
#[test]
fn no_key_all_endpoints_public() {
let (port, _guard) = common::start_server_with(&[], &[]);
let resp = reqwest::blocking::get(format!("http://127.0.0.1:{port}/scenarios"))
.expect("GET /scenarios must succeed");
assert_eq!(
resp.status().as_u16(),
200,
"GET /scenarios must return 200 when no API key is configured"
);
}
#[test]
fn no_key_health_accessible() {
let (port, _guard) = common::start_server_with(&[], &[]);
let resp = reqwest::blocking::get(format!("http://127.0.0.1:{port}/health"))
.expect("GET /health must succeed");
assert_eq!(
resp.status().as_u16(),
200,
"GET /health must return 200 when no API key is configured"
);
}