Open lifecycle, retake limits, rejoin door
Browse files at this commitd341bdfdc5150123243416d7d3ce6f53a183b54fParent: 9835fe7- test
- open lifecycle, retake limits, rejoin door
2 changed files +657 -21
- tests/e2e.rs +228 -0
- tests/integration.rs +429 -21
tests/e2e.rsmodified+228 -0
| @@ -1015,3 +1015,231 @@ async fn exam_room_expires_mid_session() { | ||
| 1015 | 1015 | assert_eq!(sheet["answers"][0]["selected"], 1); |
| 1016 | 1016 | assert_eq!(sheet["auto_score"]["earned"], 10); |
| 1017 | 1017 | } |
| 1018 | + | |
| 1019 | +/// The student's latest attempt as their own REST view sees it. | |
| 1020 | +async fn my_attempt(room: &ExamRoom) -> Value { | |
| 1021 | + room.student | |
| 1022 | + .get(format!("{}/exams/{}/attempt", room.base, room.exam_id)) | |
| 1023 | + .send() | |
| 1024 | + .await | |
| 1025 | + .unwrap() | |
| 1026 | + .json() | |
| 1027 | + .await | |
| 1028 | + .unwrap() | |
| 1029 | +} | |
| 1030 | + | |
| 1031 | +/// Wait (bounded) for the room teardown to stamp `left_at` — the socket close | |
| 1032 | +/// and the server's stamp race, so poll the REST view briefly. | |
| 1033 | +async fn wait_for_left_at(room: &ExamRoom) -> i64 { | |
| 1034 | + for _ in 0..50 { | |
| 1035 | + let attempt = my_attempt(room).await; | |
| 1036 | + if let Some(left_at) = attempt["left_at"].as_i64() { | |
| 1037 | + return left_at; | |
| 1038 | + } | |
| 1039 | + tokio::time::sleep(std::time::Duration::from_millis(100)).await; | |
| 1040 | + } | |
| 1041 | + panic!("left_at was never stamped after the socket closed"); | |
| 1042 | +} | |
| 1043 | + | |
| 1044 | +/// The rejoin door: with `allow_rejoin` off, walking out of the room stamps | |
| 1045 | +/// `left_at` and locks re-entry and further saves (REST included) — finish | |
| 1046 | +/// stays possible — until the teacher flips the door back open, which lets | |
| 1047 | +/// the student reconnect (clearing `left_at`) and keep answering. | |
| 1048 | +#[tokio::test] | |
| 1049 | +async fn exam_room_rejoin_door_is_the_teachers_call() { | |
| 1050 | + let room = exam_room_fixture(600_000).await; | |
| 1051 | + | |
| 1052 | + // Close the door before anyone sits. | |
| 1053 | + let res = room | |
| 1054 | + .teacher | |
| 1055 | + .patch(format!("{}/exams/{}", room.base, room.exam_id)) | |
| 1056 | + .json(&json!({ "allow_rejoin": false })) | |
| 1057 | + .send() | |
| 1058 | + .await | |
| 1059 | + .unwrap(); | |
| 1060 | + assert_eq!(res.status(), StatusCode::OK); | |
| 1061 | + | |
| 1062 | + let res = room | |
| 1063 | + .student | |
| 1064 | + .post(format!("{}/exams/{}/attempt", room.base, room.exam_id)) | |
| 1065 | + .send() | |
| 1066 | + .await | |
| 1067 | + .unwrap(); | |
| 1068 | + assert_eq!(res.status(), StatusCode::CREATED); | |
| 1069 | + | |
| 1070 | + // First entry is fine — the door only matters after a walk-out. | |
| 1071 | + let mut ws = ws_open(&room.base, &room.exam_id, Some(&room.cookie)) | |
| 1072 | + .await | |
| 1073 | + .expect("first entry"); | |
| 1074 | + let state = ws_next_frame(&mut ws).await.expect("connect state"); | |
| 1075 | + assert_eq!(state["status"], "in_progress", "{state}"); | |
| 1076 | + assert_eq!(state["attempt"], 1, "{state}"); | |
| 1077 | + ws.close(None).await.unwrap(); | |
| 1078 | + let left_at = wait_for_left_at(&room).await; | |
| 1079 | + assert!(left_at > 0); | |
| 1080 | + | |
| 1081 | + // Locked out: no REST saves, no re-entry. | |
| 1082 | + let res = room | |
| 1083 | + .student | |
| 1084 | + .post(format!( | |
| 1085 | + "{}/exams/{}/attempt/answers", | |
| 1086 | + room.base, room.exam_id | |
| 1087 | + )) | |
| 1088 | + .json(&json!({ "question_id": room.question_id, "selected": 1 })) | |
| 1089 | + .send() | |
| 1090 | + .await | |
| 1091 | + .unwrap(); | |
| 1092 | + assert_eq!(res.status(), StatusCode::CONFLICT); | |
| 1093 | + assert_eq!( | |
| 1094 | + ws_open(&room.base, &room.exam_id, Some(&room.cookie)) | |
| 1095 | + .await | |
| 1096 | + .expect_err("rejoin is closed"), | |
| 1097 | + 409 | |
| 1098 | + ); | |
| 1099 | + | |
| 1100 | + // The teacher reopens the door live; the student walks back in, which | |
| 1101 | + // clears the stamp, and answering works again. | |
| 1102 | + let res = room | |
| 1103 | + .teacher | |
| 1104 | + .patch(format!("{}/exams/{}", room.base, room.exam_id)) | |
| 1105 | + .json(&json!({ "allow_rejoin": true })) | |
| 1106 | + .send() | |
| 1107 | + .await | |
| 1108 | + .unwrap(); | |
| 1109 | + assert_eq!(res.status(), StatusCode::OK); | |
| 1110 | + let mut ws = ws_open(&room.base, &room.exam_id, Some(&room.cookie)) | |
| 1111 | + .await | |
| 1112 | + .expect("rejoin after the teacher reopened"); | |
| 1113 | + ws_next_frame(&mut ws).await.expect("state after rejoin"); | |
| 1114 | + let attempt = my_attempt(&room).await; | |
| 1115 | + assert!( | |
| 1116 | + attempt["left_at"].is_null(), | |
| 1117 | + "re-entry clears left_at: {attempt}" | |
| 1118 | + ); | |
| 1119 | + ws_send( | |
| 1120 | + &mut ws, | |
| 1121 | + json!({ "type": "answer", "question_id": room.question_id, "selected": 1 }), | |
| 1122 | + ) | |
| 1123 | + .await; | |
| 1124 | + ws_frame_of_type(&mut ws, "saved").await; | |
| 1125 | + | |
| 1126 | + // Walking out again with the door open: stamped, but REST saves still | |
| 1127 | + // land — rejoin is allowed, the stamp is just presence. | |
| 1128 | + ws.close(None).await.unwrap(); | |
| 1129 | + wait_for_left_at(&room).await; | |
| 1130 | + let res = room | |
| 1131 | + .student | |
| 1132 | + .post(format!( | |
| 1133 | + "{}/exams/{}/attempt/answers", | |
| 1134 | + room.base, room.exam_id | |
| 1135 | + )) | |
| 1136 | + .json(&json!({ "question_id": room.question_id, "selected": 0 })) | |
| 1137 | + .send() | |
| 1138 | + .await | |
| 1139 | + .unwrap(); | |
| 1140 | + assert_eq!(res.status(), StatusCode::OK); | |
| 1141 | + | |
| 1142 | + // A locked-out student can still submit what they saved: close the door | |
| 1143 | + // once more and finish over REST. | |
| 1144 | + let res = room | |
| 1145 | + .teacher | |
| 1146 | + .patch(format!("{}/exams/{}", room.base, room.exam_id)) | |
| 1147 | + .json(&json!({ "allow_rejoin": false })) | |
| 1148 | + .send() | |
| 1149 | + .await | |
| 1150 | + .unwrap(); | |
| 1151 | + assert_eq!(res.status(), StatusCode::OK); | |
| 1152 | + let res = room | |
| 1153 | + .student | |
| 1154 | + .post(format!( | |
| 1155 | + "{}/exams/{}/attempt/finish", | |
| 1156 | + room.base, room.exam_id | |
| 1157 | + )) | |
| 1158 | + .send() | |
| 1159 | + .await | |
| 1160 | + .unwrap(); | |
| 1161 | + assert_eq!(res.status(), StatusCode::OK, "finish is exempt from rejoin"); | |
| 1162 | +} | |
| 1163 | + | |
| 1164 | +/// An `open`-mode exam room: no deadline in the state frames, and a retake | |
| 1165 | +/// (attempt #2) enters the room with a blank sheet after the first sitting is | |
| 1166 | +/// submitted. | |
| 1167 | +#[tokio::test] | |
| 1168 | +async fn exam_room_open_mode_runs_untimed_and_retakes() { | |
| 1169 | + let room = exam_room_fixture(600_000).await; | |
| 1170 | + | |
| 1171 | + // A second, open exam in the same course: two sittings, no window. | |
| 1172 | + let exam: Value = room | |
| 1173 | + .teacher | |
| 1174 | + .post(format!("{}/courses/{}/exams", room.base, room.course_id)) | |
| 1175 | + .json(&json!({ | |
| 1176 | + "title": "practice", "kind": "quiz", "weight": 1, | |
| 1177 | + "mode": "open", "max_attempts": 2, | |
| 1178 | + })) | |
| 1179 | + .send() | |
| 1180 | + .await | |
| 1181 | + .unwrap() | |
| 1182 | + .json() | |
| 1183 | + .await | |
| 1184 | + .unwrap(); | |
| 1185 | + let exam_id = exam["id"].as_str().unwrap().to_string(); | |
| 1186 | + let question: Value = room | |
| 1187 | + .teacher | |
| 1188 | + .post(format!("{}/exams/{exam_id}/questions", room.base)) | |
| 1189 | + .json(&json!({ "text": "3 + 3?", "kind": "choice", "points": 5, | |
| 1190 | + "choices": ["5", "6"], "correct": 1 })) | |
| 1191 | + .send() | |
| 1192 | + .await | |
| 1193 | + .unwrap() | |
| 1194 | + .json() | |
| 1195 | + .await | |
| 1196 | + .unwrap(); | |
| 1197 | + let question_id = question["id"].as_str().unwrap().to_string(); | |
| 1198 | + | |
| 1199 | + let res = room | |
| 1200 | + .student | |
| 1201 | + .post(format!("{}/exams/{exam_id}/attempt", room.base)) | |
| 1202 | + .send() | |
| 1203 | + .await | |
| 1204 | + .unwrap(); | |
| 1205 | + assert_eq!(res.status(), StatusCode::CREATED); | |
| 1206 | + | |
| 1207 | + let mut ws = ws_open(&room.base, &exam_id, Some(&room.cookie)) | |
| 1208 | + .await | |
| 1209 | + .expect("open-mode room"); | |
| 1210 | + let state = ws_next_frame(&mut ws).await.expect("connect state"); | |
| 1211 | + assert_eq!(state["status"], "in_progress", "{state}"); | |
| 1212 | + assert_eq!(state["attempt"], 1, "{state}"); | |
| 1213 | + assert!(state["deadline"].is_null(), "{state}"); | |
| 1214 | + assert!(state["remaining_ms"].is_null(), "{state}"); | |
| 1215 | + | |
| 1216 | + ws_send( | |
| 1217 | + &mut ws, | |
| 1218 | + json!({ "type": "answer", "question_id": question_id, "selected": 1 }), | |
| 1219 | + ) | |
| 1220 | + .await; | |
| 1221 | + ws_frame_of_type(&mut ws, "saved").await; | |
| 1222 | + ws_send(&mut ws, json!({ "type": "finish" })).await; | |
| 1223 | + ws_frame_of_type(&mut ws, "finished").await; | |
| 1224 | + assert!(ws_next_frame(&mut ws).await.is_none(), "room closes"); | |
| 1225 | + | |
| 1226 | + // Retake: sitting #2, blank sheet, fresh room. | |
| 1227 | + let res = room | |
| 1228 | + .student | |
| 1229 | + .post(format!("{}/exams/{exam_id}/attempt", room.base)) | |
| 1230 | + .send() | |
| 1231 | + .await | |
| 1232 | + .unwrap(); | |
| 1233 | + assert_eq!(res.status(), StatusCode::CREATED); | |
| 1234 | + let body: Value = res.json().await.unwrap(); | |
| 1235 | + assert_eq!(body["attempt"], 2); | |
| 1236 | + assert_eq!(body["answered"], 0); | |
| 1237 | + | |
| 1238 | + let mut ws = ws_open(&room.base, &exam_id, Some(&room.cookie)) | |
| 1239 | + .await | |
| 1240 | + .expect("room for the retake"); | |
| 1241 | + let state = ws_next_frame(&mut ws).await.expect("state"); | |
| 1242 | + assert_eq!(state["attempt"], 2, "{state}"); | |
| 1243 | + assert_eq!(state["answered"], 0, "{state}"); | |
| 1244 | + ws.close(None).await.unwrap(); | |
| 1245 | +} |
tests/integration.rsmodified+429 -21
| @@ -73,7 +73,15 @@ async fn register_validates_input() { | ||
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | // Interior characters are an allowlist: lowercase, digits, . _ - only. |
| 76 | - for bad in ["a b", "a\nb", "a\u{1b}[31mb", "a<b>c", "a@b.c", "a/b", "a\"b"] { | |
| 76 | + for bad in [ | |
| 77 | + "a b", | |
| 78 | + "a\nb", | |
| 79 | + "a\u{1b}[31mb", | |
| 80 | + "a<b>c", | |
| 81 | + "a@b.c", | |
| 82 | + "a/b", | |
| 83 | + "a\"b", | |
| 84 | + ] { | |
| 77 | 85 | let ugly = json!({ "username": bad, "password": "secret1" }); |
| 78 | 86 | let res = send(&app, "POST", "/auth/register", None, Some(ugly)).await; |
| 79 | 87 | assert_eq!(res.status, StatusCode::BAD_REQUEST, "{bad:?} accepted"); |
| @@ -123,7 +131,10 @@ async fn register_rejects_reserved_usernames() { | ||
| 123 | 131 | let res = send(&app, "POST", "/auth/register", None, Some(creds)).await; |
| 124 | 132 | assert_eq!(res.status, StatusCode::BAD_REQUEST, "{name} accepted"); |
| 125 | 133 | let msg = res.body["error"].as_str().unwrap_or_default(); |
| 126 | - assert!(msg.contains("reserved"), "unexpected error for {name}: {msg}"); | |
| 134 | + assert!( | |
| 135 | + msg.contains("reserved"), | |
| 136 | + "unexpected error for {name}: {msg}" | |
| 137 | + ); | |
| 127 | 138 | } |
| 128 | 139 | |
| 129 | 140 | // The reservation is registration-only policy: the seeded bootstrap admin |
| @@ -3425,7 +3436,8 @@ async fn exam_scheduling_validates_and_echoes() { | ||
| 3425 | 3436 | assert_eq!(res.body["mode"], "async"); |
| 3426 | 3437 | assert_eq!(res.body["duration_ms"].as_i64(), Some(5_400_000)); |
| 3427 | 3438 | |
| 3428 | - // Unscheduled: every schedule field stays null (pre-schedule behavior). | |
| 3439 | + // Unscheduled: every schedule field stays null (pre-schedule behavior), | |
| 3440 | + // and the attempt policy shows its defaults. | |
| 3429 | 3441 | let res = create_exam_with( |
| 3430 | 3442 | &app, |
| 3431 | 3443 | &teacher, |
| @@ -3438,6 +3450,28 @@ async fn exam_scheduling_validates_and_echoes() { | ||
| 3438 | 3450 | assert!(res.body["starts_at"].is_null()); |
| 3439 | 3451 | assert!(res.body["ends_at"].is_null()); |
| 3440 | 3452 | assert!(res.body["duration_ms"].is_null()); |
| 3453 | + assert_eq!(res.body["max_attempts"], 1); | |
| 3454 | + assert_eq!(res.body["allow_rejoin"], true); | |
| 3455 | + | |
| 3456 | + // Open: no window at all; an optional per-attempt duration is fine, and | |
| 3457 | + // the attempt policy echoes what was asked for. | |
| 3458 | + let res = create_exam_with( | |
| 3459 | + &app, | |
| 3460 | + &teacher, | |
| 3461 | + &course, | |
| 3462 | + json!({ | |
| 3463 | + "title": "practice", "kind": "quiz", "weight": 1, "mode": "open", | |
| 3464 | + "duration_ms": 5_400_000, "max_attempts": 0, "allow_rejoin": false, | |
| 3465 | + }), | |
| 3466 | + ) | |
| 3467 | + .await; | |
| 3468 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 3469 | + assert_eq!(res.body["mode"], "open"); | |
| 3470 | + assert!(res.body["starts_at"].is_null()); | |
| 3471 | + assert!(res.body["ends_at"].is_null()); | |
| 3472 | + assert_eq!(res.body["duration_ms"].as_i64(), Some(5_400_000)); | |
| 3473 | + assert_eq!(res.body["max_attempts"], 0); | |
| 3474 | + assert_eq!(res.body["allow_rejoin"], false); | |
| 3441 | 3475 | |
| 3442 | 3476 | // Inconsistent schedules are rejected as a unit. |
| 3443 | 3477 | for (label, body) in [ |
| @@ -3500,6 +3534,24 @@ async fn exam_scheduling_validates_and_echoes() { | ||
| 3500 | 3534 | json!({ "title": "x", "kind": "quiz", "weight": 1, "mode": "sync", |
| 3501 | 3535 | "starts_at": now + 3_600_000, "ends_at": now - 3_600_000 }), |
| 3502 | 3536 | ), |
| 3537 | + ( | |
| 3538 | + "open with a window", | |
| 3539 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "mode": "open", | |
| 3540 | + "starts_at": now + 60_000, "ends_at": now + 120_000 }), | |
| 3541 | + ), | |
| 3542 | + ( | |
| 3543 | + "open with only an ends_at", | |
| 3544 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "mode": "open", | |
| 3545 | + "ends_at": now + 120_000 }), | |
| 3546 | + ), | |
| 3547 | + ( | |
| 3548 | + "negative attempt limit", | |
| 3549 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "max_attempts": -1 }), | |
| 3550 | + ), | |
| 3551 | + ( | |
| 3552 | + "attempt limit over the cap", | |
| 3553 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "max_attempts": 101 }), | |
| 3554 | + ), | |
| 3503 | 3555 | ] { |
| 3504 | 3556 | let res = create_exam_with(&app, &teacher, &course, body).await; |
| 3505 | 3557 | assert_eq!(res.status, StatusCode::BAD_REQUEST, "{label}: {}", res.body); |
| @@ -4181,6 +4233,316 @@ async fn attempts_cascade_with_exam_and_course_deletion() { | ||
| 4181 | 4233 | ); |
| 4182 | 4234 | } |
| 4183 | 4235 | |
| 4236 | +/// An `open` exam is sittable anytime — no window, no deadline — and | |
| 4237 | +/// `max_attempts` meters the retakes: every terminal attempt can be followed | |
| 4238 | +/// by a fresh sitting (blank answer sheet) until the limit is spent, and the | |
| 4239 | +/// limit is live-editable, `0` meaning unlimited. | |
| 4240 | +#[tokio::test] | |
| 4241 | +async fn open_exams_sit_anytime_and_retakes_respect_the_limit() { | |
| 4242 | + let (app, db) = app_and_db().await; | |
| 4243 | + let teacher = login_as(&app, &db, "open_t", "teacher").await; | |
| 4244 | + let student = login(&app, "omer").await; | |
| 4245 | + let student_id = me_id(&app, &student).await; | |
| 4246 | + let course = create_course(&app, &teacher, "practice").await; | |
| 4247 | + enroll(&app, &teacher, &course, &student_id).await; | |
| 4248 | + let exam = scheduled_exam( | |
| 4249 | + &app, | |
| 4250 | + &teacher, | |
| 4251 | + &course, | |
| 4252 | + json!({ "title": "drill", "kind": "quiz", "weight": 1, | |
| 4253 | + "mode": "open", "max_attempts": 2 }), | |
| 4254 | + ) | |
| 4255 | + .await; | |
| 4256 | + let question = create_question( | |
| 4257 | + &app, | |
| 4258 | + &teacher, | |
| 4259 | + &exam, | |
| 4260 | + json!({ "text": "2 + 2?", "kind": "choice", "points": 10, | |
| 4261 | + "choices": ["3", "4"], "correct": 1 }), | |
| 4262 | + ) | |
| 4263 | + .await; | |
| 4264 | + | |
| 4265 | + // First sitting starts right away — no window to wait for — and runs | |
| 4266 | + // without a deadline: it can only end by submission. | |
| 4267 | + let res = send( | |
| 4268 | + &app, | |
| 4269 | + "POST", | |
| 4270 | + &format!("/exams/{exam}/attempt"), | |
| 4271 | + Some(&student), | |
| 4272 | + None, | |
| 4273 | + ) | |
| 4274 | + .await; | |
| 4275 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 4276 | + assert_eq!(res.body["attempt"], 1); | |
| 4277 | + assert_eq!(res.body["attempts_used"], 1); | |
| 4278 | + assert_eq!(res.body["max_attempts"], 2); | |
| 4279 | + assert_eq!(res.body["status"], "in_progress"); | |
| 4280 | + assert!(res.body["deadline"].is_null()); | |
| 4281 | + assert!(res.body["remaining_ms"].is_null()); | |
| 4282 | + | |
| 4283 | + // Answer, then re-"start": the running attempt resumes (200), same clock. | |
| 4284 | + let res = send( | |
| 4285 | + &app, | |
| 4286 | + "POST", | |
| 4287 | + &format!("/exams/{exam}/attempt/answers"), | |
| 4288 | + Some(&student), | |
| 4289 | + Some(json!({ "question_id": question, "selected": 1 })), | |
| 4290 | + ) | |
| 4291 | + .await; | |
| 4292 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4293 | + let res = send( | |
| 4294 | + &app, | |
| 4295 | + "POST", | |
| 4296 | + &format!("/exams/{exam}/attempt"), | |
| 4297 | + Some(&student), | |
| 4298 | + None, | |
| 4299 | + ) | |
| 4300 | + .await; | |
| 4301 | + assert_eq!(res.status, StatusCode::OK); | |
| 4302 | + assert_eq!(res.body["attempt"], 1); | |
| 4303 | + assert_eq!(res.body["answered"], 1); | |
| 4304 | + | |
| 4305 | + // Submit, then start again: sitting #2, and the retake wiped the sheet. | |
| 4306 | + let res = send( | |
| 4307 | + &app, | |
| 4308 | + "POST", | |
| 4309 | + &format!("/exams/{exam}/attempt/finish"), | |
| 4310 | + Some(&student), | |
| 4311 | + None, | |
| 4312 | + ) | |
| 4313 | + .await; | |
| 4314 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4315 | + let res = send( | |
| 4316 | + &app, | |
| 4317 | + "POST", | |
| 4318 | + &format!("/exams/{exam}/attempt"), | |
| 4319 | + Some(&student), | |
| 4320 | + None, | |
| 4321 | + ) | |
| 4322 | + .await; | |
| 4323 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 4324 | + assert_eq!(res.body["attempt"], 2); | |
| 4325 | + assert_eq!(res.body["attempts_used"], 2); | |
| 4326 | + assert_eq!(res.body["answered"], 0, "retake starts from a blank sheet"); | |
| 4327 | + let res = send( | |
| 4328 | + &app, | |
| 4329 | + "GET", | |
| 4330 | + &format!("/exams/{exam}/attempt/questions"), | |
| 4331 | + Some(&student), | |
| 4332 | + None, | |
| 4333 | + ) | |
| 4334 | + .await; | |
| 4335 | + assert_eq!(res.status, StatusCode::OK); | |
| 4336 | + assert!( | |
| 4337 | + res.body[0]["answer"].is_null(), | |
| 4338 | + "wiped answer resurfaced: {}", | |
| 4339 | + res.body | |
| 4340 | + ); | |
| 4341 | + | |
| 4342 | + // The limit is spent after sitting #2 ends: no third start. | |
| 4343 | + let res = send( | |
| 4344 | + &app, | |
| 4345 | + "POST", | |
| 4346 | + &format!("/exams/{exam}/attempt/finish"), | |
| 4347 | + Some(&student), | |
| 4348 | + None, | |
| 4349 | + ) | |
| 4350 | + .await; | |
| 4351 | + assert_eq!(res.status, StatusCode::OK); | |
| 4352 | + let res = send( | |
| 4353 | + &app, | |
| 4354 | + "POST", | |
| 4355 | + &format!("/exams/{exam}/attempt"), | |
| 4356 | + Some(&student), | |
| 4357 | + None, | |
| 4358 | + ) | |
| 4359 | + .await; | |
| 4360 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 4361 | + | |
| 4362 | + // …until the teacher raises it live. `0` = unlimited: sitting #3 opens. | |
| 4363 | + let res = send( | |
| 4364 | + &app, | |
| 4365 | + "PATCH", | |
| 4366 | + &format!("/exams/{exam}"), | |
| 4367 | + Some(&teacher), | |
| 4368 | + Some(json!({ "max_attempts": 0 })), | |
| 4369 | + ) | |
| 4370 | + .await; | |
| 4371 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4372 | + assert_eq!(res.body["max_attempts"], 0); | |
| 4373 | + let res = send( | |
| 4374 | + &app, | |
| 4375 | + "POST", | |
| 4376 | + &format!("/exams/{exam}/attempt"), | |
| 4377 | + Some(&student), | |
| 4378 | + None, | |
| 4379 | + ) | |
| 4380 | + .await; | |
| 4381 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 4382 | + assert_eq!(res.body["attempt"], 3); | |
| 4383 | + assert_eq!(res.body["max_attempts"], 0); | |
| 4384 | + | |
| 4385 | + // Lowering the limit below what's used never kills the running attempt — | |
| 4386 | + // it only blocks the next start once this one is over. | |
| 4387 | + let res = send( | |
| 4388 | + &app, | |
| 4389 | + "PATCH", | |
| 4390 | + &format!("/exams/{exam}"), | |
| 4391 | + Some(&teacher), | |
| 4392 | + Some(json!({ "max_attempts": 1 })), | |
| 4393 | + ) | |
| 4394 | + .await; | |
| 4395 | + assert_eq!(res.status, StatusCode::OK); | |
| 4396 | + let res = send( | |
| 4397 | + &app, | |
| 4398 | + "GET", | |
| 4399 | + &format!("/exams/{exam}/attempt"), | |
| 4400 | + Some(&student), | |
| 4401 | + None, | |
| 4402 | + ) | |
| 4403 | + .await; | |
| 4404 | + assert_eq!(res.status, StatusCode::OK); | |
| 4405 | + assert_eq!(res.body["status"], "in_progress"); | |
| 4406 | + let res = send( | |
| 4407 | + &app, | |
| 4408 | + "POST", | |
| 4409 | + &format!("/exams/{exam}/attempt/finish"), | |
| 4410 | + Some(&student), | |
| 4411 | + None, | |
| 4412 | + ) | |
| 4413 | + .await; | |
| 4414 | + assert_eq!(res.status, StatusCode::OK); | |
| 4415 | + let res = send( | |
| 4416 | + &app, | |
| 4417 | + "POST", | |
| 4418 | + &format!("/exams/{exam}/attempt"), | |
| 4419 | + Some(&student), | |
| 4420 | + None, | |
| 4421 | + ) | |
| 4422 | + .await; | |
| 4423 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 4424 | +} | |
| 4425 | + | |
| 4426 | +/// An `open` exam with a `duration_ms` gives each sitting its own countdown | |
| 4427 | +/// (`started_at + duration`), and retakes inside a sync window restart the | |
| 4428 | +/// sheet while the monitor tracks the latest sitting. | |
| 4429 | +#[tokio::test] | |
| 4430 | +async fn open_duration_and_sync_retakes_shape_the_deadline_and_monitor() { | |
| 4431 | + let (app, db) = app_and_db().await; | |
| 4432 | + let teacher = login_as(&app, &db, "dur_t", "teacher").await; | |
| 4433 | + let student = login(&app, "duru").await; | |
| 4434 | + let student_id = me_id(&app, &student).await; | |
| 4435 | + let course = create_course(&app, &teacher, "timing").await; | |
| 4436 | + enroll(&app, &teacher, &course, &student_id).await; | |
| 4437 | + | |
| 4438 | + // Open + duration: the deadline is exactly start + budget. | |
| 4439 | + let timed = scheduled_exam( | |
| 4440 | + &app, | |
| 4441 | + &teacher, | |
| 4442 | + &course, | |
| 4443 | + json!({ "title": "sprint", "kind": "quiz", "weight": 1, | |
| 4444 | + "mode": "open", "duration_ms": 60_000 }), | |
| 4445 | + ) | |
| 4446 | + .await; | |
| 4447 | + let res = send( | |
| 4448 | + &app, | |
| 4449 | + "POST", | |
| 4450 | + &format!("/exams/{timed}/attempt"), | |
| 4451 | + Some(&student), | |
| 4452 | + None, | |
| 4453 | + ) | |
| 4454 | + .await; | |
| 4455 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 4456 | + let started_at = res.body["started_at"].as_i64().expect("started_at"); | |
| 4457 | + assert_eq!(res.body["deadline"].as_i64(), Some(started_at + 60_000)); | |
| 4458 | + let remaining = res.body["remaining_ms"].as_i64().expect("remaining_ms"); | |
| 4459 | + assert!( | |
| 4460 | + remaining > 0 && remaining <= 60_000, | |
| 4461 | + "remaining {remaining}" | |
| 4462 | + ); | |
| 4463 | + | |
| 4464 | + // Sync exam with retakes: sitting #2 opens inside the window and the | |
| 4465 | + // monitor shows the latest sitting plus the usage count. | |
| 4466 | + let now = Timestamp::now().as_millis(); | |
| 4467 | + let ends = now + 600_000; | |
| 4468 | + let exam = scheduled_exam( | |
| 4469 | + &app, | |
| 4470 | + &teacher, | |
| 4471 | + &course, | |
| 4472 | + json!({ "title": "final", "kind": "final", "weight": 1, | |
| 4473 | + "mode": "sync", "starts_at": now - 1_000, "ends_at": ends, | |
| 4474 | + "max_attempts": 3 }), | |
| 4475 | + ) | |
| 4476 | + .await; | |
| 4477 | + for _ in 0..2 { | |
| 4478 | + let res = send( | |
| 4479 | + &app, | |
| 4480 | + "POST", | |
| 4481 | + &format!("/exams/{exam}/attempt"), | |
| 4482 | + Some(&student), | |
| 4483 | + None, | |
| 4484 | + ) | |
| 4485 | + .await; | |
| 4486 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 4487 | + assert_eq!(res.body["deadline"].as_i64(), Some(ends)); | |
| 4488 | + let res = send( | |
| 4489 | + &app, | |
| 4490 | + "POST", | |
| 4491 | + &format!("/exams/{exam}/attempt/finish"), | |
| 4492 | + Some(&student), | |
| 4493 | + None, | |
| 4494 | + ) | |
| 4495 | + .await; | |
| 4496 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4497 | + } | |
| 4498 | + let res = send( | |
| 4499 | + &app, | |
| 4500 | + "GET", | |
| 4501 | + &format!("/exams/{exam}/live"), | |
| 4502 | + Some(&teacher), | |
| 4503 | + None, | |
| 4504 | + ) | |
| 4505 | + .await; | |
| 4506 | + assert_eq!(res.status, StatusCode::OK); | |
| 4507 | + assert_eq!(res.body["counts"]["submitted"], 1); | |
| 4508 | + let row = &res.body["students"][0]; | |
| 4509 | + assert_eq!(row["user"]["username"], "duru"); | |
| 4510 | + assert_eq!(row["attempt"], 2); | |
| 4511 | + assert_eq!(row["attempts_used"], 2); | |
| 4512 | + assert!(row["left_at"].is_null()); | |
| 4513 | + | |
| 4514 | + // The third sitting is the last: the window still gates retakes, so once | |
| 4515 | + // it shuts (or the limit is spent) starting conflicts. | |
| 4516 | + let res = send( | |
| 4517 | + &app, | |
| 4518 | + "POST", | |
| 4519 | + &format!("/exams/{exam}/attempt"), | |
| 4520 | + Some(&student), | |
| 4521 | + None, | |
| 4522 | + ) | |
| 4523 | + .await; | |
| 4524 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 4525 | + assert_eq!(res.body["attempt"], 3); | |
| 4526 | + let res = send( | |
| 4527 | + &app, | |
| 4528 | + "POST", | |
| 4529 | + &format!("/exams/{exam}/attempt/finish"), | |
| 4530 | + Some(&student), | |
| 4531 | + None, | |
| 4532 | + ) | |
| 4533 | + .await; | |
| 4534 | + assert_eq!(res.status, StatusCode::OK); | |
| 4535 | + let res = send( | |
| 4536 | + &app, | |
| 4537 | + "POST", | |
| 4538 | + &format!("/exams/{exam}/attempt"), | |
| 4539 | + Some(&student), | |
| 4540 | + None, | |
| 4541 | + ) | |
| 4542 | + .await; | |
| 4543 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 4544 | +} | |
| 4545 | + | |
| 4184 | 4546 | #[tokio::test] |
| 4185 | 4547 | async fn live_stream_is_sse_and_teacher_scoped() { |
| 4186 | 4548 | let (app, db) = app_and_db().await; |
| @@ -6495,14 +6857,7 @@ async fn search_rejects_a_blank_query() { | ||
| 6495 | 6857 | login(&app, "alice").await; |
| 6496 | 6858 | |
| 6497 | 6859 | // Whitespace-only (url-encoded spaces) -> 400, not the full user list. |
| 6498 | - let res = send( | |
| 6499 | - &app, | |
| 6500 | - "GET", | |
| 6501 | - "/users/search?q=%20%20", | |
| 6502 | - Some(&teacher), | |
| 6503 | - None, | |
| 6504 | - ) | |
| 6505 | - .await; | |
| 6860 | + let res = send(&app, "GET", "/users/search?q=%20%20", Some(&teacher), None).await; | |
| 6506 | 6861 | assert_eq!(res.status, StatusCode::BAD_REQUEST); |
| 6507 | 6862 | |
| 6508 | 6863 | // Missing `q` entirely is a deserialization failure, not a wildcard. |
| @@ -6604,8 +6959,8 @@ async fn settings_validation_rejects_bad_lists_and_bands() { | ||
| 6604 | 6959 | let manager = login_as(&app, &db, "val.manager", "manager").await; |
| 6605 | 6960 | |
| 6606 | 6961 | let bad = [ |
| 6607 | - json!({ "exam_kinds": [] }), // a school needs at least one kind | |
| 6608 | - json!({ "exam_kinds": [" "] }), // blank entry | |
| 6962 | + json!({ "exam_kinds": [] }), // a school needs at least one kind | |
| 6963 | + json!({ "exam_kinds": [" "] }), // blank entry | |
| 6609 | 6964 | json!({ "exam_kinds": ["Lab", "lab"] }), // case-insensitive duplicate |
| 6610 | 6965 | json!({ "attendance_statuses": ["present", "absent", "late"] }), // core dropped |
| 6611 | 6966 | json!({ "grade_bands": [{ "min": 50, "label": "CC" }] }), // no band starts at 0 |
| @@ -6615,7 +6970,14 @@ async fn settings_validation_rejects_bad_lists_and_bands() { | ||
| 6615 | 6970 | json!({ "grade_bands": [{ "min": 0, "label": " " }] }), |
| 6616 | 6971 | ]; |
| 6617 | 6972 | for body in bad { |
| 6618 | - let res = send(&app, "PATCH", "/settings", Some(&manager), Some(body.clone())).await; | |
| 6973 | + let res = send( | |
| 6974 | + &app, | |
| 6975 | + "PATCH", | |
| 6976 | + "/settings", | |
| 6977 | + Some(&manager), | |
| 6978 | + Some(body.clone()), | |
| 6979 | + ) | |
| 6980 | + .await; | |
| 6619 | 6981 | assert_eq!(res.status, StatusCode::BAD_REQUEST, "should reject {body}"); |
| 6620 | 6982 | } |
| 6621 | 6983 | |
| @@ -6860,7 +7222,14 @@ async fn terms_crud_gates_and_links_to_courses() { | ||
| 6860 | 7222 | "starts_at": 1_600_000_000_000_i64, |
| 6861 | 7223 | "ends_at": 1_610_000_000_000_i64, |
| 6862 | 7224 | }); |
| 6863 | - let res = send(&app, "POST", "/terms", Some(&teacher), Some(past_term.clone())).await; | |
| 7225 | + let res = send( | |
| 7226 | + &app, | |
| 7227 | + "POST", | |
| 7228 | + "/terms", | |
| 7229 | + Some(&teacher), | |
| 7230 | + Some(past_term.clone()), | |
| 7231 | + ) | |
| 7232 | + .await; | |
| 6864 | 7233 | assert_eq!(res.status, StatusCode::FORBIDDEN); |
| 6865 | 7234 | let res = send(&app, "POST", "/terms", Some(&manager), Some(past_term)).await; |
| 6866 | 7235 | assert_eq!(res.status, StatusCode::CREATED); |
| @@ -6954,11 +7323,25 @@ async fn terms_crud_gates_and_links_to_courses() { | ||
| 6954 | 7323 | assert_eq!(res.body["term"].as_str(), Some(term.as_str())); |
| 6955 | 7324 | |
| 6956 | 7325 | // Deleting the term unlinks its courses but never deletes them. |
| 6957 | - let res = send(&app, "DELETE", &format!("/terms/{term}"), Some(&manager), None).await; | |
| 7326 | + let res = send( | |
| 7327 | + &app, | |
| 7328 | + "DELETE", | |
| 7329 | + &format!("/terms/{term}"), | |
| 7330 | + Some(&manager), | |
| 7331 | + None, | |
| 7332 | + ) | |
| 7333 | + .await; | |
| 6958 | 7334 | assert_eq!(res.status, StatusCode::NO_CONTENT); |
| 6959 | 7335 | let res = send(&app, "GET", &format!("/terms/{term}"), Some(&student), None).await; |
| 6960 | 7336 | assert_eq!(res.status, StatusCode::NOT_FOUND); |
| 6961 | - let res = send(&app, "GET", &format!("/courses/{course}"), Some(&teacher), None).await; | |
| 7337 | + let res = send( | |
| 7338 | + &app, | |
| 7339 | + "GET", | |
| 7340 | + &format!("/courses/{course}"), | |
| 7341 | + Some(&teacher), | |
| 7342 | + None, | |
| 7343 | + ) | |
| 7344 | + .await; | |
| 6962 | 7345 | assert_eq!(res.status, StatusCode::OK); |
| 6963 | 7346 | assert_eq!(res.body["title"], "History II"); |
| 6964 | 7347 | assert_eq!(res.body["term"], serde_json::Value::Null); |
| @@ -7088,14 +7471,39 @@ async fn terms_list_newest_first_require_auth_and_unlink_in_bulk() { | ||
| 7088 | 7471 | .await; |
| 7089 | 7472 | let unrelated = id_of(&res.body); |
| 7090 | 7473 | |
| 7091 | - let res = send(&app, "DELETE", &format!("/terms/{newer}"), Some(&manager), None).await; | |
| 7474 | + let res = send( | |
| 7475 | + &app, | |
| 7476 | + "DELETE", | |
| 7477 | + &format!("/terms/{newer}"), | |
| 7478 | + Some(&manager), | |
| 7479 | + None, | |
| 7480 | + ) | |
| 7481 | + .await; | |
| 7092 | 7482 | assert_eq!(res.status, StatusCode::NO_CONTENT); |
| 7093 | 7483 | |
| 7094 | 7484 | 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}"); | |
| 7485 | + let res = send( | |
| 7486 | + &app, | |
| 7487 | + "GET", | |
| 7488 | + &format!("/courses/{course}"), | |
| 7489 | + Some(&teacher), | |
| 7490 | + None, | |
| 7491 | + ) | |
| 7492 | + .await; | |
| 7493 | + assert_eq!( | |
| 7494 | + res.body["term"], | |
| 7495 | + serde_json::Value::Null, | |
| 7496 | + "unlinked {course}" | |
| 7497 | + ); | |
| 7097 | 7498 | } |
| 7098 | - let res = send(&app, "GET", &format!("/courses/{unrelated}"), Some(&teacher), None).await; | |
| 7499 | + let res = send( | |
| 7500 | + &app, | |
| 7501 | + "GET", | |
| 7502 | + &format!("/courses/{unrelated}"), | |
| 7503 | + Some(&teacher), | |
| 7504 | + None, | |
| 7505 | + ) | |
| 7506 | + .await; | |
| 7099 | 7507 | assert_eq!(res.body["term"].as_str(), Some(older.as_str())); |
| 7100 | 7508 | } |
| 7101 | 7509 |