48 lines
1.9 KiB
TypeScript
48 lines
1.9 KiB
TypeScript
// @vitest-environment node
|
|
import { readFileSync, readdirSync } from "node:fs";
|
|
import path from "node:path";
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
const repoRoot = path.resolve(process.cwd());
|
|
const rlsDirectory = path.join(repoRoot, "prisma", "rls");
|
|
|
|
function executableSql(relativePath: string): string {
|
|
return readFileSync(path.join(rlsDirectory, relativePath), "utf8").replace(/--.*$/gm, "");
|
|
}
|
|
|
|
describe("RLS rollout artifacts", () => {
|
|
it("never provisions a reusable database password from repository SQL", () => {
|
|
const violations = readdirSync(rlsDirectory)
|
|
.filter((file) => file.endsWith(".sql"))
|
|
.filter((file) => /\b(?:CREATE|ALTER)\s+(?:ROLE|USER)\b[^;]*\bPASSWORD\b/is.test(executableSql(file)));
|
|
|
|
expect(violations).toEqual([]);
|
|
});
|
|
|
|
it("uses the canonical transaction-local tenant context name in every isolation policy", () => {
|
|
const isolationFiles = readdirSync(rlsDirectory).filter((file) => file.endsWith("-isolation.sql"));
|
|
const violations = isolationFiles.filter((file) => {
|
|
const sql = executableSql(file);
|
|
return !sql.includes("app.tenant_id") || sql.includes("app.current_tenant_id");
|
|
});
|
|
|
|
expect(violations).toEqual([]);
|
|
});
|
|
|
|
it("keeps global automation templates read-only for the tenant runtime role", () => {
|
|
const sql = executableSql("automation-isolation.sql");
|
|
const selectPolicy = /CREATE POLICY "tenant_isolation_automation_template_select"[\s\S]*?;/i.exec(sql)?.[0];
|
|
const mutablePolicies = [...sql.matchAll(/CREATE POLICY "tenant_isolation_automation_template_(?:insert|update|delete)"[\s\S]*?;/gi)].map(
|
|
(match) => match[0],
|
|
);
|
|
|
|
expect(selectPolicy).toContain('"tenantId" IS NULL OR');
|
|
expect(mutablePolicies).toHaveLength(3);
|
|
for (const policy of mutablePolicies) {
|
|
expect(policy).toContain("current_setting('app.tenant_id', true)");
|
|
expect(policy).not.toContain('"tenantId" IS NULL');
|
|
}
|
|
});
|
|
});
|