Stop a walk-out stamp reverting a submission
Browse files at this commit3c6e9b93472eb5ba7dfeafa28b150d8b023df56cParent: 992619b- fix
- stop a walk-out stamp reverting a submission
1 changed file +75 -4
- src/domain/exam_attempt.rs +75 -4
src/domain/exam_attempt.rsmodified+75 -4
| @@ -241,14 +241,29 @@ impl ExamAttempt { | ||
| 241 | 241 | |
| 242 | 242 | /// Stamp (or clear) the walked-out marker. The exam room sets it when the |
| 243 | 243 | /// student's socket closes mid-attempt and clears it when they come back. |
| 244 | + /// | |
| 245 | + /// Writes *only* the `left_at` field (never the whole row): the room's | |
| 246 | + /// teardown reads the attempt, sees it in progress, then stamps here — and | |
| 247 | + /// a submission that lands in that gap must survive. A whole-row write from | |
| 248 | + /// the pre-read snapshot would carry its blank `finished_at` back over the | |
| 249 | + /// fresh submission, silently un-submitting the exam (and, with | |
| 250 | + /// `allow_rejoin` off, locking the student out). | |
| 244 | 251 | pub async fn set_left( |
| 245 | - mut self, | |
| 252 | + self, | |
| 246 | 253 | left_at: Option<Timestamp>, |
| 247 | 254 | db: &Database, |
| 248 | 255 | ) -> Result<ExamAttempt, AppError> { |
| 249 | - self.left_at = left_at; | |
| 250 | - let updated: Option<ExamAttempt> = db.update(self.id.record()).content(self).await?; | |
| 251 | - updated.ok_or(AppError::NotFound) | |
| 256 | + let mut result = db | |
| 257 | + .query("UPDATE $id SET left_at = $left RETURN AFTER") | |
| 258 | + .bind(("id", self.id.record())) | |
| 259 | + .bind(("left", left_at)) | |
| 260 | + .await? | |
| 261 | + .check()?; | |
| 262 | + result | |
| 263 | + .take::<Vec<ExamAttempt>>(0)? | |
| 264 | + .into_iter() | |
| 265 | + .next() | |
| 266 | + .ok_or(AppError::NotFound) | |
| 252 | 267 | } |
| 253 | 268 | |
| 254 | 269 | /// One sitting by id — the exam room re-reads its own attempt this way, |
| @@ -438,4 +453,60 @@ mod tests { | ||
| 438 | 453 | assert!(!created); |
| 439 | 454 | assert_eq!(resumed.get_seq(), 2); |
| 440 | 455 | } |
| 456 | + | |
| 457 | + #[tokio::test] | |
| 458 | + async fn stamping_left_never_reverts_a_submission() { | |
| 459 | + let db = init_mem().await.unwrap(); | |
| 460 | + let (exam, _question) = open_exam_with_question(&db, 1).await; | |
| 461 | + let user = student(); | |
| 462 | + | |
| 463 | + // The student is sitting the exam. | |
| 464 | + let (attempt, _) = ExamAttempt::start(&exam, &user, &db).await.unwrap(); | |
| 465 | + assert_eq!(attempt.get_seq(), 1); | |
| 466 | + | |
| 467 | + // The exam-room teardown reads the attempt while it is still in | |
| 468 | + // progress (`stamp_left`'s read) — a snapshot with `finished_at` unset. | |
| 469 | + let stale = ExamAttempt::read(attempt.get_id(), &db) | |
| 470 | + .await | |
| 471 | + .unwrap() | |
| 472 | + .expect("attempt exists"); | |
| 473 | + assert!(stale.get_finished_at().is_none()); | |
| 474 | + | |
| 475 | + // Before the teardown writes, the student submits (a REST finish, or a | |
| 476 | + // finish over another socket) — the submission lands. | |
| 477 | + let finished = ExamAttempt::read(attempt.get_id(), &db) | |
| 478 | + .await | |
| 479 | + .unwrap() | |
| 480 | + .expect("attempt exists") | |
| 481 | + .finish(&db) | |
| 482 | + .await | |
| 483 | + .unwrap(); | |
| 484 | + assert!(finished.get_finished_at().is_some()); | |
| 485 | + | |
| 486 | + // Now the last socket's teardown stamps `left_at` from its stale | |
| 487 | + // snapshot. Stamping the walk-out must touch only `left_at` — it must | |
| 488 | + // not carry the snapshot's blank `finished_at` back over the fresh | |
| 489 | + // submission, or the student's submitted exam silently reopens (and, | |
| 490 | + // with `allow_rejoin` off, locks them out of a room they left after | |
| 491 | + // finishing). | |
| 492 | + stale.set_left(Some(Timestamp::now()), &db).await.unwrap(); | |
| 493 | + | |
| 494 | + let after = ExamAttempt::read(attempt.get_id(), &db) | |
| 495 | + .await | |
| 496 | + .unwrap() | |
| 497 | + .expect("attempt still exists"); | |
| 498 | + assert!( | |
| 499 | + after.get_finished_at().is_some(), | |
| 500 | + "stamping left_at must not revert a submission that raced it" | |
| 501 | + ); | |
| 502 | + assert!( | |
| 503 | + after.get_left_at().is_some(), | |
| 504 | + "the walk-out is still recorded" | |
| 505 | + ); | |
| 506 | + assert_eq!( | |
| 507 | + after.status(&exam, Timestamp::now()), | |
| 508 | + AttemptStatus::Submitted, | |
| 509 | + "the attempt stays submitted" | |
| 510 | + ); | |
| 511 | + } | |
| 441 | 512 | } |