//! Shared helpers for the router-level test binaries (`integration`, //! `persistence`, and friends). Drives an `axum::Router` in-process via //! `tower::ServiceExt::oneshot`, over a **per-test pair of Postgres //! databases** — control plus demo school, cloned from the shared school //! template by `database::init_test_tenants`, dropped with the test. #![allow(dead_code)] use std::path::PathBuf; use std::sync::OnceLock; use axum::Router; use axum::body::Body; use axum::http::{HeaderMap, Request, StatusCode}; use hezarfen_backend::ai::AiBridge; use hezarfen_backend::database::Database; use hezarfen_backend::module::ModuleSet; use hezarfen_backend::rate_limit::RateLimitConfig; use hezarfen_backend::state::AppState; use hezarfen_backend::telemetry::Metrics; use hezarfen_backend::tenant::{BETA_SCHOOL_ID, DEMO_SCHOOL_ID, SchoolId, Tenants}; use hezarfen_backend::{build_router, database}; use serde_json::{Value, json}; use tempfile::TempDir; use tower::ServiceExt; pub struct Res { pub status: StatusCode, pub body: Value, /// The `session=` pair, if the response set one. pub cookie: Option, } /// A well-formed id for a row that exists nowhere — the canonical ghost /// parent every FK-refusal test aims at. Replaces the ULID literal /// `01J8XZ0K3Q8G7X2M4N5P6R7S8T` the suites used before the UUID v7 port. pub const GHOST_ID: &str = "019732e3-7b00-7000-8000-00000000dead"; /// A second well-formed, never-minted id, for "look this up, expect a miss". /// Replaces `01ZZZZZZZZZZZZZZZZZZZZZZZZ`. pub const ABSENT_ID: &str = "019732e3-7b00-7000-8000-00000000eeee"; /// One shared blob directory for every app in this test process. Blob names /// are per-row ids, so apps never collide; the static keeps the `TempDir` /// (and so the directory) alive for the whole run. static FILES_DIR: OnceLock = OnceLock::new(); pub fn files_dir() -> PathBuf { FILES_DIR .get_or_init(|| tempfile::tempdir().expect("files tempdir")) .path() .to_path_buf() } /// A router plus a handle to its demo school's database. Tests that need to /// grant roles use the handle to seed them directly — the app's only role /// bootstrap path is out-of-band, exactly like production's manual SQL. pub async fn app_and_db() -> (Router, Database) { app_with_ai(None).await } /// [`app_and_db`] plus the registry behind it, for the suites whose subject is /// tenancy itself (a second school, a suspension, the builder surface). pub async fn app_and_tenants() -> (Router, Database, Tenants) { let (app, db, tenants) = app_parts(None, Metrics::noop()).await; (app, db, tenants) } /// The hyphenated uuid a test label names. `demo` and `beta` are pinned to /// the ids [`init_test_tenants`] / [`deployment_with`] register; a value that /// is already a uuid is returned unchanged. pub fn school_wire(label: &str) -> &str { match label { "demo" => DEMO_SCHOOL_ID, "beta" => BETA_SCHOOL_ID, "ata-koleji" => "019732e3-7b00-7000-8000-00000000a7a1", "2024school" => "019732e3-7b00-7000-8000-000000002024", "gamma" => "019732e3-7b00-7000-8000-00000000a11a", "p4a" => "019732e3-7b00-7000-8000-00000000a4a1", other => other, } } pub fn school_id(label: &str) -> SchoolId { SchoolId::try_parse(school_wire(label)).unwrap_or_else(|_| panic!("{label} is not a school id")) } /// Where the demo school's blobs land: one directory per school under /// `FILES_PATH`, created on the school's first upload. pub fn blob_dir() -> PathBuf { let dir = files_dir().join(DEMO_SCHOOL_ID); std::fs::create_dir_all(&dir).expect("blob dir"); dir } /// The raw session token out of a `session=.` cookie value — /// what a test needs when it writes a session row by hand. pub fn cookie_token(cookie: &str) -> &str { cookie .trim_start_matches("session=") .split_once('.') .expect("a session cookie carries its school") .1 } /// A test deployment: the registry, plus the demo school's handle. For the /// suites that build their own `AppState` instead of using [`app_and_db`]. /// (The name outlived the in-memory engine; the shape is what the suites /// bind to.) pub async fn mem_deployment() -> (Tenants, Database) { let tenants = database::init_test_tenants().await; let db = demo_db(&tenants).await; (tenants, db) } /// The demo school's handle out of a registry. pub async fn demo_db(tenants: &Tenants) -> Database { tenants .get(&SchoolId::try_parse(DEMO_SCHOOL_ID).expect("the demo school")) .await .expect("the demo school resolves") } /// The same router, with the AI bridge wired into its state. `build_router` /// arms the bridge's api-read handle whenever `ai` is `Some`, so this is the /// bootstrap that makes a QUIC service's reads dispatch into a real router. pub async fn app_with_ai(ai: Option) -> (Router, Database) { let (app, db, _) = app_parts(ai, Metrics::noop()).await; (app, db) } /// [`app_with_ai`] plus the registry behind it, for the suites whose subject is /// an AI service reading *across* schools. pub async fn app_with_ai_tenants(ai: Option) -> (Router, Database, Tenants) { app_parts(ai, Metrics::noop()).await } /// The one bootstrap: a fresh test deployment (control database + the demo /// school), a router over it, and the demo school's handle — which is what /// every suite means by "the database", since that is where users and rows /// live. async fn app_parts(ai: Option, metrics: Metrics) -> (Router, Database, Tenants) { let tenants = database::init_test_tenants().await; let db = demo_db(&tenants).await; let app = build_router(AppState { db: tenants.control().clone(), tenants: tenants.clone(), files_path: files_dir(), cookie_secure: false, // Off, so suites hammering the API never trip a limit; the dedicated // `rate_limit` test binary opts into tight configs on purpose. rate_limit: RateLimitConfig::unlimited(), chatbot_limit: Default::default(), rag_limit: Default::default(), exam_presence: Default::default(), board_hub: Default::default(), ai, metrics, }); (app, db, tenants) } /// The same router with the caller's instruments, so a test can read back what /// the HTTP edge recorded without touching the process-global meter provider. pub async fn app_with_metrics(metrics: Metrics) -> (Router, Database) { let (app, db, _) = app_parts(None, metrics).await; (app, db) } /// Attribute keys that would carry personal data out of the process. KVKK /// analysis fixed this list: telemetry names the school and the route, never /// the person, their address, or what they sent. Shared so the HTTP edge /// (`telemetry.rs`) and the AI bridge (`ai_bridge.rs`) are held to one list. /// /// `server.address` is deliberately absent: that is our own bind address, not /// a caller's. pub const FORBIDDEN_TELEMETRY_KEYS: &[&str] = &[ "url.path", "url.full", "url.query", "client.address", "network.peer.address", "user_agent.original", "user.id", "user.name", "username", "email", "enduser.id", "cookie", "http.request.body", "http.response.body", ]; /// [`FORBIDDEN_TELEMETRY_KEYS`] plus the two header prefixes — any captured /// request or response header is personal data by default. pub fn is_forbidden_key(key: &str) -> bool { FORBIDDEN_TELEMETRY_KEYS.contains(&key) || key.starts_with("http.request.header.") || key.starts_with("http.response.header.") } /// A router backed by a fresh per-test Postgres deployment. pub async fn mem_app() -> Router { app_and_db().await.0 } /// Force `username`'s role, mirroring the manual `UPDATE app_user SET /// role=...` bootstrap. Roles are read fresh per request, so this takes /// effect at once. pub async fn set_role(db: &Database, username: &str, role: &str) { sqlx::query("UPDATE app_user SET role = $2 WHERE username = $1") .bind(username) .bind(role) .execute(db) .await .expect("set_role query"); } /// Register (password `secret1`), promote to `role`, then log in. Returns the /// session `Cookie` value. pub async fn login_as(app: &Router, db: &Database, username: &str, role: &str) -> String { login_as_school(app, db, DEMO_SCHOOL_ID, username, role).await } /// [`login_as`] in a named school. `db` must be that school's own handle — the /// role is written where the user was created, which is what makes two schools /// able to hold the same username independently. pub async fn login_as_school( app: &Router, db: &Database, school: &str, username: &str, role: &str, ) -> String { let reg = send( app, "POST", "/auth/register", None, Some(json!({ "school": school_wire(school), "username": username, "password": "secret1" })), ) .await; assert_eq!(reg.status, StatusCode::CREATED, "register {username}"); set_role(db, username, role).await; let res = send( app, "POST", "/auth/login", None, Some(json!({ "username": username, "password": "secret1" })), ) .await; assert_eq!(res.status, StatusCode::OK, "login {username}"); // The account is a person: if `username` already belongs to another // school, login answers a choice list and never names a school — bind // the school this helper was called for. if res.body["schools"].is_array() { let selected = send( app, "POST", "/auth/school", res.cookie.as_deref(), Some(json!({ "school": school_wire(school) })), ) .await; assert_eq!( selected.status, StatusCode::OK, "select {school} for {username}: {}", selected.body ); selected.cookie.expect("school cookie after selection") } else { res.cookie.expect("session cookie set on login") } } /// Send one request through `app`. `cookie` is a raw `Cookie` header value. pub async fn send( app: &Router, method: &str, uri: &str, cookie: Option<&str>, body: Option, ) -> Res { let mut builder = Request::builder().method(method).uri(uri); if let Some(c) = cookie { builder = builder.header("cookie", c); } let request = match body { Some(b) => builder .header("content-type", "application/json") .body(Body::from(b.to_string())) .unwrap(), None => builder.body(Body::empty()).unwrap(), }; let response = app.clone().oneshot(request).await.unwrap(); let status = response.status(); let cookie = response .headers() .get("set-cookie") .and_then(|v| v.to_str().ok()) .and_then(|s| s.split(';').next()) .map(|s| s.to_string()); let bytes = axum::body::to_bytes(response.into_body(), usize::MAX) .await .unwrap(); let body = if bytes.is_empty() { Value::Null } else { serde_json::from_slice(&bytes).unwrap_or(Value::Null) }; Res { status, body, cookie, } } /// Send one request with a raw body and content type; returns the raw /// response (status, headers, body bytes) — for driving the file endpoints. pub async fn send_raw( app: &Router, method: &str, uri: &str, cookie: Option<&str>, content_type: Option<&str>, body: Vec, ) -> (StatusCode, HeaderMap, Vec) { let mut builder = Request::builder().method(method).uri(uri); if let Some(c) = cookie { builder = builder.header("cookie", c); } if let Some(ct) = content_type { builder = builder.header("content-type", ct); } let response = app .clone() .oneshot(builder.body(Body::from(body)).unwrap()) .await .unwrap(); let status = response.status(); let headers = response.headers().clone(); let bytes = axum::body::to_bytes(response.into_body(), usize::MAX) .await .unwrap() .to_vec(); (status, headers, bytes) } const MULTIPART_BOUNDARY: &str = "hezarfen-test-boundary"; /// A `multipart/form-data` body with a single `file` part. pub fn multipart_file(filename: &str, content_type: &str, bytes: &[u8]) -> Vec { let mut body = Vec::new(); body.extend_from_slice(format!("--{MULTIPART_BOUNDARY}\r\n").as_bytes()); body.extend_from_slice( format!("Content-Disposition: form-data; name=\"file\"; filename=\"{filename}\"\r\n") .as_bytes(), ); body.extend_from_slice(format!("Content-Type: {content_type}\r\n\r\n").as_bytes()); body.extend_from_slice(bytes); body.extend_from_slice(format!("\r\n--{MULTIPART_BOUNDARY}--\r\n").as_bytes()); body } /// Upload `bytes` as a multipart file to `uri` (no assertion). Parses the /// JSON body like `send`. pub async fn upload_file_at( app: &Router, cookie: &str, uri: &str, filename: &str, content_type: &str, bytes: &[u8], ) -> Res { let (status, _, body) = send_raw( app, "POST", uri, Some(cookie), Some(&format!( "multipart/form-data; boundary={MULTIPART_BOUNDARY}" )), multipart_file(filename, content_type, bytes), ) .await; let body = if body.is_empty() { Value::Null } else { serde_json::from_slice(&body).unwrap_or(Value::Null) }; Res { status, body, cookie: None, } } /// Upload `bytes` as a file onto a personal `note` (no assertion). pub async fn upload_file( app: &Router, cookie: &str, note: &str, filename: &str, content_type: &str, bytes: &[u8], ) -> Res { upload_file_at( app, cookie, &format!("/notes/{note}/files"), filename, content_type, bytes, ) .await } /// Upload `bytes` as a file onto a course `note` (no assertion). pub async fn upload_course_note_file( app: &Router, cookie: &str, note: &str, filename: &str, content_type: &str, bytes: &[u8], ) -> Res { upload_file_at( app, cookie, &format!("/course-notes/{note}/files"), filename, content_type, bytes, ) .await } /// Register (password `secret1`) then log in; returns the session `Cookie` value. pub async fn login(app: &Router, username: &str) -> String { let reg = send( app, "POST", "/auth/register", None, Some(json!({ "school": DEMO_SCHOOL_ID, "username": username, "password": "secret1" })), ) .await; assert_eq!(reg.status, StatusCode::CREATED, "register {username}"); let res = send( app, "POST", "/auth/login", None, Some(json!({ "username": username, "password": "secret1" })), ) .await; assert_eq!(res.status, StatusCode::OK, "login {username}"); res.cookie.expect("session cookie set on login") } pub fn id_of(v: &Value) -> String { v["id"].as_str().expect("id field").to_string() } /// The `items` array of a paginated list envelope (`{items, total, limit, /// offset}`) — the shape every `GET` list endpoint returns. Panics if `v` /// isn't such an envelope, so a test that points it at the wrong body fails /// loudly. pub fn items(v: &Value) -> &Vec { v["items"] .as_array() .unwrap_or_else(|| panic!("expected a paginated list envelope with items[], got {v}")) } /// The `total` count from a paginated list envelope. pub fn total(v: &Value) -> i64 { v["total"].as_i64().expect("paginated envelope total") } /// The caller's own user id via `GET /auth/me`. pub async fn me_id(app: &Router, cookie: &str) -> String { let res = send(app, "GET", "/auth/me", Some(cookie), None).await; assert_eq!(res.status, StatusCode::OK, "GET /auth/me"); id_of(&res.body) } /// Create a **catalog** course as `cookie` (asserts 201); returns its id. /// /// The catalog is the title and the subjects hung off it. What one şube teaches /// is an *instance* (`POST /classes/{class}/instances`), and enrollment, exams, /// sessions and homework key on that instance, never on this row — /// [`taught`] is the fixture that mints the whole stack. pub async fn create_course(app: &Router, cookie: &str, title: &str) -> String { let res = send( app, "POST", "/courses", Some(cookie), Some(json!({ "title": title })), ) .await; assert_eq!(res.status, StatusCode::CREATED, "create course {title}"); id_of(&res.body) } /// Create a subject inside `course` as `cookie` (asserts 201); returns its id. /// Every exam question must be tagged with one of its course's subjects. pub async fn create_subject(app: &Router, cookie: &str, course: &str, name: &str) -> String { let res = send( app, "POST", &format!("/courses/{course}/subjects"), Some(cookie), Some(json!({ "name": name })), ) .await; assert_eq!(res.status, StatusCode::CREATED, "create subject {name}"); let subject = id_of(&res.body); // The subject-tag gates (homework, exam questions) read the section's // resolved syllabus, so a topic a suite creates is only usable once the // offering selects it. When the course has been attached somewhere, the // offering exists — select the fresh topic there (idempotent; a // non-manager cookie cannot curate the offering and skips this, which // the tag gates then surface as their own 400). let res = send( app, "GET", &format!("/offerings?course={course}"), Some(cookie), None, ) .await; if res.status == StatusCode::OK && let Some(offering) = res.body["items"][0]["id"].as_str() { let res = send( app, "POST", &format!("/offerings/{offering}/subjects"), Some(cookie), Some(json!({ "subject": subject })), ) .await; // A non-manager cookie cannot curate the offering; the tag gates // then answer their own 400 and the caller selects another way // (see `create_homework`'s instance-door retry). assert!( matches!(res.status, StatusCode::CREATED | StatusCode::OK | StatusCode::FORBIDDEN), "select subject {name} on offering {offering}: {}", res.body ); } subject } // ---- the academic fixture --------------------------------------------------- // // The K12 remodel made the class×course **instance** the academic anchor: a // şube (class) teaches a catalog course, and that pair is one `class_course` row // every exam, session, homework, roster and karne keys on. A suite that needs a // roster to exist therefore mints three rows, not one — and the şube is school // structure, so whoever creates it holds manager+ (the course itself stays the // teacher's: the catalog is teacher-owned). /// The bounds every fixture calendar uses: one year-wide span, fixed so two /// tests cannot race a clock. pub const FIXTURE_YEAR_STARTS_AT: i64 = 1_750_000_000_000; pub const FIXTURE_YEAR_ENDS_AT: i64 = 1_800_000_000_000; /// Create an academic year as `staff` (asserts 201); returns its id. `staff` /// must hold manager+. pub async fn create_year(app: &Router, staff: &str, name: &str) -> String { let res = send( app, "POST", "/academic-years", Some(staff), Some(json!({ "name": name, "starts_at": FIXTURE_YEAR_STARTS_AT, "ends_at": FIXTURE_YEAR_ENDS_AT, })), ) .await; assert_eq!( res.status, StatusCode::CREATED, "create academic year {name}: {}", res.body ); id_of(&res.body) } /// The school's newest academic year, minted when it has none yet — a fixture /// needs a calendar without the test having to name one. pub async fn ensure_year(app: &Router, staff: &str) -> String { let res = send(app, "GET", "/academic-years?limit=1", Some(staff), None).await; assert_eq!(res.status, StatusCode::OK, "list academic years"); match items(&res.body).first() { Some(year) => id_of(year), None => create_year(app, staff, "2026-2027").await, } } /// Create a dönem inside `year` as `staff` (asserts 201); returns its id. /// Dönem names repeat freely, so a fixture may mint one per call. pub async fn create_term(app: &Router, staff: &str, year: &str, name: &str) -> String { let res = send( app, "POST", "/terms", Some(staff), Some(json!({ "name": name, "year": year, "starts_at": FIXTURE_YEAR_STARTS_AT, "ends_at": FIXTURE_YEAR_ENDS_AT, })), ) .await; assert_eq!( res.status, StatusCode::CREATED, "create term {name}: {}", res.body ); id_of(&res.body) } /// Create a class (şube) as `staff` (asserts 201); returns its id. `body` /// carries everything but the name: `year`, `teacher_id`, `grade_level` /// (defaulting to rung 9 when the caller does not name one). pub async fn create_class(app: &Router, staff: &str, name: &str, body: Value) -> String { let mut body = body; body["name"] = json!(name); if body.get("grade_level").is_none() { body["grade_level"] = json!(9); } let res = send(app, "POST", "/classes", Some(staff), Some(body)).await; assert_eq!( res.status, StatusCode::CREATED, "create class {name}: {}", res.body ); res.body["class"]["id"] .as_str() .unwrap_or_else(|| panic!("class envelope with a class id: {}", res.body)) .to_string() } /// Put `user_id` in `class` as `staff` (asserts 201). The pump enrolls them into /// every instance the şube already carries, so a pumped roster row names the /// şube as its `source` — [`enroll`] is the hand-placed row that does not. pub async fn add_member(app: &Router, staff: &str, class: &str, user_id: &str) { let res = send( app, "POST", &format!("/classes/{class}/members"), Some(staff), Some(json!({ "user_id": user_id })), ) .await; assert_eq!( res.status, StatusCode::CREATED, "add member {user_id}: {}", res.body ); } /// Attach `course` to `class` as `staff` (asserts 201); returns the **instance /// id** — the anchor every exam, session, homework and enrollment keys on. /// `staff` needs catalog rights on the course (its creator, or manager+). pub async fn attach_instance(app: &Router, staff: &str, class: &str, course: &str) -> String { let res = send( app, "POST", &format!("/classes/{class}/instances"), Some(staff), Some(json!({ "course_id": course })), ) .await; assert_eq!( res.status, StatusCode::CREATED, "attach course {course} to class {class}: {}", res.body ); id_of(&res.body) } /// A catalog course as one şube teaches it: the stack a suite needs before /// anyone can be enrolled, examined or marked. pub struct Taught { /// The academic year the şube sits in — what a karne counts into. pub year: String, /// The dönem exams written against this fixture are filed under. pub term: String, /// The catalog course: `GET|PATCH /courses/{id}`, `/courses/{id}/subjects`. pub course: String, /// The şube itself: `GET|POST|DELETE /classes/{id}/members`, `/instances`. pub class: String, /// The `class_course` anchor: `/instances/{id}/…` — enrollments, exams, /// sessions, homework, roll call, marks. pub instance: String, } /// [`taught_under`] with the caller as the şube's homeroom teacher too — for a /// suite whose actor is one manager/admin account. pub async fn taught(app: &Router, staff: &str, title: &str) -> Taught { taught_under(app, staff, staff, title).await } /// Mint a whole academic stack for `title`: a year (reused when the school has /// one), a dönem, the catalog course — created by `homeroom`, so a /// creator-gated catalog route stays theirs — a şube naming `homeroom` as its /// sınıf öğretmeni, and the instance the pair forms. /// /// `staff` must hold manager+ (a şube is school structure) and `homeroom` /// teacher+; passing the same cookie twice is the one-account fixture. Naming /// the homeroom teacher is what lets a plain `teacher` cookie act on the /// instance, because `ensure_instance_teacher` passes for the şube's own /// öğretmen as well as for manager+. pub async fn taught_under(app: &Router, staff: &str, homeroom: &str, title: &str) -> Taught { let year = ensure_year(app, staff).await; let term = create_term(app, staff, &year, "1. Dönem").await; let course = create_course(app, homeroom, title).await; let homeroom_id = me_id(app, homeroom).await; let class = create_class( app, staff, &format!("{title} şubesi"), json!({ "year": year, "teacher_id": homeroom_id }), ) .await; let instance = attach_instance(app, staff, &class, &course).await; Taught { year, term, course, class, instance, } } /// Create an exam inside `instance` as `cookie` (asserts 201); returns its id. /// `term` is the dönem the marks count into (`GET /terms`); the exam's weight in /// the instance's average comes from its kind (settings). pub async fn create_exam( app: &Router, cookie: &str, instance: &str, term: &str, title: &str, kind: &str, ) -> String { let res = send( app, "POST", &format!("/instances/{instance}/exams"), Some(cookie), Some(json!({ "title": title, "kind": kind, "term": term })), ) .await; assert_eq!( res.status, StatusCode::CREATED, "create exam {title}: {}", res.body ); id_of(&res.body) } /// Create an exam inside `instance` from a full JSON body (no assertion) — for /// exercising the scheduling fields and their validation. The body must carry /// its own `term`. pub async fn create_exam_with(app: &Router, cookie: &str, instance: &str, body: Value) -> Res { send( app, "POST", &format!("/instances/{instance}/exams"), Some(cookie), Some(body), ) .await } /// One homework-create send with the subject-gate retry: a `subject_id` the /// section's resolved syllabus does not carry is refused, and the writer — /// who holds a right over the instance (the very gate the homework write /// passed) — takes the topic onto the section's own set through the instance /// door and retries once. Every other refusal comes back untouched. async fn send_homework_create( app: &Router, cookie: &str, instance: &str, body: Value, ) -> Res { let res = send( app, "POST", &format!("/instances/{instance}/homework"), Some(cookie), Some(body.clone()), ) .await; if res.status == StatusCode::BAD_REQUEST && res.body["error"] .as_str() .is_some_and(|e| e.contains("subject is not in this section's subject set")) { let subject = body["subject_id"].as_str().expect("subject_id").to_string(); let res = send( app, "POST", &format!("/instances/{instance}/subjects"), Some(cookie), Some(json!({ "subject": subject })), ) .await; assert!( matches!(res.status, StatusCode::CREATED | StatusCode::OK), "select subject {subject} on instance {instance}: {}", res.body ); return send( app, "POST", &format!("/instances/{instance}/homework"), Some(cookie), Some(body), ) .await; } res } /// Take `subject` onto the section's own syllabus through the instance door — /// the D10 gate, the same right the question and homework writers hold. /// Idempotent: a subject already in the set re-answers 200. pub async fn select_instance_subject(app: &Router, cookie: &str, instance: &str, subject: &str) { let res = send( app, "POST", &format!("/instances/{instance}/subjects"), Some(cookie), Some(json!({ "subject": subject })), ) .await; assert!( matches!(res.status, StatusCode::CREATED | StatusCode::OK), "select subject {subject} on instance {instance}: {}", res.body ); } /// Create a homework inside `instance` as `cookie` (asserts 201); returns its /// id. Tagged with `subject` (required — every homework carries one of its /// course's subjects) and due at `due_at` (unix-millis; must not lie past the /// 60s grace). Whole-instance audience; use [`create_homework_with`] for an /// `assigned` subset. pub async fn create_homework( app: &Router, cookie: &str, instance: &str, subject: &str, title: &str, due_at: i64, ) -> String { let res = send_homework_create( app, cookie, instance, json!({ "title": title, "subject_id": subject, "due_at": due_at }), ) .await; assert_eq!( res.status, StatusCode::CREATED, "create homework {title}: {}", res.body ); id_of(&res.body) } /// Create a homework inside `instance` from a full JSON body (no assertion) — /// for exercising the `assigned` subset and the validation rejects. The /// subject-gate retry applies here too; every other refusal comes back as /// the server answered it. pub async fn create_homework_with(app: &Router, cookie: &str, instance: &str, body: Value) -> Res { send_homework_create(app, cookie, instance, body).await } /// Create a lesson session inside `instance` as `cookie` (asserts 201); returns /// its id. The session's teacher defaults to the caller. pub async fn create_session(app: &Router, cookie: &str, instance: &str, starts_at: i64) -> String { let res = send( app, "POST", &format!("/instances/{instance}/sessions"), Some(cookie), Some(json!({ "starts_at": starts_at })), ) .await; assert_eq!( res.status, StatusCode::CREATED, "create session: {}", res.body ); id_of(&res.body) } /// Drop `user_id` from `instance`'s roster as `cookie` (asserts 204). Deleting a /// catalog course is refused while instances still teach it, so cascade tests /// detach instances and empty rosters first. pub async fn unenroll(app: &Router, cookie: &str, instance: &str, user_id: &str) { let res = send( app, "DELETE", &format!("/instances/{instance}/enrollments/{user_id}"), Some(cookie), None, ) .await; assert_eq!( res.status, StatusCode::NO_CONTENT, "unenroll {user_id}: {}", res.body ); } /// Enroll `user_id` into `instance` as `cookie` (asserts 200) — the roster row a /// human placed, carrying no şube, so no şube sweep may take it back. pub async fn enroll(app: &Router, cookie: &str, instance: &str, user_id: &str) { let res = send( app, "POST", &format!("/instances/{instance}/enrollments"), Some(cookie), Some(json!({ "user_id": user_id })), ) .await; assert_eq!(res.status, StatusCode::OK, "enroll {user_id}: {}", res.body); } // ---- multi-school deployments --------------------------------------------- // // The probes whose subject is tenancy itself — cross-school isolation, // suspension, school deletion — build a deployment with more schools than // the demo one. Under database-per-school tenancy there is no separate // "remote" mode to exercise: production *is* this shape, and a second // deployment is a second `database::init_test_tenants()` away. (The // `surreal start` remote lane this section used to hold died with the // embedded engine.) /// A deployment with its own blob directory: what the tenancy probes hold. pub struct TestDeployment { pub app: Router, pub tenants: Tenants, /// `FILES_PATH` — this deployment's own directory, never the shared /// [`files_dir`], so one deployment's uploads cannot be counted by /// another's probes. pub files: PathBuf, _files: TempDir, } /// [`app_and_tenants`] with named schools created alongside the demo — where /// the former `remote_deployment` probes re-point. The named schools go /// through the real [`Tenants::create`], since the create path is part of /// what the tenancy probes examine. pub async fn deployment_with(schools: &[(&str, &str)]) -> TestDeployment { let files = tempfile::tempdir().expect("files tempdir"); let tenants = database::init_test_tenants().await; for (label, name) in schools { let id = school_id(label); tenants .create(id, name, ModuleSet::all()) .await .unwrap_or_else(|err| panic!("create school {label}: {err}")); } let app = build_router(AppState { db: tenants.control().clone(), tenants: tenants.clone(), files_path: files.path().to_path_buf(), cookie_secure: false, rate_limit: RateLimitConfig::unlimited(), chatbot_limit: Default::default(), rag_limit: Default::default(), exam_presence: Default::default(), board_hub: Default::default(), ai: None, metrics: Metrics::noop(), }); TestDeployment { app, tenants, files: files.path().to_path_buf(), _files: files, } }