AI bridge honours module entitlements — api-read 403, blob module_disabled, no rag dispatch
Browse files at this commit4a0da39225e7eb6ef806d9ab276f140e0e69bffdParent: c78e41a- test
- AI bridge honours module entitlements — api-read 403, blob module_disabled, no rag dispatch
2 changed files +266 -7
- tests/ai_bridge.rs +231 -0
- tests/ai_protocol.rs +35 -7
tests/ai_bridge.rsmodified+231 -0
| @@ -2315,3 +2315,234 @@ async fn a_body_nobody_reads_is_reset_rather_than_left_streaming_forever() { | ||
| 2315 | 2315 | "expected a reset, got {err:?}" |
| 2316 | 2316 | ); |
| 2317 | 2317 | } |
| 2318 | + | |
| 2319 | +// ------------------------------------------------------ module entitlements -- | |
| 2320 | +// | |
| 2321 | +// A school that never bought a module must be refused on both halves of the | |
| 2322 | +// bridge: the api read, where the router's `route_layer` gate answers and the | |
| 2323 | +// refusal rides back as an ordinary `Ok` status, and the blob stream, which | |
| 2324 | +// bypasses the router entirely and checks `Module::CourseNotes` by hand. | |
| 2325 | + | |
| 2326 | +use hezarfen_backend::module::Module; | |
| 2327 | +use hezarfen_backend::tenant::Tenants; | |
| 2328 | + | |
| 2329 | +/// Take `off` away from the demo school. Entitlements are read off the registry | |
| 2330 | +/// row per request, so this lands on the very next frame — no reconnect, no | |
| 2331 | +/// cached handle to go stale. | |
| 2332 | +async fn demo_without(tenants: &Tenants, off: &[Module]) { | |
| 2333 | + let mut modules = ModuleSet::all(); | |
| 2334 | + for module in off { | |
| 2335 | + modules.remove(*module); | |
| 2336 | + } | |
| 2337 | + modules.validate().expect("the narrowed set is satisfiable"); | |
| 2338 | + tenants | |
| 2339 | + .set_modules(&demo(), &modules) | |
| 2340 | + .await | |
| 2341 | + .expect("narrow the demo school"); | |
| 2342 | +} | |
| 2343 | + | |
| 2344 | +/// The body `AppError::ModuleDisabled` renders — the same JSON a browser gets. | |
| 2345 | +fn module_disabled_body(module: Module) -> Value { | |
| 2346 | + json!({ "error": "module disabled", "module": module.as_str() }) | |
| 2347 | +} | |
| 2348 | + | |
| 2349 | +#[tokio::test] | |
| 2350 | +async fn an_api_read_of_a_disabled_module_is_the_gates_own_403_not_a_transport_refusal() { | |
| 2351 | + // The gate is a router layer and the bridge dispatches through the router, | |
| 2352 | + // so the entitlement holds on this seam for free — but only if the tenant | |
| 2353 | + // the dispatch carries is the real one. A service must see the school's own | |
| 2354 | + // 403, not `unavailable` and not a dropped stream; and two unrelated | |
| 2355 | + // modules prove the gate is generic rather than a special case for one. | |
| 2356 | + let bridge = bridge().await; | |
| 2357 | + let service = connect_service( | |
| 2358 | + &bridge, | |
| 2359 | + hello("tutor", &[AI_CHAT_CAPABILITY]), | |
| 2360 | + Behaviour::Echo, | |
| 2361 | + ) | |
| 2362 | + .await; | |
| 2363 | + await_workers(&bridge, 1).await; | |
| 2364 | + let (app, db, tenants) = common::app_with_ai_tenants(Some(bridge.clone())).await; | |
| 2365 | + let cookie = common::login_as(&app, &db, "ayse", "student").await; | |
| 2366 | + let student = common::me_id(&app, &cookie).await; | |
| 2367 | + | |
| 2368 | + for (module, path) in [(Module::Notes, "/notes"), (Module::Homework, "/homework")] { | |
| 2369 | + let request = read_of(path, Some(&student)); | |
| 2370 | + let (status, _) = ok_answer(api_read(&service.conn, request.clone()).await); | |
| 2371 | + assert_eq!(status, 200, "{path} answers while `{module}` is on"); | |
| 2372 | + | |
| 2373 | + demo_without(&tenants, &[module]).await; | |
| 2374 | + let (status, body) = ok_answer(api_read(&service.conn, request.clone()).await); | |
| 2375 | + assert_eq!(status, 403, "{path} with `{module}` off: {body}"); | |
| 2376 | + assert_eq!(body, module_disabled_body(module), "{path}"); | |
| 2377 | + | |
| 2378 | + // Sold back, and the very next frame answers again: the entitlement is | |
| 2379 | + // the only thing that changed. | |
| 2380 | + demo_without(&tenants, &[]).await; | |
| 2381 | + let (status, _) = ok_answer(api_read(&service.conn, request).await); | |
| 2382 | + assert_eq!(status, 200, "{path} answers once `{module}` is back"); | |
| 2383 | + } | |
| 2384 | +} | |
| 2385 | + | |
| 2386 | +/// A school with a course note and one attachment, plus the registry behind it: | |
| 2387 | +/// (service, student id, file id, uploaded bytes, tenants). | |
| 2388 | +async fn blob_fixture(bridge: &AiBridge) -> (FakeService, String, String, Vec<u8>, Tenants) { | |
| 2389 | + let service = connect_service( | |
| 2390 | + bridge, | |
| 2391 | + hello("indexer", &[AI_RAG_INDEX_CAPABILITY]), | |
| 2392 | + Behaviour::Echo, | |
| 2393 | + ) | |
| 2394 | + .await; | |
| 2395 | + await_workers(bridge, 1).await; | |
| 2396 | + let (app, db, tenants) = common::app_with_ai_tenants(Some(bridge.clone())).await; | |
| 2397 | + | |
| 2398 | + let student_cookie = common::login_as(&app, &db, "ayse", "student").await; | |
| 2399 | + let student = common::me_id(&app, &student_cookie).await; | |
| 2400 | + let teacher = common::login_as(&app, &db, "hoca", "teacher").await; | |
| 2401 | + let course = common::create_course(&app, &teacher, "Physics").await; | |
| 2402 | + common::enroll(&app, &teacher, &course, &student).await; | |
| 2403 | + | |
| 2404 | + let res = common::send( | |
| 2405 | + &app, | |
| 2406 | + "POST", | |
| 2407 | + "/course-notes", | |
| 2408 | + Some(&teacher), | |
| 2409 | + Some(json!({ "course": course, "title": "Newton", "content": "F = ma" })), | |
| 2410 | + ) | |
| 2411 | + .await; | |
| 2412 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 2413 | + let note = common::id_of(&res.body); | |
| 2414 | + | |
| 2415 | + let uploaded = blob_bytes(64 * 1024); | |
| 2416 | + let res = common::upload_file_at( | |
| 2417 | + &app, | |
| 2418 | + &teacher, | |
| 2419 | + &format!("/course-notes/{note}/files"), | |
| 2420 | + "recap.pdf", | |
| 2421 | + "application/pdf", | |
| 2422 | + &uploaded, | |
| 2423 | + ) | |
| 2424 | + .await; | |
| 2425 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | |
| 2426 | + ( | |
| 2427 | + service, | |
| 2428 | + student, | |
| 2429 | + common::id_of(&res.body), | |
| 2430 | + uploaded, | |
| 2431 | + tenants, | |
| 2432 | + ) | |
| 2433 | +} | |
| 2434 | + | |
| 2435 | +#[tokio::test] | |
| 2436 | +async fn a_blob_read_is_refused_module_disabled_when_the_school_has_no_course_notes() { | |
| 2437 | + // The one surface the router's gate cannot reach. A refusal here must be | |
| 2438 | + // the header frame and *nothing after it*: a service reads `size` bytes on | |
| 2439 | + // an `Ok`, so a refusal followed by bytes would be read as a file. | |
| 2440 | + let bridge = bridge().await; | |
| 2441 | + let (service, student, file, uploaded, tenants) = blob_fixture(&bridge).await; | |
| 2442 | + | |
| 2443 | + let (header, bytes) = blob_read(&service.conn, blob_of(&file, Some(&student))).await; | |
| 2444 | + assert!( | |
| 2445 | + matches!(header, BlobResponse::Ok { .. }), | |
| 2446 | + "the module is on: {}", | |
| 2447 | + blob_refusal(header) | |
| 2448 | + ); | |
| 2449 | + assert_eq!(bytes.len(), uploaded.len()); | |
| 2450 | + | |
| 2451 | + demo_without(&tenants, &[Module::CourseNotes]).await; | |
| 2452 | + let (header, bytes) = blob_read(&service.conn, blob_of(&file, Some(&student))).await; | |
| 2453 | + assert_eq!(blob_refusal(header), "module_disabled"); | |
| 2454 | + assert!(bytes.is_empty(), "a refusal is followed by nothing at all"); | |
| 2455 | + | |
| 2456 | + // Sold back: the same request streams the same bytes as before. | |
| 2457 | + demo_without(&tenants, &[]).await; | |
| 2458 | + let (header, bytes) = blob_read(&service.conn, blob_of(&file, Some(&student))).await; | |
| 2459 | + assert!( | |
| 2460 | + matches!(header, BlobResponse::Ok { .. }), | |
| 2461 | + "the module is back: {}", | |
| 2462 | + blob_refusal(header) | |
| 2463 | + ); | |
| 2464 | + assert!(bytes == uploaded, "the bytes differ from what was uploaded"); | |
| 2465 | +} | |
| 2466 | + | |
| 2467 | +#[test] | |
| 2468 | +fn course_notes_without_courses_is_unsellable_so_the_blob_path_never_sees_it() { | |
| 2469 | + // The blob stream checks `course_notes` alone. That is enough *because* the | |
| 2470 | + // set it reads can never hold `course_notes` without `courses` — the | |
| 2471 | + // dependency is refused before a school is ever narrowed to it, so nobody | |
| 2472 | + // has to check the parent module on this path. | |
| 2473 | + let mut broken = ModuleSet::all(); | |
| 2474 | + broken.remove(Module::Courses); | |
| 2475 | + let err = broken | |
| 2476 | + .validate() | |
| 2477 | + .expect_err("course_notes cannot stand without courses"); | |
| 2478 | + assert!( | |
| 2479 | + err.to_string() | |
| 2480 | + .contains("course_notes requires courses, which is not enabled"), | |
| 2481 | + "{err}" | |
| 2482 | + ); | |
| 2483 | +} | |
| 2484 | + | |
| 2485 | +#[tokio::test] | |
| 2486 | +async fn a_school_without_course_notes_cannot_reach_the_rag_dispatch_at_all() { | |
| 2487 | + // The write-back of `rag_output` has exactly four triggers, all of them | |
| 2488 | + // `/course-notes` handlers (`spawn_index` in `web::course_notes`), so the | |
| 2489 | + // nest's gate is the whole answer: no create, no dispatch, no row. This | |
| 2490 | + // pins that there is no second, ungated way in. | |
| 2491 | + let bridge = bridge().await; | |
| 2492 | + let service = connect_service( | |
| 2493 | + &bridge, | |
| 2494 | + hello("indexer", &[AI_RAG_INDEX_CAPABILITY]), | |
| 2495 | + Behaviour::Answer(json!({ "summary": "x" })), | |
| 2496 | + ) | |
| 2497 | + .await; | |
| 2498 | + await_workers(&bridge, 1).await; | |
| 2499 | + let (app, db, tenants) = common::app_with_ai_tenants(Some(bridge.clone())).await; | |
| 2500 | + | |
| 2501 | + let teacher = common::login_as(&app, &db, "ogretmen", "teacher").await; | |
| 2502 | + let course = common::create_course(&app, &teacher, "fizik").await; | |
| 2503 | + demo_without(&tenants, &[Module::CourseNotes]).await; | |
| 2504 | + | |
| 2505 | + let res = common::send( | |
| 2506 | + &app, | |
| 2507 | + "POST", | |
| 2508 | + "/course-notes", | |
| 2509 | + Some(&teacher), | |
| 2510 | + Some(json!({ "course": course, "title": "Bölüm 3", "content": "özet" })), | |
| 2511 | + ) | |
| 2512 | + .await; | |
| 2513 | + assert_eq!(res.status, StatusCode::FORBIDDEN, "{}", res.body); | |
| 2514 | + assert_eq!(res.body, module_disabled_body(Module::CourseNotes)); | |
| 2515 | + | |
| 2516 | + // Nothing was sent to the indexer. The dispatch is fire-and-forget, so give | |
| 2517 | + // a frame that should never exist time to arrive before saying it did not. | |
| 2518 | + tokio::time::sleep(Duration::from_millis(300)).await; | |
| 2519 | + assert!( | |
| 2520 | + service.seen().is_empty(), | |
| 2521 | + "a gated handler still dispatched to an AI service: {:?}", | |
| 2522 | + service.seen() | |
| 2523 | + ); | |
| 2524 | +} | |
| 2525 | + | |
| 2526 | +#[tokio::test] | |
| 2527 | +async fn indexing_follows_course_notes_not_chatbot() { | |
| 2528 | + // `rag.index` is the course-notes module's own background refresh, not part | |
| 2529 | + // of the chatbot the `ai` package sells: a school that bought course notes | |
| 2530 | + // and no chatbot still gets its notes indexed. Pinned because the two are | |
| 2531 | + // easy to confuse — both go out over the same bridge. | |
| 2532 | + let bridge = bridge().await; | |
| 2533 | + let service = connect_service( | |
| 2534 | + &bridge, | |
| 2535 | + hello("indexer", &[AI_RAG_INDEX_CAPABILITY]), | |
| 2536 | + Behaviour::Answer(json!({ "summary": "x" })), | |
| 2537 | + ) | |
| 2538 | + .await; | |
| 2539 | + await_workers(&bridge, 1).await; | |
| 2540 | + let (app, db, tenants) = common::app_with_ai_tenants(Some(bridge.clone())).await; | |
| 2541 | + demo_without(&tenants, &[Module::Chatbot]).await; | |
| 2542 | + | |
| 2543 | + let (_cookie, note) = course_note(&app, &db).await; | |
| 2544 | + let stored = await_outputs(&db, ¬e).await; | |
| 2545 | + assert_eq!(stored.len(), 1); | |
| 2546 | + assert_eq!(stored[0].get_payload()["summary"], "x"); | |
| 2547 | + assert_eq!(service.seen()[0].capability, AI_RAG_INDEX_CAPABILITY); | |
| 2548 | +} |
tests/ai_protocol.rsmodified+35 -7
| @@ -1316,7 +1316,7 @@ async fn a_blob_read_names_its_school_too() { | ||
| 1316 | 1316 | let bridge = bridge().await; |
| 1317 | 1317 | let service = raw::handshake(&bridge, &raw::hello("indexer", "rag.index")).await; |
| 1318 | 1318 | await_workers(&bridge, 1).await; |
| 1319 | - let (_app, student, file, uploaded) = armed_with_file(&bridge).await; | |
| 1319 | + let (_app, student, file, uploaded, _tenants) = armed_with_file(&bridge).await; | |
| 1320 | 1320 | |
| 1321 | 1321 | let (header, _, body) = raw::blob_read( |
| 1322 | 1322 | &service.conn, |
| @@ -1361,8 +1361,8 @@ async fn a_blob_read_names_its_school_too() { | ||
| 1361 | 1361 | |
| 1362 | 1362 | /// An armed router plus an uploaded course-note file: its id, and the bytes |
| 1363 | 1363 | /// that were uploaded, read back by a service that must receive them verbatim. |
| 1364 | -async fn armed_with_file(bridge: &AiBridge) -> (axum::Router, String, String, Vec<u8>) { | |
| 1365 | - let (app, db) = common::app_with_ai(Some(bridge.clone())).await; | |
| 1364 | +async fn armed_with_file(bridge: &AiBridge) -> (axum::Router, String, String, Vec<u8>, Tenants) { | |
| 1365 | + let (app, db, tenants) = common::app_with_ai_tenants(Some(bridge.clone())).await; | |
| 1366 | 1366 | let student_cookie = common::login_as(&app, &db, "ayse", "student").await; |
| 1367 | 1367 | let student = common::me_id(&app, &student_cookie).await; |
| 1368 | 1368 | let teacher = common::login_as(&app, &db, "hoca", "teacher").await; |
| @@ -1394,7 +1394,7 @@ async fn armed_with_file(bridge: &AiBridge) -> (axum::Router, String, String, Ve | ||
| 1394 | 1394 | .await; |
| 1395 | 1395 | assert_eq!(res.status, 201, "{}", res.body); |
| 1396 | 1396 | let file = common::id_of(&res.body); |
| 1397 | - (app, student, file, uploaded) | |
| 1397 | + (app, student, file, uploaded, tenants) | |
| 1398 | 1398 | } |
| 1399 | 1399 | |
| 1400 | 1400 | #[tokio::test] |
| @@ -1406,7 +1406,7 @@ async fn the_blob_header_frame_carries_exactly_the_published_keys_then_size_byte | ||
| 1406 | 1406 | let bridge = bridge().await; |
| 1407 | 1407 | let service = raw::handshake(&bridge, &raw::hello("indexer", "rag.index")).await; |
| 1408 | 1408 | await_workers(&bridge, 1).await; |
| 1409 | - let (_app, student, file, uploaded) = armed_with_file(&bridge).await; | |
| 1409 | + let (_app, student, file, uploaded, _tenants) = armed_with_file(&bridge).await; | |
| 1410 | 1410 | |
| 1411 | 1411 | let (header, bytes, body) = raw::blob_read( |
| 1412 | 1412 | &service.conn, |
| @@ -1450,7 +1450,7 @@ async fn a_blob_refusal_frame_carries_exactly_the_published_keys_and_no_bytes() | ||
| 1450 | 1450 | let bridge = bridge().await; |
| 1451 | 1451 | let service = raw::handshake(&bridge, &raw::hello("indexer", "rag.index")).await; |
| 1452 | 1452 | await_workers(&bridge, 1).await; |
| 1453 | - let (_app, student, _file, _uploaded) = armed_with_file(&bridge).await; | |
| 1453 | + let (_app, student, file, _uploaded, tenants) = armed_with_file(&bridge).await; | |
| 1454 | 1454 | |
| 1455 | 1455 | let (header, _, body) = raw::blob_read( |
| 1456 | 1456 | &service.conn, |
| @@ -1469,6 +1469,34 @@ async fn a_blob_refusal_frame_carries_exactly_the_published_keys_and_no_bytes() | ||
| 1469 | 1469 | assert_eq!(header["code"], "not_found"); |
| 1470 | 1470 | assert_eq!(header["id"], "t-blob"); |
| 1471 | 1471 | assert!(body.is_empty(), "a refusal is followed by nothing at all"); |
| 1472 | + | |
| 1473 | + // The entitlement refusal is the same published shape, under the code the | |
| 1474 | + // README's refusal table names. It is checked on this stream by hand — the | |
| 1475 | + // router's module gate cannot reach here — so it is a wire contract too. | |
| 1476 | + let mut modules = ModuleSet::all(); | |
| 1477 | + modules.remove(hezarfen_backend::module::Module::CourseNotes); | |
| 1478 | + tenants | |
| 1479 | + .set_modules(&Slug::try_new(SCHOOL).unwrap(), &modules) | |
| 1480 | + .await | |
| 1481 | + .expect("take course_notes off the demo school"); | |
| 1482 | + | |
| 1483 | + let (header, _, body) = raw::blob_read( | |
| 1484 | + &service.conn, | |
| 1485 | + format!( | |
| 1486 | + r#"{{"id":"t-mod","school":"{SCHOOL}","file":"{file}","on_behalf_of":"{student}"}}"# | |
| 1487 | + ) | |
| 1488 | + .as_bytes(), | |
| 1489 | + ) | |
| 1490 | + .await; | |
| 1491 | + assert_eq!( | |
| 1492 | + raw::keys(&header), | |
| 1493 | + ["code", "id", "message", "school", "status"], | |
| 1494 | + "blob refusal shape changed: {header}" | |
| 1495 | + ); | |
| 1496 | + assert_eq!(header["code"], "module_disabled", "{header}"); | |
| 1497 | + assert_eq!(header["school"], SCHOOL, "{header}"); | |
| 1498 | + assert_eq!(header["id"], "t-mod"); | |
| 1499 | + assert!(body.is_empty(), "a refused blob stream carries no bytes"); | |
| 1472 | 1500 | } |
| 1473 | 1501 | |
| 1474 | 1502 | #[tokio::test] |
| @@ -1482,7 +1510,7 @@ async fn an_api_read_still_answers_on_the_shared_client_stream_path() { | ||
| 1482 | 1510 | let bridge = bridge().await; |
| 1483 | 1511 | let service = raw::handshake(&bridge, &raw::hello("indexer", "rag.index")).await; |
| 1484 | 1512 | await_workers(&bridge, 1).await; |
| 1485 | - let (_app, student, file, _uploaded) = armed_with_file(&bridge).await; | |
| 1513 | + let (_app, student, file, _uploaded, _tenants) = armed_with_file(&bridge).await; | |
| 1486 | 1514 | |
| 1487 | 1515 | let (answer, _) = raw::api_read( |
| 1488 | 1516 | &service.conn, |