🧪 pin the class pump at the router, wire, and volume
Browse files at this commitddc09d765fe277ab49b23ba3d265d8867bb96917Parent: f98f0d8- test
- 🧪 pin the class pump at the router, wire, and volume
4 changed files +841 -0
- tests/e2e.rs +106 -0
- tests/integration.rs +484 -0
- tests/pagination.rs +129 -0
- tests/persistence.rs +122 -0
tests/e2e.rsmodified+106 -0
| @@ -3002,3 +3002,109 @@ async fn a_roster_change_drops_only_the_socket_it_removed() { | ||
| 3002 | 3002 | vec!["still mine".to_string()] |
| 3003 | 3003 | ); |
| 3004 | 3004 | } |
| 3005 | + | |
| 3006 | +/// The class layer over real HTTP with three cookie jars: the office builds the | |
| 3007 | +/// class, the teacher hands over their own course, and the student — who was | |
| 3008 | +/// never enrolled by anyone — finds it on `GET /courses/me`. Taking them out of | |
| 3009 | +/// the class takes the seat back with them. | |
| 3010 | +#[tokio::test] | |
| 3011 | +async fn a_class_seats_its_roster_and_gives_the_seat_back() { | |
| 3012 | + let (base, db) = spawn_server().await; | |
| 3013 | + let manager = client(); | |
| 3014 | + let teacher = client(); | |
| 3015 | + let student = client(); | |
| 3016 | + | |
| 3017 | + for (c, name) in [(&manager, "mgr"), (&teacher, "tch"), (&student, "veli")] { | |
| 3018 | + assert_eq!(register(c, &base, name).await.status(), StatusCode::CREATED); | |
| 3019 | + } | |
| 3020 | + promote(&db, "mgr", "manager").await; | |
| 3021 | + promote(&db, "tch", "teacher").await; | |
| 3022 | + for (c, name) in [(&manager, "mgr"), (&teacher, "tch"), (&student, "veli")] { | |
| 3023 | + login(c, &base, name).await; | |
| 3024 | + } | |
| 3025 | + | |
| 3026 | + let id_of = |v: &Value| v["id"].as_str().unwrap().to_string(); | |
| 3027 | + let student_id = id_of( | |
| 3028 | + &student | |
| 3029 | + .get(format!("{base}/auth/me")) | |
| 3030 | + .send() | |
| 3031 | + .await | |
| 3032 | + .unwrap() | |
| 3033 | + .json::<Value>() | |
| 3034 | + .await | |
| 3035 | + .unwrap(), | |
| 3036 | + ); | |
| 3037 | + | |
| 3038 | + // The office opens the class and puts the student in it. | |
| 3039 | + let res = manager | |
| 3040 | + .post(format!("{base}/classes")) | |
| 3041 | + .json(&json!({ "name": "9-A", "grade": "9" })) | |
| 3042 | + .send() | |
| 3043 | + .await | |
| 3044 | + .unwrap(); | |
| 3045 | + assert_eq!(res.status(), StatusCode::CREATED); | |
| 3046 | + let class = id_of(&res.json::<Value>().await.unwrap()); | |
| 3047 | + let res = manager | |
| 3048 | + .post(format!("{base}/classes/{class}/members")) | |
| 3049 | + .json(&json!({ "user_id": student_id })) | |
| 3050 | + .send() | |
| 3051 | + .await | |
| 3052 | + .unwrap(); | |
| 3053 | + assert_eq!(res.status(), StatusCode::CREATED); | |
| 3054 | + | |
| 3055 | + // The teacher hands their own course to the class … | |
| 3056 | + let res = teacher | |
| 3057 | + .post(format!("{base}/courses")) | |
| 3058 | + .json(&json!({ "title": "Cebir" })) | |
| 3059 | + .send() | |
| 3060 | + .await | |
| 3061 | + .unwrap(); | |
| 3062 | + assert_eq!(res.status(), StatusCode::CREATED); | |
| 3063 | + let course = id_of(&res.json::<Value>().await.unwrap()); | |
| 3064 | + let res = teacher | |
| 3065 | + .post(format!("{base}/classes/{class}/courses")) | |
| 3066 | + .json(&json!({ "course_id": course })) | |
| 3067 | + .send() | |
| 3068 | + .await | |
| 3069 | + .unwrap(); | |
| 3070 | + assert_eq!(res.status(), StatusCode::CREATED); | |
| 3071 | + | |
| 3072 | + // … and the student, whom nobody ever enrolled, is in it. | |
| 3073 | + let mine: Value = student | |
| 3074 | + .get(format!("{base}/courses/me")) | |
| 3075 | + .send() | |
| 3076 | + .await | |
| 3077 | + .unwrap() | |
| 3078 | + .json() | |
| 3079 | + .await | |
| 3080 | + .unwrap(); | |
| 3081 | + assert_eq!(mine["total"], 1, "the class seated them: {mine}"); | |
| 3082 | + assert_eq!(mine["items"][0]["id"], course); | |
| 3083 | + assert_eq!(mine["items"][0]["title"], "Cebir"); | |
| 3084 | + | |
| 3085 | + // Out of the class, out of the course — the seat comes back. | |
| 3086 | + let res = manager | |
| 3087 | + .delete(format!("{base}/classes/{class}/members/{student_id}")) | |
| 3088 | + .send() | |
| 3089 | + .await | |
| 3090 | + .unwrap(); | |
| 3091 | + assert_eq!(res.status(), StatusCode::NO_CONTENT); | |
| 3092 | + let mine: Value = student | |
| 3093 | + .get(format!("{base}/courses/me")) | |
| 3094 | + .send() | |
| 3095 | + .await | |
| 3096 | + .unwrap() | |
| 3097 | + .json() | |
| 3098 | + .await | |
| 3099 | + .unwrap(); | |
| 3100 | + assert_eq!(mine["total"], 0, "the seat went with them: {mine}"); | |
| 3101 | + let roster: Value = teacher | |
| 3102 | + .get(format!("{base}/courses/{course}/enrollments")) | |
| 3103 | + .send() | |
| 3104 | + .await | |
| 3105 | + .unwrap() | |
| 3106 | + .json() | |
| 3107 | + .await | |
| 3108 | + .unwrap(); | |
| 3109 | + assert_eq!(roster["total"], 0, "and off the teacher's roster: {roster}"); | |
| 3110 | +} |
tests/integration.rsmodified+484 -0
| @@ -401,6 +401,17 @@ async fn protected_routes_require_session() { | ||
| 401 | 401 | // The board room authenticates before it upgrades, so a rejection is |
| 402 | 402 | // an HTTP status here and not an instant close. |
| 403 | 403 | ("GET", "/boards/x/ws"), |
| 404 | + ("GET", "/classes"), | |
| 405 | + ("POST", "/classes"), | |
| 406 | + ("GET", "/classes/x"), | |
| 407 | + ("PATCH", "/classes/x"), | |
| 408 | + ("DELETE", "/classes/x"), | |
| 409 | + ("POST", "/classes/x/members"), | |
| 410 | + ("GET", "/classes/x/members"), | |
| 411 | + ("DELETE", "/classes/x/members/u"), | |
| 412 | + ("POST", "/classes/x/courses"), | |
| 413 | + ("GET", "/classes/x/courses"), | |
| 414 | + ("DELETE", "/classes/x/courses/c"), | |
| 404 | 415 | ] { |
| 405 | 416 | let res = send(&app, method, uri, None, None).await; |
| 406 | 417 | assert_eq!(res.status, StatusCode::UNAUTHORIZED, "{method} {uri}"); |
| @@ -25153,3 +25164,476 @@ async fn a_class_pumps_enrollments_and_guards_each_axis_separately() { | ||
| 25153 | 25164 | StatusCode::NOT_FOUND |
| 25154 | 25165 | ); |
| 25155 | 25166 | } |
| 25167 | + | |
| 25168 | +/// Who may look at a class, and the one write a manager holds without teaching | |
| 25169 | +/// anything. Every `/classes` read is teacher+, so the student on the roster — | |
| 25170 | +/// and the parent watching them — is refused their own row; and attaching takes | |
| 25171 | +/// management of the course, which a manager has over every course in the | |
| 25172 | +/// school whether or not they run it. | |
| 25173 | +#[tokio::test] | |
| 25174 | +async fn class_reads_are_staff_only_and_a_manager_manages_every_course() { | |
| 25175 | + let (app, db) = app_and_db().await; | |
| 25176 | + let manager = login_as(&app, &db, "mgr", "manager").await; | |
| 25177 | + let teacher = login_as(&app, &db, "tch", "teacher").await; | |
| 25178 | + let parent = login_as(&app, &db, "anne", "parent").await; | |
| 25179 | + let student = login(&app, "stu").await; | |
| 25180 | + let student_id = me_id(&app, &student).await; | |
| 25181 | + let course = create_course(&app, &teacher, "algebra").await; | |
| 25182 | + | |
| 25183 | + let res = send( | |
| 25184 | + &app, | |
| 25185 | + "POST", | |
| 25186 | + "/classes", | |
| 25187 | + Some(&manager), | |
| 25188 | + Some(json!({ "name": "9-A" })), | |
| 25189 | + ) | |
| 25190 | + .await; | |
| 25191 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 25192 | + let class = id_of(&res.body); | |
| 25193 | + // The student really is on this roster, so the 403s below are the gate | |
| 25194 | + // talking and not an empty database. | |
| 25195 | + let res = send( | |
| 25196 | + &app, | |
| 25197 | + "POST", | |
| 25198 | + &format!("/classes/{class}/members"), | |
| 25199 | + Some(&manager), | |
| 25200 | + Some(json!({ "user_id": student_id })), | |
| 25201 | + ) | |
| 25202 | + .await; | |
| 25203 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 25204 | + | |
| 25205 | + for (who, cookie) in [("a student", &student), ("a parent", &parent)] { | |
| 25206 | + for uri in [ | |
| 25207 | + "/classes".to_string(), | |
| 25208 | + format!("/classes/{class}"), | |
| 25209 | + format!("/classes/{class}/members"), | |
| 25210 | + format!("/classes/{class}/courses"), | |
| 25211 | + ] { | |
| 25212 | + let res = send(&app, "GET", &uri, Some(cookie), None).await; | |
| 25213 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "GET {uri} for {who}"); | |
| 25214 | + } | |
| 25215 | + // …and neither of them writes, on either axis. | |
| 25216 | + let res = send( | |
| 25217 | + &app, | |
| 25218 | + "POST", | |
| 25219 | + &format!("/classes/{class}/members"), | |
| 25220 | + Some(cookie), | |
| 25221 | + Some(json!({ "user_id": student_id })), | |
| 25222 | + ) | |
| 25223 | + .await; | |
| 25224 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "member add for {who}"); | |
| 25225 | + let res = send( | |
| 25226 | + &app, | |
| 25227 | + "POST", | |
| 25228 | + &format!("/classes/{class}/courses"), | |
| 25229 | + Some(cookie), | |
| 25230 | + Some(json!({ "course_id": course })), | |
| 25231 | + ) | |
| 25232 | + .await; | |
| 25233 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "attach for {who}"); | |
| 25234 | + } | |
| 25235 | + | |
| 25236 | + // The manager neither created this course nor teaches it, and still | |
| 25237 | + // attaches it: the bar is management of the course, not authorship of it. | |
| 25238 | + let res = send( | |
| 25239 | + &app, | |
| 25240 | + "POST", | |
| 25241 | + &format!("/classes/{class}/courses"), | |
| 25242 | + Some(&manager), | |
| 25243 | + Some(json!({ "course_id": course })), | |
| 25244 | + ) | |
| 25245 | + .await; | |
| 25246 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 25247 | + // Stored, not merely answered: the link is on the class and the roster moved. | |
| 25248 | + let res = send( | |
| 25249 | + &app, | |
| 25250 | + "GET", | |
| 25251 | + &format!("/classes/{class}/courses"), | |
| 25252 | + Some(&teacher), | |
| 25253 | + None, | |
| 25254 | + ) | |
| 25255 | + .await; | |
| 25256 | + assert_eq!(common::total(&res.body), 1); | |
| 25257 | + assert_eq!(common::items(&res.body)[0]["course"], course); | |
| 25258 | + let res = send( | |
| 25259 | + &app, | |
| 25260 | + "GET", | |
| 25261 | + &format!("/courses/{course}/enrollments"), | |
| 25262 | + Some(&teacher), | |
| 25263 | + None, | |
| 25264 | + ) | |
| 25265 | + .await; | |
| 25266 | + assert_eq!(common::total(&res.body), 1, "{}", res.body); | |
| 25267 | +} | |
| 25268 | + | |
| 25269 | +/// A student promoted out of studenthood leaves their class as well as their | |
| 25270 | +/// rosters. Two sweeps run on that path and each owns exactly half the state — | |
| 25271 | +/// the memberships and the class counter, the enrollment rows and the course | |
| 25272 | +/// counter — so both halves are asserted stored, and then the class is deleted, | |
| 25273 | +/// which only a *released* member counter permits. | |
| 25274 | +#[tokio::test] | |
| 25275 | +async fn a_demotion_sweeps_the_class_membership_and_what_it_pumped() { | |
| 25276 | + let (app, db) = app_and_db().await; | |
| 25277 | + let admin = login_as(&app, &db, "boss", "admin").await; | |
| 25278 | + let manager = login_as(&app, &db, "mgr", "manager").await; | |
| 25279 | + let teacher = login_as(&app, &db, "tch", "teacher").await; | |
| 25280 | + let student = login(&app, "veli").await; | |
| 25281 | + let student_id = me_id(&app, &student).await; | |
| 25282 | + let course = create_course(&app, &teacher, "algebra").await; | |
| 25283 | + | |
| 25284 | + let res = send( | |
| 25285 | + &app, | |
| 25286 | + "POST", | |
| 25287 | + "/classes", | |
| 25288 | + Some(&manager), | |
| 25289 | + Some(json!({ "name": "9-A" })), | |
| 25290 | + ) | |
| 25291 | + .await; | |
| 25292 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 25293 | + let class = id_of(&res.body); | |
| 25294 | + let res = send( | |
| 25295 | + &app, | |
| 25296 | + "POST", | |
| 25297 | + &format!("/classes/{class}/courses"), | |
| 25298 | + Some(&teacher), | |
| 25299 | + Some(json!({ "course_id": course })), | |
| 25300 | + ) | |
| 25301 | + .await; | |
| 25302 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 25303 | + let res = send( | |
| 25304 | + &app, | |
| 25305 | + "POST", | |
| 25306 | + &format!("/classes/{class}/members"), | |
| 25307 | + Some(&manager), | |
| 25308 | + Some(json!({ "user_id": student_id })), | |
| 25309 | + ) | |
| 25310 | + .await; | |
| 25311 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 25312 | + let res = send( | |
| 25313 | + &app, | |
| 25314 | + "GET", | |
| 25315 | + &format!("/courses/{course}/enrollments"), | |
| 25316 | + Some(&teacher), | |
| 25317 | + None, | |
| 25318 | + ) | |
| 25319 | + .await; | |
| 25320 | + assert_eq!(common::total(&res.body), 1, "the class seated them first"); | |
| 25321 | + | |
| 25322 | + // Out of studenthood, through the endpoint an admin actually uses. | |
| 25323 | + let res = send( | |
| 25324 | + &app, | |
| 25325 | + "PATCH", | |
| 25326 | + &format!("/users/{student_id}/role"), | |
| 25327 | + Some(&admin), | |
| 25328 | + Some(json!({ "role": "teacher" })), | |
| 25329 | + ) | |
| 25330 | + .await; | |
| 25331 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 25332 | + | |
| 25333 | + let res = send( | |
| 25334 | + &app, | |
| 25335 | + "GET", | |
| 25336 | + &format!("/classes/{class}/members"), | |
| 25337 | + Some(&teacher), | |
| 25338 | + None, | |
| 25339 | + ) | |
| 25340 | + .await; | |
| 25341 | + assert_eq!(common::total(&res.body), 0, "the roster let them go"); | |
| 25342 | + let res = send( | |
| 25343 | + &app, | |
| 25344 | + "GET", | |
| 25345 | + &format!("/courses/{course}/enrollments"), | |
| 25346 | + Some(&teacher), | |
| 25347 | + None, | |
| 25348 | + ) | |
| 25349 | + .await; | |
| 25350 | + assert_eq!(common::total(&res.body), 0, "and so did the course"); | |
| 25351 | + | |
| 25352 | + // Both tables and both counters, read off the store itself. | |
| 25353 | + let mut result = db | |
| 25354 | + .query( | |
| 25355 | + "SELECT VALUE id FROM class_member; \ | |
| 25356 | + SELECT VALUE id FROM enrollment; \ | |
| 25357 | + SELECT VALUE class_member_count FROM class_group; \ | |
| 25358 | + SELECT VALUE enrollment_count FROM course;", | |
| 25359 | + ) | |
| 25360 | + .await | |
| 25361 | + .expect("read the swept state") | |
| 25362 | + .check() | |
| 25363 | + .expect("read the swept state"); | |
| 25364 | + assert!( | |
| 25365 | + result | |
| 25366 | + .take::<Vec<surrealdb::types::RecordId>>(0) | |
| 25367 | + .expect("class_member rows") | |
| 25368 | + .is_empty(), | |
| 25369 | + "the membership row is deleted, not merely filtered out" | |
| 25370 | + ); | |
| 25371 | + assert!( | |
| 25372 | + result | |
| 25373 | + .take::<Vec<surrealdb::types::RecordId>>(1) | |
| 25374 | + .expect("enrollment rows") | |
| 25375 | + .is_empty(), | |
| 25376 | + "the pumped enrollment is deleted too" | |
| 25377 | + ); | |
| 25378 | + assert_eq!( | |
| 25379 | + result.take::<Vec<i64>>(2).expect("class_member_count"), | |
| 25380 | + vec![0], | |
| 25381 | + "the class counter came back" | |
| 25382 | + ); | |
| 25383 | + assert_eq!( | |
| 25384 | + result.take::<Vec<i64>>(3).expect("enrollment_count"), | |
| 25385 | + vec![0], | |
| 25386 | + "the course counter came back" | |
| 25387 | + ); | |
| 25388 | + | |
| 25389 | + // The bite: a membership left behind (or a counter never released) makes | |
| 25390 | + // the class undeletable forever. | |
| 25391 | + assert_eq!( | |
| 25392 | + send( | |
| 25393 | + &app, | |
| 25394 | + "DELETE", | |
| 25395 | + &format!("/classes/{class}/courses/{course}"), | |
| 25396 | + Some(&teacher), | |
| 25397 | + None, | |
| 25398 | + ) | |
| 25399 | + .await | |
| 25400 | + .status, | |
| 25401 | + StatusCode::NO_CONTENT | |
| 25402 | + ); | |
| 25403 | + let res = send( | |
| 25404 | + &app, | |
| 25405 | + "DELETE", | |
| 25406 | + &format!("/classes/{class}"), | |
| 25407 | + Some(&manager), | |
| 25408 | + None, | |
| 25409 | + ) | |
| 25410 | + .await; | |
| 25411 | + assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); | |
| 25412 | +} | |
| 25413 | + | |
| 25414 | +/// A class files itself under a term exactly as a course does, so the term's | |
| 25415 | +/// delete guard has to count classes too — otherwise deleting the calendar | |
| 25416 | +/// entry would leave the class pointing at nothing. | |
| 25417 | +#[tokio::test] | |
| 25418 | +async fn a_term_a_class_points_at_refuses_to_delete() { | |
| 25419 | + let (app, db) = app_and_db().await; | |
| 25420 | + let manager = login_as(&app, &db, "mgr", "manager").await; | |
| 25421 | + | |
| 25422 | + let res = send( | |
| 25423 | + &app, | |
| 25424 | + "POST", | |
| 25425 | + "/terms", | |
| 25426 | + Some(&manager), | |
| 25427 | + Some(json!({ | |
| 25428 | + "name": "2025 Fall", | |
| 25429 | + "starts_at": 1_600_000_000_000_i64, | |
| 25430 | + "ends_at": 1_610_000_000_000_i64, | |
| 25431 | + })), | |
| 25432 | + ) | |
| 25433 | + .await; | |
| 25434 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 25435 | + let term = id_of(&res.body); | |
| 25436 | + let res = send( | |
| 25437 | + &app, | |
| 25438 | + "POST", | |
| 25439 | + "/classes", | |
| 25440 | + Some(&manager), | |
| 25441 | + Some(json!({ "name": "9-A", "term_id": term })), | |
| 25442 | + ) | |
| 25443 | + .await; | |
| 25444 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 25445 | + assert_eq!(res.body["term"].as_str(), Some(term.as_str())); | |
| 25446 | + let class = id_of(&res.body); | |
| 25447 | + | |
| 25448 | + let res = send( | |
| 25449 | + &app, | |
| 25450 | + "DELETE", | |
| 25451 | + &format!("/terms/{term}"), | |
| 25452 | + Some(&manager), | |
| 25453 | + None, | |
| 25454 | + ) | |
| 25455 | + .await; | |
| 25456 | + assert_eq!( | |
| 25457 | + res.status, | |
| 25458 | + StatusCode::CONFLICT, | |
| 25459 | + "a class still points at it: {}", | |
| 25460 | + res.body | |
| 25461 | + ); | |
| 25462 | + | |
| 25463 | + // Unlinking gives the reference back — an explicit `null` on the named | |
| 25464 | + // field, because a `{}` body short-circuits before any guard runs. | |
| 25465 | + let res = send( | |
| 25466 | + &app, | |
| 25467 | + "PATCH", | |
| 25468 | + &format!("/classes/{class}"), | |
| 25469 | + Some(&manager), | |
| 25470 | + Some(json!({ "term_id": null })), | |
| 25471 | + ) | |
| 25472 | + .await; | |
| 25473 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | |
| 25474 | + assert!(res.body["term"].is_null()); | |
| 25475 | + let res = send( | |
| 25476 | + &app, | |
| 25477 | + "DELETE", | |
| 25478 | + &format!("/terms/{term}"), | |
| 25479 | + Some(&manager), | |
| 25480 | + None, | |
| 25481 | + ) | |
| 25482 | + .await; | |
| 25483 | + assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); | |
| 25484 | + // And the class outlives the term it was filed under. | |
| 25485 | + assert_eq!( | |
| 25486 | + send( | |
| 25487 | + &app, | |
| 25488 | + "GET", | |
| 25489 | + &format!("/classes/{class}"), | |
| 25490 | + Some(&manager), | |
| 25491 | + None | |
| 25492 | + ) | |
| 25493 | + .await | |
| 25494 | + .status, | |
| 25495 | + StatusCode::OK | |
| 25496 | + ); | |
| 25497 | +} | |
| 25498 | + | |
| 25499 | +/// All-or-nothing where a client can see it: a course with one free seat and a | |
| 25500 | +/// class of two takes *neither* of them. The 409 names the course so a manager | |
| 25501 | +/// knows whose capacity to raise, and — the half only stored state can show — | |
| 25502 | +/// the seats spent on the way to that refusal are all given back. | |
| 25503 | +#[tokio::test] | |
| 25504 | +async fn a_course_that_cannot_seat_the_whole_class_seats_none_of_it() { | |
| 25505 | + let (app, db) = app_and_db().await; | |
| 25506 | + let manager = login_as(&app, &db, "mgr", "manager").await; | |
| 25507 | + let teacher = login_as(&app, &db, "tch", "teacher").await; | |
| 25508 | + let hand = login(&app, "hand").await; | |
| 25509 | + let ali = login(&app, "ali").await; | |
| 25510 | + let veli = login(&app, "veli").await; | |
| 25511 | + let hand_id = me_id(&app, &hand).await; | |
| 25512 | + let ali_id = me_id(&app, &ali).await; | |
| 25513 | + let veli_id = me_id(&app, &veli).await; | |
| 25514 | + | |
| 25515 | + // Two seats, one of them already spent by hand: the class of two needs two. | |
| 25516 | + let res = send( | |
| 25517 | + &app, | |
| 25518 | + "POST", | |
| 25519 | + "/courses", | |
| 25520 | + Some(&teacher), | |
| 25521 | + Some(json!({ "title": "small", "capacity": 2 })), | |
| 25522 | + ) | |
| 25523 | + .await; | |
| 25524 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 25525 | + let course = id_of(&res.body); | |
| 25526 | + enroll(&app, &teacher, &course, &hand_id).await; | |
| 25527 | + | |
| 25528 | + let res = send( | |
| 25529 | + &app, | |
| 25530 | + "POST", | |
| 25531 | + "/classes", | |
| 25532 | + Some(&manager), | |
| 25533 | + Some(json!({ "name": "9-A" })), | |
| 25534 | + ) | |
| 25535 | + .await; | |
| 25536 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 25537 | + let class = id_of(&res.body); | |
| 25538 | + for student in [&ali_id, &veli_id] { | |
| 25539 | + let res = send( | |
| 25540 | + &app, | |
| 25541 | + "POST", | |
| 25542 | + &format!("/classes/{class}/members"), | |
| 25543 | + Some(&manager), | |
| 25544 | + Some(json!({ "user_id": student })), | |
| 25545 | + ) | |
| 25546 | + .await; | |
| 25547 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 25548 | + } | |
| 25549 | + | |
| 25550 | + let res = send( | |
| 25551 | + &app, | |
| 25552 | + "POST", | |
| 25553 | + &format!("/classes/{class}/courses"), | |
| 25554 | + Some(&teacher), | |
| 25555 | + Some(json!({ "course_id": course })), | |
| 25556 | + ) | |
| 25557 | + .await; | |
| 25558 | + assert_eq!(res.status, StatusCode::CONFLICT, "{}", res.body); | |
| 25559 | + let message = res.body["error"].as_str().unwrap_or_default(); | |
| 25560 | + assert!( | |
| 25561 | + message.contains(&format!("course:{course}")), | |
| 25562 | + "the refusal must name the full course: {message}" | |
| 25563 | + ); | |
| 25564 | + | |
| 25565 | + // Nothing at all was written: not the seat that did fit, not the link. | |
| 25566 | + let res = send( | |
| 25567 | + &app, | |
| 25568 | + "GET", | |
| 25569 | + &format!("/courses/{course}/enrollments"), | |
| 25570 | + Some(&teacher), | |
| 25571 | + None, | |
| 25572 | + ) | |
| 25573 | + .await; | |
| 25574 | + assert_eq!(common::total(&res.body), 1, "{}", res.body); | |
| 25575 | + assert_eq!( | |
| 25576 | + common::items(&res.body)[0]["user"]["id"], | |
| 25577 | + hand_id, | |
| 25578 | + "only the hand-placed row survives" | |
| 25579 | + ); | |
| 25580 | + let res = send( | |
| 25581 | + &app, | |
| 25582 | + "GET", | |
| 25583 | + &format!("/classes/{class}/courses"), | |
| 25584 | + Some(&teacher), | |
| 25585 | + None, | |
| 25586 | + ) | |
| 25587 | + .await; | |
| 25588 | + assert_eq!(common::total(&res.body), 0); | |
| 25589 | + assert!(common::items(&res.body).is_empty(), "no link row"); | |
| 25590 | + let mut result = db | |
| 25591 | + .query("SELECT VALUE enrollment_count FROM course; SELECT VALUE id FROM class_course;") | |
| 25592 | + .await | |
| 25593 | + .expect("read the refused state") | |
| 25594 | + .check() | |
| 25595 | + .expect("read the refused state"); | |
| 25596 | + assert_eq!( | |
| 25597 | + result.take::<Vec<i64>>(0).expect("enrollment_count"), | |
| 25598 | + vec![1], | |
| 25599 | + "the counter must not have moved" | |
| 25600 | + ); | |
| 25601 | + assert!( | |
| 25602 | + result | |
| 25603 | + .take::<Vec<surrealdb::types::RecordId>>(1) | |
| 25604 | + .expect("class_course rows") | |
| 25605 | + .is_empty(), | |
| 25606 | + ); | |
| 25607 | + | |
| 25608 | + // It really was a seat shortfall: a class of one fits, and lands. | |
| 25609 | + assert_eq!( | |
| 25610 | + send( | |
| 25611 | + &app, | |
| 25612 | + "DELETE", | |
| 25613 | + &format!("/classes/{class}/members/{veli_id}"), | |
| 25614 | + Some(&manager), | |
| 25615 | + None, | |
| 25616 | + ) | |
| 25617 | + .await | |
| 25618 | + .status, | |
| 25619 | + StatusCode::NO_CONTENT | |
| 25620 | + ); | |
| 25621 | + let res = send( | |
| 25622 | + &app, | |
| 25623 | + "POST", | |
| 25624 | + &format!("/classes/{class}/courses"), | |
| 25625 | + Some(&teacher), | |
| 25626 | + Some(json!({ "course_id": course })), | |
| 25627 | + ) | |
| 25628 | + .await; | |
| 25629 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 25630 | + let res = send( | |
| 25631 | + &app, | |
| 25632 | + "GET", | |
| 25633 | + &format!("/courses/{course}/enrollments"), | |
| 25634 | + Some(&teacher), | |
| 25635 | + None, | |
| 25636 | + ) | |
| 25637 | + .await; | |
| 25638 | + assert_eq!(common::total(&res.body), 2, "{}", res.body); | |
| 25639 | +} |
tests/pagination.rsmodified+129 -0
| @@ -529,3 +529,132 @@ async fn board_lists_are_paged() { | ||
| 529 | 529 | assert_eq!(total(&res.body), 3, "the scoped total is the scoped count"); |
| 530 | 530 | assert_eq!(items(&res.body).len(), 1, "offset 2 of 3 leaves 1"); |
| 531 | 531 | } |
| 532 | + | |
| 533 | +/// The class layer's three lists speak the same envelope: the class index, one | |
| 534 | +/// class's roster and the courses it carries. All three join people onto the | |
| 535 | +/// page, so the window is checked with the refs still riding on the rows. | |
| 536 | +#[tokio::test] | |
| 537 | +async fn class_lists_are_paged() { | |
| 538 | + let (app, db) = app_and_db().await; | |
| 539 | + let manager = login_as(&app, &db, "mgr", "manager").await; | |
| 540 | + | |
| 541 | + // Three classes; the first carries three students and three courses. | |
| 542 | + let mut classes = Vec::new(); | |
| 543 | + for n in 0..3 { | |
| 544 | + let res = send( | |
| 545 | + &app, | |
| 546 | + "POST", | |
| 547 | + "/classes", | |
| 548 | + Some(&manager), | |
| 549 | + Some(json!({ "name": format!("9-{n}") })), | |
| 550 | + ) | |
| 551 | + .await; | |
| 552 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 553 | + classes.push(res.body["id"].as_str().unwrap().to_string()); | |
| 554 | + } | |
| 555 | + let class = classes[0].clone(); | |
| 556 | + for n in 0..3 { | |
| 557 | + let course = create_course(&app, &manager, &format!("ders{n}")).await; | |
| 558 | + let res = send( | |
| 559 | + &app, | |
| 560 | + "POST", | |
| 561 | + &format!("/classes/{class}/courses"), | |
| 562 | + Some(&manager), | |
| 563 | + Some(json!({ "course_id": course })), | |
| 564 | + ) | |
| 565 | + .await; | |
| 566 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 567 | + | |
| 568 | + let student = login(&app, &format!("stud{n}")).await; | |
| 569 | + let sid = me_id(&app, &student).await; | |
| 570 | + let res = send( | |
| 571 | + &app, | |
| 572 | + "POST", | |
| 573 | + &format!("/classes/{class}/members"), | |
| 574 | + Some(&manager), | |
| 575 | + Some(json!({ "user_id": sid })), | |
| 576 | + ) | |
| 577 | + .await; | |
| 578 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 579 | + } | |
| 580 | + | |
| 581 | + for uri in [ | |
| 582 | + "/classes".to_string(), | |
| 583 | + format!("/classes/{class}/members"), | |
| 584 | + format!("/classes/{class}/courses"), | |
| 585 | + ] { | |
| 586 | + // Unpaged: everything, `limit` echoes null. | |
| 587 | + let res = send(&app, "GET", &uri, Some(&manager), None).await; | |
| 588 | + assert_eq!(res.status, StatusCode::OK, "GET {uri}: {}", res.body); | |
| 589 | + assert_eq!(total(&res.body), 3, "GET {uri} total"); | |
| 590 | + assert_eq!(items(&res.body).len(), 3, "GET {uri} items"); | |
| 591 | + assert!(res.body["limit"].is_null(), "GET {uri} echoes a null limit"); | |
| 592 | + assert_eq!(res.body["offset"], 0); | |
| 593 | + | |
| 594 | + // A window of 1: the total stays the unpaged count, consecutive windows | |
| 595 | + // are disjoint, and past the end is an empty page rather than an error. | |
| 596 | + let res = send( | |
| 597 | + &app, | |
| 598 | + "GET", | |
| 599 | + &format!("{uri}?limit=1&offset=0"), | |
| 600 | + Some(&manager), | |
| 601 | + None, | |
| 602 | + ) | |
| 603 | + .await; | |
| 604 | + assert_eq!(total(&res.body), 3, "GET {uri} windowed total"); | |
| 605 | + assert_eq!(items(&res.body).len(), 1, "GET {uri} window size"); | |
| 606 | + assert_eq!(res.body["limit"], 1); | |
| 607 | + let first = items(&res.body)[0]["id"].clone(); | |
| 608 | + let res = send( | |
| 609 | + &app, | |
| 610 | + "GET", | |
| 611 | + &format!("{uri}?limit=1&offset=1"), | |
| 612 | + Some(&manager), | |
| 613 | + None, | |
| 614 | + ) | |
| 615 | + .await; | |
| 616 | + assert_eq!(res.body["offset"], 1); | |
| 617 | + assert_ne!(items(&res.body)[0]["id"], first, "GET {uri} pages overlap"); | |
| 618 | + let res = send( | |
| 619 | + &app, | |
| 620 | + "GET", | |
| 621 | + &format!("{uri}?limit=1&offset=99"), | |
| 622 | + Some(&manager), | |
| 623 | + None, | |
| 624 | + ) | |
| 625 | + .await; | |
| 626 | + assert_eq!(res.status, StatusCode::OK); | |
| 627 | + assert!(items(&res.body).is_empty(), "GET {uri} past the end"); | |
| 628 | + assert_eq!(total(&res.body), 3, "GET {uri} total stays honest"); | |
| 629 | + | |
| 630 | + // The shared bounds bite on every one of them. | |
| 631 | + for bad in ["limit=0", "limit=501", "offset=-1"] { | |
| 632 | + let res = send(&app, "GET", &format!("{uri}?{bad}"), Some(&manager), None).await; | |
| 633 | + assert_eq!( | |
| 634 | + res.status, | |
| 635 | + StatusCode::BAD_REQUEST, | |
| 636 | + "GET {uri}?{bad} should be 400" | |
| 637 | + ); | |
| 638 | + } | |
| 639 | + } | |
| 640 | + | |
| 641 | + // The joins run over the page, not the table: each windowed row still | |
| 642 | + // carries the people it names. | |
| 643 | + let res = send( | |
| 644 | + &app, | |
| 645 | + "GET", | |
| 646 | + "/classes?limit=1&offset=0", | |
| 647 | + Some(&manager), | |
| 648 | + None, | |
| 649 | + ) | |
| 650 | + .await; | |
| 651 | + assert_eq!(items(&res.body)[0]["creator"]["username"], "mgr"); | |
| 652 | + let uri = format!("/classes/{class}/members?limit=1&offset=0"); | |
| 653 | + let res = send(&app, "GET", &uri, Some(&manager), None).await; | |
| 654 | + let row = &items(&res.body)[0]; | |
| 655 | + assert!(row["user"]["username"].is_string(), "member ref: {row}"); | |
| 656 | + assert_eq!(row["added_by"]["username"], "mgr"); | |
| 657 | + let uri = format!("/classes/{class}/courses?limit=1&offset=0"); | |
| 658 | + let res = send(&app, "GET", &uri, Some(&manager), None).await; | |
| 659 | + assert_eq!(items(&res.body)[0]["attached_by"]["username"], "mgr"); | |
| 660 | +} |
tests/persistence.rsmodified+122 -0
| @@ -2100,3 +2100,125 @@ async fn boards_and_their_strokes_survive_remigration() { | ||
| 2100 | 2100 | "drawing resumed after the boot" |
| 2101 | 2101 | ); |
| 2102 | 2102 | } |
| 2103 | + | |
| 2104 | +/// Every course's `enrollment_count`, straight off the store. | |
| 2105 | +async fn seats_taken(db: &Database) -> Vec<i64> { | |
| 2106 | + db.query("SELECT VALUE enrollment_count FROM course;") | |
| 2107 | + .await | |
| 2108 | + .expect("read the counter") | |
| 2109 | + .check() | |
| 2110 | + .expect("read the counter") | |
| 2111 | + .take::<Vec<i64>>(0) | |
| 2112 | + .expect("enrollment_count") | |
| 2113 | +} | |
| 2114 | + | |
| 2115 | +/// An `enrollment` row written before the class layer (2026-08-01) carries no | |
| 2116 | +/// `source` key at all, and that absence *is* the meaning — "a person placed | |
| 2117 | +/// this student" — so there is nothing to backfill and everything to preserve. | |
| 2118 | +/// Such a row must still decode once the column exists, still count towards its | |
| 2119 | +/// course's cap, and never be adopted by a class that later attaches the same | |
| 2120 | +/// course: the class skips a pair that already has a row, so the sweep on its | |
| 2121 | +/// way out must find nothing of its own to take. | |
| 2122 | +#[tokio::test] | |
| 2123 | +async fn pre_class_enrollments_keep_their_absent_source() { | |
| 2124 | + let (app, db) = common::app_and_db().await; | |
| 2125 | + let manager = common::login_as(&app, &db, "mgr", "manager").await; | |
| 2126 | + let teacher = common::login_as(&app, &db, "teacher", "teacher").await; | |
| 2127 | + let ali = common::login(&app, "ali").await; | |
| 2128 | + let veli = common::login(&app, "veli").await; | |
| 2129 | + let (ali_id, veli_id) = (me_id(&app, &ali).await, me_id(&app, &veli).await); | |
| 2130 | + | |
| 2131 | + let course = create_course(&app, &teacher, "algebra").await; | |
| 2132 | + enroll(&app, &teacher, &course, &ali_id).await; | |
| 2133 | + enroll(&app, &teacher, &course, &veli_id).await; | |
| 2134 | + | |
| 2135 | + // Age both rows into the pre-class shape — no `source` key — and the course | |
| 2136 | + // into the pre-counter shape a volume that old also carries. | |
| 2137 | + db.query("UPDATE enrollment UNSET source; UPDATE course UNSET enrollment_count;") | |
| 2138 | + .await | |
| 2139 | + .expect("age the rows") | |
| 2140 | + .check() | |
| 2141 | + .expect("age the rows"); | |
| 2142 | + | |
| 2143 | + let app = reboot(&db).await; | |
| 2144 | + | |
| 2145 | + // They decode, and the counter is seeded from them. | |
| 2146 | + let roster = send( | |
| 2147 | + &app, | |
| 2148 | + "GET", | |
| 2149 | + &format!("/courses/{course}/enrollments"), | |
| 2150 | + Some(&teacher), | |
| 2151 | + None, | |
| 2152 | + ) | |
| 2153 | + .await; | |
| 2154 | + assert_eq!(roster.status, StatusCode::OK, "{}", roster.body); | |
| 2155 | + assert_eq!( | |
| 2156 | + common::total(&roster.body), | |
| 2157 | + 2, | |
| 2158 | + "a sourceless row must still read: {}", | |
| 2159 | + roster.body | |
| 2160 | + ); | |
| 2161 | + assert_eq!(seats_taken(&db).await, vec![2], "seeded from the roster"); | |
| 2162 | + | |
| 2163 | + // A class attaching that same course finds ali already seated: no second | |
| 2164 | + // seat is charged, and their row keeps its absent `source`… | |
| 2165 | + let res = send( | |
| 2166 | + &app, | |
| 2167 | + "POST", | |
| 2168 | + "/classes", | |
| 2169 | + Some(&manager), | |
| 2170 | + Some(json!({ "name": "9-A" })), | |
| 2171 | + ) | |
| 2172 | + .await; | |
| 2173 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 2174 | + let class = common::id_of(&res.body); | |
| 2175 | + let res = send( | |
| 2176 | + &app, | |
| 2177 | + "POST", | |
| 2178 | + &format!("/classes/{class}/members"), | |
| 2179 | + Some(&manager), | |
| 2180 | + Some(json!({ "user_id": ali_id })), | |
| 2181 | + ) | |
| 2182 | + .await; | |
| 2183 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 2184 | + let res = send( | |
| 2185 | + &app, | |
| 2186 | + "POST", | |
| 2187 | + &format!("/classes/{class}/courses"), | |
| 2188 | + Some(&manager), | |
| 2189 | + Some(json!({ "course_id": course })), | |
| 2190 | + ) | |
| 2191 | + .await; | |
| 2192 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 2193 | + assert_eq!(seats_taken(&db).await, vec![2], "no second seat charged"); | |
| 2194 | + | |
| 2195 | + // …so the detach sweeps nothing, and the pre-class rows outlive the class. | |
| 2196 | + let res = send( | |
| 2197 | + &app, | |
| 2198 | + "DELETE", | |
| 2199 | + &format!("/classes/{class}/courses/{course}"), | |
| 2200 | + Some(&manager), | |
| 2201 | + None, | |
| 2202 | + ) | |
| 2203 | + .await; | |
| 2204 | + assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); | |
| 2205 | + let roster = send( | |
| 2206 | + &app, | |
| 2207 | + "GET", | |
| 2208 | + &format!("/courses/{course}/enrollments"), | |
| 2209 | + Some(&teacher), | |
| 2210 | + None, | |
| 2211 | + ) | |
| 2212 | + .await; | |
| 2213 | + assert_eq!( | |
| 2214 | + common::total(&roster.body), | |
| 2215 | + 2, | |
| 2216 | + "hand-placed rows are never swept by a class: {}", | |
| 2217 | + roster.body | |
| 2218 | + ); | |
| 2219 | + assert_eq!( | |
| 2220 | + seats_taken(&db).await, | |
| 2221 | + vec![2], | |
| 2222 | + "and no seat was given back" | |
| 2223 | + ); | |
| 2224 | +} |