Settings, dynamic kinds/statuses, bands, terms
Browse files at this commite7fdc28f886e8c55393fc8f9310966d6d76cd69fParent: ea8bfeb- test
- settings, dynamic kinds/statuses, bands, terms
2 changed files +718 -0
- tests/integration.rs +638 -0
- tests/persistence.rs +80 -0
tests/integration.rsmodified+638 -0
| @@ -6537,3 +6537,641 @@ async fn course_catalog_lists_a_creator_enrolled_course_once() { | ||
| 6537 | 6537 | assert_eq!(exams.len(), 1, "one exam listed once: {exams:?}"); |
| 6538 | 6538 | assert_eq!(exams[0]["id"], json!(exam)); |
| 6539 | 6539 | } |
| 6540 | + | |
| 6541 | +// --- school settings + terms ---------------------------------------------- | |
| 6542 | + | |
| 6543 | +#[tokio::test] | |
| 6544 | +async fn settings_serve_defaults_and_gate_edits_to_manager() { | |
| 6545 | + let (app, db) = app_and_db().await; | |
| 6546 | + let student = login(&app, "set.student").await; | |
| 6547 | + let teacher = login_as(&app, &db, "set.teacher", "teacher").await; | |
| 6548 | + let manager = login_as(&app, &db, "set.manager", "manager").await; | |
| 6549 | + | |
| 6550 | + // Reading requires a session; the policy is not public. | |
| 6551 | + let res = send(&app, "GET", "/settings", None, None).await; | |
| 6552 | + assert_eq!(res.status, StatusCode::UNAUTHORIZED); | |
| 6553 | + | |
| 6554 | + // Any authenticated user reads; the defaults mirror the old constants. | |
| 6555 | + let res = send(&app, "GET", "/settings", Some(&student), None).await; | |
| 6556 | + assert_eq!(res.status, StatusCode::OK); | |
| 6557 | + assert_eq!( | |
| 6558 | + res.body["exam_kinds"], | |
| 6559 | + json!(["homework", "quiz", "midterm", "final", "project", "oral"]) | |
| 6560 | + ); | |
| 6561 | + assert_eq!( | |
| 6562 | + res.body["attendance_statuses"], | |
| 6563 | + json!(["present", "absent", "late", "excused"]) | |
| 6564 | + ); | |
| 6565 | + assert_eq!(res.body["grade_bands"], json!([])); | |
| 6566 | + | |
| 6567 | + // Students and teachers cannot edit school policy. | |
| 6568 | + for cookie in [&student, &teacher] { | |
| 6569 | + let res = send( | |
| 6570 | + &app, | |
| 6571 | + "PATCH", | |
| 6572 | + "/settings", | |
| 6573 | + Some(cookie), | |
| 6574 | + Some(json!({ "exam_kinds": ["lab"] })), | |
| 6575 | + ) | |
| 6576 | + .await; | |
| 6577 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | |
| 6578 | + } | |
| 6579 | + | |
| 6580 | + // A manager edits one field; omitted fields keep their value. | |
| 6581 | + let res = send( | |
| 6582 | + &app, | |
| 6583 | + "PATCH", | |
| 6584 | + "/settings", | |
| 6585 | + Some(&manager), | |
| 6586 | + Some(json!({ "exam_kinds": ["lab", "Quiz"] })), | |
| 6587 | + ) | |
| 6588 | + .await; | |
| 6589 | + assert_eq!(res.status, StatusCode::OK); | |
| 6590 | + assert_eq!(res.body["exam_kinds"], json!(["lab", "Quiz"])); | |
| 6591 | + assert_eq!( | |
| 6592 | + res.body["attendance_statuses"], | |
| 6593 | + json!(["present", "absent", "late", "excused"]) | |
| 6594 | + ); | |
| 6595 | + | |
| 6596 | + // Every reader sees the new policy at once. | |
| 6597 | + let res = send(&app, "GET", "/settings", Some(&student), None).await; | |
| 6598 | + assert_eq!(res.body["exam_kinds"], json!(["lab", "Quiz"])); | |
| 6599 | +} | |
| 6600 | + | |
| 6601 | +#[tokio::test] | |
| 6602 | +async fn settings_validation_rejects_bad_lists_and_bands() { | |
| 6603 | + let (app, db) = app_and_db().await; | |
| 6604 | + let manager = login_as(&app, &db, "val.manager", "manager").await; | |
| 6605 | + | |
| 6606 | + let bad = [ | |
| 6607 | + json!({ "exam_kinds": [] }), // a school needs at least one kind | |
| 6608 | + json!({ "exam_kinds": [" "] }), // blank entry | |
| 6609 | + json!({ "exam_kinds": ["Lab", "lab"] }), // case-insensitive duplicate | |
| 6610 | + json!({ "attendance_statuses": ["present", "absent", "late"] }), // core dropped | |
| 6611 | + json!({ "grade_bands": [{ "min": 50, "label": "CC" }] }), // no band starts at 0 | |
| 6612 | + json!({ "grade_bands": [{ "min": 0, "label": "F" }, { "min": 0, "label": "E" }] }), | |
| 6613 | + json!({ "grade_bands": [{ "min": -1, "label": "F" }] }), | |
| 6614 | + json!({ "grade_bands": [{ "min": 101, "label": "A" }] }), | |
| 6615 | + json!({ "grade_bands": [{ "min": 0, "label": " " }] }), | |
| 6616 | + ]; | |
| 6617 | + for body in bad { | |
| 6618 | + let res = send(&app, "PATCH", "/settings", Some(&manager), Some(body.clone())).await; | |
| 6619 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "should reject {body}"); | |
| 6620 | + } | |
| 6621 | + | |
| 6622 | + // No failed PATCH half-applied anything. | |
| 6623 | + let res = send(&app, "GET", "/settings", Some(&manager), None).await; | |
| 6624 | + assert_eq!(res.body["exam_kinds"].as_array().unwrap().len(), 6); | |
| 6625 | + assert_eq!(res.body["grade_bands"], json!([])); | |
| 6626 | +} | |
| 6627 | + | |
| 6628 | +#[tokio::test] | |
| 6629 | +async fn exam_kinds_follow_settings_for_new_writes_only() { | |
| 6630 | + let (app, db) = app_and_db().await; | |
| 6631 | + let teacher = login_as(&app, &db, "kind.teacher", "teacher").await; | |
| 6632 | + let manager = login_as(&app, &db, "kind.manager", "manager").await; | |
| 6633 | + | |
| 6634 | + let course = create_course(&app, &teacher, "Chemistry").await; | |
| 6635 | + let exam = create_exam(&app, &teacher, &course, "Midterm", "midterm", 1).await; | |
| 6636 | + | |
| 6637 | + // The school swaps its kind list wholesale. | |
| 6638 | + let res = send( | |
| 6639 | + &app, | |
| 6640 | + "PATCH", | |
| 6641 | + "/settings", | |
| 6642 | + Some(&manager), | |
| 6643 | + Some(json!({ "exam_kinds": ["lab"] })), | |
| 6644 | + ) | |
| 6645 | + .await; | |
| 6646 | + assert_eq!(res.status, StatusCode::OK); | |
| 6647 | + | |
| 6648 | + // New exams are held to the new list. | |
| 6649 | + let res = create_exam_with( | |
| 6650 | + &app, | |
| 6651 | + &teacher, | |
| 6652 | + &course, | |
| 6653 | + json!({ "title": "Retired kind", "kind": "midterm", "weight": 1 }), | |
| 6654 | + ) | |
| 6655 | + .await; | |
| 6656 | + assert_eq!(res.status, StatusCode::BAD_REQUEST); | |
| 6657 | + let res = create_exam_with( | |
| 6658 | + &app, | |
| 6659 | + &teacher, | |
| 6660 | + &course, | |
| 6661 | + json!({ "title": "Lab 1", "kind": "lab", "weight": 1 }), | |
| 6662 | + ) | |
| 6663 | + .await; | |
| 6664 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 6665 | + | |
| 6666 | + // The stored exam keeps its retired kind: a title-only PATCH passes… | |
| 6667 | + let res = send( | |
| 6668 | + &app, | |
| 6669 | + "PATCH", | |
| 6670 | + &format!("/exams/{exam}"), | |
| 6671 | + Some(&teacher), | |
| 6672 | + Some(json!({ "title": "Midterm A" })), | |
| 6673 | + ) | |
| 6674 | + .await; | |
| 6675 | + assert_eq!(res.status, StatusCode::OK); | |
| 6676 | + assert_eq!(res.body["kind"], "midterm"); | |
| 6677 | + // …but a kind this request sets is validated against the current list. | |
| 6678 | + let res = send( | |
| 6679 | + &app, | |
| 6680 | + "PATCH", | |
| 6681 | + &format!("/exams/{exam}"), | |
| 6682 | + Some(&teacher), | |
| 6683 | + Some(json!({ "kind": "midterm" })), | |
| 6684 | + ) | |
| 6685 | + .await; | |
| 6686 | + assert_eq!(res.status, StatusCode::BAD_REQUEST); | |
| 6687 | + let res = send( | |
| 6688 | + &app, | |
| 6689 | + "PATCH", | |
| 6690 | + &format!("/exams/{exam}"), | |
| 6691 | + Some(&teacher), | |
| 6692 | + Some(json!({ "kind": "lab" })), | |
| 6693 | + ) | |
| 6694 | + .await; | |
| 6695 | + assert_eq!(res.status, StatusCode::OK); | |
| 6696 | + assert_eq!(res.body["kind"], "lab"); | |
| 6697 | +} | |
| 6698 | + | |
| 6699 | +#[tokio::test] | |
| 6700 | +async fn attendance_statuses_follow_settings_and_bucket_in_reports() { | |
| 6701 | + let (app, db) = app_and_db().await; | |
| 6702 | + let manager = login_as(&app, &db, "att.manager", "manager").await; | |
| 6703 | + let student = login(&app, "att.student").await; | |
| 6704 | + | |
| 6705 | + // Add a school-specific status on top of the mandatory core. | |
| 6706 | + let res = send( | |
| 6707 | + &app, | |
| 6708 | + "PATCH", | |
| 6709 | + "/settings", | |
| 6710 | + Some(&manager), | |
| 6711 | + Some(json!({ | |
| 6712 | + "attendance_statuses": ["present", "absent", "late", "excused", "online"] | |
| 6713 | + })), | |
| 6714 | + ) | |
| 6715 | + .await; | |
| 6716 | + assert_eq!(res.status, StatusCode::OK); | |
| 6717 | + | |
| 6718 | + let res = send( | |
| 6719 | + &app, | |
| 6720 | + "POST", | |
| 6721 | + "/events", | |
| 6722 | + Some(&manager), | |
| 6723 | + Some(json!({ "title": "Assembly" })), | |
| 6724 | + ) | |
| 6725 | + .await; | |
| 6726 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 6727 | + let event = id_of(&res.body); | |
| 6728 | + | |
| 6729 | + // The custom status is markable; garbage still is not. | |
| 6730 | + let res = send( | |
| 6731 | + &app, | |
| 6732 | + "POST", | |
| 6733 | + &format!("/events/{event}/attendance"), | |
| 6734 | + Some(&student), | |
| 6735 | + Some(json!({ "status": "online" })), | |
| 6736 | + ) | |
| 6737 | + .await; | |
| 6738 | + assert_eq!(res.status, StatusCode::OK); | |
| 6739 | + let res = send( | |
| 6740 | + &app, | |
| 6741 | + "POST", | |
| 6742 | + &format!("/events/{event}/attendance"), | |
| 6743 | + Some(&student), | |
| 6744 | + Some(json!({ "status": "maybe" })), | |
| 6745 | + ) | |
| 6746 | + .await; | |
| 6747 | + assert_eq!(res.status, StatusCode::BAD_REQUEST); | |
| 6748 | + | |
| 6749 | + // The report buckets it under `custom` and keeps it out of the rate. | |
| 6750 | + let res = send(&app, "GET", "/attendance/me", Some(&student), None).await; | |
| 6751 | + assert_eq!(res.status, StatusCode::OK); | |
| 6752 | + assert_eq!(res.body["events"]["custom"]["online"], 1); | |
| 6753 | + assert_eq!(res.body["events"]["total"], 1); | |
| 6754 | + assert_eq!(res.body["events"]["rate"], serde_json::Value::Null); | |
| 6755 | + | |
| 6756 | + // Retiring the status blocks new marks; the stored row keeps reporting. | |
| 6757 | + let res = send( | |
| 6758 | + &app, | |
| 6759 | + "PATCH", | |
| 6760 | + "/settings", | |
| 6761 | + Some(&manager), | |
| 6762 | + Some(json!({ | |
| 6763 | + "attendance_statuses": ["present", "absent", "late", "excused"] | |
| 6764 | + })), | |
| 6765 | + ) | |
| 6766 | + .await; | |
| 6767 | + assert_eq!(res.status, StatusCode::OK); | |
| 6768 | + let res = send( | |
| 6769 | + &app, | |
| 6770 | + "POST", | |
| 6771 | + &format!("/events/{event}/attendance"), | |
| 6772 | + Some(&student), | |
| 6773 | + Some(json!({ "status": "online" })), | |
| 6774 | + ) | |
| 6775 | + .await; | |
| 6776 | + assert_eq!(res.status, StatusCode::BAD_REQUEST); | |
| 6777 | + let res = send(&app, "GET", "/attendance/me", Some(&student), None).await; | |
| 6778 | + assert_eq!(res.body["events"]["custom"]["online"], 1); | |
| 6779 | +} | |
| 6780 | + | |
| 6781 | +#[tokio::test] | |
| 6782 | +async fn grade_bands_label_the_marks_report() { | |
| 6783 | + let (app, db) = app_and_db().await; | |
| 6784 | + let teacher = login_as(&app, &db, "band.teacher", "teacher").await; | |
| 6785 | + let manager = login_as(&app, &db, "band.manager", "manager").await; | |
| 6786 | + let student = login(&app, "band.student").await; | |
| 6787 | + let student_id = me_id(&app, &student).await; | |
| 6788 | + | |
| 6789 | + let res = send( | |
| 6790 | + &app, | |
| 6791 | + "PATCH", | |
| 6792 | + "/settings", | |
| 6793 | + Some(&manager), | |
| 6794 | + Some(json!({ | |
| 6795 | + "grade_bands": [ | |
| 6796 | + { "min": 0, "label": "FF" }, | |
| 6797 | + { "min": 50, "label": "CC" }, | |
| 6798 | + { "min": 85, "label": "AA" }, | |
| 6799 | + ] | |
| 6800 | + })), | |
| 6801 | + ) | |
| 6802 | + .await; | |
| 6803 | + assert_eq!(res.status, StatusCode::OK); | |
| 6804 | + // Bands echo highest-first — the canonical order. | |
| 6805 | + assert_eq!(res.body["grade_bands"][0]["label"], "AA"); | |
| 6806 | + | |
| 6807 | + let course = create_course(&app, &teacher, "Physics").await; | |
| 6808 | + enroll(&app, &teacher, &course, &student_id).await; | |
| 6809 | + let exam = create_exam(&app, &teacher, &course, "Final", "final", 2).await; | |
| 6810 | + let res = send( | |
| 6811 | + &app, | |
| 6812 | + "POST", | |
| 6813 | + &format!("/exams/{exam}/results"), | |
| 6814 | + Some(&teacher), | |
| 6815 | + Some(json!({ "user_id": student_id, "mark": 90 })), | |
| 6816 | + ) | |
| 6817 | + .await; | |
| 6818 | + assert_eq!(res.status, StatusCode::OK); | |
| 6819 | + | |
| 6820 | + let res = send(&app, "GET", "/marks/me", Some(&student), None).await; | |
| 6821 | + assert_eq!(res.status, StatusCode::OK); | |
| 6822 | + let block = &res.body["courses"][0]; | |
| 6823 | + assert_eq!(block["results"][0]["mark"], 90); | |
| 6824 | + assert_eq!(block["results"][0]["grade"], "AA"); | |
| 6825 | + assert_eq!(block["average"], 90.0); | |
| 6826 | + assert_eq!(block["average_grade"], "AA"); | |
| 6827 | + assert_eq!(res.body["overall_grade"], "AA"); | |
| 6828 | + | |
| 6829 | + // Clearing the bands turns labels off without touching the numbers. | |
| 6830 | + let res = send( | |
| 6831 | + &app, | |
| 6832 | + "PATCH", | |
| 6833 | + "/settings", | |
| 6834 | + Some(&manager), | |
| 6835 | + Some(json!({ "grade_bands": [] })), | |
| 6836 | + ) | |
| 6837 | + .await; | |
| 6838 | + assert_eq!(res.status, StatusCode::OK); | |
| 6839 | + let res = send(&app, "GET", "/marks/me", Some(&student), None).await; | |
| 6840 | + assert_eq!(res.body["courses"][0]["results"][0]["mark"], 90); | |
| 6841 | + assert_eq!( | |
| 6842 | + res.body["courses"][0]["results"][0]["grade"], | |
| 6843 | + serde_json::Value::Null | |
| 6844 | + ); | |
| 6845 | + assert_eq!(res.body["overall_grade"], serde_json::Value::Null); | |
| 6846 | +} | |
| 6847 | + | |
| 6848 | +#[tokio::test] | |
| 6849 | +async fn terms_crud_gates_and_links_to_courses() { | |
| 6850 | + let (app, db) = app_and_db().await; | |
| 6851 | + let manager = login_as(&app, &db, "term.manager", "manager").await; | |
| 6852 | + let teacher = login_as(&app, &db, "term.teacher", "teacher").await; | |
| 6853 | + let student = login(&app, "term.student").await; | |
| 6854 | + | |
| 6855 | + // Writes are manager+; and a term may lie fully in the past — a school | |
| 6856 | + // adopting the app mid-year backfills its calendar, unlike the no-past | |
| 6857 | + // rule on exams/lessons/events. | |
| 6858 | + let past_term = json!({ | |
| 6859 | + "name": "2025 Fall", | |
| 6860 | + "starts_at": 1_600_000_000_000_i64, | |
| 6861 | + "ends_at": 1_610_000_000_000_i64, | |
| 6862 | + }); | |
| 6863 | + let res = send(&app, "POST", "/terms", Some(&teacher), Some(past_term.clone())).await; | |
| 6864 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | |
| 6865 | + let res = send(&app, "POST", "/terms", Some(&manager), Some(past_term)).await; | |
| 6866 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 6867 | + let term = id_of(&res.body); | |
| 6868 | + | |
| 6869 | + // The range stays ordered, merged PATCHes included. | |
| 6870 | + let res = send( | |
| 6871 | + &app, | |
| 6872 | + "POST", | |
| 6873 | + "/terms", | |
| 6874 | + Some(&manager), | |
| 6875 | + Some(json!({ "name": "Broken", "starts_at": 2, "ends_at": 1 })), | |
| 6876 | + ) | |
| 6877 | + .await; | |
| 6878 | + assert_eq!(res.status, StatusCode::BAD_REQUEST); | |
| 6879 | + let res = send( | |
| 6880 | + &app, | |
| 6881 | + "PATCH", | |
| 6882 | + &format!("/terms/{term}"), | |
| 6883 | + Some(&manager), | |
| 6884 | + Some(json!({ "ends_at": 1_500_000_000_000_i64 })), | |
| 6885 | + ) | |
| 6886 | + .await; | |
| 6887 | + assert_eq!(res.status, StatusCode::BAD_REQUEST); | |
| 6888 | + let res = send( | |
| 6889 | + &app, | |
| 6890 | + "PATCH", | |
| 6891 | + &format!("/terms/{term}"), | |
| 6892 | + Some(&manager), | |
| 6893 | + Some(json!({ "name": "2025/26 Fall" })), | |
| 6894 | + ) | |
| 6895 | + .await; | |
| 6896 | + assert_eq!(res.status, StatusCode::OK); | |
| 6897 | + assert_eq!(res.body["name"], "2025/26 Fall"); | |
| 6898 | + | |
| 6899 | + // Any authenticated user reads the calendar. | |
| 6900 | + let res = send(&app, "GET", "/terms", Some(&student), None).await; | |
| 6901 | + assert_eq!(res.status, StatusCode::OK); | |
| 6902 | + assert_eq!(res.body.as_array().unwrap().len(), 1); | |
| 6903 | + | |
| 6904 | + // Courses link to a term at creation; a bogus id is a 400, not a silent null. | |
| 6905 | + let res = send( | |
| 6906 | + &app, | |
| 6907 | + "POST", | |
| 6908 | + "/courses", | |
| 6909 | + Some(&teacher), | |
| 6910 | + Some(json!({ "title": "History", "term_id": term })), | |
| 6911 | + ) | |
| 6912 | + .await; | |
| 6913 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 6914 | + let course = id_of(&res.body); | |
| 6915 | + assert_eq!(res.body["term"].as_str(), Some(term.as_str())); | |
| 6916 | + let res = send( | |
| 6917 | + &app, | |
| 6918 | + "POST", | |
| 6919 | + "/courses", | |
| 6920 | + Some(&teacher), | |
| 6921 | + Some(json!({ "title": "Broken", "term_id": "nope" })), | |
| 6922 | + ) | |
| 6923 | + .await; | |
| 6924 | + assert_eq!(res.status, StatusCode::BAD_REQUEST); | |
| 6925 | + | |
| 6926 | + // PATCH: null unlinks, a value re-links, omitting keeps. | |
| 6927 | + let res = send( | |
| 6928 | + &app, | |
| 6929 | + "PATCH", | |
| 6930 | + &format!("/courses/{course}"), | |
| 6931 | + Some(&teacher), | |
| 6932 | + Some(json!({ "term_id": null })), | |
| 6933 | + ) | |
| 6934 | + .await; | |
| 6935 | + assert_eq!(res.status, StatusCode::OK); | |
| 6936 | + assert_eq!(res.body["term"], serde_json::Value::Null); | |
| 6937 | + let res = send( | |
| 6938 | + &app, | |
| 6939 | + "PATCH", | |
| 6940 | + &format!("/courses/{course}"), | |
| 6941 | + Some(&teacher), | |
| 6942 | + Some(json!({ "term_id": term })), | |
| 6943 | + ) | |
| 6944 | + .await; | |
| 6945 | + assert_eq!(res.body["term"].as_str(), Some(term.as_str())); | |
| 6946 | + let res = send( | |
| 6947 | + &app, | |
| 6948 | + "PATCH", | |
| 6949 | + &format!("/courses/{course}"), | |
| 6950 | + Some(&teacher), | |
| 6951 | + Some(json!({ "title": "History II" })), | |
| 6952 | + ) | |
| 6953 | + .await; | |
| 6954 | + assert_eq!(res.body["term"].as_str(), Some(term.as_str())); | |
| 6955 | + | |
| 6956 | + // Deleting the term unlinks its courses but never deletes them. | |
| 6957 | + let res = send(&app, "DELETE", &format!("/terms/{term}"), Some(&manager), None).await; | |
| 6958 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | |
| 6959 | + let res = send(&app, "GET", &format!("/terms/{term}"), Some(&student), None).await; | |
| 6960 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | |
| 6961 | + let res = send(&app, "GET", &format!("/courses/{course}"), Some(&teacher), None).await; | |
| 6962 | + assert_eq!(res.status, StatusCode::OK); | |
| 6963 | + assert_eq!(res.body["title"], "History II"); | |
| 6964 | + assert_eq!(res.body["term"], serde_json::Value::Null); | |
| 6965 | +} | |
| 6966 | + | |
| 6967 | +#[tokio::test] | |
| 6968 | +async fn roll_call_accepts_school_statuses_and_buckets_session_reports() { | |
| 6969 | + let (app, db) = app_and_db().await; | |
| 6970 | + let manager = login_as(&app, &db, "roll.manager", "manager").await; | |
| 6971 | + let teacher = login_as(&app, &db, "roll.teacher", "teacher").await; | |
| 6972 | + let student = login(&app, "roll.student").await; | |
| 6973 | + let student_id = me_id(&app, &student).await; | |
| 6974 | + | |
| 6975 | + // The event path is already covered; this proves the second marking | |
| 6976 | + // path — lesson roll call — reads the same settings list. | |
| 6977 | + let res = send( | |
| 6978 | + &app, | |
| 6979 | + "PATCH", | |
| 6980 | + "/settings", | |
| 6981 | + Some(&manager), | |
| 6982 | + Some(json!({ | |
| 6983 | + "attendance_statuses": ["present", "absent", "late", "excused", "online"] | |
| 6984 | + })), | |
| 6985 | + ) | |
| 6986 | + .await; | |
| 6987 | + assert_eq!(res.status, StatusCode::OK); | |
| 6988 | + | |
| 6989 | + let course = create_course(&app, &teacher, "Biology").await; | |
| 6990 | + enroll(&app, &teacher, &course, &student_id).await; | |
| 6991 | + let future = Timestamp::now().as_millis() + 3_600_000; | |
| 6992 | + let session = create_session(&app, &teacher, &course, future).await; | |
| 6993 | + | |
| 6994 | + // Custom status marks; garbage still dies. | |
| 6995 | + let res = send( | |
| 6996 | + &app, | |
| 6997 | + "POST", | |
| 6998 | + &format!("/sessions/{session}/attendance"), | |
| 6999 | + Some(&teacher), | |
| 7000 | + Some(json!({ "status": "online", "user_id": student_id })), | |
| 7001 | + ) | |
| 7002 | + .await; | |
| 7003 | + assert_eq!(res.status, StatusCode::OK); | |
| 7004 | + assert_eq!(res.body["status"], "online"); | |
| 7005 | + let res = send( | |
| 7006 | + &app, | |
| 7007 | + "POST", | |
| 7008 | + &format!("/sessions/{session}/attendance"), | |
| 7009 | + Some(&teacher), | |
| 7010 | + Some(json!({ "status": "onsite", "user_id": student_id })), | |
| 7011 | + ) | |
| 7012 | + .await; | |
| 7013 | + assert_eq!(res.status, StatusCode::BAD_REQUEST); | |
| 7014 | + | |
| 7015 | + // Both the overall session tally and the per-course block bucket it | |
| 7016 | + // under `custom`, rate-neutral. | |
| 7017 | + let res = send(&app, "GET", "/attendance/me", Some(&student), None).await; | |
| 7018 | + assert_eq!(res.status, StatusCode::OK); | |
| 7019 | + assert_eq!(res.body["sessions"]["custom"]["online"], 1); | |
| 7020 | + assert_eq!(res.body["sessions"]["total"], 1); | |
| 7021 | + assert_eq!(res.body["sessions"]["rate"], serde_json::Value::Null); | |
| 7022 | + let block = &res.body["courses"][0]; | |
| 7023 | + assert_eq!(block["counts"]["custom"]["online"], 1); | |
| 7024 | + assert_eq!(block["counts"]["rate"], serde_json::Value::Null); | |
| 7025 | +} | |
| 7026 | + | |
| 7027 | +#[tokio::test] | |
| 7028 | +async fn terms_list_newest_first_require_auth_and_unlink_in_bulk() { | |
| 7029 | + let (app, db) = app_and_db().await; | |
| 7030 | + let manager = login_as(&app, &db, "bulk.manager", "manager").await; | |
| 7031 | + let teacher = login_as(&app, &db, "bulk.teacher", "teacher").await; | |
| 7032 | + | |
| 7033 | + // The calendar is not public. | |
| 7034 | + let res = send(&app, "GET", "/terms", None, None).await; | |
| 7035 | + assert_eq!(res.status, StatusCode::UNAUTHORIZED); | |
| 7036 | + | |
| 7037 | + let older = send( | |
| 7038 | + &app, | |
| 7039 | + "POST", | |
| 7040 | + "/terms", | |
| 7041 | + Some(&manager), | |
| 7042 | + Some(json!({ "name": "2025 Fall", "starts_at": 1_000, "ends_at": 2_000 })), | |
| 7043 | + ) | |
| 7044 | + .await; | |
| 7045 | + let newer = send( | |
| 7046 | + &app, | |
| 7047 | + "POST", | |
| 7048 | + "/terms", | |
| 7049 | + Some(&manager), | |
| 7050 | + Some(json!({ "name": "2026 Spring", "starts_at": 5_000, "ends_at": 6_000 })), | |
| 7051 | + ) | |
| 7052 | + .await; | |
| 7053 | + let (older, newer) = (id_of(&older.body), id_of(&newer.body)); | |
| 7054 | + | |
| 7055 | + // Newest (by starts_at) first. | |
| 7056 | + let res = send(&app, "GET", "/terms", Some(&teacher), None).await; | |
| 7057 | + let names: Vec<&str> = res | |
| 7058 | + .body | |
| 7059 | + .as_array() | |
| 7060 | + .unwrap() | |
| 7061 | + .iter() | |
| 7062 | + .map(|t| t["name"].as_str().unwrap()) | |
| 7063 | + .collect(); | |
| 7064 | + assert_eq!(names, ["2026 Spring", "2025 Fall"]); | |
| 7065 | + | |
| 7066 | + // Deleting a term unlinks EVERY linked course in one stroke, and leaves | |
| 7067 | + // courses on other terms alone. | |
| 7068 | + let mut linked = Vec::new(); | |
| 7069 | + for title in ["Algebra", "Geometry"] { | |
| 7070 | + let res = send( | |
| 7071 | + &app, | |
| 7072 | + "POST", | |
| 7073 | + "/courses", | |
| 7074 | + Some(&teacher), | |
| 7075 | + Some(json!({ "title": title, "term_id": newer })), | |
| 7076 | + ) | |
| 7077 | + .await; | |
| 7078 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 7079 | + linked.push(id_of(&res.body)); | |
| 7080 | + } | |
| 7081 | + let res = send( | |
| 7082 | + &app, | |
| 7083 | + "POST", | |
| 7084 | + "/courses", | |
| 7085 | + Some(&teacher), | |
| 7086 | + Some(json!({ "title": "History", "term_id": older })), | |
| 7087 | + ) | |
| 7088 | + .await; | |
| 7089 | + let unrelated = id_of(&res.body); | |
| 7090 | + | |
| 7091 | + let res = send(&app, "DELETE", &format!("/terms/{newer}"), Some(&manager), None).await; | |
| 7092 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | |
| 7093 | + | |
| 7094 | + for course in &linked { | |
| 7095 | + let res = send(&app, "GET", &format!("/courses/{course}"), Some(&teacher), None).await; | |
| 7096 | + assert_eq!(res.body["term"], serde_json::Value::Null, "unlinked {course}"); | |
| 7097 | + } | |
| 7098 | + let res = send(&app, "GET", &format!("/courses/{unrelated}"), Some(&teacher), None).await; | |
| 7099 | + assert_eq!(res.body["term"].as_str(), Some(older.as_str())); | |
| 7100 | +} | |
| 7101 | + | |
| 7102 | +#[tokio::test] | |
| 7103 | +async fn grade_band_boundary_applies_to_the_weighted_average() { | |
| 7104 | + let (app, db) = app_and_db().await; | |
| 7105 | + let teacher = login_as(&app, &db, "edge.teacher", "teacher").await; | |
| 7106 | + let manager = login_as(&app, &db, "edge.manager", "manager").await; | |
| 7107 | + let student = login(&app, "edge.student").await; | |
| 7108 | + let student_id = me_id(&app, &student).await; | |
| 7109 | + | |
| 7110 | + let res = send( | |
| 7111 | + &app, | |
| 7112 | + "PATCH", | |
| 7113 | + "/settings", | |
| 7114 | + Some(&manager), | |
| 7115 | + Some(json!({ | |
| 7116 | + "grade_bands": [{ "min": 0, "label": "FF" }, { "min": 85, "label": "AA" }] | |
| 7117 | + })), | |
| 7118 | + ) | |
| 7119 | + .await; | |
| 7120 | + assert_eq!(res.status, StatusCode::OK); | |
| 7121 | + | |
| 7122 | + // Two equal-weight marks straddling the band edge: 80 and 90 → average | |
| 7123 | + // exactly 85.0, which is INSIDE the AA band (min is inclusive), while | |
| 7124 | + // the 80 itself still reads FF. | |
| 7125 | + let course = create_course(&app, &teacher, "Calculus").await; | |
| 7126 | + enroll(&app, &teacher, &course, &student_id).await; | |
| 7127 | + for (title, mark) in [("Quiz A", 80), ("Quiz B", 90)] { | |
| 7128 | + let exam = create_exam(&app, &teacher, &course, title, "quiz", 1).await; | |
| 7129 | + let res = send( | |
| 7130 | + &app, | |
| 7131 | + "POST", | |
| 7132 | + &format!("/exams/{exam}/results"), | |
| 7133 | + Some(&teacher), | |
| 7134 | + Some(json!({ "user_id": student_id, "mark": mark })), | |
| 7135 | + ) | |
| 7136 | + .await; | |
| 7137 | + assert_eq!(res.status, StatusCode::OK); | |
| 7138 | + } | |
| 7139 | + | |
| 7140 | + let res = send(&app, "GET", "/marks/me", Some(&student), None).await; | |
| 7141 | + let block = &res.body["courses"][0]; | |
| 7142 | + assert_eq!(block["average"], 85.0); | |
| 7143 | + assert_eq!(block["average_grade"], "AA"); | |
| 7144 | + let grades: Vec<(&str, &str)> = block["results"] | |
| 7145 | + .as_array() | |
| 7146 | + .unwrap() | |
| 7147 | + .iter() | |
| 7148 | + .map(|r| (r["title"].as_str().unwrap(), r["grade"].as_str().unwrap())) | |
| 7149 | + .collect(); | |
| 7150 | + assert!(grades.contains(&("Quiz A", "FF"))); | |
| 7151 | + assert!(grades.contains(&("Quiz B", "AA"))); | |
| 7152 | + assert_eq!(res.body["overall_grade"], "AA"); | |
| 7153 | +} | |
| 7154 | + | |
| 7155 | +#[tokio::test] | |
| 7156 | +async fn settings_accept_admin_edits_trim_entries_and_noop_on_empty_patch() { | |
| 7157 | + let (app, db) = app_and_db().await; | |
| 7158 | + let admin = login_as(&app, &db, "adm.admin", "admin").await; | |
| 7159 | + | |
| 7160 | + // An empty PATCH is a valid no-op: current policy back, nothing changed. | |
| 7161 | + let res = send(&app, "PATCH", "/settings", Some(&admin), Some(json!({}))).await; | |
| 7162 | + assert_eq!(res.status, StatusCode::OK); | |
| 7163 | + assert_eq!(res.body["exam_kinds"].as_array().unwrap().len(), 6); | |
| 7164 | + | |
| 7165 | + // Admin clears the manager bar (hierarchy, not equality), and entries | |
| 7166 | + // arrive trimmed on the wire. | |
| 7167 | + let res = send( | |
| 7168 | + &app, | |
| 7169 | + "PATCH", | |
| 7170 | + "/settings", | |
| 7171 | + Some(&admin), | |
| 7172 | + Some(json!({ "exam_kinds": [" lab ", "quiz"] })), | |
| 7173 | + ) | |
| 7174 | + .await; | |
| 7175 | + assert_eq!(res.status, StatusCode::OK); | |
| 7176 | + assert_eq!(res.body["exam_kinds"], json!(["lab", "quiz"])); | |
| 7177 | +} |
tests/persistence.rsmodified+80 -0
| @@ -299,3 +299,83 @@ async fn course_marks_survive_reopen() { | ||
| 299 | 299 | assert_eq!(roster.body.as_array().unwrap().len(), 1, "roster intact"); |
| 300 | 300 | } |
| 301 | 301 | } |
| 302 | + | |
| 303 | +/// School policy and terms survive a close + reopen — the settings singleton | |
| 304 | +/// (nested band objects included) and the course→term link both come back, | |
| 305 | +/// and the second boot's idempotent migration doesn't disturb them. | |
| 306 | +#[tokio::test] | |
| 307 | +async fn settings_and_terms_survive_reopen() { | |
| 308 | + let dir = tempfile::tempdir().unwrap(); | |
| 309 | + let cfg = config_at(&dir); | |
| 310 | + | |
| 311 | + let term; | |
| 312 | + { | |
| 313 | + let db = database::init(&cfg).await.expect("open file db"); | |
| 314 | + let app = build_router(state(db.clone())); | |
| 315 | + | |
| 316 | + let creds = json!({ "username": "boss", "password": "secret1" }); | |
| 317 | + send(&app, "POST", "/auth/register", None, Some(creds.clone())).await; | |
| 318 | + set_role(&db, "boss", "manager").await; | |
| 319 | + let cookie = send(&app, "POST", "/auth/login", None, Some(creds)) | |
| 320 | + .await | |
| 321 | + .cookie | |
| 322 | + .unwrap(); | |
| 323 | + | |
| 324 | + let res = send( | |
| 325 | + &app, | |
| 326 | + "PATCH", | |
| 327 | + "/settings", | |
| 328 | + Some(&cookie), | |
| 329 | + Some(json!({ | |
| 330 | + "exam_kinds": ["lab", "quiz"], | |
| 331 | + "grade_bands": [{ "min": 0, "label": "F" }, { "min": 50, "label": "P" }], | |
| 332 | + })), | |
| 333 | + ) | |
| 334 | + .await; | |
| 335 | + assert_eq!(res.status, StatusCode::OK); | |
| 336 | + | |
| 337 | + let res = send( | |
| 338 | + &app, | |
| 339 | + "POST", | |
| 340 | + "/terms", | |
| 341 | + Some(&cookie), | |
| 342 | + Some(json!({ "name": "2026 Fall", "starts_at": 1, "ends_at": 2 })), | |
| 343 | + ) | |
| 344 | + .await; | |
| 345 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 346 | + term = res.body["id"].as_str().unwrap().to_string(); | |
| 347 | + | |
| 348 | + let res = send( | |
| 349 | + &app, | |
| 350 | + "POST", | |
| 351 | + "/courses", | |
| 352 | + Some(&cookie), | |
| 353 | + Some(json!({ "title": "History", "term_id": term })), | |
| 354 | + ) | |
| 355 | + .await; | |
| 356 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 357 | + } | |
| 358 | + | |
| 359 | + // New handle at the same path — a fresh boot, migration re-applied. | |
| 360 | + let db = reopen(&cfg).await; | |
| 361 | + let app = build_router(state(db)); | |
| 362 | + let creds = json!({ "username": "boss", "password": "secret1" }); | |
| 363 | + let cookie = send(&app, "POST", "/auth/login", None, Some(creds)) | |
| 364 | + .await | |
| 365 | + .cookie | |
| 366 | + .unwrap(); | |
| 367 | + | |
| 368 | + let res = send(&app, "GET", "/settings", Some(&cookie), None).await; | |
| 369 | + assert_eq!(res.status, StatusCode::OK); | |
| 370 | + assert_eq!(res.body["exam_kinds"], json!(["lab", "quiz"])); | |
| 371 | + assert_eq!(res.body["grade_bands"][0]["label"], "P"); | |
| 372 | + | |
| 373 | + let res = send(&app, "GET", &format!("/terms/{term}"), Some(&cookie), None).await; | |
| 374 | + assert_eq!(res.status, StatusCode::OK); | |
| 375 | + assert_eq!(res.body["name"], "2026 Fall"); | |
| 376 | + | |
| 377 | + let res = send(&app, "GET", "/courses", Some(&cookie), None).await; | |
| 378 | + let courses = res.body.as_array().unwrap(); | |
| 379 | + assert_eq!(courses.len(), 1); | |
| 380 | + assert_eq!(courses[0]["term"].as_str(), Some(term.as_str())); | |
| 381 | +} |