use crate::a2a::server::{check_gate_authenticated, tokens_match};
#[test]
fn loopback_without_a_key_is_allowed() {
assert!(check_gate_authenticated("127.0.0.1", None).is_ok());
assert!(check_gate_authenticated("::1", None).is_ok());
}
#[test]
fn any_key_makes_any_bind_allowed() {
assert!(check_gate_authenticated("0.0.0.0", Some("secret")).is_ok());
assert!(check_gate_authenticated("192.168.1.10", Some("secret")).is_ok());
}
#[test]
fn a_wildcard_bind_without_a_key_is_refused() {
let err = check_gate_authenticated("0.0.0.0", None).unwrap_err();
assert!(
err.contains("not loopback"),
"message names the cause: {err}"
);
assert!(err.contains("api_key"), "message says how to fix it: {err}");
}
#[test]
fn a_public_ip_without_a_key_is_refused() {
assert!(check_gate_authenticated("192.168.1.10", None).is_err());
assert!(check_gate_authenticated("10.0.0.5", None).is_err());
}
#[test]
fn a_non_ip_bind_without_a_key_fails_safe() {
assert!(check_gate_authenticated("localhost", None).is_err());
assert!(check_gate_authenticated("", None).is_err());
}
#[test]
fn a_matching_token_authorizes() {
assert!(tokens_match("the-secret-token", "the-secret-token"));
}
#[test]
fn a_wrong_token_is_rejected() {
assert!(!tokens_match("wrong", "the-secret-token"));
assert!(!tokens_match("the-secret-toke", "the-secret-token"));
assert!(!tokens_match("the-secret-tokenX", "the-secret-token"));
}
#[test]
fn an_empty_presented_token_is_rejected() {
assert!(!tokens_match("", "the-secret-token"));
}