Skip to content

Commit bc8d79b

Browse files
committed
test(postgres): add a new rls test
1 parent 3a5f69a commit bc8d79b

File tree

4 files changed

+496
-7
lines changed

4 files changed

+496
-7
lines changed

plans/BATCH_MERGE_AND_RLS.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,4 @@ The batch path is used for all platforms (SQLite client, SQLiteCloud, PostgreSQL
163163
164164
## TODO
165165
166-
- add a new test like the n° 27 with more columns and more cases
167166
- update documentation: RLS.md, README.md and the https://github.com/sqlitecloud/docs repo

test/postgresql/27_rls_batch_merge.sql

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,16 @@ SELECT COALESCE(max(db_version), 0) AS max_dbv_1 FROM cloudsync_changes \gset
105105
-- Apply as superuser (service-role pattern)
106106
\connect cloudsync_test_27_b
107107
\ir helper_psql_conn_setup.sql
108-
SELECT cloudsync_payload_apply(decode(:'payload_hex_1', 'hex')) AS _apply_1 \gset
108+
SELECT cloudsync_payload_apply(decode(:'payload_hex_1', 'hex')) AS apply_1 \gset
109+
110+
-- 1 row × 3 non-PK columns = 3 column-change entries
111+
SELECT (:apply_1::int = 3) AS apply_1_ok \gset
112+
\if :apply_1_ok
113+
\echo [PASS] (:testid) RLS: apply returned :apply_1
114+
\else
115+
\echo [FAIL] (:testid) RLS: apply returned :apply_1 (expected 3)
116+
SELECT (:fail::int + 1) AS fail \gset
117+
\endif
109118

110119
-- Verify complete row written (all columns present)
111120
SELECT COUNT(*) AS doc1_count FROM documents WHERE id = 'doc1' AND title = 'Title 1' AND content = 'Content 1' AND user_id = :'USER1'::UUID \gset
@@ -135,7 +144,16 @@ SELECT COALESCE(max(db_version), 0) AS max_dbv_2 FROM cloudsync_changes \gset
135144
-- Apply as superuser
136145
\connect cloudsync_test_27_b
137146
\ir helper_psql_conn_setup.sql
138-
SELECT cloudsync_payload_apply(decode(:'payload_hex_2', 'hex')) AS _apply_2 \gset
147+
SELECT cloudsync_payload_apply(decode(:'payload_hex_2', 'hex')) AS apply_2 \gset
148+
149+
-- 1 row × 3 non-PK columns = 3 entries
150+
SELECT (:apply_2::int = 3) AS apply_2_ok \gset
151+
\if :apply_2_ok
152+
\echo [PASS] (:testid) RLS: apply returned :apply_2
153+
\else
154+
\echo [FAIL] (:testid) RLS: apply returned :apply_2 (expected 3)
155+
SELECT (:fail::int + 1) AS fail \gset
156+
\endif
139157

140158
-- Verify doc2 exists (superuser sees all)
141159
SELECT COUNT(*) AS doc2_exists FROM documents WHERE id = 'doc2' \gset
@@ -172,7 +190,16 @@ SELECT COALESCE(max(db_version), 0) AS max_dbv_3 FROM cloudsync_changes \gset
172190
-- Apply as superuser
173191
\connect cloudsync_test_27_b
174192
\ir helper_psql_conn_setup.sql
175-
SELECT cloudsync_payload_apply(decode(:'payload_hex_3', 'hex')) AS _apply_3 \gset
193+
SELECT cloudsync_payload_apply(decode(:'payload_hex_3', 'hex')) AS apply_3 \gset
194+
195+
-- 1 row × 1 changed column (title) = 1 entry
196+
SELECT (:apply_3::int = 1) AS apply_3_ok \gset
197+
\if :apply_3_ok
198+
\echo [PASS] (:testid) RLS: apply returned :apply_3
199+
\else
200+
\echo [FAIL] (:testid) RLS: apply returned :apply_3 (expected 1)
201+
SELECT (:fail::int + 1) AS fail \gset
202+
\endif
176203

177204
-- Verify update applied (superuser check)
178205
SELECT COUNT(*) AS doc1_updated FROM documents WHERE id = 'doc1' AND title = 'Title 1 Updated' \gset
@@ -210,9 +237,18 @@ SELECT COALESCE(max(db_version), 0) AS max_dbv_4 FROM cloudsync_changes \gset
210237
\ir helper_psql_conn_setup.sql
211238
SET app.current_user_id = :'USER1';
212239
SET ROLE test_rls_user;
213-
SELECT cloudsync_payload_apply(decode(:'payload_hex_4', 'hex')) AS _apply_4 \gset
240+
SELECT cloudsync_payload_apply(decode(:'payload_hex_4', 'hex')) AS apply_4 \gset
214241
RESET ROLE;
215242

243+
-- 1 row × 3 non-PK columns = 3 entries
244+
SELECT (:apply_4::int = 3) AS apply_4_ok \gset
245+
\if :apply_4_ok
246+
\echo [PASS] (:testid) RLS auth: apply returned :apply_4
247+
\else
248+
\echo [FAIL] (:testid) RLS auth: apply returned :apply_4 (expected 3)
249+
SELECT (:fail::int + 1) AS fail \gset
250+
\endif
251+
216252
-- Verify doc3 exists with all columns correct
217253
SELECT COUNT(*) AS doc3_count FROM documents WHERE id = 'doc3' AND title = 'Title 3' AND content = 'Content 3' AND user_id = :'USER1'::UUID \gset
218254
SELECT (:doc3_count::int = 1) AS test4_ok \gset
@@ -243,12 +279,21 @@ SELECT COALESCE(max(db_version), 0) AS max_dbv_5 FROM cloudsync_changes \gset
243279
\ir helper_psql_conn_setup.sql
244280
SET app.current_user_id = :'USER1';
245281
SET ROLE test_rls_user;
246-
SELECT cloudsync_payload_apply(decode(:'payload_hex_5', 'hex'));
282+
SELECT cloudsync_payload_apply(decode(:'payload_hex_5', 'hex')) AS apply_5 \gset
247283

248284
-- Reconnect for clean state after expected RLS denial
249285
\connect cloudsync_test_27_b
250286
\ir helper_psql_conn_setup.sql
251287

288+
-- 1 row × 3 non-PK columns = 3 entries (returned even if denied)
289+
SELECT (:apply_5::int = 3) AS apply_5_ok \gset
290+
\if :apply_5_ok
291+
\echo [PASS] (:testid) RLS auth: denied apply returned :apply_5
292+
\else
293+
\echo [FAIL] (:testid) RLS auth: denied apply returned :apply_5 (expected 3)
294+
SELECT (:fail::int + 1) AS fail \gset
295+
\endif
296+
252297
-- Verify doc4 does NOT exist (superuser check)
253298
SELECT COUNT(*) AS doc4_count FROM documents WHERE id = 'doc4' \gset
254299
SELECT (:doc4_count::int = 0) AS test5_ok \gset
@@ -276,9 +321,18 @@ WHERE site_id = cloudsync_siteid()
276321
\ir helper_psql_conn_setup.sql
277322
SET app.current_user_id = :'USER1';
278323
SET ROLE test_rls_user;
279-
SELECT cloudsync_payload_apply(decode(:'payload_hex_6', 'hex')) AS _apply_6 \gset
324+
SELECT cloudsync_payload_apply(decode(:'payload_hex_6', 'hex')) AS apply_6 \gset
280325
RESET ROLE;
281326

327+
-- 1 row × 1 changed column (title) = 1 entry
328+
SELECT (:apply_6::int = 1) AS apply_6_ok \gset
329+
\if :apply_6_ok
330+
\echo [PASS] (:testid) RLS auth: apply returned :apply_6
331+
\else
332+
\echo [FAIL] (:testid) RLS auth: apply returned :apply_6 (expected 1)
333+
SELECT (:fail::int + 1) AS fail \gset
334+
\endif
335+
282336
-- Verify doc3 title was updated
283337
SELECT COUNT(*) AS doc3_updated FROM documents WHERE id = 'doc3' AND title = 'Title 3 Updated' \gset
284338
SELECT (:doc3_updated::int = 1) AS test6_ok \gset

0 commit comments

Comments
 (0)