Attempts, questions, answers, and exam-room coverage
Browse files at this commit4169291cc9b34acf024ad55edd1fda773f500a85Parent: ade4fed- test
- attempts, questions, answers, and exam-room coverage
3 changed files +2699 -2
- tests/common/mod.rs +13 -0
- tests/e2e.rs +649 -1
- tests/integration.rs +2037 -1
tests/common/mod.rsmodified+13 -0
| @@ -162,6 +162,19 @@ pub async fn create_exam( | ||
| 162 | 162 | id_of(&res.body) |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | +/// Create an exam inside `course` from a full JSON body (no assertion) — | |
| 166 | +/// for exercising the scheduling fields and their validation. | |
| 167 | +pub async fn create_exam_with(app: &Router, cookie: &str, course: &str, body: Value) -> Res { | |
| 168 | + send( | |
| 169 | + app, | |
| 170 | + "POST", | |
| 171 | + &format!("/courses/{course}/exams"), | |
| 172 | + Some(cookie), | |
| 173 | + Some(body), | |
| 174 | + ) | |
| 175 | + .await | |
| 176 | +} | |
| 177 | + | |
| 165 | 178 | /// Enroll `user_id` into `course` as `cookie` (asserts 200). |
| 166 | 179 | pub async fn enroll(app: &Router, cookie: &str, course: &str, user_id: &str) { |
| 167 | 180 | let res = send( |
tests/e2e.rsmodified+649 -1
| @@ -168,9 +168,32 @@ async fn full_user_journey() { | ||
| 168 | 168 | .await |
| 169 | 169 | .unwrap(); |
| 170 | 170 | assert_eq!(roster.as_array().unwrap().len(), 1); |
| 171 | - assert_eq!(roster[0]["user"], veli_id); | |
| 171 | + // People come back as refs — id plus something a human can read. | |
| 172 | + assert_eq!(roster[0]["user"]["id"], veli_id); | |
| 173 | + assert_eq!(roster[0]["user"]["username"], "veli"); | |
| 174 | + assert_eq!(roster[0]["marked_by"]["username"], "ali"); | |
| 172 | 175 | assert_eq!(roster[0]["status"], "present"); |
| 173 | 176 | |
| 177 | + // --- user search (for the pickers) ----------------------------------- | |
| 178 | + // Teacher+ finds people by fragment; the refs carry no contact details. | |
| 179 | + let found: Value = ali | |
| 180 | + .get(format!("{base}/users/search?q=vel")) | |
| 181 | + .send() | |
| 182 | + .await | |
| 183 | + .unwrap() | |
| 184 | + .json() | |
| 185 | + .await | |
| 186 | + .unwrap(); | |
| 187 | + assert_eq!(found[0]["username"], "veli"); | |
| 188 | + assert!(found[0].get("email").is_none()); | |
| 189 | + | |
| 190 | + let res = veli | |
| 191 | + .get(format!("{base}/users/search?q=ali")) | |
| 192 | + .send() | |
| 193 | + .await | |
| 194 | + .unwrap(); | |
| 195 | + assert_eq!(res.status(), StatusCode::FORBIDDEN); | |
| 196 | + | |
| 174 | 197 | // veli is only a student, so she cannot create events. |
| 175 | 198 | let res = veli |
| 176 | 199 | .post(format!("{base}/events")) |
| @@ -274,3 +297,628 @@ async fn attendance_rollup_across_users() { | ||
| 274 | 297 | .unwrap(); |
| 275 | 298 | assert_eq!(res.status(), StatusCode::UNAUTHORIZED); |
| 276 | 299 | } |
| 300 | + | |
| 301 | +/// The live exam monitor over a real TCP connection: an `EventSource`-style | |
| 302 | +/// GET must yield a `snapshot` event carrying the roster within the first | |
| 303 | +/// ticks (the first one fires immediately on connect). | |
| 304 | +#[tokio::test] | |
| 305 | +async fn live_exam_stream_pushes_snapshots_over_http() { | |
| 306 | + let (base, db) = spawn_server().await; | |
| 307 | + let teacher = client(); | |
| 308 | + register(&teacher, &base, "hoca").await; | |
| 309 | + promote(&db, "hoca", "teacher").await; | |
| 310 | + login(&teacher, &base, "hoca").await; | |
| 311 | + | |
| 312 | + let student = client(); | |
| 313 | + register(&student, &base, "veli").await; | |
| 314 | + login(&student, &base, "veli").await; | |
| 315 | + let student_id: Value = student | |
| 316 | + .get(format!("{base}/auth/me")) | |
| 317 | + .send() | |
| 318 | + .await | |
| 319 | + .unwrap() | |
| 320 | + .json() | |
| 321 | + .await | |
| 322 | + .unwrap(); | |
| 323 | + let student_id = student_id["id"].as_str().unwrap().to_string(); | |
| 324 | + | |
| 325 | + // Course + enrolled student + a sync exam whose window is open now | |
| 326 | + // (window times judged by the server clock, fetched from `/time`). | |
| 327 | + let now: Value = teacher | |
| 328 | + .get(format!("{base}/time")) | |
| 329 | + .send() | |
| 330 | + .await | |
| 331 | + .unwrap() | |
| 332 | + .json() | |
| 333 | + .await | |
| 334 | + .unwrap(); | |
| 335 | + let now = now["now"].as_i64().unwrap(); | |
| 336 | + | |
| 337 | + let course: Value = teacher | |
| 338 | + .post(format!("{base}/courses")) | |
| 339 | + .json(&json!({ "title": "algebra" })) | |
| 340 | + .send() | |
| 341 | + .await | |
| 342 | + .unwrap() | |
| 343 | + .json() | |
| 344 | + .await | |
| 345 | + .unwrap(); | |
| 346 | + let course_id = course["id"].as_str().unwrap(); | |
| 347 | + let res = teacher | |
| 348 | + .post(format!("{base}/courses/{course_id}/enrollments")) | |
| 349 | + .json(&json!({ "user_id": student_id })) | |
| 350 | + .send() | |
| 351 | + .await | |
| 352 | + .unwrap(); | |
| 353 | + assert_eq!(res.status(), StatusCode::OK); | |
| 354 | + let exam: Value = teacher | |
| 355 | + .post(format!("{base}/courses/{course_id}/exams")) | |
| 356 | + .json(&json!({ | |
| 357 | + "title": "final", "kind": "final", "weight": 2, | |
| 358 | + "mode": "sync", "starts_at": now - 1_000, "ends_at": now + 600_000, | |
| 359 | + })) | |
| 360 | + .send() | |
| 361 | + .await | |
| 362 | + .unwrap() | |
| 363 | + .json() | |
| 364 | + .await | |
| 365 | + .unwrap(); | |
| 366 | + let exam_id = exam["id"].as_str().unwrap(); | |
| 367 | + | |
| 368 | + // The student sits down — that's the live-attendance signal. | |
| 369 | + let res = student | |
| 370 | + .post(format!("{base}/exams/{exam_id}/attempt")) | |
| 371 | + .send() | |
| 372 | + .await | |
| 373 | + .unwrap(); | |
| 374 | + assert_eq!(res.status(), StatusCode::CREATED); | |
| 375 | + | |
| 376 | + // Open the stream and read until one full snapshot event has arrived. | |
| 377 | + let mut res = teacher | |
| 378 | + .get(format!("{base}/exams/{exam_id}/live/stream")) | |
| 379 | + .send() | |
| 380 | + .await | |
| 381 | + .unwrap(); | |
| 382 | + assert_eq!(res.status(), StatusCode::OK); | |
| 383 | + let content_type = res | |
| 384 | + .headers() | |
| 385 | + .get("content-type") | |
| 386 | + .expect("content-type") | |
| 387 | + .to_str() | |
| 388 | + .unwrap() | |
| 389 | + .to_string(); | |
| 390 | + assert!( | |
| 391 | + content_type.starts_with("text/event-stream"), | |
| 392 | + "{content_type}" | |
| 393 | + ); | |
| 394 | + | |
| 395 | + let mut body = String::new(); | |
| 396 | + let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(5); | |
| 397 | + while !(body.contains("event: snapshot") && body.contains("\n\n")) { | |
| 398 | + let chunk = tokio::time::timeout_at(deadline, res.chunk()) | |
| 399 | + .await | |
| 400 | + .expect("a snapshot event within 5s") | |
| 401 | + .expect("stream stays open") | |
| 402 | + .expect("stream yields data"); | |
| 403 | + body.push_str(&String::from_utf8_lossy(&chunk)); | |
| 404 | + } | |
| 405 | + assert!(body.contains("\"in_progress\""), "{body}"); | |
| 406 | + assert!(body.contains("veli"), "{body}"); | |
| 407 | + assert!(body.contains("\"enrolled\":1"), "{body}"); | |
| 408 | +} | |
| 409 | + | |
| 410 | +/// Log in without a cookie jar and hand back the raw `session=<token>` pair — | |
| 411 | +/// the exact header value the WebSocket handshake needs (tungstenite carries | |
| 412 | +/// no jar of its own). | |
| 413 | +async fn raw_session_cookie(base: &str, user: &str) -> String { | |
| 414 | + let res = Client::new() | |
| 415 | + .post(format!("{base}/auth/login")) | |
| 416 | + .json(&json!({ "username": user, "password": "secret1" })) | |
| 417 | + .send() | |
| 418 | + .await | |
| 419 | + .unwrap(); | |
| 420 | + assert_eq!(res.status(), StatusCode::OK, "login {user}"); | |
| 421 | + res.headers() | |
| 422 | + .get("set-cookie") | |
| 423 | + .expect("session cookie set on login") | |
| 424 | + .to_str() | |
| 425 | + .unwrap() | |
| 426 | + .split(';') | |
| 427 | + .next() | |
| 428 | + .unwrap() | |
| 429 | + .to_string() | |
| 430 | +} | |
| 431 | + | |
| 432 | +// ---- the exam room (WebSocket) --------------------------------------------- | |
| 433 | + | |
| 434 | +use futures_util::{SinkExt, StreamExt}; | |
| 435 | +use tokio_tungstenite::tungstenite::client::IntoClientRequest; | |
| 436 | +use tokio_tungstenite::tungstenite::protocol::Message; | |
| 437 | +use tokio_tungstenite::{MaybeTlsStream, WebSocketStream, connect_async}; | |
| 438 | + | |
| 439 | +type WsStream = WebSocketStream<MaybeTlsStream<tokio::net::TcpStream>>; | |
| 440 | + | |
| 441 | +/// Open the exam room, optionally authenticated. `Ok` is the upgraded socket; | |
| 442 | +/// `Err` is the HTTP status a pre-upgrade gate refused with. | |
| 443 | +async fn ws_open(base: &str, exam_id: &str, cookie: Option<&str>) -> Result<WsStream, u16> { | |
| 444 | + let url = format!( | |
| 445 | + "{}/exams/{exam_id}/attempt/ws", | |
| 446 | + base.replace("http://", "ws://") | |
| 447 | + ); | |
| 448 | + let mut request = url.into_client_request().unwrap(); | |
| 449 | + if let Some(cookie) = cookie { | |
| 450 | + request | |
| 451 | + .headers_mut() | |
| 452 | + .insert("cookie", cookie.parse().unwrap()); | |
| 453 | + } | |
| 454 | + match connect_async(request).await { | |
| 455 | + Ok((ws, _)) => Ok(ws), | |
| 456 | + Err(tokio_tungstenite::tungstenite::Error::Http(response)) => { | |
| 457 | + Err(response.status().as_u16()) | |
| 458 | + } | |
| 459 | + Err(other) => panic!("unexpected handshake failure: {other}"), | |
| 460 | + } | |
| 461 | +} | |
| 462 | + | |
| 463 | +async fn ws_send(ws: &mut WsStream, frame: Value) { | |
| 464 | + ws.send(Message::Text(frame.to_string().into())) | |
| 465 | + .await | |
| 466 | + .unwrap(); | |
| 467 | +} | |
| 468 | + | |
| 469 | +/// The next JSON text frame, within a deadline; `None` once the server closes. | |
| 470 | +/// 6 s covers the room's 2 s tick with room to spare on a loaded machine. | |
| 471 | +async fn ws_next_frame(ws: &mut WsStream) -> Option<Value> { | |
| 472 | + loop { | |
| 473 | + let message = tokio::time::timeout(std::time::Duration::from_secs(6), ws.next()) | |
| 474 | + .await | |
| 475 | + .expect("a frame within 6s")? | |
| 476 | + .expect("stream stays healthy"); | |
| 477 | + match message { | |
| 478 | + Message::Text(text) => { | |
| 479 | + return Some(serde_json::from_str(text.as_str()).expect("JSON frame")); | |
| 480 | + } | |
| 481 | + Message::Close(_) => return None, | |
| 482 | + _ => continue, | |
| 483 | + } | |
| 484 | + } | |
| 485 | +} | |
| 486 | + | |
| 487 | +/// Skip periodic `state` ticks until a frame of `kind` arrives. | |
| 488 | +async fn ws_frame_of_type(ws: &mut WsStream, kind: &str) -> Value { | |
| 489 | + loop { | |
| 490 | + let frame = ws_next_frame(ws) | |
| 491 | + .await | |
| 492 | + .unwrap_or_else(|| panic!("room closed while waiting for a {kind:?} frame")); | |
| 493 | + if frame["type"] == kind { | |
| 494 | + return frame; | |
| 495 | + } | |
| 496 | + assert_eq!(frame["type"], "state", "unexpected frame: {frame}"); | |
| 497 | + } | |
| 498 | +} | |
| 499 | + | |
| 500 | +/// A booted server with teacher `hoca`, enrolled student `veli` (jar client + | |
| 501 | +/// raw cookie for the handshake), and an open sync exam holding one choice | |
| 502 | +/// question — the spine of every exam-room test. The window closes | |
| 503 | +/// `window_ms` from now. | |
| 504 | +struct ExamRoom { | |
| 505 | + base: String, | |
| 506 | + teacher: Client, | |
| 507 | + student: Client, | |
| 508 | + student_id: String, | |
| 509 | + cookie: String, | |
| 510 | + course_id: String, | |
| 511 | + exam_id: String, | |
| 512 | + question_id: String, | |
| 513 | +} | |
| 514 | + | |
| 515 | +async fn exam_room_fixture(window_ms: i64) -> ExamRoom { | |
| 516 | + let (base, db) = spawn_server().await; | |
| 517 | + let teacher = client(); | |
| 518 | + register(&teacher, &base, "hoca").await; | |
| 519 | + promote(&db, "hoca", "teacher").await; | |
| 520 | + login(&teacher, &base, "hoca").await; | |
| 521 | + | |
| 522 | + let student = client(); | |
| 523 | + register(&student, &base, "veli").await; | |
| 524 | + login(&student, &base, "veli").await; | |
| 525 | + let me: Value = student | |
| 526 | + .get(format!("{base}/auth/me")) | |
| 527 | + .send() | |
| 528 | + .await | |
| 529 | + .unwrap() | |
| 530 | + .json() | |
| 531 | + .await | |
| 532 | + .unwrap(); | |
| 533 | + let student_id = me["id"].as_str().unwrap().to_string(); | |
| 534 | + | |
| 535 | + let now: Value = teacher | |
| 536 | + .get(format!("{base}/time")) | |
| 537 | + .send() | |
| 538 | + .await | |
| 539 | + .unwrap() | |
| 540 | + .json() | |
| 541 | + .await | |
| 542 | + .unwrap(); | |
| 543 | + let now = now["now"].as_i64().unwrap(); | |
| 544 | + let course: Value = teacher | |
| 545 | + .post(format!("{base}/courses")) | |
| 546 | + .json(&json!({ "title": "algebra" })) | |
| 547 | + .send() | |
| 548 | + .await | |
| 549 | + .unwrap() | |
| 550 | + .json() | |
| 551 | + .await | |
| 552 | + .unwrap(); | |
| 553 | + let course_id = course["id"].as_str().unwrap().to_string(); | |
| 554 | + let res = teacher | |
| 555 | + .post(format!("{base}/courses/{course_id}/enrollments")) | |
| 556 | + .json(&json!({ "user_id": student_id })) | |
| 557 | + .send() | |
| 558 | + .await | |
| 559 | + .unwrap(); | |
| 560 | + assert_eq!(res.status(), StatusCode::OK); | |
| 561 | + let exam: Value = teacher | |
| 562 | + .post(format!("{base}/courses/{course_id}/exams")) | |
| 563 | + .json(&json!({ | |
| 564 | + "title": "final", "kind": "final", "weight": 2, | |
| 565 | + "mode": "sync", "starts_at": now - 1_000, "ends_at": now + window_ms, | |
| 566 | + })) | |
| 567 | + .send() | |
| 568 | + .await | |
| 569 | + .unwrap() | |
| 570 | + .json() | |
| 571 | + .await | |
| 572 | + .unwrap(); | |
| 573 | + let exam_id = exam["id"].as_str().unwrap().to_string(); | |
| 574 | + let question: Value = teacher | |
| 575 | + .post(format!("{base}/exams/{exam_id}/questions")) | |
| 576 | + .json(&json!({ "text": "2 + 2?", "kind": "choice", "points": 10, | |
| 577 | + "choices": ["3", "4"], "correct": 1 })) | |
| 578 | + .send() | |
| 579 | + .await | |
| 580 | + .unwrap() | |
| 581 | + .json() | |
| 582 | + .await | |
| 583 | + .unwrap(); | |
| 584 | + let question_id = question["id"].as_str().unwrap().to_string(); | |
| 585 | + let cookie = raw_session_cookie(&base, "veli").await; | |
| 586 | + ExamRoom { | |
| 587 | + base, | |
| 588 | + teacher, | |
| 589 | + student, | |
| 590 | + student_id, | |
| 591 | + cookie, | |
| 592 | + course_id, | |
| 593 | + exam_id, | |
| 594 | + question_id, | |
| 595 | + } | |
| 596 | +} | |
| 597 | + | |
| 598 | +/// The student exam room over a real TCP WebSocket: connect with the session | |
| 599 | +/// cookie, get the state frame, autosave an answer, survive junk input, | |
| 600 | +/// finish, and watch the server close the room — with the result visible to | |
| 601 | +/// the teacher over REST. | |
| 602 | +#[tokio::test] | |
| 603 | +async fn exam_room_websocket_round_trip() { | |
| 604 | + let room = exam_room_fixture(600_000).await; | |
| 605 | + let ExamRoom { | |
| 606 | + base, | |
| 607 | + teacher, | |
| 608 | + student, | |
| 609 | + student_id, | |
| 610 | + cookie, | |
| 611 | + exam_id, | |
| 612 | + question_id, | |
| 613 | + .. | |
| 614 | + } = &room; | |
| 615 | + // A second question so progress counts have something to be partial over. | |
| 616 | + let res = teacher | |
| 617 | + .post(format!("{base}/exams/{exam_id}/questions")) | |
| 618 | + .json(&json!({ "text": "Explain.", "kind": "text", "points": 20 })) | |
| 619 | + .send() | |
| 620 | + .await | |
| 621 | + .unwrap(); | |
| 622 | + assert_eq!(res.status(), StatusCode::CREATED); | |
| 623 | + | |
| 624 | + // The room requires an attempt — the gate rejects at HTTP time (404), | |
| 625 | + // before any upgrade. | |
| 626 | + assert_eq!( | |
| 627 | + ws_open(base, exam_id, Some(cookie)).await.err(), | |
| 628 | + Some(404), | |
| 629 | + "start the attempt first" | |
| 630 | + ); | |
| 631 | + | |
| 632 | + let res = student | |
| 633 | + .post(format!("{base}/exams/{exam_id}/attempt")) | |
| 634 | + .send() | |
| 635 | + .await | |
| 636 | + .unwrap(); | |
| 637 | + assert_eq!(res.status(), StatusCode::CREATED); | |
| 638 | + | |
| 639 | + let mut ws = ws_open(base, exam_id, Some(cookie)).await.expect("upgrade"); | |
| 640 | + | |
| 641 | + // Connect-time state: in progress, nothing answered yet, server clock in. | |
| 642 | + let state = ws_next_frame(&mut ws).await.expect("connect state"); | |
| 643 | + assert_eq!(state["type"], "state", "{state}"); | |
| 644 | + assert_eq!(state["status"], "in_progress"); | |
| 645 | + assert_eq!(state["answered"], 0); | |
| 646 | + assert_eq!(state["question_count"], 2); | |
| 647 | + assert!(state["remaining_ms"].as_i64().unwrap() > 0); | |
| 648 | + assert!(state["now"].as_i64().is_some()); | |
| 649 | + | |
| 650 | + // Ping/pong keeps the client's clock honest between ticks. | |
| 651 | + ws_send(&mut ws, json!({ "type": "ping" })).await; | |
| 652 | + ws_frame_of_type(&mut ws, "pong").await; | |
| 653 | + | |
| 654 | + // Autosave: answer -> saved ack -> a state showing the progress. | |
| 655 | + ws_send( | |
| 656 | + &mut ws, | |
| 657 | + json!({ "type": "answer", "question_id": question_id, "selected": 1 }), | |
| 658 | + ) | |
| 659 | + .await; | |
| 660 | + let saved = ws_frame_of_type(&mut ws, "saved").await; | |
| 661 | + assert_eq!(saved["question_id"], question_id.as_str()); | |
| 662 | + assert!(saved["updated_at"].as_i64().is_some()); | |
| 663 | + let state = ws_frame_of_type(&mut ws, "state").await; | |
| 664 | + assert_eq!(state["answered"], 1, "{state}"); | |
| 665 | + | |
| 666 | + // A payload that doesn't fit the question is an error frame, not a close. | |
| 667 | + ws_send( | |
| 668 | + &mut ws, | |
| 669 | + json!({ "type": "answer", "question_id": question_id, "text": "4" }), | |
| 670 | + ) | |
| 671 | + .await; | |
| 672 | + let error = ws_frame_of_type(&mut ws, "error").await; | |
| 673 | + assert!( | |
| 674 | + error["message"].as_str().unwrap().contains("selected"), | |
| 675 | + "{error}" | |
| 676 | + ); | |
| 677 | + | |
| 678 | + // Junk and unknown message types too — the room shrugs and stays up. | |
| 679 | + ws.send(Message::Text("not json".into())).await.unwrap(); | |
| 680 | + let error = ws_frame_of_type(&mut ws, "error").await; | |
| 681 | + assert!( | |
| 682 | + error["message"].as_str().unwrap().contains("unrecognized"), | |
| 683 | + "{error}" | |
| 684 | + ); | |
| 685 | + ws_send(&mut ws, json!({ "type": "selfdestruct" })).await; | |
| 686 | + let error = ws_frame_of_type(&mut ws, "error").await; | |
| 687 | + assert!( | |
| 688 | + error["message"].as_str().unwrap().contains("unrecognized"), | |
| 689 | + "{error}" | |
| 690 | + ); | |
| 691 | + ws_send(&mut ws, json!({ "type": "ping" })).await; | |
| 692 | + ws_frame_of_type(&mut ws, "pong").await; | |
| 693 | + | |
| 694 | + // Finish: acknowledged, then the server closes the room. | |
| 695 | + ws_send(&mut ws, json!({ "type": "finish" })).await; | |
| 696 | + let finished = ws_frame_of_type(&mut ws, "finished").await; | |
| 697 | + assert!(finished["finished_at"].as_i64().is_some(), "{finished}"); | |
| 698 | + loop { | |
| 699 | + match ws_next_frame(&mut ws).await { | |
| 700 | + None => break, // closed — as promised | |
| 701 | + Some(frame) => assert_eq!(frame["type"], "state", "{frame}"), | |
| 702 | + } | |
| 703 | + } | |
| 704 | + | |
| 705 | + // The submission and the saved answer are visible over REST. | |
| 706 | + let attempt: Value = student | |
| 707 | + .get(format!("{base}/exams/{exam_id}/attempt")) | |
| 708 | + .send() | |
| 709 | + .await | |
| 710 | + .unwrap() | |
| 711 | + .json() | |
| 712 | + .await | |
| 713 | + .unwrap(); | |
| 714 | + assert_eq!(attempt["status"], "submitted"); | |
| 715 | + assert_eq!(attempt["answered"], 1); | |
| 716 | + | |
| 717 | + let sheet: Value = teacher | |
| 718 | + .get(format!( | |
| 719 | + "{base}/exams/{exam_id}/attempts/{student_id}/answers" | |
| 720 | + )) | |
| 721 | + .send() | |
| 722 | + .await | |
| 723 | + .unwrap() | |
| 724 | + .json() | |
| 725 | + .await | |
| 726 | + .unwrap(); | |
| 727 | + assert_eq!(sheet["answers"][0]["is_correct"], true); | |
| 728 | + assert_eq!(sheet["auto_score"]["earned"], 10); | |
| 729 | + assert_eq!(sheet["auto_score"]["possible"], 10); | |
| 730 | +} | |
| 731 | + | |
| 732 | +/// Every pre-upgrade gate answers with a proper HTTP status, so a rejected | |
| 733 | +/// client sees *why* instead of an instant close. | |
| 734 | +#[tokio::test] | |
| 735 | +async fn exam_room_rejects_bad_handshakes() { | |
| 736 | + let room = exam_room_fixture(600_000).await; | |
| 737 | + | |
| 738 | + // No session cookie: 401 before anything else. | |
| 739 | + assert_eq!( | |
| 740 | + ws_open(&room.base, &room.exam_id, None).await.err(), | |
| 741 | + Some(401) | |
| 742 | + ); | |
| 743 | + | |
| 744 | + // Unknown exam: 404. | |
| 745 | + assert_eq!( | |
| 746 | + ws_open(&room.base, "does-not-exist", Some(&room.cookie)) | |
| 747 | + .await | |
| 748 | + .err(), | |
| 749 | + Some(404) | |
| 750 | + ); | |
| 751 | + | |
| 752 | + // An unscheduled exam has no room to join: 409. | |
| 753 | + let unscheduled: Value = room | |
| 754 | + .teacher | |
| 755 | + .post(format!("{}/courses/{}/exams", room.base, room.course_id)) | |
| 756 | + .json(&json!({ "title": "homework", "kind": "homework", "weight": 1 })) | |
| 757 | + .send() | |
| 758 | + .await | |
| 759 | + .unwrap() | |
| 760 | + .json() | |
| 761 | + .await | |
| 762 | + .unwrap(); | |
| 763 | + let unscheduled_id = unscheduled["id"].as_str().unwrap(); | |
| 764 | + assert_eq!( | |
| 765 | + ws_open(&room.base, unscheduled_id, Some(&room.cookie)) | |
| 766 | + .await | |
| 767 | + .err(), | |
| 768 | + Some(409) | |
| 769 | + ); | |
| 770 | + | |
| 771 | + // Not enrolled: 403, even on a scheduled, open exam. | |
| 772 | + let outsider = client(); | |
| 773 | + register(&outsider, &room.base, "omer").await; | |
| 774 | + let outsider_cookie = raw_session_cookie(&room.base, "omer").await; | |
| 775 | + assert_eq!( | |
| 776 | + ws_open(&room.base, &room.exam_id, Some(&outsider_cookie)) | |
| 777 | + .await | |
| 778 | + .err(), | |
| 779 | + Some(403) | |
| 780 | + ); | |
| 781 | + | |
| 782 | + // A submitted attempt has nothing left to write: 409. | |
| 783 | + let res = room | |
| 784 | + .student | |
| 785 | + .post(format!("{}/exams/{}/attempt", room.base, room.exam_id)) | |
| 786 | + .send() | |
| 787 | + .await | |
| 788 | + .unwrap(); | |
| 789 | + assert_eq!(res.status(), StatusCode::CREATED); | |
| 790 | + let res = room | |
| 791 | + .student | |
| 792 | + .post(format!( | |
| 793 | + "{}/exams/{}/attempt/finish", | |
| 794 | + room.base, room.exam_id | |
| 795 | + )) | |
| 796 | + .send() | |
| 797 | + .await | |
| 798 | + .unwrap(); | |
| 799 | + assert_eq!(res.status(), StatusCode::OK); | |
| 800 | + assert_eq!( | |
| 801 | + ws_open(&room.base, &room.exam_id, Some(&room.cookie)) | |
| 802 | + .await | |
| 803 | + .err(), | |
| 804 | + Some(409) | |
| 805 | + ); | |
| 806 | +} | |
| 807 | + | |
| 808 | +/// A teacher extending `ends_at` mid-exam moves the open room's countdown on | |
| 809 | +/// the next tick — the deadline is re-read, never cached in the socket. | |
| 810 | +#[tokio::test] | |
| 811 | +async fn exam_room_deadline_moves_with_a_live_extension() { | |
| 812 | + let room = exam_room_fixture(600_000).await; | |
| 813 | + let res = room | |
| 814 | + .student | |
| 815 | + .post(format!("{}/exams/{}/attempt", room.base, room.exam_id)) | |
| 816 | + .send() | |
| 817 | + .await | |
| 818 | + .unwrap(); | |
| 819 | + assert_eq!(res.status(), StatusCode::CREATED); | |
| 820 | + | |
| 821 | + let mut ws = ws_open(&room.base, &room.exam_id, Some(&room.cookie)) | |
| 822 | + .await | |
| 823 | + .expect("upgrade"); | |
| 824 | + let state = ws_next_frame(&mut ws).await.expect("connect state"); | |
| 825 | + let deadline = state["deadline"].as_i64().expect("deadline"); | |
| 826 | + | |
| 827 | + let extended = deadline + 300_000; | |
| 828 | + let res = room | |
| 829 | + .teacher | |
| 830 | + .patch(format!("{}/exams/{}", room.base, room.exam_id)) | |
| 831 | + .json(&json!({ "ends_at": extended })) | |
| 832 | + .send() | |
| 833 | + .await | |
| 834 | + .unwrap(); | |
| 835 | + assert_eq!(res.status(), StatusCode::OK); | |
| 836 | + | |
| 837 | + // Within a couple of ticks the room follows. | |
| 838 | + let mut moved = false; | |
| 839 | + for _ in 0..5 { | |
| 840 | + let state = ws_frame_of_type(&mut ws, "state").await; | |
| 841 | + if state["deadline"].as_i64() == Some(extended) { | |
| 842 | + assert!(state["remaining_ms"].as_i64().unwrap() > 300_000); | |
| 843 | + moved = true; | |
| 844 | + break; | |
| 845 | + } | |
| 846 | + } | |
| 847 | + assert!(moved, "the extension reaches the open room"); | |
| 848 | +} | |
| 849 | + | |
| 850 | +/// When the deadline passes mid-session, a tick notices: the room announces | |
| 851 | +/// `expired`, closes, and everything saved in time survives for grading. | |
| 852 | +#[tokio::test] | |
| 853 | +async fn exam_room_expires_mid_session() { | |
| 854 | + // The window closes ~2.6 s in — enough to connect and save, gone by the | |
| 855 | + // second tick. Real time: expiry is judged by the server clock. | |
| 856 | + let room = exam_room_fixture(2_600).await; | |
| 857 | + let res = room | |
| 858 | + .student | |
| 859 | + .post(format!("{}/exams/{}/attempt", room.base, room.exam_id)) | |
| 860 | + .send() | |
| 861 | + .await | |
| 862 | + .unwrap(); | |
| 863 | + assert_eq!(res.status(), StatusCode::CREATED); | |
| 864 | + | |
| 865 | + let mut ws = ws_open(&room.base, &room.exam_id, Some(&room.cookie)) | |
| 866 | + .await | |
| 867 | + .expect("upgrade"); | |
| 868 | + let state = ws_next_frame(&mut ws).await.expect("connect state"); | |
| 869 | + assert_eq!(state["status"], "in_progress", "{state}"); | |
| 870 | + | |
| 871 | + // One answer lands inside the window. | |
| 872 | + ws_send( | |
| 873 | + &mut ws, | |
| 874 | + json!({ "type": "answer", "question_id": room.question_id, "selected": 1 }), | |
| 875 | + ) | |
| 876 | + .await; | |
| 877 | + ws_frame_of_type(&mut ws, "saved").await; | |
| 878 | + | |
| 879 | + // A tick notices the deadline: `expired`, then the room closes. | |
| 880 | + ws_frame_of_type(&mut ws, "expired").await; | |
| 881 | + assert!( | |
| 882 | + ws_next_frame(&mut ws).await.is_none(), | |
| 883 | + "room closes after expiring" | |
| 884 | + ); | |
| 885 | + | |
| 886 | + // The server agrees over REST: no more writes, the attempt is expired, | |
| 887 | + // and the in-time answer is still there for the grader. | |
| 888 | + let res = room | |
| 889 | + .student | |
| 890 | + .post(format!( | |
| 891 | + "{}/exams/{}/attempt/answers", | |
| 892 | + room.base, room.exam_id | |
| 893 | + )) | |
| 894 | + .json(&json!({ "question_id": room.question_id, "selected": 0 })) | |
| 895 | + .send() | |
| 896 | + .await | |
| 897 | + .unwrap(); | |
| 898 | + assert_eq!(res.status(), StatusCode::CONFLICT); | |
| 899 | + let attempt: Value = room | |
| 900 | + .student | |
| 901 | + .get(format!("{}/exams/{}/attempt", room.base, room.exam_id)) | |
| 902 | + .send() | |
| 903 | + .await | |
| 904 | + .unwrap() | |
| 905 | + .json() | |
| 906 | + .await | |
| 907 | + .unwrap(); | |
| 908 | + assert_eq!(attempt["status"], "expired"); | |
| 909 | + assert_eq!(attempt["answered"], 1); | |
| 910 | + let sheet: Value = room | |
| 911 | + .teacher | |
| 912 | + .get(format!( | |
| 913 | + "{}/exams/{}/attempts/{}/answers", | |
| 914 | + room.base, room.exam_id, room.student_id | |
| 915 | + )) | |
| 916 | + .send() | |
| 917 | + .await | |
| 918 | + .unwrap() | |
| 919 | + .json() | |
| 920 | + .await | |
| 921 | + .unwrap(); | |
| 922 | + assert_eq!(sheet["answers"][0]["selected"], 1); | |
| 923 | + assert_eq!(sheet["auto_score"]["earned"], 10); | |
| 924 | +} |
tests/integration.rsmodified+2037 -1
Diff too large; view file
| @@ -6,8 +6,11 @@ mod common; | ||
| 6 | 6 | use axum::body::Body; |
| 7 | 7 | use axum::http::{Request, StatusCode}; |
| 8 | 8 | use common::{ |
| 9 | - app_and_db, create_course, create_exam, enroll, id_of, login, login_as, me_id, mem_app, send, | |
| 9 | + app_and_db, create_course, create_exam, create_exam_with, enroll, id_of, login, login_as, | |
| 10 | + me_id, mem_app, send, | |
| 10 | 11 | }; |
| 12 | +use hezarfen_backend::domain::exam::ExamId; | |
| 13 | +use hezarfen_backend::domain::exam_attempt::ExamAttempt; | |
| 11 | 14 | use hezarfen_backend::domain::session::Session; |
| 12 | 15 | use hezarfen_backend::domain::timestamp::Timestamp; |
| 13 | 16 | use hezarfen_backend::domain::user::{Password, User, Username}; |
| @@ -117,6 +120,20 @@ async fn protected_routes_require_session() { | ||
| 117 | 120 | ("POST", "/courses"), |
| 118 | 121 | ("GET", "/courses/me"), |
| 119 | 122 | ("GET", "/marks/me"), |
| 123 | + ("POST", "/exams/x/attempt"), | |
| 124 | + ("GET", "/exams/x/attempt"), | |
| 125 | + ("POST", "/exams/x/attempt/finish"), | |
| 126 | + ("GET", "/exams/x/live"), | |
| 127 | + ("GET", "/exams/x/live/stream"), | |
| 128 | + ("POST", "/exams/x/questions"), | |
| 129 | + ("GET", "/exams/x/questions"), | |
| 130 | + ("PATCH", "/exams/x/questions/y"), | |
| 131 | + ("DELETE", "/exams/x/questions/y"), | |
| 132 | + ("GET", "/exams/x/attempt/questions"), | |
| 133 | + ("POST", "/exams/x/attempt/answers"), | |
| 134 | + ("GET", "/exams/x/attempts/u/answers"), | |
| 135 | + // The WebSocket room authenticates before it upgrades. | |
| 136 | + ("GET", "/exams/x/attempt/ws"), | |
| 120 | 137 | ] { |
| 121 | 138 | let res = send(&app, method, uri, None, None).await; |
| 122 | 139 | assert_eq!(res.status, StatusCode::UNAUTHORIZED, "{method} {uri}"); |
| @@ -3046,3 +3063,2022 @@ async fn admin_seed_refuses_existing_non_admin() { | ||
| 3046 | 3063 | assert_eq!(res.status, StatusCode::OK); |
| 3047 | 3064 | assert_eq!(res.body["role"], "student"); |
| 3048 | 3065 | } |
| 3066 | + | |
| 3067 | +// --- scheduled exams: sync/async attempts + the live monitor --------------- | |
| 3068 | + | |
| 3069 | +/// Create a scheduled exam from a full body and return its id (asserts 201). | |
| 3070 | +async fn scheduled_exam( | |
| 3071 | + app: &axum::Router, | |
| 3072 | + cookie: &str, | |
| 3073 | + course: &str, | |
| 3074 | + body: serde_json::Value, | |
| 3075 | +) -> String { | |
| 3076 | + let res = create_exam_with(app, cookie, course, body).await; | |
| 3077 | + assert_eq!( | |
| 3078 | + res.status, | |
| 3079 | + StatusCode::CREATED, | |
| 3080 | + "create scheduled exam: {}", | |
| 3081 | + res.body | |
| 3082 | + ); | |
| 3083 | + id_of(&res.body) | |
| 3084 | +} | |
| 3085 | + | |
| 3086 | +#[tokio::test] | |
| 3087 | +async fn exam_scheduling_validates_and_echoes() { | |
| 3088 | + let (app, db) = app_and_db().await; | |
| 3089 | + let teacher = login_as(&app, &db, "sched_t", "teacher").await; | |
| 3090 | + let course = create_course(&app, &teacher, "algebra").await; | |
| 3091 | + let now = Timestamp::now().as_millis(); | |
| 3092 | + | |
| 3093 | + // Sync: the window is echoed, no duration. | |
| 3094 | + let res = create_exam_with( | |
| 3095 | + &app, | |
| 3096 | + &teacher, | |
| 3097 | + &course, | |
| 3098 | + json!({ | |
| 3099 | + "title": "midterm", "kind": "midterm", "weight": 2, | |
| 3100 | + "mode": "sync", "starts_at": now + 60_000, "ends_at": now + 120_000, | |
| 3101 | + }), | |
| 3102 | + ) | |
| 3103 | + .await; | |
| 3104 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 3105 | + assert_eq!(res.body["mode"], "sync"); | |
| 3106 | + assert_eq!(res.body["starts_at"].as_i64(), Some(now + 60_000)); | |
| 3107 | + assert_eq!(res.body["ends_at"].as_i64(), Some(now + 120_000)); | |
| 3108 | + assert!(res.body["duration_ms"].is_null()); | |
| 3109 | + | |
| 3110 | + // Async: adds the per-student duration. | |
| 3111 | + let res = create_exam_with( | |
| 3112 | + &app, | |
| 3113 | + &teacher, | |
| 3114 | + &course, | |
| 3115 | + json!({ | |
| 3116 | + "title": "takehome", "kind": "quiz", "weight": 1, | |
| 3117 | + "mode": "async", "starts_at": now, "ends_at": now + 7_200_000, | |
| 3118 | + "duration_ms": 5_400_000, | |
| 3119 | + }), | |
| 3120 | + ) | |
| 3121 | + .await; | |
| 3122 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 3123 | + assert_eq!(res.body["mode"], "async"); | |
| 3124 | + assert_eq!(res.body["duration_ms"].as_i64(), Some(5_400_000)); | |
| 3125 | + | |
| 3126 | + // Unscheduled: every schedule field stays null (pre-schedule behavior). | |
| 3127 | + let res = create_exam_with( | |
| 3128 | + &app, | |
| 3129 | + &teacher, | |
| 3130 | + &course, | |
| 3131 | + json!({ "title": "homework", "kind": "homework", "weight": 1 }), | |
| 3132 | + ) | |
| 3133 | + .await; | |
| 3134 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 3135 | + assert!(res.body["mode"].is_null()); | |
| 3136 | + assert!(res.body["starts_at"].is_null()); | |
| 3137 | + assert!(res.body["ends_at"].is_null()); | |
| 3138 | + assert!(res.body["duration_ms"].is_null()); | |
| 3139 | + | |
| 3140 | + // Inconsistent schedules are rejected as a unit. | |
| 3141 | + for (label, body) in [ | |
| 3142 | + ( | |
| 3143 | + "unknown mode", | |
| 3144 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "mode": "weekly", | |
| 3145 | + "starts_at": now, "ends_at": now + 1_000 }), | |
| 3146 | + ), | |
| 3147 | + ( | |
| 3148 | + "times without mode", | |
| 3149 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "starts_at": now }), | |
| 3150 | + ), | |
| 3151 | + ( | |
| 3152 | + "duration without mode", | |
| 3153 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "duration_ms": 60_000 }), | |
| 3154 | + ), | |
| 3155 | + ( | |
| 3156 | + "sync missing ends_at", | |
| 3157 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "mode": "sync", | |
| 3158 | + "starts_at": now }), | |
| 3159 | + ), | |
| 3160 | + ( | |
| 3161 | + "sync with duration", | |
| 3162 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "mode": "sync", | |
| 3163 | + "starts_at": now, "ends_at": now + 1_000, "duration_ms": 60_000 }), | |
| 3164 | + ), | |
| 3165 | + ( | |
| 3166 | + "async without duration", | |
| 3167 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "mode": "async", | |
| 3168 | + "starts_at": now, "ends_at": now + 1_000 }), | |
| 3169 | + ), | |
| 3170 | + ( | |
| 3171 | + "backwards window", | |
| 3172 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "mode": "sync", | |
| 3173 | + "starts_at": now + 2_000, "ends_at": now + 1_000 }), | |
| 3174 | + ), | |
| 3175 | + ( | |
| 3176 | + "empty window", | |
| 3177 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "mode": "sync", | |
| 3178 | + "starts_at": now, "ends_at": now }), | |
| 3179 | + ), | |
| 3180 | + ( | |
| 3181 | + "duration too short", | |
| 3182 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "mode": "async", | |
| 3183 | + "starts_at": now, "ends_at": now + 90_000_000, "duration_ms": 59_999 }), | |
| 3184 | + ), | |
| 3185 | + ( | |
| 3186 | + "duration too long", | |
| 3187 | + json!({ "title": "x", "kind": "quiz", "weight": 1, "mode": "async", | |
| 3188 | + "starts_at": now, "ends_at": now + 90_000_000, "duration_ms": 86_400_001 }), | |
| 3189 | + ), | |
| 3190 | + ] { | |
| 3191 | + let res = create_exam_with(&app, &teacher, &course, body).await; | |
| 3192 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{label}: {}", res.body); | |
| 3193 | + } | |
| 3194 | +} | |
| 3195 | + | |
| 3196 | +#[tokio::test] | |
| 3197 | +async fn sync_attempt_lifecycle_feeds_the_live_monitor() { | |
| 3198 | + let (app, db) = app_and_db().await; | |
| 3199 | + let teacher = login_as(&app, &db, "live_t", "teacher").await; | |
| 3200 | + let ayse = login(&app, "ayse").await; | |
| 3201 | + let veli = login(&app, "veli").await; | |
| 3202 | + let ayse_id = me_id(&app, &ayse).await; | |
| 3203 | + let veli_id = me_id(&app, &veli).await; | |
| 3204 | + let course = create_course(&app, &teacher, "physics").await; | |
| 3205 | + enroll(&app, &teacher, &course, &ayse_id).await; | |
| 3206 | + enroll(&app, &teacher, &course, &veli_id).await; | |
| 3207 | + | |
| 3208 | + let now = Timestamp::now().as_millis(); | |
| 3209 | + let ends = now + 600_000; | |
| 3210 | + let exam = scheduled_exam( | |
| 3211 | + &app, | |
| 3212 | + &teacher, | |
| 3213 | + &course, | |
| 3214 | + json!({ "title": "final", "kind": "final", "weight": 3, | |
| 3215 | + "mode": "sync", "starts_at": now - 1_000, "ends_at": ends }), | |
| 3216 | + ) | |
| 3217 | + .await; | |
| 3218 | + | |
| 3219 | + // No attempt yet. | |
| 3220 | + let res = send( | |
| 3221 | + &app, | |
| 3222 | + "GET", | |
| 3223 | + &format!("/exams/{exam}/attempt"), | |
| 3224 | + Some(&veli), | |
| 3225 | + None, | |
| 3226 | + ) | |
| 3227 | + .await; | |
| 3228 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | |
| 3229 | + | |
| 3230 | + // Start: 201, in progress, and (sync) the deadline is the window close. | |
| 3231 | + let res = send( | |
| 3232 | + &app, | |
| 3233 | + "POST", | |
| 3234 | + &format!("/exams/{exam}/attempt"), | |
| 3235 | + Some(&veli), | |
| 3236 | + None, | |
| 3237 | + ) | |
| 3238 | + .await; | |
| 3239 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 3240 | + assert_eq!(res.body["status"], "in_progress"); | |
| 3241 | + assert_eq!(res.body["user"]["username"], "veli"); | |
| 3242 | + assert_eq!(res.body["deadline"].as_i64(), Some(ends)); | |
| 3243 | + assert!(res.body["mark"].is_null()); | |
| 3244 | + let started_at = res.body["started_at"].as_i64().expect("started_at"); | |
| 3245 | + let remaining = res.body["remaining_ms"].as_i64().expect("remaining_ms"); | |
| 3246 | + assert!( | |
| 3247 | + remaining > 0 && remaining <= 601_000, | |
| 3248 | + "remaining {remaining}" | |
| 3249 | + ); | |
| 3250 | + | |
| 3251 | + // Re-"start" resumes the same attempt with the original clock: 200. | |
| 3252 | + let res = send( | |
| 3253 | + &app, | |
| 3254 | + "POST", | |
| 3255 | + &format!("/exams/{exam}/attempt"), | |
| 3256 | + Some(&veli), | |
| 3257 | + None, | |
| 3258 | + ) | |
| 3259 | + .await; | |
| 3260 | + assert_eq!(res.status, StatusCode::OK); | |
| 3261 | + assert_eq!(res.body["started_at"].as_i64(), Some(started_at)); | |
| 3262 | + | |
| 3263 | + // The monitor sees one writer and one absentee, sorted by username. | |
| 3264 | + let res = send( | |
| 3265 | + &app, | |
| 3266 | + "GET", | |
| 3267 | + &format!("/exams/{exam}/live"), | |
| 3268 | + Some(&teacher), | |
| 3269 | + None, | |
| 3270 | + ) | |
| 3271 | + .await; | |
| 3272 | + assert_eq!(res.status, StatusCode::OK); | |
| 3273 | + assert_eq!(res.body["counts"]["enrolled"], 2); | |
| 3274 | + assert_eq!(res.body["counts"]["in_progress"], 1); | |
| 3275 | + assert_eq!(res.body["counts"]["not_started"], 1); | |
| 3276 | + assert!(res.body["now"].as_i64().is_some()); | |
| 3277 | + let students = res.body["students"].as_array().expect("students"); | |
| 3278 | + assert_eq!(students[0]["user"]["username"], "ayse"); | |
| 3279 | + assert_eq!(students[0]["status"], "not_started"); | |
| 3280 | + assert!(students[0]["started_at"].is_null()); | |
| 3281 | + assert!(students[0]["deadline"].is_null()); | |
| 3282 | + assert_eq!(students[1]["user"]["username"], "veli"); | |
| 3283 | + assert_eq!(students[1]["status"], "in_progress"); | |
| 3284 | + assert!(students[1]["remaining_ms"].as_i64().expect("remaining") > 0); | |
| 3285 | + | |
| 3286 | + // Submit, then grade mid-session; both land in the next snapshot. | |
| 3287 | + let res = send( | |
| 3288 | + &app, | |
| 3289 | + "POST", | |
| 3290 | + &format!("/exams/{exam}/attempt/finish"), | |
| 3291 | + Some(&veli), | |
| 3292 | + None, | |
| 3293 | + ) | |
| 3294 | + .await; | |
| 3295 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 3296 | + assert_eq!(res.body["status"], "submitted"); | |
| 3297 | + assert!(res.body["finished_at"].as_i64().is_some()); | |
| 3298 | + assert!(res.body["remaining_ms"].is_null()); | |
| 3299 | + | |
| 3300 | + let res = send( | |
| 3301 | + &app, | |
| 3302 | + "POST", | |
| 3303 | + &format!("/exams/{exam}/results"), | |
| 3304 | + Some(&teacher), | |
| 3305 | + Some(json!({ "mark": 85, "user_id": veli_id })), | |
| 3306 | + ) | |
| 3307 | + .await; | |
| 3308 | + assert_eq!(res.status, StatusCode::OK); | |
| 3309 | + | |
| 3310 | + let res = send( | |
| 3311 | + &app, | |
| 3312 | + "GET", | |
| 3313 | + &format!("/exams/{exam}/live"), | |
| 3314 | + Some(&teacher), | |
| 3315 | + None, | |
| 3316 | + ) | |
| 3317 | + .await; | |
| 3318 | + assert_eq!(res.body["counts"]["submitted"], 1); | |
| 3319 | + assert_eq!(res.body["counts"]["graded"], 1); | |
| 3320 | + assert_eq!(res.body["students"][1]["mark"], 85); | |
| 3321 | + | |
| 3322 | + // The student's own view carries the mark; the monitor stays teacher+. | |
| 3323 | + let res = send( | |
| 3324 | + &app, | |
| 3325 | + "GET", | |
| 3326 | + &format!("/exams/{exam}/attempt"), | |
| 3327 | + Some(&veli), | |
| 3328 | + None, | |
| 3329 | + ) | |
| 3330 | + .await; | |
| 3331 | + assert_eq!(res.body["status"], "submitted"); | |
| 3332 | + assert_eq!(res.body["mark"], 85); | |
| 3333 | + let res = send( | |
| 3334 | + &app, | |
| 3335 | + "GET", | |
| 3336 | + &format!("/exams/{exam}/live"), | |
| 3337 | + Some(&veli), | |
| 3338 | + None, | |
| 3339 | + ) | |
| 3340 | + .await; | |
| 3341 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | |
| 3342 | +} | |
| 3343 | + | |
| 3344 | +#[tokio::test] | |
| 3345 | +async fn async_deadline_is_start_plus_duration_clamped_to_window() { | |
| 3346 | + let (app, db) = app_and_db().await; | |
| 3347 | + let teacher = login_as(&app, &db, "async_t", "teacher").await; | |
| 3348 | + let student = login(&app, "asli").await; | |
| 3349 | + let student_id = me_id(&app, &student).await; | |
| 3350 | + let course = create_course(&app, &teacher, "chem").await; | |
| 3351 | + enroll(&app, &teacher, &course, &student_id).await; | |
| 3352 | + let now = Timestamp::now().as_millis(); | |
| 3353 | + | |
| 3354 | + // Roomy window: the personal duration is the binding constraint. | |
| 3355 | + let exam = scheduled_exam( | |
| 3356 | + &app, | |
| 3357 | + &teacher, | |
| 3358 | + &course, | |
| 3359 | + json!({ "title": "quiz", "kind": "quiz", "weight": 1, | |
| 3360 | + "mode": "async", "starts_at": now - 1_000, "ends_at": now + 6_000_000, | |
| 3361 | + "duration_ms": 60_000 }), | |
| 3362 | + ) | |
| 3363 | + .await; | |
| 3364 | + let res = send( | |
| 3365 | + &app, | |
| 3366 | + "POST", | |
| 3367 | + &format!("/exams/{exam}/attempt"), | |
| 3368 | + Some(&student), | |
| 3369 | + None, | |
| 3370 | + ) | |
| 3371 | + .await; | |
| 3372 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 3373 | + let started = res.body["started_at"].as_i64().expect("started_at"); | |
| 3374 | + assert_eq!(res.body["deadline"].as_i64(), Some(started + 60_000)); | |
| 3375 | + | |
| 3376 | + // Tight window: `ends_at` clamps the personal deadline. | |
| 3377 | + let ends = now + 30_000; | |
| 3378 | + let exam = scheduled_exam( | |
| 3379 | + &app, | |
| 3380 | + &teacher, | |
| 3381 | + &course, | |
| 3382 | + json!({ "title": "quiz2", "kind": "quiz", "weight": 1, | |
| 3383 | + "mode": "async", "starts_at": now - 1_000, "ends_at": ends, | |
| 3384 | + "duration_ms": 60_000 }), | |
| 3385 | + ) | |
| 3386 | + .await; | |
| 3387 | + let res = send( | |
| 3388 | + &app, | |
| 3389 | + "POST", | |
| 3390 | + &format!("/exams/{exam}/attempt"), | |
| 3391 | + Some(&student), | |
| 3392 | + None, | |
| 3393 | + ) | |
| 3394 | + .await; | |
| 3395 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 3396 | + assert_eq!(res.body["deadline"].as_i64(), Some(ends)); | |
| 3397 | +} | |
| 3398 | + | |
| 3399 | +#[tokio::test] | |
| 3400 | +async fn attempts_gate_on_schedule_enrollment_and_window() { | |
| 3401 | + let (app, db) = app_and_db().await; | |
| 3402 | + let teacher = login_as(&app, &db, "gate_t", "teacher").await; | |
| 3403 | + let student = login(&app, "gita").await; | |
| 3404 | + let student_id = me_id(&app, &student).await; | |
| 3405 | + let course = create_course(&app, &teacher, "bio").await; | |
| 3406 | + enroll(&app, &teacher, &course, &student_id).await; | |
| 3407 | + let now = Timestamp::now().as_millis(); | |
| 3408 | + | |
| 3409 | + // Unscheduled exams cannot be sat at all. | |
| 3410 | + let unscheduled = create_exam(&app, &teacher, &course, "hw", "homework", 1).await; | |
| 3411 | + let res = send( | |
| 3412 | + &app, | |
| 3413 | + "POST", | |
| 3414 | + &format!("/exams/{unscheduled}/attempt"), | |
| 3415 | + Some(&student), | |
| 3416 | + None, | |
| 3417 | + ) | |
| 3418 | + .await; | |
| 3419 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 3420 | + | |
| 3421 | + // Not-yet-open and already-closed windows are conflicts too. | |
| 3422 | + let future = scheduled_exam( | |
| 3423 | + &app, | |
| 3424 | + &teacher, | |
| 3425 | + &course, | |
| 3426 | + json!({ "title": "later", "kind": "quiz", "weight": 1, | |
| 3427 | + "mode": "sync", "starts_at": now + 600_000, "ends_at": now + 1_200_000 }), | |
| 3428 | + ) | |
| 3429 | + .await; | |
| 3430 | + let res = send( | |
| 3431 | + &app, | |
| 3432 | + "POST", | |
| 3433 | + &format!("/exams/{future}/attempt"), | |
| 3434 | + Some(&student), | |
| 3435 | + None, | |
| 3436 | + ) | |
| 3437 | + .await; | |
| 3438 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 3439 | + | |
| 3440 | + let past = scheduled_exam( | |
| 3441 | + &app, | |
| 3442 | + &teacher, | |
| 3443 | + &course, | |
| 3444 | + json!({ "title": "gone", "kind": "quiz", "weight": 1, | |
| 3445 | + "mode": "sync", "starts_at": now - 120_000, "ends_at": now - 60_000 }), | |
| 3446 | + ) | |
| 3447 | + .await; | |
| 3448 | + let res = send( | |
| 3449 | + &app, | |
| 3450 | + "POST", | |
| 3451 | + &format!("/exams/{past}/attempt"), | |
| 3452 | + Some(&student), | |
| 3453 | + None, | |
| 3454 | + ) | |
| 3455 | + .await; | |
| 3456 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 3457 | + | |
| 3458 | + // Enrollment is required — teachers included (the monitor is their view). | |
| 3459 | + let open = scheduled_exam( | |
| 3460 | + &app, | |
| 3461 | + &teacher, | |
| 3462 | + &course, | |
| 3463 | + json!({ "title": "open", "kind": "quiz", "weight": 1, | |
| 3464 | + "mode": "sync", "starts_at": now - 1_000, "ends_at": now + 600_000 }), | |
| 3465 | + ) | |
| 3466 | + .await; | |
| 3467 | + let outsider = login(&app, "omer").await; | |
| 3468 | + let res = send( | |
| 3469 | + &app, | |
| 3470 | + "POST", | |
| 3471 | + &format!("/exams/{open}/attempt"), | |
| 3472 | + Some(&outsider), | |
| 3473 | + None, | |
| 3474 | + ) | |
| 3475 | + .await; | |
| 3476 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | |
| 3477 | + let res = send( | |
| 3478 | + &app, | |
| 3479 | + "POST", | |
| 3480 | + &format!("/exams/{open}/attempt"), | |
| 3481 | + Some(&teacher), | |
| 3482 | + None, | |
| 3483 | + ) | |
| 3484 | + .await; | |
| 3485 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | |
| 3486 | + | |
| 3487 | + // A missing exam is a 404, not a conflict. | |
| 3488 | + let res = send( | |
| 3489 | + &app, | |
| 3490 | + "POST", | |
| 3491 | + "/exams/does-not-exist/attempt", | |
| 3492 | + Some(&student), | |
| 3493 | + None, | |
| 3494 | + ) | |
| 3495 | + .await; | |
| 3496 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | |
| 3497 | +} | |
| 3498 | + | |
| 3499 | +#[tokio::test] | |
| 3500 | +async fn finish_rejects_double_submit_missing_attempt_and_expiry() { | |
| 3501 | + let (app, db) = app_and_db().await; | |
| 3502 | + let teacher = login_as(&app, &db, "fin_t", "teacher").await; | |
| 3503 | + let student = login(&app, "fern").await; | |
| 3504 | + let student_id = me_id(&app, &student).await; | |
| 3505 | + let course = create_course(&app, &teacher, "cs").await; | |
| 3506 | + enroll(&app, &teacher, &course, &student_id).await; | |
| 3507 | + let now = Timestamp::now().as_millis(); | |
| 3508 | + | |
| 3509 | + let exam = scheduled_exam( | |
| 3510 | + &app, | |
| 3511 | + &teacher, | |
| 3512 | + &course, | |
| 3513 | + json!({ "title": "final", "kind": "final", "weight": 1, | |
| 3514 | + "mode": "sync", "starts_at": now - 1_000, "ends_at": now + 600_000 }), | |
| 3515 | + ) | |
| 3516 | + .await; | |
| 3517 | + | |
| 3518 | + // Finishing before starting: no attempt row to close. | |
| 3519 | + let res = send( | |
| 3520 | + &app, | |
| 3521 | + "POST", | |
| 3522 | + &format!("/exams/{exam}/attempt/finish"), | |
| 3523 | + Some(&student), | |
| 3524 | + None, | |
| 3525 | + ) | |
| 3526 | + .await; | |
| 3527 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | |
| 3528 | + | |
| 3529 | + let res = send( | |
| 3530 | + &app, | |
| 3531 | + "POST", | |
| 3532 | + &format!("/exams/{exam}/attempt"), | |
| 3533 | + Some(&student), | |
| 3534 | + None, | |
| 3535 | + ) | |
| 3536 | + .await; | |
| 3537 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 3538 | + let res = send( | |
| 3539 | + &app, | |
| 3540 | + "POST", | |
| 3541 | + &format!("/exams/{exam}/attempt/finish"), | |
| 3542 | + Some(&student), | |
| 3543 | + None, | |
| 3544 | + ) | |
| 3545 | + .await; | |
| 3546 | + assert_eq!(res.status, StatusCode::OK); | |
| 3547 | + let res = send( | |
| 3548 | + &app, | |
| 3549 | + "POST", | |
| 3550 | + &format!("/exams/{exam}/attempt/finish"), | |
| 3551 | + Some(&student), | |
| 3552 | + None, | |
| 3553 | + ) | |
| 3554 | + .await; | |
| 3555 | + assert_eq!(res.status, StatusCode::CONFLICT, "double submit"); | |
| 3556 | + | |
| 3557 | + // Expiry: a window that closes right after the start. Real time, not a | |
| 3558 | + // mocked clock — the server judges expiry by `Timestamp::now()`. | |
| 3559 | + let brief = scheduled_exam( | |
| 3560 | + &app, | |
| 3561 | + &teacher, | |
| 3562 | + &course, | |
| 3563 | + json!({ "title": "blitz", "kind": "quiz", "weight": 1, | |
| 3564 | + "mode": "sync", "starts_at": now - 1_000, | |
| 3565 | + "ends_at": Timestamp::now().as_millis() + 1_500 }), | |
| 3566 | + ) | |
| 3567 | + .await; | |
| 3568 | + let res = send( | |
| 3569 | + &app, | |
| 3570 | + "POST", | |
| 3571 | + &format!("/exams/{brief}/attempt"), | |
| 3572 | + Some(&student), | |
| 3573 | + None, | |
| 3574 | + ) | |
| 3575 | + .await; | |
| 3576 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 3577 | + assert_eq!(res.body["status"], "in_progress"); | |
| 3578 | + | |
| 3579 | + tokio::time::sleep(std::time::Duration::from_millis(1_700)).await; | |
| 3580 | + | |
| 3581 | + let res = send( | |
| 3582 | + &app, | |
| 3583 | + "POST", | |
| 3584 | + &format!("/exams/{brief}/attempt/finish"), | |
| 3585 | + Some(&student), | |
| 3586 | + None, | |
| 3587 | + ) | |
| 3588 | + .await; | |
| 3589 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 3590 | + let res = send( | |
| 3591 | + &app, | |
| 3592 | + "GET", | |
| 3593 | + &format!("/exams/{brief}/attempt"), | |
| 3594 | + Some(&student), | |
| 3595 | + None, | |
| 3596 | + ) | |
| 3597 | + .await; | |
| 3598 | + assert_eq!(res.body["status"], "expired"); | |
| 3599 | + assert!(res.body["finished_at"].is_null()); | |
| 3600 | + assert!(res.body["remaining_ms"].is_null()); | |
| 3601 | +} | |
| 3602 | + | |
| 3603 | +#[tokio::test] | |
| 3604 | +async fn mode_freezes_after_attempts_but_times_extend_live() { | |
| 3605 | + let (app, db) = app_and_db().await; | |
| 3606 | + let teacher = login_as(&app, &db, "ext_t", "teacher").await; | |
| 3607 | + let student = login(&app, "elif").await; | |
| 3608 | + let student_id = me_id(&app, &student).await; | |
| 3609 | + let course = create_course(&app, &teacher, "math").await; | |
| 3610 | + enroll(&app, &teacher, &course, &student_id).await; | |
| 3611 | + let now = Timestamp::now().as_millis(); | |
| 3612 | + let ends = now + 60_000; | |
| 3613 | + | |
| 3614 | + let exam = scheduled_exam( | |
| 3615 | + &app, | |
| 3616 | + &teacher, | |
| 3617 | + &course, | |
| 3618 | + json!({ "title": "final", "kind": "final", "weight": 1, | |
| 3619 | + "mode": "sync", "starts_at": now - 1_000, "ends_at": ends }), | |
| 3620 | + ) | |
| 3621 | + .await; | |
| 3622 | + | |
| 3623 | + // Before any attempt the whole schedule is editable — even the mode... | |
| 3624 | + let res = send( | |
| 3625 | + &app, | |
| 3626 | + "PATCH", | |
| 3627 | + &format!("/exams/{exam}"), | |
| 3628 | + Some(&teacher), | |
| 3629 | + Some(json!({ "mode": "async", "duration_ms": 3_600_000 })), | |
| 3630 | + ) | |
| 3631 | + .await; | |
| 3632 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 3633 | + assert_eq!(res.body["mode"], "async"); | |
| 3634 | + | |
| 3635 | + // ...but a PATCH may not leave a half-schedule behind. | |
| 3636 | + let res = send( | |
| 3637 | + &app, | |
| 3638 | + "PATCH", | |
| 3639 | + &format!("/exams/{exam}"), | |
| 3640 | + Some(&teacher), | |
| 3641 | + Some(json!({ "mode": null })), | |
| 3642 | + ) | |
| 3643 | + .await; | |
| 3644 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | |
| 3645 | + | |
| 3646 | + // Clearing everything unschedules; restoring re-schedules. | |
| 3647 | + let res = send( | |
| 3648 | + &app, | |
| 3649 | + "PATCH", | |
| 3650 | + &format!("/exams/{exam}"), | |
| 3651 | + Some(&teacher), | |
| 3652 | + Some(json!({ "mode": null, "starts_at": null, "ends_at": null, "duration_ms": null })), | |
| 3653 | + ) | |
| 3654 | + .await; | |
| 3655 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 3656 | + assert!(res.body["mode"].is_null()); | |
| 3657 | + let res = send( | |
| 3658 | + &app, | |
| 3659 | + "PATCH", | |
| 3660 | + &format!("/exams/{exam}"), | |
| 3661 | + Some(&teacher), | |
| 3662 | + Some(json!({ "mode": "sync", "starts_at": now - 1_000, "ends_at": ends })), | |
| 3663 | + ) | |
| 3664 | + .await; | |
| 3665 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 3666 | + | |
| 3667 | + // A student sits down; the mode is now frozen (unscheduling included). | |
| 3668 | + let res = send( | |
| 3669 | + &app, | |
| 3670 | + "POST", | |
| 3671 | + &format!("/exams/{exam}/attempt"), | |
| 3672 | + Some(&student), | |
| 3673 | + None, | |
| 3674 | + ) | |
| 3675 | + .await; | |
| 3676 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 3677 | + let res = send( | |
| 3678 | + &app, | |
| 3679 | + "PATCH", | |
| 3680 | + &format!("/exams/{exam}"), | |
| 3681 | + Some(&teacher), | |
| 3682 | + Some(json!({ "mode": "async", "duration_ms": 3_600_000 })), | |
| 3683 | + ) | |
| 3684 | + .await; | |
| 3685 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 3686 | + let res = send( | |
| 3687 | + &app, | |
| 3688 | + "PATCH", | |
| 3689 | + &format!("/exams/{exam}"), | |
| 3690 | + Some(&teacher), | |
| 3691 | + Some(json!({ "mode": null, "starts_at": null, "ends_at": null })), | |
| 3692 | + ) | |
| 3693 | + .await; | |
| 3694 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 3695 | + | |
| 3696 | + // ...but extending the window moves the running deadline immediately. | |
| 3697 | + let extended = ends + 300_000; | |
| 3698 | + let res = send( | |
| 3699 | + &app, | |
| 3700 | + "PATCH", | |
| 3701 | + &format!("/exams/{exam}"), | |
| 3702 | + Some(&teacher), | |
| 3703 | + Some(json!({ "ends_at": extended })), | |
| 3704 | + ) | |
| 3705 | + .await; | |
| 3706 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 3707 | + let res = send( | |
| 3708 | + &app, | |
| 3709 | + "GET", | |
| 3710 | + &format!("/exams/{exam}/attempt"), | |
| 3711 | + Some(&student), | |
| 3712 | + None, | |
| 3713 | + ) | |
| 3714 | + .await; | |
| 3715 | + assert_eq!(res.body["status"], "in_progress"); | |
| 3716 | + assert_eq!(res.body["deadline"].as_i64(), Some(extended)); | |
| 3717 | +} | |
| 3718 | + | |
| 3719 | +#[tokio::test] | |
| 3720 | +async fn attempts_cascade_with_exam_and_course_deletion() { | |
| 3721 | + let (app, db) = app_and_db().await; | |
| 3722 | + let teacher = login_as(&app, &db, "casc_t", "teacher").await; | |
| 3723 | + let student = login(&app, "cansu").await; | |
| 3724 | + let student_id = me_id(&app, &student).await; | |
| 3725 | + let course = create_course(&app, &teacher, "hist").await; | |
| 3726 | + enroll(&app, &teacher, &course, &student_id).await; | |
| 3727 | + let now = Timestamp::now().as_millis(); | |
| 3728 | + let schedule = json!({ "title": "final", "kind": "final", "weight": 1, | |
| 3729 | + "mode": "sync", "starts_at": now - 1_000, "ends_at": now + 600_000 }); | |
| 3730 | + | |
| 3731 | + // Deleting the exam removes its attempts. | |
| 3732 | + let exam = scheduled_exam(&app, &teacher, &course, schedule.clone()).await; | |
| 3733 | + let res = send( | |
| 3734 | + &app, | |
| 3735 | + "POST", | |
| 3736 | + &format!("/exams/{exam}/attempt"), | |
| 3737 | + Some(&student), | |
| 3738 | + None, | |
| 3739 | + ) | |
| 3740 | + .await; | |
| 3741 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 3742 | + assert!( | |
| 3743 | + ExamAttempt::any_for_exam(&ExamId::from_key(&exam), &db) | |
| 3744 | + .await | |
| 3745 | + .unwrap() | |
| 3746 | + ); | |
| 3747 | + let res = send( | |
| 3748 | + &app, | |
| 3749 | + "DELETE", | |
| 3750 | + &format!("/exams/{exam}"), | |
| 3751 | + Some(&teacher), | |
| 3752 | + None, | |
| 3753 | + ) | |
| 3754 | + .await; | |
| 3755 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | |
| 3756 | + assert!( | |
| 3757 | + !ExamAttempt::any_for_exam(&ExamId::from_key(&exam), &db) | |
| 3758 | + .await | |
| 3759 | + .unwrap() | |
| 3760 | + ); | |
| 3761 | + | |
| 3762 | + // Deleting the whole course cascades through its exams' attempts too. | |
| 3763 | + let exam = scheduled_exam(&app, &teacher, &course, schedule).await; | |
| 3764 | + let res = send( | |
| 3765 | + &app, | |
| 3766 | + "POST", | |
| 3767 | + &format!("/exams/{exam}/attempt"), | |
| 3768 | + Some(&student), | |
| 3769 | + None, | |
| 3770 | + ) | |
| 3771 | + .await; | |
| 3772 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 3773 | + let res = send( | |
| 3774 | + &app, | |
| 3775 | + "DELETE", | |
| 3776 | + &format!("/courses/{course}"), | |
| 3777 | + Some(&teacher), | |
| 3778 | + None, | |
| 3779 | + ) | |
| 3780 | + .await; | |
| 3781 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | |
| 3782 | + assert!( | |
| 3783 | + !ExamAttempt::any_for_exam(&ExamId::from_key(&exam), &db) | |
| 3784 | + .await | |
| 3785 | + .unwrap() | |
| 3786 | + ); | |
| 3787 | +} | |
| 3788 | + | |
| 3789 | +#[tokio::test] | |
| 3790 | +async fn live_stream_is_sse_and_teacher_scoped() { | |
| 3791 | + let (app, db) = app_and_db().await; | |
| 3792 | + let teacher = login_as(&app, &db, "sse_t", "teacher").await; | |
| 3793 | + let student = login(&app, "selin").await; | |
| 3794 | + let course = create_course(&app, &teacher, "geo").await; | |
| 3795 | + let now = Timestamp::now().as_millis(); | |
| 3796 | + let exam = scheduled_exam( | |
| 3797 | + &app, | |
| 3798 | + &teacher, | |
| 3799 | + &course, | |
| 3800 | + json!({ "title": "final", "kind": "final", "weight": 1, | |
| 3801 | + "mode": "sync", "starts_at": now - 1_000, "ends_at": now + 600_000 }), | |
| 3802 | + ) | |
| 3803 | + .await; | |
| 3804 | + | |
| 3805 | + // Teacher: 200 with SSE headers. The body is an endless stream, so only | |
| 3806 | + // the head of the response is inspected (common::send would hang). | |
| 3807 | + let request = Request::builder() | |
| 3808 | + .method("GET") | |
| 3809 | + .uri(format!("/exams/{exam}/live/stream")) | |
| 3810 | + .header("cookie", &teacher) | |
| 3811 | + .body(Body::empty()) | |
| 3812 | + .unwrap(); | |
| 3813 | + let response = app.clone().oneshot(request).await.unwrap(); | |
| 3814 | + assert_eq!(response.status(), StatusCode::OK); | |
| 3815 | + let content_type = response | |
| 3816 | + .headers() | |
| 3817 | + .get("content-type") | |
| 3818 | + .expect("content-type") | |
| 3819 | + .to_str() | |
| 3820 | + .unwrap(); | |
| 3821 | + assert!( | |
| 3822 | + content_type.starts_with("text/event-stream"), | |
| 3823 | + "{content_type}" | |
| 3824 | + ); | |
| 3825 | + drop(response); | |
| 3826 | + | |
| 3827 | + // Students can't watch the monitor; a missing exam is a 404 up front. | |
| 3828 | + let res = send( | |
| 3829 | + &app, | |
| 3830 | + "GET", | |
| 3831 | + &format!("/exams/{exam}/live/stream"), | |
| 3832 | + Some(&student), | |
| 3833 | + None, | |
| 3834 | + ) | |
| 3835 | + .await; | |
| 3836 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | |
| 3837 | + let res = send(&app, "GET", "/exams/nope/live/stream", Some(&teacher), None).await; | |
| 3838 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | |
| 3839 | +} | |
| 3840 | + | |
| 3841 | +// --- exam questions + answers (taking an exam) ----------------------------- | |
| 3842 | + | |
| 3843 | +/// Create a question via `POST /exams/{exam}/questions` (asserts 201); | |
| 3844 | +/// returns its id. | |
| 3845 | +async fn create_question( | |
| 3846 | + app: &axum::Router, | |
| 3847 | + cookie: &str, | |
| 3848 | + exam: &str, | |
| 3849 | + body: serde_json::Value, | |
| 3850 | +) -> String { | |
| 3851 | + let res = send( | |
| 3852 | + app, | |
| 3853 | + "POST", | |
| 3854 | + &format!("/exams/{exam}/questions"), | |
| 3855 | + Some(cookie), | |
| 3856 | + Some(body), | |
| 3857 | + ) | |
| 3858 | + .await; | |
| 3859 | + assert_eq!( | |
| 3860 | + res.status, | |
| 3861 | + StatusCode::CREATED, | |
| 3862 | + "create question: {}", | |
| 3863 | + res.body | |
| 3864 | + ); | |
| 3865 | + id_of(&res.body) | |
| 3866 | +} | |
| 3867 | + | |
| 3868 | +/// A course with an open sync window and one enrolled student, the spine of | |
| 3869 | +/// the question/answer tests. Returns (course, exam). | |
| 3870 | +async fn open_exam_with_student( | |
| 3871 | + app: &axum::Router, | |
| 3872 | + teacher: &str, | |
| 3873 | + student_id: &str, | |
| 3874 | + course_title: &str, | |
| 3875 | +) -> (String, String) { | |
| 3876 | + let course = create_course(app, teacher, course_title).await; | |
| 3877 | + enroll(app, teacher, &course, student_id).await; | |
| 3878 | + let now = Timestamp::now().as_millis(); | |
| 3879 | + let exam = scheduled_exam( | |
| 3880 | + app, | |
| 3881 | + teacher, | |
| 3882 | + &course, | |
| 3883 | + json!({ "title": "final", "kind": "final", "weight": 1, | |
| 3884 | + "mode": "sync", "starts_at": now - 1_000, "ends_at": now + 600_000 }), | |
| 3885 | + ) | |
| 3886 | + .await; | |
| 3887 | + (course, exam) | |
| 3888 | +} | |
| 3889 | + | |
| 3890 | +#[tokio::test] | |
| 3891 | +async fn question_crud_validation_and_rbac() { | |
| 3892 | + let (app, db) = app_and_db().await; | |
| 3893 | + let teacher = login_as(&app, &db, "q_t", "teacher").await; | |
| 3894 | + let other_teacher = login_as(&app, &db, "q_t2", "teacher").await; | |
| 3895 | + let student = login(&app, "quinn").await; | |
| 3896 | + let course = create_course(&app, &teacher, "logic").await; | |
| 3897 | + let exam = create_exam(&app, &teacher, &course, "final", "final", 1).await; | |
| 3898 | + | |
| 3899 | + // A choice question echoes its full authoring view, correct included. | |
| 3900 | + let res = send( | |
| 3901 | + &app, | |
| 3902 | + "POST", | |
| 3903 | + &format!("/exams/{exam}/questions"), | |
| 3904 | + Some(&teacher), | |
| 3905 | + Some(json!({ "text": "2 + 2?", "kind": "choice", "points": 10, | |
| 3906 | + "choices": ["3", "4", "5"], "correct": 1 })), | |
| 3907 | + ) | |
| 3908 | + .await; | |
| 3909 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 3910 | + assert_eq!(res.body["exam"], exam); | |
| 3911 | + assert_eq!(res.body["kind"], "choice"); | |
| 3912 | + assert_eq!(res.body["points"], 10); | |
| 3913 | + assert_eq!(res.body["choices"][1], "4"); | |
| 3914 | + assert_eq!(res.body["correct"], 1); | |
| 3915 | + let choice_q = id_of(&res.body); | |
| 3916 | + | |
| 3917 | + // A text question carries neither choices nor correct. | |
| 3918 | + let res = send( | |
| 3919 | + &app, | |
| 3920 | + "POST", | |
| 3921 | + &format!("/exams/{exam}/questions"), | |
| 3922 | + Some(&teacher), | |
| 3923 | + Some(json!({ "text": "Explain.", "kind": "text", "points": 20 })), | |
| 3924 | + ) | |
| 3925 | + .await; | |
| 3926 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 3927 | + assert!(res.body["choices"].is_null()); | |
| 3928 | + assert!(res.body["correct"].is_null()); | |
| 3929 | + let text_q = id_of(&res.body); | |
| 3930 | + | |
| 3931 | + // The teacher list shows both, in creation order. | |
| 3932 | + let res = send( | |
| 3933 | + &app, | |
| 3934 | + "GET", | |
| 3935 | + &format!("/exams/{exam}/questions"), | |
| 3936 | + Some(&teacher), | |
| 3937 | + None, | |
| 3938 | + ) | |
| 3939 | + .await; | |
| 3940 | + assert_eq!(res.status, StatusCode::OK); | |
| 3941 | + let questions = res.body.as_array().expect("questions"); | |
| 3942 | + assert_eq!(questions.len(), 2); | |
| 3943 | + assert_eq!(id_of(&questions[0]), choice_q); | |
| 3944 | + assert_eq!(id_of(&questions[1]), text_q); | |
| 3945 | + | |
| 3946 | + // Kind-dependent fields are validated as a unit. | |
| 3947 | + for (label, body) in [ | |
| 3948 | + ( | |
| 3949 | + "unknown kind", | |
| 3950 | + json!({ "text": "x", "kind": "essay", "points": 1 }), | |
| 3951 | + ), | |
| 3952 | + ( | |
| 3953 | + "zero points", | |
| 3954 | + json!({ "text": "x", "kind": "text", "points": 0 }), | |
| 3955 | + ), | |
| 3956 | + ( | |
| 3957 | + "oversized points", | |
| 3958 | + json!({ "text": "x", "kind": "text", "points": 101 }), | |
| 3959 | + ), | |
| 3960 | + ( | |
| 3961 | + "blank text", | |
| 3962 | + json!({ "text": " ", "kind": "text", "points": 1 }), | |
| 3963 | + ), | |
| 3964 | + ( | |
| 3965 | + "choice without choices", | |
| 3966 | + json!({ "text": "x", "kind": "choice", "points": 1, "correct": 0 }), | |
| 3967 | + ), | |
| 3968 | + ( | |
| 3969 | + "choice without correct", | |
| 3970 | + json!({ "text": "x", "kind": "choice", "points": 1, "choices": ["a", "b"] }), | |
| 3971 | + ), | |
| 3972 | + ( | |
| 3973 | + "correct out of range", | |
| 3974 | + json!({ "text": "x", "kind": "choice", "points": 1, "choices": ["a", "b"], "correct": 2 }), | |
| 3975 | + ), | |
| 3976 | + ( | |
| 3977 | + "negative correct", | |
| 3978 | + json!({ "text": "x", "kind": "choice", "points": 1, "choices": ["a", "b"], "correct": -1 }), | |
| 3979 | + ), | |
| 3980 | + ( | |
| 3981 | + "one choice", | |
| 3982 | + json!({ "text": "x", "kind": "choice", "points": 1, "choices": ["a"], "correct": 0 }), | |
| 3983 | + ), | |
| 3984 | + ( | |
| 3985 | + "eleven choices", | |
| 3986 | + json!({ "text": "x", "kind": "choice", "points": 1, | |
| 3987 | + "choices": ["a","b","c","d","e","f","g","h","i","j","k"], "correct": 0 }), | |
| 3988 | + ), | |
| 3989 | + ( | |
| 3990 | + "blank choice", | |
| 3991 | + json!({ "text": "x", "kind": "choice", "points": 1, "choices": ["a", " "], "correct": 0 }), | |
| 3992 | + ), | |
| 3993 | + ( | |
| 3994 | + "text with choices", | |
| 3995 | + json!({ "text": "x", "kind": "text", "points": 1, "choices": ["a", "b"] }), | |
| 3996 | + ), | |
| 3997 | + ( | |
| 3998 | + "text with correct", | |
| 3999 | + json!({ "text": "x", "kind": "text", "points": 1, "correct": 0 }), | |
| 4000 | + ), | |
| 4001 | + ] { | |
| 4002 | + let res = send( | |
| 4003 | + &app, | |
| 4004 | + "POST", | |
| 4005 | + &format!("/exams/{exam}/questions"), | |
| 4006 | + Some(&teacher), | |
| 4007 | + Some(body), | |
| 4008 | + ) | |
| 4009 | + .await; | |
| 4010 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{label}: {}", res.body); | |
| 4011 | + } | |
| 4012 | + | |
| 4013 | + // Authoring needs management rights over the course; reading needs teacher+. | |
| 4014 | + let question_body = json!({ "text": "x", "kind": "text", "points": 1 }); | |
| 4015 | + let res = send( | |
| 4016 | + &app, | |
| 4017 | + "POST", | |
| 4018 | + &format!("/exams/{exam}/questions"), | |
| 4019 | + Some(&student), | |
| 4020 | + Some(question_body.clone()), | |
| 4021 | + ) | |
| 4022 | + .await; | |
| 4023 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | |
| 4024 | + let res = send( | |
| 4025 | + &app, | |
| 4026 | + "POST", | |
| 4027 | + &format!("/exams/{exam}/questions"), | |
| 4028 | + Some(&other_teacher), | |
| 4029 | + Some(question_body), | |
| 4030 | + ) | |
| 4031 | + .await; | |
| 4032 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "not the course creator"); | |
| 4033 | + let res = send( | |
| 4034 | + &app, | |
| 4035 | + "GET", | |
| 4036 | + &format!("/exams/{exam}/questions"), | |
| 4037 | + Some(&student), | |
| 4038 | + None, | |
| 4039 | + ) | |
| 4040 | + .await; | |
| 4041 | + assert_eq!( | |
| 4042 | + res.status, | |
| 4043 | + StatusCode::FORBIDDEN, | |
| 4044 | + "correct stays teacher-side" | |
| 4045 | + ); | |
| 4046 | + | |
| 4047 | + // PATCH: keep-by-omission, and the kind bundle re-validates as a unit. | |
| 4048 | + let res = send( | |
| 4049 | + &app, | |
| 4050 | + "PATCH", | |
| 4051 | + &format!("/exams/{exam}/questions/{choice_q}"), | |
| 4052 | + Some(&teacher), | |
| 4053 | + Some(json!({ "points": 15 })), | |
| 4054 | + ) | |
| 4055 | + .await; | |
| 4056 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4057 | + assert_eq!(res.body["points"], 15); | |
| 4058 | + assert_eq!(res.body["correct"], 1, "kind bundle kept"); | |
| 4059 | + | |
| 4060 | + // Switching to text must clear the bundle explicitly... | |
| 4061 | + let res = send( | |
| 4062 | + &app, | |
| 4063 | + "PATCH", | |
| 4064 | + &format!("/exams/{exam}/questions/{choice_q}"), | |
| 4065 | + Some(&teacher), | |
| 4066 | + Some(json!({ "kind": "text" })), | |
| 4067 | + ) | |
| 4068 | + .await; | |
| 4069 | + assert_eq!( | |
| 4070 | + res.status, | |
| 4071 | + StatusCode::BAD_REQUEST, | |
| 4072 | + "half-switch: {}", | |
| 4073 | + res.body | |
| 4074 | + ); | |
| 4075 | + let res = send( | |
| 4076 | + &app, | |
| 4077 | + "PATCH", | |
| 4078 | + &format!("/exams/{exam}/questions/{choice_q}"), | |
| 4079 | + Some(&teacher), | |
| 4080 | + Some(json!({ "kind": "text", "choices": null, "correct": null })), | |
| 4081 | + ) | |
| 4082 | + .await; | |
| 4083 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4084 | + assert!(res.body["choices"].is_null()); | |
| 4085 | + | |
| 4086 | + // ...and back, bringing the bundle along. | |
| 4087 | + let res = send( | |
| 4088 | + &app, | |
| 4089 | + "PATCH", | |
| 4090 | + &format!("/exams/{exam}/questions/{choice_q}"), | |
| 4091 | + Some(&teacher), | |
| 4092 | + Some(json!({ "kind": "choice", "choices": ["yes", "no"], "correct": 0 })), | |
| 4093 | + ) | |
| 4094 | + .await; | |
| 4095 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4096 | + | |
| 4097 | + // Editing is management-gated too. | |
| 4098 | + let res = send( | |
| 4099 | + &app, | |
| 4100 | + "PATCH", | |
| 4101 | + &format!("/exams/{exam}/questions/{choice_q}"), | |
| 4102 | + Some(&other_teacher), | |
| 4103 | + Some(json!({ "points": 1 })), | |
| 4104 | + ) | |
| 4105 | + .await; | |
| 4106 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | |
| 4107 | + | |
| 4108 | + // Unknown ids are 404s — including a question under the wrong exam. | |
| 4109 | + let other_exam = create_exam(&app, &teacher, &course, "quiz", "quiz", 1).await; | |
| 4110 | + for (method, uri) in [ | |
| 4111 | + ("POST", "/exams/missing/questions".to_string()), | |
| 4112 | + ("GET", "/exams/missing/questions".to_string()), | |
| 4113 | + ("PATCH", format!("/exams/{exam}/questions/missing")), | |
| 4114 | + ("DELETE", format!("/exams/{exam}/questions/missing")), | |
| 4115 | + ("PATCH", format!("/exams/{other_exam}/questions/{choice_q}")), | |
| 4116 | + ( | |
| 4117 | + "DELETE", | |
| 4118 | + format!("/exams/{other_exam}/questions/{choice_q}"), | |
| 4119 | + ), | |
| 4120 | + ] { | |
| 4121 | + let body = match method { | |
| 4122 | + "POST" => Some(json!({ "text": "x", "kind": "text", "points": 1 })), | |
| 4123 | + "PATCH" => Some(json!({ "points": 2 })), | |
| 4124 | + _ => None, | |
| 4125 | + }; | |
| 4126 | + let res = send(&app, method, &uri, Some(&teacher), body).await; | |
| 4127 | + assert_eq!(res.status, StatusCode::NOT_FOUND, "{method} {uri}"); | |
| 4128 | + } | |
| 4129 | + | |
| 4130 | + // Delete removes from the list; deleting is management-gated. | |
| 4131 | + let res = send( | |
| 4132 | + &app, | |
| 4133 | + "DELETE", | |
| 4134 | + &format!("/exams/{exam}/questions/{text_q}"), | |
| 4135 | + Some(&student), | |
| 4136 | + None, | |
| 4137 | + ) | |
| 4138 | + .await; | |
| 4139 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | |
| 4140 | + let res = send( | |
| 4141 | + &app, | |
| 4142 | + "DELETE", | |
| 4143 | + &format!("/exams/{exam}/questions/{text_q}"), | |
| 4144 | + Some(&teacher), | |
| 4145 | + None, | |
| 4146 | + ) | |
| 4147 | + .await; | |
| 4148 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | |
| 4149 | + let res = send( | |
| 4150 | + &app, | |
| 4151 | + "GET", | |
| 4152 | + &format!("/exams/{exam}/questions"), | |
| 4153 | + Some(&teacher), | |
| 4154 | + None, | |
| 4155 | + ) | |
| 4156 | + .await; | |
| 4157 | + assert_eq!(res.body.as_array().unwrap().len(), 1); | |
| 4158 | +} | |
| 4159 | + | |
| 4160 | +#[tokio::test] | |
| 4161 | +async fn questions_freeze_once_attempts_start() { | |
| 4162 | + let (app, db) = app_and_db().await; | |
| 4163 | + let teacher = login_as(&app, &db, "frz_t", "teacher").await; | |
| 4164 | + let student = login(&app, "firat").await; | |
| 4165 | + let student_id = me_id(&app, &student).await; | |
| 4166 | + let (_course, exam) = open_exam_with_student(&app, &teacher, &student_id, "algo").await; | |
| 4167 | + let question = create_question( | |
| 4168 | + &app, | |
| 4169 | + &teacher, | |
| 4170 | + &exam, | |
| 4171 | + json!({ "text": "2 + 2?", "kind": "choice", "points": 10, | |
| 4172 | + "choices": ["3", "4"], "correct": 1 }), | |
| 4173 | + ) | |
| 4174 | + .await; | |
| 4175 | + | |
| 4176 | + let res = send( | |
| 4177 | + &app, | |
| 4178 | + "POST", | |
| 4179 | + &format!("/exams/{exam}/attempt"), | |
| 4180 | + Some(&student), | |
| 4181 | + None, | |
| 4182 | + ) | |
| 4183 | + .await; | |
| 4184 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 4185 | + | |
| 4186 | + // Create, edit, and delete are all conflicts now. | |
| 4187 | + let res = send( | |
| 4188 | + &app, | |
| 4189 | + "POST", | |
| 4190 | + &format!("/exams/{exam}/questions"), | |
| 4191 | + Some(&teacher), | |
| 4192 | + Some(json!({ "text": "late", "kind": "text", "points": 1 })), | |
| 4193 | + ) | |
| 4194 | + .await; | |
| 4195 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 4196 | + let res = send( | |
| 4197 | + &app, | |
| 4198 | + "PATCH", | |
| 4199 | + &format!("/exams/{exam}/questions/{question}"), | |
| 4200 | + Some(&teacher), | |
| 4201 | + Some(json!({ "points": 1 })), | |
| 4202 | + ) | |
| 4203 | + .await; | |
| 4204 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 4205 | + let res = send( | |
| 4206 | + &app, | |
| 4207 | + "DELETE", | |
| 4208 | + &format!("/exams/{exam}/questions/{question}"), | |
| 4209 | + Some(&teacher), | |
| 4210 | + None, | |
| 4211 | + ) | |
| 4212 | + .await; | |
| 4213 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 4214 | +} | |
| 4215 | + | |
| 4216 | +#[tokio::test] | |
| 4217 | +async fn student_question_view_hides_correct_and_embeds_answers() { | |
| 4218 | + let (app, db) = app_and_db().await; | |
| 4219 | + let teacher = login_as(&app, &db, "sv_t", "teacher").await; | |
| 4220 | + let student = login(&app, "sona").await; | |
| 4221 | + let student_id = me_id(&app, &student).await; | |
| 4222 | + let (_course, exam) = open_exam_with_student(&app, &teacher, &student_id, "art").await; | |
| 4223 | + let choice_q = create_question( | |
| 4224 | + &app, | |
| 4225 | + &teacher, | |
| 4226 | + &exam, | |
| 4227 | + json!({ "text": "2 + 2?", "kind": "choice", "points": 10, | |
| 4228 | + "choices": ["3", "4"], "correct": 1 }), | |
| 4229 | + ) | |
| 4230 | + .await; | |
| 4231 | + create_question( | |
| 4232 | + &app, | |
| 4233 | + &teacher, | |
| 4234 | + &exam, | |
| 4235 | + json!({ "text": "Explain.", "kind": "text", "points": 20 }), | |
| 4236 | + ) | |
| 4237 | + .await; | |
| 4238 | + | |
| 4239 | + // No early peek: the student view requires an attempt. | |
| 4240 | + let uri = format!("/exams/{exam}/attempt/questions"); | |
| 4241 | + let res = send(&app, "GET", &uri, Some(&student), None).await; | |
| 4242 | + assert_eq!(res.status, StatusCode::NOT_FOUND, "attempt first"); | |
| 4243 | + let res = send( | |
| 4244 | + &app, | |
| 4245 | + "POST", | |
| 4246 | + &format!("/exams/{exam}/attempt"), | |
| 4247 | + Some(&student), | |
| 4248 | + None, | |
| 4249 | + ) | |
| 4250 | + .await; | |
| 4251 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 4252 | + | |
| 4253 | + // The sitting view: same order, no `correct` key anywhere, answers null. | |
| 4254 | + let res = send(&app, "GET", &uri, Some(&student), None).await; | |
| 4255 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4256 | + let questions = res.body.as_array().expect("questions"); | |
| 4257 | + assert_eq!(questions.len(), 2); | |
| 4258 | + assert_eq!(questions[0]["kind"], "choice"); | |
| 4259 | + assert_eq!(questions[0]["choices"][1], "4"); | |
| 4260 | + for question in questions { | |
| 4261 | + assert!( | |
| 4262 | + question.as_object().unwrap().get("correct").is_none(), | |
| 4263 | + "correct must never reach a student: {question}" | |
| 4264 | + ); | |
| 4265 | + assert!(question["answer"].is_null()); | |
| 4266 | + } | |
| 4267 | + | |
| 4268 | + // A saved answer comes back embedded on the next read. | |
| 4269 | + let res = send( | |
| 4270 | + &app, | |
| 4271 | + "POST", | |
| 4272 | + &format!("/exams/{exam}/attempt/answers"), | |
| 4273 | + Some(&student), | |
| 4274 | + Some(json!({ "question_id": choice_q, "selected": 0 })), | |
| 4275 | + ) | |
| 4276 | + .await; | |
| 4277 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4278 | + assert_eq!(res.body["question"], choice_q); | |
| 4279 | + assert_eq!(res.body["selected"], 0); | |
| 4280 | + let saved_at = res.body["updated_at"].as_i64().expect("updated_at"); | |
| 4281 | + | |
| 4282 | + let res = send(&app, "GET", &uri, Some(&student), None).await; | |
| 4283 | + let questions = res.body.as_array().expect("questions"); | |
| 4284 | + assert_eq!(questions[0]["answer"]["selected"], 0); | |
| 4285 | + assert_eq!(questions[0]["answer"]["updated_at"], saved_at); | |
| 4286 | + assert!(questions[1]["answer"].is_null()); | |
| 4287 | +} | |
| 4288 | + | |
| 4289 | +#[tokio::test] | |
| 4290 | +async fn answer_saves_gate_on_attempt_state_and_kind() { | |
| 4291 | + let (app, db) = app_and_db().await; | |
| 4292 | + let teacher = login_as(&app, &db, "ans_t", "teacher").await; | |
| 4293 | + let student = login(&app, "arda").await; | |
| 4294 | + let student_id = me_id(&app, &student).await; | |
| 4295 | + let (course, exam) = open_exam_with_student(&app, &teacher, &student_id, "phys").await; | |
| 4296 | + let choice_q = create_question( | |
| 4297 | + &app, | |
| 4298 | + &teacher, | |
| 4299 | + &exam, | |
| 4300 | + json!({ "text": "2 + 2?", "kind": "choice", "points": 10, | |
| 4301 | + "choices": ["3", "4", "5"], "correct": 1 }), | |
| 4302 | + ) | |
| 4303 | + .await; | |
| 4304 | + let text_q = create_question( | |
| 4305 | + &app, | |
| 4306 | + &teacher, | |
| 4307 | + &exam, | |
| 4308 | + json!({ "text": "Explain.", "kind": "text", "points": 20 }), | |
| 4309 | + ) | |
| 4310 | + .await; | |
| 4311 | + let answers_uri = format!("/exams/{exam}/attempt/answers"); | |
| 4312 | + | |
| 4313 | + // Saving needs an attempt (404 before start). | |
| 4314 | + let res = send( | |
| 4315 | + &app, | |
| 4316 | + "POST", | |
| 4317 | + &answers_uri, | |
| 4318 | + Some(&student), | |
| 4319 | + Some(json!({ "question_id": choice_q, "selected": 0 })), | |
| 4320 | + ) | |
| 4321 | + .await; | |
| 4322 | + assert_eq!(res.status, StatusCode::NOT_FOUND, "attempt first"); | |
| 4323 | + let res = send( | |
| 4324 | + &app, | |
| 4325 | + "POST", | |
| 4326 | + &format!("/exams/{exam}/attempt"), | |
| 4327 | + Some(&student), | |
| 4328 | + None, | |
| 4329 | + ) | |
| 4330 | + .await; | |
| 4331 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 4332 | + | |
| 4333 | + // Upsert: re-answering overwrites the one row and re-stamps its clock. | |
| 4334 | + let res = send( | |
| 4335 | + &app, | |
| 4336 | + "POST", | |
| 4337 | + &answers_uri, | |
| 4338 | + Some(&student), | |
| 4339 | + Some(json!({ "question_id": choice_q, "selected": 0 })), | |
| 4340 | + ) | |
| 4341 | + .await; | |
| 4342 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4343 | + let first_saved_at = res.body["updated_at"].as_i64().expect("updated_at"); | |
| 4344 | + tokio::time::sleep(std::time::Duration::from_millis(5)).await; | |
| 4345 | + let res = send( | |
| 4346 | + &app, | |
| 4347 | + "POST", | |
| 4348 | + &answers_uri, | |
| 4349 | + Some(&student), | |
| 4350 | + Some(json!({ "question_id": choice_q, "selected": 2 })), | |
| 4351 | + ) | |
| 4352 | + .await; | |
| 4353 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4354 | + assert_eq!(res.body["selected"], 2); | |
| 4355 | + assert!( | |
| 4356 | + res.body["updated_at"].as_i64().expect("updated_at") > first_saved_at, | |
| 4357 | + "a re-save moves updated_at forward" | |
| 4358 | + ); | |
| 4359 | + let res = send( | |
| 4360 | + &app, | |
| 4361 | + "POST", | |
| 4362 | + &answers_uri, | |
| 4363 | + Some(&student), | |
| 4364 | + Some(json!({ "question_id": text_q, "text": "because" })), | |
| 4365 | + ) | |
| 4366 | + .await; | |
| 4367 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4368 | + assert_eq!(res.body["text"], "because"); | |
| 4369 | + | |
| 4370 | + // The attempt view counts distinct questions answered, not saves. | |
| 4371 | + let res = send( | |
| 4372 | + &app, | |
| 4373 | + "GET", | |
| 4374 | + &format!("/exams/{exam}/attempt"), | |
| 4375 | + Some(&student), | |
| 4376 | + None, | |
| 4377 | + ) | |
| 4378 | + .await; | |
| 4379 | + assert_eq!(res.body["answered"], 2); | |
| 4380 | + assert_eq!(res.body["question_count"], 2); | |
| 4381 | + | |
| 4382 | + // Payloads that don't fit the question's kind are 400s. | |
| 4383 | + for (label, body) in [ | |
| 4384 | + ( | |
| 4385 | + "text on a choice question", | |
| 4386 | + json!({ "question_id": choice_q, "text": "4" }), | |
| 4387 | + ), | |
| 4388 | + ( | |
| 4389 | + "both fields", | |
| 4390 | + json!({ "question_id": choice_q, "selected": 1, "text": "4" }), | |
| 4391 | + ), | |
| 4392 | + ( | |
| 4393 | + "selected out of range", | |
| 4394 | + json!({ "question_id": choice_q, "selected": 3 }), | |
| 4395 | + ), | |
| 4396 | + ( | |
| 4397 | + "negative selected", | |
| 4398 | + json!({ "question_id": choice_q, "selected": -1 }), | |
| 4399 | + ), | |
| 4400 | + ("nothing to save", json!({ "question_id": choice_q })), | |
| 4401 | + ( | |
| 4402 | + "selected on a text question", | |
| 4403 | + json!({ "question_id": text_q, "selected": 0 }), | |
| 4404 | + ), | |
| 4405 | + ( | |
| 4406 | + "oversized text", | |
| 4407 | + json!({ "question_id": text_q, "text": "x".repeat(10_001) }), | |
| 4408 | + ), | |
| 4409 | + ] { | |
| 4410 | + let res = send(&app, "POST", &answers_uri, Some(&student), Some(body)).await; | |
| 4411 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{label}: {}", res.body); | |
| 4412 | + } | |
| 4413 | + | |
| 4414 | + // Unknown questions — and questions of *other* exams — are 404s. | |
| 4415 | + let res = send( | |
| 4416 | + &app, | |
| 4417 | + "POST", | |
| 4418 | + &answers_uri, | |
| 4419 | + Some(&student), | |
| 4420 | + Some(json!({ "question_id": "missing", "selected": 0 })), | |
| 4421 | + ) | |
| 4422 | + .await; | |
| 4423 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | |
| 4424 | + let foreign_exam = create_exam(&app, &teacher, &course, "other", "quiz", 1).await; | |
| 4425 | + let foreign_q = create_question( | |
| 4426 | + &app, | |
| 4427 | + &teacher, | |
| 4428 | + &foreign_exam, | |
| 4429 | + json!({ "text": "not yours", "kind": "choice", "points": 1, | |
| 4430 | + "choices": ["a", "b"], "correct": 0 }), | |
| 4431 | + ) | |
| 4432 | + .await; | |
| 4433 | + let res = send( | |
| 4434 | + &app, | |
| 4435 | + "POST", | |
| 4436 | + &answers_uri, | |
| 4437 | + Some(&student), | |
| 4438 | + Some(json!({ "question_id": foreign_q, "selected": 0 })), | |
| 4439 | + ) | |
| 4440 | + .await; | |
| 4441 | + assert_eq!( | |
| 4442 | + res.status, | |
| 4443 | + StatusCode::NOT_FOUND, | |
| 4444 | + "a real question under the wrong exam: {}", | |
| 4445 | + res.body | |
| 4446 | + ); | |
| 4447 | + | |
| 4448 | + // Submitting closes the write path. | |
| 4449 | + let res = send( | |
| 4450 | + &app, | |
| 4451 | + "POST", | |
| 4452 | + &format!("/exams/{exam}/attempt/finish"), | |
| 4453 | + Some(&student), | |
| 4454 | + None, | |
| 4455 | + ) | |
| 4456 | + .await; | |
| 4457 | + assert_eq!(res.status, StatusCode::OK); | |
| 4458 | + let res = send( | |
| 4459 | + &app, | |
| 4460 | + "POST", | |
| 4461 | + &answers_uri, | |
| 4462 | + Some(&student), | |
| 4463 | + Some(json!({ "question_id": choice_q, "selected": 1 })), | |
| 4464 | + ) | |
| 4465 | + .await; | |
| 4466 | + assert_eq!( | |
| 4467 | + res.status, | |
| 4468 | + StatusCode::CONFLICT, | |
| 4469 | + "after submit: {}", | |
| 4470 | + res.body | |
| 4471 | + ); | |
| 4472 | + | |
| 4473 | + // ...but the sitting view stays readable for review, answers intact. | |
| 4474 | + let res = send( | |
| 4475 | + &app, | |
| 4476 | + "GET", | |
| 4477 | + &format!("/exams/{exam}/attempt/questions"), | |
| 4478 | + Some(&student), | |
| 4479 | + None, | |
| 4480 | + ) | |
| 4481 | + .await; | |
| 4482 | + assert_eq!( | |
| 4483 | + res.status, | |
| 4484 | + StatusCode::OK, | |
| 4485 | + "review after submit: {}", | |
| 4486 | + res.body | |
| 4487 | + ); | |
| 4488 | + assert_eq!(res.body[0]["answer"]["selected"], 2); | |
| 4489 | + assert_eq!(res.body[1]["answer"]["text"], "because"); | |
| 4490 | +} | |
| 4491 | + | |
| 4492 | +#[tokio::test] | |
| 4493 | +async fn answer_saves_stop_at_the_deadline() { | |
| 4494 | + let (app, db) = app_and_db().await; | |
| 4495 | + let teacher = login_as(&app, &db, "dl_t", "teacher").await; | |
| 4496 | + let student = login(&app, "dila").await; | |
| 4497 | + let student_id = me_id(&app, &student).await; | |
| 4498 | + let course = create_course(&app, &teacher, "geo").await; | |
| 4499 | + enroll(&app, &teacher, &course, &student_id).await; | |
| 4500 | + let now = Timestamp::now().as_millis(); | |
| 4501 | + | |
| 4502 | + // A window that closes right after the start — real time, the server | |
| 4503 | + // judges expiry by `Timestamp::now()`. | |
| 4504 | + let exam = scheduled_exam( | |
| 4505 | + &app, | |
| 4506 | + &teacher, | |
| 4507 | + &course, | |
| 4508 | + json!({ "title": "blitz", "kind": "quiz", "weight": 1, | |
| 4509 | + "mode": "sync", "starts_at": now - 1_000, "ends_at": now + 1_500 }), | |
| 4510 | + ) | |
| 4511 | + .await; | |
| 4512 | + let question = create_question( | |
| 4513 | + &app, | |
| 4514 | + &teacher, | |
| 4515 | + &exam, | |
| 4516 | + json!({ "text": "quick", "kind": "choice", "points": 1, | |
| 4517 | + "choices": ["a", "b"], "correct": 0 }), | |
| 4518 | + ) | |
| 4519 | + .await; | |
| 4520 | + let res = send( | |
| 4521 | + &app, | |
| 4522 | + "POST", | |
| 4523 | + &format!("/exams/{exam}/attempt"), | |
| 4524 | + Some(&student), | |
| 4525 | + None, | |
| 4526 | + ) | |
| 4527 | + .await; | |
| 4528 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 4529 | + let res = send( | |
| 4530 | + &app, | |
| 4531 | + "POST", | |
| 4532 | + &format!("/exams/{exam}/attempt/answers"), | |
| 4533 | + Some(&student), | |
| 4534 | + Some(json!({ "question_id": question, "selected": 0 })), | |
| 4535 | + ) | |
| 4536 | + .await; | |
| 4537 | + assert_eq!( | |
| 4538 | + res.status, | |
| 4539 | + StatusCode::OK, | |
| 4540 | + "inside the window: {}", | |
| 4541 | + res.body | |
| 4542 | + ); | |
| 4543 | + | |
| 4544 | + tokio::time::sleep(std::time::Duration::from_millis(1_700)).await; | |
| 4545 | + | |
| 4546 | + let res = send( | |
| 4547 | + &app, | |
| 4548 | + "POST", | |
| 4549 | + &format!("/exams/{exam}/attempt/answers"), | |
| 4550 | + Some(&student), | |
| 4551 | + Some(json!({ "question_id": question, "selected": 1 })), | |
| 4552 | + ) | |
| 4553 | + .await; | |
| 4554 | + assert_eq!(res.status, StatusCode::CONFLICT, "expired: {}", res.body); | |
| 4555 | + | |
| 4556 | + // The pre-deadline answer survives untouched for grading. | |
| 4557 | + let res = send( | |
| 4558 | + &app, | |
| 4559 | + "GET", | |
| 4560 | + &format!("/exams/{exam}/attempts/{student_id}/answers"), | |
| 4561 | + Some(&teacher), | |
| 4562 | + None, | |
| 4563 | + ) | |
| 4564 | + .await; | |
| 4565 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4566 | + assert_eq!(res.body["answers"][0]["selected"], 0); | |
| 4567 | +} | |
| 4568 | + | |
| 4569 | +#[tokio::test] | |
| 4570 | +async fn teacher_answer_sheet_judges_choices_and_suggests_a_score() { | |
| 4571 | + let (app, db) = app_and_db().await; | |
| 4572 | + let teacher = login_as(&app, &db, "sc_t", "teacher").await; | |
| 4573 | + let student = login(&app, "sena").await; | |
| 4574 | + let student_id = me_id(&app, &student).await; | |
| 4575 | + let (_course, exam) = open_exam_with_student(&app, &teacher, &student_id, "chem").await; | |
| 4576 | + let q1 = create_question( | |
| 4577 | + &app, | |
| 4578 | + &teacher, | |
| 4579 | + &exam, | |
| 4580 | + json!({ "text": "2 + 2?", "kind": "choice", "points": 10, | |
| 4581 | + "choices": ["3", "4"], "correct": 1 }), | |
| 4582 | + ) | |
| 4583 | + .await; | |
| 4584 | + let q2 = create_question( | |
| 4585 | + &app, | |
| 4586 | + &teacher, | |
| 4587 | + &exam, | |
| 4588 | + json!({ "text": "3 + 3?", "kind": "choice", "points": 20, | |
| 4589 | + "choices": ["6", "7"], "correct": 0 }), | |
| 4590 | + ) | |
| 4591 | + .await; | |
| 4592 | + let q3 = create_question( | |
| 4593 | + &app, | |
| 4594 | + &teacher, | |
| 4595 | + &exam, | |
| 4596 | + json!({ "text": "Explain.", "kind": "text", "points": 30 }), | |
| 4597 | + ) | |
| 4598 | + .await; | |
| 4599 | + let sheet_uri = format!("/exams/{exam}/attempts/{student_id}/answers"); | |
| 4600 | + | |
| 4601 | + // No attempt yet: no sheet to read. | |
| 4602 | + let res = send(&app, "GET", &sheet_uri, Some(&teacher), None).await; | |
| 4603 | + assert_eq!(res.status, StatusCode::NOT_FOUND, "no attempt yet"); | |
| 4604 | + | |
| 4605 | + let res = send( | |
| 4606 | + &app, | |
| 4607 | + "POST", | |
| 4608 | + &format!("/exams/{exam}/attempt"), | |
| 4609 | + Some(&student), | |
| 4610 | + None, | |
| 4611 | + ) | |
| 4612 | + .await; | |
| 4613 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 4614 | + for body in [ | |
| 4615 | + json!({ "question_id": q1, "selected": 1 }), // right | |
| 4616 | + json!({ "question_id": q2, "selected": 1 }), // wrong | |
| 4617 | + json!({ "question_id": q3, "text": "entropy grows" }), | |
| 4618 | + ] { | |
| 4619 | + let res = send( | |
| 4620 | + &app, | |
| 4621 | + "POST", | |
| 4622 | + &format!("/exams/{exam}/attempt/answers"), | |
| 4623 | + Some(&student), | |
| 4624 | + Some(body), | |
| 4625 | + ) | |
| 4626 | + .await; | |
| 4627 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4628 | + } | |
| 4629 | + | |
| 4630 | + // The grader's view: judged rows plus the suggested score. | |
| 4631 | + let res = send(&app, "GET", &sheet_uri, Some(&teacher), None).await; | |
| 4632 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4633 | + assert_eq!(res.body["exam"], exam); | |
| 4634 | + assert_eq!(res.body["user"]["username"], "sena"); | |
| 4635 | + let answers = res.body["answers"].as_array().expect("answers"); | |
| 4636 | + assert_eq!(answers.len(), 3); | |
| 4637 | + assert_eq!(answers[0]["question"], q1); | |
| 4638 | + assert_eq!(answers[0]["is_correct"], true); | |
| 4639 | + assert_eq!(answers[1]["is_correct"], false); | |
| 4640 | + assert_eq!(answers[2]["text"], "entropy grows"); | |
| 4641 | + assert!( | |
| 4642 | + answers[2]["is_correct"].is_null(), | |
| 4643 | + "text is the grader's call" | |
| 4644 | + ); | |
| 4645 | + assert_eq!(res.body["auto_score"]["earned"], 10); | |
| 4646 | + assert_eq!( | |
| 4647 | + res.body["auto_score"]["possible"], 30, | |
| 4648 | + "text points never counted" | |
| 4649 | + ); | |
| 4650 | + | |
| 4651 | + // The sheet is teacher-side only; students keep using their own view. | |
| 4652 | + let res = send(&app, "GET", &sheet_uri, Some(&student), None).await; | |
| 4653 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | |
| 4654 | +} | |
| 4655 | + | |
| 4656 | +#[tokio::test] | |
| 4657 | +async fn live_monitor_tracks_answer_progress() { | |
| 4658 | + let (app, db) = app_and_db().await; | |
| 4659 | + let teacher = login_as(&app, &db, "prg_t", "teacher").await; | |
| 4660 | + let ayla = login(&app, "ayla").await; | |
| 4661 | + let bora = login(&app, "bora").await; | |
| 4662 | + let ayla_id = me_id(&app, &ayla).await; | |
| 4663 | + let bora_id = me_id(&app, &bora).await; | |
| 4664 | + let (course, exam) = open_exam_with_student(&app, &teacher, &ayla_id, "stats").await; | |
| 4665 | + enroll(&app, &teacher, &course, &bora_id).await; | |
| 4666 | + let question = create_question( | |
| 4667 | + &app, | |
| 4668 | + &teacher, | |
| 4669 | + &exam, | |
| 4670 | + json!({ "text": "2 + 2?", "kind": "choice", "points": 10, | |
| 4671 | + "choices": ["3", "4"], "correct": 1 }), | |
| 4672 | + ) | |
| 4673 | + .await; | |
| 4674 | + create_question( | |
| 4675 | + &app, | |
| 4676 | + &teacher, | |
| 4677 | + &exam, | |
| 4678 | + json!({ "text": "Explain.", "kind": "text", "points": 20 }), | |
| 4679 | + ) | |
| 4680 | + .await; | |
| 4681 | + | |
| 4682 | + // Starting reports zero progress out of two questions. | |
| 4683 | + let res = send( | |
| 4684 | + &app, | |
| 4685 | + "POST", | |
| 4686 | + &format!("/exams/{exam}/attempt"), | |
| 4687 | + Some(&ayla), | |
| 4688 | + None, | |
| 4689 | + ) | |
| 4690 | + .await; | |
| 4691 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 4692 | + assert_eq!(res.body["answered"], 0); | |
| 4693 | + assert_eq!(res.body["question_count"], 2); | |
| 4694 | + | |
| 4695 | + let res = send( | |
| 4696 | + &app, | |
| 4697 | + "POST", | |
| 4698 | + &format!("/exams/{exam}/attempt/answers"), | |
| 4699 | + Some(&ayla), | |
| 4700 | + Some(json!({ "question_id": question, "selected": 1 })), | |
| 4701 | + ) | |
| 4702 | + .await; | |
| 4703 | + assert_eq!(res.status, StatusCode::OK); | |
| 4704 | + let saved_at = res.body["updated_at"].as_i64().expect("updated_at"); | |
| 4705 | + | |
| 4706 | + // The monitor: per-student progress and last activity, plus the total. | |
| 4707 | + let res = send( | |
| 4708 | + &app, | |
| 4709 | + "GET", | |
| 4710 | + &format!("/exams/{exam}/live"), | |
| 4711 | + Some(&teacher), | |
| 4712 | + None, | |
| 4713 | + ) | |
| 4714 | + .await; | |
| 4715 | + assert_eq!(res.status, StatusCode::OK); | |
| 4716 | + assert_eq!(res.body["question_count"], 2); | |
| 4717 | + let students = res.body["students"].as_array().expect("students"); | |
| 4718 | + assert_eq!(students[0]["user"]["username"], "ayla"); | |
| 4719 | + assert_eq!(students[0]["answered"], 1); | |
| 4720 | + assert_eq!(students[0]["last_activity"].as_i64(), Some(saved_at)); | |
| 4721 | + assert_eq!(students[1]["user"]["username"], "bora"); | |
| 4722 | + assert_eq!(students[1]["answered"], 0); | |
| 4723 | + assert!(students[1]["last_activity"].is_null()); | |
| 4724 | +} | |
| 4725 | + | |
| 4726 | +#[tokio::test] | |
| 4727 | +async fn questions_and_answers_cascade_with_deletes() { | |
| 4728 | + use hezarfen_backend::domain::exam_answer::ExamAnswer; | |
| 4729 | + use hezarfen_backend::domain::exam_question::ExamQuestion; | |
| 4730 | + | |
| 4731 | + let (app, db) = app_and_db().await; | |
| 4732 | + let teacher = login_as(&app, &db, "qc_t", "teacher").await; | |
| 4733 | + let student = login(&app, "cem").await; | |
| 4734 | + let student_id = me_id(&app, &student).await; | |
| 4735 | + let (course, exam) = open_exam_with_student(&app, &teacher, &student_id, "geo2").await; | |
| 4736 | + let q1 = create_question( | |
| 4737 | + &app, | |
| 4738 | + &teacher, | |
| 4739 | + &exam, | |
| 4740 | + json!({ "text": "2 + 2?", "kind": "choice", "points": 10, | |
| 4741 | + "choices": ["3", "4"], "correct": 1 }), | |
| 4742 | + ) | |
| 4743 | + .await; | |
| 4744 | + let q2 = create_question( | |
| 4745 | + &app, | |
| 4746 | + &teacher, | |
| 4747 | + &exam, | |
| 4748 | + json!({ "text": "Explain.", "kind": "text", "points": 20 }), | |
| 4749 | + ) | |
| 4750 | + .await; | |
| 4751 | + let res = send( | |
| 4752 | + &app, | |
| 4753 | + "POST", | |
| 4754 | + &format!("/exams/{exam}/attempt"), | |
| 4755 | + Some(&student), | |
| 4756 | + None, | |
| 4757 | + ) | |
| 4758 | + .await; | |
| 4759 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 4760 | + for body in [ | |
| 4761 | + json!({ "question_id": q1, "selected": 1 }), | |
| 4762 | + json!({ "question_id": q2, "text": "so" }), | |
| 4763 | + ] { | |
| 4764 | + let res = send( | |
| 4765 | + &app, | |
| 4766 | + "POST", | |
| 4767 | + &format!("/exams/{exam}/attempt/answers"), | |
| 4768 | + Some(&student), | |
| 4769 | + Some(body), | |
| 4770 | + ) | |
| 4771 | + .await; | |
| 4772 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4773 | + } | |
| 4774 | + let exam_id = ExamId::from_key(&exam); | |
| 4775 | + assert_eq!( | |
| 4776 | + ExamQuestion::list_for_exam(&exam_id, &db) | |
| 4777 | + .await | |
| 4778 | + .unwrap() | |
| 4779 | + .len(), | |
| 4780 | + 2 | |
| 4781 | + ); | |
| 4782 | + assert_eq!( | |
| 4783 | + ExamAnswer::list_for_exam(&exam_id, &db) | |
| 4784 | + .await | |
| 4785 | + .unwrap() | |
| 4786 | + .len(), | |
| 4787 | + 2 | |
| 4788 | + ); | |
| 4789 | + | |
| 4790 | + // Deleting one question takes its answers with it — the API freezes | |
| 4791 | + // question deletes once attempts exist, so exercise the domain cascade | |
| 4792 | + // directly (it also runs under the exam/course cascades below). | |
| 4793 | + let question = ExamQuestion::list_for_exam(&exam_id, &db) | |
| 4794 | + .await | |
| 4795 | + .unwrap() | |
| 4796 | + .into_iter() | |
| 4797 | + .next() | |
| 4798 | + .unwrap(); | |
| 4799 | + question.delete(&db).await.unwrap(); | |
| 4800 | + assert_eq!( | |
| 4801 | + ExamQuestion::list_for_exam(&exam_id, &db) | |
| 4802 | + .await | |
| 4803 | + .unwrap() | |
| 4804 | + .len(), | |
| 4805 | + 1 | |
| 4806 | + ); | |
| 4807 | + assert_eq!( | |
| 4808 | + ExamAnswer::list_for_exam(&exam_id, &db) | |
| 4809 | + .await | |
| 4810 | + .unwrap() | |
| 4811 | + .len(), | |
| 4812 | + 1, | |
| 4813 | + "only the deleted question's answer goes" | |
| 4814 | + ); | |
| 4815 | + | |
| 4816 | + // Deleting the exam clears the rest. | |
| 4817 | + let res = send( | |
| 4818 | + &app, | |
| 4819 | + "DELETE", | |
| 4820 | + &format!("/exams/{exam}"), | |
| 4821 | + Some(&teacher), | |
| 4822 | + None, | |
| 4823 | + ) | |
| 4824 | + .await; | |
| 4825 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | |
| 4826 | + assert!( | |
| 4827 | + ExamQuestion::list_for_exam(&exam_id, &db) | |
| 4828 | + .await | |
| 4829 | + .unwrap() | |
| 4830 | + .is_empty() | |
| 4831 | + ); | |
| 4832 | + assert!( | |
| 4833 | + ExamAnswer::list_for_exam(&exam_id, &db) | |
| 4834 | + .await | |
| 4835 | + .unwrap() | |
| 4836 | + .is_empty() | |
| 4837 | + ); | |
| 4838 | + | |
| 4839 | + // And a course delete cascades through its exams' questions and answers. | |
| 4840 | + let now = Timestamp::now().as_millis(); | |
| 4841 | + let exam2 = scheduled_exam( | |
| 4842 | + &app, | |
| 4843 | + &teacher, | |
| 4844 | + &course, | |
| 4845 | + json!({ "title": "retake", "kind": "quiz", "weight": 1, | |
| 4846 | + "mode": "sync", "starts_at": now - 1_000, "ends_at": now + 600_000 }), | |
| 4847 | + ) | |
| 4848 | + .await; | |
| 4849 | + let q = create_question( | |
| 4850 | + &app, | |
| 4851 | + &teacher, | |
| 4852 | + &exam2, | |
| 4853 | + json!({ "text": "2 + 2?", "kind": "choice", "points": 10, | |
| 4854 | + "choices": ["3", "4"], "correct": 1 }), | |
| 4855 | + ) | |
| 4856 | + .await; | |
| 4857 | + let res = send( | |
| 4858 | + &app, | |
| 4859 | + "POST", | |
| 4860 | + &format!("/exams/{exam2}/attempt"), | |
| 4861 | + Some(&student), | |
| 4862 | + None, | |
| 4863 | + ) | |
| 4864 | + .await; | |
| 4865 | + assert_eq!(res.status, StatusCode::CREATED); | |
| 4866 | + let res = send( | |
| 4867 | + &app, | |
| 4868 | + "POST", | |
| 4869 | + &format!("/exams/{exam2}/attempt/answers"), | |
| 4870 | + Some(&student), | |
| 4871 | + Some(json!({ "question_id": q, "selected": 1 })), | |
| 4872 | + ) | |
| 4873 | + .await; | |
| 4874 | + assert_eq!(res.status, StatusCode::OK); | |
| 4875 | + | |
| 4876 | + let res = send( | |
| 4877 | + &app, | |
| 4878 | + "DELETE", | |
| 4879 | + &format!("/courses/{course}"), | |
| 4880 | + Some(&teacher), | |
| 4881 | + None, | |
| 4882 | + ) | |
| 4883 | + .await; | |
| 4884 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | |
| 4885 | + let exam2_id = ExamId::from_key(&exam2); | |
| 4886 | + assert!( | |
| 4887 | + ExamQuestion::list_for_exam(&exam2_id, &db) | |
| 4888 | + .await | |
| 4889 | + .unwrap() | |
| 4890 | + .is_empty() | |
| 4891 | + ); | |
| 4892 | + assert!( | |
| 4893 | + ExamAnswer::list_for_exam(&exam2_id, &db) | |
| 4894 | + .await | |
| 4895 | + .unwrap() | |
| 4896 | + .is_empty() | |
| 4897 | + ); | |
| 4898 | +} | |
| 4899 | + | |
| 4900 | +#[tokio::test] | |
| 4901 | +async fn question_patch_revalidates_the_stale_kind_bundle() { | |
| 4902 | + let (app, db) = app_and_db().await; | |
| 4903 | + let teacher = login_as(&app, &db, "rv_t", "teacher").await; | |
| 4904 | + let course = create_course(&app, &teacher, "sets").await; | |
| 4905 | + let exam = create_exam(&app, &teacher, &course, "final", "final", 1).await; | |
| 4906 | + let question = create_question( | |
| 4907 | + &app, | |
| 4908 | + &teacher, | |
| 4909 | + &exam, | |
| 4910 | + json!({ "text": "pick", "kind": "choice", "points": 10, | |
| 4911 | + "choices": ["a", "b", "c"], "correct": 2 }), | |
| 4912 | + ) | |
| 4913 | + .await; | |
| 4914 | + let uri = format!("/exams/{exam}/questions/{question}"); | |
| 4915 | + | |
| 4916 | + // Shrinking the options while keeping the old `correct` would leave it | |
| 4917 | + // dangling past the end — the merge must re-validate the bundle. | |
| 4918 | + let res = send( | |
| 4919 | + &app, | |
| 4920 | + "PATCH", | |
| 4921 | + &uri, | |
| 4922 | + Some(&teacher), | |
| 4923 | + Some(json!({ "choices": ["a", "b"] })), | |
| 4924 | + ) | |
| 4925 | + .await; | |
| 4926 | + assert_eq!( | |
| 4927 | + res.status, | |
| 4928 | + StatusCode::BAD_REQUEST, | |
| 4929 | + "kept correct=2 dangles: {}", | |
| 4930 | + res.body | |
| 4931 | + ); | |
| 4932 | + | |
| 4933 | + // Same shrink with `correct` brought along is fine. | |
| 4934 | + let res = send( | |
| 4935 | + &app, | |
| 4936 | + "PATCH", | |
| 4937 | + &uri, | |
| 4938 | + Some(&teacher), | |
| 4939 | + Some(json!({ "choices": ["a", "b"], "correct": 1 })), | |
| 4940 | + ) | |
| 4941 | + .await; | |
| 4942 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 4943 | + assert_eq!(res.body["correct"], 1); | |
| 4944 | + | |
| 4945 | + // A lone out-of-range `correct` against the kept choices is caught too. | |
| 4946 | + let res = send( | |
| 4947 | + &app, | |
| 4948 | + "PATCH", | |
| 4949 | + &uri, | |
| 4950 | + Some(&teacher), | |
| 4951 | + Some(json!({ "correct": 5 })), | |
| 4952 | + ) | |
| 4953 | + .await; | |
| 4954 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | |
| 4955 | + | |
| 4956 | + // Clearing just one half of the bundle can't leave a half-question. | |
| 4957 | + let res = send( | |
| 4958 | + &app, | |
| 4959 | + "PATCH", | |
| 4960 | + &uri, | |
| 4961 | + Some(&teacher), | |
| 4962 | + Some(json!({ "correct": null })), | |
| 4963 | + ) | |
| 4964 | + .await; | |
| 4965 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "{}", res.body); | |
| 4966 | + | |
| 4967 | + // Nothing of the failed PATCHes stuck. | |
| 4968 | + let res = send( | |
| 4969 | + &app, | |
| 4970 | + "GET", | |
| 4971 | + &format!("/exams/{exam}/questions"), | |
| 4972 | + Some(&teacher), | |
| 4973 | + None, | |
| 4974 | + ) | |
| 4975 | + .await; | |
| 4976 | + assert_eq!(res.body[0]["choices"].as_array().unwrap().len(), 2); | |
| 4977 | + assert_eq!(res.body[0]["correct"], 1); | |
| 4978 | +} | |
| 4979 | + | |
| 4980 | +#[tokio::test] | |
| 4981 | +async fn question_authoring_follows_course_management() { | |
| 4982 | + let (app, db) = app_and_db().await; | |
| 4983 | + let teacher = login_as(&app, &db, "own_t", "teacher").await; | |
| 4984 | + let boss = login_as(&app, &db, "own_m", "manager").await; | |
| 4985 | + let course = create_course(&app, &teacher, "greek").await; | |
| 4986 | + let exam = create_exam(&app, &teacher, &course, "final", "final", 1).await; | |
| 4987 | + | |
| 4988 | + // A manager+ authors questions in anyone's course, like every other | |
| 4989 | + // course-management write. | |
| 4990 | + let question = create_question( | |
| 4991 | + &app, | |
| 4992 | + &boss, | |
| 4993 | + &exam, | |
| 4994 | + json!({ "text": "pick", "kind": "choice", "points": 10, | |
| 4995 | + "choices": ["a", "b"], "correct": 0 }), | |
| 4996 | + ) | |
| 4997 | + .await; | |
| 4998 | + let res = send( | |
| 4999 | + &app, | |
| 5000 | + "PATCH", | |
| 5001 | + &format!("/exams/{exam}/questions/{question}"), | |
| 5002 | + Some(&boss), | |
| 5003 | + Some(json!({ "points": 20 })), | |
| 5004 | + ) | |
| 5005 | + .await; | |
| 5006 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 5007 | + let res = send( | |
| 5008 | + &app, | |
| 5009 | + "DELETE", | |
| 5010 | + &format!("/exams/{exam}/questions/{question}"), | |
| 5011 | + Some(&boss), | |
| 5012 | + None, | |
| 5013 | + ) | |
| 5014 | + .await; | |
| 5015 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | |
| 5016 | +} | |
| 5017 | + | |
| 5018 | +#[tokio::test] | |
| 5019 | +async fn concurrent_answer_saves_never_collide() { | |
| 5020 | + let (app, db) = app_and_db().await; | |
| 5021 | + let teacher = login_as(&app, &db, "cc_t", "teacher").await; | |
| 5022 | + let student = login(&app, "cana").await; | |
| 5023 | + let student_id = me_id(&app, &student).await; | |
| 5024 | + let (_course, exam) = open_exam_with_student(&app, &teacher, &student_id, "race").await; | |
| 5025 | + let question = create_question( | |
| 5026 | + &app, | |
| 5027 | + &teacher, | |
| 5028 | + &exam, | |
| 5029 | + json!({ "text": "pick", "kind": "choice", "points": 10, | |
| 5030 | + "choices": ["a", "b", "c"], "correct": 0 }), |