MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / test_rls_rules

Function test_rls_rules

crates/core/src/sql/execute.rs:546–769  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

544 /// Test querying a table with RLS rules
545 #[tokio::test]
546 async fn test_rls_rules() -> anyhow::Result<()> {
547 let db = TestDB::in_memory()?;
548
549 let id_for_a = identity_from_u8(1);
550 let id_for_b = identity_from_u8(2);
551
552 let users_schema = [("identity", AlgebraicType::identity())];
553 let sales_schema = [
554 ("order_id", AlgebraicType::U64),
555 ("customer", AlgebraicType::identity()),
556 ];
557
558 let users_table_id = db.create_table_for_test("users", &users_schema, &[])?;
559 let sales_table_id = db.create_table_for_test("sales", &sales_schema, &[])?;
560
561 insert_rows(&db, users_table_id, vec![product![id_for_a], product![id_for_b]])?;
562 insert_rows(
563 &db,
564 sales_table_id,
565 vec![
566 product![1u64, id_for_a],
567 product![2u64, id_for_b],
568 product![3u64, id_for_a],
569 product![4u64, id_for_b],
570 ],
571 )?;
572
573 insert_rls_rules(
574 &db,
575 [users_table_id, sales_table_id],
576 [
577 "select * from users where identity = :sender",
578 "select s.* from users u join sales s on u.identity = s.customer",
579 ],
580 )?;
581
582 let auth_for_a = AuthCtx::new(Identity::ZERO, id_for_a);
583 let auth_for_b = AuthCtx::new(Identity::ZERO, id_for_b);
584
585 assert_query_results(
586 db.clone(),
587 // Should only return the identity for sender "a"
588 "select * from users",
589 auth_for_a.clone(),
590 [product![id_for_a]],
591 )
592 .await;
593 assert_query_results(
594 db.clone(),
595 // Should only return the identity for sender "b"
596 "select * from users",
597 auth_for_b.clone(),
598 [product![id_for_b]],
599 )
600 .await;
601 assert_query_results(
602 db.clone(),
603 // Should only return the orders for sender "a"

Callers

nothing calls this directly

Calls 9

assert_query_resultsFunction · 0.85
create_table_for_testMethod · 0.80
identity_from_u8Function · 0.70
insert_rowsFunction · 0.70
insert_rls_rulesFunction · 0.70
identityFunction · 0.50
newFunction · 0.50
OkFunction · 0.50
cloneMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…