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

Function test_nested_rls_rules

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

Source from the content-addressed store, hash-verified

771 /// Test querying tables with multiple levels of RLS rules
772 #[tokio::test]
773 async fn test_nested_rls_rules() -> anyhow::Result<()> {
774 let db = TestDB::in_memory()?;
775
776 let id_for_a = identity_from_u8(1);
777 let id_for_b = identity_from_u8(2);
778 let id_for_c = identity_from_u8(3);
779
780 let users_schema = [("identity", AlgebraicType::identity())];
781 let sales_schema = [
782 ("order_id", AlgebraicType::U64),
783 ("product_id", AlgebraicType::U64),
784 ("customer", AlgebraicType::identity()),
785 ];
786
787 let users_table_id = db.create_table_for_test("users", &users_schema, &[0.into()])?;
788 let admin_table_id = db.create_table_for_test("admins", &users_schema, &[0.into()])?;
789 let sales_table_id = db.create_table_for_test("sales", &sales_schema, &[0.into()])?;
790
791 insert_rows(&db, admin_table_id, [product![id_for_c]])?;
792 insert_rows(
793 &db,
794 users_table_id,
795 [product![id_for_a], product![id_for_b], product![id_for_c]],
796 )?;
797 insert_rows(
798 &db,
799 sales_table_id,
800 [product![1u64, 1u64, id_for_a], product![2u64, 2u64, id_for_b]],
801 )?;
802
803 insert_rls_rules(
804 &db,
805 [admin_table_id, users_table_id, users_table_id, sales_table_id],
806 [
807 "select * from admins where identity = :sender",
808 "select * from users where identity = :sender",
809 "select users.* from admins join users",
810 "select s.* from users u join sales s on u.identity = s.customer",
811 ],
812 )?;
813
814 let auth_for_a = AuthCtx::new(Identity::ZERO, id_for_a);
815 let auth_for_b = AuthCtx::new(Identity::ZERO, id_for_b);
816 let auth_for_c = AuthCtx::new(Identity::ZERO, id_for_c);
817
818 assert_query_results(
819 db.clone(),
820 "select * from admins",
821 auth_for_a.clone(),
822 // Identity "a" is not an admin
823 [],
824 )
825 .await;
826 assert_query_results(
827 db.clone(),
828 "select * from admins",
829 auth_for_b.clone(),
830 // Identity "b" is not an admin

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…