1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 19:24:02 +00:00

Check RLS policies by name

This commit is contained in:
lukinovec 2023-06-15 13:16:57 +02:00
parent 0f9e0f33b0
commit 7b092ec9d3

View file

@ -113,7 +113,7 @@ test('postgres user can get deleted using the job', function() {
test('correct rls policies get created', function () {
$tenantModels = tenancy()->getTenantModels();
$modelTables = collect($tenantModels)->map(fn (Model $model) => $model->getTable());
$getRlsPolicies = fn () => DB::select('select * from pg_policies');
$getRlsPolicies = fn () => array_map(fn ($policy) => $policy->policyname, DB::select('select * from pg_policies'));
$getRlsTables = fn () => $modelTables->map(fn ($table) => DB::select('select relname, relrowsecurity, relforcerowsecurity from pg_class WHERE oid = ' . "'$table'::regclass"))->collapse();
// Drop all existing policies to check if the command creates policies for multiple tables
@ -128,7 +128,9 @@ test('correct rls policies get created', function () {
// Check if all tables with policies are RLS protected (even the ones not directly related to the tenant)
// Models related to tenant through some model must use the BelongsToPrimaryModel trait
// For the command to create the policy correctly for the model's table
expect($getRlsPolicies())->toHaveCount(count($tenantModels)); // 2
expect($getRlsPolicies())
->toContain(...$tenantModels->map(fn (Model $model) => $model->getTable() . '_rls_policy', ))
->toHaveCount(count($tenantModels)); // 2
expect($getRlsTables())->toHaveCount(count($tenantModels)); // 2
foreach ($getRlsTables() as $table) {