Integration suite
Browse files at this commitc54c66009c0c7a7017f090532a2be97e77f08b04Parent: 854c5fb- test
- integration suite
2 changed files +1420 -2
- tests/common/mod.rs +38 -0
- tests/integration.rs +1382 -2
tests/common/mod.rsmodified+38 -0
| @@ -304,6 +304,44 @@ pub async fn create_exam_with(app: &Router, cookie: &str, course: &str, body: Va | |||
| 304 | .await | 304 | .await |
| 305 | } | 305 | } |
| 306 | 306 | ||
| 307 | +/// Create a homework inside `course` as `cookie` (asserts 201); returns its | ||
| 308 | +/// id. Tagged with `subject` (required — every homework carries one of its | ||
| 309 | +/// course's subjects) and due at `due_at` (unix-millis; must not lie past the | ||
| 310 | +/// 60s grace). Whole-course audience; use [`create_homework_with`] for an | ||
| 311 | +/// `assigned` subset. | ||
| 312 | +pub async fn create_homework( | ||
| 313 | + app: &Router, | ||
| 314 | + cookie: &str, | ||
| 315 | + course: &str, | ||
| 316 | + subject: &str, | ||
| 317 | + title: &str, | ||
| 318 | + due_at: i64, | ||
| 319 | +) -> String { | ||
| 320 | + let res = send( | ||
| 321 | + app, | ||
| 322 | + "POST", | ||
| 323 | + &format!("/courses/{course}/homework"), | ||
| 324 | + Some(cookie), | ||
| 325 | + Some(json!({ "title": title, "subject_id": subject, "due_at": due_at })), | ||
| 326 | + ) | ||
| 327 | + .await; | ||
| 328 | + assert_eq!(res.status, StatusCode::CREATED, "create homework {title}"); | ||
| 329 | + id_of(&res.body) | ||
| 330 | +} | ||
| 331 | + | ||
| 332 | +/// Create a homework inside `course` from a full JSON body (no assertion) — | ||
| 333 | +/// for exercising the `assigned` subset and the validation rejects. | ||
| 334 | +pub async fn create_homework_with(app: &Router, cookie: &str, course: &str, body: Value) -> Res { | ||
| 335 | + send( | ||
| 336 | + app, | ||
| 337 | + "POST", | ||
| 338 | + &format!("/courses/{course}/homework"), | ||
| 339 | + Some(cookie), | ||
| 340 | + Some(body), | ||
| 341 | + ) | ||
| 342 | + .await | ||
| 343 | +} | ||
| 344 | + | ||
| 307 | /// Create a lesson session inside `course` as `cookie` (asserts 201); returns | 345 | /// Create a lesson session inside `course` as `cookie` (asserts 201); returns |
| 308 | /// its id. The session's teacher defaults to the caller. | 346 | /// its id. The session's teacher defaults to the caller. |
| 309 | pub async fn create_session(app: &Router, cookie: &str, course: &str, starts_at: i64) -> String { | 347 | pub async fn create_session(app: &Router, cookie: &str, course: &str, starts_at: i64) -> String { |
tests/integration.rsmodified+1382 -2
| @@ -6,8 +6,8 @@ mod common; | |||
| 6 | use axum::body::Body; | 6 | use axum::body::Body; |
| 7 | use axum::http::{Request, StatusCode}; | 7 | use axum::http::{Request, StatusCode}; |
| 8 | use common::{ | 8 | use common::{ |
| 9 | - app_and_db, create_course, create_exam, create_exam_with, create_session, create_subject, | 9 | + app_and_db, create_course, create_exam, create_exam_with, create_homework, create_session, |
| 10 | - enroll, id_of, login, login_as, me_id, mem_app, send, set_role, | 10 | + create_subject, enroll, id_of, login, login_as, me_id, mem_app, send, set_role, |
| 11 | }; | 11 | }; |
| 12 | use hezarfen_backend::domain::exam::ExamId; | 12 | use hezarfen_backend::domain::exam::ExamId; |
| 13 | use hezarfen_backend::domain::exam_attempt::ExamAttempt; | 13 | use hezarfen_backend::domain::exam_attempt::ExamAttempt; |
| @@ -12895,3 +12895,1383 @@ async fn pool_photo_reads_require_student_role() { | |||
| 12895 | "parents can't read a solution photo" | 12895 | "parents can't read a solution photo" |
| 12896 | ); | 12896 | ); |
| 12897 | } | 12897 | } |
| 12898 | + | ||
| 12899 | +// --- homework ------------------------------------------------------------- | ||
| 12900 | + | ||
| 12901 | +/// A teacher-run course with a subject and two enrolled students — the stage | ||
| 12902 | +/// most homework tests play on. | ||
| 12903 | +struct HwWorld { | ||
| 12904 | + teacher: String, | ||
| 12905 | + ali: String, | ||
| 12906 | + ali_id: String, | ||
| 12907 | + veli: String, | ||
| 12908 | + veli_id: String, | ||
| 12909 | + course: String, | ||
| 12910 | + subject: String, | ||
| 12911 | +} | ||
| 12912 | + | ||
| 12913 | +async fn hw_world(app: &axum::Router, db: &hezarfen_backend::database::Database) -> HwWorld { | ||
| 12914 | + let teacher = login_as(app, db, "teacher", "teacher").await; | ||
| 12915 | + let ali = login(app, "ali").await; | ||
| 12916 | + let ali_id = me_id(app, &ali).await; | ||
| 12917 | + let veli = login(app, "veli").await; | ||
| 12918 | + let veli_id = me_id(app, &veli).await; | ||
| 12919 | + let course = create_course(app, &teacher, "math").await; | ||
| 12920 | + let subject = create_subject(app, &teacher, &course, "algebra").await; | ||
| 12921 | + enroll(app, &teacher, &course, &ali_id).await; | ||
| 12922 | + enroll(app, &teacher, &course, &veli_id).await; | ||
| 12923 | + HwWorld { | ||
| 12924 | + teacher, | ||
| 12925 | + ali, | ||
| 12926 | + ali_id, | ||
| 12927 | + veli, | ||
| 12928 | + veli_id, | ||
| 12929 | + course, | ||
| 12930 | + subject, | ||
| 12931 | + } | ||
| 12932 | +} | ||
| 12933 | + | ||
| 12934 | +/// POST the caller's submission (optional text) to `hw` — no assertion. | ||
| 12935 | +async fn submit_hw(app: &axum::Router, cookie: &str, hw: &str, text: Option<&str>) -> common::Res { | ||
| 12936 | + let body = match text { | ||
| 12937 | + Some(text) => json!({ "text": text }), | ||
| 12938 | + None => json!({}), | ||
| 12939 | + }; | ||
| 12940 | + send( | ||
| 12941 | + app, | ||
| 12942 | + "POST", | ||
| 12943 | + &format!("/homework/{hw}/submission"), | ||
| 12944 | + Some(cookie), | ||
| 12945 | + Some(body), | ||
| 12946 | + ) | ||
| 12947 | + .await | ||
| 12948 | +} | ||
| 12949 | + | ||
| 12950 | +/// Upload `bytes` as a file onto the caller's submission to `hw` — no | ||
| 12951 | +/// assertion. Parses the JSON body like `send`. | ||
| 12952 | +async fn upload_hw_file( | ||
| 12953 | + app: &axum::Router, | ||
| 12954 | + cookie: &str, | ||
| 12955 | + hw: &str, | ||
| 12956 | + filename: &str, | ||
| 12957 | + content_type: &str, | ||
| 12958 | + bytes: &[u8], | ||
| 12959 | +) -> common::Res { | ||
| 12960 | + let (status, _, body) = common::send_raw( | ||
| 12961 | + app, | ||
| 12962 | + "POST", | ||
| 12963 | + &format!("/homework/{hw}/submission/files"), | ||
| 12964 | + Some(cookie), | ||
| 12965 | + Some("multipart/form-data; boundary=hezarfen-test-boundary"), | ||
| 12966 | + common::multipart_file(filename, content_type, bytes), | ||
| 12967 | + ) | ||
| 12968 | + .await; | ||
| 12969 | + let body = if body.is_empty() { | ||
| 12970 | + serde_json::Value::Null | ||
| 12971 | + } else { | ||
| 12972 | + serde_json::from_slice(&body).unwrap_or(serde_json::Value::Null) | ||
| 12973 | + }; | ||
| 12974 | + common::Res { | ||
| 12975 | + status, | ||
| 12976 | + body, | ||
| 12977 | + cookie: None, | ||
| 12978 | + } | ||
| 12979 | +} | ||
| 12980 | + | ||
| 12981 | +/// POST a grade for `user` onto `hw` — no assertion. | ||
| 12982 | +async fn grade_hw( | ||
| 12983 | + app: &axum::Router, | ||
| 12984 | + cookie: &str, | ||
| 12985 | + hw: &str, | ||
| 12986 | + user: &str, | ||
| 12987 | + status: &str, | ||
| 12988 | + mark: Option<i64>, | ||
| 12989 | +) -> common::Res { | ||
| 12990 | + let mut body = json!({ "user": user, "status": status }); | ||
| 12991 | + if let Some(mark) = mark { | ||
| 12992 | + body["mark"] = json!(mark); | ||
| 12993 | + } | ||
| 12994 | + send( | ||
| 12995 | + app, | ||
| 12996 | + "POST", | ||
| 12997 | + &format!("/homework/{hw}/results"), | ||
| 12998 | + Some(cookie), | ||
| 12999 | + Some(body), | ||
| 13000 | + ) | ||
| 13001 | + .await | ||
| 13002 | +} | ||
| 13003 | + | ||
| 13004 | +/// Every stored homework-file blob name, straight from the table. | ||
| 13005 | +async fn homework_blob_keys(db: &hezarfen_backend::database::Database) -> Vec<String> { | ||
| 13006 | + let mut result = db | ||
| 13007 | + .query("SELECT VALUE file FROM homework_file") | ||
| 13008 | + .await | ||
| 13009 | + .unwrap() | ||
| 13010 | + .check() | ||
| 13011 | + .unwrap(); | ||
| 13012 | + result.take::<Vec<String>>(0).unwrap() | ||
| 13013 | +} | ||
| 13014 | + | ||
| 13015 | +/// Row count of `table`, straight from the database — a cascade must really | ||
| 13016 | +/// delete, not merely hide behind the role checks. | ||
| 13017 | +async fn hw_row_count(db: &hezarfen_backend::database::Database, table: &str) -> usize { | ||
| 13018 | + let mut result = db | ||
| 13019 | + .query(format!("SELECT VALUE id FROM {table}")) | ||
| 13020 | + .await | ||
| 13021 | + .unwrap() | ||
| 13022 | + .check() | ||
| 13023 | + .unwrap(); | ||
| 13024 | + result | ||
| 13025 | + .take::<Vec<surrealdb::types::RecordId>>(0) | ||
| 13026 | + .unwrap() | ||
| 13027 | + .len() | ||
| 13028 | +} | ||
| 13029 | + | ||
| 13030 | +/// Creation validates the due date (60s grace), the subject's course, the | ||
| 13031 | +/// subset's enrollment, and the subset cap — and stays teacher-only. | ||
| 13032 | +#[tokio::test] | ||
| 13033 | +async fn homework_create_validates_due_subject_and_subset() { | ||
| 13034 | + let (app, db) = app_and_db().await; | ||
| 13035 | + let w = hw_world(&app, &db).await; | ||
| 13036 | + let now = Timestamp::now().as_millis(); | ||
| 13037 | + | ||
| 13038 | + // due_at beyond the 60s grace lies in the past — rejected. | ||
| 13039 | + let res = common::create_homework_with( | ||
| 13040 | + &app, | ||
| 13041 | + &w.teacher, | ||
| 13042 | + &w.course, | ||
| 13043 | + json!({ "title": "old", "subject_id": w.subject, "due_at": now - 120_000 }), | ||
| 13044 | + ) | ||
| 13045 | + .await; | ||
| 13046 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | ||
| 13047 | + | ||
| 13048 | + // The subject must belong to this very course. | ||
| 13049 | + let other_course = create_course(&app, &w.teacher, "physics").await; | ||
| 13050 | + let foreign = create_subject(&app, &w.teacher, &other_course, "optics").await; | ||
| 13051 | + let res = common::create_homework_with( | ||
| 13052 | + &app, | ||
| 13053 | + &w.teacher, | ||
| 13054 | + &w.course, | ||
| 13055 | + json!({ "title": "x", "subject_id": foreign, "due_at": now + 3_600_000 }), | ||
| 13056 | + ) | ||
| 13057 | + .await; | ||
| 13058 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | ||
| 13059 | + | ||
| 13060 | + // Every assigned student must be enrolled… | ||
| 13061 | + let mehmet = login(&app, "mehmet").await; | ||
| 13062 | + let mehmet_id = me_id(&app, &mehmet).await; | ||
| 13063 | + let res = common::create_homework_with( | ||
| 13064 | + &app, | ||
| 13065 | + &w.teacher, | ||
| 13066 | + &w.course, | ||
| 13067 | + json!({ "title": "x", "subject_id": w.subject, "due_at": now + 3_600_000, | ||
| 13068 | + "assigned": [mehmet_id] }), | ||
| 13069 | + ) | ||
| 13070 | + .await; | ||
| 13071 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | ||
| 13072 | + | ||
| 13073 | + // …and the subset caps at 200 names (checked before enrollment, so fakes | ||
| 13074 | + // trip it without 201 signups). | ||
| 13075 | + let fakes: Vec<String> = (0..201).map(|i| format!("01FAKE{i:020}")).collect(); | ||
| 13076 | + let res = common::create_homework_with( | ||
| 13077 | + &app, | ||
| 13078 | + &w.teacher, | ||
| 13079 | + &w.course, | ||
| 13080 | + json!({ "title": "x", "subject_id": w.subject, "due_at": now + 3_600_000, | ||
| 13081 | + "assigned": fakes }), | ||
| 13082 | + ) | ||
| 13083 | + .await; | ||
| 13084 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | ||
| 13085 | + | ||
| 13086 | + // Students don't assign homework. | ||
| 13087 | + let res = common::create_homework_with( | ||
| 13088 | + &app, | ||
| 13089 | + &w.ali, | ||
| 13090 | + &w.course, | ||
| 13091 | + json!({ "title": "x", "subject_id": w.subject, "due_at": now + 3_600_000 }), | ||
| 13092 | + ) | ||
| 13093 | + .await; | ||
| 13094 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 13095 | + | ||
| 13096 | + // Within the grace counts as "due now" — accepted, like a plain future one. | ||
| 13097 | + let res = common::create_homework_with( | ||
| 13098 | + &app, | ||
| 13099 | + &w.teacher, | ||
| 13100 | + &w.course, | ||
| 13101 | + json!({ "title": "just now", "subject_id": w.subject, "due_at": now - 30_000 }), | ||
| 13102 | + ) | ||
| 13103 | + .await; | ||
| 13104 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 13105 | + let hw = create_homework( | ||
| 13106 | + &app, | ||
| 13107 | + &w.teacher, | ||
| 13108 | + &w.course, | ||
| 13109 | + &w.subject, | ||
| 13110 | + "ch. 3", | ||
| 13111 | + now + 3_600_000, | ||
| 13112 | + ) | ||
| 13113 | + .await; | ||
| 13114 | + let res = send(&app, "GET", &format!("/homework/{hw}"), Some(&w.ali), None).await; | ||
| 13115 | + assert_eq!(res.status, StatusCode::OK); | ||
| 13116 | + assert_eq!(res.body["title"], "ch. 3"); | ||
| 13117 | + assert_eq!(res.body["subject"], w.subject.as_str()); | ||
| 13118 | + assert!( | ||
| 13119 | + res.body["assigned"].is_null(), | ||
| 13120 | + "whole-course stores no subset" | ||
| 13121 | + ); | ||
| 13122 | +} | ||
| 13123 | + | ||
| 13124 | +/// A subset assignment must never leak to the students it leaves out (404s, | ||
| 13125 | +/// list omissions), off-course teachers are walled off, the cross-course list | ||
| 13126 | +/// narrows per caller, and staff never submit. | ||
| 13127 | +#[tokio::test] | ||
| 13128 | +async fn homework_subset_hides_from_outsiders() { | ||
| 13129 | + let (app, db) = app_and_db().await; | ||
| 13130 | + let w = hw_world(&app, &db).await; | ||
| 13131 | + let now = Timestamp::now().as_millis(); | ||
| 13132 | + | ||
| 13133 | + let res = common::create_homework_with( | ||
| 13134 | + &app, | ||
| 13135 | + &w.teacher, | ||
| 13136 | + &w.course, | ||
| 13137 | + json!({ "title": "secret drill", "subject_id": w.subject, "due_at": now + 3_600_000, | ||
| 13138 | + "assigned": [w.ali_id] }), | ||
| 13139 | + ) | ||
| 13140 | + .await; | ||
| 13141 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 13142 | + let hw = id_of(&res.body); | ||
| 13143 | + | ||
| 13144 | + // ali is named: both lists carry it and the fetch works. | ||
| 13145 | + let uri = format!("/courses/{}/homework", w.course); | ||
| 13146 | + let res = send(&app, "GET", &uri, Some(&w.ali), None).await; | ||
| 13147 | + assert_eq!(common::total(&res.body), 1); | ||
| 13148 | + let res = send(&app, "GET", "/homework", Some(&w.ali), None).await; | ||
| 13149 | + assert_eq!(common::total(&res.body), 1); | ||
| 13150 | + // veli is enrolled but unnamed: the homework must not even seem to exist. | ||
| 13151 | + let res = send(&app, "GET", &uri, Some(&w.veli), None).await; | ||
| 13152 | + assert_eq!( | ||
| 13153 | + common::total(&res.body), | ||
| 13154 | + 0, | ||
| 13155 | + "unnamed student sees no subset" | ||
| 13156 | + ); | ||
| 13157 | + let res = send(&app, "GET", "/homework", Some(&w.veli), None).await; | ||
| 13158 | + assert_eq!(common::total(&res.body), 0); | ||
| 13159 | + let res = send(&app, "GET", &format!("/homework/{hw}"), Some(&w.veli), None).await; | ||
| 13160 | + assert_eq!(res.status, StatusCode::NOT_FOUND, "404, never a 403 leak"); | ||
| 13161 | + let res = submit_hw(&app, &w.veli, &hw, Some("hi")).await; | ||
| 13162 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 13163 | + | ||
| 13164 | + // A teacher without management rights over the course is walled off. | ||
| 13165 | + let rival = login_as(&app, &db, "rival", "teacher").await; | ||
| 13166 | + let res = send(&app, "GET", &format!("/homework/{hw}"), Some(&rival), None).await; | ||
| 13167 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 13168 | + let res = send( | ||
| 13169 | + &app, | ||
| 13170 | + "PATCH", | ||
| 13171 | + &format!("/homework/{hw}"), | ||
| 13172 | + Some(&rival), | ||
| 13173 | + Some(json!({ "title": "hijack" })), | ||
| 13174 | + ) | ||
| 13175 | + .await; | ||
| 13176 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 13177 | + let res = send( | ||
| 13178 | + &app, | ||
| 13179 | + "GET", | ||
| 13180 | + &format!("/homework/{hw}/submissions"), | ||
| 13181 | + Some(&rival), | ||
| 13182 | + None, | ||
| 13183 | + ) | ||
| 13184 | + .await; | ||
| 13185 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 13186 | + let res = grade_hw(&app, &rival, &hw, &w.ali_id, "done", None).await; | ||
| 13187 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 13188 | + let res = send( | ||
| 13189 | + &app, | ||
| 13190 | + "DELETE", | ||
| 13191 | + &format!("/homework/{hw}"), | ||
| 13192 | + Some(&rival), | ||
| 13193 | + None, | ||
| 13194 | + ) | ||
| 13195 | + .await; | ||
| 13196 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 13197 | + | ||
| 13198 | + // The cross-course list narrows per caller: manager+ everything, an | ||
| 13199 | + // off-course teacher nothing, the managing teacher their course's rows. | ||
| 13200 | + let boss = login_as(&app, &db, "boss", "manager").await; | ||
| 13201 | + let res = send(&app, "GET", "/homework", Some(&boss), None).await; | ||
| 13202 | + assert_eq!(common::total(&res.body), 1); | ||
| 13203 | + let res = send(&app, "GET", "/homework", Some(&rival), None).await; | ||
| 13204 | + assert_eq!(common::total(&res.body), 0); | ||
| 13205 | + let res = send(&app, "GET", "/homework", Some(&w.teacher), None).await; | ||
| 13206 | + assert_eq!(common::total(&res.body), 1); | ||
| 13207 | + | ||
| 13208 | + // Staff never submit — a 403 on the live role, whatever they can see. | ||
| 13209 | + let res = submit_hw(&app, &boss, &hw, Some("i am staff")).await; | ||
| 13210 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); | ||
| 13211 | + let res = submit_hw(&app, &w.teacher, &hw, Some("me neither")).await; | ||
| 13212 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 13213 | +} | ||
| 13214 | + | ||
| 13215 | +/// PATCH re-checks a newly set due date and subject, refuses a narrowing that | ||
| 13216 | +/// would orphan existing work (naming the blockers), and leaves stored past | ||
| 13217 | +/// due dates alone on unrelated edits. | ||
| 13218 | +#[tokio::test] | ||
| 13219 | +async fn homework_patch_rechecks_and_blocks_orphaning() { | ||
| 13220 | + let (app, db) = app_and_db().await; | ||
| 13221 | + let w = hw_world(&app, &db).await; | ||
| 13222 | + let now = Timestamp::now().as_millis(); | ||
| 13223 | + let res = common::create_homework_with( | ||
| 13224 | + &app, | ||
| 13225 | + &w.teacher, | ||
| 13226 | + &w.course, | ||
| 13227 | + json!({ "title": "drill", "subject_id": w.subject, "due_at": now + 3_600_000, | ||
| 13228 | + "assigned": [w.ali_id, w.veli_id] }), | ||
| 13229 | + ) | ||
| 13230 | + .await; | ||
| 13231 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 13232 | + let hw = id_of(&res.body); | ||
| 13233 | + | ||
| 13234 | + // A newly set due date must not be past… | ||
| 13235 | + let res = send( | ||
| 13236 | + &app, | ||
| 13237 | + "PATCH", | ||
| 13238 | + &format!("/homework/{hw}"), | ||
| 13239 | + Some(&w.teacher), | ||
| 13240 | + Some(json!({ "due_at": now - 120_000 })), | ||
| 13241 | + ) | ||
| 13242 | + .await; | ||
| 13243 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | ||
| 13244 | + | ||
| 13245 | + // …and a new subject must be the course's own. | ||
| 13246 | + let other_course = create_course(&app, &w.teacher, "physics").await; | ||
| 13247 | + let foreign = create_subject(&app, &w.teacher, &other_course, "optics").await; | ||
| 13248 | + let res = send( | ||
| 13249 | + &app, | ||
| 13250 | + "PATCH", | ||
| 13251 | + &format!("/homework/{hw}"), | ||
| 13252 | + Some(&w.teacher), | ||
| 13253 | + Some(json!({ "subject_id": foreign })), | ||
| 13254 | + ) | ||
| 13255 | + .await; | ||
| 13256 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | ||
| 13257 | + let algebra2 = create_subject(&app, &w.teacher, &w.course, "algebra II").await; | ||
| 13258 | + let res = send( | ||
| 13259 | + &app, | ||
| 13260 | + "PATCH", | ||
| 13261 | + &format!("/homework/{hw}"), | ||
| 13262 | + Some(&w.teacher), | ||
| 13263 | + Some(json!({ "subject_id": algebra2 })), | ||
| 13264 | + ) | ||
| 13265 | + .await; | ||
| 13266 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13267 | + assert_eq!(res.body["subject"], algebra2.as_str()); | ||
| 13268 | + | ||
| 13269 | + // ali submits; narrowing to veli alone would strand that work — refused, | ||
| 13270 | + // naming the blocker. Narrowing to exactly ali passes. | ||
| 13271 | + let res = submit_hw(&app, &w.ali, &hw, Some("done")).await; | ||
| 13272 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 13273 | + let res = send( | ||
| 13274 | + &app, | ||
| 13275 | + "PATCH", | ||
| 13276 | + &format!("/homework/{hw}"), | ||
| 13277 | + Some(&w.teacher), | ||
| 13278 | + Some(json!({ "assigned": [w.veli_id] })), | ||
| 13279 | + ) | ||
| 13280 | + .await; | ||
| 13281 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | ||
| 13282 | + assert!( | ||
| 13283 | + res.body["error"].as_str().unwrap().contains(&w.ali_id), | ||
| 13284 | + "the 409 names whose work blocks: {}", | ||
| 13285 | + res.body | ||
| 13286 | + ); | ||
| 13287 | + let res = send( | ||
| 13288 | + &app, | ||
| 13289 | + "PATCH", | ||
| 13290 | + &format!("/homework/{hw}"), | ||
| 13291 | + Some(&w.teacher), | ||
| 13292 | + Some(json!({ "assigned": [w.ali_id] })), | ||
| 13293 | + ) | ||
| 13294 | + .await; | ||
| 13295 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13296 | + | ||
| 13297 | + // Widening back to the whole course always passes. | ||
| 13298 | + let res = send( | ||
| 13299 | + &app, | ||
| 13300 | + "PATCH", | ||
| 13301 | + &format!("/homework/{hw}"), | ||
| 13302 | + Some(&w.teacher), | ||
| 13303 | + Some(json!({ "assigned": [] })), | ||
| 13304 | + ) | ||
| 13305 | + .await; | ||
| 13306 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13307 | + assert!(res.body["assigned"].is_null()); | ||
| 13308 | + | ||
| 13309 | + // A title-only edit on a graced, already-due homework keeps its stored | ||
| 13310 | + // due date without tripping the not-past check. | ||
| 13311 | + let old = create_homework(&app, &w.teacher, &w.course, &w.subject, "old", now - 30_000).await; | ||
| 13312 | + let res = send( | ||
| 13313 | + &app, | ||
| 13314 | + "PATCH", | ||
| 13315 | + &format!("/homework/{old}"), | ||
| 13316 | + Some(&w.teacher), | ||
| 13317 | + Some(json!({ "title": "renamed" })), | ||
| 13318 | + ) | ||
| 13319 | + .await; | ||
| 13320 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13321 | +} | ||
| 13322 | + | ||
| 13323 | +/// Submitting creates once (201) and edits in place after (200): the | ||
| 13324 | +/// first-hand-in stamp is pinned, text is replaced whole (omitting clears), | ||
| 13325 | +/// and withdrawal removes it all. | ||
| 13326 | +#[tokio::test] | ||
| 13327 | +async fn homework_submission_lifecycle_and_stamps() { | ||
| 13328 | + let (app, db) = app_and_db().await; | ||
| 13329 | + let w = hw_world(&app, &db).await; | ||
| 13330 | + let now = Timestamp::now().as_millis(); | ||
| 13331 | + let hw = create_homework( | ||
| 13332 | + &app, | ||
| 13333 | + &w.teacher, | ||
| 13334 | + &w.course, | ||
| 13335 | + &w.subject, | ||
| 13336 | + "essay", | ||
| 13337 | + now + 3_600_000, | ||
| 13338 | + ) | ||
| 13339 | + .await; | ||
| 13340 | + | ||
| 13341 | + let uri = format!("/homework/{hw}/submission"); | ||
| 13342 | + let res = send(&app, "GET", &uri, Some(&w.ali), None).await; | ||
| 13343 | + assert_eq!(res.status, StatusCode::NOT_FOUND, "nothing submitted yet"); | ||
| 13344 | + | ||
| 13345 | + let res = submit_hw(&app, &w.ali, &hw, Some("draft one")).await; | ||
| 13346 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 13347 | + assert_eq!(res.body["text"], "draft one"); | ||
| 13348 | + assert_eq!(res.body["late"], false); | ||
| 13349 | + let submitted_at = res.body["submitted_at"].as_i64().unwrap(); | ||
| 13350 | + | ||
| 13351 | + let res = submit_hw(&app, &w.ali, &hw, Some("draft two")).await; | ||
| 13352 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13353 | + assert_eq!(res.body["text"], "draft two"); | ||
| 13354 | + assert_eq!( | ||
| 13355 | + res.body["submitted_at"].as_i64().unwrap(), | ||
| 13356 | + submitted_at, | ||
| 13357 | + "the first-submit stamp never moves" | ||
| 13358 | + ); | ||
| 13359 | + assert!(res.body["updated_at"].as_i64().unwrap() >= submitted_at); | ||
| 13360 | + | ||
| 13361 | + // The text rides whole in each submit: omitting it clears. | ||
| 13362 | + let res = submit_hw(&app, &w.ali, &hw, None).await; | ||
| 13363 | + assert_eq!(res.status, StatusCode::OK); | ||
| 13364 | + assert!(res.body["text"].is_null()); | ||
| 13365 | + | ||
| 13366 | + // Reads are strictly own: veli asking gets veli's (absent) submission. | ||
| 13367 | + let res = send(&app, "GET", &uri, Some(&w.ali), None).await; | ||
| 13368 | + assert_eq!(res.status, StatusCode::OK); | ||
| 13369 | + assert!(res.body["result"].is_null()); | ||
| 13370 | + let res = send(&app, "GET", &uri, Some(&w.veli), None).await; | ||
| 13371 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 13372 | + | ||
| 13373 | + // Withdrawal, twice: gone, then nothing left to withdraw. | ||
| 13374 | + let res = send(&app, "DELETE", &uri, Some(&w.ali), None).await; | ||
| 13375 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 13376 | + let res = send(&app, "GET", &uri, Some(&w.ali), None).await; | ||
| 13377 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 13378 | + let res = send(&app, "DELETE", &uri, Some(&w.ali), None).await; | ||
| 13379 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 13380 | +} | ||
| 13381 | + | ||
| 13382 | +/// The late flag is computed against `due_at` on every read, and the roster | ||
| 13383 | +/// computes `missing` for unsubmitted-past-due rows — no sleeping: a graced | ||
| 13384 | +/// creation plants the due date ~30s in the past. | ||
| 13385 | +#[tokio::test] | ||
| 13386 | +async fn homework_lateness_is_computed_across_due() { | ||
| 13387 | + let (app, db) = app_and_db().await; | ||
| 13388 | + let w = hw_world(&app, &db).await; | ||
| 13389 | + let now = Timestamp::now().as_millis(); | ||
| 13390 | + | ||
| 13391 | + let overdue = create_homework( | ||
| 13392 | + &app, | ||
| 13393 | + &w.teacher, | ||
| 13394 | + &w.course, | ||
| 13395 | + &w.subject, | ||
| 13396 | + "late one", | ||
| 13397 | + now - 30_000, | ||
| 13398 | + ) | ||
| 13399 | + .await; | ||
| 13400 | + let res = submit_hw(&app, &w.ali, &overdue, Some("sorry")).await; | ||
| 13401 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 13402 | + assert_eq!(res.body["late"], true, "touched after due_at"); | ||
| 13403 | + | ||
| 13404 | + let open = create_homework( | ||
| 13405 | + &app, | ||
| 13406 | + &w.teacher, | ||
| 13407 | + &w.course, | ||
| 13408 | + &w.subject, | ||
| 13409 | + "open one", | ||
| 13410 | + now + 3_600_000, | ||
| 13411 | + ) | ||
| 13412 | + .await; | ||
| 13413 | + let res = submit_hw(&app, &w.ali, &open, Some("early")).await; | ||
| 13414 | + assert_eq!(res.body["late"], false); | ||
| 13415 | + | ||
| 13416 | + // The roster mirrors the flag and derives `missing` for veli, who never | ||
| 13417 | + // submitted — a computation, not a teacher verdict. | ||
| 13418 | + let res = send( | ||
| 13419 | + &app, | ||
| 13420 | + "GET", | ||
| 13421 | + &format!("/homework/{overdue}/submissions"), | ||
| 13422 | + Some(&w.teacher), | ||
| 13423 | + None, | ||
| 13424 | + ) | ||
| 13425 | + .await; | ||
| 13426 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13427 | + assert_eq!(common::total(&res.body), 2); | ||
| 13428 | + let rows = common::items(&res.body); | ||
| 13429 | + let ali_row = rows | ||
| 13430 | + .iter() | ||
| 13431 | + .find(|row| row["user"] == w.ali_id.as_str()) | ||
| 13432 | + .unwrap(); | ||
| 13433 | + assert_eq!(ali_row["submission"]["late"], true); | ||
| 13434 | + assert_eq!(ali_row["missing"], false); | ||
| 13435 | + assert_eq!(ali_row["unenrolled"], false); | ||
| 13436 | + let veli_row = rows | ||
| 13437 | + .iter() | ||
| 13438 | + .find(|row| row["user"] == w.veli_id.as_str()) | ||
| 13439 | + .unwrap(); | ||
| 13440 | + assert!(veli_row["submission"].is_null()); | ||
| 13441 | + assert_eq!(veli_row["missing"], true, "unsubmitted past due"); | ||
| 13442 | +} | ||
| 13443 | + | ||
| 13444 | +/// Grading walls: students never grade, nobody grades themselves, and the | ||
| 13445 | +/// target must exist, hold the student role, be enrolled, and be in the | ||
| 13446 | +/// audience; the verdict set is closed and the mark bounded. A regrade | ||
| 13447 | +/// overwrites its one row, and the student reads the verdict back. | ||
| 13448 | +#[tokio::test] | ||
| 13449 | +async fn homework_grading_gates_and_bounds() { | ||
| 13450 | + let (app, db) = app_and_db().await; | ||
| 13451 | + let w = hw_world(&app, &db).await; | ||
| 13452 | + let now = Timestamp::now().as_millis(); | ||
| 13453 | + let res = common::create_homework_with( | ||
| 13454 | + &app, | ||
| 13455 | + &w.teacher, | ||
| 13456 | + &w.course, | ||
| 13457 | + json!({ "title": "drill", "subject_id": w.subject, "due_at": now + 3_600_000, | ||
| 13458 | + "assigned": [w.ali_id] }), | ||
| 13459 | + ) | ||
| 13460 | + .await; | ||
| 13461 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 13462 | + let hw = id_of(&res.body); | ||
| 13463 | + let teacher_id = me_id(&app, &w.teacher).await; | ||
| 13464 | + | ||
| 13465 | + let res = grade_hw(&app, &w.ali, &hw, &w.ali_id, "done", None).await; | ||
| 13466 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "students never grade"); | ||
| 13467 | + let res = grade_hw(&app, &w.teacher, &hw, &teacher_id, "done", None).await; | ||
| 13468 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "no self-grading"); | ||
| 13469 | + let res = grade_hw( | ||
| 13470 | + &app, | ||
| 13471 | + &w.teacher, | ||
| 13472 | + &hw, | ||
| 13473 | + "01ZZZZZZZZZZZZZZZZZZZZZZZZ", | ||
| 13474 | + "done", | ||
| 13475 | + None, | ||
| 13476 | + ) | ||
| 13477 | + .await; | ||
| 13478 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "target must exist"); | ||
| 13479 | + let rival = login_as(&app, &db, "rival", "teacher").await; | ||
| 13480 | + let rival_id = me_id(&app, &rival).await; | ||
| 13481 | + let res = grade_hw(&app, &w.teacher, &hw, &rival_id, "done", None).await; | ||
| 13482 | + assert_eq!( | ||
| 13483 | + res.status, | ||
| 13484 | + StatusCode::BAD_REQUEST, | ||
| 13485 | + "only students carry grades" | ||
| 13486 | + ); | ||
| 13487 | + let mehmet = login(&app, "mehmet").await; | ||
| 13488 | + let mehmet_id = me_id(&app, &mehmet).await; | ||
| 13489 | + let res = grade_hw(&app, &w.teacher, &hw, &mehmet_id, "done", None).await; | ||
| 13490 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "must be enrolled"); | ||
| 13491 | + let res = grade_hw(&app, &w.teacher, &hw, &w.veli_id, "done", None).await; | ||
| 13492 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "outside the audience"); | ||
| 13493 | + let res = grade_hw(&app, &w.teacher, &hw, &w.ali_id, "late", None).await; | ||
| 13494 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "junk status"); | ||
| 13495 | + let res = grade_hw(&app, &w.teacher, &hw, &w.ali_id, "done", Some(101)).await; | ||
| 13496 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "mark over 100"); | ||
| 13497 | + let res = grade_hw(&app, &w.teacher, &hw, &w.ali_id, "done", Some(-1)).await; | ||
| 13498 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "negative mark"); | ||
| 13499 | + | ||
| 13500 | + // 200 upsert semantics: the regrade overwrites, never stacks. | ||
| 13501 | + let res = grade_hw(&app, &w.teacher, &hw, &w.ali_id, "incomplete", None).await; | ||
| 13502 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13503 | + assert_eq!(res.body["status"], "incomplete"); | ||
| 13504 | + assert!(res.body["mark"].is_null()); | ||
| 13505 | + let res = grade_hw(&app, &w.teacher, &hw, &w.ali_id, "done", Some(85)).await; | ||
| 13506 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13507 | + assert_eq!(res.body["mark"], 85); | ||
| 13508 | + assert_eq!(hw_row_count(&db, "homework_result").await, 1); | ||
| 13509 | + | ||
| 13510 | + // The student reads their own verdict; ungraded means 404, for anyone. | ||
| 13511 | + let uri = format!("/homework/{hw}/result"); | ||
| 13512 | + let res = send(&app, "GET", &uri, Some(&w.ali), None).await; | ||
| 13513 | + assert_eq!(res.status, StatusCode::OK); | ||
| 13514 | + assert_eq!(res.body["status"], "done"); | ||
| 13515 | + assert_eq!(res.body["mark"], 85); | ||
| 13516 | + let res = send(&app, "GET", &uri, Some(&w.veli), None).await; | ||
| 13517 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 13518 | +} | ||
| 13519 | + | ||
| 13520 | +/// A stored grade freezes the submission — text, files, withdrawal — until | ||
| 13521 | +/// the teacher removes it; and work never handed in can be graded `missing`, | ||
| 13522 | +/// a verdict the student reads from `/result` despite having no submission. | ||
| 13523 | +#[tokio::test] | ||
| 13524 | +async fn homework_grade_freezes_until_removed() { | ||
| 13525 | + let (app, db) = app_and_db().await; | ||
| 13526 | + let w = hw_world(&app, &db).await; | ||
| 13527 | + let now = Timestamp::now().as_millis(); | ||
| 13528 | + let hw = create_homework( | ||
| 13529 | + &app, | ||
| 13530 | + &w.teacher, | ||
| 13531 | + &w.course, | ||
| 13532 | + &w.subject, | ||
| 13533 | + "essay", | ||
| 13534 | + now + 3_600_000, | ||
| 13535 | + ) | ||
| 13536 | + .await; | ||
| 13537 | + | ||
| 13538 | + let res = submit_hw(&app, &w.ali, &hw, Some("v1")).await; | ||
| 13539 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 13540 | + let up = upload_hw_file(&app, &w.ali, &hw, "draft.txt", "text/plain", b"notes").await; | ||
| 13541 | + assert_eq!(up.status, StatusCode::CREATED, "{}", up.body); | ||
| 13542 | + let fid = id_of(&up.body); | ||
| 13543 | + | ||
| 13544 | + let res = grade_hw(&app, &w.teacher, &hw, &w.ali_id, "incomplete", None).await; | ||
| 13545 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13546 | + let res = submit_hw(&app, &w.ali, &hw, Some("v2")).await; | ||
| 13547 | + assert_eq!(res.status, StatusCode::CONFLICT, "text frozen"); | ||
| 13548 | + let res = upload_hw_file(&app, &w.ali, &hw, "late.txt", "text/plain", b"more").await; | ||
| 13549 | + assert_eq!(res.status, StatusCode::CONFLICT, "file add frozen"); | ||
| 13550 | + let res = send( | ||
| 13551 | + &app, | ||
| 13552 | + "DELETE", | ||
| 13553 | + &format!("/homework/{hw}/submission/files/{fid}"), | ||
| 13554 | + Some(&w.ali), | ||
| 13555 | + None, | ||
| 13556 | + ) | ||
| 13557 | + .await; | ||
| 13558 | + assert_eq!(res.status, StatusCode::CONFLICT, "file delete frozen"); | ||
| 13559 | + let res = send( | ||
| 13560 | + &app, | ||
| 13561 | + "DELETE", | ||
| 13562 | + &format!("/homework/{hw}/submission"), | ||
| 13563 | + Some(&w.ali), | ||
| 13564 | + None, | ||
| 13565 | + ) | ||
| 13566 | + .await; | ||
| 13567 | + assert_eq!(res.status, StatusCode::CONFLICT, "withdrawal frozen"); | ||
| 13568 | + // The grade rides on the student's own submission read. | ||
| 13569 | + let res = send( | ||
| 13570 | + &app, | ||
| 13571 | + "GET", | ||
| 13572 | + &format!("/homework/{hw}/submission"), | ||
| 13573 | + Some(&w.ali), | ||
| 13574 | + None, | ||
| 13575 | + ) | ||
| 13576 | + .await; | ||
| 13577 | + assert_eq!(res.body["result"]["status"], "incomplete"); | ||
| 13578 | + | ||
| 13579 | + // Un-grading reopens the work; a second removal finds nothing. | ||
| 13580 | + let uri = format!("/homework/{hw}/results/{}", w.ali_id); | ||
| 13581 | + let res = send(&app, "DELETE", &uri, Some(&w.teacher), None).await; | ||
| 13582 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 13583 | + let res = submit_hw(&app, &w.ali, &hw, Some("v2")).await; | ||
| 13584 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13585 | + let res = send(&app, "DELETE", &uri, Some(&w.teacher), None).await; | ||
| 13586 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 13587 | + | ||
| 13588 | + // Never-submitted work still takes a `missing` verdict, and `/result` is | ||
| 13589 | + // where its student reads it — the submission endpoints have nothing. | ||
| 13590 | + let res = grade_hw(&app, &w.teacher, &hw, &w.veli_id, "missing", None).await; | ||
| 13591 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13592 | + let res = send( | ||
| 13593 | + &app, | ||
| 13594 | + "GET", | ||
| 13595 | + &format!("/homework/{hw}/submission"), | ||
| 13596 | + Some(&w.veli), | ||
| 13597 | + None, | ||
| 13598 | + ) | ||
| 13599 | + .await; | ||
| 13600 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 13601 | + let res = send( | ||
| 13602 | + &app, | ||
| 13603 | + "GET", | ||
| 13604 | + &format!("/homework/{hw}/result"), | ||
| 13605 | + Some(&w.veli), | ||
| 13606 | + None, | ||
| 13607 | + ) | ||
| 13608 | + .await; | ||
| 13609 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13610 | + assert_eq!(res.body["status"], "missing"); | ||
| 13611 | +} | ||
| 13612 | + | ||
| 13613 | +/// Files round-trip: a photo-first upload auto-creates the submission, the | ||
| 13614 | +/// owner and the managing teacher download the exact bytes as a forced | ||
| 13615 | +/// attachment, file ids are scoped to their homework, other students see | ||
| 13616 | +/// nothing, and a delete unlinks the blob. | ||
| 13617 | +#[tokio::test] | ||
| 13618 | +async fn homework_files_roundtrip_and_scope() { | ||
| 13619 | + let (app, db) = app_and_db().await; | ||
| 13620 | + let w = hw_world(&app, &db).await; | ||
| 13621 | + let now = Timestamp::now().as_millis(); | ||
| 13622 | + let hw = create_homework( | ||
| 13623 | + &app, | ||
| 13624 | + &w.teacher, | ||
| 13625 | + &w.course, | ||
| 13626 | + &w.subject, | ||
| 13627 | + "photo hw", | ||
| 13628 | + now + 3_600_000, | ||
| 13629 | + ) | ||
| 13630 | + .await; | ||
| 13631 | + | ||
| 13632 | + let bytes = b"%PDF-1.4 homework \x00\x01".to_vec(); | ||
| 13633 | + let up = upload_hw_file(&app, &w.ali, &hw, "odev.pdf", "application/pdf", &bytes).await; | ||
| 13634 | + assert_eq!(up.status, StatusCode::CREATED, "{}", up.body); | ||
| 13635 | + assert_eq!(up.body["name"], "odev.pdf"); | ||
| 13636 | + assert_eq!(up.body["content_type"], "application/pdf"); | ||
| 13637 | + assert_eq!(up.body["size"], bytes.len() as i64); | ||
| 13638 | + let fid = id_of(&up.body); | ||
| 13639 | + let res = send( | ||
| 13640 | + &app, | ||
| 13641 | + "GET", | ||
| 13642 | + &format!("/homework/{hw}/submission"), | ||
| 13643 | + Some(&w.ali), | ||
| 13644 | + None, | ||
| 13645 | + ) | ||
| 13646 | + .await; | ||
| 13647 | + assert_eq!(res.status, StatusCode::OK, "photo-first auto-creates"); | ||
| 13648 | + assert!(res.body["text"].is_null()); | ||
| 13649 | + assert_eq!(res.body["files"].as_array().unwrap().len(), 1); | ||
| 13650 | + | ||
| 13651 | + // Owner and managing teacher read the same bytes — always an attachment: | ||
| 13652 | + // homework files take any content type, so inline HTML/SVG would be XSS. | ||
| 13653 | + let file_uri = format!("/homework/{hw}/submission/files/{fid}"); | ||
| 13654 | + for viewer in [&w.ali, &w.teacher] { | ||
| 13655 | + let (status, headers, body) = | ||
| 13656 | + common::send_raw(&app, "GET", &file_uri, Some(viewer), None, Vec::new()).await; | ||
| 13657 | + assert_eq!(status, StatusCode::OK); | ||
| 13658 | + assert_eq!(body, bytes); | ||
| 13659 | + assert_eq!(headers["content-type"], "application/pdf"); | ||
| 13660 | + assert_eq!( | ||
| 13661 | + headers["content-disposition"], | ||
| 13662 | + "attachment; filename=\"odev.pdf\"; filename*=UTF-8''odev.pdf" | ||
| 13663 | + ); | ||
| 13664 | + } | ||
| 13665 | + // The id only resolves under its own homework, and never for a classmate. | ||
| 13666 | + let other = create_homework( | ||
| 13667 | + &app, | ||
| 13668 | + &w.teacher, | ||
| 13669 | + &w.course, | ||
| 13670 | + &w.subject, | ||
| 13671 | + "other", | ||
| 13672 | + now + 3_600_000, | ||
| 13673 | + ) | ||
| 13674 | + .await; | ||
| 13675 | + let (status, _, _) = common::send_raw( | ||
| 13676 | + &app, | ||
| 13677 | + "GET", | ||
| 13678 | + &format!("/homework/{other}/submission/files/{fid}"), | ||
| 13679 | + Some(&w.teacher), | ||
| 13680 | + None, | ||
| 13681 | + Vec::new(), | ||
| 13682 | + ) | ||
| 13683 | + .await; | ||
| 13684 | + assert_eq!(status, StatusCode::NOT_FOUND, "scoped to its homework"); | ||
| 13685 | + let (status, _, _) = | ||
| 13686 | + common::send_raw(&app, "GET", &file_uri, Some(&w.veli), None, Vec::new()).await; | ||
| 13687 | + assert_eq!(status, StatusCode::NOT_FOUND, "classmates see nothing"); | ||
| 13688 | + | ||
| 13689 | + // Delete removes the row and unlinks the blob. | ||
| 13690 | + let keys = homework_blob_keys(&db).await; | ||
| 13691 | + assert_eq!(keys.len(), 1); | ||
| 13692 | + let res = send(&app, "DELETE", &file_uri, Some(&w.ali), None).await; | ||
| 13693 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 13694 | + assert!( | ||
| 13695 | + !common::files_dir().join(&keys[0]).exists(), | ||
| 13696 | + "blob must be unlinked with its row" | ||
| 13697 | + ); | ||
| 13698 | + let res = send( | ||
| 13699 | + &app, | ||
| 13700 | + "GET", | ||
| 13701 | + &format!("/homework/{hw}/submission"), | ||
| 13702 | + Some(&w.ali), | ||
| 13703 | + None, | ||
| 13704 | + ) | ||
| 13705 | + .await; | ||
| 13706 | + assert_eq!(res.body["files"].as_array().unwrap().len(), 0); | ||
| 13707 | +} | ||
| 13708 | + | ||
| 13709 | +/// File limits: empty uploads are rejected, the 11th file trips the cap, and | ||
| 13710 | +/// the school's `max_file_bytes` bounds each file (413 over, 201 at). | ||
| 13711 | +#[tokio::test] | ||
| 13712 | +async fn homework_file_limits_follow_cap_and_settings() { | ||
| 13713 | + let (app, db) = app_and_db().await; | ||
| 13714 | + let w = hw_world(&app, &db).await; | ||
| 13715 | + let boss = login_as(&app, &db, "boss", "manager").await; | ||
| 13716 | + let now = Timestamp::now().as_millis(); | ||
| 13717 | + let hw = create_homework( | ||
| 13718 | + &app, | ||
| 13719 | + &w.teacher, | ||
| 13720 | + &w.course, | ||
| 13721 | + &w.subject, | ||
| 13722 | + "many files", | ||
| 13723 | + now + 3_600_000, | ||
| 13724 | + ) | ||
| 13725 | + .await; | ||
| 13726 | + | ||
| 13727 | + let res = upload_hw_file(&app, &w.ali, &hw, "empty.txt", "text/plain", b"").await; | ||
| 13728 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | ||
| 13729 | + | ||
| 13730 | + for i in 0..10 { | ||
| 13731 | + let res = upload_hw_file(&app, &w.ali, &hw, &format!("f{i}.txt"), "text/plain", b"x").await; | ||
| 13732 | + assert_eq!(res.status, StatusCode::CREATED, "file {i}: {}", res.body); | ||
| 13733 | + } | ||
| 13734 | + let res = upload_hw_file(&app, &w.ali, &hw, "f10.txt", "text/plain", b"x").await; | ||
| 13735 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | ||
| 13736 | + assert_eq!( | ||
| 13737 | + homework_blob_keys(&db).await.len(), | ||
| 13738 | + 10, | ||
| 13739 | + "the losing upload leaves no row" | ||
| 13740 | + ); | ||
| 13741 | + | ||
| 13742 | + let res = send( | ||
| 13743 | + &app, | ||
| 13744 | + "PATCH", | ||
| 13745 | + "/settings", | ||
| 13746 | + Some(&boss), | ||
| 13747 | + Some(json!({ "max_file_bytes": 1024 })), | ||
| 13748 | + ) | ||
| 13749 | + .await; | ||
| 13750 | + assert_eq!(res.status, StatusCode::OK); | ||
| 13751 | + let hw2 = create_homework( | ||
| 13752 | + &app, | ||
| 13753 | + &w.teacher, | ||
| 13754 | + &w.course, | ||
| 13755 | + &w.subject, | ||
| 13756 | + "sized", | ||
| 13757 | + now + 3_600_000, | ||
| 13758 | + ) | ||
| 13759 | + .await; | ||
| 13760 | + let res = upload_hw_file(&app, &w.ali, &hw2, "big.bin", "", &vec![7u8; 1025]).await; | ||
| 13761 | + assert_eq!(res.status, StatusCode::PAYLOAD_TOO_LARGE, "{}", res.body); | ||
| 13762 | + let res = upload_hw_file(&app, &w.ali, &hw2, "fits.bin", "", &vec![7u8; 1024]).await; | ||
| 13763 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 13764 | + assert_eq!(res.body["content_type"], "application/octet-stream"); | ||
| 13765 | +} | ||
| 13766 | + | ||
| 13767 | +/// The roster carries the whole audience — and keeps a straggler's work | ||
| 13768 | +/// visible (flagged `unenrolled`) instead of letting it vanish; a subset | ||
| 13769 | +/// roster lists the named students only; the list pages. | ||
| 13770 | +#[tokio::test] | ||
| 13771 | +async fn homework_roster_covers_audience_and_stragglers() { | ||
| 13772 | + let (app, db) = app_and_db().await; | ||
| 13773 | + let w = hw_world(&app, &db).await; | ||
| 13774 | + let now = Timestamp::now().as_millis(); | ||
| 13775 | + | ||
| 13776 | + let hw = create_homework( | ||
| 13777 | + &app, | ||
| 13778 | + &w.teacher, | ||
| 13779 | + &w.course, | ||
| 13780 | + &w.subject, | ||
| 13781 | + "for all", | ||
| 13782 | + now + 3_600_000, | ||
| 13783 | + ) | ||
| 13784 | + .await; | ||
| 13785 | + let roster_uri = format!("/homework/{hw}/submissions"); | ||
| 13786 | + let res = send(&app, "GET", &roster_uri, Some(&w.teacher), None).await; | ||
| 13787 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13788 | + assert_eq!(common::total(&res.body), 2, "whole course, both enrolled"); | ||
| 13789 | + | ||
| 13790 | + // ali submits, then leaves the course: the work stays on the roster, | ||
| 13791 | + // flagged — and grading the straggler is refused. | ||
| 13792 | + let res = submit_hw(&app, &w.ali, &hw, Some("mine")).await; | ||
| 13793 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 13794 | + let res = send( | ||
| 13795 | + &app, | ||
| 13796 | + "DELETE", | ||
| 13797 | + &format!("/courses/{}/enrollments/{}", w.course, w.ali_id), | ||
| 13798 | + Some(&w.teacher), | ||
| 13799 | + None, | ||
| 13800 | + ) | ||
| 13801 | + .await; | ||
| 13802 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 13803 | + let res = send(&app, "GET", &roster_uri, Some(&w.teacher), None).await; | ||
| 13804 | + assert_eq!(common::total(&res.body), 2, "the straggler's work stays"); | ||
| 13805 | + let rows = common::items(&res.body); | ||
| 13806 | + let ali_row = rows | ||
| 13807 | + .iter() | ||
| 13808 | + .find(|row| row["user"] == w.ali_id.as_str()) | ||
| 13809 | + .unwrap(); | ||
| 13810 | + assert_eq!(ali_row["unenrolled"], true); | ||
| 13811 | + assert_eq!(ali_row["submission"]["text"], "mine"); | ||
| 13812 | + let res = grade_hw(&app, &w.teacher, &hw, &w.ali_id, "done", None).await; | ||
| 13813 | + assert_eq!( | ||
| 13814 | + res.status, | ||
| 13815 | + StatusCode::BAD_REQUEST, | ||
| 13816 | + "no grading the unenrolled" | ||
| 13817 | + ); | ||
| 13818 | + | ||
| 13819 | + // The roster pages like every list. | ||
| 13820 | + let res = send( | ||
| 13821 | + &app, | ||
| 13822 | + "GET", | ||
| 13823 | + &format!("{roster_uri}?limit=1&offset=1"), | ||
| 13824 | + Some(&w.teacher), | ||
| 13825 | + None, | ||
| 13826 | + ) | ||
| 13827 | + .await; | ||
| 13828 | + assert_eq!(common::total(&res.body), 2); | ||
| 13829 | + assert_eq!(common::items(&res.body).len(), 1); | ||
| 13830 | + assert_eq!(res.body["limit"], 1); | ||
| 13831 | + assert_eq!(res.body["offset"], 1); | ||
| 13832 | + | ||
| 13833 | + // A subset roster is the named students, nobody else. | ||
| 13834 | + enroll(&app, &w.teacher, &w.course, &w.ali_id).await; | ||
| 13835 | + let res = common::create_homework_with( | ||
| 13836 | + &app, | ||
| 13837 | + &w.teacher, | ||
| 13838 | + &w.course, | ||
| 13839 | + json!({ "title": "subset", "subject_id": w.subject, "due_at": now + 3_600_000, | ||
| 13840 | + "assigned": [w.ali_id] }), | ||
| 13841 | + ) | ||
| 13842 | + .await; | ||
| 13843 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 13844 | + let subset_hw = id_of(&res.body); | ||
| 13845 | + let res = send( | ||
| 13846 | + &app, | ||
| 13847 | + "GET", | ||
| 13848 | + &format!("/homework/{subset_hw}/submissions"), | ||
| 13849 | + Some(&w.teacher), | ||
| 13850 | + None, | ||
| 13851 | + ) | ||
| 13852 | + .await; | ||
| 13853 | + assert_eq!(common::total(&res.body), 1); | ||
| 13854 | + assert_eq!(common::items(&res.body)[0]["user"], w.ali_id.as_str()); | ||
| 13855 | +} | ||
| 13856 | + | ||
| 13857 | +/// The observer report: full for manager+ and a linked parent, narrowed to | ||
| 13858 | +/// managed courses for a teacher, walled for everyone else — and it carries | ||
| 13859 | +/// statuses, marks, and flags, never file bytes. | ||
| 13860 | +#[tokio::test] | ||
| 13861 | +async fn homework_report_serves_observers_only() { | ||
| 13862 | + let (app, db) = app_and_db().await; | ||
| 13863 | + let w = hw_world(&app, &db).await; | ||
| 13864 | + let admin = login_as(&app, &db, "boss", "admin").await; | ||
| 13865 | + let now = Timestamp::now().as_millis(); | ||
| 13866 | + | ||
| 13867 | + // A second course under another teacher; ali sits in both. | ||
| 13868 | + let rival = login_as(&app, &db, "rival", "teacher").await; | ||
| 13869 | + let course2 = create_course(&app, &rival, "physics").await; | ||
| 13870 | + let subject2 = create_subject(&app, &rival, &course2, "optics").await; | ||
| 13871 | + enroll(&app, &rival, &course2, &w.ali_id).await; | ||
| 13872 | + | ||
| 13873 | + // Three states across the two courses: graded, submitted-late, missing. | ||
| 13874 | + let graded = create_homework( | ||
| 13875 | + &app, | ||
| 13876 | + &w.teacher, | ||
| 13877 | + &w.course, | ||
| 13878 | + &w.subject, | ||
| 13879 | + "graded", | ||
| 13880 | + now + 3_600_000, | ||
| 13881 | + ) | ||
| 13882 | + .await; | ||
| 13883 | + let res = submit_hw(&app, &w.ali, &graded, Some("done")).await; | ||
| 13884 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 13885 | + let res = grade_hw(&app, &w.teacher, &graded, &w.ali_id, "done", Some(90)).await; | ||
| 13886 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13887 | + let overdue = create_homework(&app, &rival, &course2, &subject2, "overdue", now - 30_000).await; | ||
| 13888 | + let res = submit_hw(&app, &w.ali, &overdue, Some("late")).await; | ||
| 13889 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 13890 | + let missing = create_homework(&app, &rival, &course2, &subject2, "skipped", now - 30_000).await; | ||
| 13891 | + | ||
| 13892 | + // The admin reads all three rows, each in its computed state. | ||
| 13893 | + let report_uri = format!("/homework/report/{}", w.ali_id); | ||
| 13894 | + let res = send(&app, "GET", &report_uri, Some(&admin), None).await; | ||
| 13895 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13896 | + assert_eq!(common::total(&res.body), 3); | ||
| 13897 | + let rows = common::items(&res.body); | ||
| 13898 | + let graded_row = rows | ||
| 13899 | + .iter() | ||
| 13900 | + .find(|row| row["homework"] == graded.as_str()) | ||
| 13901 | + .unwrap(); | ||
| 13902 | + assert_eq!(graded_row["submitted"], true); | ||
| 13903 | + assert_eq!(graded_row["late"], false); | ||
| 13904 | + assert_eq!(graded_row["result"]["status"], "done"); | ||
| 13905 | + assert_eq!(graded_row["result"]["mark"], 90); | ||
| 13906 | + let late_row = rows | ||
| 13907 | + .iter() | ||
| 13908 | + .find(|row| row["homework"] == overdue.as_str()) | ||
| 13909 | + .unwrap(); | ||
| 13910 | + assert_eq!(late_row["late"], true); | ||
| 13911 | + assert_eq!(late_row["missing"], false); | ||
| 13912 | + assert!(late_row["result"].is_null()); | ||
| 13913 | + let missing_row = rows | ||
| 13914 | + .iter() | ||
| 13915 | + .find(|row| row["homework"] == missing.as_str()) | ||
| 13916 | + .unwrap(); | ||
| 13917 | + assert_eq!(missing_row["submitted"], false); | ||
| 13918 | + assert_eq!(missing_row["missing"], true); | ||
| 13919 | + | ||
| 13920 | + // A teacher reads only the slice they manage. | ||
| 13921 | + let res = send(&app, "GET", &report_uri, Some(&w.teacher), None).await; | ||
| 13922 | + assert_eq!(common::total(&res.body), 1); | ||
| 13923 | + assert_eq!(common::items(&res.body)[0]["course"], w.course.as_str()); | ||
| 13924 | + | ||
| 13925 | + // A linked parent reads all of it (paged like every list)… | ||
| 13926 | + let mom = login_as(&app, &db, "mom", "parent").await; | ||
| 13927 | + let mom_id = me_id(&app, &mom).await; | ||
| 13928 | + let res = send( | ||
| 13929 | + &app, | ||
| 13930 | + "POST", | ||
| 13931 | + &format!("/users/{mom_id}/students"), | ||
| 13932 | + Some(&admin), | ||
| 13933 | + Some(json!({ "user_id": w.ali_id })), | ||
| 13934 | + ) | ||
| 13935 | + .await; | ||
| 13936 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 13937 | + let res = send(&app, "GET", &report_uri, Some(&mom), None).await; | ||
| 13938 | + assert_eq!(res.status, StatusCode::OK); | ||
| 13939 | + assert_eq!(common::total(&res.body), 3); | ||
| 13940 | + let res = send( | ||
| 13941 | + &app, | ||
| 13942 | + "GET", | ||
| 13943 | + &format!("{report_uri}?limit=2&offset=2"), | ||
| 13944 | + Some(&mom), | ||
| 13945 | + None, | ||
| 13946 | + ) | ||
| 13947 | + .await; | ||
| 13948 | + assert_eq!(common::total(&res.body), 3); | ||
| 13949 | + assert_eq!(common::items(&res.body).len(), 1); | ||
| 13950 | + | ||
| 13951 | + // …but never the underlying work: no submission reads, no file bytes. | ||
| 13952 | + let up = upload_hw_file(&app, &w.ali, &overdue, "p.png", "image/png", b"png").await; | ||
| 13953 | + assert_eq!(up.status, StatusCode::CREATED, "{}", up.body); | ||
| 13954 | + let fid = id_of(&up.body); | ||
| 13955 | + let res = send( | ||
| 13956 | + &app, | ||
| 13957 | + "GET", | ||
| 13958 | + &format!("/homework/{overdue}/submission"), | ||
| 13959 | + Some(&mom), | ||
| 13960 | + None, | ||
| 13961 | + ) | ||
| 13962 | + .await; | ||
| 13963 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 13964 | + let (status, _, _) = common::send_raw( | ||
| 13965 | + &app, | ||
| 13966 | + "GET", | ||
| 13967 | + &format!("/homework/{overdue}/submission/files/{fid}"), | ||
| 13968 | + Some(&mom), | ||
| 13969 | + None, | ||
| 13970 | + Vec::new(), | ||
| 13971 | + ) | ||
| 13972 | + .await; | ||
| 13973 | + assert_eq!(status, StatusCode::FORBIDDEN, "reports, never bytes"); | ||
| 13974 | + | ||
| 13975 | + // Unlinked parent, the student themselves, and a ghost target all fail. | ||
| 13976 | + let dad = login_as(&app, &db, "dad", "parent").await; | ||
| 13977 | + let res = send(&app, "GET", &report_uri, Some(&dad), None).await; | ||
| 13978 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "unlinked parent"); | ||
| 13979 | + let res = send(&app, "GET", &report_uri, Some(&w.ali), None).await; | ||
| 13980 | + assert_eq!( | ||
| 13981 | + res.status, | ||
| 13982 | + StatusCode::FORBIDDEN, | ||
| 13983 | + "self-reads use /homework" | ||
| 13984 | + ); | ||
| 13985 | + let res = send( | ||
| 13986 | + &app, | ||
| 13987 | + "GET", | ||
| 13988 | + "/homework/report/01ZZZZZZZZZZZZZZZZZZZZZZZZ", | ||
| 13989 | + Some(&admin), | ||
| 13990 | + None, | ||
| 13991 | + ) | ||
| 13992 | + .await; | ||
| 13993 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 13994 | +} | ||
| 13995 | + | ||
| 13996 | +/// Every delete path collects its garbage: withdrawing a submission, deleting | ||
| 13997 | +/// a homework, and deleting the whole course each remove the database rows | ||
| 13998 | +/// *and* the blobs on disk. | ||
| 13999 | +#[tokio::test] | ||
| 14000 | +async fn homework_gc_removes_rows_and_blobs() { | ||
| 14001 | + let (app, db) = app_and_db().await; | ||
| 14002 | + let w = hw_world(&app, &db).await; | ||
| 14003 | + let now = Timestamp::now().as_millis(); | ||
| 14004 | + | ||
| 14005 | + let hw1 = create_homework( | ||
| 14006 | + &app, | ||
| 14007 | + &w.teacher, | ||
| 14008 | + &w.course, | ||
| 14009 | + &w.subject, | ||
| 14010 | + "one", | ||
| 14011 | + now + 3_600_000, | ||
| 14012 | + ) | ||
| 14013 | + .await; | ||
| 14014 | + let hw2 = create_homework( | ||
| 14015 | + &app, | ||
| 14016 | + &w.teacher, | ||
| 14017 | + &w.course, | ||
| 14018 | + &w.subject, | ||
| 14019 | + "two", | ||
| 14020 | + now + 3_600_000, | ||
| 14021 | + ) | ||
| 14022 | + .await; | ||
| 14023 | + for (cookie, hw, name) in [ | ||
| 14024 | + (&w.ali, &hw1, "a.txt"), | ||
| 14025 | + (&w.veli, &hw1, "b.txt"), | ||
| 14026 | + (&w.ali, &hw2, "c.txt"), | ||
| 14027 | + ] { | ||
| 14028 | + let res = upload_hw_file(&app, cookie, hw, name, "text/plain", b"data").await; | ||
| 14029 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 14030 | + } | ||
| 14031 | + let res = grade_hw(&app, &w.teacher, &hw1, &w.veli_id, "done", None).await; | ||
| 14032 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 14033 | + let keys = homework_blob_keys(&db).await; | ||
| 14034 | + assert_eq!(keys.len(), 3); | ||
| 14035 | + for key in &keys { | ||
| 14036 | + assert!(common::files_dir().join(key).exists(), "blob {key} on disk"); | ||
| 14037 | + } | ||
| 14038 | + | ||
| 14039 | + // Withdrawing a submission takes its file rows and blobs. | ||
| 14040 | + let res = send( | ||
| 14041 | + &app, | ||
| 14042 | + "DELETE", | ||
| 14043 | + &format!("/homework/{hw2}/submission"), | ||
| 14044 | + Some(&w.ali), | ||
| 14045 | + None, | ||
| 14046 | + ) | ||
| 14047 | + .await; | ||
| 14048 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 14049 | + let remaining = homework_blob_keys(&db).await; | ||
| 14050 | + assert_eq!(remaining.len(), 2); | ||
| 14051 | + for gone in keys.iter().filter(|key| !remaining.contains(key)) { | ||
| 14052 | + assert!( | ||
| 14053 | + !common::files_dir().join(gone).exists(), | ||
| 14054 | + "withdrawn blob {gone} must be unlinked" | ||
| 14055 | + ); | ||
| 14056 | + } | ||
| 14057 | + | ||
| 14058 | + // Deleting the homework cascades submissions, files, and grades — blobs | ||
| 14059 | + // included. | ||
| 14060 | + let res = send( | ||
| 14061 | + &app, | ||
| 14062 | + "DELETE", | ||
| 14063 | + &format!("/homework/{hw1}"), | ||
| 14064 | + Some(&w.teacher), | ||
| 14065 | + None, | ||
| 14066 | + ) | ||
| 14067 | + .await; | ||
| 14068 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 14069 | + assert_eq!(hw_row_count(&db, "homework_submission").await, 0); | ||
| 14070 | + assert_eq!(hw_row_count(&db, "homework_file").await, 0); | ||
| 14071 | + assert_eq!(hw_row_count(&db, "homework_result").await, 0); | ||
| 14072 | + assert_eq!(hw_row_count(&db, "homework").await, 1, "hw2 remains"); | ||
| 14073 | + for key in &remaining { | ||
| 14074 | + assert!( | ||
| 14075 | + !common::files_dir().join(key).exists(), | ||
| 14076 | + "blob {key} must die with the homework" | ||
| 14077 | + ); | ||
| 14078 | + } | ||
| 14079 | + | ||
| 14080 | + // Deleting the course takes everything left. | ||
| 14081 | + let res = upload_hw_file(&app, &w.ali, &hw2, "d.txt", "text/plain", b"data").await; | ||
| 14082 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 14083 | + let res = grade_hw(&app, &w.teacher, &hw2, &w.veli_id, "missing", None).await; | ||
| 14084 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 14085 | + let keys = homework_blob_keys(&db).await; | ||
| 14086 | + assert_eq!(keys.len(), 1); | ||
| 14087 | + let res = send( | ||
| 14088 | + &app, | ||
| 14089 | + "DELETE", | ||
| 14090 | + &format!("/courses/{}", w.course), | ||
| 14091 | + Some(&w.teacher), | ||
| 14092 | + None, | ||
| 14093 | + ) | ||
| 14094 | + .await; | ||
| 14095 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 14096 | + for table in [ | ||
| 14097 | + "homework", | ||
| 14098 | + "homework_submission", | ||
| 14099 | + "homework_file", | ||
| 14100 | + "homework_result", | ||
| 14101 | + ] { | ||
| 14102 | + assert_eq!( | ||
| 14103 | + hw_row_count(&db, table).await, | ||
| 14104 | + 0, | ||
| 14105 | + "{table} died with course" | ||
| 14106 | + ); | ||
| 14107 | + } | ||
| 14108 | + for key in &keys { | ||
| 14109 | + assert!( | ||
| 14110 | + !common::files_dir().join(key).exists(), | ||
| 14111 | + "blob {key} must die with the course" | ||
| 14112 | + ); | ||
| 14113 | + } | ||
| 14114 | +} | ||
| 14115 | + | ||
| 14116 | +/// A promotion sweeps the enrollment but never the homework rows; the gates | ||
| 14117 | +/// re-read the live role and deny, and the roster keeps the stale work | ||
| 14118 | +/// visible, flagged `unenrolled`. | ||
| 14119 | +#[tokio::test] | ||
| 14120 | +async fn homework_rows_survive_promotion_but_gates_deny() { | ||
| 14121 | + let (app, db) = app_and_db().await; | ||
| 14122 | + let w = hw_world(&app, &db).await; | ||
| 14123 | + let admin = login_as(&app, &db, "boss", "admin").await; | ||
| 14124 | + let now = Timestamp::now().as_millis(); | ||
| 14125 | + let hw = create_homework( | ||
| 14126 | + &app, | ||
| 14127 | + &w.teacher, | ||
| 14128 | + &w.course, | ||
| 14129 | + &w.subject, | ||
| 14130 | + "before", | ||
| 14131 | + now + 3_600_000, | ||
| 14132 | + ) | ||
| 14133 | + .await; | ||
| 14134 | + let res = submit_hw(&app, &w.ali, &hw, Some("as a student")).await; | ||
| 14135 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 14136 | + let res = grade_hw(&app, &w.teacher, &hw, &w.ali_id, "done", Some(70)).await; | ||
| 14137 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 14138 | + | ||
| 14139 | + let res = send( | ||
| 14140 | + &app, | ||
| 14141 | + "PATCH", | ||
| 14142 | + &format!("/users/{}/role", w.ali_id), | ||
| 14143 | + Some(&admin), | ||
| 14144 | + Some(json!({ "role": "teacher" })), | ||
| 14145 | + ) | ||
| 14146 | + .await; | ||
| 14147 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 14148 | + assert_eq!(hw_row_count(&db, "homework_submission").await, 1); | ||
| 14149 | + assert_eq!(hw_row_count(&db, "homework_result").await, 1); | ||
| 14150 | + | ||
| 14151 | + // Live-role gates: no submission access, no fresh grades onto staff. | ||
| 14152 | + let res = send( | ||
| 14153 | + &app, | ||
| 14154 | + "GET", | ||
| 14155 | + &format!("/homework/{hw}/submission"), | ||
| 14156 | + Some(&w.ali), | ||
| 14157 | + None, | ||
| 14158 | + ) | ||
| 14159 | + .await; | ||
| 14160 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 14161 | + let res = submit_hw(&app, &w.ali, &hw, Some("as staff")).await; | ||
| 14162 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 14163 | + let res = grade_hw(&app, &w.teacher, &hw, &w.ali_id, "done", None).await; | ||
| 14164 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "no longer a student"); | ||
| 14165 | + | ||
| 14166 | + // The roster keeps the stale work, flagged (the promotion swept the | ||
| 14167 | + // enrollment). | ||
| 14168 | + let res = send( | ||
| 14169 | + &app, | ||
| 14170 | + "GET", | ||
| 14171 | + &format!("/homework/{hw}/submissions"), | ||
| 14172 | + Some(&w.teacher), | ||
| 14173 | + None, | ||
| 14174 | + ) | ||
| 14175 | + .await; | ||
| 14176 | + let rows = common::items(&res.body); | ||
| 14177 | + let row = rows | ||
| 14178 | + .iter() | ||
| 14179 | + .find(|row| row["user"] == w.ali_id.as_str()) | ||
| 14180 | + .unwrap(); | ||
| 14181 | + assert_eq!(row["unenrolled"], true); | ||
| 14182 | + assert_eq!(row["result"]["mark"], 70); | ||
| 14183 | +} | ||
| 14184 | + | ||
| 14185 | +/// A subject with homework can't be deleted (409 naming homework) until the | ||
| 14186 | +/// homework is re-tagged or removed — the guard the exam questions already | ||
| 14187 | +/// have. | ||
| 14188 | +#[tokio::test] | ||
| 14189 | +async fn homework_blocks_subject_delete_until_retagged() { | ||
| 14190 | + let (app, db) = app_and_db().await; | ||
| 14191 | + let w = hw_world(&app, &db).await; | ||
| 14192 | + let now = Timestamp::now().as_millis(); | ||
| 14193 | + let hw = create_homework( | ||
| 14194 | + &app, | ||
| 14195 | + &w.teacher, | ||
| 14196 | + &w.course, | ||
| 14197 | + &w.subject, | ||
| 14198 | + "tagged", | ||
| 14199 | + now + 3_600_000, | ||
| 14200 | + ) | ||
| 14201 | + .await; | ||
| 14202 | + | ||
| 14203 | + let res = send( | ||
| 14204 | + &app, | ||
| 14205 | + "DELETE", | ||
| 14206 | + &format!("/subjects/{}", w.subject), | ||
| 14207 | + Some(&w.teacher), | ||
| 14208 | + None, | ||
| 14209 | + ) | ||
| 14210 | + .await; | ||
| 14211 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | ||
| 14212 | + assert!( | ||
| 14213 | + res.body["error"].as_str().unwrap().contains("homework"), | ||
| 14214 | + "{}", | ||
| 14215 | + res.body | ||
| 14216 | + ); | ||
| 14217 | + | ||
| 14218 | + // Re-tagging frees the old subject and guards the new one. | ||
| 14219 | + let second = create_subject(&app, &w.teacher, &w.course, "geometry").await; | ||
| 14220 | + let res = send( | ||
| 14221 | + &app, | ||
| 14222 | + "PATCH", | ||
| 14223 | + &format!("/homework/{hw}"), | ||
| 14224 | + Some(&w.teacher), | ||
| 14225 | + Some(json!({ "subject_id": second })), | ||
| 14226 | + ) | ||
| 14227 | + .await; | ||
| 14228 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 14229 | + let res = send( | ||
| 14230 | + &app, | ||
| 14231 | + "DELETE", | ||
| 14232 | + &format!("/subjects/{}", w.subject), | ||
| 14233 | + Some(&w.teacher), | ||
| 14234 | + None, | ||
| 14235 | + ) | ||
| 14236 | + .await; | ||
| 14237 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 14238 | + let res = send( | ||
| 14239 | + &app, | ||
| 14240 | + "DELETE", | ||
| 14241 | + &format!("/subjects/{second}"), | ||
| 14242 | + Some(&w.teacher), | ||
| 14243 | + None, | ||
| 14244 | + ) | ||
| 14245 | + .await; | ||
| 14246 | + assert_eq!(res.status, StatusCode::CONFLICT); | ||
| 14247 | +} | ||
| 14248 | + | ||
| 14249 | +/// The two homework lists page through the standard envelope. | ||
| 14250 | +#[tokio::test] | ||
| 14251 | +async fn homework_lists_paginate() { | ||
| 14252 | + let (app, db) = app_and_db().await; | ||
| 14253 | + let w = hw_world(&app, &db).await; | ||
| 14254 | + let now = Timestamp::now().as_millis(); | ||
| 14255 | + for i in 0..3 { | ||
| 14256 | + create_homework( | ||
| 14257 | + &app, | ||
| 14258 | + &w.teacher, | ||
| 14259 | + &w.course, | ||
| 14260 | + &w.subject, | ||
| 14261 | + &format!("hw {i}"), | ||
| 14262 | + now + 3_600_000, | ||
| 14263 | + ) | ||
| 14264 | + .await; | ||
| 14265 | + } | ||
| 14266 | + for uri in [ | ||
| 14267 | + format!("/courses/{}/homework?limit=2&offset=2", w.course), | ||
| 14268 | + "/homework?limit=2&offset=2".to_string(), | ||
| 14269 | + ] { | ||
| 14270 | + let res = send(&app, "GET", &uri, Some(&w.ali), None).await; | ||
| 14271 | + assert_eq!(res.status, StatusCode::OK, "{uri}"); | ||
| 14272 | + assert_eq!(common::total(&res.body), 3, "{uri}"); | ||
| 14273 | + assert_eq!(common::items(&res.body).len(), 1, "{uri}"); | ||
| 14274 | + assert_eq!(res.body["limit"], 2); | ||
| 14275 | + assert_eq!(res.body["offset"], 2); | ||
| 14276 | + } | ||
| 14277 | +} | ||