From ea1782899c3eab2f9675011ab651b62cf6789555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 22 Jul 2026 06:48:34 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(exams):=20reject=20a=20durat?= =?UTF-8?q?ion=20over=20its=20window?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An `async` exam accepted any `duration_ms` inside the global 1min–24h bounds, even one longer than its own `starts_at`..`ends_at` window. Such a schedule saved happily and was then silently truncated at attempt time: a teacher who set 90 minutes inside a 60-minute window saw a 90-minute exam echoed back, while every student actually got 60. The lie only surfaced once students were already sitting it. `ExamSchedule::try_new` now rejects that up front with a 400 (`duration_ms: must fit within the starts_at..ends_at window`), so the impossible schedule never reaches the database at all. Runtime enforcement was already correct — `ExamAttempt::deadline()` caps the personal deadline at `ends_at` — and is untouched; this only stops the teacher from being told something the exam will not honour. The bound is inclusive on purpose. A duration exactly equal to the window is a legitimate "the whole slot is yours" exam, and because `update_exam` re-validates the entire stored schedule on every PATCH, an exclusive bound would have made an existing row like that unpatchable for completely unrelated edits (title, kind, attempt limit). Only `async` can reach the check: `sync` takes no duration and `open` has no window. The unit test `schedule_invariants_hold` used to assert the buggy behaviour, so it flips to the fix plus the inclusive boundary. Two integration tests happened to be built on now-illegal schedules and were moved onto legal ones without losing what they cover. --- README.md | 11 ++++++----- src/domain/exam.rs | 23 +++++++++++++++++++---- src/web/courses.rs | 2 +- src/web/exams.rs | 2 +- tests/integration.rs | 34 +++++++++++++++++++++++++++++----- 5 files changed, 56 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c0b4e13..a7dd20d 100644 --- a/README.md +++ b/README.md @@ -740,11 +740,12 @@ consistent unit (validated together on create and after every `PATCH` merge): *optional* — set it for a per-attempt countdown (`started_at + duration_ms`), omit it for unlimited time (the attempt only ends by submission). -- `duration_ms` is 1 minute to 24 hours wherever it appears. `ends_at` must be - strictly after `starts_at`, and neither may be *set* in the past — on create - or `PATCH` (kept values are exempt, so a running exam stays editable). All - instants are the usual UTC unix-milliseconds, judged only by the server - clock (`GET /time` for sync). +- `duration_ms` is 1 minute to 24 hours wherever it appears, and for `async` + cannot exceed the `starts_at`..`ends_at` window (equal to it is fine). + `ends_at` must be strictly after `starts_at`, and neither may be *set* in + the past — on create or `PATCH` (kept values are exempt, so a running exam + stays editable). All instants are the usual UTC unix-milliseconds, judged + only by the server clock (`GET /time` for sync). Two per-exam policy knobs ride along, both **live-editable** at any point: diff --git a/src/domain/exam.rs b/src/domain/exam.rs index 733295c..56b368a 100644 --- a/src/domain/exam.rs +++ b/src/domain/exam.rs @@ -162,8 +162,9 @@ impl ExamAttemptLimit { /// exam; attempts are rejected), /// - `sync` → `starts_at` + `ends_at`, no duration (everyone's deadline is /// `ends_at`), -/// - `async` → `starts_at` + `ends_at` + `duration_ms` (a student who starts -/// at `t` gets until `min(t + duration_ms, ends_at)`), +/// - `async` → `starts_at` + `ends_at` + `duration_ms`, with `duration_ms` no +/// greater than the window (a student who starts at `t` gets until +/// `min(t + duration_ms, ends_at)`), /// - `open` → no window; `duration_ms` optional (a student who starts at `t` /// gets until `t + duration_ms`, or forever when absent), /// - `ends_at` strictly after `starts_at` whenever the window exists. @@ -212,6 +213,17 @@ impl ExamSchedule { if ends_at <= starts_at { return Err(invalid("ends_at", "must be after starts_at")); } + if let (Some(duration), Some(starts), Some(ends)) = + (duration_ms, starts_at, ends_at) + { + let window_ms = ends.as_millis().saturating_sub(starts.as_millis()); + if duration.as_millis() > window_ms { + return Err(invalid( + "duration_ms", + "must fit within the starts_at..ends_at window", + )); + } + } match (m.as_str(), duration_ms.is_some()) { ("async", false) => { return Err(invalid("duration_ms", "required for an async exam")); @@ -553,8 +565,11 @@ mod tests { assert!(ExamSchedule::try_new(mode("sync"), at(1), None, None).is_err()); assert!(ExamSchedule::try_new(mode("sync"), at(1), at(2), dur).is_err()); - // Async: window plus a per-student duration. - assert!(ExamSchedule::try_new(mode("async"), at(1), at(2), dur).is_ok()); + // Async: window plus a per-student duration, which must fit inside it + // (equal to the window is fine; the window `at(1), at(2)` above is + // too narrow for `dur`, so give it one exactly `dur` wide instead). + assert!(ExamSchedule::try_new(mode("async"), at(1), at(1 + 90 * 60 * 1000), dur).is_ok()); + assert!(ExamSchedule::try_new(mode("async"), at(1), at(2), dur).is_err()); assert!(ExamSchedule::try_new(mode("async"), at(1), at(2), None).is_err()); // Open: no window, duration optional (limited or unlimited time). diff --git a/src/web/courses.rs b/src/web/courses.rs index 4ee9696..ef642a5 100644 --- a/src/web/courses.rs +++ b/src/web/courses.rs @@ -749,7 +749,7 @@ async fn unenroll( request_body = CreateExamInCourse, responses( (status = 201, description = "Exam created", body = ExamResponse), - (status = 400, description = "Invalid fields, kind, attempt limit, or schedule (malformed window, or times in the past)", body = ErrorResponse), + (status = 400, description = "Invalid fields, kind, attempt limit, or schedule (malformed window, duration exceeding the window, or times in the past)", body = ErrorResponse), (status = 401, description = "Not authenticated", body = ErrorResponse), (status = 403, description = "Not the course creator or an assigned teacher (and not a manager/admin)", body = ErrorResponse), (status = 404, description = "Course not found", body = ErrorResponse), diff --git a/src/web/exams.rs b/src/web/exams.rs index fbd536c..78e3a00 100644 --- a/src/web/exams.rs +++ b/src/web/exams.rs @@ -317,7 +317,7 @@ async fn get_exam( request_body = UpdateExam, responses( (status = 200, description = "Updated exam", body = ExamResponse), - (status = 400, description = "Invalid fields, kind, attempt limit, or schedule (malformed window, or newly set times in the past)", body = ErrorResponse), + (status = 400, description = "Invalid fields, kind, attempt limit, or schedule (malformed window, duration exceeding the window, or newly set times in the past)", body = ErrorResponse), (status = 401, description = "Not authenticated", body = ErrorResponse), (status = 403, description = "Not the course creator or an assigned teacher (and not a manager/admin)", body = ErrorResponse), (status = 404, description = "Not found", body = ErrorResponse), diff --git a/tests/integration.rs b/tests/integration.rs index dc70329..8a9ebcc 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -5624,6 +5624,20 @@ async fn exam_scheduling_validates_and_echoes() { assert_eq!(res.body["mode"], "async"); assert_eq!(res.body["duration_ms"].as_i64(), Some(5_400_000)); + // Async: a duration exactly as long as the window is fine — only + // strictly exceeding it is rejected (see "duration exceeds window" below). + scheduled_exam( + &app, + &teacher, + &course, + json!({ + "title": "exact-fit", "kind": "quiz", + "mode": "async", "starts_at": now, "ends_at": now + 3_600_000, + "duration_ms": 3_600_000, + }), + ) + .await; + // Unscheduled: every schedule field stays null (pre-schedule behavior), // and the attempt policy shows its defaults. let res = create_exam_with( @@ -5711,6 +5725,13 @@ async fn exam_scheduling_validates_and_echoes() { json!({ "title": "x", "kind": "quiz", "mode": "async", "starts_at": now, "ends_at": now + 90_000_000, "duration_ms": 86_400_001 }), ), + ( + // Within the global 1min-24h bounds, but longer than its own + // window: a 90-minute duration inside a 60-minute window. + "duration exceeds window", + json!({ "title": "x", "kind": "quiz", "mode": "async", + "starts_at": now, "ends_at": now + 3_600_000, "duration_ms": 5_400_000 }), + ), ( "past starts_at", json!({ "title": "x", "kind": "quiz", "mode": "sync", @@ -6139,14 +6160,17 @@ async fn async_deadline_is_start_plus_duration_clamped_to_window() { let started = res.body["started_at"].as_i64().expect("started_at"); assert_eq!(res.body["deadline"].as_i64(), Some(started + 60_000)); - // Tight window: `ends_at` clamps the personal deadline. - let ends = now + 30_000; + // Tight window: starting partway through it still lets `ends_at` clamp + // the personal deadline (duration_ms can be at most the full window now, + // so the clamp only bites once a student starts late inside it). + let starts = now - 40_000; + let ends = now + 20_000; let exam = scheduled_exam( &app, &teacher, &course, json!({ "title": "quiz2", "kind": "quiz", - "mode": "async", "starts_at": now - 1_000, "ends_at": ends, + "mode": "async", "starts_at": starts, "ends_at": ends, "duration_ms": 60_000 }), ) .await; @@ -6393,7 +6417,7 @@ async fn mode_freezes_after_attempts_but_times_extend_live() { "PATCH", &format!("/exams/{exam}"), Some(&teacher), - Some(json!({ "mode": "async", "duration_ms": 3_600_000 })), + Some(json!({ "mode": "async", "duration_ms": 60_000 })), ) .await; assert_eq!(res.status, StatusCode::OK, "{}", res.body); @@ -6446,7 +6470,7 @@ async fn mode_freezes_after_attempts_but_times_extend_live() { "PATCH", &format!("/exams/{exam}"), Some(&teacher), - Some(json!({ "mode": "async", "duration_ms": 3_600_000 })), + Some(json!({ "mode": "async", "duration_ms": 60_000 })), ) .await; assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body);