From f1a8cf3587cc8a5d02930e8eae98918b463457a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 24 Jul 2026 17:04:20 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20appointments=20=E2=80=94=20?= =?UTF-8?q?only=20the=20requester=20may=20cancel=20-=20tighten=20the=20can?= =?UTF-8?q?cel=20guard=20to=20requester-only:=20a=20slot-owning=20teacher?= =?UTF-8?q?=20or=20a=20=20=20manager/admin=20can=20no=20longer=20cancel=20?= =?UTF-8?q?a=20booking=20(403);=20they=20end=20it=20by=20=20=20rejecting?= =?UTF-8?q?=20while=20pending=20or=20by=20rescheduling=20instead=20-=20dro?= =?UTF-8?q?p=20the=20now-dead=20slot=20read=20the=20can=5Fmanage=20check?= =?UTF-8?q?=20needed=20-=20tests:=20teacher=5Fcannot=5Fcancel,=20manager?= =?UTF-8?q?=5Fcannot=5Fcancel,=20student=5Fcannot=5Freject,=20=20=20and=20?= =?UTF-8?q?only=5Fthe=5Frequester=5Fcancels=20(reworked=20from=20either=5F?= =?UTF-8?q?side=5Fcancels)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/web/appointments.rs | 11 +++--- tests/integration.rs | 77 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 73 insertions(+), 15 deletions(-) diff --git a/src/web/appointments.rs b/src/web/appointments.rs index 9bfd53b..d1c3713 100644 --- a/src/web/appointments.rs +++ b/src/web/appointments.rs @@ -626,7 +626,7 @@ async fn reject( (status = 200, description = "Cancelled", body = AppointmentResponse), (status = 400, description = "Reason is over-long", body = ErrorResponse), (status = 401, description = "Not authenticated", body = ErrorResponse), - (status = 403, description = "Neither the requester nor the slot's teacher", body = ErrorResponse), + (status = 403, description = "Not the requester", body = ErrorResponse), (status = 404, description = "Not found", body = ErrorResponse), (status = 409, description = "Already settled, or the appointment has already started", body = ErrorResponse), ), @@ -641,12 +641,11 @@ async fn cancel( let appointment = Appointment::read(&id, &st.db) .await? .ok_or(AppError::NotFound)?; - let slot = AppointmentSlot::read(appointment.get_slot(), &st.db) - .await? - .ok_or(AppError::NotFound)?; - if appointment.get_requester() != user.get_id() && !can_manage(&slot, &user) { + // Only the requester (student/parent) may cancel. Teachers/managers end a + // booking by rejecting (while pending) or rescheduling — never cancelling. + if appointment.get_requester() != user.get_id() { return Err(AppError::Forbidden( - "only the requester or the slot's teacher can cancel this appointment", + "only the requester can cancel this appointment", )); } // A blank or absent reason records none; a present one is validated (400 if diff --git a/tests/integration.rs b/tests/integration.rs index 26908a6..47632c9 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -15831,8 +15831,10 @@ async fn accepting_a_reschedule_re_runs_the_overlap_guard() { ); } +/// Only the requester (student/parent) may cancel a booking — not the slot's +/// teacher, not an unrelated student. Deciders reject or reschedule instead. #[tokio::test] -async fn either_side_cancels_until_the_window_opens() { +async fn only_the_requester_cancels() { let (app, db) = app_and_db().await; let ali = login_as(&app, &db, "ali", "teacher").await; let veli = login(&app, "veli").await; @@ -15879,21 +15881,78 @@ async fn either_side_cancels_until_the_window_opens() { assert_eq!(res.body["cancelled_by"]["id"], veli_id.as_str()); assert_eq!(res.body["cancel_reason"], "Rahatsızlandım"); - // The slot's teacher may too. + // The slot's teacher may NOT cancel — cancelling is the requester's lever; + // a decider ends a booking by rejecting (pending) or rescheduling. 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; + assert_eq!(res.status, StatusCode::FORBIDDEN, "owner-teacher: {}", res.body); + // An unrelated student may not either. + let res = decide(&app, &veli, &booking, "cancel").await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "outsider: {}", res.body); + // The requester can, and a settled booking cannot be cancelled twice. + assert_eq!( + decide(&app, &ayse, &booking, "cancel").await.status, + StatusCode::OK + ); let res = decide(&app, &ayse, &booking, "cancel").await; + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); +} + +/// A student is not a decider: rejecting a booking is a teacher/manager call, +/// so the requester calling `reject` on their own booking is refused. +#[tokio::test] +async fn student_cannot_reject_a_booking() { + 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 res = decide(&app, &veli, &booking, "reject").await; assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); +} + +/// A slot-owning teacher may not *cancel* a booking — rejecting (pending) or +/// rescheduling is their lever; cancelling belongs to the requester alone. +#[tokio::test] +async fn teacher_cannot_cancel_a_booking() { + 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(); + + // Pending booking: owner-teacher cancel is refused (the booking stays live). + 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, "cancel").await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "pending: {}", res.body); + + // Approved booking on a fresh slot: same refusal. + let slot2 = publish_slot(&app, &ali, now + 3 * HOUR_MS, now + 4 * HOUR_MS).await; + let booking = book_ok(&app, &veli, &slot2).await; assert_eq!( - decide(&app, &veli, &booking, "cancel").await.status, + decide(&app, &ali, &booking, "approve").await.status, StatusCode::OK ); - let res = decide(&app, &veli, &booking, "cancel").await; - assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); + let res = decide(&app, &ali, &booking, "cancel").await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "approved: {}", res.body); +} + +/// A manager who is not the requester also cannot cancel — no oversight override +/// exists; only the requester (student/parent) cancels. +#[tokio::test] +async fn manager_cannot_cancel_a_booking() { + let (app, db) = app_and_db().await; + let ali = login_as(&app, &db, "ali", "teacher").await; + let mgr = login_as(&app, &db, "mgr", "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; + + let res = decide(&app, &mgr, &booking, "cancel").await; + assert_eq!(res.status, StatusCode::FORBIDDEN, "manager: {}", res.body); } #[tokio::test]