From 9f0328f9efc369137682ab21abcc3225c3501942 Mon Sep 17 00:00:00 2001 From: Hayatunnabi Nabil Date: Fri, 7 Nov 2025 22:58:23 +0600 Subject: [PATCH] test: Add unit test for dropRLSPolicies to verify removal of PostgreSQL policies --- tests/RLS/PolicyTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/RLS/PolicyTest.php b/tests/RLS/PolicyTest.php index ee9bf5cc..9bce22be 100644 --- a/tests/RLS/PolicyTest.php +++ b/tests/RLS/PolicyTest.php @@ -17,6 +17,7 @@ use Stancl\Tenancy\Commands\CreateUserWithRLSPolicies; use Stancl\Tenancy\RLS\PolicyManagers\TableRLSManager; use Stancl\Tenancy\RLS\PolicyManagers\TraitRLSManager; use Stancl\Tenancy\Bootstrappers\PostgresRLSBootstrapper; +use Stancl\Tenancy\Tenancy; use function Stancl\Tenancy\Tests\pest; beforeEach(function () { @@ -189,6 +190,23 @@ test('rls command recreates policies if the force option is passed', function (s TraitRLSManager::class, ]); +test('dropRLSPolicies removes postgres policies', function () { + // create dummy policy with characters that require quoting to ensure identifier handling works + DB::statement('CREATE POLICY "comments_rls_policy_manual" ON comments USING (true)'); + + $policyCount = fn () => count(DB::select( + 'SELECT policyname FROM pg_policies WHERE tablename = ? AND policyname = ?', + ['comments', 'comments_rls_policy_manual'] + )); + + expect($policyCount())->toBe(1); + + $removed = Tenancy::dropRLSPolicies('comments'); + + expect($removed)->toBe(1); + expect($policyCount())->toBe(0); +}); + test('queries will stop working when the tenant session variable is not set', function(string $manager, bool $forceRls) { CreateUserWithRLSPolicies::$forceRls = $forceRls;