forge
Explore
Log in

Don't you dare go Hollow

✨ appointments — only the requester may cancel

Browse files at this commit

f1a8cf3587cc8a5d02930e8eae98918b463457a7Ahmet Kaan GümüşParent: 8b07cfa
  • feat
    • ✨ appointments — only the requester may cancel
Show full message
feat: ✨ appointments — only the requester may cancel
- tighten the cancel guard to requester-only: a slot-owning teacher or a
  manager/admin can no longer cancel a booking (403); they end it by
  rejecting while pending or by rescheduling instead
- drop the now-dead slot read the can_manage check needed
- tests: teacher_cannot_cancel, manager_cannot_cancel, student_cannot_reject,
  and only_the_requester_cancels (reworked from either_side_cancels)
src/web/appointments.rsmodified+5 -6
@@ -626,7 +626,7 @@ async fn reject(
626626 (status = 200, description = "Cancelled", body = AppointmentResponse),
627627 (status = 400, description = "Reason is over-long", body = ErrorResponse),
628628 (status = 401, description = "Not authenticated", body = ErrorResponse),
629- (status = 403, description = "Neither the requester nor the slot's teacher", body = ErrorResponse),
629+ (status = 403, description = "Not the requester", body = ErrorResponse),
630630 (status = 404, description = "Not found", body = ErrorResponse),
631631 (status = 409, description = "Already settled, or the appointment has already started", body = ErrorResponse),
632632 ),
@@ -641,12 +641,11 @@ async fn cancel(
641641 let appointment = Appointment::read(&id, &st.db)
642642 .await?
643643 .ok_or(AppError::NotFound)?;
644- let slot = AppointmentSlot::read(appointment.get_slot(), &st.db)
645- .await?
646- .ok_or(AppError::NotFound)?;
647- if appointment.get_requester() != user.get_id() && !can_manage(&slot, &user) {
644+ // Only the requester (student/parent) may cancel. Teachers/managers end a
645+ // booking by rejecting (while pending) or rescheduling — never cancelling.
646+ if appointment.get_requester() != user.get_id() {
648647 return Err(AppError::Forbidden(
649- "only the requester or the slot's teacher can cancel this appointment",
648+ "only the requester can cancel this appointment",
650649 ));
651650 }
652651 // A blank or absent reason records none; a present one is validated (400 if
tests/integration.rsmodified+68 -9
@@ -15831,8 +15831,10 @@ async fn accepting_a_reschedule_re_runs_the_overlap_guard() {
1583115831 );
1583215832 }
1583315833
15834+/// Only the requester (student/parent) may cancel a booking — not the slot's
15835+/// teacher, not an unrelated student. Deciders reject or reschedule instead.
1583415836 #[tokio::test]
15835-async fn either_side_cancels_until_the_window_opens() {
15837+async fn only_the_requester_cancels() {
1583615838 let (app, db) = app_and_db().await;
1583715839 let ali = login_as(&app, &db, "ali", "teacher").await;
1583815840 let veli = login(&app, "veli").await;
@@ -15879,21 +15881,78 @@ async fn either_side_cancels_until_the_window_opens() {
1587915881 assert_eq!(res.body["cancelled_by"]["id"], veli_id.as_str());
1588015882 assert_eq!(res.body["cancel_reason"], "Rahatsızlandım");
1588115883
15882- // The slot's teacher may too.
15884+ // The slot's teacher may NOT cancel — cancelling is the requester's lever;
15885+ // a decider ends a booking by rejecting (pending) or rescheduling.
1588315886 let booking = book_ok(&app, &ayse, &slot).await;
1588415887 let res = decide(&app, &ali, &booking, "cancel").await;
15885- assert_eq!(res.status, StatusCode::OK, "{}", res.body);
15886- assert_eq!(res.body["status"], "cancelled");
15887- // An outsider may not, and a settled booking cannot be cancelled twice.
15888- let booking = book_ok(&app, &veli, &slot).await;
15888+ assert_eq!(res.status, StatusCode::FORBIDDEN, "owner-teacher: {}", res.body);
15889+ // An unrelated student may not either.
15890+ let res = decide(&app, &veli, &booking, "cancel").await;
15891+ assert_eq!(res.status, StatusCode::FORBIDDEN, "outsider: {}", res.body);
15892+ // The requester can, and a settled booking cannot be cancelled twice.
15893+ assert_eq!(
15894+ decide(&app, &ayse, &booking, "cancel").await.status,
15895+ StatusCode::OK
15896+ );
1588915897 let res = decide(&app, &ayse, &booking, "cancel").await;
15898+ assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body);
15899+}
15900+
15901+/// A student is not a decider: rejecting a booking is a teacher/manager call,
15902+/// so the requester calling `reject` on their own booking is refused.
15903+#[tokio::test]
15904+async fn student_cannot_reject_a_booking() {
15905+ let (app, db) = app_and_db().await;
15906+ let ali = login_as(&app, &db, "ali", "teacher").await;
15907+ let veli = login(&app, "veli").await;
15908+ let now = Timestamp::now().as_millis();
15909+ let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await;
15910+ let booking = book_ok(&app, &veli, &slot).await;
15911+
15912+ let res = decide(&app, &veli, &booking, "reject").await;
1589015913 assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body);
15914+}
15915+
15916+/// A slot-owning teacher may not *cancel* a booking — rejecting (pending) or
15917+/// rescheduling is their lever; cancelling belongs to the requester alone.
15918+#[tokio::test]
15919+async fn teacher_cannot_cancel_a_booking() {
15920+ let (app, db) = app_and_db().await;
15921+ let ali = login_as(&app, &db, "ali", "teacher").await;
15922+ let veli = login(&app, "veli").await;
15923+ let now = Timestamp::now().as_millis();
15924+
15925+ // Pending booking: owner-teacher cancel is refused (the booking stays live).
15926+ let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await;
15927+ let booking = book_ok(&app, &veli, &slot).await;
15928+ let res = decide(&app, &ali, &booking, "cancel").await;
15929+ assert_eq!(res.status, StatusCode::FORBIDDEN, "pending: {}", res.body);
15930+
15931+ // Approved booking on a fresh slot: same refusal.
15932+ let slot2 = publish_slot(&app, &ali, now + 3 * HOUR_MS, now + 4 * HOUR_MS).await;
15933+ let booking = book_ok(&app, &veli, &slot2).await;
1589115934 assert_eq!(
15892- decide(&app, &veli, &booking, "cancel").await.status,
15935+ decide(&app, &ali, &booking, "approve").await.status,
1589315936 StatusCode::OK
1589415937 );
15895- let res = decide(&app, &veli, &booking, "cancel").await;
15896- assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body);
15938+ let res = decide(&app, &ali, &booking, "cancel").await;
15939+ assert_eq!(res.status, StatusCode::FORBIDDEN, "approved: {}", res.body);
15940+}
15941+
15942+/// A manager who is not the requester also cannot cancel — no oversight override
15943+/// exists; only the requester (student/parent) cancels.
15944+#[tokio::test]
15945+async fn manager_cannot_cancel_a_booking() {
15946+ let (app, db) = app_and_db().await;
15947+ let ali = login_as(&app, &db, "ali", "teacher").await;
15948+ let mgr = login_as(&app, &db, "mgr", "manager").await;
15949+ let veli = login(&app, "veli").await;
15950+ let now = Timestamp::now().as_millis();
15951+ let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await;
15952+ let booking = book_ok(&app, &veli, &slot).await;
15953+
15954+ let res = decide(&app, &mgr, &booking, "cancel").await;
15955+ assert_eq!(res.status, StatusCode::FORBIDDEN, "manager: {}", res.body);
1589715956 }
1589815957
1589915958 #[tokio::test]