✨ appointments — only the requester may cancel
Browse files at this commitf1a8cf3587cc8a5d02930e8eae98918b463457a7Parent: 8b07cfa- feat
- ✨ appointments — only the requester may cancel
2 changed files +73 -15
- src/web/appointments.rs +5 -6
- tests/integration.rs +68 -9
src/web/appointments.rsmodified+5 -6
| @@ -626,7 +626,7 @@ async fn reject( | ||
| 626 | 626 | (status = 200, description = "Cancelled", body = AppointmentResponse), |
| 627 | 627 | (status = 400, description = "Reason is over-long", body = ErrorResponse), |
| 628 | 628 | (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), | |
| 630 | 630 | (status = 404, description = "Not found", body = ErrorResponse), |
| 631 | 631 | (status = 409, description = "Already settled, or the appointment has already started", body = ErrorResponse), |
| 632 | 632 | ), |
| @@ -641,12 +641,11 @@ async fn cancel( | ||
| 641 | 641 | let appointment = Appointment::read(&id, &st.db) |
| 642 | 642 | .await? |
| 643 | 643 | .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() { | |
| 648 | 647 | 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", | |
| 650 | 649 | )); |
| 651 | 650 | } |
| 652 | 651 | // 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() { | ||
| 15831 | 15831 | ); |
| 15832 | 15832 | } |
| 15833 | 15833 | |
| 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. | |
| 15834 | 15836 | #[tokio::test] |
| 15835 | -async fn either_side_cancels_until_the_window_opens() { | |
| 15837 | +async fn only_the_requester_cancels() { | |
| 15836 | 15838 | let (app, db) = app_and_db().await; |
| 15837 | 15839 | let ali = login_as(&app, &db, "ali", "teacher").await; |
| 15838 | 15840 | let veli = login(&app, "veli").await; |
| @@ -15879,21 +15881,78 @@ async fn either_side_cancels_until_the_window_opens() { | ||
| 15879 | 15881 | assert_eq!(res.body["cancelled_by"]["id"], veli_id.as_str()); |
| 15880 | 15882 | assert_eq!(res.body["cancel_reason"], "Rahatsızlandım"); |
| 15881 | 15883 | |
| 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. | |
| 15883 | 15886 | let booking = book_ok(&app, &ayse, &slot).await; |
| 15884 | 15887 | 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 | + ); | |
| 15889 | 15897 | 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; | |
| 15890 | 15913 | 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; | |
| 15891 | 15934 | assert_eq!( |
| 15892 | - decide(&app, &veli, &booking, "cancel").await.status, | |
| 15935 | + decide(&app, &ali, &booking, "approve").await.status, | |
| 15893 | 15936 | StatusCode::OK |
| 15894 | 15937 | ); |
| 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); | |
| 15897 | 15956 | } |
| 15898 | 15957 | |
| 15899 | 15958 | #[tokio::test] |