hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching·0 forks·Rust·8.3 MiB·6d68754·Ahmet Kaan Gümüş·2 months ago·566 commits
| 1 | //! Pagination behavior common to every list endpoint: the `{items, total, |
| 2 | //! limit, offset}` envelope, opt-in windowing, join-on-the-page, and the `400`s |
| 3 | //! for out-of-range parameters. One representative endpoint per shape — the |
| 4 | //! machinery (`web::page`) is shared, so this pins the contract without |
| 5 | //! re-testing all 16 lists. |
| 6 | |
| 7 | mod common; |
| 8 | |
| 9 | use axum::http::StatusCode; |
| 10 | use common::{ |
| 11 | app_and_db, create_course, enroll, items, login, login_as, me_id, send, taught_under, total, |
| 12 | }; |
| 13 | use serde_json::json; |
| 14 | |
| 15 | /// Register `user00..user{n-1}` as plain students. |
| 16 | async fn seed_students(app: &axum::Router, n: usize) { |
| 17 | for i in 0..n { |
| 18 | let name = format!("user{i:02}"); |
| 19 | let creds = json!({ "school": "019732e3-7b00-7000-8000-00000000dead", "username": name, "password": "secret1" }); |
| 20 | let res = send(app, "POST", "/auth/register", None, Some(creds)).await; |
| 21 | assert_eq!(res.status, StatusCode::CREATED, "register {name}"); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | #[tokio::test] |
| 26 | async fn envelope_windows_and_total_is_stable() { |
| 27 | let (app, db) = app_and_db().await; |
| 28 | // 1 admin (to read /users) + 24 students = 25 accounts. |
| 29 | let admin = login_as(&app, &db, "boss", "admin").await; |
| 30 | seed_students(&app, 24).await; |
| 31 | |
| 32 | // Unpaged: the whole list, `limit` echoes null, `total` counts everyone. |
| 33 | let res = send(&app, "GET", "/users", Some(&admin), None).await; |
| 34 | assert_eq!(res.status, StatusCode::OK); |
| 35 | assert_eq!(total(&res.body), 25); |
| 36 | assert_eq!(items(&res.body).len(), 25); |
| 37 | assert!(res.body["limit"].is_null(), "omitted limit echoes null"); |
| 38 | assert_eq!(res.body["offset"], 0); |
| 39 | |
| 40 | // First window of 10. |
| 41 | let res = send(&app, "GET", "/users?limit=10&offset=0", Some(&admin), None).await; |
| 42 | assert_eq!(total(&res.body), 25); |
| 43 | assert_eq!(items(&res.body).len(), 10); |
| 44 | assert_eq!(res.body["limit"], 10); |
| 45 | assert_eq!(res.body["offset"], 0); |
| 46 | let page1: Vec<_> = items(&res.body).iter().map(|u| u["id"].clone()).collect(); |
| 47 | |
| 48 | // Second window — disjoint from the first, identical total. |
| 49 | let res = send(&app, "GET", "/users?limit=10&offset=10", Some(&admin), None).await; |
| 50 | assert_eq!(total(&res.body), 25); |
| 51 | assert_eq!(items(&res.body).len(), 10); |
| 52 | assert_eq!(res.body["offset"], 10); |
| 53 | let page2: Vec<_> = items(&res.body).iter().map(|u| u["id"].clone()).collect(); |
| 54 | assert!( |
| 55 | page2.iter().all(|id| !page1.contains(id)), |
| 56 | "consecutive pages must not overlap" |
| 57 | ); |
| 58 | |
| 59 | // Tail — the remaining 5, even though a full window of 10 was asked for. |
| 60 | let res = send(&app, "GET", "/users?limit=10&offset=20", Some(&admin), None).await; |
| 61 | assert_eq!(items(&res.body).len(), 5, "tail returns only the remainder"); |
| 62 | assert_eq!(total(&res.body), 25); |
| 63 | |
| 64 | // Past the end — an empty page, not an error, and `total` stays honest. |
| 65 | let res = send( |
| 66 | &app, |
| 67 | "GET", |
| 68 | "/users?limit=10&offset=999", |
| 69 | Some(&admin), |
| 70 | None, |
| 71 | ) |
| 72 | .await; |
| 73 | assert_eq!(res.status, StatusCode::OK); |
| 74 | assert!(items(&res.body).is_empty(), "offset past the end is empty"); |
| 75 | assert_eq!(total(&res.body), 25); |
| 76 | } |
| 77 | |
| 78 | #[tokio::test] |
| 79 | async fn bad_paging_params_are_rejected() { |
| 80 | let (app, db) = app_and_db().await; |
| 81 | let admin = login_as(&app, &db, "boss", "admin").await; |
| 82 | |
| 83 | for bad in [ |
| 84 | "limit=0", // below the floor |
| 85 | "limit=501", // above the 500 cap |
| 86 | "limit=-1", // negative |
| 87 | "offset=-1", // negative offset |
| 88 | "limit=abc", // not an integer |
| 89 | ] { |
| 90 | let res = send(&app, "GET", &format!("/users?{bad}"), Some(&admin), None).await; |
| 91 | assert_eq!( |
| 92 | res.status, |
| 93 | StatusCode::BAD_REQUEST, |
| 94 | "GET /users?{bad} should be 400" |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | // The inclusive bounds and a bare offset are accepted. |
| 99 | for ok in ["limit=1", "limit=500", "offset=0", "limit=50&offset=0"] { |
| 100 | let res = send(&app, "GET", &format!("/users?{ok}"), Some(&admin), None).await; |
| 101 | assert_eq!(res.status, StatusCode::OK, "GET /users?{ok} should be 200"); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | #[tokio::test] |
| 106 | async fn paged_roster_still_embeds_people_on_the_page() { |
| 107 | // A list that joins person refs must run that join over the returned page, |
| 108 | // not the whole table — the page rows still carry their embedded people. |
| 109 | let (app, db) = app_and_db().await; |
| 110 | let teacher = login_as(&app, &db, "teacher", "teacher").await; |
| 111 | // The roster keys on the instance — one catalog course as one şube teaches |
| 112 | // it. A şube is school structure, so a manager mints it and names `teacher` |
| 113 | // the homeroom teacher, which is what keeps the teacher's rights over the |
| 114 | // instance (`ensure_instance_teacher`). |
| 115 | let mudur = login_as(&app, &db, "mudur", "manager").await; |
| 116 | let t = taught_under(&app, &mudur, &teacher, "Algebra").await; |
| 117 | |
| 118 | for i in 0..5 { |
| 119 | let student = login(&app, &format!("stud{i}")).await; |
| 120 | let sid = me_id(&app, &student).await; |
| 121 | enroll(&app, &teacher, &t.instance, &sid).await; |
| 122 | } |
| 123 | |
| 124 | let uri = format!("/instances/{}/enrollments?limit=2&offset=0", t.instance); |
| 125 | let res = send(&app, "GET", &uri, Some(&teacher), None).await; |
| 126 | assert_eq!(res.status, StatusCode::OK); |
| 127 | assert_eq!(total(&res.body), 5, "total counts the whole roster"); |
| 128 | let page = items(&res.body); |
| 129 | assert_eq!(page.len(), 2, "only the requested window comes back"); |
| 130 | for row in page { |
| 131 | assert!( |
| 132 | row["user"]["username"].is_string(), |
| 133 | "enrolled student ref present on the page: {row}" |
| 134 | ); |
| 135 | assert!( |
| 136 | row["enrolled_by"]["username"].is_string(), |
| 137 | "enroller ref present on the page: {row}" |
| 138 | ); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /// Far enough ahead to clear the not-in-the-past guard without touching the |
| 143 | /// clock, so the appointment lists below never race the wall clock. |
| 144 | const SLOT_START: i64 = 1_900_000_000_000; |
| 145 | const WEEK: i64 = 7 * 24 * 60 * 60 * 1000; |
| 146 | |
| 147 | /// Publish `n` weekly occurrences as `teacher`; returns their ids, earliest |
| 148 | /// first — the order both appointment lists page in. |
| 149 | async fn publish_slots(app: &axum::Router, teacher: &str, n: i64) -> Vec<String> { |
| 150 | let res = send( |
| 151 | app, |
| 152 | "POST", |
| 153 | "/appointments/slots", |
| 154 | Some(teacher), |
| 155 | Some(json!({ |
| 156 | "starts_at": SLOT_START, |
| 157 | "ends_at": SLOT_START + 1_800_000, |
| 158 | "repeat_weekly": true, |
| 159 | "until": SLOT_START + (n - 1) * WEEK, |
| 160 | })), |
| 161 | ) |
| 162 | .await; |
| 163 | assert_eq!( |
| 164 | res.status, |
| 165 | StatusCode::CREATED, |
| 166 | "publish slots: {}", |
| 167 | res.body |
| 168 | ); |
| 169 | let slots = res |
| 170 | .body |
| 171 | .as_array() |
| 172 | .expect("publish returns an array") |
| 173 | .clone(); |
| 174 | assert_eq!(slots.len() as i64, n, "one row per week"); |
| 175 | slots |
| 176 | .iter() |
| 177 | .map(|slot| slot["id"].as_str().unwrap().to_string()) |
| 178 | .collect() |
| 179 | } |
| 180 | |
| 181 | #[tokio::test] |
| 182 | async fn appointment_slots_are_paged() { |
| 183 | let (app, db) = app_and_db().await; |
| 184 | let teacher = login_as(&app, &db, "ali", "teacher").await; |
| 185 | publish_slots(&app, &teacher, 6).await; |
| 186 | |
| 187 | // Unpaged: the whole calendar, `limit` echoes null, `total` counts it all. |
| 188 | let res = send(&app, "GET", "/appointments/slots", Some(&teacher), None).await; |
| 189 | assert_eq!(res.status, StatusCode::OK, "{}", res.body); |
| 190 | assert_eq!(total(&res.body), 6); |
| 191 | assert_eq!(items(&res.body).len(), 6, "omitting limit returns them all"); |
| 192 | assert!(res.body["limit"].is_null()); |
| 193 | assert_eq!(res.body["offset"], 0); |
| 194 | |
| 195 | // A window: `total` still carries the unpaged count. |
| 196 | let res = send( |
| 197 | &app, |
| 198 | "GET", |
| 199 | "/appointments/slots?limit=2&offset=4", |
| 200 | Some(&teacher), |
| 201 | None, |
| 202 | ) |
| 203 | .await; |
| 204 | assert_eq!(total(&res.body), 6, "total is the unpaged count"); |
| 205 | assert_eq!(items(&res.body).len(), 2); |
| 206 | assert_eq!(res.body["limit"], 2); |
| 207 | assert_eq!(res.body["offset"], 4); |
| 208 | |
| 209 | // Past the end — empty page, honest total, and the person join still rides |
| 210 | // along on the rows that do come back. |
| 211 | let res = send( |
| 212 | &app, |
| 213 | "GET", |
| 214 | "/appointments/slots?limit=2&offset=99", |
| 215 | Some(&teacher), |
| 216 | None, |
| 217 | ) |
| 218 | .await; |
| 219 | assert_eq!(res.status, StatusCode::OK); |
| 220 | assert!(items(&res.body).is_empty()); |
| 221 | assert_eq!(total(&res.body), 6); |
| 222 | |
| 223 | // A student browses the same calendar — bookable slots, paged identically. |
| 224 | let student = login(&app, "ayse").await; |
| 225 | let res = send( |
| 226 | &app, |
| 227 | "GET", |
| 228 | "/appointments/slots?limit=3&offset=0", |
| 229 | Some(&student), |
| 230 | None, |
| 231 | ) |
| 232 | .await; |
| 233 | assert_eq!(res.status, StatusCode::OK, "{}", res.body); |
| 234 | assert_eq!(total(&res.body), 6); |
| 235 | let page = items(&res.body); |
| 236 | assert_eq!(page.len(), 3); |
| 237 | for slot in page { |
| 238 | assert_eq!( |
| 239 | slot["teacher"]["username"], "ali", |
| 240 | "teacher ref present on the page: {slot}" |
| 241 | ); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | #[tokio::test] |
| 246 | async fn appointments_are_paged() { |
| 247 | let (app, db) = app_and_db().await; |
| 248 | let teacher = login_as(&app, &db, "ali", "teacher").await; |
| 249 | let student = login(&app, "ayse").await; |
| 250 | // Five weekly slots, all booked by the same student. Distinct weeks, so no |
| 251 | // booking collides with another. |
| 252 | let slots = publish_slots(&app, &teacher, 5).await; |
| 253 | for slot in &slots { |
| 254 | let res = send( |
| 255 | &app, |
| 256 | "POST", |
| 257 | "/appointments", |
| 258 | Some(&student), |
| 259 | Some(json!({ "slot": slot, "reason": "ödev" })), |
| 260 | ) |
| 261 | .await; |
| 262 | assert_eq!(res.status, StatusCode::CREATED, "book {slot}: {}", res.body); |
| 263 | } |
| 264 | |
| 265 | // Unpaged: every booking the student asked for. |
| 266 | let res = send(&app, "GET", "/appointments", Some(&student), None).await; |
| 267 | assert_eq!(res.status, StatusCode::OK, "{}", res.body); |
| 268 | assert_eq!(total(&res.body), 5); |
| 269 | assert_eq!(items(&res.body).len(), 5, "omitting limit returns them all"); |
| 270 | assert!(res.body["limit"].is_null()); |
| 271 | assert_eq!(res.body["offset"], 0); |
| 272 | |
| 273 | // Consecutive windows are disjoint and share one total. |
| 274 | let res = send( |
| 275 | &app, |
| 276 | "GET", |
| 277 | "/appointments?limit=2&offset=0", |
| 278 | Some(&student), |
| 279 | None, |
| 280 | ) |
| 281 | .await; |
| 282 | assert_eq!(total(&res.body), 5, "total is the unpaged count"); |
| 283 | assert_eq!(res.body["limit"], 2); |
| 284 | let page1: Vec<_> = items(&res.body).iter().map(|a| a["id"].clone()).collect(); |
| 285 | let res = send( |
| 286 | &app, |
| 287 | "GET", |
| 288 | "/appointments?limit=2&offset=2", |
| 289 | Some(&student), |
| 290 | None, |
| 291 | ) |
| 292 | .await; |
| 293 | let page2: Vec<_> = items(&res.body).iter().map(|a| a["id"].clone()).collect(); |
| 294 | assert!(page2.iter().all(|id| !page1.contains(id))); |
| 295 | |
| 296 | // The tail is the remainder, and the slot/person join runs over the page. |
| 297 | let res = send( |
| 298 | &app, |
| 299 | "GET", |
| 300 | "/appointments?limit=2&offset=4", |
| 301 | Some(&student), |
| 302 | None, |
| 303 | ) |
| 304 | .await; |
| 305 | assert_eq!(total(&res.body), 5); |
| 306 | let page = items(&res.body); |
| 307 | assert_eq!(page.len(), 1, "offset 4 of 5 leaves 1"); |
| 308 | assert_eq!(page[0]["requester"]["username"], "ayse"); |
| 309 | assert_eq!(page[0]["teacher"]["username"], "ali"); |
| 310 | |
| 311 | // The teacher's inbox is the same five bookings, paged the same way. |
| 312 | let res = send( |
| 313 | &app, |
| 314 | "GET", |
| 315 | "/appointments?limit=3&offset=0", |
| 316 | Some(&teacher), |
| 317 | None, |
| 318 | ) |
| 319 | .await; |
| 320 | assert_eq!(res.status, StatusCode::OK, "{}", res.body); |
| 321 | assert_eq!(total(&res.body), 5, "the teacher's inbox counts all five"); |
| 322 | assert_eq!(items(&res.body).len(), 3); |
| 323 | } |
| 324 | |
| 325 | #[tokio::test] |
| 326 | async fn user_search_is_paged_not_capped() { |
| 327 | // `/users/search` used to hard-cap at 10 rows; it now pages like the rest. |
| 328 | let (app, db) = app_and_db().await; |
| 329 | let teacher = login_as(&app, &db, "teacher", "teacher").await; |
| 330 | // 12 accounts sharing the "grp" fragment — past the old cap. |
| 331 | for i in 0..12 { |
| 332 | let creds = |
| 333 | json!({ "school": "019732e3-7b00-7000-8000-00000000dead", "username": format!("grp{i:02}"), "password": "secret1" }); |
| 334 | let res = send(&app, "POST", "/auth/register", None, Some(creds)).await; |
| 335 | assert_eq!(res.status, StatusCode::CREATED); |
| 336 | } |
| 337 | |
| 338 | // Unpaged: every match comes back — no 10-row ceiling. |
| 339 | let res = send(&app, "GET", "/users/search?q=grp", Some(&teacher), None).await; |
| 340 | assert_eq!(res.status, StatusCode::OK); |
| 341 | assert_eq!( |
| 342 | total(&res.body), |
| 343 | 12, |
| 344 | "all matches counted, not capped at 10" |
| 345 | ); |
| 346 | assert_eq!(items(&res.body).len(), 12); |
| 347 | |
| 348 | // Windowed like any other list — the tail is the remainder. |
| 349 | let res = send( |
| 350 | &app, |
| 351 | "GET", |
| 352 | "/users/search?q=grp&limit=5&offset=10", |
| 353 | Some(&teacher), |
| 354 | None, |
| 355 | ) |
| 356 | .await; |
| 357 | assert_eq!(total(&res.body), 12); |
| 358 | assert_eq!(items(&res.body).len(), 2, "offset 10 of 12 leaves 2"); |
| 359 | |
| 360 | // Paging bounds are enforced. |
| 361 | let res = send( |
| 362 | &app, |
| 363 | "GET", |
| 364 | "/users/search?q=grp&limit=0", |
| 365 | Some(&teacher), |
| 366 | None, |
| 367 | ) |
| 368 | .await; |
| 369 | assert_eq!(res.status, StatusCode::BAD_REQUEST, "limit=0 is a 400"); |
| 370 | |
| 371 | // A blank `q` is the opening directory, not an error: everyone the caller |
| 372 | // may see — the 12 `grp` accounts plus the teacher themselves. |
| 373 | let res = send(&app, "GET", "/users/search?q=", Some(&teacher), None).await; |
| 374 | assert_eq!(res.status, StatusCode::OK, "{}", res.body); |
| 375 | assert_eq!(total(&res.body), 13, "blank q lists everyone visible"); |
| 376 | |
| 377 | // Blank q scoped to a role is a listing, not an error. |
| 378 | let res = send( |
| 379 | &app, |
| 380 | "GET", |
| 381 | "/users/search?q=&role=student", |
| 382 | Some(&teacher), |
| 383 | None, |
| 384 | ) |
| 385 | .await; |
| 386 | assert_eq!(res.status, StatusCode::OK); |
| 387 | assert_eq!(total(&res.body), 12, "blank q + role lists the whole role"); |
| 388 | let res = send( |
| 389 | &app, |
| 390 | "GET", |
| 391 | "/users/search?q=&role=teacher", |
| 392 | Some(&teacher), |
| 393 | None, |
| 394 | ) |
| 395 | .await; |
| 396 | assert_eq!(res.status, StatusCode::OK); |
| 397 | assert_eq!(total(&res.body), 1, "role scope excludes the students"); |
| 398 | } |
| 399 | |
| 400 | /// The whiteboard's four lists all speak the envelope: the board list, the live |
| 401 | /// canvas, the whole history and the epoch index. `/epochs` is the odd one — |
| 402 | /// it pages in the web layer over a bounded read (`web/boards.rs:391`), so it |
| 403 | /// gets the same windowing proof as the three DB-paged lists. |
| 404 | #[tokio::test] |
| 405 | async fn board_lists_are_paged() { |
| 406 | let (app, db) = app_and_db().await; |
| 407 | let ali = login(&app, "ali").await; |
| 408 | let ali_id = me_id(&app, &ali).await; |
| 409 | |
| 410 | // Three boards, and one of them carries the strokes. |
| 411 | let mut boards = Vec::new(); |
| 412 | for n in 0..3 { |
| 413 | let res = send( |
| 414 | &app, |
| 415 | "POST", |
| 416 | "/boards", |
| 417 | Some(&ali), |
| 418 | Some(json!({ "title": format!("Tahta {n}") })), |
| 419 | ) |
| 420 | .await; |
| 421 | assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); |
| 422 | boards.push(res.body["id"].as_str().unwrap().to_string()); |
| 423 | } |
| 424 | let board = boards[0].clone(); |
| 425 | |
| 426 | // Two closed epochs of 2 marks each, then 5 live ones: 2 markers in the |
| 427 | // index, 11 rows in the history (4 marks + 2 markers + 5 live), 5 on the |
| 428 | // canvas. |
| 429 | for epoch in 0..2 { |
| 430 | for n in 0..2 { |
| 431 | hezarfen_backend::db::board_stroke::append( |
| 432 | &db, |
| 433 | &hezarfen_backend::domain::board::BoardId::from_key(&board), |
| 434 | &hezarfen_backend::domain::user::UserId::from_key(&ali_id), |
| 435 | &format!("{{\"p\":[{epoch},{n}]}}"), |
| 436 | epoch, |
| 437 | ) |
| 438 | .await |
| 439 | .expect("append"); |
| 440 | } |
| 441 | let res = send( |
| 442 | &app, |
| 443 | "POST", |
| 444 | &format!("/boards/{board}/clear"), |
| 445 | Some(&ali), |
| 446 | None, |
| 447 | ) |
| 448 | .await; |
| 449 | assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); |
| 450 | } |
| 451 | for n in 0..5 { |
| 452 | hezarfen_backend::db::board_stroke::append( |
| 453 | &db, |
| 454 | &hezarfen_backend::domain::board::BoardId::from_key(&board), |
| 455 | &hezarfen_backend::domain::user::UserId::from_key(&ali_id), |
| 456 | &format!("{{\"p\":[2,{n}]}}"), |
| 457 | 2, |
| 458 | ) |
| 459 | .await |
| 460 | .expect("append"); |
| 461 | } |
| 462 | |
| 463 | for (uri, count) in [ |
| 464 | ("/boards".to_string(), 3), |
| 465 | (format!("/boards/{board}/strokes"), 5), |
| 466 | (format!("/boards/{board}/history"), 11), |
| 467 | (format!("/boards/{board}/epochs"), 2), |
| 468 | ] { |
| 469 | // Unpaged: everything, `limit` echoes null. |
| 470 | let res = send(&app, "GET", &uri, Some(&ali), None).await; |
| 471 | assert_eq!(res.status, StatusCode::OK, "GET {uri}: {}", res.body); |
| 472 | assert_eq!(total(&res.body), count, "GET {uri} total"); |
| 473 | assert_eq!(items(&res.body).len() as i64, count, "GET {uri} items"); |
| 474 | assert!(res.body["limit"].is_null(), "GET {uri} echoes a null limit"); |
| 475 | assert_eq!(res.body["offset"], 0); |
| 476 | |
| 477 | // A window of 1: the total stays the unpaged count, the windows are |
| 478 | // disjoint, and the page past the end is empty rather than an error. |
| 479 | let res = send( |
| 480 | &app, |
| 481 | "GET", |
| 482 | &format!("{uri}?limit=1&offset=0"), |
| 483 | Some(&ali), |
| 484 | None, |
| 485 | ) |
| 486 | .await; |
| 487 | assert_eq!(total(&res.body), count, "GET {uri} windowed total"); |
| 488 | assert_eq!(items(&res.body).len(), 1, "GET {uri} window size"); |
| 489 | assert_eq!(res.body["limit"], 1); |
| 490 | let first = items(&res.body)[0]["id"].clone(); |
| 491 | let res = send( |
| 492 | &app, |
| 493 | "GET", |
| 494 | &format!("{uri}?limit=1&offset=1"), |
| 495 | Some(&ali), |
| 496 | None, |
| 497 | ) |
| 498 | .await; |
| 499 | assert_eq!(res.body["offset"], 1); |
| 500 | assert_ne!(items(&res.body)[0]["id"], first, "GET {uri} pages overlap"); |
| 501 | let res = send( |
| 502 | &app, |
| 503 | "GET", |
| 504 | &format!("{uri}?limit=1&offset=99"), |
| 505 | Some(&ali), |
| 506 | None, |
| 507 | ) |
| 508 | .await; |
| 509 | assert_eq!(res.status, StatusCode::OK); |
| 510 | assert!(items(&res.body).is_empty(), "GET {uri} past the end"); |
| 511 | assert_eq!(total(&res.body), count, "GET {uri} total stays honest"); |
| 512 | |
| 513 | // The shared bounds bite on every one of them. |
| 514 | for bad in ["limit=0", "limit=501", "offset=-1"] { |
| 515 | let res = send(&app, "GET", &format!("{uri}?{bad}"), Some(&ali), None).await; |
| 516 | assert_eq!( |
| 517 | res.status, |
| 518 | StatusCode::BAD_REQUEST, |
| 519 | "GET {uri}?{bad} should be 400" |
| 520 | ); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // `?epoch=` narrows the history and still pages: epoch 0 is 2 marks plus |
| 525 | // the marker that closed it. |
| 526 | let uri = format!("/boards/{board}/history?epoch=0"); |
| 527 | let res = send(&app, "GET", &uri, Some(&ali), None).await; |
| 528 | assert_eq!(total(&res.body), 3, "{}", res.body); |
| 529 | let res = send( |
| 530 | &app, |
| 531 | "GET", |
| 532 | &format!("{uri}&limit=2&offset=2"), |
| 533 | Some(&ali), |
| 534 | None, |
| 535 | ) |
| 536 | .await; |
| 537 | assert_eq!(total(&res.body), 3, "the scoped total is the scoped count"); |
| 538 | assert_eq!(items(&res.body).len(), 1, "offset 2 of 3 leaves 1"); |
| 539 | |
| 540 | // `?open=` narrows the board list the same way `?read=` narrows a message |
| 541 | // folder: the filter reaches the database, so `total` is the filtered count |
| 542 | // and the window still pages. A closed board is never deleted, so this is |
| 543 | // the only way a heavy creator trims the retired ones out. |
| 544 | let res = send( |
| 545 | &app, |
| 546 | "POST", |
| 547 | &format!("/boards/{}/close", boards[2]), |
| 548 | Some(&ali), |
| 549 | None, |
| 550 | ) |
| 551 | .await; |
| 552 | assert_eq!(res.status, StatusCode::OK, "{}", res.body); |
| 553 | for (query, count) in [("", 3), ("?open=true", 2), ("?open=false", 1)] { |
| 554 | let res = send(&app, "GET", &format!("/boards{query}"), Some(&ali), None).await; |
| 555 | assert_eq!( |
| 556 | res.status, |
| 557 | StatusCode::OK, |
| 558 | "GET /boards{query}: {}", |
| 559 | res.body |
| 560 | ); |
| 561 | assert_eq!(total(&res.body), count, "GET /boards{query} total"); |
| 562 | assert_eq!(items(&res.body).len() as i64, count, "GET /boards{query}"); |
| 563 | } |
| 564 | // The board that is merely *locked* is still open — `?open=` filters |
| 565 | // `closed_at` and nothing else. |
| 566 | let res = send( |
| 567 | &app, |
| 568 | "PATCH", |
| 569 | &format!("/boards/{}", boards[1]), |
| 570 | Some(&ali), |
| 571 | Some(json!({ "locked": true })), |
| 572 | ) |
| 573 | .await; |
| 574 | assert_eq!(res.status, StatusCode::OK, "{}", res.body); |
| 575 | let res = send(&app, "GET", "/boards?open=true&limit=1", Some(&ali), None).await; |
| 576 | assert_eq!(total(&res.body), 2, "a locked board is still an open one"); |
| 577 | assert_eq!(items(&res.body).len(), 1, "the filtered list still windows"); |
| 578 | assert_eq!(res.body["limit"], 1); |
| 579 | } |
| 580 | |
| 581 | /// The class layer's three lists speak the same envelope: the class index, one |
| 582 | /// class's roster and the instances it carries. All three join people onto the |
| 583 | /// page, so the window is checked with the refs still riding on the rows. |
| 584 | #[tokio::test] |
| 585 | async fn class_lists_are_paged() { |
| 586 | let (app, db) = app_and_db().await; |
| 587 | let manager = login_as(&app, &db, "mgr", "manager").await; |
| 588 | |
| 589 | // Three classes; the first carries three students and three courses. |
| 590 | let mut classes = Vec::new(); |
| 591 | for n in 0..3 { |
| 592 | let res = send( |
| 593 | &app, |
| 594 | "POST", |
| 595 | "/classes", |
| 596 | Some(&manager), |
| 597 | Some(json!({ "name": format!("9-{n}"), "grade_level": 9 })), |
| 598 | ) |
| 599 | .await; |
| 600 | assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); |
| 601 | classes.push(res.body["class"]["id"].as_str().unwrap().to_string()); |
| 602 | } |
| 603 | let class = classes[0].clone(); |
| 604 | for n in 0..3 { |
| 605 | let course = create_course(&app, &manager, &format!("ders{n}")).await; |
| 606 | let res = send( |
| 607 | &app, |
| 608 | "POST", |
| 609 | &format!("/classes/{class}/instances"), |
| 610 | Some(&manager), |
| 611 | Some(json!({ "course_id": course })), |
| 612 | ) |
| 613 | .await; |
| 614 | assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); |
| 615 | |
| 616 | let student = login(&app, &format!("stud{n}")).await; |
| 617 | let sid = me_id(&app, &student).await; |
| 618 | let res = send( |
| 619 | &app, |
| 620 | "POST", |
| 621 | &format!("/classes/{class}/members"), |
| 622 | Some(&manager), |
| 623 | Some(json!({ "user_id": sid })), |
| 624 | ) |
| 625 | .await; |
| 626 | assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); |
| 627 | } |
| 628 | |
| 629 | for uri in [ |
| 630 | "/classes".to_string(), |
| 631 | format!("/classes/{class}/members"), |
| 632 | format!("/classes/{class}/instances"), |
| 633 | ] { |
| 634 | // Unpaged: everything, `limit` echoes null. |
| 635 | let res = send(&app, "GET", &uri, Some(&manager), None).await; |
| 636 | assert_eq!(res.status, StatusCode::OK, "GET {uri}: {}", res.body); |
| 637 | assert_eq!(total(&res.body), 3, "GET {uri} total"); |
| 638 | assert_eq!(items(&res.body).len(), 3, "GET {uri} items"); |
| 639 | assert!(res.body["limit"].is_null(), "GET {uri} echoes a null limit"); |
| 640 | assert_eq!(res.body["offset"], 0); |
| 641 | |
| 642 | // A window of 1: the total stays the unpaged count, consecutive windows |
| 643 | // are disjoint, and past the end is an empty page rather than an error. |
| 644 | let res = send( |
| 645 | &app, |
| 646 | "GET", |
| 647 | &format!("{uri}?limit=1&offset=0"), |
| 648 | Some(&manager), |
| 649 | None, |
| 650 | ) |
| 651 | .await; |
| 652 | assert_eq!(total(&res.body), 3, "GET {uri} windowed total"); |
| 653 | assert_eq!(items(&res.body).len(), 1, "GET {uri} window size"); |
| 654 | assert_eq!(res.body["limit"], 1); |
| 655 | let first = items(&res.body)[0]["id"].clone(); |
| 656 | let res = send( |
| 657 | &app, |
| 658 | "GET", |
| 659 | &format!("{uri}?limit=1&offset=1"), |
| 660 | Some(&manager), |
| 661 | None, |
| 662 | ) |
| 663 | .await; |
| 664 | assert_eq!(res.body["offset"], 1); |
| 665 | assert_ne!(items(&res.body)[0]["id"], first, "GET {uri} pages overlap"); |
| 666 | let res = send( |
| 667 | &app, |
| 668 | "GET", |
| 669 | &format!("{uri}?limit=1&offset=99"), |
| 670 | Some(&manager), |
| 671 | None, |
| 672 | ) |
| 673 | .await; |
| 674 | assert_eq!(res.status, StatusCode::OK); |
| 675 | assert!(items(&res.body).is_empty(), "GET {uri} past the end"); |
| 676 | assert_eq!(total(&res.body), 3, "GET {uri} total stays honest"); |
| 677 | |
| 678 | // The shared bounds bite on every one of them. |
| 679 | for bad in ["limit=0", "limit=501", "offset=-1"] { |
| 680 | let res = send(&app, "GET", &format!("{uri}?{bad}"), Some(&manager), None).await; |
| 681 | assert_eq!( |
| 682 | res.status, |
| 683 | StatusCode::BAD_REQUEST, |
| 684 | "GET {uri}?{bad} should be 400" |
| 685 | ); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | // The joins run over the page, not the table: each windowed row still |
| 690 | // carries the people it names. |
| 691 | let res = send( |
| 692 | &app, |
| 693 | "GET", |
| 694 | "/classes?limit=1&offset=0", |
| 695 | Some(&manager), |
| 696 | None, |
| 697 | ) |
| 698 | .await; |
| 699 | assert_eq!(items(&res.body)[0]["creator"]["username"], "mgr"); |
| 700 | let uri = format!("/classes/{class}/members?limit=1&offset=0"); |
| 701 | let res = send(&app, "GET", &uri, Some(&manager), None).await; |
| 702 | let row = &items(&res.body)[0]; |
| 703 | assert!(row["user"]["username"].is_string(), "member ref: {row}"); |
| 704 | assert_eq!(row["added_by"]["username"], "mgr"); |
| 705 | let uri = format!("/classes/{class}/instances?limit=1&offset=0"); |
| 706 | let res = send(&app, "GET", &uri, Some(&manager), None).await; |
| 707 | assert_eq!(items(&res.body)[0]["attached_by"]["username"], "mgr"); |
| 708 | } |