Files
lunch/tests/rls.test.ts
T
Kai BöhmandClaude Opus 5.5 fa83e560b2
ci / olares chart · sanity (pull_request) Successful in 5s
ci / app · types + tests + build (pull_request) Successful in 3m54s
release / which version (pull_request) Successful in 1s
release / olares chart · sanity (pull_request) Successful in 3s
release / app (pull_request) Successful in 1m25s
release / tag and release (pull_request) Skipped
Scaffold the app, chart, CI and release (26.11.1)
Next.js 15 with next-intl (de, en, fr, es; locale per person, no URL
prefix), Tailwind v4 tokens for the warm palette with dark mode, a
shadcn/ui button, Serwist service worker and PWA manifest with icons.
One Postgres pool with withHousehold() setting app.household_id per
transaction; the first migration creates households with FORCE ROW
LEVEL SECURITY, and a two-household test checks the isolation as the
table owner. scripts/migrate.mjs applies db/migrations once each with a
checksum and retries until the database is reachable.

Olares chart with one deployment (init: chown, migrate from the same
image), ClusterIP service, entrance internal until the own sign-in
exists, image path fixed in the template and tag from appVersion.
scripts/check-chart.sh carries the rules from Rocket; CI runs it, type
check, tests against Postgres and the build; release builds
lunch-app, tags and attaches the chart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HUc2nv7rJVU2gp3Bgtha3q
2026-11-06 19:08:21 +00:00

63 lines
2.5 KiB
TypeScript

import { randomUUID } from "node:crypto";
import pg from "pg";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
// Der Test mit zwei Haushalten (CLAUDE.md, Regel 3). Läuft gegen eine
// migrierte Datenbank als Eigentümerin der Tabellen — genau wie die App auf
// der Box. Ohne FORCE sähe diese Rolle alles, und der Test bräche.
const url = process.env.TEST_DATABASE_URL;
describe.skipIf(!url)("row level security", () => {
const client = new pg.Client({ connectionString: url });
const a = randomUUID();
const b = randomUUID();
async function asHousehold<T>(id: string | null, fn: () => Promise<T>): Promise<T> {
await client.query("begin");
try {
if (id) await client.query("select set_config('app.household_id', $1, true)", [id]);
return await fn();
} finally {
await client.query("commit");
}
}
beforeAll(async () => {
await client.connect();
await asHousehold(a, () => client.query("insert into households (id, name) values ($1, 'A')", [a]));
await asHousehold(b, () => client.query("insert into households (id, name) values ($1, 'B')", [b]));
});
afterAll(async () => {
await client.end();
});
it("every table with household data forces RLS", async () => {
const { rows } = await client.query(`
select c.relname, c.relrowsecurity, c.relforcerowsecurity
from pg_class c join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public' and c.relkind = 'r' and c.relname <> 'schema_migrations'`);
expect(rows.length).toBeGreaterThan(0);
for (const r of rows) expect([r.relname, r.relrowsecurity, r.relforcerowsecurity]).toEqual([r.relname, true, true]);
});
it("a household sees only itself", async () => {
const seen = await asHousehold(a, () => client.query("select id from households"));
expect(seen.rows.map((r) => r.id)).toEqual([a]);
});
it("without a household nothing is visible", async () => {
const seen = await asHousehold(null, () => client.query("select count(*)::int as n from households"));
expect(seen.rows[0].n).toBe(0);
});
it("a household cannot write into another", async () => {
await expect(
asHousehold(a, () => client.query("update households set name = 'X' where id = $1 returning id", [b])),
).resolves.toMatchObject({ rowCount: 0 });
await expect(
asHousehold(a, () => client.query("insert into households (id, name) values ($1, 'C')", [randomUUID()])),
).rejects.toThrow(/row-level security/);
});
});