Sessions, roll call, work log, report coverage
Browse files at this commit6f2996a2c669c7d1a4db83a8c4c77e70a5c4a442Parent: 5b6aac1- test
- sessions, roll call, work log, report coverage
2 changed files +816 -2
- tests/common/mod.rs +15 -0
- tests/integration.rs +801 -2
tests/common/mod.rsmodified+15 -0
| @@ -175,6 +175,21 @@ pub async fn create_exam_with(app: &Router, cookie: &str, course: &str, body: Va | |||
| 175 | .await | 175 | .await |
| 176 | } | 176 | } |
| 177 | 177 | ||
| 178 | +/// Create a lesson session inside `course` as `cookie` (asserts 201); returns | ||
| 179 | +/// its id. The session's teacher defaults to the caller. | ||
| 180 | +pub async fn create_session(app: &Router, cookie: &str, course: &str, starts_at: i64) -> String { | ||
| 181 | + let res = send( | ||
| 182 | + app, | ||
| 183 | + "POST", | ||
| 184 | + &format!("/courses/{course}/sessions"), | ||
| 185 | + Some(cookie), | ||
| 186 | + Some(json!({ "starts_at": starts_at })), | ||
| 187 | + ) | ||
| 188 | + .await; | ||
| 189 | + assert_eq!(res.status, StatusCode::CREATED, "create session"); | ||
| 190 | + id_of(&res.body) | ||
| 191 | +} | ||
| 192 | + | ||
| 178 | /// Enroll `user_id` into `course` as `cookie` (asserts 200). | 193 | /// Enroll `user_id` into `course` as `cookie` (asserts 200). |
| 179 | pub async fn enroll(app: &Router, cookie: &str, course: &str, user_id: &str) { | 194 | pub async fn enroll(app: &Router, cookie: &str, course: &str, user_id: &str) { |
| 180 | let res = send( | 195 | let res = send( |
tests/integration.rsmodified+801 -2
| @@ -6,8 +6,8 @@ mod common; | |||
| 6 | use axum::body::Body; | 6 | use axum::body::Body; |
| 7 | use axum::http::{Request, StatusCode}; | 7 | use axum::http::{Request, StatusCode}; |
| 8 | use common::{ | 8 | use common::{ |
| 9 | - app_and_db, create_course, create_exam, create_exam_with, enroll, id_of, login, login_as, | 9 | + app_and_db, create_course, create_exam, create_exam_with, create_session, enroll, id_of, login, |
| 10 | - me_id, mem_app, send, | 10 | + login_as, me_id, mem_app, send, |
| 11 | }; | 11 | }; |
| 12 | use hezarfen_backend::domain::exam::ExamId; | 12 | use hezarfen_backend::domain::exam::ExamId; |
| 13 | use hezarfen_backend::domain::exam_attempt::ExamAttempt; | 13 | use hezarfen_backend::domain::exam_attempt::ExamAttempt; |
| @@ -134,6 +134,22 @@ async fn protected_routes_require_session() { | |||
| 134 | ("GET", "/exams/x/attempts/u/answers"), | 134 | ("GET", "/exams/x/attempts/u/answers"), |
| 135 | // The WebSocket room authenticates before it upgrades. | 135 | // The WebSocket room authenticates before it upgrades. |
| 136 | ("GET", "/exams/x/attempt/ws"), | 136 | ("GET", "/exams/x/attempt/ws"), |
| 137 | + ("POST", "/courses/x/sessions"), | ||
| 138 | + ("GET", "/courses/x/sessions"), | ||
| 139 | + ("GET", "/sessions/x"), | ||
| 140 | + ("PATCH", "/sessions/x"), | ||
| 141 | + ("DELETE", "/sessions/x"), | ||
| 142 | + ("POST", "/sessions/x/attendance"), | ||
| 143 | + ("GET", "/sessions/x/attendance"), | ||
| 144 | + ("DELETE", "/sessions/x/attendance/u"), | ||
| 145 | + ("POST", "/work/check-in"), | ||
| 146 | + ("POST", "/work/check-out"), | ||
| 147 | + ("GET", "/work/me"), | ||
| 148 | + ("GET", "/work/u"), | ||
| 149 | + ("PATCH", "/work/entries/x"), | ||
| 150 | + ("DELETE", "/work/entries/x"), | ||
| 151 | + ("GET", "/attendance/me"), | ||
| 152 | + ("GET", "/attendance/u"), | ||
| 137 | ] { | 153 | ] { |
| 138 | let res = send(&app, method, uri, None, None).await; | 154 | let res = send(&app, method, uri, None, None).await; |
| 139 | assert_eq!(res.status, StatusCode::UNAUTHORIZED, "{method} {uri}"); | 155 | assert_eq!(res.status, StatusCode::UNAUTHORIZED, "{method} {uri}"); |
| @@ -5082,3 +5098,786 @@ async fn concurrent_answer_saves_never_collide() { | |||
| 5082 | .await; | 5098 | .await; |
| 5083 | assert_eq!(res.body["answered"], 1, "24 saves, one answer"); | 5099 | assert_eq!(res.body["answered"], 1, "24 saves, one answer"); |
| 5084 | } | 5100 | } |
| 5101 | + | ||
| 5102 | +// --- course sessions + roll call ------------------------------------------ | ||
| 5103 | + | ||
| 5104 | +#[tokio::test] | ||
| 5105 | +async fn session_crud_follows_course_management() { | ||
| 5106 | + let (app, db) = app_and_db().await; | ||
| 5107 | + let owner = login_as(&app, &db, "owner", "teacher").await; | ||
| 5108 | + let rival = login_as(&app, &db, "rival", "teacher").await; | ||
| 5109 | + let boss = login_as(&app, &db, "boss", "manager").await; | ||
| 5110 | + let student = login(&app, "ali").await; | ||
| 5111 | + let course = create_course(&app, &owner, "algebra").await; | ||
| 5112 | + | ||
| 5113 | + // Create: student 403, unrelated teacher 403, owner 201, manager+ 201. | ||
| 5114 | + let body = json!({ "topic": "limits", "starts_at": 1_700_000_000_000_i64 }); | ||
| 5115 | + let res = send( | ||
| 5116 | + &app, | ||
| 5117 | + "POST", | ||
| 5118 | + &format!("/courses/{course}/sessions"), | ||
| 5119 | + Some(&student), | ||
| 5120 | + Some(body.clone()), | ||
| 5121 | + ) | ||
| 5122 | + .await; | ||
| 5123 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 5124 | + let res = send( | ||
| 5125 | + &app, | ||
| 5126 | + "POST", | ||
| 5127 | + &format!("/courses/{course}/sessions"), | ||
| 5128 | + Some(&rival), | ||
| 5129 | + Some(body.clone()), | ||
| 5130 | + ) | ||
| 5131 | + .await; | ||
| 5132 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 5133 | + let res = send( | ||
| 5134 | + &app, | ||
| 5135 | + "POST", | ||
| 5136 | + &format!("/courses/{course}/sessions"), | ||
| 5137 | + Some(&owner), | ||
| 5138 | + Some(body.clone()), | ||
| 5139 | + ) | ||
| 5140 | + .await; | ||
| 5141 | + assert_eq!(res.status, StatusCode::CREATED); | ||
| 5142 | + let session = id_of(&res.body); | ||
| 5143 | + // The teacher defaults to the caller, rendered as a PersonRef. | ||
| 5144 | + assert_eq!(res.body["teacher"]["username"], "owner"); | ||
| 5145 | + assert_eq!(res.body["topic"], "limits"); | ||
| 5146 | + assert_eq!(res.body["starts_at"], 1_700_000_000_000_i64); | ||
| 5147 | + assert!(res.body["ends_at"].is_null()); | ||
| 5148 | + let res = send( | ||
| 5149 | + &app, | ||
| 5150 | + "POST", | ||
| 5151 | + &format!("/courses/{course}/sessions"), | ||
| 5152 | + Some(&boss), | ||
| 5153 | + Some(body.clone()), | ||
| 5154 | + ) | ||
| 5155 | + .await; | ||
| 5156 | + assert_eq!( | ||
| 5157 | + res.status, | ||
| 5158 | + StatusCode::CREATED, | ||
| 5159 | + "manager+ manages any course" | ||
| 5160 | + ); | ||
| 5161 | + | ||
| 5162 | + // Explicit teacher_id: must exist and be teacher+. | ||
| 5163 | + let res = send( | ||
| 5164 | + &app, | ||
| 5165 | + "POST", | ||
| 5166 | + &format!("/courses/{course}/sessions"), | ||
| 5167 | + Some(&owner), | ||
| 5168 | + Some(json!({ "starts_at": 1, "teacher_id": "01UNKNOWN" })), | ||
| 5169 | + ) | ||
| 5170 | + .await; | ||
| 5171 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "unknown teacher"); | ||
| 5172 | + let ali_id = me_id(&app, &student).await; | ||
| 5173 | + let res = send( | ||
| 5174 | + &app, | ||
| 5175 | + "POST", | ||
| 5176 | + &format!("/courses/{course}/sessions"), | ||
| 5177 | + Some(&owner), | ||
| 5178 | + Some(json!({ "starts_at": 1, "teacher_id": ali_id })), | ||
| 5179 | + ) | ||
| 5180 | + .await; | ||
| 5181 | + assert_eq!( | ||
| 5182 | + res.status, | ||
| 5183 | + StatusCode::BAD_REQUEST, | ||
| 5184 | + "a student cannot teach" | ||
| 5185 | + ); | ||
| 5186 | + let rival_id = me_id(&app, &rival).await; | ||
| 5187 | + let res = send( | ||
| 5188 | + &app, | ||
| 5189 | + "POST", | ||
| 5190 | + &format!("/courses/{course}/sessions"), | ||
| 5191 | + Some(&owner), | ||
| 5192 | + Some(json!({ "starts_at": 2, "teacher_id": rival_id })), | ||
| 5193 | + ) | ||
| 5194 | + .await; | ||
| 5195 | + assert_eq!(res.status, StatusCode::CREATED); | ||
| 5196 | + assert_eq!(res.body["teacher"]["username"], "rival"); | ||
| 5197 | + let rival_session = id_of(&res.body); | ||
| 5198 | + | ||
| 5199 | + // Time range validated on create and update. | ||
| 5200 | + let res = send( | ||
| 5201 | + &app, | ||
| 5202 | + "POST", | ||
| 5203 | + &format!("/courses/{course}/sessions"), | ||
| 5204 | + Some(&owner), | ||
| 5205 | + Some(json!({ "starts_at": 10, "ends_at": 5 })), | ||
| 5206 | + ) | ||
| 5207 | + .await; | ||
| 5208 | + assert_eq!(res.status, StatusCode::BAD_REQUEST); | ||
| 5209 | + | ||
| 5210 | + // Reads are open to any logged-in user; parents must exist. | ||
| 5211 | + let res = send( | ||
| 5212 | + &app, | ||
| 5213 | + "GET", | ||
| 5214 | + &format!("/courses/{course}/sessions"), | ||
| 5215 | + Some(&student), | ||
| 5216 | + None, | ||
| 5217 | + ) | ||
| 5218 | + .await; | ||
| 5219 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5220 | + // owner's, the manager's, and the one taught by `rival` — newest starts_at | ||
| 5221 | + // first, so the two 1.7e12 lessons precede the starts_at=2 one. | ||
| 5222 | + assert_eq!(res.body.as_array().unwrap().len(), 3); | ||
| 5223 | + assert_eq!(res.body[2]["starts_at"], 2); | ||
| 5224 | + let res = send(&app, "GET", "/courses/nope/sessions", Some(&student), None).await; | ||
| 5225 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 5226 | + let res = send( | ||
| 5227 | + &app, | ||
| 5228 | + "GET", | ||
| 5229 | + &format!("/sessions/{session}"), | ||
| 5230 | + Some(&student), | ||
| 5231 | + None, | ||
| 5232 | + ) | ||
| 5233 | + .await; | ||
| 5234 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5235 | + let res = send(&app, "GET", "/sessions/nope", Some(&student), None).await; | ||
| 5236 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 5237 | + | ||
| 5238 | + // PATCH: management rights; set/keep/clear semantics; range check. | ||
| 5239 | + let res = send( | ||
| 5240 | + &app, | ||
| 5241 | + "PATCH", | ||
| 5242 | + &format!("/sessions/{session}"), | ||
| 5243 | + Some(&rival), | ||
| 5244 | + Some(json!({ "topic": "hijack" })), | ||
| 5245 | + ) | ||
| 5246 | + .await; | ||
| 5247 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 5248 | + let res = send( | ||
| 5249 | + &app, | ||
| 5250 | + "PATCH", | ||
| 5251 | + &format!("/sessions/{session}"), | ||
| 5252 | + Some(&owner), | ||
| 5253 | + Some(json!({ "topic": "derivatives", "ends_at": 1_700_000_100_000_i64 })), | ||
| 5254 | + ) | ||
| 5255 | + .await; | ||
| 5256 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5257 | + assert_eq!(res.body["topic"], "derivatives"); | ||
| 5258 | + assert_eq!(res.body["ends_at"], 1_700_000_100_000_i64); | ||
| 5259 | + assert_eq!( | ||
| 5260 | + res.body["starts_at"], 1_700_000_000_000_i64, | ||
| 5261 | + "omitted keeps" | ||
| 5262 | + ); | ||
| 5263 | + let res = send( | ||
| 5264 | + &app, | ||
| 5265 | + "PATCH", | ||
| 5266 | + &format!("/sessions/{session}"), | ||
| 5267 | + Some(&owner), | ||
| 5268 | + Some(json!({ "ends_at": null })), | ||
| 5269 | + ) | ||
| 5270 | + .await; | ||
| 5271 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5272 | + assert!(res.body["ends_at"].is_null(), "explicit null clears"); | ||
| 5273 | + let res = send( | ||
| 5274 | + &app, | ||
| 5275 | + "PATCH", | ||
| 5276 | + &format!("/sessions/{session}"), | ||
| 5277 | + Some(&owner), | ||
| 5278 | + Some(json!({ "ends_at": 999 })), | ||
| 5279 | + ) | ||
| 5280 | + .await; | ||
| 5281 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "ends before starts"); | ||
| 5282 | + // Reassigning the teacher revalidates the target. | ||
| 5283 | + let res = send( | ||
| 5284 | + &app, | ||
| 5285 | + "PATCH", | ||
| 5286 | + &format!("/sessions/{rival_session}"), | ||
| 5287 | + Some(&boss), | ||
| 5288 | + Some(json!({ "teacher_id": ali_id })), | ||
| 5289 | + ) | ||
| 5290 | + .await; | ||
| 5291 | + assert_eq!(res.status, StatusCode::BAD_REQUEST); | ||
| 5292 | + | ||
| 5293 | + // DELETE: management rights. | ||
| 5294 | + let res = send( | ||
| 5295 | + &app, | ||
| 5296 | + "DELETE", | ||
| 5297 | + &format!("/sessions/{session}"), | ||
| 5298 | + Some(&rival), | ||
| 5299 | + None, | ||
| 5300 | + ) | ||
| 5301 | + .await; | ||
| 5302 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 5303 | + let res = send( | ||
| 5304 | + &app, | ||
| 5305 | + "DELETE", | ||
| 5306 | + &format!("/sessions/{session}"), | ||
| 5307 | + Some(&owner), | ||
| 5308 | + None, | ||
| 5309 | + ) | ||
| 5310 | + .await; | ||
| 5311 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 5312 | + let res = send( | ||
| 5313 | + &app, | ||
| 5314 | + "GET", | ||
| 5315 | + &format!("/sessions/{session}"), | ||
| 5316 | + Some(&owner), | ||
| 5317 | + None, | ||
| 5318 | + ) | ||
| 5319 | + .await; | ||
| 5320 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 5321 | +} | ||
| 5322 | + | ||
| 5323 | +#[tokio::test] | ||
| 5324 | +async fn roll_call_rbac_and_upsert() { | ||
| 5325 | + let (app, db) = app_and_db().await; | ||
| 5326 | + let owner = login_as(&app, &db, "owner", "teacher").await; | ||
| 5327 | + let hoca = login_as(&app, &db, "hoca", "teacher").await; | ||
| 5328 | + let rival = login_as(&app, &db, "rival", "teacher").await; | ||
| 5329 | + let boss = login_as(&app, &db, "boss", "manager").await; | ||
| 5330 | + let ali = login(&app, "ali").await; | ||
| 5331 | + let veli = login(&app, "veli").await; | ||
| 5332 | + let ali_id = me_id(&app, &ali).await; | ||
| 5333 | + let veli_id = me_id(&app, &veli).await; | ||
| 5334 | + let hoca_id = me_id(&app, &hoca).await; | ||
| 5335 | + | ||
| 5336 | + let course = create_course(&app, &owner, "algebra").await; | ||
| 5337 | + enroll(&app, &owner, &course, &ali_id).await; | ||
| 5338 | + let res = send( | ||
| 5339 | + &app, | ||
| 5340 | + "POST", | ||
| 5341 | + &format!("/courses/{course}/sessions"), | ||
| 5342 | + Some(&owner), | ||
| 5343 | + Some(json!({ "starts_at": 1, "teacher_id": hoca_id })), | ||
| 5344 | + ) | ||
| 5345 | + .await; | ||
| 5346 | + assert_eq!(res.status, StatusCode::CREATED); | ||
| 5347 | + let session = id_of(&res.body); | ||
| 5348 | + let mark_uri = format!("/sessions/{session}/attendance"); | ||
| 5349 | + | ||
| 5350 | + // The session's teacher takes roll even without course-management rights. | ||
| 5351 | + let res = send( | ||
| 5352 | + &app, | ||
| 5353 | + "POST", | ||
| 5354 | + &mark_uri, | ||
| 5355 | + Some(&hoca), | ||
| 5356 | + Some(json!({ "status": "present", "user_id": ali_id })), | ||
| 5357 | + ) | ||
| 5358 | + .await; | ||
| 5359 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5360 | + assert_eq!(res.body["user"]["username"], "ali"); | ||
| 5361 | + assert_eq!(res.body["marked_by"]["username"], "hoca"); | ||
| 5362 | + assert_eq!(res.body["course"].as_str().unwrap(), course); | ||
| 5363 | + | ||
| 5364 | + // Re-marking overwrites — one row per session+user (course manager may too). | ||
| 5365 | + let res = send( | ||
| 5366 | + &app, | ||
| 5367 | + "POST", | ||
| 5368 | + &mark_uri, | ||
| 5369 | + Some(&owner), | ||
| 5370 | + Some(json!({ "status": "absent", "user_id": ali_id })), | ||
| 5371 | + ) | ||
| 5372 | + .await; | ||
| 5373 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5374 | + let res = send(&app, "GET", &mark_uri, Some(&ali), None).await; | ||
| 5375 | + assert_eq!(res.status, StatusCode::OK, "roster is readable by students"); | ||
| 5376 | + let roster = res.body.as_array().unwrap(); | ||
| 5377 | + assert_eq!(roster.len(), 1); | ||
| 5378 | + assert_eq!(roster[0]["status"], "absent"); | ||
| 5379 | + | ||
| 5380 | + // Neither an unrelated teacher nor the student themselves may mark. | ||
| 5381 | + let res = send( | ||
| 5382 | + &app, | ||
| 5383 | + "POST", | ||
| 5384 | + &mark_uri, | ||
| 5385 | + Some(&rival), | ||
| 5386 | + Some(json!({ "status": "present", "user_id": ali_id })), | ||
| 5387 | + ) | ||
| 5388 | + .await; | ||
| 5389 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 5390 | + let res = send( | ||
| 5391 | + &app, | ||
| 5392 | + "POST", | ||
| 5393 | + &mark_uri, | ||
| 5394 | + Some(&ali), | ||
| 5395 | + Some(json!({ "status": "present", "user_id": ali_id })), | ||
| 5396 | + ) | ||
| 5397 | + .await; | ||
| 5398 | + assert_eq!( | ||
| 5399 | + res.status, | ||
| 5400 | + StatusCode::FORBIDDEN, | ||
| 5401 | + "students never self-mark" | ||
| 5402 | + ); | ||
| 5403 | + | ||
| 5404 | + // The roster comes from enrollment; targets must exist; statuses are closed. | ||
| 5405 | + let res = send( | ||
| 5406 | + &app, | ||
| 5407 | + "POST", | ||
| 5408 | + &mark_uri, | ||
| 5409 | + Some(&hoca), | ||
| 5410 | + Some(json!({ "status": "present", "user_id": veli_id })), | ||
| 5411 | + ) | ||
| 5412 | + .await; | ||
| 5413 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "not enrolled"); | ||
| 5414 | + let res = send( | ||
| 5415 | + &app, | ||
| 5416 | + "POST", | ||
| 5417 | + &mark_uri, | ||
| 5418 | + Some(&hoca), | ||
| 5419 | + Some(json!({ "status": "present", "user_id": "01UNKNOWN" })), | ||
| 5420 | + ) | ||
| 5421 | + .await; | ||
| 5422 | + assert_eq!(res.status, StatusCode::BAD_REQUEST); | ||
| 5423 | + let res = send( | ||
| 5424 | + &app, | ||
| 5425 | + "POST", | ||
| 5426 | + &mark_uri, | ||
| 5427 | + Some(&hoca), | ||
| 5428 | + Some(json!({ "status": "sleeping", "user_id": ali_id })), | ||
| 5429 | + ) | ||
| 5430 | + .await; | ||
| 5431 | + assert_eq!(res.status, StatusCode::BAD_REQUEST); | ||
| 5432 | + let res = send( | ||
| 5433 | + &app, | ||
| 5434 | + "POST", | ||
| 5435 | + "/sessions/nope/attendance", | ||
| 5436 | + Some(&hoca), | ||
| 5437 | + Some(json!({ "status": "present", "user_id": ali_id })), | ||
| 5438 | + ) | ||
| 5439 | + .await; | ||
| 5440 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 5441 | + | ||
| 5442 | + // The session teacher's own row is management's call: even the course | ||
| 5443 | + // creator (teacher role) can't write it; a manager can. | ||
| 5444 | + let res = send( | ||
| 5445 | + &app, | ||
| 5446 | + "POST", | ||
| 5447 | + &mark_uri, | ||
| 5448 | + Some(&hoca), | ||
| 5449 | + Some(json!({ "status": "present", "user_id": hoca_id })), | ||
| 5450 | + ) | ||
| 5451 | + .await; | ||
| 5452 | + assert_eq!( | ||
| 5453 | + res.status, | ||
| 5454 | + StatusCode::FORBIDDEN, | ||
| 5455 | + "no self-declared presence" | ||
| 5456 | + ); | ||
| 5457 | + let res = send( | ||
| 5458 | + &app, | ||
| 5459 | + "POST", | ||
| 5460 | + &mark_uri, | ||
| 5461 | + Some(&owner), | ||
| 5462 | + Some(json!({ "status": "present", "user_id": hoca_id })), | ||
| 5463 | + ) | ||
| 5464 | + .await; | ||
| 5465 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 5466 | + let res = send( | ||
| 5467 | + &app, | ||
| 5468 | + "POST", | ||
| 5469 | + &mark_uri, | ||
| 5470 | + Some(&boss), | ||
| 5471 | + Some(json!({ "status": "late", "user_id": hoca_id })), | ||
| 5472 | + ) | ||
| 5473 | + .await; | ||
| 5474 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5475 | + | ||
| 5476 | + // Removal mirrors marking rights. | ||
| 5477 | + let res = send( | ||
| 5478 | + &app, | ||
| 5479 | + "DELETE", | ||
| 5480 | + &format!("{mark_uri}/{ali_id}"), | ||
| 5481 | + Some(&rival), | ||
| 5482 | + None, | ||
| 5483 | + ) | ||
| 5484 | + .await; | ||
| 5485 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 5486 | + let res = send( | ||
| 5487 | + &app, | ||
| 5488 | + "DELETE", | ||
| 5489 | + &format!("{mark_uri}/{hoca_id}"), | ||
| 5490 | + Some(&hoca), | ||
| 5491 | + None, | ||
| 5492 | + ) | ||
| 5493 | + .await; | ||
| 5494 | + assert_eq!( | ||
| 5495 | + res.status, | ||
| 5496 | + StatusCode::FORBIDDEN, | ||
| 5497 | + "teacher row needs manager+" | ||
| 5498 | + ); | ||
| 5499 | + let res = send( | ||
| 5500 | + &app, | ||
| 5501 | + "DELETE", | ||
| 5502 | + &format!("{mark_uri}/{hoca_id}"), | ||
| 5503 | + Some(&boss), | ||
| 5504 | + None, | ||
| 5505 | + ) | ||
| 5506 | + .await; | ||
| 5507 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 5508 | + let res = send( | ||
| 5509 | + &app, | ||
| 5510 | + "DELETE", | ||
| 5511 | + &format!("{mark_uri}/{ali_id}"), | ||
| 5512 | + Some(&hoca), | ||
| 5513 | + None, | ||
| 5514 | + ) | ||
| 5515 | + .await; | ||
| 5516 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 5517 | + let res = send( | ||
| 5518 | + &app, | ||
| 5519 | + "DELETE", | ||
| 5520 | + &format!("{mark_uri}/{ali_id}"), | ||
| 5521 | + Some(&hoca), | ||
| 5522 | + None, | ||
| 5523 | + ) | ||
| 5524 | + .await; | ||
| 5525 | + assert_eq!(res.status, StatusCode::NOT_FOUND, "row already gone"); | ||
| 5526 | +} | ||
| 5527 | + | ||
| 5528 | +#[tokio::test] | ||
| 5529 | +async fn deleting_session_or_course_cascades_roll_call() { | ||
| 5530 | + let (app, db) = app_and_db().await; | ||
| 5531 | + let owner = login_as(&app, &db, "owner", "teacher").await; | ||
| 5532 | + let ali = login(&app, "ali").await; | ||
| 5533 | + let ali_id = me_id(&app, &ali).await; | ||
| 5534 | + | ||
| 5535 | + let course = create_course(&app, &owner, "algebra").await; | ||
| 5536 | + enroll(&app, &owner, &course, &ali_id).await; | ||
| 5537 | + let s1 = create_session(&app, &owner, &course, 1).await; | ||
| 5538 | + let s2 = create_session(&app, &owner, &course, 2).await; | ||
| 5539 | + for s in [&s1, &s2] { | ||
| 5540 | + let res = send( | ||
| 5541 | + &app, | ||
| 5542 | + "POST", | ||
| 5543 | + &format!("/sessions/{s}/attendance"), | ||
| 5544 | + Some(&owner), | ||
| 5545 | + Some(json!({ "status": "present", "user_id": ali_id })), | ||
| 5546 | + ) | ||
| 5547 | + .await; | ||
| 5548 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5549 | + } | ||
| 5550 | + let report = send(&app, "GET", "/attendance/me", Some(&ali), None).await; | ||
| 5551 | + assert_eq!(report.body["sessions"]["total"], 2); | ||
| 5552 | + | ||
| 5553 | + // Deleting one session removes exactly its rows... | ||
| 5554 | + let res = send( | ||
| 5555 | + &app, | ||
| 5556 | + "DELETE", | ||
| 5557 | + &format!("/sessions/{s1}"), | ||
| 5558 | + Some(&owner), | ||
| 5559 | + None, | ||
| 5560 | + ) | ||
| 5561 | + .await; | ||
| 5562 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 5563 | + let report = send(&app, "GET", "/attendance/me", Some(&ali), None).await; | ||
| 5564 | + assert_eq!(report.body["sessions"]["total"], 1); | ||
| 5565 | + | ||
| 5566 | + // ...and deleting the course removes the rest, sessions included. | ||
| 5567 | + let res = send( | ||
| 5568 | + &app, | ||
| 5569 | + "DELETE", | ||
| 5570 | + &format!("/courses/{course}"), | ||
| 5571 | + Some(&owner), | ||
| 5572 | + None, | ||
| 5573 | + ) | ||
| 5574 | + .await; | ||
| 5575 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 5576 | + let res = send(&app, "GET", &format!("/sessions/{s2}"), Some(&ali), None).await; | ||
| 5577 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 5578 | + let report = send(&app, "GET", "/attendance/me", Some(&ali), None).await; | ||
| 5579 | + assert_eq!(report.body["sessions"]["total"], 0); | ||
| 5580 | + assert!(report.body["courses"].as_array().unwrap().is_empty()); | ||
| 5581 | +} | ||
| 5582 | + | ||
| 5583 | +// --- work log --------------------------------------------------------------- | ||
| 5584 | + | ||
| 5585 | +#[tokio::test] | ||
| 5586 | +async fn work_log_lifecycle_is_server_stamped_and_exclusive() { | ||
| 5587 | + let (app, db) = app_and_db().await; | ||
| 5588 | + let hoca = login_as(&app, &db, "hoca", "teacher").await; | ||
| 5589 | + let ali = login(&app, "ali").await; | ||
| 5590 | + | ||
| 5591 | + // Staff only. | ||
| 5592 | + let res = send(&app, "POST", "/work/check-in", Some(&ali), None).await; | ||
| 5593 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 5594 | + let res = send(&app, "GET", "/work/me", Some(&ali), None).await; | ||
| 5595 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 5596 | + | ||
| 5597 | + // Check out before check-in conflicts. | ||
| 5598 | + let res = send(&app, "POST", "/work/check-out", Some(&hoca), None).await; | ||
| 5599 | + assert_eq!(res.status, StatusCode::CONFLICT); | ||
| 5600 | + | ||
| 5601 | + let res = send(&app, "POST", "/work/check-in", Some(&hoca), None).await; | ||
| 5602 | + assert_eq!(res.status, StatusCode::CREATED); | ||
| 5603 | + assert!(res.body["check_out"].is_null()); | ||
| 5604 | + let first_in = res.body["check_in"].as_i64().unwrap(); | ||
| 5605 | + | ||
| 5606 | + // A second check-in neither opens a second stint nor resets the clock. | ||
| 5607 | + let res = send(&app, "POST", "/work/check-in", Some(&hoca), None).await; | ||
| 5608 | + assert_eq!(res.status, StatusCode::CONFLICT); | ||
| 5609 | + let res = send(&app, "GET", "/work/me", Some(&hoca), None).await; | ||
| 5610 | + let log = res.body.as_array().unwrap(); | ||
| 5611 | + assert_eq!(log.len(), 1); | ||
| 5612 | + assert_eq!(log[0]["check_in"].as_i64().unwrap(), first_in); | ||
| 5613 | + | ||
| 5614 | + let res = send(&app, "POST", "/work/check-out", Some(&hoca), None).await; | ||
| 5615 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5616 | + let out = res.body["check_out"].as_i64().unwrap(); | ||
| 5617 | + assert!(out >= first_in); | ||
| 5618 | + assert_eq!(res.body["duration_ms"].as_i64().unwrap(), out - first_in); | ||
| 5619 | + let res = send(&app, "POST", "/work/check-out", Some(&hoca), None).await; | ||
| 5620 | + assert_eq!(res.status, StatusCode::CONFLICT, "already checked out"); | ||
| 5621 | + | ||
| 5622 | + // The open slot is free again; the log keeps both stints, newest first. | ||
| 5623 | + let res = send(&app, "POST", "/work/check-in", Some(&hoca), None).await; | ||
| 5624 | + assert_eq!(res.status, StatusCode::CREATED); | ||
| 5625 | + let res = send(&app, "GET", "/work/me", Some(&hoca), None).await; | ||
| 5626 | + let log = res.body.as_array().unwrap(); | ||
| 5627 | + assert_eq!(log.len(), 2); | ||
| 5628 | + assert!(log[0]["check_out"].is_null(), "open stint sorts newest"); | ||
| 5629 | + assert!(!log[1]["check_out"].is_null()); | ||
| 5630 | +} | ||
| 5631 | + | ||
| 5632 | +#[tokio::test] | ||
| 5633 | +async fn work_log_manager_reads_and_corrections() { | ||
| 5634 | + let (app, db) = app_and_db().await; | ||
| 5635 | + let hoca = login_as(&app, &db, "hoca", "teacher").await; | ||
| 5636 | + let other = login_as(&app, &db, "other", "teacher").await; | ||
| 5637 | + let boss = login_as(&app, &db, "boss", "manager").await; | ||
| 5638 | + let hoca_id = me_id(&app, &hoca).await; | ||
| 5639 | + | ||
| 5640 | + // One closed stint and one open stint for hoca. | ||
| 5641 | + send(&app, "POST", "/work/check-in", Some(&hoca), None).await; | ||
| 5642 | + let closed = send(&app, "POST", "/work/check-out", Some(&hoca), None).await; | ||
| 5643 | + let closed_id = id_of(&closed.body); | ||
| 5644 | + let open = send(&app, "POST", "/work/check-in", Some(&hoca), None).await; | ||
| 5645 | + let open_id = id_of(&open.body); | ||
| 5646 | + | ||
| 5647 | + // Reading someone's log is manager+; unknown users are 404. | ||
| 5648 | + let res = send(&app, "GET", &format!("/work/{hoca_id}"), Some(&other), None).await; | ||
| 5649 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 5650 | + let res = send(&app, "GET", &format!("/work/{hoca_id}"), Some(&boss), None).await; | ||
| 5651 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5652 | + assert_eq!(res.body.as_array().unwrap().len(), 2); | ||
| 5653 | + let res = send(&app, "GET", "/work/01UNKNOWN", Some(&boss), None).await; | ||
| 5654 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 5655 | + | ||
| 5656 | + // Corrections: manager+ only, closed entries only, ordered instants only. | ||
| 5657 | + let res = send( | ||
| 5658 | + &app, | ||
| 5659 | + "PATCH", | ||
| 5660 | + &format!("/work/entries/{closed_id}"), | ||
| 5661 | + Some(&hoca), | ||
| 5662 | + Some(json!({ "check_in": 1000 })), | ||
| 5663 | + ) | ||
| 5664 | + .await; | ||
| 5665 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 5666 | + let res = send( | ||
| 5667 | + &app, | ||
| 5668 | + "PATCH", | ||
| 5669 | + &format!("/work/entries/{closed_id}"), | ||
| 5670 | + Some(&boss), | ||
| 5671 | + Some(json!({ "check_in": 1000, "check_out": 2000 })), | ||
| 5672 | + ) | ||
| 5673 | + .await; | ||
| 5674 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5675 | + assert_eq!(res.body["check_in"], 1000); | ||
| 5676 | + assert_eq!(res.body["check_out"], 2000); | ||
| 5677 | + assert_eq!(res.body["duration_ms"], 1000); | ||
| 5678 | + let res = send( | ||
| 5679 | + &app, | ||
| 5680 | + "PATCH", | ||
| 5681 | + &format!("/work/entries/{closed_id}"), | ||
| 5682 | + Some(&boss), | ||
| 5683 | + Some(json!({ "check_out": 500 })), | ||
| 5684 | + ) | ||
| 5685 | + .await; | ||
| 5686 | + assert_eq!(res.status, StatusCode::BAD_REQUEST, "out precedes in"); | ||
| 5687 | + let res = send( | ||
| 5688 | + &app, | ||
| 5689 | + "PATCH", | ||
| 5690 | + &format!("/work/entries/{open_id}"), | ||
| 5691 | + Some(&boss), | ||
| 5692 | + Some(json!({ "check_in": 1 })), | ||
| 5693 | + ) | ||
| 5694 | + .await; | ||
| 5695 | + assert_eq!( | ||
| 5696 | + res.status, | ||
| 5697 | + StatusCode::CONFLICT, | ||
| 5698 | + "open entries aren't correctable" | ||
| 5699 | + ); | ||
| 5700 | + let res = send( | ||
| 5701 | + &app, | ||
| 5702 | + "PATCH", | ||
| 5703 | + "/work/entries/nope", | ||
| 5704 | + Some(&boss), | ||
| 5705 | + Some(json!({})), | ||
| 5706 | + ) | ||
| 5707 | + .await; | ||
| 5708 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 5709 | + | ||
| 5710 | + // Deletion: manager+, any entry (open ones included — the stray-row broom). | ||
| 5711 | + let res = send( | ||
| 5712 | + &app, | ||
| 5713 | + "DELETE", | ||
| 5714 | + &format!("/work/entries/{closed_id}"), | ||
| 5715 | + Some(&hoca), | ||
| 5716 | + None, | ||
| 5717 | + ) | ||
| 5718 | + .await; | ||
| 5719 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 5720 | + let res = send( | ||
| 5721 | + &app, | ||
| 5722 | + "DELETE", | ||
| 5723 | + &format!("/work/entries/{open_id}"), | ||
| 5724 | + Some(&boss), | ||
| 5725 | + None, | ||
| 5726 | + ) | ||
| 5727 | + .await; | ||
| 5728 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 5729 | + let res = send( | ||
| 5730 | + &app, | ||
| 5731 | + "DELETE", | ||
| 5732 | + &format!("/work/entries/{open_id}"), | ||
| 5733 | + Some(&boss), | ||
| 5734 | + None, | ||
| 5735 | + ) | ||
| 5736 | + .await; | ||
| 5737 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 5738 | + // With the open row swept, hoca can check in again. | ||
| 5739 | + let res = send(&app, "POST", "/work/check-in", Some(&hoca), None).await; | ||
| 5740 | + assert_eq!(res.status, StatusCode::CREATED); | ||
| 5741 | +} | ||
| 5742 | + | ||
| 5743 | +// --- attendance reports ------------------------------------------------------- | ||
| 5744 | + | ||
| 5745 | +#[tokio::test] | ||
| 5746 | +async fn attendance_report_tallies_events_and_sessions_per_course() { | ||
| 5747 | + let (app, db) = app_and_db().await; | ||
| 5748 | + let owner = login_as(&app, &db, "owner", "teacher").await; | ||
| 5749 | + let ali = login(&app, "ali").await; | ||
| 5750 | + let veli = login(&app, "veli").await; | ||
| 5751 | + let ali_id = me_id(&app, &ali).await; | ||
| 5752 | + | ||
| 5753 | + // A fresh user's report is all zeros with null rates. | ||
| 5754 | + let res = send(&app, "GET", "/attendance/me", Some(&ali), None).await; | ||
| 5755 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5756 | + assert_eq!(res.body["events"]["total"], 0); | ||
| 5757 | + assert!(res.body["events"]["rate"].is_null()); | ||
| 5758 | + assert!(res.body["courses"].as_array().unwrap().is_empty()); | ||
| 5759 | + | ||
| 5760 | + // Two event rows: one self-marked, one teacher-marked. | ||
| 5761 | + let ev1 = send( | ||
| 5762 | + &app, | ||
| 5763 | + "POST", | ||
| 5764 | + "/events", | ||
| 5765 | + Some(&owner), | ||
| 5766 | + Some(json!({ "title": "assembly" })), | ||
| 5767 | + ) | ||
| 5768 | + .await; | ||
| 5769 | + let ev2 = send( | ||
| 5770 | + &app, | ||
| 5771 | + "POST", | ||
| 5772 | + "/events", | ||
| 5773 | + Some(&owner), | ||
| 5774 | + Some(json!({ "title": "trip" })), | ||
| 5775 | + ) | ||
| 5776 | + .await; | ||
| 5777 | + let (ev1, ev2) = (id_of(&ev1.body), id_of(&ev2.body)); | ||
| 5778 | + send( | ||
| 5779 | + &app, | ||
| 5780 | + "POST", | ||
| 5781 | + &format!("/events/{ev1}/attendance"), | ||
| 5782 | + Some(&ali), | ||
| 5783 | + Some(json!({ "status": "present" })), | ||
| 5784 | + ) | ||
| 5785 | + .await; | ||
| 5786 | + send( | ||
| 5787 | + &app, | ||
| 5788 | + "POST", | ||
| 5789 | + &format!("/events/{ev2}/attendance"), | ||
| 5790 | + Some(&owner), | ||
| 5791 | + Some(json!({ "status": "absent", "user_id": ali_id })), | ||
| 5792 | + ) | ||
| 5793 | + .await; | ||
| 5794 | + | ||
| 5795 | + // Roll call across two courses: algebra present+late, physics absent+excused. | ||
| 5796 | + let algebra = create_course(&app, &owner, "algebra").await; | ||
| 5797 | + let physics = create_course(&app, &owner, "physics").await; | ||
| 5798 | + for (course, statuses) in [ | ||
| 5799 | + (&algebra, ["present", "late"]), | ||
| 5800 | + (&physics, ["absent", "excused"]), | ||
| 5801 | + ] { | ||
| 5802 | + enroll(&app, &owner, course, &ali_id).await; | ||
| 5803 | + for (n, status) in statuses.iter().enumerate() { | ||
| 5804 | + let session = create_session(&app, &owner, course, n as i64 + 1).await; | ||
| 5805 | + let res = send( | ||
| 5806 | + &app, | ||
| 5807 | + "POST", | ||
| 5808 | + &format!("/sessions/{session}/attendance"), | ||
| 5809 | + Some(&owner), | ||
| 5810 | + Some(json!({ "status": status, "user_id": ali_id })), | ||
| 5811 | + ) | ||
| 5812 | + .await; | ||
| 5813 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5814 | + } | ||
| 5815 | + } | ||
| 5816 | + | ||
| 5817 | + let res = send(&app, "GET", "/attendance/me", Some(&ali), None).await; | ||
| 5818 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5819 | + assert_eq!(res.body["user"].as_str().unwrap(), ali_id); | ||
| 5820 | + // Events: 1 present + 1 absent → rate 1/2. | ||
| 5821 | + assert_eq!(res.body["events"]["present"], 1); | ||
| 5822 | + assert_eq!(res.body["events"]["absent"], 1); | ||
| 5823 | + assert_eq!(res.body["events"]["total"], 2); | ||
| 5824 | + assert_eq!(res.body["events"]["rate"].as_f64().unwrap(), 0.5); | ||
| 5825 | + // Sessions overall: present+late+absent+excused → rate (1+1)/3. | ||
| 5826 | + assert_eq!(res.body["sessions"]["total"], 4); | ||
| 5827 | + assert_eq!(res.body["sessions"]["excused"], 1); | ||
| 5828 | + let rate = res.body["sessions"]["rate"].as_f64().unwrap(); | ||
| 5829 | + assert!( | ||
| 5830 | + (rate - 2.0 / 3.0).abs() < 1e-12, | ||
| 5831 | + "excused stays out: {rate}" | ||
| 5832 | + ); | ||
| 5833 | + // Per-course blocks: algebra 100% attended, physics 0% (excused uncounted). | ||
| 5834 | + let courses = res.body["courses"].as_array().unwrap(); | ||
| 5835 | + assert_eq!(courses.len(), 2); | ||
| 5836 | + let block = |title: &str| { | ||
| 5837 | + courses | ||
| 5838 | + .iter() | ||
| 5839 | + .find(|b| b["course"]["title"] == title) | ||
| 5840 | + .unwrap_or_else(|| panic!("missing course block {title}")) | ||
| 5841 | + .clone() | ||
| 5842 | + }; | ||
| 5843 | + assert_eq!(block("algebra")["counts"]["rate"].as_f64().unwrap(), 1.0); | ||
| 5844 | + assert_eq!(block("algebra")["counts"]["late"], 1); | ||
| 5845 | + assert_eq!(block("physics")["counts"]["rate"].as_f64().unwrap(), 0.0); | ||
| 5846 | + assert_eq!(block("physics")["counts"]["excused"], 1); | ||
| 5847 | + | ||
| 5848 | + // Unenrolling hides marks, never absences: the physics block stays. | ||
| 5849 | + let res = send( | ||
| 5850 | + &app, | ||
| 5851 | + "DELETE", | ||
| 5852 | + &format!("/courses/{physics}/enrollments/{ali_id}"), | ||
| 5853 | + Some(&owner), | ||
| 5854 | + None, | ||
| 5855 | + ) | ||
| 5856 | + .await; | ||
| 5857 | + assert_eq!(res.status, StatusCode::NO_CONTENT); | ||
| 5858 | + let res = send(&app, "GET", "/attendance/me", Some(&ali), None).await; | ||
| 5859 | + assert_eq!(res.body["courses"].as_array().unwrap().len(), 2); | ||
| 5860 | + | ||
| 5861 | + // Someone else's report: teacher+ only; unknown users are 404. | ||
| 5862 | + let res = send( | ||
| 5863 | + &app, | ||
| 5864 | + "GET", | ||
| 5865 | + &format!("/attendance/{ali_id}"), | ||
| 5866 | + Some(&veli), | ||
| 5867 | + None, | ||
| 5868 | + ) | ||
| 5869 | + .await; | ||
| 5870 | + assert_eq!(res.status, StatusCode::FORBIDDEN); | ||
| 5871 | + let res = send( | ||
| 5872 | + &app, | ||
| 5873 | + "GET", | ||
| 5874 | + &format!("/attendance/{ali_id}"), | ||
| 5875 | + Some(&owner), | ||
| 5876 | + None, | ||
| 5877 | + ) | ||
| 5878 | + .await; | ||
| 5879 | + assert_eq!(res.status, StatusCode::OK); | ||
| 5880 | + assert_eq!(res.body["sessions"]["total"], 4); | ||
| 5881 | + let res = send(&app, "GET", "/attendance/01UNKNOWN", Some(&owner), None).await; | ||
| 5882 | + assert_eq!(res.status, StatusCode::NOT_FOUND); | ||
| 5883 | +} | ||