From e169cf49fa5af554287c2bfab70545127a68c88e Mon Sep 17 00:00:00 2001 From: lukinovec Date: Wed, 26 Apr 2023 18:09:50 +0200 Subject: [PATCH] Create and drop schema in tests --- src/Actions/CreateRLSPoliciesForTables.php | 3 +-- tests/PostgresTest.php | 28 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Actions/CreateRLSPoliciesForTables.php b/src/Actions/CreateRLSPoliciesForTables.php index 82f58da2..9a29eb2d 100644 --- a/src/Actions/CreateRLSPoliciesForTables.php +++ b/src/Actions/CreateRLSPoliciesForTables.php @@ -43,8 +43,6 @@ class CreateRLSPoliciesForTables ) )"); - dump(DB::select('select CURRENT_USER')); - DB::statement("ALTER TABLE {$table} FORCE ROW LEVEL SECURITY"); return Command::SUCCESS; @@ -60,6 +58,7 @@ class CreateRLSPoliciesForTables DB::statement("ALTER TABLE {$table} FORCE ROW LEVEL SECURITY"); + // $this->components->info("Created RLS policy for table '$table'"); } diff --git a/tests/PostgresTest.php b/tests/PostgresTest.php index 5bf6463c..08b2d96c 100644 --- a/tests/PostgresTest.php +++ b/tests/PostgresTest.php @@ -4,6 +4,8 @@ declare(strict_types=1); use Illuminate\Support\Facades\DB; use Stancl\Tenancy\Tests\Etc\Tenant; +use Illuminate\Support\Facades\Schema; +use Illuminate\Database\Schema\Blueprint; use Stancl\Tenancy\Jobs\DeleteTenantsPostgresUser; use Stancl\Tenancy\Jobs\CreatePostgresUserForTenant; use Stancl\Tenancy\Actions\CreateRLSPoliciesForTables; @@ -53,6 +55,27 @@ test('correct rls policies get created', function(bool $action) { ], ]); + Schema::dropIfExists('comments'); + Schema::dropIfExists('posts'); + + Schema::create('posts', function (Blueprint $table) { + $table->increments('id'); + $table->string('text'); + + $table->string('tenant_id'); + + $table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade'); + }); + + Schema::create('comments', function (Blueprint $table) { + $table->increments('id'); + $table->string('text'); + + $table->unsignedInteger('post_id'); + + $table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade'); + }); + $getRlsPolicies = fn () => DB::select('select * from pg_policies'); $getModelTables = fn () => collect(config('tenancy.models.rls'))->map(fn (string $model) => (new $model)->getTable()); $getRlsTables = fn() => $getModelTables()->map(fn ($table) => DB::select('select relname, relrowsecurity, relforcerowsecurity from pg_class WHERE oid = ' . "'$table'::regclass"))->collapse(); @@ -94,4 +117,7 @@ test('correct rls policies get created', function(bool $action) { expect($getModelTables())->toContain($table->relname); expect($table->relforcerowsecurity)->toBeTrue(); } -})->with(['action' => true, 'command' => false]); +})->with([ + 'action' => true, + 'command' => false +]);