Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+112
@@ -0,0 +1,112 @@
|
||||
// @vitest-environment node
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import pg from "pg";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
|
||||
const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../../..");
|
||||
|
||||
// R-P5 — APPS-ENGINE RLS GATE PROOF. Proves that once apps-engine-isolation.sql is
|
||||
// applied, the restricted hivegrid_app role (NOSUPERUSER, NOBYPASSRLS) is confined
|
||||
// to its own tenant's DataConnection / AppSyncRun rows, sees nothing without a
|
||||
// tenant context, and is blocked by WITH CHECK from writing cross-tenant. The
|
||||
// global `App` catalog stays readable (no RLS).
|
||||
//
|
||||
// Runs ONLY when RLS_TEST_DATABASE_URL is set; otherwise skipped.
|
||||
//
|
||||
// Setup:
|
||||
// docker run -d --name hg-rls-pg -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=hivegrid -p 5433:5432 pgvector/pgvector:pg16
|
||||
// DIRECT_URL=postgresql://postgres:postgres@localhost:5433/hivegrid pnpm exec prisma db push
|
||||
// RLS_TEST_DATABASE_URL=postgresql://postgres:postgres@localhost:5433/hivegrid pnpm vitest run rls-apps-engine
|
||||
|
||||
const ADMIN_URL = process.env.RLS_TEST_DATABASE_URL;
|
||||
const APP_ROLE = "hivegrid_app";
|
||||
const APP_PASSWORD = "hivegrid_app_pw";
|
||||
|
||||
function appUrlFrom(adminUrl: string): string {
|
||||
const url = new URL(adminUrl);
|
||||
url.username = APP_ROLE;
|
||||
url.password = APP_PASSWORD;
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
const describeIfDb = ADMIN_URL ? describe : describe.skip;
|
||||
|
||||
describeIfDb("Apps-Engine RLS tenant isolation gate proof (DataConnection, AppSyncRun)", () => {
|
||||
let admin: pg.Client;
|
||||
let app: pg.Client;
|
||||
|
||||
beforeAll(async () => {
|
||||
admin = new pg.Client({ connectionString: ADMIN_URL });
|
||||
await admin.connect();
|
||||
|
||||
await admin.query(fs.readFileSync(path.join(REPO_ROOT, "prisma/rls/tenant-isolation.sql"), "utf8"));
|
||||
await admin.query(fs.readFileSync(path.join(REPO_ROOT, "prisma/rls/apps-engine-isolation.sql"), "utf8"));
|
||||
await admin.query(`GRANT CONNECT ON DATABASE ${new URL(ADMIN_URL!).pathname.slice(1)} TO ${APP_ROLE}`);
|
||||
|
||||
// Global App catalog (no tenant) + two tenants' connections + sync runs.
|
||||
await admin.query(`INSERT INTO "App"(id,"updatedAt","key","title","category","connectionType") VALUES
|
||||
('rls_app1',now(),'sample-data-sync','Sample','erp','rest_api') ON CONFLICT (id) DO NOTHING`);
|
||||
await admin.query(`INSERT INTO "DataConnection"(id,"updatedAt","tenantId","appId","label") VALUES
|
||||
('rls_dcA',now(),'rls_tenantA','rls_app1','A'),
|
||||
('rls_dcB',now(),'rls_tenantB','rls_app1','B') ON CONFLICT (id) DO NOTHING`);
|
||||
await admin.query(`INSERT INTO "AppSyncRun"(id,"updatedAt","tenantId","dataConnectionId") VALUES
|
||||
('rls_runA',now(),'rls_tenantA','rls_dcA'),
|
||||
('rls_runB',now(),'rls_tenantB','rls_dcB') ON CONFLICT (id) DO NOTHING`);
|
||||
|
||||
app = new pg.Client({ connectionString: appUrlFrom(ADMIN_URL!) });
|
||||
await app.connect();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app?.end();
|
||||
await admin?.end();
|
||||
});
|
||||
|
||||
async function underTenant<T>(tenantId: string, run: () => Promise<T>): Promise<T> {
|
||||
await app.query("BEGIN");
|
||||
try {
|
||||
await app.query("SELECT set_config('app.tenant_id', $1, true)", [tenantId]);
|
||||
const result = await run();
|
||||
await app.query("COMMIT");
|
||||
return result;
|
||||
} catch (error) {
|
||||
await app.query("ROLLBACK");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
it("confirms the app role is neither superuser nor BYPASSRLS", async () => {
|
||||
const { rows } = await admin.query(`SELECT rolsuper, rolbypassrls FROM pg_roles WHERE rolname = $1`, [APP_ROLE]);
|
||||
expect(rows[0]).toMatchObject({ rolsuper: false, rolbypassrls: false });
|
||||
});
|
||||
|
||||
it("under tenant A, only tenant A connections are visible", async () => {
|
||||
const ids = await underTenant("rls_tenantA", async () => (await app.query(`SELECT id FROM "DataConnection" ORDER BY id`)).rows.map((r) => r.id));
|
||||
expect(ids).toEqual(["rls_dcA"]);
|
||||
});
|
||||
|
||||
it("sees no connections without a tenant context", async () => {
|
||||
expect((await app.query(`SELECT count(*)::int AS c FROM "DataConnection"`)).rows[0].c).toBe(0);
|
||||
});
|
||||
|
||||
it("WITH CHECK blocks inserting a connection for a foreign tenant", async () => {
|
||||
await expect(
|
||||
underTenant("rls_tenantA", async () =>
|
||||
app.query(`INSERT INTO "DataConnection"(id,"updatedAt","tenantId","appId","label") VALUES ('rls_dcX',now(),'rls_tenantB','rls_app1','x')`),
|
||||
),
|
||||
).rejects.toThrow(/row-level security/i);
|
||||
});
|
||||
|
||||
it("isolates AppSyncRun rows by tenant", async () => {
|
||||
const ids = await underTenant("rls_tenantA", async () => (await app.query(`SELECT id FROM "AppSyncRun" ORDER BY id`)).rows.map((r) => r.id));
|
||||
expect(ids).toEqual(["rls_runA"]);
|
||||
});
|
||||
|
||||
it("keeps the global App catalog readable under any tenant context", async () => {
|
||||
const count = await underTenant("rls_tenantA", async () => (await app.query(`SELECT count(*)::int AS c FROM "App"`)).rows[0].c);
|
||||
expect(count).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
});
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
// @vitest-environment node
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import pg from "pg";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
|
||||
// Resolve the repo root from this file's location so the SQL artifacts load
|
||||
// regardless of the cwd vitest runs under (root vs the @hivegrid/web package dir).
|
||||
// This file lives at apps/web/app/utils/db/.server/ → six segments below root.
|
||||
const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../../..");
|
||||
|
||||
// VA-P23 — EMAIL-INBOX RLS GATE PROOF. Companion to rls-file-isolation: proves
|
||||
// that once the email-inbox RLS block (prisma/rls/email-inbox-isolation.sql) is
|
||||
// applied, the restricted hivegrid_app role (NOSUPERUSER, NOBYPASSRLS) is
|
||||
// confined to its own tenant's EmailIntegrationMailbox / -Message rows, sees
|
||||
// nothing without a tenant context, and is blocked by WITH CHECK from writing
|
||||
// cross-tenant rows. This makes the P5 activation a pure switch: the wrapped
|
||||
// emailInbox db layer already runs inside withTenantRLS().
|
||||
//
|
||||
// Runs ONLY when RLS_TEST_DATABASE_URL is set (a SUPERUSER connection to a
|
||||
// throwaway DB with the schema pushed). Without it the block is skipped so the
|
||||
// normal unit suite stays green without a DB.
|
||||
//
|
||||
// Setup:
|
||||
// docker run -d --name hg-rls-pg -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=hivegrid -p 5433:5432 pgvector/pgvector:pg16
|
||||
// DIRECT_URL=postgresql://postgres:postgres@localhost:5433/hivegrid pnpm exec prisma db push
|
||||
// RLS_TEST_DATABASE_URL=postgresql://postgres:postgres@localhost:5433/hivegrid pnpm vitest run rls-email-inbox-isolation
|
||||
|
||||
const ADMIN_URL = process.env.RLS_TEST_DATABASE_URL;
|
||||
const APP_ROLE = "hivegrid_app";
|
||||
const APP_PASSWORD = "hivegrid_app_pw";
|
||||
|
||||
function appUrlFrom(adminUrl: string): string {
|
||||
const url = new URL(adminUrl);
|
||||
url.username = APP_ROLE;
|
||||
url.password = APP_PASSWORD;
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
const describeIfDb = ADMIN_URL ? describe : describe.skip;
|
||||
|
||||
describeIfDb("Email-inbox RLS tenant isolation gate proof (EmailIntegrationMailbox, EmailIntegrationMessage)", () => {
|
||||
let admin: pg.Client;
|
||||
let app: pg.Client;
|
||||
|
||||
beforeAll(async () => {
|
||||
admin = new pg.Client({ connectionString: ADMIN_URL });
|
||||
await admin.connect();
|
||||
|
||||
// 1) Base RLS mechanic + restricted role (creates hivegrid_app, NOBYPASSRLS).
|
||||
const baseSql = fs.readFileSync(path.join(REPO_ROOT, "prisma/rls/tenant-isolation.sql"), "utf8");
|
||||
await admin.query(baseSql);
|
||||
// 2) Email-inbox RLS block: ENABLE/FORCE + tenant_isolation policy + GRANTs
|
||||
// on the four VA-P23 tables (the deferred P5 activation artifact).
|
||||
const emailInboxRlsSql = fs.readFileSync(path.join(REPO_ROOT, "prisma/rls/email-inbox-isolation.sql"), "utf8");
|
||||
await admin.query(emailInboxRlsSql);
|
||||
await admin.query(`GRANT CONNECT ON DATABASE ${new URL(ADMIN_URL!).pathname.slice(1)} TO ${APP_ROLE}`);
|
||||
|
||||
// 3) Seed two tenants' mailboxes + messages as superuser (bypasses RLS).
|
||||
// Mailbox has no FK on tenantId/ownerUserId, so no User/Tenant rows needed.
|
||||
await admin.query(`INSERT INTO "EmailIntegrationMailbox"(id,"updatedAt","tenantId","ownerUserId",provider,"emailAddress","externalAccountId") VALUES
|
||||
('rls_mboxA',now(),'rls_tenantA','rls_userA','gmail','a@example.com','extA'),
|
||||
('rls_mboxB',now(),'rls_tenantB','rls_userB','microsoft','b@example.com','extB')
|
||||
ON CONFLICT (id) DO NOTHING`);
|
||||
await admin.query(`INSERT INTO "EmailIntegrationMessage"(id,"updatedAt","tenantId","mailboxId","providerMessageId",direction,"fromAddress","sentAt") VALUES
|
||||
('rls_msgA',now(),'rls_tenantA','rls_mboxA','pmA','inbound','x@example.com',now()),
|
||||
('rls_msgB',now(),'rls_tenantB','rls_mboxB','pmB','inbound','y@example.com',now())
|
||||
ON CONFLICT (id) DO NOTHING`);
|
||||
|
||||
app = new pg.Client({ connectionString: appUrlFrom(ADMIN_URL!) });
|
||||
await app.connect();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app?.end();
|
||||
await admin?.end();
|
||||
});
|
||||
|
||||
async function underTenant<T>(tenantId: string, run: () => Promise<T>): Promise<T> {
|
||||
await app.query("BEGIN");
|
||||
try {
|
||||
await app.query("SELECT set_config('app.tenant_id', $1, true)", [tenantId]);
|
||||
const result = await run();
|
||||
await app.query("COMMIT");
|
||||
return result;
|
||||
} catch (error) {
|
||||
await app.query("ROLLBACK");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
it("confirms the app role is neither superuser nor BYPASSRLS", async () => {
|
||||
const { rows } = await admin.query(`SELECT rolsuper, rolbypassrls FROM pg_roles WHERE rolname = $1`, [APP_ROLE]);
|
||||
expect(rows[0]).toMatchObject({ rolsuper: false, rolbypassrls: false });
|
||||
});
|
||||
|
||||
it("under tenant A context, only tenant A mailboxes are visible", async () => {
|
||||
const ids = await underTenant("rls_tenantA", async () => (await app.query(`SELECT id FROM "EmailIntegrationMailbox" ORDER BY id`)).rows.map((r) => r.id));
|
||||
expect(ids).toEqual(["rls_mboxA"]);
|
||||
});
|
||||
|
||||
it("cannot read another tenant's messages even with an explicit WHERE filter", async () => {
|
||||
const count = await underTenant(
|
||||
"rls_tenantA",
|
||||
async () => (await app.query(`SELECT count(*)::int AS c FROM "EmailIntegrationMessage" WHERE "tenantId" = 'rls_tenantB'`)).rows[0].c,
|
||||
);
|
||||
expect(count).toBe(0);
|
||||
});
|
||||
|
||||
it("sees no mailboxes at all when no tenant context is set", async () => {
|
||||
const count = (await app.query(`SELECT count(*)::int AS c FROM "EmailIntegrationMailbox"`)).rows[0].c;
|
||||
expect(count).toBe(0);
|
||||
});
|
||||
|
||||
it("WITH CHECK blocks inserting a message for a foreign tenant", async () => {
|
||||
await expect(
|
||||
underTenant("rls_tenantA", async () =>
|
||||
app.query(
|
||||
`INSERT INTO "EmailIntegrationMessage"(id,"updatedAt","tenantId","mailboxId","providerMessageId",direction,"fromAddress","sentAt")
|
||||
VALUES ('rls_msgX',now(),'rls_tenantB','rls_mboxB','pmX','inbound','z@example.com',now())`,
|
||||
),
|
||||
),
|
||||
).rejects.toThrow(/row-level security/i);
|
||||
});
|
||||
|
||||
it("allows inserting and reading a message for the active tenant", async () => {
|
||||
await underTenant("rls_tenantA", async () => {
|
||||
await app.query(
|
||||
`INSERT INTO "EmailIntegrationMessage"(id,"updatedAt","tenantId","mailboxId","providerMessageId",direction,"fromAddress","sentAt")
|
||||
VALUES ('rls_msgA2',now(),'rls_tenantA','rls_mboxA','pmA2','outbound','a@example.com',now())
|
||||
ON CONFLICT (id) DO NOTHING`,
|
||||
);
|
||||
const ids = (await app.query(`SELECT id FROM "EmailIntegrationMessage" ORDER BY id`)).rows.map((r) => r.id);
|
||||
expect(ids).toContain("rls_msgA2");
|
||||
expect(ids).not.toContain("rls_msgB");
|
||||
});
|
||||
});
|
||||
|
||||
it("isolates sync cursors and webhook events by tenant as well", async () => {
|
||||
await admin.query(`INSERT INTO "EmailIntegrationSyncCursor"(id,"updatedAt","tenantId","mailboxId","externalFolderId") VALUES
|
||||
('rls_curA',now(),'rls_tenantA','rls_mboxA','*'),
|
||||
('rls_curB',now(),'rls_tenantB','rls_mboxB','inbox')
|
||||
ON CONFLICT (id) DO NOTHING`);
|
||||
await admin.query(`INSERT INTO "EmailIntegrationWebhookEvent"(id,"tenantId","mailboxId",provider,"notificationPayload") VALUES
|
||||
('rls_whA','rls_tenantA','rls_mboxA','gmail','{}'),
|
||||
('rls_whB','rls_tenantB','rls_mboxB','microsoft','{}')
|
||||
ON CONFLICT (id) DO NOTHING`);
|
||||
|
||||
const cursorIds = await underTenant("rls_tenantA", async () => (await app.query(`SELECT id FROM "EmailIntegrationSyncCursor" ORDER BY id`)).rows.map((r) => r.id));
|
||||
expect(cursorIds).toEqual(["rls_curA"]);
|
||||
const eventIds = await underTenant("rls_tenantA", async () => (await app.query(`SELECT id FROM "EmailIntegrationWebhookEvent" ORDER BY id`)).rows.map((r) => r.id));
|
||||
expect(eventIds).toEqual(["rls_whA"]);
|
||||
});
|
||||
});
|
||||
+1
-1
@@ -7,7 +7,7 @@ import pg from "pg";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
|
||||
// Resolve the repo root from this file's location so the SQL artifacts load
|
||||
// regardless of the cwd vitest runs under (root vs the @toolctrl/web package dir).
|
||||
// regardless of the cwd vitest runs under (root vs the @hivegrid/web package dir).
|
||||
// This file lives at apps/web/app/utils/db/.server/ → six segments below root.
|
||||
const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../../..");
|
||||
|
||||
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
// @vitest-environment node
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import pg from "pg";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
|
||||
// Resolve the repo root from this file's location so the SQL artifacts load
|
||||
// regardless of the cwd vitest runs under. This file lives at
|
||||
// apps/web/app/utils/db/.server/ → six segments below root.
|
||||
const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../../..");
|
||||
|
||||
// AK-P17a — HR RLS GATE PROOF. Companion to the task/file/tenant isolation tests:
|
||||
// proves that once the HR RLS block (prisma/rls/hr-isolation.sql) is applied, the
|
||||
// restricted hivegrid_app role (NOSUPERUSER, NOBYPASSRLS) is confined to its own
|
||||
// tenant's HR rows (here: HrEmployeeProfile + EmployeePasswordSafeEntry), sees
|
||||
// nothing without a tenant context, and is blocked by WITH CHECK from writing
|
||||
// cross-tenant rows. This is what makes the P5 activation a pure switch: the
|
||||
// wrapped hr*Service helpers already set the GUC, so flipping RLS on cannot break
|
||||
// the module.
|
||||
//
|
||||
// Runs ONLY when RLS_TEST_DATABASE_URL is set (a SUPERUSER connection to a
|
||||
// throwaway DB with the schema pushed). Without it the block is skipped so the
|
||||
// normal unit suite stays green without a DB.
|
||||
//
|
||||
// Setup:
|
||||
// docker run -d --name hg-rls-pg -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=hivegrid -p 5433:5432 pgvector/pgvector:pg16
|
||||
// DIRECT_URL=postgresql://postgres:postgres@localhost:5433/hivegrid pnpm exec prisma db push
|
||||
// RLS_TEST_DATABASE_URL=postgresql://postgres:postgres@localhost:5433/hivegrid pnpm vitest run rls-hr-isolation
|
||||
|
||||
const ADMIN_URL = process.env.RLS_TEST_DATABASE_URL;
|
||||
const APP_ROLE = "hivegrid_app";
|
||||
const APP_PASSWORD = "hivegrid_app_pw";
|
||||
|
||||
function appUrlFrom(adminUrl: string): string {
|
||||
const url = new URL(adminUrl);
|
||||
url.username = APP_ROLE;
|
||||
url.password = APP_PASSWORD;
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
const describeIfDb = ADMIN_URL ? describe : describe.skip;
|
||||
|
||||
describeIfDb("HR RLS tenant isolation gate proof (HrEmployeeProfile, EmployeePasswordSafeEntry)", () => {
|
||||
let admin: pg.Client;
|
||||
let app: pg.Client;
|
||||
|
||||
beforeAll(async () => {
|
||||
admin = new pg.Client({ connectionString: ADMIN_URL });
|
||||
await admin.connect();
|
||||
|
||||
// 1) Base RLS mechanic + restricted role (creates hivegrid_app, NOBYPASSRLS).
|
||||
const baseSql = fs.readFileSync(path.join(REPO_ROOT, "prisma/rls/tenant-isolation.sql"), "utf8");
|
||||
await admin.query(baseSql);
|
||||
// 2) HR RLS block: ENABLE/FORCE + tenant_isolation policy + GRANTs on the HR
|
||||
// tables (the deferred P5 activation artifact, applied here).
|
||||
const hrRlsSql = fs.readFileSync(path.join(REPO_ROOT, "prisma/rls/hr-isolation.sql"), "utf8");
|
||||
await admin.query(hrRlsSql);
|
||||
await admin.query(`GRANT CONNECT ON DATABASE ${new URL(ADMIN_URL!).pathname.slice(1)} TO ${APP_ROLE}`);
|
||||
|
||||
// 3) Seed two tenants' HR rows as superuser (bypasses RLS). The HR models use
|
||||
// bare scalars (AD-21), so no User/Tenant rows are required. The vault is
|
||||
// seeded for the FK from the entry.
|
||||
await admin.query(`INSERT INTO "HrEmployeeProfile"(id,"updatedAt","tenantId","userId","position") VALUES
|
||||
('rls_hrpA',now(),'rls_tenantA','rls_userA','Engineer'),
|
||||
('rls_hrpB',now(),'rls_tenantB','rls_userB','Engineer')
|
||||
ON CONFLICT (id) DO NOTHING`);
|
||||
await admin.query(`INSERT INTO "EmployeePasswordSafeVault"(id,"updatedAt","tenantId","name","createdByUserId") VALUES
|
||||
('rls_vaultA',now(),'rls_tenantA','Vault A','rls_userA'),
|
||||
('rls_vaultB',now(),'rls_tenantB','Vault B','rls_userB')
|
||||
ON CONFLICT (id) DO NOTHING`);
|
||||
await admin.query(`INSERT INTO "EmployeePasswordSafeEntry"(id,"updatedAt","tenantId","vaultId","title") VALUES
|
||||
('rls_entryA',now(),'rls_tenantA','rls_vaultA','Entry A'),
|
||||
('rls_entryB',now(),'rls_tenantB','rls_vaultB','Entry B')
|
||||
ON CONFLICT (id) DO NOTHING`);
|
||||
|
||||
app = new pg.Client({ connectionString: appUrlFrom(ADMIN_URL!) });
|
||||
await app.connect();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app?.end();
|
||||
await admin?.end();
|
||||
});
|
||||
|
||||
async function underTenant<T>(tenantId: string, run: () => Promise<T>): Promise<T> {
|
||||
await app.query("BEGIN");
|
||||
try {
|
||||
await app.query("SELECT set_config('app.tenant_id', $1, true)", [tenantId]);
|
||||
const result = await run();
|
||||
await app.query("COMMIT");
|
||||
return result;
|
||||
} catch (error) {
|
||||
await app.query("ROLLBACK");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
it("confirms the app role is neither superuser nor BYPASSRLS", async () => {
|
||||
const { rows } = await admin.query(`SELECT rolsuper, rolbypassrls FROM pg_roles WHERE rolname = $1`, [APP_ROLE]);
|
||||
expect(rows[0]).toMatchObject({ rolsuper: false, rolbypassrls: false });
|
||||
});
|
||||
|
||||
it("under tenant A context, only tenant A employee profiles are visible", async () => {
|
||||
const ids = await underTenant("rls_tenantA", async () => (await app.query(`SELECT id FROM "HrEmployeeProfile" ORDER BY id`)).rows.map((r) => r.id));
|
||||
expect(ids).toEqual(["rls_hrpA"]);
|
||||
});
|
||||
|
||||
it("cannot read another tenant's profile even with an explicit WHERE filter", async () => {
|
||||
const count = await underTenant("rls_tenantA", async () => (await app.query(`SELECT count(*)::int AS c FROM "HrEmployeeProfile" WHERE "tenantId" = 'rls_tenantB'`)).rows[0].c);
|
||||
expect(count).toBe(0);
|
||||
});
|
||||
|
||||
it("sees no HR rows at all when no tenant context is set", async () => {
|
||||
const count = (await app.query(`SELECT count(*)::int AS c FROM "HrEmployeeProfile"`)).rows[0].c;
|
||||
expect(count).toBe(0);
|
||||
});
|
||||
|
||||
it("WITH CHECK blocks inserting a profile for a foreign tenant", async () => {
|
||||
await expect(
|
||||
underTenant("rls_tenantA", async () =>
|
||||
app.query(`INSERT INTO "HrEmployeeProfile"(id,"updatedAt","tenantId","userId") VALUES ('rls_hrpX',now(),'rls_tenantB','rls_userB')`),
|
||||
),
|
||||
).rejects.toThrow(/row-level security/i);
|
||||
});
|
||||
|
||||
it("isolates password-safe entries by tenant as well", async () => {
|
||||
const ids = await underTenant("rls_tenantA", async () => (await app.query(`SELECT id FROM "EmployeePasswordSafeEntry" ORDER BY id`)).rows.map((r) => r.id));
|
||||
expect(ids).toEqual(["rls_entryA"]);
|
||||
const crossCount = await underTenant("rls_tenantA", async () => (await app.query(`SELECT count(*)::int AS c FROM "EmployeePasswordSafeEntry" WHERE "tenantId" = 'rls_tenantB'`)).rows[0].c);
|
||||
expect(crossCount).toBe(0);
|
||||
});
|
||||
});
|
||||
+1
-1
@@ -7,7 +7,7 @@ import pg from "pg";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
|
||||
// Resolve the repo root from this file's location so the SQL artifacts load
|
||||
// regardless of the cwd vitest runs under (root vs the @toolctrl/web package dir).
|
||||
// regardless of the cwd vitest runs under (root vs the @hivegrid/web package dir).
|
||||
// This file lives at apps/web/app/utils/db/.server/ → six segments below root.
|
||||
const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../../..");
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
import type { Prisma } from "@toolctrl/db/prisma-browser";
|
||||
import type { Prisma } from "@hivegrid/db/prisma-browser";
|
||||
|
||||
import { db } from "~/utils/db.server";
|
||||
|
||||
|
||||
Reference in new issue
Block a user