wren/glyph Public Public: anyone can read this; nobody can change it.
forked from apps/glyph
workbenchthis machine
Files acd87b6b6146da11e0c9272c92a02a8e707adf4a
| 1 | /** |
| 2 | * Every door answers as the table says. The test acts as each identity the |
| 3 | * contract declares — on a note that names them and on one that names |
| 4 | * someone else — and calls the real actions. A rule written by hand in an |
| 5 | * action, or a cell changed without its door, fails here. |
| 6 | */ |
| 7 | import { expect, test } from "bun:test"; |
| 8 | import { mkdtempSync } from "node:fs"; |
| 9 | import { tmpdir } from "node:os"; |
| 10 | import { join } from "node:path"; |
| 11 | import { |
| 12 | addNote, |
| 13 | deleteNote, |
| 14 | getAudit, |
| 15 | getNote, |
| 16 | listAudit, |
| 17 | listNotes, |
| 18 | updateNote, |
| 19 | } from "./actions.ts"; |
| 20 | import { record } from "./audit.ts"; |
| 21 | import { actingAs, identities, table, type Verb } from "./can.ts"; |
| 22 | import { contract } from "./contract.ts"; |
| 23 | import { openDb } from "./db.ts"; |
| 24 | import type { Who } from "./who.ts"; |
| 25 | |
| 26 | const db = openDb(mkdtempSync(join(tmpdir(), "app-contract-"))); |
| 27 | |
| 28 | /** The identity the evaluator acts as, as the forge would have signed it. */ |
| 29 | function whoIs(identity: string): Who { |
| 30 | const { actor } = actingAs(contract, identity); |
| 31 | const role = actor.roles[0]; |
| 32 | if (actor.id === null || (role !== "user" && role !== "admin")) |
| 33 | return { role: "anon" }; |
| 34 | return { role, id: actor.id, name: identity }; |
| 35 | } |
| 36 | |
| 37 | const noteBy = (authorId: string): number => |
| 38 | db |
| 39 | .query< |
| 40 | { id: number }, |
| 41 | [string] |
| 42 | >("INSERT INTO note (body, author_id, author_name) VALUES ('n', ?1, ?1) RETURNING id") |
| 43 | .get(authorId)!.id; |
| 44 | |
| 45 | const auditRow = (): number => { |
| 46 | record(db, { |
| 47 | who: { role: "anon" }, |
| 48 | resource: "note", |
| 49 | verb: "create", |
| 50 | outcome: "signin_required", |
| 51 | }); |
| 52 | return db.query<{ id: number }, []>("SELECT max(id) AS id FROM audit").get()! |
| 53 | .id; |
| 54 | }; |
| 55 | |
| 56 | /** One door per verb an action serves: did it let `who` through, on `row`? */ |
| 57 | const doors: Record< |
| 58 | string, |
| 59 | Partial<Record<Verb, (who: Who, authorId: string) => boolean>> |
| 60 | > = { |
| 61 | note: { |
| 62 | create: (who) => addNote(db, who, { body: "hello" }).ok, |
| 63 | list: (who) => listNotes(db, who).ok, |
| 64 | read: (who, authorId) => getNote(db, who, { id: noteBy(authorId) }).ok, |
| 65 | update: (who, authorId) => |
| 66 | updateNote(db, who, { id: noteBy(authorId), body: "changed" }).ok, |
| 67 | delete: (who, authorId) => deleteNote(db, who, { id: noteBy(authorId) }).ok, |
| 68 | }, |
| 69 | // The trail is written by the app itself, never through a door: there |
| 70 | // is no action that creates, changes or removes a row of it. |
| 71 | audit: { |
| 72 | list: (who) => listAudit(db, who).ok, |
| 73 | read: (who) => getAudit(db, who, { id: auditRow() }).ok, |
| 74 | create: () => false, |
| 75 | update: () => false, |
| 76 | delete: () => false, |
| 77 | }, |
| 78 | }; |
| 79 | |
| 80 | test("the contract names the identities the forge signs", () => { |
| 81 | expect(identities(contract).sort()).toEqual(["admin", "anon", "user"]); |
| 82 | }); |
| 83 | |
| 84 | test("every resource in the contract has every door here", () => { |
| 85 | expect(Object.keys(doors).sort()).toEqual( |
| 86 | Object.keys(contract.resources).sort(), |
| 87 | ); |
| 88 | for (const resource of Object.keys(contract.resources)) |
| 89 | expect(Object.keys(doors[resource] ?? {}).sort()).toEqual([ |
| 90 | "create", |
| 91 | "delete", |
| 92 | "list", |
| 93 | "read", |
| 94 | "update", |
| 95 | ]); |
| 96 | }); |
| 97 | |
| 98 | test("every door answers as the table says, as every identity", () => { |
| 99 | let asked = 0; |
| 100 | for (const cell of table(contract)) { |
| 101 | const door = doors[cell.resource]?.[cell.verb]; |
| 102 | if (door === undefined) continue; |
| 103 | const who = whoIs(cell.as); |
| 104 | const { mine, theirs } = actingAs(contract, cell.as); |
| 105 | const onMine = door(who, String(mine["author_id"])); |
| 106 | const onTheirs = door(who, String(theirs["author_id"])); |
| 107 | const said = `${cell.resource}.${cell.verb} as ${cell.as}`; |
| 108 | asked++; |
| 109 | if (cell.answer === "yes") |
| 110 | expect([said, onMine, onTheirs]).toEqual([said, true, true]); |
| 111 | else if (cell.answer === "no") |
| 112 | expect([said, onMine, onTheirs]).toEqual([said, false, false]); |
| 113 | else if (cell.verb === "list") expect([said, onMine]).toEqual([said, true]); |
| 114 | else expect([said, onMine, onTheirs]).toEqual([said, true, false]); |
| 115 | } |
| 116 | expect(asked).toBeGreaterThan(0); |
| 117 | }); |