diff --git a/tests/integration.rs b/tests/integration.rs index e7162d6..00e6f8e 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -15424,3 +15424,722 @@ async fn chat_rate_limit_tier_is_configurable_and_zero_disables_it() { ); } } + +// --- appointments ---------------------------------------------------------- + +const HOUR_MS: i64 = 3_600_000; +const WEEK_MS: i64 = 7 * 24 * HOUR_MS; + +/// Publish availability from a full JSON body (no assertion). The response is +/// always an array: one element for a one-off, one per occurrence for a weekly. +async fn publish_slots(app: &axum::Router, cookie: &str, body: serde_json::Value) -> common::Res { + send(app, "POST", "/appointments/slots", Some(cookie), Some(body)).await +} + +/// Publish one slot as `cookie` (asserts 201); returns its id. +async fn publish_slot(app: &axum::Router, cookie: &str, starts_at: i64, ends_at: i64) -> String { + let res = publish_slots( + app, + cookie, + json!({ "starts_at": starts_at, "ends_at": ends_at }), + ) + .await; + assert_eq!( + res.status, + StatusCode::CREATED, + "publish slot: {}", + res.body + ); + res.body[0]["id"].as_str().expect("slot id").to_string() +} + +/// Ask for a meeting on `slot` (no assertion). +async fn book_slot(app: &axum::Router, cookie: &str, slot: &str) -> common::Res { + send( + app, + "POST", + "/appointments", + Some(cookie), + Some(json!({ "slot": slot, "reason": "görüşmek istiyorum" })), + ) + .await +} + +/// Book `slot`, asserting it lands pending; returns the booking id. +async fn book_ok(app: &axum::Router, cookie: &str, slot: &str) -> String { + let res = book_slot(app, cookie, slot).await; + assert_eq!(res.status, StatusCode::CREATED, "book slot: {}", res.body); + assert_eq!(res.body["status"], "pending"); + id_of(&res.body) +} + +/// `PATCH /appointments/{id}/{action}` — approve, reject, cancel, or either +/// answer to a counter-proposal (no assertion, no body). +async fn decide(app: &axum::Router, cookie: &str, id: &str, action: &str) -> common::Res { + send( + app, + "PATCH", + &format!("/appointments/{id}/{action}"), + Some(cookie), + None, + ) + .await +} + +/// Counter-propose another window for `id` (no assertion). +async fn propose( + app: &axum::Router, + cookie: &str, + id: &str, + starts_at: i64, + ends_at: i64, +) -> common::Res { + send( + app, + "PATCH", + &format!("/appointments/{id}/reschedule"), + Some(cookie), + Some(json!({ "starts_at": starts_at, "ends_at": ends_at })), + ) + .await +} + +#[tokio::test] +async fn appointment_book_and_approve_holds_the_slot() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let ali_id = me_id(&app, &ali).await; + let veli = login(&app, "veli").await; + let veli_id = me_id(&app, &veli).await; + let ayse = login(&app, "ayse").await; + let now = Timestamp::now().as_millis(); + + let res = publish_slots( + &app, + &ali, + json!({ "starts_at": now + HOUR_MS, "ends_at": now + 2 * HOUR_MS, + "note": "ofis saati" }), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + assert_eq!(res.body.as_array().expect("array body").len(), 1); + let slot = res.body[0]["id"].as_str().expect("slot id").to_string(); + assert_eq!(res.body[0]["teacher"]["id"], ali_id.as_str()); + assert_eq!(res.body[0]["note"], "ofis saati"); + assert!(res.body[0]["series"].is_null(), "a one-off has no series"); + + // Booking lands pending, echoing the slot's own window. + let res = book_slot(&app, &veli, &slot).await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + assert_eq!(res.body["status"], "pending"); + assert_eq!(res.body["requester"]["id"], veli_id.as_str()); + assert_eq!(res.body["teacher"]["id"], ali_id.as_str()); + assert_eq!(res.body["starts_at"], now + HOUR_MS); + assert!(res.body["decided_by"].is_null(), "pending has no decider"); + let booking = id_of(&res.body); + + // The pending request already holds the slot against everyone else. + let res = book_slot(&app, &ayse, &slot).await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); + + let res = decide(&app, &ali, &booking, "approve").await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(res.body["status"], "approved"); + assert_eq!(res.body["decided_by"]["id"], ali_id.as_str()); + + // Both sides read it back from their own list. + let mine = send(&app, "GET", "/appointments", Some(&veli), None).await; + assert_eq!(common::items(&mine.body).len(), 1); + let inbox = send(&app, "GET", "/appointments", Some(&ali), None).await; + assert_eq!(common::items(&inbox.body)[0]["id"], booking.as_str()); +} + +#[tokio::test] +async fn rejecting_a_booking_frees_the_slot() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let veli = login(&app, "veli").await; + let ayse = login(&app, "ayse").await; + let now = Timestamp::now().as_millis(); + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + + let booking = book_ok(&app, &veli, &slot).await; + let res = decide(&app, &ali, &booking, "reject").await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(res.body["status"], "rejected"); + + // Occupancy counts live rows only, so the next person may take it. + let res = book_slot(&app, &ayse, &slot).await; + assert_eq!( + res.status, + StatusCode::CREATED, + "a rejected booking must not keep the seat: {}", + res.body + ); + // And the rejected one cannot be decided twice. + let res = decide(&app, &ali, &booking, "approve").await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); +} + +#[tokio::test] +async fn approval_refuses_a_teacher_double_booking() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let veli = login(&app, "veli").await; + let ayse = login(&app, "ayse").await; + let now = Timestamp::now().as_millis(); + + // Two overlapping windows on the same calendar, one requester each: the + // request is fine (a wish), the second approval is not. + let first = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + let second = publish_slot(&app, &ali, now + HOUR_MS + 600_000, now + 3 * HOUR_MS).await; + let one = book_ok(&app, &veli, &first).await; + let two = book_ok(&app, &ayse, &second).await; + + assert_eq!( + decide(&app, &ali, &one, "approve").await.status, + StatusCode::OK + ); + let res = decide(&app, &ali, &two, "approve").await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); + // Refused, not half-applied: it is still pending and still decidable. + let res = decide(&app, &ali, &two, "reject").await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); +} + +#[tokio::test] +async fn approval_refuses_a_requester_double_booking() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let mert = login_as(&app, &db, "mert", "teacher").await; + let veli = login(&app, "veli").await; + let now = Timestamp::now().as_millis(); + + // One student, two different teachers, overlapping times. Both requests + // stand (nothing is committed yet); the second approval collides. + let first = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + let second = publish_slot(&app, &mert, now + HOUR_MS + 600_000, now + 3 * HOUR_MS).await; + let one = book_ok(&app, &veli, &first).await; + let two = book_ok(&app, &veli, &second).await; + + assert_eq!( + decide(&app, &ali, &one, "approve").await.status, + StatusCode::OK + ); + let res = decide(&app, &mert, &two, "approve").await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); + + // Booking a fresh overlap once one is committed is refused up front. + let third = publish_slot(&app, &mert, now + HOUR_MS, now + 2 * HOUR_MS).await; + let res = book_slot(&app, &veli, &third).await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); +} + +#[tokio::test] +async fn touching_windows_never_collide() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let veli = login(&app, "veli").await; + let now = Timestamp::now().as_millis(); + + // Half-open windows: 10:00–11:00 and 11:00–12:00 are back-to-back, not a + // clash — for the teacher and for the student alike. + let first = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + let second = publish_slot(&app, &ali, now + 2 * HOUR_MS, now + 3 * HOUR_MS).await; + let one = book_ok(&app, &veli, &first).await; + let two = book_ok(&app, &veli, &second).await; + + for booking in [&one, &two] { + let res = decide(&app, &ali, booking, "approve").await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(res.body["status"], "approved"); + } +} + +#[tokio::test] +async fn reschedule_returns_to_pending_until_the_requester_accepts() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let ali_id = me_id(&app, &ali).await; + let veli = login(&app, "veli").await; + let ayse = login(&app, "ayse").await; + let now = Timestamp::now().as_millis(); + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + let booking = book_ok(&app, &veli, &slot).await; + assert_eq!( + decide(&app, &ali, &booking, "approve").await.status, + StatusCode::OK + ); + + // A counter-proposal un-commits the approved meeting rather than moving it + // silently. + let res = propose(&app, &ali, &booking, now + 3 * HOUR_MS, now + 4 * HOUR_MS).await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(res.body["status"], "pending"); + assert!(res.body["decided_by"].is_null(), "approval was withdrawn"); + assert_eq!(res.body["proposed_starts_at"], now + 3 * HOUR_MS); + assert_eq!(res.body["proposed_ends_at"], now + 4 * HOUR_MS); + assert_eq!(res.body["proposed_by"]["id"], ali_id.as_str()); + // The effective window already reads as the proposed one. + assert_eq!(res.body["starts_at"], now + 3 * HOUR_MS); + + // Only the requester may answer it. + let res = decide(&app, &ayse, &booking, "reschedule/accept").await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); + let res = decide(&app, &ali, &booking, "reschedule/accept").await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); + + let res = decide(&app, &veli, &booking, "reschedule/accept").await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(res.body["status"], "approved"); + assert_eq!(res.body["starts_at"], now + 3 * HOUR_MS); + assert_eq!(res.body["ends_at"], now + 4 * HOUR_MS); +} + +#[tokio::test] +async fn a_started_proposal_is_refused() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let veli = login(&app, "veli").await; + let now = Timestamp::now().as_millis(); + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + let booking = book_ok(&app, &veli, &slot).await; + + // A proposal 30s into the past is inside the publishing grace, so the times + // themselves validate — but the window is already underway, and a meeting + // that began is one `cancel` refuses to undo. Refused at the proposal, not + // left for the requester to agree to. + let res = propose(&app, &ali, &booking, now - 30_000, now + HOUR_MS).await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); + // Refused, not half-applied: the teacher can still propose a real time. + let res = send(&app, "GET", "/appointments", Some(&veli), None).await; + assert_eq!(common::items(&res.body)[0]["status"], "pending"); + assert!( + common::items(&res.body)[0]["proposed_starts_at"].is_null(), + "a refused proposal leaves nothing behind" + ); + + assert_eq!( + propose(&app, &ali, &booking, now + 3 * HOUR_MS, now + 4 * HOUR_MS) + .await + .status, + StatusCode::OK + ); + let res = decide(&app, &veli, &booking, "reschedule/accept").await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(res.body["status"], "approved"); +} + +#[tokio::test] +async fn declining_a_reschedule_cancels_the_booking() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let veli = login(&app, "veli").await; + let ayse = login(&app, "ayse").await; + let now = Timestamp::now().as_millis(); + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + let booking = book_ok(&app, &veli, &slot).await; + + // Nothing proposed yet: there is nothing to decline. + let res = decide(&app, &veli, &booking, "reschedule/decline").await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); + + assert_eq!( + propose(&app, &ali, &booking, now + 3 * HOUR_MS, now + 4 * HOUR_MS) + .await + .status, + StatusCode::OK + ); + let res = decide(&app, &veli, &booking, "reschedule/decline").await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(res.body["status"], "cancelled"); + // The refused proposal stays readable on the cancelled row. + assert_eq!(res.body["proposed_starts_at"], now + 3 * HOUR_MS); + + // And the slot is free again. + let res = book_slot(&app, &ayse, &slot).await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); +} + +/// Declining cancels, so it answers to the cancel deadline. The guard lives in +/// `Appointment::cancel` itself — when it sat in the cancel *handler*, decline +/// went straight past it and called off a meeting already underway. +#[tokio::test] +async fn declining_a_started_reschedule_is_refused() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let veli = login(&app, "veli").await; + let now = Timestamp::now().as_millis(); + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + let booking = book_ok(&app, &veli, &slot).await; + + // A proposal that is still ahead when made, and underway a moment later: + // the only way an effective window opens with the booking still live. + let opens_at = Timestamp::now().as_millis() + 700; + assert_eq!( + propose(&app, &ali, &booking, opens_at, opens_at + HOUR_MS) + .await + .status, + StatusCode::OK + ); + tokio::time::sleep(std::time::Duration::from_millis(800)).await; + let res = decide(&app, &veli, &booking, "reschedule/decline").await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); + // The plain cancel door answers the same, and the booking survives both. + let res = decide(&app, &veli, &booking, "cancel").await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); + let res = send(&app, "GET", "/appointments", Some(&veli), None).await; + assert_eq!(common::items(&res.body)[0]["status"], "pending"); + + // Moved back into the future, declining cancels as it always did. + assert_eq!( + propose(&app, &ali, &booking, now + 3 * HOUR_MS, now + 4 * HOUR_MS) + .await + .status, + StatusCode::OK + ); + let res = decide(&app, &veli, &booking, "reschedule/decline").await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(res.body["status"], "cancelled"); +} + +#[tokio::test] +async fn accepting_a_reschedule_re_runs_the_overlap_guard() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let mert = login_as(&app, &db, "mert", "teacher").await; + let veli = login(&app, "veli").await; + let now = Timestamp::now().as_millis(); + + // Committed with ali at +5h. + let taken = publish_slot(&app, &ali, now + 5 * HOUR_MS, now + 6 * HOUR_MS).await; + let committed = book_ok(&app, &veli, &taken).await; + assert_eq!( + decide(&app, &ali, &committed, "approve").await.status, + StatusCode::OK + ); + + // Mert counter-proposes straight onto that hour; accepting would commit + // the student twice, so it is refused. + let slot = publish_slot(&app, &mert, now + HOUR_MS, now + 2 * HOUR_MS).await; + let booking = book_ok(&app, &veli, &slot).await; + assert_eq!( + propose(&app, &mert, &booking, now + 5 * HOUR_MS, now + 6 * HOUR_MS) + .await + .status, + StatusCode::OK + ); + let res = decide(&app, &veli, &booking, "reschedule/accept").await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); + assert_eq!( + send(&app, "GET", "/appointments", Some(&veli), None) + .await + .body["items"][0]["status"], + "pending", + "a refused acceptance leaves the booking pending" + ); +} + +#[tokio::test] +async fn either_side_cancels_until_the_window_opens() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let veli = login(&app, "veli").await; + let ayse = login(&app, "ayse").await; + let now = Timestamp::now().as_millis(); + + // The requester's own call, on an approved meeting. + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + let booking = book_ok(&app, &veli, &slot).await; + assert_eq!( + decide(&app, &ali, &booking, "approve").await.status, + StatusCode::OK + ); + let res = decide(&app, &veli, &booking, "cancel").await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(res.body["status"], "cancelled"); + + // The slot's teacher may too. + let booking = book_ok(&app, &ayse, &slot).await; + let res = decide(&app, &ali, &booking, "cancel").await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(res.body["status"], "cancelled"); + // An outsider may not, and a settled booking cannot be cancelled twice. + let booking = book_ok(&app, &veli, &slot).await; + let res = decide(&app, &ayse, &booking, "cancel").await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); + assert_eq!( + decide(&app, &veli, &booking, "cancel").await.status, + StatusCode::OK + ); + let res = decide(&app, &veli, &booking, "cancel").await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); +} + +#[tokio::test] +async fn a_started_slot_cannot_be_booked() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let veli = login(&app, "veli").await; + let now = Timestamp::now().as_millis(); + + // A window that opened 30s ago is inside the publishing grace, so the slot + // is created — but the meeting has begun, and a meeting that began is + // history, not a plan. Booking it would land a row `cancel` refuses to + // undo, stuck for good. + let underway = publish_slot(&app, &ali, now - 30_000, now + HOUR_MS).await; + let res = book_slot(&app, &veli, &underway).await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); + + // A window still ahead is untouched by the guard. + let upcoming = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + let res = book_slot(&app, &veli, &upcoming).await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); +} + +#[tokio::test] +async fn slot_delete_waits_for_the_booking_to_settle() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let veli = login(&app, "veli").await; + let now = Timestamp::now().as_millis(); + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + let booking = book_ok(&app, &veli, &slot).await; + + let uri = format!("/appointments/slots/{slot}"); + let res = send(&app, "DELETE", &uri, Some(&ali), None).await; + assert_eq!( + res.status, + StatusCode::CONFLICT, + "someone is waiting on this slot: {}", + res.body + ); + assert_eq!( + decide(&app, &veli, &booking, "cancel").await.status, + StatusCode::OK + ); + let res = send(&app, "DELETE", &uri, Some(&ali), None).await; + assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); + assert_eq!( + send(&app, "DELETE", &uri, Some(&ali), None).await.status, + StatusCode::NOT_FOUND + ); +} + +#[tokio::test] +async fn weekly_slots_share_a_series_and_are_dropped_together() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let veli = login(&app, "veli").await; + let now = Timestamp::now().as_millis(); + let starts_at = now + HOUR_MS; + + // `until` is inclusive, so three weeks out is four occurrences. + let res = publish_slots( + &app, + &ali, + json!({ "starts_at": starts_at, "ends_at": starts_at + HOUR_MS, + "repeat_weekly": true, "until": starts_at + 3 * WEEK_MS }), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let slots = res.body.as_array().expect("array body").clone(); + assert_eq!(slots.len(), 4); + let series = slots[0]["series"].as_str().expect("series id").to_string(); + for (week, slot) in slots.iter().enumerate() { + assert_eq!(slot["series"], series.as_str(), "one publish, one series"); + assert_eq!(slot["starts_at"], starts_at + week as i64 * WEEK_MS); + } + + // `repeat_weekly` without an end, and past the 52-occurrence cap. + let res = publish_slots( + &app, + &ali, + json!({ "starts_at": starts_at, "ends_at": starts_at + HOUR_MS, + "repeat_weekly": true }), + ) + .await; + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); + let res = publish_slots( + &app, + &ali, + json!({ "starts_at": starts_at, "ends_at": starts_at + HOUR_MS, + "repeat_weekly": true, "until": starts_at + 52 * WEEK_MS }), + ) + .await; + assert_eq!( + res.status, + StatusCode::BAD_REQUEST, + "53 occurrences: {}", + res.body + ); + + // An end at `i64::MAX` clears every gate — future, not inverted, 52 + // occurrences — and then overflows on the first weekly shift. Unchecked it + // wrapped the end below the start, and an inverted window never overlaps + // anything, so the double-booking guard would have gone blind. + let res = publish_slots( + &app, + &ali, + json!({ "starts_at": starts_at, "ends_at": i64::MAX, + "repeat_weekly": true, "until": starts_at + 51 * WEEK_MS }), + ) + .await; + assert_eq!( + res.status, + StatusCode::BAD_REQUEST, + "a shift that overflows: {}", + res.body + ); + + // All-or-nothing: one waiting requester anywhere in the series blocks it. + let booked = slots[1]["id"].as_str().expect("slot id"); + let booking = book_ok(&app, &veli, booked).await; + let uri = format!("/appointments/slots/series/{series}"); + let res = send(&app, "DELETE", &uri, Some(&ali), None).await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); + + assert_eq!( + decide(&app, &ali, &booking, "reject").await.status, + StatusCode::OK + ); + let res = send(&app, "DELETE", &uri, Some(&ali), None).await; + assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); + let res = send(&app, "GET", "/appointments/slots", Some(&ali), None).await; + assert_eq!(common::total(&res.body), 0, "the whole series went"); + assert_eq!( + send(&app, "DELETE", &uri, Some(&ali), None).await.status, + StatusCode::NOT_FOUND + ); +} + +#[tokio::test] +async fn only_students_and_parents_book_appointments() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let mert = login_as(&app, &db, "mert", "teacher").await; + let mudur = login_as(&app, &db, "mudur", "manager").await; + let admin = login_as(&app, &db, "yonetici", "admin").await; + let anne = login_as(&app, &db, "anne", "parent").await; + let veli = login(&app, "veli").await; + let now = Timestamp::now().as_millis(); + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + + // Staff arrange between themselves off this API. + for (who, label) in [(&mert, "teacher"), (&mudur, "manager"), (&admin, "admin")] { + let res = book_slot(&app, who, &slot).await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "{label}: {}", res.body); + } + // A parent books for themselves — the parent-teacher conference. + let res = book_slot(&app, &anne, &slot).await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let second = publish_slot(&app, &ali, now + 3 * HOUR_MS, now + 4 * HOUR_MS).await; + let res = book_slot(&app, &veli, &second).await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); +} + +#[tokio::test] +async fn only_the_slots_teacher_decides_its_bookings() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let mert = login_as(&app, &db, "mert", "teacher").await; + let mudur = login_as(&app, &db, "mudur", "manager").await; + let veli = login(&app, "veli").await; + let now = Timestamp::now().as_millis(); + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + let booking = book_ok(&app, &veli, &slot).await; + + // Another teacher's calendar is not theirs to run. + for action in ["approve", "reject"] { + let res = decide(&app, &mert, &booking, action).await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "{action}: {}", res.body); + } + let res = propose(&app, &mert, &booking, now + 3 * HOUR_MS, now + 4 * HOUR_MS).await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); + let res = send( + &app, + "DELETE", + &format!("/appointments/slots/{slot}"), + Some(&mert), + None, + ) + .await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); + // The requester cannot approve their own request either. + assert_eq!( + decide(&app, &veli, &booking, "approve").await.status, + StatusCode::FORBIDDEN + ); + // A manager may decide anyone's. + let res = decide(&app, &mudur, &booking, "approve").await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(res.body["status"], "approved"); +} + +#[tokio::test] +async fn appointment_times_must_be_sane_and_future() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let veli = login(&app, "veli").await; + let now = Timestamp::now().as_millis(); + + // Past (beyond the 60s grace), empty, and inverted windows. + for (body, label) in [ + ( + json!({ "starts_at": now - 10 * HOUR_MS, "ends_at": now - 9 * HOUR_MS }), + "past", + ), + ( + json!({ "starts_at": now + HOUR_MS, "ends_at": now + HOUR_MS }), + "empty", + ), + ( + json!({ "starts_at": now + 2 * HOUR_MS, "ends_at": now + HOUR_MS }), + "inverted", + ), + ] { + let res = publish_slots(&app, &ali, body).await; + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{label}: {}", res.body); + } + + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + let booking = book_ok(&app, &veli, &slot).await; + // A counter-proposal answers to the same rules. + for (starts_at, ends_at, label) in [ + (now - 10 * HOUR_MS, now - 9 * HOUR_MS, "past"), + (now + HOUR_MS, now + HOUR_MS, "empty"), + (now + 2 * HOUR_MS, now + HOUR_MS, "inverted"), + ] { + let res = propose(&app, &ali, &booking, starts_at, ends_at).await; + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{label}: {}", res.body); + } + // A blank reason is refused too — the teacher decides on it. + let res = send( + &app, + "POST", + "/appointments", + Some(&veli), + Some(json!({ "slot": slot, "reason": "" })), + ) + .await; + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); +} + +#[tokio::test] +async fn a_demoted_teachers_slots_go_inert() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let veli = login(&app, "veli").await; + let now = Timestamp::now().as_millis(); + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; + + let res = send(&app, "GET", "/appointments/slots", Some(&veli), None).await; + assert_eq!(common::items(&res.body).len(), 1, "{}", res.body); + + // The row survives the demotion; the offer does not. + set_role(&db, "ali", "student").await; + let res = send(&app, "GET", "/appointments/slots", Some(&veli), None).await; + assert_eq!(common::total(&res.body), 0, "an inert slot is not offered"); + let res = book_slot(&app, &veli, &slot).await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); +} diff --git a/tests/pagination.rs b/tests/pagination.rs index 5979acf..da0db06 100644 --- a/tests/pagination.rs +++ b/tests/pagination.rs @@ -132,6 +132,189 @@ async fn paged_roster_still_embeds_people_on_the_page() { } } +/// Far enough ahead to clear the not-in-the-past guard without touching the +/// clock, so the appointment lists below never race the wall clock. +const SLOT_START: i64 = 1_900_000_000_000; +const WEEK: i64 = 7 * 24 * 60 * 60 * 1000; + +/// Publish `n` weekly occurrences as `teacher`; returns their ids, earliest +/// first — the order both appointment lists page in. +async fn publish_slots(app: &axum::Router, teacher: &str, n: i64) -> Vec { + let res = send( + app, + "POST", + "/appointments/slots", + Some(teacher), + Some(json!({ + "starts_at": SLOT_START, + "ends_at": SLOT_START + 1_800_000, + "repeat_weekly": true, + "until": SLOT_START + (n - 1) * WEEK, + })), + ) + .await; + assert_eq!( + res.status, + StatusCode::CREATED, + "publish slots: {}", + res.body + ); + let slots = res + .body + .as_array() + .expect("publish returns an array") + .clone(); + assert_eq!(slots.len() as i64, n, "one row per week"); + slots + .iter() + .map(|slot| slot["id"].as_str().unwrap().to_string()) + .collect() +} + +#[tokio::test] +async fn appointment_slots_are_paged() { + let (app, db) = app_and_db().await; + let teacher = login_as(&app, &db, "ali", "teacher").await; + publish_slots(&app, &teacher, 6).await; + + // Unpaged: the whole calendar, `limit` echoes null, `total` counts it all. + let res = send(&app, "GET", "/appointments/slots", Some(&teacher), None).await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(total(&res.body), 6); + assert_eq!(items(&res.body).len(), 6, "omitting limit returns them all"); + assert!(res.body["limit"].is_null()); + assert_eq!(res.body["offset"], 0); + + // A window: `total` still carries the unpaged count. + let res = send( + &app, + "GET", + "/appointments/slots?limit=2&offset=4", + Some(&teacher), + None, + ) + .await; + assert_eq!(total(&res.body), 6, "total is the unpaged count"); + assert_eq!(items(&res.body).len(), 2); + assert_eq!(res.body["limit"], 2); + assert_eq!(res.body["offset"], 4); + + // Past the end — empty page, honest total, and the person join still rides + // along on the rows that do come back. + let res = send( + &app, + "GET", + "/appointments/slots?limit=2&offset=99", + Some(&teacher), + None, + ) + .await; + assert_eq!(res.status, StatusCode::OK); + assert!(items(&res.body).is_empty()); + assert_eq!(total(&res.body), 6); + + // A student browses the same calendar — bookable slots, paged identically. + let student = login(&app, "ayse").await; + let res = send( + &app, + "GET", + "/appointments/slots?limit=3&offset=0", + Some(&student), + None, + ) + .await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(total(&res.body), 6); + let page = items(&res.body); + assert_eq!(page.len(), 3); + for slot in page { + assert_eq!( + slot["teacher"]["username"], "ali", + "teacher ref present on the page: {slot}" + ); + } +} + +#[tokio::test] +async fn appointments_are_paged() { + let (app, db) = app_and_db().await; + let teacher = login_as(&app, &db, "ali", "teacher").await; + let student = login(&app, "ayse").await; + // Five weekly slots, all booked by the same student. Distinct weeks, so no + // booking collides with another. + let slots = publish_slots(&app, &teacher, 5).await; + for slot in &slots { + let res = send( + &app, + "POST", + "/appointments", + Some(&student), + Some(json!({ "slot": slot, "reason": "ödev" })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "book {slot}: {}", res.body); + } + + // Unpaged: every booking the student asked for. + let res = send(&app, "GET", "/appointments", Some(&student), None).await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(total(&res.body), 5); + assert_eq!(items(&res.body).len(), 5, "omitting limit returns them all"); + assert!(res.body["limit"].is_null()); + assert_eq!(res.body["offset"], 0); + + // Consecutive windows are disjoint and share one total. + let res = send( + &app, + "GET", + "/appointments?limit=2&offset=0", + Some(&student), + None, + ) + .await; + assert_eq!(total(&res.body), 5, "total is the unpaged count"); + assert_eq!(res.body["limit"], 2); + let page1: Vec<_> = items(&res.body).iter().map(|a| a["id"].clone()).collect(); + let res = send( + &app, + "GET", + "/appointments?limit=2&offset=2", + Some(&student), + None, + ) + .await; + let page2: Vec<_> = items(&res.body).iter().map(|a| a["id"].clone()).collect(); + assert!(page2.iter().all(|id| !page1.contains(id))); + + // The tail is the remainder, and the slot/person join runs over the page. + let res = send( + &app, + "GET", + "/appointments?limit=2&offset=4", + Some(&student), + None, + ) + .await; + assert_eq!(total(&res.body), 5); + let page = items(&res.body); + assert_eq!(page.len(), 1, "offset 4 of 5 leaves 1"); + assert_eq!(page[0]["requester"]["username"], "ayse"); + assert_eq!(page[0]["teacher"]["username"], "ali"); + + // The teacher's inbox is the same five bookings, paged the same way. + let res = send( + &app, + "GET", + "/appointments?limit=3&offset=0", + Some(&teacher), + None, + ) + .await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(total(&res.body), 5, "the teacher's inbox counts all five"); + assert_eq!(items(&res.body).len(), 3); +} + #[tokio::test] async fn user_search_is_paged_not_capped() { // `/users/search` used to hard-cap at 10 rows; it now pages like the rest. diff --git a/tests/persistence.rs b/tests/persistence.rs index cd9de35..7ff7209 100644 --- a/tests/persistence.rs +++ b/tests/persistence.rs @@ -574,6 +574,160 @@ async fn legacy_exams_backfill_to_published() { assert_eq!(common::items(&listed.body).len(), 1); } +/// A recurring publish and the booking sitting on it survive a second boot: +/// the slot's `note` and shared `series`, and the booking's counter-proposal +/// triple, `decided_by` and `status`, all come back untouched by the +/// re-applied migration. Every time in this schema is a unix-millisecond +/// `TYPE int` — never a `datetime` — so the round trip is asserted on odd +/// millisecond values, which any lossy conversion would round off. +#[tokio::test] +async fn appointments_survive_remigration() { + /// Far enough ahead to clear `check_not_past` without touching the clock, + /// and deliberately not a round number of seconds. + const START: i64 = 1_900_000_000_123; + const END: i64 = 1_900_000_003_777; + const WEEK: i64 = 7 * 24 * 60 * 60 * 1000; + const PROPOSED_START: i64 = START + 5_000_001; + const PROPOSED_END: i64 = START + 8_000_003; + + let (app, db) = common::app_and_db().await; + let teacher_creds = json!({ "username": "ali", "password": "secret1" }); + let student_creds = json!({ "username": "ayse", "password": "secret1" }); + for creds in [&teacher_creds, &student_creds] { + assert_eq!( + send(&app, "POST", "/auth/register", None, Some((*creds).clone())) + .await + .status, + StatusCode::CREATED + ); + } + set_role(&db, "ali", "teacher").await; + let teacher = send( + &app, + "POST", + "/auth/login", + None, + Some(teacher_creds.clone()), + ) + .await + .cookie + .unwrap(); + let student = send( + &app, + "POST", + "/auth/login", + None, + Some(student_creds.clone()), + ) + .await + .cookie + .unwrap(); + + // A weekly publish: three occurrences sharing one series id and one note. + let res = send( + &app, + "POST", + "/appointments/slots", + Some(&teacher), + Some(json!({ + "starts_at": START, + "ends_at": END, + "note": "veli görüşmesi", + "repeat_weekly": true, + "until": START + 2 * WEEK, + })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let published = res + .body + .as_array() + .expect("publish returns an array") + .clone(); + assert_eq!(published.len(), 3, "{}", res.body); + let slot_id = published[0]["id"].as_str().unwrap().to_string(); + let series = published[0]["series"].as_str().unwrap().to_string(); + let slot_created_at = published[0]["created_at"].as_i64().unwrap(); + + // Book the first occurrence, counter-propose another time, accept it — the + // row then carries every optional column at once: a proposal triple, a + // decider, and `approved`. + let res = send( + &app, + "POST", + "/appointments", + Some(&student), + Some(json!({ "slot": slot_id, "reason": "ödev" })), + ) + .await; + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); + let appointment = common::id_of(&res.body); + let created_at = res.body["created_at"].as_i64().unwrap(); + let res = send( + &app, + "PATCH", + &format!("/appointments/{appointment}/reschedule"), + Some(&teacher), + Some(json!({ "starts_at": PROPOSED_START, "ends_at": PROPOSED_END })), + ) + .await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + let res = send( + &app, + "PATCH", + &format!("/appointments/{appointment}/reschedule/accept"), + Some(&student), + None, + ) + .await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + + // Second boot: migration re-applied over live data. + let app = reboot(&db).await; + let teacher = send(&app, "POST", "/auth/login", None, Some(teacher_creds)) + .await + .cookie + .unwrap(); + let student = send(&app, "POST", "/auth/login", None, Some(student_creds)) + .await + .cookie + .unwrap(); + + let res = send(&app, "GET", "/appointments/slots", Some(&teacher), None).await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(common::total(&res.body), 3, "{}", res.body); + let slots = common::items(&res.body); + assert_eq!(slots[0]["id"], slot_id.as_str()); + assert_eq!(slots[0]["starts_at"], START, "exact unix-ms, not rounded"); + assert_eq!(slots[0]["ends_at"], END); + assert_eq!(slots[0]["note"], "veli görüşmesi"); + assert_eq!(slots[0]["created_at"], slot_created_at); + for (week, slot) in slots.iter().enumerate() { + assert_eq!(slot["series"], series.as_str(), "one series, all weeks"); + assert_eq!(slot["starts_at"], START + week as i64 * WEEK); + assert_eq!(slot["ends_at"], END + week as i64 * WEEK); + } + + let res = send(&app, "GET", "/appointments", Some(&student), None).await; + assert_eq!(res.status, StatusCode::OK, "{}", res.body); + assert_eq!(common::total(&res.body), 1, "{}", res.body); + let booking = &common::items(&res.body)[0]; + assert_eq!(booking["id"], appointment.as_str()); + assert_eq!(booking["slot"], slot_id.as_str()); + assert_eq!(booking["status"], "approved"); + assert_eq!(booking["reason"], "ödev"); + assert_eq!(booking["proposed_starts_at"], PROPOSED_START); + assert_eq!(booking["proposed_ends_at"], PROPOSED_END); + assert_eq!(booking["proposed_by"]["username"], "ali"); + assert_eq!(booking["decided_by"]["username"], "ayse"); + // The accepted proposal *is* the meeting's time; the slot's own window lost. + assert_eq!(booking["starts_at"], PROPOSED_START); + assert_eq!(booking["ends_at"], PROPOSED_END); + assert_eq!(booking["teacher"]["username"], "ali"); + assert_eq!(booking["requester"]["username"], "ayse"); + assert_eq!(booking["created_at"], created_at); +} + /// A chatbot thread and every turn in it die together: deleting the /// conversation cascades its `chat_message` rows in one transaction, so no /// orphan survives a reboot. And the thread is owner-scoped end to end — a