use axum::http::StatusCode;
use serde_json::json;
use crate::tests::harness::{author, TestServer};
const TENANT: &str = "/docs/acme";
#[tokio::test]
async fn the_api_root_is_an_authenticated_no_op() {
let server = TestServer::start().await;
let response = server.get("").await;
response.expect_status(StatusCode::OK);
assert_eq!(response.json(), json!({ "pong": true }));
assert!(response.json().get("replica").is_none());
}
#[tokio::test]
async fn every_content_route_requires_the_api_key() {
let server = TestServer::start().await;
server.write_file(TENANT, "a.md", "x").await;
for path in [
"",
TENANT,
&format!("{}/files", TENANT),
&format!("{}/files/a.md", TENANT),
&format!("{}/commits", TENANT),
&format!("{}/order", TENANT),
&format!("{}/count/files", TENANT),
] {
let response = server.get_unauthenticated(path).await;
response.expect_status(StatusCode::UNAUTHORIZED);
assert_eq!(response.json()["error"], "missing or invalid API key");
}
}
#[tokio::test]
async fn a_wrong_or_malformed_credential_is_refused() {
let server = TestServer::start().await;
for header in [
"Bearer wrong-key",
"Bearer ",
"test-api-key",
"Basic dGVzdDp0ZXN0",
"bearer test-api-key",
] {
let response = reqwest::Client::new()
.get(&server.base_url)
.header("Authorization", header)
.send()
.await
.expect("request failed");
assert_eq!(
response.status(),
StatusCode::UNAUTHORIZED,
"accepted credential: {:?}",
header
);
}
}
#[tokio::test]
async fn the_health_routes_are_public() {
let server = TestServer::start().await;
let status = server.get_unauthenticated("/_health/status").await;
status.expect_status(StatusCode::OK);
let body = status.json();
assert_eq!(body["status"], "healthy");
assert_eq!(body["name"], "githttp-fs");
assert_eq!(body["version"], env!("CARGO_PKG_VERSION"));
assert_eq!(body["role"], "standalone");
assert_eq!(body["writable"], true);
assert!(body["uptime_secs"].is_number());
chrono::DateTime::parse_from_rfc3339(body["started_at"].as_str().unwrap())
.expect("started_at is not RFC 3339");
let replication = server.get_unauthenticated("/_health/replication").await;
replication.expect_status(StatusCode::OK);
let body = replication.json();
assert_eq!(body["node"]["role"], "standalone");
assert_eq!(body["status"], "healthy");
assert!(body["issues"].as_array().unwrap().is_empty());
chrono::DateTime::parse_from_rfc3339(body["observed_at"].as_str().unwrap())
.expect("observed_at is not RFC 3339");
}
#[tokio::test]
async fn health_takes_exactly_two_reserved_paths() {
let server = TestServer::start().await;
server
.get_unauthenticated("/_health/status/extra")
.await
.expect_status(StatusCode::NOT_FOUND);
server
.get_unauthenticated("/_health/acme/files")
.await
.expect_status(StatusCode::UNAUTHORIZED);
server
.get("/_health/acme/files")
.await
.expect_status(StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn the_server_root_redirects_to_the_versioned_prefix() {
let server = TestServer::start().await;
let root = server
.base_url
.strip_suffix("/v1")
.expect("base url should end in /v1")
.to_string();
let response = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.expect("cannot build client")
.get(format!("{}/", root))
.send()
.await
.expect("request failed");
assert_eq!(response.status(), StatusCode::PERMANENT_REDIRECT);
assert_eq!(
response
.headers()
.get("location")
.and_then(|value| value.to_str().ok()),
Some("/v1")
);
}
#[tokio::test]
async fn a_replica_refuses_writes_with_locked() {
let server = TestServer::builder().replica().start().await;
let response = server
.put(
&format!("{}/files/a.md", TENANT),
json!({ "author": author(), "content": "x" }),
)
.await;
response.expect_status(StatusCode::LOCKED);
assert!(response.error_message().contains("read-only replica"));
server
.delete(
&format!("{}/files/a.md", TENANT),
json!({ "author": author() }),
)
.await
.expect_status(StatusCode::LOCKED);
server
.post(
&format!("{}/files/a.md/move", TENANT),
json!({ "author": author(), "destination": "b.md" }),
)
.await
.expect_status(StatusCode::LOCKED);
server
.request(reqwest::Method::DELETE, TENANT, None, true)
.await
.expect_status(StatusCode::LOCKED);
}
#[tokio::test]
async fn a_cold_replica_refuses_content_reads_but_still_explains_itself() {
let server = TestServer::builder().replica().start().await;
let read = server.get(&format!("{}/files", TENANT)).await;
read.expect_status(StatusCode::SERVICE_UNAVAILABLE);
assert!(read.error_message().contains("bootstrapping"));
let ping = server.get("").await;
ping.expect_status(StatusCode::OK);
assert_eq!(ping.json()["pong"], true);
let replica = &ping.json()["replica"];
assert_eq!(replica["state"], "bootstrapping");
assert_eq!(replica["stream_connected"], false);
assert!(replica["last_reconcile_at"].is_null());
let status = server.get_unauthenticated("/_health/status").await;
status.expect_status(StatusCode::OK);
assert_eq!(status.json()["status"], "bootstrapping");
assert_eq!(status.json()["role"], "replica");
assert_eq!(status.json()["writable"], false);
}
#[tokio::test]
async fn the_batch_read_route_is_the_one_write_shaped_route_a_replica_serves() {
let server = TestServer::builder().replica().start().await;
let response = server
.post(
&format!("{}/batch/files/read", TENANT),
json!({ "files": ["a.md"] }),
)
.await;
response.expect_status(StatusCode::SERVICE_UNAVAILABLE);
}
#[tokio::test]
async fn an_unauthenticated_caller_learns_nothing_about_the_node_role() {
let server = TestServer::builder().replica().start().await;
let response = server
.request(
reqwest::Method::PUT,
&format!("{}/files/a.md", TENANT),
Some(json!({ "author": author(), "content": "x" })),
false,
)
.await;
response.expect_status(StatusCode::UNAUTHORIZED);
}