Files
tern-server/tests/routes.rs
T
Mateusz PstruchaandClaude Opus 5.5 56e2c89999
CI / test (push) Failing after 3m23s
Allow editing an account
PUT /api/accounts/:id replaces an account's settings; the password is
kept unless a new one is given. Accounts now have an updated_at, and
the sync manager restarts a task whose account changed since it
started — the task holds its own copy of the settings. Its status is
reset to pending so the old error doesn't linger.

Mail already synced stays when it's still the same mailbox (new
password, renamed host); a genuinely different mailbox shows up as a
UIDVALIDITY change on connect and re-syncs, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 14:11:10 +02:00

164 lines
5.6 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()),
sync_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}")),
("PUT", 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"
);
}
/// The deployed webapp lives on another origin and sends `Authorization`.
/// Browsers don't let `Access-Control-Allow-Headers: *` cover that header,
/// so it has to be named in the preflight answer.
#[tokio::test]
async fn cors_preflight_allows_the_authorization_header() {
let request = Request::builder()
.method("OPTIONS")
.uri("/api/threads")
.header("Origin", "https://tern.mtps.pl")
.header("Access-Control-Request-Method", "GET")
.header("Access-Control-Request-Headers", "authorization")
.body(Body::empty())
.unwrap();
let response = test_router().oneshot(request).await.unwrap();
assert!(response.status().is_success());
let allowed = response
.headers()
.get("access-control-allow-headers")
.and_then(|v| v.to_str().ok())
.unwrap_or_default()
.to_lowercase();
assert!(
allowed.contains("authorization"),
"allowed headers: {allowed:?}"
);
}