use nodedb::bridge::envelope::{ErrorCode, PhysicalPlan, Status};
use nodedb::bridge::physical_plan::DocumentOp;
use crate::helpers::*;
#[test]
fn sparse_cross_tenant_put_does_not_overwrite() {
let (mut core, mut tx, mut rx, _dir) = make_core();
send_ok_as_tenant(
&mut core,
&mut tx,
&mut rx,
TENANT_A,
PhysicalPlan::Document(DocumentOp::PointPut {
collection: "profiles".into(),
document_id: "user_1".into(),
value: b"{\"name\":\"alice\",\"secret\":\"tenant_a_secret\"}".to_vec(),
surrogate: nodedb_types::Surrogate::ZERO,
pk_bytes: Vec::new(),
}),
);
send_ok_as_tenant(
&mut core,
&mut tx,
&mut rx,
TENANT_B,
PhysicalPlan::Document(DocumentOp::PointPut {
collection: "profiles".into(),
document_id: "user_1".into(),
value: b"{\"name\":\"bob\",\"secret\":\"tenant_b_secret\"}".to_vec(),
surrogate: nodedb_types::Surrogate::ZERO,
pk_bytes: Vec::new(),
}),
);
let resp_a = send_raw_as_tenant(
&mut core,
&mut tx,
&mut rx,
TENANT_A,
PhysicalPlan::Document(DocumentOp::PointGet {
collection: "profiles".into(),
document_id: "user_1".into(),
rls_filters: Vec::new(),
system_as_of_ms: None,
valid_at_ms: None,
surrogate: nodedb_types::Surrogate::ZERO,
pk_bytes: Vec::new(),
}),
);
assert_eq!(resp_a.status, Status::Ok);
let json_a = payload_json(&resp_a.payload);
assert!(
json_a.contains("tenant_a_secret"),
"Tenant A's document must be intact after Tenant B's cross-tenant Put; got: {json_a}"
);
assert!(
!json_a.contains("tenant_b_secret"),
"Tenant B's data must NOT appear in Tenant A's document; got: {json_a}"
);
}
#[test]
fn sparse_cross_tenant_delete_does_not_affect_owner() {
let (mut core, mut tx, mut rx, _dir) = make_core();
send_ok_as_tenant(
&mut core,
&mut tx,
&mut rx,
TENANT_A,
PhysicalPlan::Document(DocumentOp::PointPut {
collection: "records".into(),
document_id: "rec_42".into(),
value: b"{\"data\":\"confidential\"}".to_vec(),
surrogate: nodedb_types::Surrogate::ZERO,
pk_bytes: Vec::new(),
}),
);
let resp_del = send_raw_as_tenant(
&mut core,
&mut tx,
&mut rx,
TENANT_B,
PhysicalPlan::Document(DocumentOp::PointDelete {
collection: "records".into(),
document_id: "rec_42".into(),
surrogate: nodedb_types::Surrogate::ZERO,
pk_bytes: Vec::new(),
returning: None,
}),
);
let ok_or_not_found =
resp_del.status == Status::Ok || resp_del.error_code == Some(ErrorCode::NotFound);
assert!(
ok_or_not_found,
"Cross-tenant document delete must be Ok or NotFound, got {:?}",
resp_del.error_code
);
let resp_a = send_raw_as_tenant(
&mut core,
&mut tx,
&mut rx,
TENANT_A,
PhysicalPlan::Document(DocumentOp::PointGet {
collection: "records".into(),
document_id: "rec_42".into(),
rls_filters: Vec::new(),
system_as_of_ms: None,
valid_at_ms: None,
surrogate: nodedb_types::Surrogate::ZERO,
pk_bytes: Vec::new(),
}),
);
assert_eq!(resp_a.status, Status::Ok);
let json_a = payload_json(&resp_a.payload);
assert!(
json_a.contains("confidential"),
"Tenant A's document must survive Tenant B's cross-tenant delete; got: {json_a}"
);
}