Cover the booking lifecycle and its guards
Browse files at this commitfff77ce6f05918617a55df4a3845b6bd73a451d6Parent: 771936e- test
- cover the booking lifecycle and its guards
3 changed files +1056 -0
- tests/integration.rs +719 -0
- tests/pagination.rs +183 -0
- tests/persistence.rs +154 -0
tests/integration.rsmodified+719 -0
| @@ -15424,3 +15424,722 @@ async fn chat_rate_limit_tier_is_configurable_and_zero_disables_it() { | ||
| 15424 | 15424 | ); |
| 15425 | 15425 | } |
| 15426 | 15426 | } |
| 15427 | + | |
| 15428 | +// --- appointments ---------------------------------------------------------- | |
| 15429 | + | |
| 15430 | +const HOUR_MS: i64 = 3_600_000; | |
| 15431 | +const WEEK_MS: i64 = 7 * 24 * HOUR_MS; | |
| 15432 | + | |
| 15433 | +/// Publish availability from a full JSON body (no assertion). The response is | |
| 15434 | +/// always an array: one element for a one-off, one per occurrence for a weekly. | |
| 15435 | +async fn publish_slots(app: &axum::Router, cookie: &str, body: serde_json::Value) -> common::Res { | |
| 15436 | + send(app, "POST", "/appointments/slots", Some(cookie), Some(body)).await | |
| 15437 | +} | |
| 15438 | + | |
| 15439 | +/// Publish one slot as `cookie` (asserts 201); returns its id. | |
| 15440 | +async fn publish_slot(app: &axum::Router, cookie: &str, starts_at: i64, ends_at: i64) -> String { | |
| 15441 | + let res = publish_slots( | |
| 15442 | + app, | |
| 15443 | + cookie, | |
| 15444 | + json!({ "starts_at": starts_at, "ends_at": ends_at }), | |
| 15445 | + ) | |
| 15446 | + .await; | |
| 15447 | + assert_eq!( | |
| 15448 | + res.status, | |
| 15449 | + StatusCode::CREATED, | |
| 15450 | + "publish slot: {}", | |
| 15451 | + res.body | |
| 15452 | + ); | |
| 15453 | + res.body[0]["id"].as_str().expect("slot id").to_string() | |
| 15454 | +} | |
| 15455 | + | |
| 15456 | +/// Ask for a meeting on `slot` (no assertion). | |
| 15457 | +async fn book_slot(app: &axum::Router, cookie: &str, slot: &str) -> common::Res { | |
| 15458 | + send( | |
| 15459 | + app, | |
| 15460 | + "POST", | |
| 15461 | + "/appointments", | |
| 15462 | + Some(cookie), | |
| 15463 | + Some(json!({ "slot": slot, "reason": "görüşmek istiyorum" })), | |
| 15464 | + ) | |
| 15465 | + .await | |
| 15466 | +} | |
| 15467 | + | |
| 15468 | +/// Book `slot`, asserting it lands pending; returns the booking id. | |
| 15469 | +async fn book_ok(app: &axum::Router, cookie: &str, slot: &str) -> String { | |
| 15470 | + let res = book_slot(app, cookie, slot).await; | |
| 15471 | + assert_eq!(res.status, StatusCode::CREATED, "book slot: {}", res.body); | |
| 15472 | + assert_eq!(res.body["status"], "pending"); | |
| 15473 | + id_of(&res.body) | |
| 15474 | +} | |
| 15475 | + | |
| 15476 | +/// `PATCH /appointments/{id}/{action}` — approve, reject, cancel, or either | |
| 15477 | +/// answer to a counter-proposal (no assertion, no body). | |
| 15478 | +async fn decide(app: &axum::Router, cookie: &str, id: &str, action: &str) -> common::Res { | |
| 15479 | + send( | |
| 15480 | + app, | |
| 15481 | + "PATCH", | |
| 15482 | + &format!("/appointments/{id}/{action}"), | |
| 15483 | + Some(cookie), | |
| 15484 | + None, | |
| 15485 | + ) | |
| 15486 | + .await | |
| 15487 | +} | |
| 15488 | + | |
| 15489 | +/// Counter-propose another window for `id` (no assertion). | |
| 15490 | +async fn propose( | |
| 15491 | + app: &axum::Router, | |
| 15492 | + cookie: &str, | |
| 15493 | + id: &str, | |
| 15494 | + starts_at: i64, | |
| 15495 | + ends_at: i64, | |
| 15496 | +) -> common::Res { | |
| 15497 | + send( | |
| 15498 | + app, | |
| 15499 | + "PATCH", | |
| 15500 | + &format!("/appointments/{id}/reschedule"), | |
| 15501 | + Some(cookie), | |
| 15502 | + Some(json!({ "starts_at": starts_at, "ends_at": ends_at })), | |
| 15503 | + ) | |
| 15504 | + .await | |
| 15505 | +} | |
| 15506 | + | |
| 15507 | +#[tokio::test] | |
| 15508 | +async fn appointment_book_and_approve_holds_the_slot() { | |
| 15509 | + let (app, db) = app_and_db().await; | |
| 15510 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15511 | + let ali_id = me_id(&app, &ali).await; | |
| 15512 | + let veli = login(&app, "veli").await; | |
| 15513 | + let veli_id = me_id(&app, &veli).await; | |
| 15514 | + let ayse = login(&app, "ayse").await; | |
| 15515 | + let now = Timestamp::now().as_millis(); | |
| 15516 | + | |
| 15517 | + let res = publish_slots( | |
| 15518 | + &app, | |
| 15519 | + &ali, | |
| 15520 | + json!({ "starts_at": now + HOUR_MS, "ends_at": now + 2 * HOUR_MS, | |
| 15521 | + "note": "ofis saati" }), | |
| 15522 | + ) | |
| 15523 | + .await; | |
| 15524 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 15525 | + assert_eq!(res.body.as_array().expect("array body").len(), 1); | |
| 15526 | + let slot = res.body[0]["id"].as_str().expect("slot id").to_string(); | |
| 15527 | + assert_eq!(res.body[0]["teacher"]["id"], ali_id.as_str()); | |
| 15528 | + assert_eq!(res.body[0]["note"], "ofis saati"); | |
| 15529 | + assert!(res.body[0]["series"].is_null(), "a one-off has no series"); | |
| 15530 | + | |
| 15531 | + // Booking lands pending, echoing the slot's own window. | |
| 15532 | + let res = book_slot(&app, &veli, &slot).await; | |
| 15533 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 15534 | + assert_eq!(res.body["status"], "pending"); | |
| 15535 | + assert_eq!(res.body["requester"]["id"], veli_id.as_str()); | |
| 15536 | + assert_eq!(res.body["teacher"]["id"], ali_id.as_str()); | |
| 15537 | + assert_eq!(res.body["starts_at"], now + HOUR_MS); | |
| 15538 | + assert!(res.body["decided_by"].is_null(), "pending has no decider"); | |
| 15539 | + let booking = id_of(&res.body); | |
| 15540 | + | |
| 15541 | + // The pending request already holds the slot against everyone else. | |
| 15542 | + let res = book_slot(&app, &ayse, &slot).await; | |
| 15543 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 15544 | + | |
| 15545 | + let res = decide(&app, &ali, &booking, "approve").await; | |
| 15546 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 15547 | + assert_eq!(res.body["status"], "approved"); | |
| 15548 | + assert_eq!(res.body["decided_by"]["id"], ali_id.as_str()); | |
| 15549 | + | |
| 15550 | + // Both sides read it back from their own list. | |
| 15551 | + let mine = send(&app, "GET", "/appointments", Some(&veli), None).await; | |
| 15552 | + assert_eq!(common::items(&mine.body).len(), 1); | |
| 15553 | + let inbox = send(&app, "GET", "/appointments", Some(&ali), None).await; | |
| 15554 | + assert_eq!(common::items(&inbox.body)[0]["id"], booking.as_str()); | |
| 15555 | +} | |
| 15556 | + | |
| 15557 | +#[tokio::test] | |
| 15558 | +async fn rejecting_a_booking_frees_the_slot() { | |
| 15559 | + let (app, db) = app_and_db().await; | |
| 15560 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15561 | + let veli = login(&app, "veli").await; | |
| 15562 | + let ayse = login(&app, "ayse").await; | |
| 15563 | + let now = Timestamp::now().as_millis(); | |
| 15564 | + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 15565 | + | |
| 15566 | + let booking = book_ok(&app, &veli, &slot).await; | |
| 15567 | + let res = decide(&app, &ali, &booking, "reject").await; | |
| 15568 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 15569 | + assert_eq!(res.body["status"], "rejected"); | |
| 15570 | + | |
| 15571 | + // Occupancy counts live rows only, so the next person may take it. | |
| 15572 | + let res = book_slot(&app, &ayse, &slot).await; | |
| 15573 | + assert_eq!( | |
| 15574 | + res.status, | |
| 15575 | + StatusCode::CREATED, | |
| 15576 | + "a rejected booking must not keep the seat: {}", | |
| 15577 | + res.body | |
| 15578 | + ); | |
| 15579 | + // And the rejected one cannot be decided twice. | |
| 15580 | + let res = decide(&app, &ali, &booking, "approve").await; | |
| 15581 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 15582 | +} | |
| 15583 | + | |
| 15584 | +#[tokio::test] | |
| 15585 | +async fn approval_refuses_a_teacher_double_booking() { | |
| 15586 | + let (app, db) = app_and_db().await; | |
| 15587 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15588 | + let veli = login(&app, "veli").await; | |
| 15589 | + let ayse = login(&app, "ayse").await; | |
| 15590 | + let now = Timestamp::now().as_millis(); | |
| 15591 | + | |
| 15592 | + // Two overlapping windows on the same calendar, one requester each: the | |
| 15593 | + // request is fine (a wish), the second approval is not. | |
| 15594 | + let first = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 15595 | + let second = publish_slot(&app, &ali, now + HOUR_MS + 600_000, now + 3 * HOUR_MS).await; | |
| 15596 | + let one = book_ok(&app, &veli, &first).await; | |
| 15597 | + let two = book_ok(&app, &ayse, &second).await; | |
| 15598 | + | |
| 15599 | + assert_eq!( | |
| 15600 | + decide(&app, &ali, &one, "approve").await.status, | |
| 15601 | + StatusCode::OK | |
| 15602 | + ); | |
| 15603 | + let res = decide(&app, &ali, &two, "approve").await; | |
| 15604 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 15605 | + // Refused, not half-applied: it is still pending and still decidable. | |
| 15606 | + let res = decide(&app, &ali, &two, "reject").await; | |
| 15607 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 15608 | +} | |
| 15609 | + | |
| 15610 | +#[tokio::test] | |
| 15611 | +async fn approval_refuses_a_requester_double_booking() { | |
| 15612 | + let (app, db) = app_and_db().await; | |
| 15613 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15614 | + let mert = login_as(&app, &db, "mert", "teacher").await; | |
| 15615 | + let veli = login(&app, "veli").await; | |
| 15616 | + let now = Timestamp::now().as_millis(); | |
| 15617 | + | |
| 15618 | + // One student, two different teachers, overlapping times. Both requests | |
| 15619 | + // stand (nothing is committed yet); the second approval collides. | |
| 15620 | + let first = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 15621 | + let second = publish_slot(&app, &mert, now + HOUR_MS + 600_000, now + 3 * HOUR_MS).await; | |
| 15622 | + let one = book_ok(&app, &veli, &first).await; | |
| 15623 | + let two = book_ok(&app, &veli, &second).await; | |
| 15624 | + | |
| 15625 | + assert_eq!( | |
| 15626 | + decide(&app, &ali, &one, "approve").await.status, | |
| 15627 | + StatusCode::OK | |
| 15628 | + ); | |
| 15629 | + let res = decide(&app, &mert, &two, "approve").await; | |
| 15630 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 15631 | + | |
| 15632 | + // Booking a fresh overlap once one is committed is refused up front. | |
| 15633 | + let third = publish_slot(&app, &mert, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 15634 | + let res = book_slot(&app, &veli, &third).await; | |
| 15635 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 15636 | +} | |
| 15637 | + | |
| 15638 | +#[tokio::test] | |
| 15639 | +async fn touching_windows_never_collide() { | |
| 15640 | + let (app, db) = app_and_db().await; | |
| 15641 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15642 | + let veli = login(&app, "veli").await; | |
| 15643 | + let now = Timestamp::now().as_millis(); | |
| 15644 | + | |
| 15645 | + // Half-open windows: 10:00–11:00 and 11:00–12:00 are back-to-back, not a | |
| 15646 | + // clash — for the teacher and for the student alike. | |
| 15647 | + let first = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 15648 | + let second = publish_slot(&app, &ali, now + 2 * HOUR_MS, now + 3 * HOUR_MS).await; | |
| 15649 | + let one = book_ok(&app, &veli, &first).await; | |
| 15650 | + let two = book_ok(&app, &veli, &second).await; | |
| 15651 | + | |
| 15652 | + for booking in [&one, &two] { | |
| 15653 | + let res = decide(&app, &ali, booking, "approve").await; | |
| 15654 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 15655 | + assert_eq!(res.body["status"], "approved"); | |
| 15656 | + } | |
| 15657 | +} | |
| 15658 | + | |
| 15659 | +#[tokio::test] | |
| 15660 | +async fn reschedule_returns_to_pending_until_the_requester_accepts() { | |
| 15661 | + let (app, db) = app_and_db().await; | |
| 15662 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15663 | + let ali_id = me_id(&app, &ali).await; | |
| 15664 | + let veli = login(&app, "veli").await; | |
| 15665 | + let ayse = login(&app, "ayse").await; | |
| 15666 | + let now = Timestamp::now().as_millis(); | |
| 15667 | + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 15668 | + let booking = book_ok(&app, &veli, &slot).await; | |
| 15669 | + assert_eq!( | |
| 15670 | + decide(&app, &ali, &booking, "approve").await.status, | |
| 15671 | + StatusCode::OK | |
| 15672 | + ); | |
| 15673 | + | |
| 15674 | + // A counter-proposal un-commits the approved meeting rather than moving it | |
| 15675 | + // silently. | |
| 15676 | + let res = propose(&app, &ali, &booking, now + 3 * HOUR_MS, now + 4 * HOUR_MS).await; | |
| 15677 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 15678 | + assert_eq!(res.body["status"], "pending"); | |
| 15679 | + assert!(res.body["decided_by"].is_null(), "approval was withdrawn"); | |
| 15680 | + assert_eq!(res.body["proposed_starts_at"], now + 3 * HOUR_MS); | |
| 15681 | + assert_eq!(res.body["proposed_ends_at"], now + 4 * HOUR_MS); | |
| 15682 | + assert_eq!(res.body["proposed_by"]["id"], ali_id.as_str()); | |
| 15683 | + // The effective window already reads as the proposed one. | |
| 15684 | + assert_eq!(res.body["starts_at"], now + 3 * HOUR_MS); | |
| 15685 | + | |
| 15686 | + // Only the requester may answer it. | |
| 15687 | + let res = decide(&app, &ayse, &booking, "reschedule/accept").await; | |
| 15688 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); | |
| 15689 | + let res = decide(&app, &ali, &booking, "reschedule/accept").await; | |
| 15690 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); | |
| 15691 | + | |
| 15692 | + let res = decide(&app, &veli, &booking, "reschedule/accept").await; | |
| 15693 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 15694 | + assert_eq!(res.body["status"], "approved"); | |
| 15695 | + assert_eq!(res.body["starts_at"], now + 3 * HOUR_MS); | |
| 15696 | + assert_eq!(res.body["ends_at"], now + 4 * HOUR_MS); | |
| 15697 | +} | |
| 15698 | + | |
| 15699 | +#[tokio::test] | |
| 15700 | +async fn a_started_proposal_is_refused() { | |
| 15701 | + let (app, db) = app_and_db().await; | |
| 15702 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15703 | + let veli = login(&app, "veli").await; | |
| 15704 | + let now = Timestamp::now().as_millis(); | |
| 15705 | + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 15706 | + let booking = book_ok(&app, &veli, &slot).await; | |
| 15707 | + | |
| 15708 | + // A proposal 30s into the past is inside the publishing grace, so the times | |
| 15709 | + // themselves validate — but the window is already underway, and a meeting | |
| 15710 | + // that began is one `cancel` refuses to undo. Refused at the proposal, not | |
| 15711 | + // left for the requester to agree to. | |
| 15712 | + let res = propose(&app, &ali, &booking, now - 30_000, now + HOUR_MS).await; | |
| 15713 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 15714 | + // Refused, not half-applied: the teacher can still propose a real time. | |
| 15715 | + let res = send(&app, "GET", "/appointments", Some(&veli), None).await; | |
| 15716 | + assert_eq!(common::items(&res.body)[0]["status"], "pending"); | |
| 15717 | + assert!( | |
| 15718 | + common::items(&res.body)[0]["proposed_starts_at"].is_null(), | |
| 15719 | + "a refused proposal leaves nothing behind" | |
| 15720 | + ); | |
| 15721 | + | |
| 15722 | + assert_eq!( | |
| 15723 | + propose(&app, &ali, &booking, now + 3 * HOUR_MS, now + 4 * HOUR_MS) | |
| 15724 | + .await | |
| 15725 | + .status, | |
| 15726 | + StatusCode::OK | |
| 15727 | + ); | |
| 15728 | + let res = decide(&app, &veli, &booking, "reschedule/accept").await; | |
| 15729 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 15730 | + assert_eq!(res.body["status"], "approved"); | |
| 15731 | +} | |
| 15732 | + | |
| 15733 | +#[tokio::test] | |
| 15734 | +async fn declining_a_reschedule_cancels_the_booking() { | |
| 15735 | + let (app, db) = app_and_db().await; | |
| 15736 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15737 | + let veli = login(&app, "veli").await; | |
| 15738 | + let ayse = login(&app, "ayse").await; | |
| 15739 | + let now = Timestamp::now().as_millis(); | |
| 15740 | + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 15741 | + let booking = book_ok(&app, &veli, &slot).await; | |
| 15742 | + | |
| 15743 | + // Nothing proposed yet: there is nothing to decline. | |
| 15744 | + let res = decide(&app, &veli, &booking, "reschedule/decline").await; | |
| 15745 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 15746 | + | |
| 15747 | + assert_eq!( | |
| 15748 | + propose(&app, &ali, &booking, now + 3 * HOUR_MS, now + 4 * HOUR_MS) | |
| 15749 | + .await | |
| 15750 | + .status, | |
| 15751 | + StatusCode::OK | |
| 15752 | + ); | |
| 15753 | + let res = decide(&app, &veli, &booking, "reschedule/decline").await; | |
| 15754 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 15755 | + assert_eq!(res.body["status"], "cancelled"); | |
| 15756 | + // The refused proposal stays readable on the cancelled row. | |
| 15757 | + assert_eq!(res.body["proposed_starts_at"], now + 3 * HOUR_MS); | |
| 15758 | + | |
| 15759 | + // And the slot is free again. | |
| 15760 | + let res = book_slot(&app, &ayse, &slot).await; | |
| 15761 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 15762 | +} | |
| 15763 | + | |
| 15764 | +/// Declining cancels, so it answers to the cancel deadline. The guard lives in | |
| 15765 | +/// `Appointment::cancel` itself — when it sat in the cancel *handler*, decline | |
| 15766 | +/// went straight past it and called off a meeting already underway. | |
| 15767 | +#[tokio::test] | |
| 15768 | +async fn declining_a_started_reschedule_is_refused() { | |
| 15769 | + let (app, db) = app_and_db().await; | |
| 15770 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15771 | + let veli = login(&app, "veli").await; | |
| 15772 | + let now = Timestamp::now().as_millis(); | |
| 15773 | + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 15774 | + let booking = book_ok(&app, &veli, &slot).await; | |
| 15775 | + | |
| 15776 | + // A proposal that is still ahead when made, and underway a moment later: | |
| 15777 | + // the only way an effective window opens with the booking still live. | |
| 15778 | + let opens_at = Timestamp::now().as_millis() + 700; | |
| 15779 | + assert_eq!( | |
| 15780 | + propose(&app, &ali, &booking, opens_at, opens_at + HOUR_MS) | |
| 15781 | + .await | |
| 15782 | + .status, | |
| 15783 | + StatusCode::OK | |
| 15784 | + ); | |
| 15785 | + tokio::time::sleep(std::time::Duration::from_millis(800)).await; | |
| 15786 | + let res = decide(&app, &veli, &booking, "reschedule/decline").await; | |
| 15787 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 15788 | + // The plain cancel door answers the same, and the booking survives both. | |
| 15789 | + let res = decide(&app, &veli, &booking, "cancel").await; | |
| 15790 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 15791 | + let res = send(&app, "GET", "/appointments", Some(&veli), None).await; | |
| 15792 | + assert_eq!(common::items(&res.body)[0]["status"], "pending"); | |
| 15793 | + | |
| 15794 | + // Moved back into the future, declining cancels as it always did. | |
| 15795 | + assert_eq!( | |
| 15796 | + propose(&app, &ali, &booking, now + 3 * HOUR_MS, now + 4 * HOUR_MS) | |
| 15797 | + .await | |
| 15798 | + .status, | |
| 15799 | + StatusCode::OK | |
| 15800 | + ); | |
| 15801 | + let res = decide(&app, &veli, &booking, "reschedule/decline").await; | |
| 15802 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 15803 | + assert_eq!(res.body["status"], "cancelled"); | |
| 15804 | +} | |
| 15805 | + | |
| 15806 | +#[tokio::test] | |
| 15807 | +async fn accepting_a_reschedule_re_runs_the_overlap_guard() { | |
| 15808 | + let (app, db) = app_and_db().await; | |
| 15809 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15810 | + let mert = login_as(&app, &db, "mert", "teacher").await; | |
| 15811 | + let veli = login(&app, "veli").await; | |
| 15812 | + let now = Timestamp::now().as_millis(); | |
| 15813 | + | |
| 15814 | + // Committed with ali at +5h. | |
| 15815 | + let taken = publish_slot(&app, &ali, now + 5 * HOUR_MS, now + 6 * HOUR_MS).await; | |
| 15816 | + let committed = book_ok(&app, &veli, &taken).await; | |
| 15817 | + assert_eq!( | |
| 15818 | + decide(&app, &ali, &committed, "approve").await.status, | |
| 15819 | + StatusCode::OK | |
| 15820 | + ); | |
| 15821 | + | |
| 15822 | + // Mert counter-proposes straight onto that hour; accepting would commit | |
| 15823 | + // the student twice, so it is refused. | |
| 15824 | + let slot = publish_slot(&app, &mert, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 15825 | + let booking = book_ok(&app, &veli, &slot).await; | |
| 15826 | + assert_eq!( | |
| 15827 | + propose(&app, &mert, &booking, now + 5 * HOUR_MS, now + 6 * HOUR_MS) | |
| 15828 | + .await | |
| 15829 | + .status, | |
| 15830 | + StatusCode::OK | |
| 15831 | + ); | |
| 15832 | + let res = decide(&app, &veli, &booking, "reschedule/accept").await; | |
| 15833 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 15834 | + assert_eq!( | |
| 15835 | + send(&app, "GET", "/appointments", Some(&veli), None) | |
| 15836 | + .await | |
| 15837 | + .body["items"][0]["status"], | |
| 15838 | + "pending", | |
| 15839 | + "a refused acceptance leaves the booking pending" | |
| 15840 | + ); | |
| 15841 | +} | |
| 15842 | + | |
| 15843 | +#[tokio::test] | |
| 15844 | +async fn either_side_cancels_until_the_window_opens() { | |
| 15845 | + let (app, db) = app_and_db().await; | |
| 15846 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15847 | + let veli = login(&app, "veli").await; | |
| 15848 | + let ayse = login(&app, "ayse").await; | |
| 15849 | + let now = Timestamp::now().as_millis(); | |
| 15850 | + | |
| 15851 | + // The requester's own call, on an approved meeting. | |
| 15852 | + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 15853 | + let booking = book_ok(&app, &veli, &slot).await; | |
| 15854 | + assert_eq!( | |
| 15855 | + decide(&app, &ali, &booking, "approve").await.status, | |
| 15856 | + StatusCode::OK | |
| 15857 | + ); | |
| 15858 | + let res = decide(&app, &veli, &booking, "cancel").await; | |
| 15859 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 15860 | + assert_eq!(res.body["status"], "cancelled"); | |
| 15861 | + | |
| 15862 | + // The slot's teacher may too. | |
| 15863 | + let booking = book_ok(&app, &ayse, &slot).await; | |
| 15864 | + let res = decide(&app, &ali, &booking, "cancel").await; | |
| 15865 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 15866 | + assert_eq!(res.body["status"], "cancelled"); | |
| 15867 | + // An outsider may not, and a settled booking cannot be cancelled twice. | |
| 15868 | + let booking = book_ok(&app, &veli, &slot).await; | |
| 15869 | + let res = decide(&app, &ayse, &booking, "cancel").await; | |
| 15870 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); | |
| 15871 | + assert_eq!( | |
| 15872 | + decide(&app, &veli, &booking, "cancel").await.status, | |
| 15873 | + StatusCode::OK | |
| 15874 | + ); | |
| 15875 | + let res = decide(&app, &veli, &booking, "cancel").await; | |
| 15876 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 15877 | +} | |
| 15878 | + | |
| 15879 | +#[tokio::test] | |
| 15880 | +async fn a_started_slot_cannot_be_booked() { | |
| 15881 | + let (app, db) = app_and_db().await; | |
| 15882 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15883 | + let veli = login(&app, "veli").await; | |
| 15884 | + let now = Timestamp::now().as_millis(); | |
| 15885 | + | |
| 15886 | + // A window that opened 30s ago is inside the publishing grace, so the slot | |
| 15887 | + // is created — but the meeting has begun, and a meeting that began is | |
| 15888 | + // history, not a plan. Booking it would land a row `cancel` refuses to | |
| 15889 | + // undo, stuck for good. | |
| 15890 | + let underway = publish_slot(&app, &ali, now - 30_000, now + HOUR_MS).await; | |
| 15891 | + let res = book_slot(&app, &veli, &underway).await; | |
| 15892 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 15893 | + | |
| 15894 | + // A window still ahead is untouched by the guard. | |
| 15895 | + let upcoming = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 15896 | + let res = book_slot(&app, &veli, &upcoming).await; | |
| 15897 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 15898 | +} | |
| 15899 | + | |
| 15900 | +#[tokio::test] | |
| 15901 | +async fn slot_delete_waits_for_the_booking_to_settle() { | |
| 15902 | + let (app, db) = app_and_db().await; | |
| 15903 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15904 | + let veli = login(&app, "veli").await; | |
| 15905 | + let now = Timestamp::now().as_millis(); | |
| 15906 | + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 15907 | + let booking = book_ok(&app, &veli, &slot).await; | |
| 15908 | + | |
| 15909 | + let uri = format!("/appointments/slots/{slot}"); | |
| 15910 | + let res = send(&app, "DELETE", &uri, Some(&ali), None).await; | |
| 15911 | + assert_eq!( | |
| 15912 | + res.status, | |
| 15913 | + StatusCode::CONFLICT, | |
| 15914 | + "someone is waiting on this slot: {}", | |
| 15915 | + res.body | |
| 15916 | + ); | |
| 15917 | + assert_eq!( | |
| 15918 | + decide(&app, &veli, &booking, "cancel").await.status, | |
| 15919 | + StatusCode::OK | |
| 15920 | + ); | |
| 15921 | + let res = send(&app, "DELETE", &uri, Some(&ali), None).await; | |
| 15922 | + assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); | |
| 15923 | + assert_eq!( | |
| 15924 | + send(&app, "DELETE", &uri, Some(&ali), None).await.status, | |
| 15925 | + StatusCode::NOT_FOUND | |
| 15926 | + ); | |
| 15927 | +} | |
| 15928 | + | |
| 15929 | +#[tokio::test] | |
| 15930 | +async fn weekly_slots_share_a_series_and_are_dropped_together() { | |
| 15931 | + let (app, db) = app_and_db().await; | |
| 15932 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 15933 | + let veli = login(&app, "veli").await; | |
| 15934 | + let now = Timestamp::now().as_millis(); | |
| 15935 | + let starts_at = now + HOUR_MS; | |
| 15936 | + | |
| 15937 | + // `until` is inclusive, so three weeks out is four occurrences. | |
| 15938 | + let res = publish_slots( | |
| 15939 | + &app, | |
| 15940 | + &ali, | |
| 15941 | + json!({ "starts_at": starts_at, "ends_at": starts_at + HOUR_MS, | |
| 15942 | + "repeat_weekly": true, "until": starts_at + 3 * WEEK_MS }), | |
| 15943 | + ) | |
| 15944 | + .await; | |
| 15945 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 15946 | + let slots = res.body.as_array().expect("array body").clone(); | |
| 15947 | + assert_eq!(slots.len(), 4); | |
| 15948 | + let series = slots[0]["series"].as_str().expect("series id").to_string(); | |
| 15949 | + for (week, slot) in slots.iter().enumerate() { | |
| 15950 | + assert_eq!(slot["series"], series.as_str(), "one publish, one series"); | |
| 15951 | + assert_eq!(slot["starts_at"], starts_at + week as i64 * WEEK_MS); | |
| 15952 | + } | |
| 15953 | + | |
| 15954 | + // `repeat_weekly` without an end, and past the 52-occurrence cap. | |
| 15955 | + let res = publish_slots( | |
| 15956 | + &app, | |
| 15957 | + &ali, | |
| 15958 | + json!({ "starts_at": starts_at, "ends_at": starts_at + HOUR_MS, | |
| 15959 | + "repeat_weekly": true }), | |
| 15960 | + ) | |
| 15961 | + .await; | |
| 15962 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | |
| 15963 | + let res = publish_slots( | |
| 15964 | + &app, | |
| 15965 | + &ali, | |
| 15966 | + json!({ "starts_at": starts_at, "ends_at": starts_at + HOUR_MS, | |
| 15967 | + "repeat_weekly": true, "until": starts_at + 52 * WEEK_MS }), | |
| 15968 | + ) | |
| 15969 | + .await; | |
| 15970 | + assert_eq!( | |
| 15971 | + res.status, | |
| 15972 | + StatusCode::BAD_REQUEST, | |
| 15973 | + "53 occurrences: {}", | |
| 15974 | + res.body | |
| 15975 | + ); | |
| 15976 | + | |
| 15977 | + // An end at `i64::MAX` clears every gate — future, not inverted, 52 | |
| 15978 | + // occurrences — and then overflows on the first weekly shift. Unchecked it | |
| 15979 | + // wrapped the end below the start, and an inverted window never overlaps | |
| 15980 | + // anything, so the double-booking guard would have gone blind. | |
| 15981 | + let res = publish_slots( | |
| 15982 | + &app, | |
| 15983 | + &ali, | |
| 15984 | + json!({ "starts_at": starts_at, "ends_at": i64::MAX, | |
| 15985 | + "repeat_weekly": true, "until": starts_at + 51 * WEEK_MS }), | |
| 15986 | + ) | |
| 15987 | + .await; | |
| 15988 | + assert_eq!( | |
| 15989 | + res.status, | |
| 15990 | + StatusCode::BAD_REQUEST, | |
| 15991 | + "a shift that overflows: {}", | |
| 15992 | + res.body | |
| 15993 | + ); | |
| 15994 | + | |
| 15995 | + // All-or-nothing: one waiting requester anywhere in the series blocks it. | |
| 15996 | + let booked = slots[1]["id"].as_str().expect("slot id"); | |
| 15997 | + let booking = book_ok(&app, &veli, booked).await; | |
| 15998 | + let uri = format!("/appointments/slots/series/{series}"); | |
| 15999 | + let res = send(&app, "DELETE", &uri, Some(&ali), None).await; | |
| 16000 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 16001 | + | |
| 16002 | + assert_eq!( | |
| 16003 | + decide(&app, &ali, &booking, "reject").await.status, | |
| 16004 | + StatusCode::OK | |
| 16005 | + ); | |
| 16006 | + let res = send(&app, "DELETE", &uri, Some(&ali), None).await; | |
| 16007 | + assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); | |
| 16008 | + let res = send(&app, "GET", "/appointments/slots", Some(&ali), None).await; | |
| 16009 | + assert_eq!(common::total(&res.body), 0, "the whole series went"); | |
| 16010 | + assert_eq!( | |
| 16011 | + send(&app, "DELETE", &uri, Some(&ali), None).await.status, | |
| 16012 | + StatusCode::NOT_FOUND | |
| 16013 | + ); | |
| 16014 | +} | |
| 16015 | + | |
| 16016 | +#[tokio::test] | |
| 16017 | +async fn only_students_and_parents_book_appointments() { | |
| 16018 | + let (app, db) = app_and_db().await; | |
| 16019 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 16020 | + let mert = login_as(&app, &db, "mert", "teacher").await; | |
| 16021 | + let mudur = login_as(&app, &db, "mudur", "manager").await; | |
| 16022 | + let admin = login_as(&app, &db, "yonetici", "admin").await; | |
| 16023 | + let anne = login_as(&app, &db, "anne", "parent").await; | |
| 16024 | + let veli = login(&app, "veli").await; | |
| 16025 | + let now = Timestamp::now().as_millis(); | |
| 16026 | + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 16027 | + | |
| 16028 | + // Staff arrange between themselves off this API. | |
| 16029 | + for (who, label) in [(&mert, "teacher"), (&mudur, "manager"), (&admin, "admin")] { | |
| 16030 | + let res = book_slot(&app, who, &slot).await; | |
| 16031 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "{label}: {}", res.body); | |
| 16032 | + } | |
| 16033 | + // A parent books for themselves — the parent-teacher conference. | |
| 16034 | + let res = book_slot(&app, &anne, &slot).await; | |
| 16035 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 16036 | + let second = publish_slot(&app, &ali, now + 3 * HOUR_MS, now + 4 * HOUR_MS).await; | |
| 16037 | + let res = book_slot(&app, &veli, &second).await; | |
| 16038 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 16039 | +} | |
| 16040 | + | |
| 16041 | +#[tokio::test] | |
| 16042 | +async fn only_the_slots_teacher_decides_its_bookings() { | |
| 16043 | + let (app, db) = app_and_db().await; | |
| 16044 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 16045 | + let mert = login_as(&app, &db, "mert", "teacher").await; | |
| 16046 | + let mudur = login_as(&app, &db, "mudur", "manager").await; | |
| 16047 | + let veli = login(&app, "veli").await; | |
| 16048 | + let now = Timestamp::now().as_millis(); | |
| 16049 | + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 16050 | + let booking = book_ok(&app, &veli, &slot).await; | |
| 16051 | + | |
| 16052 | + // Another teacher's calendar is not theirs to run. | |
| 16053 | + for action in ["approve", "reject"] { | |
| 16054 | + let res = decide(&app, &mert, &booking, action).await; | |
| 16055 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "{action}: {}", res.body); | |
| 16056 | + } | |
| 16057 | + let res = propose(&app, &mert, &booking, now + 3 * HOUR_MS, now + 4 * HOUR_MS).await; | |
| 16058 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); | |
| 16059 | + let res = send( | |
| 16060 | + &app, | |
| 16061 | + "DELETE", | |
| 16062 | + &format!("/appointments/slots/{slot}"), | |
| 16063 | + Some(&mert), | |
| 16064 | + None, | |
| 16065 | + ) | |
| 16066 | + .await; | |
| 16067 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); | |
| 16068 | + // The requester cannot approve their own request either. | |
| 16069 | + assert_eq!( | |
| 16070 | + decide(&app, &veli, &booking, "approve").await.status, | |
| 16071 | + StatusCode::FORBIDDEN | |
| 16072 | + ); | |
| 16073 | + // A manager may decide anyone's. | |
| 16074 | + let res = decide(&app, &mudur, &booking, "approve").await; | |
| 16075 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 16076 | + assert_eq!(res.body["status"], "approved"); | |
| 16077 | +} | |
| 16078 | + | |
| 16079 | +#[tokio::test] | |
| 16080 | +async fn appointment_times_must_be_sane_and_future() { | |
| 16081 | + let (app, db) = app_and_db().await; | |
| 16082 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 16083 | + let veli = login(&app, "veli").await; | |
| 16084 | + let now = Timestamp::now().as_millis(); | |
| 16085 | + | |
| 16086 | + // Past (beyond the 60s grace), empty, and inverted windows. | |
| 16087 | + for (body, label) in [ | |
| 16088 | + ( | |
| 16089 | + json!({ "starts_at": now - 10 * HOUR_MS, "ends_at": now - 9 * HOUR_MS }), | |
| 16090 | + "past", | |
| 16091 | + ), | |
| 16092 | + ( | |
| 16093 | + json!({ "starts_at": now + HOUR_MS, "ends_at": now + HOUR_MS }), | |
| 16094 | + "empty", | |
| 16095 | + ), | |
| 16096 | + ( | |
| 16097 | + json!({ "starts_at": now + 2 * HOUR_MS, "ends_at": now + HOUR_MS }), | |
| 16098 | + "inverted", | |
| 16099 | + ), | |
| 16100 | + ] { | |
| 16101 | + let res = publish_slots(&app, &ali, body).await; | |
| 16102 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{label}: {}", res.body); | |
| 16103 | + } | |
| 16104 | + | |
| 16105 | + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 16106 | + let booking = book_ok(&app, &veli, &slot).await; | |
| 16107 | + // A counter-proposal answers to the same rules. | |
| 16108 | + for (starts_at, ends_at, label) in [ | |
| 16109 | + (now - 10 * HOUR_MS, now - 9 * HOUR_MS, "past"), | |
| 16110 | + (now + HOUR_MS, now + HOUR_MS, "empty"), | |
| 16111 | + (now + 2 * HOUR_MS, now + HOUR_MS, "inverted"), | |
| 16112 | + ] { | |
| 16113 | + let res = propose(&app, &ali, &booking, starts_at, ends_at).await; | |
| 16114 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{label}: {}", res.body); | |
| 16115 | + } | |
| 16116 | + // A blank reason is refused too — the teacher decides on it. | |
| 16117 | + let res = send( | |
| 16118 | + &app, | |
| 16119 | + "POST", | |
| 16120 | + "/appointments", | |
| 16121 | + Some(&veli), | |
| 16122 | + Some(json!({ "slot": slot, "reason": "" })), | |
| 16123 | + ) | |
| 16124 | + .await; | |
| 16125 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | |
| 16126 | +} | |
| 16127 | + | |
| 16128 | +#[tokio::test] | |
| 16129 | +async fn a_demoted_teachers_slots_go_inert() { | |
| 16130 | + let (app, db) = app_and_db().await; | |
| 16131 | + let ali = login_as(&app, &db, "ali", "teacher").await; | |
| 16132 | + let veli = login(&app, "veli").await; | |
| 16133 | + let now = Timestamp::now().as_millis(); | |
| 16134 | + let slot = publish_slot(&app, &ali, now + HOUR_MS, now + 2 * HOUR_MS).await; | |
| 16135 | + | |
| 16136 | + let res = send(&app, "GET", "/appointments/slots", Some(&veli), None).await; | |
| 16137 | + assert_eq!(common::items(&res.body).len(), 1, "{}", res.body); | |
| 16138 | + | |
| 16139 | + // The row survives the demotion; the offer does not. | |
| 16140 | + set_role(&db, "ali", "student").await; | |
| 16141 | + let res = send(&app, "GET", "/appointments/slots", Some(&veli), None).await; | |
| 16142 | + assert_eq!(common::total(&res.body), 0, "an inert slot is not offered"); | |
| 16143 | + let res = book_slot(&app, &veli, &slot).await; | |
| 16144 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 16145 | +} |
tests/pagination.rsmodified+183 -0
| @@ -132,6 +132,189 @@ async fn paged_roster_still_embeds_people_on_the_page() { | ||
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | +/// Far enough ahead to clear the not-in-the-past guard without touching the | |
| 136 | +/// clock, so the appointment lists below never race the wall clock. | |
| 137 | +const SLOT_START: i64 = 1_900_000_000_000; | |
| 138 | +const WEEK: i64 = 7 * 24 * 60 * 60 * 1000; | |
| 139 | + | |
| 140 | +/// Publish `n` weekly occurrences as `teacher`; returns their ids, earliest | |
| 141 | +/// first — the order both appointment lists page in. | |
| 142 | +async fn publish_slots(app: &axum::Router, teacher: &str, n: i64) -> Vec<String> { | |
| 143 | + let res = send( | |
| 144 | + app, | |
| 145 | + "POST", | |
| 146 | + "/appointments/slots", | |
| 147 | + Some(teacher), | |
| 148 | + Some(json!({ | |
| 149 | + "starts_at": SLOT_START, | |
| 150 | + "ends_at": SLOT_START + 1_800_000, | |
| 151 | + "repeat_weekly": true, | |
| 152 | + "until": SLOT_START + (n - 1) * WEEK, | |
| 153 | + })), | |
| 154 | + ) | |
| 155 | + .await; | |
| 156 | + assert_eq!( | |
| 157 | + res.status, | |
| 158 | + StatusCode::CREATED, | |
| 159 | + "publish slots: {}", | |
| 160 | + res.body | |
| 161 | + ); | |
| 162 | + let slots = res | |
| 163 | + .body | |
| 164 | + .as_array() | |
| 165 | + .expect("publish returns an array") | |
| 166 | + .clone(); | |
| 167 | + assert_eq!(slots.len() as i64, n, "one row per week"); | |
| 168 | + slots | |
| 169 | + .iter() | |
| 170 | + .map(|slot| slot["id"].as_str().unwrap().to_string()) | |
| 171 | + .collect() | |
| 172 | +} | |
| 173 | + | |
| 174 | +#[tokio::test] | |
| 175 | +async fn appointment_slots_are_paged() { | |
| 176 | + let (app, db) = app_and_db().await; | |
| 177 | + let teacher = login_as(&app, &db, "ali", "teacher").await; | |
| 178 | + publish_slots(&app, &teacher, 6).await; | |
| 179 | + | |
| 180 | + // Unpaged: the whole calendar, `limit` echoes null, `total` counts it all. | |
| 181 | + let res = send(&app, "GET", "/appointments/slots", Some(&teacher), None).await; | |
| 182 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 183 | + assert_eq!(total(&res.body), 6); | |
| 184 | + assert_eq!(items(&res.body).len(), 6, "omitting limit returns them all"); | |
| 185 | + assert!(res.body["limit"].is_null()); | |
| 186 | + assert_eq!(res.body["offset"], 0); | |
| 187 | + | |
| 188 | + // A window: `total` still carries the unpaged count. | |
| 189 | + let res = send( | |
| 190 | + &app, | |
| 191 | + "GET", | |
| 192 | + "/appointments/slots?limit=2&offset=4", | |
| 193 | + Some(&teacher), | |
| 194 | + None, | |
| 195 | + ) | |
| 196 | + .await; | |
| 197 | + assert_eq!(total(&res.body), 6, "total is the unpaged count"); | |
| 198 | + assert_eq!(items(&res.body).len(), 2); | |
| 199 | + assert_eq!(res.body["limit"], 2); | |
| 200 | + assert_eq!(res.body["offset"], 4); | |
| 201 | + | |
| 202 | + // Past the end — empty page, honest total, and the person join still rides | |
| 203 | + // along on the rows that do come back. | |
| 204 | + let res = send( | |
| 205 | + &app, | |
| 206 | + "GET", | |
| 207 | + "/appointments/slots?limit=2&offset=99", | |
| 208 | + Some(&teacher), | |
| 209 | + None, | |
| 210 | + ) | |
| 211 | + .await; | |
| 212 | + assert_eq!(res.status, StatusCode::OK); | |
| 213 | + assert!(items(&res.body).is_empty()); | |
| 214 | + assert_eq!(total(&res.body), 6); | |
| 215 | + | |
| 216 | + // A student browses the same calendar — bookable slots, paged identically. | |
| 217 | + let student = login(&app, "ayse").await; | |
| 218 | + let res = send( | |
| 219 | + &app, | |
| 220 | + "GET", | |
| 221 | + "/appointments/slots?limit=3&offset=0", | |
| 222 | + Some(&student), | |
| 223 | + None, | |
| 224 | + ) | |
| 225 | + .await; | |
| 226 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 227 | + assert_eq!(total(&res.body), 6); | |
| 228 | + let page = items(&res.body); | |
| 229 | + assert_eq!(page.len(), 3); | |
| 230 | + for slot in page { | |
| 231 | + assert_eq!( | |
| 232 | + slot["teacher"]["username"], "ali", | |
| 233 | + "teacher ref present on the page: {slot}" | |
| 234 | + ); | |
| 235 | + } | |
| 236 | +} | |
| 237 | + | |
| 238 | +#[tokio::test] | |
| 239 | +async fn appointments_are_paged() { | |
| 240 | + let (app, db) = app_and_db().await; | |
| 241 | + let teacher = login_as(&app, &db, "ali", "teacher").await; | |
| 242 | + let student = login(&app, "ayse").await; | |
| 243 | + // Five weekly slots, all booked by the same student. Distinct weeks, so no | |
| 244 | + // booking collides with another. | |
| 245 | + let slots = publish_slots(&app, &teacher, 5).await; | |
| 246 | + for slot in &slots { | |
| 247 | + let res = send( | |
| 248 | + &app, | |
| 249 | + "POST", | |
| 250 | + "/appointments", | |
| 251 | + Some(&student), | |
| 252 | + Some(json!({ "slot": slot, "reason": "ödev" })), | |
| 253 | + ) | |
| 254 | + .await; | |
| 255 | + assert_eq!(res.status, StatusCode::CREATED, "book {slot}: {}", res.body); | |
| 256 | + } | |
| 257 | + | |
| 258 | + // Unpaged: every booking the student asked for. | |
| 259 | + let res = send(&app, "GET", "/appointments", Some(&student), None).await; | |
| 260 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 261 | + assert_eq!(total(&res.body), 5); | |
| 262 | + assert_eq!(items(&res.body).len(), 5, "omitting limit returns them all"); | |
| 263 | + assert!(res.body["limit"].is_null()); | |
| 264 | + assert_eq!(res.body["offset"], 0); | |
| 265 | + | |
| 266 | + // Consecutive windows are disjoint and share one total. | |
| 267 | + let res = send( | |
| 268 | + &app, | |
| 269 | + "GET", | |
| 270 | + "/appointments?limit=2&offset=0", | |
| 271 | + Some(&student), | |
| 272 | + None, | |
| 273 | + ) | |
| 274 | + .await; | |
| 275 | + assert_eq!(total(&res.body), 5, "total is the unpaged count"); | |
| 276 | + assert_eq!(res.body["limit"], 2); | |
| 277 | + let page1: Vec<_> = items(&res.body).iter().map(|a| a["id"].clone()).collect(); | |
| 278 | + let res = send( | |
| 279 | + &app, | |
| 280 | + "GET", | |
| 281 | + "/appointments?limit=2&offset=2", | |
| 282 | + Some(&student), | |
| 283 | + None, | |
| 284 | + ) | |
| 285 | + .await; | |
| 286 | + let page2: Vec<_> = items(&res.body).iter().map(|a| a["id"].clone()).collect(); | |
| 287 | + assert!(page2.iter().all(|id| !page1.contains(id))); | |
| 288 | + | |
| 289 | + // The tail is the remainder, and the slot/person join runs over the page. | |
| 290 | + let res = send( | |
| 291 | + &app, | |
| 292 | + "GET", | |
| 293 | + "/appointments?limit=2&offset=4", | |
| 294 | + Some(&student), | |
| 295 | + None, | |
| 296 | + ) | |
| 297 | + .await; | |
| 298 | + assert_eq!(total(&res.body), 5); | |
| 299 | + let page = items(&res.body); | |
| 300 | + assert_eq!(page.len(), 1, "offset 4 of 5 leaves 1"); | |
| 301 | + assert_eq!(page[0]["requester"]["username"], "ayse"); | |
| 302 | + assert_eq!(page[0]["teacher"]["username"], "ali"); | |
| 303 | + | |
| 304 | + // The teacher's inbox is the same five bookings, paged the same way. | |
| 305 | + let res = send( | |
| 306 | + &app, | |
| 307 | + "GET", | |
| 308 | + "/appointments?limit=3&offset=0", | |
| 309 | + Some(&teacher), | |
| 310 | + None, | |
| 311 | + ) | |
| 312 | + .await; | |
| 313 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 314 | + assert_eq!(total(&res.body), 5, "the teacher's inbox counts all five"); | |
| 315 | + assert_eq!(items(&res.body).len(), 3); | |
| 316 | +} | |
| 317 | + | |
| 135 | 318 | #[tokio::test] |
| 136 | 319 | async fn user_search_is_paged_not_capped() { |
| 137 | 320 | // `/users/search` used to hard-cap at 10 rows; it now pages like the rest. |
tests/persistence.rsmodified+154 -0
| @@ -574,6 +574,160 @@ async fn legacy_exams_backfill_to_published() { | ||
| 574 | 574 | assert_eq!(common::items(&listed.body).len(), 1); |
| 575 | 575 | } |
| 576 | 576 | |
| 577 | +/// A recurring publish and the booking sitting on it survive a second boot: | |
| 578 | +/// the slot's `note` and shared `series`, and the booking's counter-proposal | |
| 579 | +/// triple, `decided_by` and `status`, all come back untouched by the | |
| 580 | +/// re-applied migration. Every time in this schema is a unix-millisecond | |
| 581 | +/// `TYPE int` — never a `datetime` — so the round trip is asserted on odd | |
| 582 | +/// millisecond values, which any lossy conversion would round off. | |
| 583 | +#[tokio::test] | |
| 584 | +async fn appointments_survive_remigration() { | |
| 585 | + /// Far enough ahead to clear `check_not_past` without touching the clock, | |
| 586 | + /// and deliberately not a round number of seconds. | |
| 587 | + const START: i64 = 1_900_000_000_123; | |
| 588 | + const END: i64 = 1_900_000_003_777; | |
| 589 | + const WEEK: i64 = 7 * 24 * 60 * 60 * 1000; | |
| 590 | + const PROPOSED_START: i64 = START + 5_000_001; | |
| 591 | + const PROPOSED_END: i64 = START + 8_000_003; | |
| 592 | + | |
| 593 | + let (app, db) = common::app_and_db().await; | |
| 594 | + let teacher_creds = json!({ "username": "ali", "password": "secret1" }); | |
| 595 | + let student_creds = json!({ "username": "ayse", "password": "secret1" }); | |
| 596 | + for creds in [&teacher_creds, &student_creds] { | |
| 597 | + assert_eq!( | |
| 598 | + send(&app, "POST", "/auth/register", None, Some((*creds).clone())) | |
| 599 | + .await | |
| 600 | + .status, | |
| 601 | + StatusCode::CREATED | |
| 602 | + ); | |
| 603 | + } | |
| 604 | + set_role(&db, "ali", "teacher").await; | |
| 605 | + let teacher = send( | |
| 606 | + &app, | |
| 607 | + "POST", | |
| 608 | + "/auth/login", | |
| 609 | + None, | |
| 610 | + Some(teacher_creds.clone()), | |
| 611 | + ) | |
| 612 | + .await | |
| 613 | + .cookie | |
| 614 | + .unwrap(); | |
| 615 | + let student = send( | |
| 616 | + &app, | |
| 617 | + "POST", | |
| 618 | + "/auth/login", | |
| 619 | + None, | |
| 620 | + Some(student_creds.clone()), | |
| 621 | + ) | |
| 622 | + .await | |
| 623 | + .cookie | |
| 624 | + .unwrap(); | |
| 625 | + | |
| 626 | + // A weekly publish: three occurrences sharing one series id and one note. | |
| 627 | + let res = send( | |
| 628 | + &app, | |
| 629 | + "POST", | |
| 630 | + "/appointments/slots", | |
| 631 | + Some(&teacher), | |
| 632 | + Some(json!({ | |
| 633 | + "starts_at": START, | |
| 634 | + "ends_at": END, | |
| 635 | + "note": "veli görüşmesi", | |
| 636 | + "repeat_weekly": true, | |
| 637 | + "until": START + 2 * WEEK, | |
| 638 | + })), | |
| 639 | + ) | |
| 640 | + .await; | |
| 641 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 642 | + let published = res | |
| 643 | + .body | |
| 644 | + .as_array() | |
| 645 | + .expect("publish returns an array") | |
| 646 | + .clone(); | |
| 647 | + assert_eq!(published.len(), 3, "{}", res.body); | |
| 648 | + let slot_id = published[0]["id"].as_str().unwrap().to_string(); | |
| 649 | + let series = published[0]["series"].as_str().unwrap().to_string(); | |
| 650 | + let slot_created_at = published[0]["created_at"].as_i64().unwrap(); | |
| 651 | + | |
| 652 | + // Book the first occurrence, counter-propose another time, accept it — the | |
| 653 | + // row then carries every optional column at once: a proposal triple, a | |
| 654 | + // decider, and `approved`. | |
| 655 | + let res = send( | |
| 656 | + &app, | |
| 657 | + "POST", | |
| 658 | + "/appointments", | |
| 659 | + Some(&student), | |
| 660 | + Some(json!({ "slot": slot_id, "reason": "ödev" })), | |
| 661 | + ) | |
| 662 | + .await; | |
| 663 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 664 | + let appointment = common::id_of(&res.body); | |
| 665 | + let created_at = res.body["created_at"].as_i64().unwrap(); | |
| 666 | + let res = send( | |
| 667 | + &app, | |
| 668 | + "PATCH", | |
| 669 | + &format!("/appointments/{appointment}/reschedule"), | |
| 670 | + Some(&teacher), | |
| 671 | + Some(json!({ "starts_at": PROPOSED_START, "ends_at": PROPOSED_END })), | |
| 672 | + ) | |
| 673 | + .await; | |
| 674 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 675 | + let res = send( | |
| 676 | + &app, | |
| 677 | + "PATCH", | |
| 678 | + &format!("/appointments/{appointment}/reschedule/accept"), | |
| 679 | + Some(&student), | |
| 680 | + None, | |
| 681 | + ) | |
| 682 | + .await; | |
| 683 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 684 | + | |
| 685 | + // Second boot: migration re-applied over live data. | |
| 686 | + let app = reboot(&db).await; | |
| 687 | + let teacher = send(&app, "POST", "/auth/login", None, Some(teacher_creds)) | |
| 688 | + .await | |
| 689 | + .cookie | |
| 690 | + .unwrap(); | |
| 691 | + let student = send(&app, "POST", "/auth/login", None, Some(student_creds)) | |
| 692 | + .await | |
| 693 | + .cookie | |
| 694 | + .unwrap(); | |
| 695 | + | |
| 696 | + let res = send(&app, "GET", "/appointments/slots", Some(&teacher), None).await; | |
| 697 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 698 | + assert_eq!(common::total(&res.body), 3, "{}", res.body); | |
| 699 | + let slots = common::items(&res.body); | |
| 700 | + assert_eq!(slots[0]["id"], slot_id.as_str()); | |
| 701 | + assert_eq!(slots[0]["starts_at"], START, "exact unix-ms, not rounded"); | |
| 702 | + assert_eq!(slots[0]["ends_at"], END); | |
| 703 | + assert_eq!(slots[0]["note"], "veli görüşmesi"); | |
| 704 | + assert_eq!(slots[0]["created_at"], slot_created_at); | |
| 705 | + for (week, slot) in slots.iter().enumerate() { | |
| 706 | + assert_eq!(slot["series"], series.as_str(), "one series, all weeks"); | |
| 707 | + assert_eq!(slot["starts_at"], START + week as i64 * WEEK); | |
| 708 | + assert_eq!(slot["ends_at"], END + week as i64 * WEEK); | |
| 709 | + } | |
| 710 | + | |
| 711 | + let res = send(&app, "GET", "/appointments", Some(&student), None).await; | |
| 712 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 713 | + assert_eq!(common::total(&res.body), 1, "{}", res.body); | |
| 714 | + let booking = &common::items(&res.body)[0]; | |
| 715 | + assert_eq!(booking["id"], appointment.as_str()); | |
| 716 | + assert_eq!(booking["slot"], slot_id.as_str()); | |
| 717 | + assert_eq!(booking["status"], "approved"); | |
| 718 | + assert_eq!(booking["reason"], "ödev"); | |
| 719 | + assert_eq!(booking["proposed_starts_at"], PROPOSED_START); | |
| 720 | + assert_eq!(booking["proposed_ends_at"], PROPOSED_END); | |
| 721 | + assert_eq!(booking["proposed_by"]["username"], "ali"); | |
| 722 | + assert_eq!(booking["decided_by"]["username"], "ayse"); | |
| 723 | + // The accepted proposal *is* the meeting's time; the slot's own window lost. | |
| 724 | + assert_eq!(booking["starts_at"], PROPOSED_START); | |
| 725 | + assert_eq!(booking["ends_at"], PROPOSED_END); | |
| 726 | + assert_eq!(booking["teacher"]["username"], "ali"); | |
| 727 | + assert_eq!(booking["requester"]["username"], "ayse"); | |
| 728 | + assert_eq!(booking["created_at"], created_at); | |
| 729 | +} | |
| 730 | + | |
| 577 | 731 | /// A chatbot thread and every turn in it die together: deleting the |
| 578 | 732 | /// conversation cascades its `chat_message` rows in one transaction, so no |
| 579 | 733 | /// orphan survives a reboot. And the thread is owner-scoped end to end — a |