A group's template suppression is "coarse by reference" (path for routes, handler-name for triggers), and both resolution points dropped ANY inherited row matching that reference across the whole subtree — including one a NEARER descendant group deliberately re-declared. So an ancestor group G that declines a far-ancestor's `/x` (or `audit` handler) would also silently kill a child group H's OWN `/x` / `audit`-bound trigger at the same reference, violating the documented "an owner can only decline what it inherits, never a descendant's own rows" invariant. Fix: gate each decline on the chain DEPTH of the suppressor vs the target's owner — a suppressor at depth `d_s` may only decline a row whose owner is strictly ABOVE it (`target_depth > d_s`): - Routes: `list_route_suppressions` now returns `(app, path, suppressor_depth)`; the rebuild folds it to the min depth per `(app, path)` and `compile_effective_routes` skips an inherited route only when `route.depth > suppressor_depth`. (This also subsumes the old `depth > 0` inherited-only gate.) - Triggers: the dispatch anti-join gains `AND sc.depth < c.depth` (the suppressor's chain depth below the trigger owner's), correlating on the outer `chain c` that every kv/docs/files/pubsub match query already binds. An app's own suppression is depth 0 → still declines anything it inherits; a group's suppression declines only what that group itself inherits. `sealed` still overrides. No schema change. Pinned by a new `compile_effective_routes` unit test (a depth-1 descendant route survives a depth-2 suppression that declines a depth-3 template) and a new `group_suppression` DB test (same for triggers); all existing suppression / sealed / template journeys stay green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
266 lines
8.7 KiB
Rust
266 lines
8.7 KiB
Rust
//! §11 tail integration test: per-app opt-out of inherited group templates.
|
|
//! An app that declares a suppression must NOT match the inherited TRIGGER
|
|
//! (dispatch anti-join) nor serve the inherited ROUTE (rebuild-time skip); a
|
|
//! sibling app with no suppression still inherits both. Suppression is
|
|
//! inheritance-only — an app's OWN trigger on the suppressed handler still
|
|
//! fires.
|
|
//!
|
|
//! Deterministic: drives `list_matching_kv` + `list_effective` /
|
|
//! `compile_effective_routes` directly (no async dispatcher). Skips cleanly
|
|
//! when `DATABASE_URL` is unset.
|
|
|
|
#![allow(
|
|
clippy::needless_pass_by_value,
|
|
clippy::many_single_char_names,
|
|
clippy::too_many_lines
|
|
)]
|
|
|
|
use picloud_manager_core::route_admin::compile_effective_routes;
|
|
use picloud_manager_core::route_repo::{PostgresRouteRepository, RouteRepository};
|
|
use picloud_manager_core::trigger_repo::{PostgresTriggerRepo, TriggerRepo};
|
|
use picloud_shared::{AppId, KvEventOp};
|
|
use sqlx::postgres::PgPoolOptions;
|
|
use sqlx::PgPool;
|
|
use uuid::Uuid;
|
|
|
|
async fn pool_or_skip() -> Option<PgPool> {
|
|
let Ok(url) = std::env::var("DATABASE_URL") else {
|
|
eprintln!("template_suppression: DATABASE_URL unset — skipping");
|
|
return None;
|
|
};
|
|
let pool = PgPoolOptions::new()
|
|
.max_connections(2)
|
|
.connect(&url)
|
|
.await
|
|
.expect("connect to DATABASE_URL");
|
|
sqlx::migrate!("./migrations")
|
|
.run(&pool)
|
|
.await
|
|
.expect("apply migrations");
|
|
Some(pool)
|
|
}
|
|
|
|
async fn id1(pool: &PgPool, sql: &str, bind: &str) -> Uuid {
|
|
let row: (Uuid,) = sqlx::query_as(sql)
|
|
.bind(bind)
|
|
.fetch_one(pool)
|
|
.await
|
|
.expect("insert returning id");
|
|
row.0
|
|
}
|
|
|
|
async fn app_under(pool: &PgPool, slug: &str, group: Uuid) -> Uuid {
|
|
let row: (Uuid,) =
|
|
sqlx::query_as("INSERT INTO apps (slug, name, group_id) VALUES ($1, $1, $2) RETURNING id")
|
|
.bind(slug)
|
|
.bind(group)
|
|
.fetch_one(pool)
|
|
.await
|
|
.expect("app insert");
|
|
row.0
|
|
}
|
|
|
|
/// Whether `app`'s compiled route slice serves `/hello`.
|
|
async fn serves_hello(repo: &PostgresRouteRepository, app: Uuid) -> bool {
|
|
let effective = repo.list_effective().await.expect("list_effective");
|
|
let mut suppressed: std::collections::HashMap<(AppId, String), i32> =
|
|
std::collections::HashMap::new();
|
|
for (a, p, d) in repo
|
|
.list_route_suppressions()
|
|
.await
|
|
.expect("list_route_suppressions")
|
|
{
|
|
suppressed
|
|
.entry((a, p))
|
|
.and_modify(|x| *x = (*x).min(d))
|
|
.or_insert(d);
|
|
}
|
|
compile_effective_routes(&effective, &suppressed)
|
|
.into_iter()
|
|
.any(|c| c.app_id == AppId::from(app) && format!("{:?}", c.path).contains("/hello"))
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn suppression_declines_inherited_trigger_and_route_only_for_declaring_app() {
|
|
let Some(pool) = pool_or_skip().await else {
|
|
return;
|
|
};
|
|
let sfx = Uuid::new_v4().simple().to_string();
|
|
let admin = id1(
|
|
&pool,
|
|
"INSERT INTO admin_users (username, password_hash) VALUES ($1, 'x') RETURNING id",
|
|
&format!("ts-{sfx}"),
|
|
)
|
|
.await;
|
|
|
|
// Group G owns a handler `audit` + a kv trigger template + a /hello route
|
|
// template, both bound to it.
|
|
let g = id1(
|
|
&pool,
|
|
"INSERT INTO groups (slug, name) VALUES ($1, $1) RETURNING id",
|
|
&format!("ts-g-{sfx}"),
|
|
)
|
|
.await;
|
|
let handler: (Uuid,) = sqlx::query_as(
|
|
"INSERT INTO scripts (name, source, group_id) VALUES ($1, 'x', $2) RETURNING id",
|
|
)
|
|
.bind(format!("audit-{sfx}"))
|
|
.bind(g)
|
|
.fetch_one(&pool)
|
|
.await
|
|
.expect("group handler");
|
|
let handler_name = format!("audit-{sfx}");
|
|
|
|
let tmpl: (Uuid,) = sqlx::query_as(
|
|
"INSERT INTO triggers \
|
|
(group_id, script_id, kind, enabled, dispatch_mode, \
|
|
retry_max_attempts, retry_backoff, retry_base_ms, \
|
|
registered_by_principal, name) \
|
|
VALUES ($1, $2, 'kv', TRUE, 'async', 3, 'exponential', 1000, $3, $4) RETURNING id",
|
|
)
|
|
.bind(g)
|
|
.bind(handler.0)
|
|
.bind(admin)
|
|
.bind(format!("tmpl-{sfx}"))
|
|
.fetch_one(&pool)
|
|
.await
|
|
.expect("kv template");
|
|
sqlx::query(
|
|
"INSERT INTO kv_trigger_details (trigger_id, collection_glob, ops) \
|
|
VALUES ($1, '*', ARRAY['insert'])",
|
|
)
|
|
.bind(tmpl.0)
|
|
.execute(&pool)
|
|
.await
|
|
.expect("kv details");
|
|
sqlx::query(
|
|
"INSERT INTO routes (group_id, script_id, host_kind, host, path_kind, path, method) \
|
|
VALUES ($1, $2, 'any', '', 'exact', '/hello', NULL)",
|
|
)
|
|
.bind(g)
|
|
.bind(handler.0)
|
|
.execute(&pool)
|
|
.await
|
|
.expect("route template");
|
|
|
|
// App A (suppresses both) + app B (no suppression), both under G.
|
|
let a = app_under(&pool, &format!("ts-a-{sfx}"), g).await;
|
|
let b = app_under(&pool, &format!("ts-b-{sfx}"), g).await;
|
|
sqlx::query(
|
|
"INSERT INTO template_suppressions (app_id, target_kind, reference) \
|
|
VALUES ($1, 'trigger', $2), ($1, 'route', '/hello')",
|
|
)
|
|
.bind(a)
|
|
.bind(&handler_name)
|
|
.execute(&pool)
|
|
.await
|
|
.expect("suppressions for A");
|
|
|
|
let trig = PostgresTriggerRepo::new(pool.clone());
|
|
let routes = PostgresRouteRepository::new(pool.clone());
|
|
|
|
// A suppressed → neither the inherited trigger matches nor the route serves.
|
|
let a_trig = trig
|
|
.list_matching_kv(AppId::from(a), "users", KvEventOp::Insert)
|
|
.await
|
|
.expect("match A");
|
|
assert!(
|
|
!a_trig.iter().any(|m| m.trigger_id == tmpl.0.into()),
|
|
"the suppressing app must NOT match the inherited trigger"
|
|
);
|
|
assert!(
|
|
!serves_hello(&routes, a).await,
|
|
"the suppressing app must NOT serve the inherited route"
|
|
);
|
|
|
|
// B did not suppress → still inherits both.
|
|
let b_trig = trig
|
|
.list_matching_kv(AppId::from(b), "users", KvEventOp::Insert)
|
|
.await
|
|
.expect("match B");
|
|
assert!(
|
|
b_trig.iter().any(|m| m.trigger_id == tmpl.0.into()),
|
|
"a sibling app that did not suppress still inherits the trigger"
|
|
);
|
|
assert!(
|
|
serves_hello(&routes, b).await,
|
|
"a sibling app that did not suppress still serves the route"
|
|
);
|
|
|
|
// Suppression is inheritance-only: A's OWN kv trigger on the same handler
|
|
// name (a distinct app-owned script) still fires despite the suppression.
|
|
let a_own_script: (Uuid,) = sqlx::query_as(
|
|
"INSERT INTO scripts (name, source, app_id) VALUES ($1, 'x', $2) RETURNING id",
|
|
)
|
|
.bind(&handler_name)
|
|
.bind(a)
|
|
.fetch_one(&pool)
|
|
.await
|
|
.expect("A own script");
|
|
let a_own_trig: (Uuid,) = sqlx::query_as(
|
|
"INSERT INTO triggers \
|
|
(app_id, script_id, kind, enabled, dispatch_mode, \
|
|
retry_max_attempts, retry_backoff, retry_base_ms, \
|
|
registered_by_principal, name) \
|
|
VALUES ($1, $2, 'kv', TRUE, 'async', 3, 'exponential', 1000, $3, $4) RETURNING id",
|
|
)
|
|
.bind(a)
|
|
.bind(a_own_script.0)
|
|
.bind(admin)
|
|
.bind(format!("own-{sfx}"))
|
|
.fetch_one(&pool)
|
|
.await
|
|
.expect("A own trigger");
|
|
sqlx::query(
|
|
"INSERT INTO kv_trigger_details (trigger_id, collection_glob, ops) \
|
|
VALUES ($1, '*', ARRAY['insert'])",
|
|
)
|
|
.bind(a_own_trig.0)
|
|
.execute(&pool)
|
|
.await
|
|
.expect("A own kv details");
|
|
|
|
let a_after = trig
|
|
.list_matching_kv(AppId::from(a), "users", KvEventOp::Insert)
|
|
.await
|
|
.expect("match A after own trigger");
|
|
assert!(
|
|
a_after.iter().any(|m| m.trigger_id == a_own_trig.0.into()),
|
|
"the app's OWN trigger fires — suppression only declines inherited ones"
|
|
);
|
|
assert!(
|
|
!a_after.iter().any(|m| m.trigger_id == tmpl.0.into()),
|
|
"the inherited trigger stays suppressed"
|
|
);
|
|
|
|
// Cleanup (FK order).
|
|
let _ = sqlx::query("DELETE FROM triggers WHERE id = ANY($1)")
|
|
.bind(vec![tmpl.0, a_own_trig.0])
|
|
.execute(&pool)
|
|
.await;
|
|
let _ = sqlx::query("DELETE FROM routes WHERE group_id = $1")
|
|
.bind(g)
|
|
.execute(&pool)
|
|
.await;
|
|
let _ = sqlx::query("DELETE FROM template_suppressions WHERE app_id = $1")
|
|
.bind(a)
|
|
.execute(&pool)
|
|
.await;
|
|
let _ = sqlx::query("DELETE FROM scripts WHERE id = ANY($1)")
|
|
.bind(vec![handler.0, a_own_script.0])
|
|
.execute(&pool)
|
|
.await;
|
|
let _ = sqlx::query("DELETE FROM apps WHERE id = ANY($1)")
|
|
.bind(vec![a, b])
|
|
.execute(&pool)
|
|
.await;
|
|
let _ = sqlx::query("DELETE FROM groups WHERE id = $1")
|
|
.bind(g)
|
|
.execute(&pool)
|
|
.await;
|
|
let _ = sqlx::query("DELETE FROM admin_users WHERE id = $1")
|
|
.bind(admin)
|
|
.execute(&pool)
|
|
.await;
|
|
}
|