Place search only for logged-in users; test that routes need login
- /api/geo/countries and /api/geo/places (the frontend's pass-through to the horoscope-api) now answer 401 without a session, so other apps cannot use the place search. - New backend test: every route in routes/mod.rs except the listed public ones (login, config, catalog, shared links, invitation pages, waitlist, unsubscribe) answers 401 without a session. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -632,6 +632,58 @@ async fn export_and_delete_account(pool: PgPool) {
|
||||
assert_eq!(app.get("/v1/me", Some(&bela)).await.status, StatusCode::OK);
|
||||
}
|
||||
|
||||
/// Only these answer without a session: login and sign-up, the public
|
||||
/// config and catalog, shared links, invitation pages, the waitlist and
|
||||
/// unsubscribing. Every other route (charts, calculations, analyses) needs a
|
||||
/// logged-in user, so other apps cannot use them.
|
||||
const PUBLIC_ROUTES: [&str; 14] = [
|
||||
"/healthz",
|
||||
"/v1/config",
|
||||
"/v1/packages",
|
||||
"/v1/auth/register",
|
||||
"/v1/auth/login",
|
||||
"/v1/auth/google",
|
||||
"/v1/auth/verify-email",
|
||||
"/v1/auth/password-reset",
|
||||
"/v1/auth/password-reset/confirm",
|
||||
"/v1/public/shares/{slug}",
|
||||
"/v1/public/invites/{token}",
|
||||
"/v1/public/invitations/unsubscribe",
|
||||
"/v1/public/invitations/{token}",
|
||||
"/v1/public/waitlist",
|
||||
];
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn everything_else_needs_login(pool: PgPool) {
|
||||
let app = TestApp::new(pool).await;
|
||||
let source = include_str!("routes/mod.rs");
|
||||
let id = uuid::Uuid::now_v7().to_string();
|
||||
let mut checked = 0;
|
||||
for line in source.lines().map(str::trim).filter(|l| l.starts_with(".route(\"")) {
|
||||
let path = line.split('"').nth(1).unwrap();
|
||||
if PUBLIC_ROUTES.contains(&path) {
|
||||
continue;
|
||||
}
|
||||
let uri = path
|
||||
.split('/')
|
||||
.map(|seg| if seg.starts_with('{') { id.as_str() } else { seg })
|
||||
.collect::<Vec<_>>()
|
||||
.join("/");
|
||||
for (name, method) in
|
||||
[("get(", "GET"), ("post(", "POST"), ("patch(", "PATCH"), ("put(", "PUT"), ("delete(", "DELETE")]
|
||||
{
|
||||
if !line.contains(name) {
|
||||
continue;
|
||||
}
|
||||
let body = (method != "GET" && method != "DELETE").then(|| json!({}));
|
||||
let r = app.call(method, &uri, None, body).await;
|
||||
assert_eq!(r.status, StatusCode::UNAUTHORIZED, "{method} {uri} answers without login: {}", r.body);
|
||||
checked += 1;
|
||||
}
|
||||
}
|
||||
assert!(checked > 40, "{checked}");
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn public_config_and_health(pool: PgPool) {
|
||||
let app = TestApp::new(pool).await;
|
||||
|
||||
Reference in New Issue
Block a user