forge
Explore
Log in

Don't you dare go Hollow

Server clock endpoint and pre-purge expiry

Browse files at this commit

877eead4acb7a11f370fbb9591f29f45a3a1a0f8Ahmet Kaan GümüşParent: b8eeecc
  • test
    • server clock endpoint and pre-purge expiry
Show full message
✅ test(time): server clock endpoint and pre-purge expiry

- /time bracketed between two local reads: same epoch, millis
- expired session row fails auth by clock alone — purge only runs
  at login, so rejection must not depend on the sweep
tests/integration.rsmodified+51 -0
@@ -9,6 +9,7 @@ use common::{
9 app_and_db, create_course, create_exam, enroll, id_of, login, login_as, me_id, mem_app, send,9 app_and_db, create_course, create_exam, enroll, id_of, login, login_as, me_id, mem_app, send,
10 };10 };
11 use hezarfen_backend::domain::session::Session;11 use hezarfen_backend::domain::session::Session;
12+use hezarfen_backend::domain::timestamp::Timestamp;
12 use hezarfen_backend::domain::user::{Password, User, Username};13 use hezarfen_backend::domain::user::{Password, User, Username};
13 use hezarfen_backend::state::AppState;14 use hezarfen_backend::state::AppState;
14 use hezarfen_backend::{build_router, database};15 use hezarfen_backend::{build_router, database};
@@ -25,6 +26,21 @@ async fn health_reports_ok() {
25 assert_eq!(res.body["status"], "ok");26 assert_eq!(res.body["status"], "ok");
26 }27 }
27 28
29+#[tokio::test]
30+async fn time_serves_server_clock_without_auth() {
31+ let app = mem_app().await;
32+
33+ let before = Timestamp::now().as_millis();
34+ let res = send(&app, "GET", "/time", None, None).await;
35+ let after = Timestamp::now().as_millis();
36+
37+ assert_eq!(res.status, StatusCode::OK);
38+ // Bracketed by two local reads: proves it is the same clock in millis,
39+ // not seconds/nanos or some other epoch.
40+ let now = res.body["now"].as_i64().expect("now is an integer");
41+ assert!(before <= now && now <= after);
42+}
43+
28 #[tokio::test]44 #[tokio::test]
29 async fn register_validates_input() {45 async fn register_validates_input() {
30 let app = mem_app().await;46 let app = mem_app().await;
@@ -2535,6 +2551,41 @@ async fn login_purges_expired_sessions() {
2535 );2551 );
2536 }2552 }
2537 2553
2554+/// An expired session row is rejected at auth even while it still exists —
2555+/// expiry must not depend on the purge sweep having run.
2556+#[tokio::test]
2557+async fn expired_session_is_unauthorized_before_any_purge() {
2558+ let (app, db) = app_and_db().await;
2559+ let ali = login(&app, "ali").await;
2560+
2561+ // Copy ali's live session into a second row that expired long ago
2562+ // (epoch millis 1), pointing at the same real user.
2563+ db.query(
2564+ "CREATE session SET \
2565+ user = (SELECT VALUE user FROM ONLY session WHERE token = $live LIMIT 1), \
2566+ token = 'stale-token', expires_at = 1",
2567+ )
2568+ .bind(("live", ali.trim_start_matches("session=").to_string()))
2569+ .await
2570+ .unwrap()
2571+ .check()
2572+ .unwrap();
2573+
2574+ // Row exists, but the clock says no.
2575+ assert!(
2576+ Session::find_by_token("stale-token", &db)
2577+ .await
2578+ .unwrap()
2579+ .is_some()
2580+ );
2581+ assert_eq!(
2582+ send(&app, "GET", "/auth/me", Some("session=stale-token"), None)
2583+ .await
2584+ .status,
2585+ StatusCode::UNAUTHORIZED
2586+ );
2587+}
2588+
2538 /// Regression: `Username::try_new` used to validate the trimmed value but store2589 /// Regression: `Username::try_new` used to validate the trimmed value but store
2539 /// the raw input, so `"ali"`, `"ali "` and `" ali"` were three distinct,2590 /// the raw input, so `"ali"`, `"ali "` and `" ali"` were three distinct,
2540 /// visually identical accounts — and a user who registered with padding could2591 /// visually identical accounts — and a user who registered with padding could