Caretaker Express · PostgreSQL · Cedar

Checkpoint · Lesson 8.3

An isolation test suite: checkpoint files

Git tag in the reference repository
lesson/m08-l03 (commit 20db85d)
Replay result
All steps replayed from an empty folder
Tests at this point
pass (tests 120, pass 120, fail 0)

Compare your folder with the reference using Git, if you cloned it: git diff lesson/m08-l03 -- src.

Files changed in this lesson

test/tenant-isolation.test.js added

test/tenant-isolation.test.js
import { test, before, after } from 'node:test';import assert from 'node:assert/strict';import { pool } from '../src/db.js';import { resetDatabase, loginAs } from './helpers.js'; const TABLES = ['organisations', 'buildings', 'users', 'memberships', 'building_role_assignments', 'issues', 'inspections', 'inspection_status_changes', 'permission_changes']; async function fingerprint() {  const result = {};  for (const table of TABLES) {    const { rows } = await pool.query(`SELECT md5(coalesce(string_agg(t::text, '|' ORDER BY t::text), '')) AS hash FROM ${table} t`);    result[table] = rows[0].hash;  }  return result;} // Every route, pointed at Liffey (organisation 1, buildings 10 and 20).const ATTACKS_ON_LIFFEY = [  ['GET', '/organisations/1'],  ['GET', '/organisations/1/edit'],  ['POST', '/organisations/1/edit', { name: 'Taken over' }],  ['POST', '/organisations/1/delete'],  ['GET', '/organisations/1/members'],  ['POST', '/organisations/1/members', { email: 'bob@example.test', role: 'org_admin' }],  ['GET', '/organisations/1/buildings/new'],  ['POST', '/organisations/1/buildings', { name: 'Hidden annex' }],  ['GET', '/buildings/10'],  ['GET', '/buildings/10/edit'],  ['POST', '/buildings/10/edit', { name: 'Renamed' }],  ['POST', '/buildings/10/delete'],  ['GET', '/buildings/10/access'],  ['POST', '/buildings/10/access', { user_id: '2', role: 'building_admin' }],  ['POST', '/buildings/10/access/1/revoke'],  ['GET', '/issues/101'],  ['GET', '/issues/101/edit'],  ['POST', '/issues/101/edit', { title: 'Changed', priority: 'low', status: 'closed', occurred_at: '2026-11-02T09:15' }],  ['GET', '/issues/101/move'],  ['POST', '/issues/101/move', { building_id: '30' }],  ['POST', '/issues/101/delete'],  ['GET', '/inspections/201'],  ['GET', '/inspections/203/edit'],  ['POST', '/inspections/203/edit', { title: 'Changed', findings: '' }],  ['POST', '/inspections/203/submit'],  ['POST', '/inspections/201/approve'],  ['POST', '/inspections/201/return'],]; const CREATES_IN_LIFFEY = [  ['POST', '/issues', { title: 'Injected issue', building_id: '10', priority: 'low', occurred_at: '2026-11-22T09:00' }],  ['POST', '/inspections', { title: 'Injected inspection', building_id: '20', findings: '' }],]; const send = (agent, [method, path, form]) => (method === 'GET' ? agent.get(path) : agent.post(path).type('form').send(form ?? {})); let agents;before(async () => {  await resetDatabase();  agents = { bob: await loginAs('bob'), ravi: await loginAs('ravi'), nora: await loginAs('nora') };});after(() => pool.end()); for (const name of ['bob', 'ravi', 'nora']) {  test(`${name} gets 404 for every Liffey route, and nothing changes`, async () => {    const before = await fingerprint();    const unexpected = [];    for (const attack of ATTACKS_ON_LIFFEY) {      const res = await send(agents[name], attack);      if (res.status !== 404) unexpected.push(`${attack[0]} ${attack[1]} -> ${res.status}`);    }    for (const attack of CREATES_IN_LIFFEY) {      const res = await send(agents[name], attack);      if (res.status !== 400) unexpected.push(`${attack[0]} ${attack[1]} -> ${res.status}`);    }    assert.deepEqual(unexpected, []);    assert.deepEqual(await fingerprint(), before);  });} test('inside one tenant: alice, a viewer at building 20, cannot change anything there', async () => {  const alice = await loginAs('alice');  const before = await fingerprint();  const attempts = [    ['GET', '/issues/103/edit', 403],    ['POST', '/issues/103/edit', 403, { title: 'Changed', priority: 'low', status: 'closed', occurred_at: '2026-11-08T07:05' }],    ['POST', '/issues/104/delete', 403],    ['GET', '/buildings/20/edit', 403],    ['GET', '/buildings/20/access', 403],    ['POST', '/buildings/20/access/5/revoke', 403],    ['POST', '/buildings/10/access/5/revoke', 404],    ['POST', '/inspections/203/submit', 403],  ];  const unexpected = [];  for (const [method, path, expected, form] of attempts) {    const res = await send(alice, [method, path, form]);    if (res.status !== expected) unexpected.push(`${method} ${path} -> ${res.status}, expected ${expected}`);  }  assert.deepEqual(unexpected, []);  assert.deepEqual(await fingerprint(), before);});

