Files
tern-server/tests/routes.rs
T
Mateusz PstruchaandClaude Opus 5.5 22c55854f3
CI / test (push) Successful in 4m39s
Add the tern.mtps.pl deployment, a registration gate and a healthcheck
deploy/tern.yml is the whole stack as one compose project for
ygg/actions/deploy (deploy/compose on kara): Postgres, the server at the
released version, ygg/static-server serving the matching tern-web
release, and an nginx proxy putting webapp and API (incl. /api/ws) on
one origin on port 11901, with sign-in rate-limited. Kara's nginx
fronts it as tern.mtps.pl. tern-web's release triggers the deploy.

Only the first user may now register unless OPEN_REGISTRATION=true,
so a public deployment isn't open sign-up; an advisory lock keeps two
racing first registrations from both succeeding.

`tern-server healthcheck` backs a Dockerfile HEALTHCHECK, so
`docker compose up --wait` waits for the server to serve — and fails a
deploy whose server can't start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 20:47:32 +02:00

135 lines
4.5 KiB
Rust

//! Route-shape tests.
//!
//! These exist because path-parameter syntax is version-specific and fails
//! silently: axum 0.7 spells a parameter `:id`, while 0.8 spells it `{id}`.
//! Using the wrong one doesn't error at compile time or startup — the router
//! just treats it as a literal segment and every request 404s. Three
//! endpoints shipped broken that way, and a list-endpoint smoke test didn't
//! notice.
//!
//! A 404 here means the route isn't matching. Any other status (401 without
//! credentials, 500 with no database) means it matched, which is all these
//! assert.
use axum::body::Body;
use axum::http::{Request, StatusCode};
use tower::ServiceExt;
/// Builds the real router against a lazily-connected pool. The pool is never
/// used, because every request under test stops at the auth extractor or the
/// router itself — but it has to be a real `PgPool` to construct AppState.
fn test_router() -> axum::Router {
let pool = sqlx::postgres::PgPoolOptions::new()
.connect_lazy("postgres://tern:tern@127.0.0.1:1/placeholder")
.expect("lazy pool");
let state = tern_server::api::AppState {
events: tern_server::events::EventHub::new(pool.clone()),
pusher: tern_server::push::Pusher::with_key(
pool.clone(),
tern_server::push::webpush::Vapid::generate("mailto:test@example.org".into()),
Default::default(),
)
.expect("pusher"),
pool,
cipher: tern_server::crypto::CredentialCipher::new(&[0u8; 32]),
blobs: tern_server::storage::BlobStore::new(std::path::PathBuf::from("/tmp/tern-test")),
session_ttl_hours: 1,
config: tern_server::config::Config {
database_url: String::new(),
bind_addr: String::new(),
credential_key: [0u8; 32],
blob_storage_dir: std::path::PathBuf::from("/tmp/tern-test"),
session_ttl_hours: 1,
trash_retention_days: 30,
open_registration: false,
push: Default::default(),
},
outbox_wake: std::sync::Arc::new(tokio::sync::Notify::new()),
};
tern_server::api::build(state)
}
async fn status_for(method: &str, uri: &str) -> StatusCode {
let request = Request::builder()
.method(method)
.uri(uri)
.body(Body::empty())
.unwrap();
test_router().oneshot(request).await.unwrap().status()
}
#[tokio::test]
async fn parameterised_routes_match() {
let id = "4f6cf11f-da82-49cf-9988-7b450b97a1c3";
for (method, uri) in [
("GET", format!("/api/messages/{id}")),
("GET", format!("/api/threads/{id}")),
("DELETE", format!("/api/accounts/{id}")),
("POST", format!("/api/threads/{id}/read")),
("PUT", format!("/api/outbox/{id}")),
("DELETE", format!("/api/outbox/{id}")),
("POST", format!("/api/outbox/{id}/send")),
("POST", format!("/api/threads/{id}/actions")),
("PUT", format!("/api/folders/{id}")),
("DELETE", format!("/api/folders/{id}")),
("PUT", format!("/api/tags/{id}")),
("DELETE", format!("/api/tags/{id}")),
("PUT", format!("/api/rules/{id}")),
("DELETE", format!("/api/rules/{id}")),
("POST", format!("/api/rules/{id}/run")),
("PUT", format!("/api/devices/{id}")),
("DELETE", format!("/api/devices/{id}")),
("POST", format!("/api/devices/{id}/test")),
] {
let status = status_for(method, &uri).await;
assert_ne!(
status,
StatusCode::NOT_FOUND,
"{method} {uri} did not match any route — check the path parameter syntax \
against the axum version in Cargo.toml"
);
}
}
#[tokio::test]
async fn collection_routes_match() {
for uri in [
"/api/accounts",
"/api/folders",
"/api/messages",
"/api/threads",
"/api/events",
"/api/ws",
"/api/push",
"/api/devices",
"/api/outbox",
"/api/tags",
"/api/rules",
] {
let status = status_for("GET", uri).await;
assert_ne!(
status,
StatusCode::NOT_FOUND,
"GET {uri} did not match any route"
);
}
}
#[tokio::test]
async fn healthz_is_public() {
assert_eq!(status_for("GET", "/healthz").await, StatusCode::OK);
}
#[tokio::test]
async fn unknown_route_still_404s() {
assert_eq!(
status_for("GET", "/api/nope").await,
StatusCode::NOT_FOUND,
"sanity check: a genuinely missing route should 404"
);
}