|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// ADR-0056 — declarative `private` OWD proof on the REAL showcase app. |
| 4 | +// `showcase_private_note` declares `sharingModel: 'private'` and NOTHING else: |
| 5 | +// no RLS policy, no owner predicate, no permission-set rule. Two plain sign-ups |
| 6 | +// (governed only by the default member set) each create notes; the engine scopes |
| 7 | +// every read/write to the owner purely from the OWD baseline + the auto-stamped |
| 8 | +// `owner_id`. This is the canonical "declare one word, get owner isolation" |
| 9 | +// capability — proven end-to-end through the real HTTP stack. |
| 10 | + |
| 11 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 12 | +import showcaseStack from '@objectstack/example-showcase'; |
| 13 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 14 | + |
| 15 | +const OBJ = '/data/showcase_private_note'; |
| 16 | +const idOf = (b: any) => b?.id ?? b?.record?.id ?? b?.data?.id ?? b?.recordId; |
| 17 | + |
| 18 | +describe('showcase: declarative private OWD (ADR-0056)', () => { |
| 19 | + let stack: VerifyStack; |
| 20 | + let aToken: string; |
| 21 | + let bToken: string; |
| 22 | + let aNoteId: string; |
| 23 | + let bNoteId: string; |
| 24 | + |
| 25 | + beforeAll(async () => { |
| 26 | + stack = await bootStack(showcaseStack); |
| 27 | + await stack.signIn(); // seed dev admin (first user) |
| 28 | + aToken = await stack.signUp('owd-alice@verify.test'); |
| 29 | + bToken = await stack.signUp('owd-bob@verify.test'); |
| 30 | + |
| 31 | + const a1 = await stack.apiAs(aToken, 'POST', OBJ, { title: 'Alice private 1', body: 'a-body' }); |
| 32 | + expect(a1.status, 'alice creates note').toBeLessThan(300); |
| 33 | + aNoteId = idOf(await a1.json()); |
| 34 | + await stack.apiAs(aToken, 'POST', OBJ, { title: 'Alice private 2' }); |
| 35 | + |
| 36 | + const b1 = await stack.apiAs(bToken, 'POST', OBJ, { title: 'Bob private 1' }); |
| 37 | + expect(b1.status, 'bob creates note').toBeLessThan(300); |
| 38 | + bNoteId = idOf(await b1.json()); |
| 39 | + |
| 40 | + expect(aNoteId, 'alice note id').toBeTruthy(); |
| 41 | + expect(bNoteId, 'bob note id').toBeTruthy(); |
| 42 | + }, 60_000); |
| 43 | + |
| 44 | + afterAll(async () => { await stack?.stop(); }); |
| 45 | + |
| 46 | + it('owner_id is auto-stamped (no manual assignment, no predicate)', async () => { |
| 47 | + const ql: any = await stack.kernel.getServiceAsync('objectql'); |
| 48 | + const row = await ql.findOne('showcase_private_note', { where: { id: aNoteId }, context: { isSystem: true } }); |
| 49 | + expect(row?.owner_id, 'owner_id stamped to a real user id').toBeTruthy(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('a member LISTS only the notes they own', async () => { |
| 53 | + const r = await stack.apiAs(aToken, 'GET', OBJ); |
| 54 | + expect(r.status).toBe(200); |
| 55 | + const body: any = await r.json(); |
| 56 | + const titles: string[] = (body.records ?? body.data ?? body ?? []).map((x: any) => x.title); |
| 57 | + expect(titles).toContain('Alice private 1'); |
| 58 | + expect(titles).toContain('Alice private 2'); |
| 59 | + expect(titles).not.toContain('Bob private 1'); // owner isolation, no RLS authored |
| 60 | + }); |
| 61 | + |
| 62 | + it('a member cannot READ another owner’s note by id', async () => { |
| 63 | + const r = await stack.apiAs(aToken, 'GET', `${OBJ}/${bNoteId}`); |
| 64 | + expect(r.status, 'alice must not read bob note').not.toBe(200); |
| 65 | + }); |
| 66 | + |
| 67 | + it('a member cannot WRITE another owner’s note, but can write their own', async () => { |
| 68 | + const foreign = await stack.apiAs(aToken, 'PATCH', `${OBJ}/${bNoteId}`, { body: 'hacked' }); |
| 69 | + expect(foreign.status, 'alice must not edit bob note').not.toBeLessThan(300); |
| 70 | + |
| 71 | + const own = await stack.apiAs(aToken, 'PATCH', `${OBJ}/${aNoteId}`, { body: 'updated' }); |
| 72 | + expect(own.status, 'alice edits her own note').toBeLessThan(300); |
| 73 | + }); |
| 74 | + |
| 75 | + it('the OTHER member is symmetric (bob sees only his own)', async () => { |
| 76 | + const r = await stack.apiAs(bToken, 'GET', OBJ); |
| 77 | + const body: any = await r.json(); |
| 78 | + const titles: string[] = (body.records ?? body.data ?? body ?? []).map((x: any) => x.title); |
| 79 | + expect(titles).toContain('Bob private 1'); |
| 80 | + expect(titles).not.toContain('Alice private 1'); |
| 81 | + const foreign = await stack.apiAs(bToken, 'GET', `${OBJ}/${aNoteId}`); |
| 82 | + expect(foreign.status).not.toBe(200); |
| 83 | + }); |
| 84 | +}); |
0 commit comments