use super::*;
use crate::backend::types::LoginParams;
use crate::error::GarrisonError;
use crate::GarrisonDao;
use crate::GarrisonResult;
use async_trait::async_trait;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use http_body_util::BodyExt;
use tower::ServiceExt;
struct MockAuthBackend;
#[async_trait]
impl AuthBackend for MockAuthBackend {
async fn login(&self, login_id: &str, _params: &LoginParams) -> GarrisonResult<String> {
Ok(format!("token-{}", login_id))
}
async fn logout(&self, _token: &str) -> GarrisonResult<()> {
Ok(())
}
async fn check_login(&self, token: &str) -> GarrisonResult<bool> {
Ok(token.starts_with("token-"))
}
async fn check_permission(&self, _token: &str, _permission: &str) -> GarrisonResult<()> {
Ok(())
}
async fn check_role(&self, _token: &str, _role: &str) -> GarrisonResult<()> {
Ok(())
}
async fn check_safe(&self, _token: &str) -> GarrisonResult<bool> {
Ok(false)
}
async fn check_disable(&self, _token: &str) -> GarrisonResult<bool> {
Ok(false)
}
async fn check_api_key(&self, _api_key: &str, _namespace: &str) -> GarrisonResult<()> {
Ok(())
}
async fn get_token_info(
&self,
token: &str,
) -> GarrisonResult<crate::backend::types::TokenInfo> {
Ok(crate::backend::types::TokenInfo {
token: token.to_string(),
created_at: 1000,
last_active_at: 2000,
})
}
async fn get_session(&self, token: &str) -> GarrisonResult<crate::backend::types::SessionData> {
Ok(crate::backend::types::SessionData {
token: token.to_string(),
login_id: "mock-user".to_string(),
created_at: 1000,
last_active_at: 2000,
attrs: std::collections::HashMap::new(),
device: None,
ip: None,
user_agent: None,
safe_services: std::collections::HashMap::new(),
#[cfg(feature = "dynamic-active-timeout")]
dynamic_active_timeout: None,
#[cfg(feature = "anonymous-session")]
is_anon: false,
})
}
async fn kickout(&self, _login_id: &str) -> GarrisonResult<()> {
Ok(())
}
async fn switch_to(&self, _token: &str, _target_login_id: &str) -> GarrisonResult<()> {
Ok(())
}
async fn renew_to_equivalent(&self, token: &str) -> GarrisonResult<String> {
Ok(format!("renewed-{}", token))
}
}
fn make_server() -> GarrisonAuthServer {
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
GarrisonAuthServer::new(backend)
.with_internal_api_key("test-api-key")
.with_rate_limit(100)
}
#[tokio::test]
async fn test_external_router_handles_login() {
let server = make_server();
let app = server.external_router();
let body = serde_json::json!({
"login_id": "user1",
"params": LoginParams::default()
});
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/api/v1/auth/login")
.header("content-type", "application/json")
.body(Body::from(body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let bytes = resp.into_body().collect().await.unwrap().to_bytes();
let resp_json: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(resp_json["data"], "token-user1");
}
#[tokio::test]
async fn test_internal_router_handles_health() {
let server = make_server();
let app = server.internal_router();
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/api/v1/auth/health")
.header("x-api-key", "test-api-key")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let bytes = resp.into_body().collect().await.unwrap().to_bytes();
let resp_json: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(resp_json["data"], "ok");
}
#[tokio::test]
async fn test_internal_router_rejects_missing_api_key() {
let server = make_server();
let app = server.internal_router();
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/api/v1/auth/health")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
}
#[tokio::test]
async fn test_external_router_rate_limit() {
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend)
.with_internal_api_key("test-api-key")
.with_rate_limit(2);
let app = server.external_router();
let body = serde_json::json!({
"login_id": "user1",
"params": LoginParams::default()
});
for _ in 0..2 {
let resp = app
.clone()
.oneshot(
Request::builder()
.method("POST")
.uri("/api/v1/auth/login")
.header("content-type", "application/json")
.body(Body::from(body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/api/v1/auth/login")
.header("content-type", "application/json")
.body(Body::from(body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::TOO_MANY_REQUESTS);
}
#[tokio::test]
async fn test_builder_methods() {
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend)
.with_external_port(9000)
.with_internal_port(9001)
.with_rate_limit(50)
.with_internal_api_key("my-key");
assert_eq!(server.config.external_port, 9000);
assert_eq!(server.config.internal_port, 9001);
assert_eq!(server.config.external_rate_limit_per_ip, 50);
assert_eq!(server.config.internal_api_key, "my-key");
}
#[tokio::test]
async fn test_default_config() {
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend);
assert_eq!(server.config.external_port, 8080);
assert_eq!(server.config.internal_port, 8081);
assert_eq!(server.config.external_rate_limit_per_ip, 100);
assert!(
server.config.internal_api_key.is_empty(),
"Default internal_api_key 必须为空(fail-closed,M-SAST-1/M-5)"
);
}
#[cfg(feature = "backend-kit")]
#[tokio::test]
async fn test_new_with_kit_builds_server() {
use crate::backend::BackendModule;
use trait_kit::kit::AsyncKit;
let mut kit = AsyncKit::new();
kit.register::<BackendModule>()
.expect("register BackendModule failed");
let kit = kit.build().await.expect("kit build failed");
let server = GarrisonAuthServer::new_with_kit(kit)
.await
.expect("new_with_kit failed");
assert_eq!(server.config.external_port, 8080);
assert_eq!(server.config.internal_port, 8081);
}
#[cfg(feature = "backend-kit")]
#[tokio::test]
async fn test_new_with_kit_missing_module_fails() {
use trait_kit::kit::AsyncKit;
let kit = AsyncKit::new();
let kit = kit.build().await.expect("empty build should succeed");
let result = GarrisonAuthServer::new_with_kit(kit).await;
assert!(
result.is_err(),
"new_with_kit 应在 kit 未注册 BackendModule 时返回错误"
);
}
#[cfg(feature = "tls")]
#[tokio::test]
async fn test_with_tls_sets_config() {
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend).with_tls("/path/to/cert.pem", "/path/to/key.pem");
let tls_config = server
.tls_config
.as_ref()
.expect("with_tls 后 tls_config 必须为 Some");
assert_eq!(
tls_config.cert_path,
std::path::PathBuf::from("/path/to/cert.pem")
);
assert_eq!(
tls_config.key_path,
std::path::PathBuf::from("/path/to/key.pem")
);
}
#[cfg(feature = "tls")]
#[tokio::test]
async fn test_without_tls_config_is_none() {
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend);
assert!(
server.tls_config.is_none(),
"未调用 with_tls 时 tls_config 必须为 None"
);
}
#[cfg(feature = "tls")]
#[tokio::test]
async fn test_with_tls_chainable() {
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend)
.with_external_port(9000)
.with_internal_port(9001)
.with_rate_limit(50)
.with_internal_api_key("my-key")
.with_tls("/cert.pem", "/key.pem");
assert_eq!(server.config.external_port, 9000);
assert_eq!(server.config.internal_port, 9001);
assert_eq!(server.config.external_rate_limit_per_ip, 50);
assert_eq!(server.config.internal_api_key, "my-key");
assert!(server.tls_config.is_some());
}
#[test]
fn test_to_api_response_ok() {
let result: Result<i32, GarrisonError> = Ok(42);
let resp = to_api_response(result);
assert_eq!(resp.data, Some(42));
assert!(resp.error_code.is_none());
assert!(resp.message.is_none());
}
#[test]
fn test_to_api_response_err() {
let result: Result<i32, GarrisonError> = Err(GarrisonError::Internal("test error".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("INTERNAL_ERROR"));
assert_eq!(resp.message.as_deref(), Some("内部错误"));
}
#[tokio::test]
async fn test_external_router_rejects_internal_path() {
let server = make_server();
let app = server.external_router();
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/api/v1/auth/check/login")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_internal_router_rejects_external_path() {
let server = make_server();
let app = server.internal_router();
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/api/v1/auth/login")
.header("x-api-key", "test-api-key")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_internal_router_rejects_wrong_api_key() {
let server = make_server();
let app = server.internal_router();
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/api/v1/auth/health")
.header("x-api-key", "wrong-api-key")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
}
#[test]
fn test_auth_server_config_default() {
let config = AuthServerConfig::default();
assert_eq!(config.external_port, 8080);
assert_eq!(config.internal_port, 8081);
assert_eq!(config.external_rate_limit_per_ip, 100);
assert!(
config.internal_api_key.is_empty(),
"Default internal_api_key 必须为空(fail-closed)"
);
}
#[tokio::test]
async fn test_listen_returns_error_when_external_port_in_use() {
let external_listener = tokio::net::TcpListener::bind("0.0.0.0:0")
.await
.expect("绑定测试端口应成功");
let external_port = external_listener
.local_addr()
.expect("获取端口应成功")
.port();
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend)
.with_external_port(external_port)
.with_internal_port(0);
let result = server.listen().await;
assert!(result.is_err(), "外网端口被占用时 listen 应返回错误");
}
#[tokio::test]
async fn test_listen_returns_error_when_internal_port_in_use() {
let internal_listener = tokio::net::TcpListener::bind("0.0.0.0:0")
.await
.expect("绑定测试端口应成功");
let internal_port = internal_listener
.local_addr()
.expect("获取端口应成功")
.port();
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend)
.with_external_port(0) .with_internal_port(internal_port);
let result = server.listen().await;
assert!(result.is_err(), "内网端口被占用时 listen 应返回错误");
}
#[tokio::test]
async fn test_listen_starts_and_runs() {
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend)
.with_external_port(0)
.with_internal_port(0);
tokio::select! {
result = server.listen() => {
panic!("listen 不应在 300ms 内返回: {:?}", result);
},
_ = tokio::time::sleep(std::time::Duration::from_millis(300)) => {
},
}
}
#[cfg(feature = "tls")]
#[tokio::test]
async fn test_listen_tls_returns_error_when_cert_not_found() {
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend)
.with_external_port(0)
.with_internal_port(0)
.with_tls("/nonexistent/cert.pem", "/nonexistent/key.pem");
let result = server.listen().await;
assert!(result.is_err(), "TLS 证书不存在时 listen 应返回错误");
}
#[test]
fn test_to_api_response_with_not_login_error() {
let result: Result<i32, GarrisonError> =
Err(GarrisonError::NotLogin("not logged in".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("NOT_LOGIN"));
assert_eq!(resp.message.as_deref(), Some("未登录"));
}
#[test]
fn test_to_api_response_with_dao_error() {
let result: Result<i32, GarrisonError> =
Err(GarrisonError::Dao("db connection failed".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("DAO_ERROR"));
assert_eq!(resp.message.as_deref(), Some("数据访问错误"));
}
#[test]
fn test_to_api_response_with_invalid_param_error() {
let result: Result<i32, GarrisonError> =
Err(GarrisonError::InvalidParam("missing field".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("INVALID_PARAM"));
assert_eq!(resp.message.as_deref(), Some("参数无效"));
}
#[test]
fn test_to_api_response_with_not_permission_error() {
let result: Result<i32, GarrisonError> =
Err(GarrisonError::NotPermission("admin:read".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("NOT_PERMISSION"));
assert_eq!(resp.message.as_deref(), Some("无权限"));
}
#[test]
fn test_to_api_response_with_expired_token_error() {
let result: Result<i32, GarrisonError> =
Err(GarrisonError::ExpiredToken("token-abc".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("EXPIRED_TOKEN"));
assert_eq!(resp.message.as_deref(), Some("Token 已过期"));
}
#[test]
fn test_to_api_response_with_not_role_error() {
let result: Result<i32, GarrisonError> = Err(GarrisonError::NotRole("admin".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("NOT_ROLE"));
assert_eq!(resp.message.as_deref(), Some("无角色"));
}
#[test]
fn test_to_api_response_with_invalid_token_error() {
let result: Result<i32, GarrisonError> =
Err(GarrisonError::InvalidToken("bad-format".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("INVALID_TOKEN"));
assert_eq!(resp.message.as_deref(), Some("Token 无效"));
}
#[test]
fn test_to_api_response_with_token_revoked_error() {
let result: Result<i32, GarrisonError> =
Err(GarrisonError::TokenRevoked("revoked-token".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("TOKEN_REVOKED"));
assert_eq!(resp.message.as_deref(), Some("Token 已吊销"));
}
#[test]
fn test_to_api_response_with_config_error() {
let result: Result<i32, GarrisonError> =
Err(GarrisonError::Config("invalid config".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("CONFIG_ERROR"));
assert_eq!(resp.message.as_deref(), Some("配置错误"));
}
#[test]
fn test_to_api_response_with_session_error() {
let result: Result<i32, GarrisonError> =
Err(GarrisonError::Session("session expired".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("SESSION_ERROR"));
assert_eq!(resp.message.as_deref(), Some("会话错误"));
}
#[test]
fn test_to_api_response_with_network_error() {
let result: Result<i32, GarrisonError> = Err(GarrisonError::Network("timeout".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("NETWORK_ERROR"));
assert_eq!(resp.message.as_deref(), Some("网络错误"));
}
#[test]
fn test_to_api_response_with_not_implemented_error() {
let result: Result<i32, GarrisonError> =
Err(GarrisonError::NotImplemented("not yet".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("NOT_IMPLEMENTED"));
assert_eq!(resp.message.as_deref(), Some("未实现"));
}
#[test]
fn test_to_api_response_with_firewall_blocked_error() {
let result: Result<i32, GarrisonError> =
Err(GarrisonError::FirewallBlocked("bruteforce".to_string()));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("FIREWALL_BLOCKED"));
assert_eq!(resp.message.as_deref(), Some("防火墙拦截"));
}
#[test]
fn test_to_api_response_with_disable_service_error() {
let result: Result<i32, GarrisonError> = Err(GarrisonError::DisableService {
service: "default".to_string(),
until: None,
});
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("DISABLE_SERVICE"));
assert_eq!(resp.message.as_deref(), Some("账号已被封禁"));
}
#[test]
fn test_to_api_response_with_not_safe_error() {
let result: Result<i32, GarrisonError> = Err(GarrisonError::NotSafe {
reason: "MFA_REQUIRED".to_string(),
});
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("NOT_SAFE"));
assert_eq!(resp.message.as_deref(), Some("未完成二次认证"));
}
#[test]
fn test_to_api_response_with_sms_rate_limit_exceeded_error() {
let result: Result<i32, GarrisonError> = Err(GarrisonError::SmsRateLimitExceeded {
window: "hourly".to_string(),
});
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("SMS_RATE_LIMIT_EXCEEDED"));
assert_eq!(resp.message.as_deref(), Some("短信发送频繁"));
}
#[test]
fn test_to_api_response_with_sms_verify_max_attempts_error() {
let result: Result<i32, GarrisonError> = Err(GarrisonError::SmsVerifyMaxAttempts);
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("SMS_VERIFY_MAX_ATTEMPTS"));
assert_eq!(resp.message.as_deref(), Some("验证码尝试次数超限"));
}
#[test]
fn test_to_api_response_with_sms_code_not_found_error() {
let result: Result<i32, GarrisonError> = Err(GarrisonError::SmsCodeNotFound);
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("SMS_CODE_NOT_FOUND"));
assert_eq!(resp.message.as_deref(), Some("验证码不存在或已过期"));
}
#[test]
fn test_to_api_response_with_sms_channel_recycled_error() {
let result: Result<i32, GarrisonError> = Err(GarrisonError::SmsChannelRecycled);
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("SMS_CHANNEL_RECYCLED"));
assert_eq!(resp.message.as_deref(), Some("短信通道已回收"));
}
#[test]
fn test_to_api_response_with_exception_not_login() {
use crate::exception::GarrisonException;
let result: Result<i32, GarrisonError> = Err(GarrisonError::Exception(GarrisonException::new(
-1,
"请先登录",
)));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("NOT_LOGIN"));
assert_eq!(resp.message.as_deref(), Some("未登录"));
}
#[test]
fn test_to_api_response_with_exception_not_permission() {
use crate::exception::GarrisonException;
let result: Result<i32, GarrisonError> = Err(GarrisonError::Exception(GarrisonException::new(
-2,
"无权限",
)));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("NOT_PERMISSION"));
assert_eq!(resp.message.as_deref(), Some("无权限"));
}
#[test]
fn test_to_api_response_with_exception_other_code() {
use crate::exception::GarrisonException;
let result: Result<i32, GarrisonError> = Err(GarrisonError::Exception(GarrisonException::new(
1001,
"业务异常",
)));
let resp = to_api_response(result);
assert!(resp.data.is_none());
assert_eq!(resp.error_code.as_deref(), Some("EXCEPTION"));
assert_eq!(resp.message.as_deref(), Some("业务异常"));
}
#[tokio::test]
async fn test_rate_limit_zero_rejects_all_requests() {
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend)
.with_internal_api_key("test-api-key")
.with_rate_limit(0);
let app = server.external_router();
let body = serde_json::json!({
"login_id": "user1",
"params": LoginParams::default()
});
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/api/v1/auth/login")
.header("content-type", "application/json")
.body(Body::from(body.to_string()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::TOO_MANY_REQUESTS,
"rate_limit=0 时所有请求应被限速"
);
}
#[tokio::test]
async fn test_empty_internal_api_key_rejects_all() {
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend)
.with_internal_api_key("")
.with_rate_limit(100);
let app = server.internal_router();
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/api/v1/auth/health")
.header("x-api-key", "")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::UNAUTHORIZED,
"空 API Key 应拒绝所有内网请求"
);
}
#[cfg(feature = "oauth2-server")]
struct SimpleMockDao;
#[cfg(feature = "oauth2-server")]
#[async_trait]
impl GarrisonDao for SimpleMockDao {
async fn get(&self, _key: &str) -> GarrisonResult<Option<String>> {
Ok(None)
}
async fn set(&self, _key: &str, _value: &str, _ttl_seconds: u64) -> GarrisonResult<()> {
Ok(())
}
async fn update(&self, _key: &str, _value: &str) -> GarrisonResult<()> {
Ok(())
}
async fn expire(&self, _key: &str, _seconds: u64) -> GarrisonResult<()> {
Ok(())
}
async fn delete(&self, _key: &str) -> GarrisonResult<()> {
Ok(())
}
}
#[cfg(feature = "oauth2-server")]
struct SimpleMockClientStore;
#[cfg(feature = "oauth2-server")]
#[async_trait]
impl crate::oauth2_server::client::OAuth2ClientStore for SimpleMockClientStore {
async fn create(
&self,
_client: crate::oauth2_server::client::OAuth2Client,
) -> GarrisonResult<()> {
Ok(())
}
async fn get(
&self,
_client_id: &str,
) -> GarrisonResult<Option<crate::oauth2_server::client::OAuth2Client>> {
Ok(None)
}
async fn update(
&self,
_client: crate::oauth2_server::client::OAuth2Client,
) -> GarrisonResult<()> {
Ok(())
}
async fn delete(&self, _client_id: &str) -> GarrisonResult<()> {
Ok(())
}
async fn list(&self) -> GarrisonResult<Vec<crate::oauth2_server::client::OAuth2Client>> {
Ok(Vec::new())
}
}
#[cfg(feature = "oauth2-server")]
#[tokio::test]
async fn test_external_router_includes_oauth2_routes_when_set() {
let dao: Arc<dyn GarrisonDao> = Arc::new(SimpleMockDao);
let store: Arc<dyn crate::oauth2_server::client::OAuth2ClientStore> =
Arc::new(SimpleMockClientStore);
let oauth2_state = Arc::new(oauth2_routes::OAuth2State::new(
store,
dao,
"http://localhost/login".to_string(),
));
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend)
.with_internal_api_key("test-api-key")
.with_rate_limit(100)
.with_oauth2(oauth2_state);
let app = server.external_router();
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/oauth2/authorize?response_type=code&client_id=test&redirect_uri=http://localhost/cb&code_challenge=test&code_challenge_method=S256&state=xyz")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_ne!(
resp.status(),
StatusCode::NOT_FOUND,
"with_oauth2 设置后 /oauth2/authorize 端点应存在"
);
}
#[cfg(feature = "oauth2-server")]
#[tokio::test]
async fn test_internal_router_includes_oauth2_routes_when_set() {
let dao: Arc<dyn GarrisonDao> = Arc::new(SimpleMockDao);
let store: Arc<dyn crate::oauth2_server::client::OAuth2ClientStore> =
Arc::new(SimpleMockClientStore);
let oauth2_state = Arc::new(oauth2_routes::OAuth2State::new(
store,
dao,
"http://localhost/login".to_string(),
));
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend)
.with_internal_api_key("test-api-key")
.with_rate_limit(100)
.with_oauth2(oauth2_state);
let app = server.internal_router();
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/introspect")
.header("x-api-key", "test-api-key")
.header("content-type", "application/json")
.body(Body::from(r#"{"token":"test"}"#))
.unwrap(),
)
.await
.unwrap();
assert_ne!(
resp.status(),
StatusCode::NOT_FOUND,
"with_oauth2 设置后 /oauth2/introspect 端点应存在"
);
}
#[cfg(feature = "oauth2-server")]
#[tokio::test]
async fn test_external_router_excludes_oauth2_routes_when_not_set() {
let server = make_server();
let app = server.external_router();
let resp = app
.oneshot(
Request::builder()
.method("GET")
.uri("/oauth2/authorize")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::NOT_FOUND,
"未设置 oauth2_state 时 /oauth2/authorize 应返回 404"
);
}
#[cfg(feature = "oauth2-server")]
#[tokio::test]
async fn test_internal_router_excludes_oauth2_routes_when_not_set() {
let server = make_server();
let app = server.internal_router();
let resp = app
.oneshot(
Request::builder()
.method("POST")
.uri("/oauth2/introspect")
.header("x-api-key", "test-api-key")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::NOT_FOUND,
"未设置 oauth2_state 时 /oauth2/introspect 应返回 404"
);
}
#[cfg(feature = "oauth2-server")]
#[tokio::test]
async fn test_with_oauth2_chainable() {
let dao: Arc<dyn GarrisonDao> = Arc::new(SimpleMockDao);
let store: Arc<dyn crate::oauth2_server::client::OAuth2ClientStore> =
Arc::new(SimpleMockClientStore);
let oauth2_state = Arc::new(oauth2_routes::OAuth2State::new(
store,
dao,
"http://localhost/login".to_string(),
));
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend)
.with_external_port(9000)
.with_internal_port(9001)
.with_rate_limit(50)
.with_internal_api_key("my-key")
.with_oauth2(oauth2_state);
assert_eq!(server.config.external_port, 9000);
assert_eq!(server.config.internal_port, 9001);
assert_eq!(server.config.external_rate_limit_per_ip, 50);
assert_eq!(server.config.internal_api_key, "my-key");
assert!(
server.oauth2_state.is_some(),
"with_oauth2 后 oauth2_state 必须为 Some"
);
}
#[tokio::test]
async fn test_with_rate_limit_large_value() {
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server = GarrisonAuthServer::new(backend)
.with_internal_api_key("test-api-key")
.with_rate_limit(u32::MAX);
assert_eq!(server.config.external_rate_limit_per_ip, u32::MAX);
}
#[tokio::test]
async fn test_with_internal_api_key_accepts_str_and_string() {
let backend: Arc<dyn AuthBackend> = Arc::new(MockAuthBackend);
let server1 = GarrisonAuthServer::new(backend.clone()).with_internal_api_key("str-key");
assert_eq!(server1.config.internal_api_key, "str-key");
let server2 = GarrisonAuthServer::new(backend).with_internal_api_key("string-key".to_string());
assert_eq!(server2.config.internal_api_key, "string-key");
}