use std::time::Duration;
#[tokio::test]
async fn test_invalid_api_key() {
let client = reqwest::Client::new();
let result = tokio::time::timeout(
Duration::from_secs(5),
client
.get("http://localhost:8580/v1/account")
.header("X-API-Key", "invalid_key_12345")
.send()
).await;
let response = match result {
Ok(Ok(resp)) => resp,
_ => {
println!("⚠️ Servidor não está rodando, pulando teste");
return;
}
};
assert_eq!(response.status(), 401);
}
#[tokio::test]
async fn test_bearer_token_auth() {
let client = reqwest::Client::new();
let result = tokio::time::timeout(
Duration::from_secs(5),
client
.get("http://localhost:8580/v1/account")
.header("Authorization", "Bearer invalid_key")
.send()
).await;
let response = match result {
Ok(Ok(resp)) => resp,
_ => {
println!("⚠️ Servidor não está rodando, pulando teste");
return;
}
};
assert_ne!(response.status(), 404);
}
#[tokio::test]
async fn test_no_auth_header() {
let client = reqwest::Client::new();
let result = tokio::time::timeout(
Duration::from_secs(5),
client.get("http://localhost:8580/v1/account").send()
).await;
let response = match result {
Ok(Ok(resp)) => resp,
_ => {
println!("⚠️ Servidor não está rodando, pulando teste");
return;
}
};
assert_eq!(response.status(), 401);
}
#[tokio::test]
async fn test_protected_endpoints_require_auth() {
let client = reqwest::Client::new();
let endpoints = vec![
"/v1/account",
"/v1/account/usage",
"/v1/databases",
"/v1/query",
"/v1/batch",
"/v1/transaction",
];
for endpoint in endpoints {
let result = tokio::time::timeout(
Duration::from_secs(5),
client.get(&format!("http://localhost:8580{}", endpoint)).send()
).await;
let response = match result {
Ok(Ok(resp)) => resp,
_ => {
println!("⚠️ Servidor não está rodando, pulando teste");
return;
}
};
let status = response.status();
if status != 401 && status != 405 {
panic!("Endpoint {} retornou {} em vez de 401", endpoint, status);
}
}
}
#[tokio::test]
async fn test_auth_error_response_format() {
let client = reqwest::Client::new();
let result = tokio::time::timeout(
Duration::from_secs(5),
client.get("http://localhost:8580/v1/account").send()
).await;
let response = match result {
Ok(Ok(resp)) => resp,
_ => {
println!("⚠️ Servidor não está rodando, pulando teste");
return;
}
};
if response.status() == 401 {
let body: serde_json::Value = response.json().await.unwrap();
assert!(
body.get("success").is_some() ||
body.get("error").is_some() ||
body.get("message").is_some(),
"Resposta de erro deve conter 'success', 'error' ou 'message'"
);
}
}