Project tree at this checkpoint

.env.example
.env.test
.gitignore
.npmrc
compose.yaml
db/init/01-create-test-database.sql
migrations/001_create_organisations_buildings_issues.sql
migrations/002_create_inspections.sql
migrations/003_create_users.sql
migrations/004_create_session_table.sql
migrations/005_create_memberships_and_building_roles.sql
migrations/006_create_permission_changes.sql
package-lock.json
package.json
public/css/style.css
public/js/app.js
scripts/authz-bench.js
scripts/authz-explain.js
scripts/cedar-errors-demo.js
scripts/cedar-hello.js
scripts/migrate.js
scripts/partial-demo.js
scripts/seed.js
scripts/template-demo.js
scripts/validate-policies.js
src/app.js
src/authz/authorize.js
src/authz/cedar.js
src/authz/entities.js
src/authz/policies/building-admin.cedar
src/authz/policies/building-read.cedar
src/authz/policies/forbid-building-admin-grants.cedar
src/authz/policies/forbid-self-approval.cedar
src/authz/policies/forbid-self-membership-change.cedar
src/authz/policies/forbid-self-role-change.cedar
src/authz/policies/inspector-create-inspection.cedar
src/authz/policies/inspector-own-inspections.cedar
src/authz/policies/org-admin.cedar
src/authz/policies/org-member.cedar
src/authz/policies/reporter-create-issue.cedar
src/authz/policies/reporter-own-issues.cedar
src/authz/policy-store.js
src/authz/request.js
src/authz/rules.js
src/authz/schema.cedarschema
src/config.js
src/db.js
src/lib/http.js
src/lib/ids.js
src/lib/validation.js
src/middleware/auth.js
src/middleware/csrf.js
src/middleware/flash.js
src/middleware/rate-limit.js
src/migrate.js
src/models/access.js
src/models/assignments.js
src/models/buildings.js
src/models/dashboard.js
src/models/inspections.js
src/models/issues.js
src/models/members.js
src/models/organisations.js
src/models/scope.js
src/models/users.js
src/routes/account.js
src/routes/auth.js
src/routes/buildings.js
src/routes/inspections.js
src/routes/issues.js
src/routes/organisations.js
src/seed.js
src/server.js
test/access-matrix.test.js
test/access.test.js
test/auth.test.js
test/authz-decisions.test.js
test/building-access.test.js
test/buildings.test.js
test/csrf.test.js
test/dashboard.test.js
test/helpers.js
test/inspections.test.js
test/isolation.test.js
test/issue-authz.test.js
test/issue-list.test.js
test/issue-moves.test.js
test/issues.test.js
test/list-parity.test.js
test/members.test.js
test/organisations.test.js
test/policies.test.js
test/rate-limit.test.js
test/require-login.test.js
test/resource-authz.test.js
test/tenant-isolation.test.js
views/account.ejs
views/auth/login.ejs
views/auth/register.ejs
views/buildings/access.ejs
views/buildings/form.ejs
views/buildings/show.ejs
views/dashboard.ejs
views/error.ejs
views/forbidden.ejs
views/inspections/form.ejs
views/inspections/index.ejs
views/inspections/show.ejs
views/issues/form.ejs
views/issues/index.ejs
views/issues/move.ejs
views/issues/show.ejs
views/not-found.ejs
views/organisations/form.ejs
views/organisations/index.ejs
views/organisations/members.ejs
views/organisations/show.ejs
views/partials/foot.ejs
views/partials/head.ejs
views/partials/nav.ejs

Back to lesson 8.3