|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// @proof: readonly-static-write |
| 4 | +// |
| 5 | +// #2948 / #3003 — static `readonly: true` is SERVER-enforced on UPDATE, not a |
| 6 | +// UI-only affordance. The #3003 field report: an approval-flow object declared |
| 7 | +// `approval_status` / `approval_stage` as `readonly: true`, the create/edit |
| 8 | +// forms never rendered them — and a logged-in, non-admin user forged all of |
| 9 | +// them (plus an amount column) with one direct REST PATCH from the same |
| 10 | +// session, self-approving a 4-stage approval. The strip added for #2948 |
| 11 | +// (`stripReadonlyFields`, objectql/engine.ts) closes exactly that: on a |
| 12 | +// non-system UPDATE, caller-supplied writes to statically-readonly fields are |
| 13 | +// silently dropped (HTTP 200, persisted value kept) — symmetric with the |
| 14 | +// `readonlyWhen` strip. INSERT is deliberately exempt (a create may seed a |
| 15 | +// readonly column: defaultValue, import, migration), matching `readonlyWhen`. |
| 16 | +// |
| 17 | +// Proven here on the REAL showcase app over HTTP: `showcase_contact.lead_score` |
| 18 | +// is the stand-in for the #3003 approval/status/amount columns — readonly, |
| 19 | +// "computed by scoring rules — not user-editable", never on the create form. |
| 20 | + |
| 21 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 22 | +import showcaseStack from '@objectstack/example-showcase'; |
| 23 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 24 | + |
| 25 | +const OBJ = '/data/showcase_contact'; |
| 26 | +const idOf = (b: any) => b?.id ?? b?.record?.id ?? b?.data?.id ?? b?.recordId; |
| 27 | +const recordOf = (b: any) => b?.record ?? b?.data ?? b; |
| 28 | + |
| 29 | +describe('showcase: static readonly write enforcement (#2948 / #3003)', () => { |
| 30 | + let stack: VerifyStack; |
| 31 | + let token: string; |
| 32 | + let contactId: string; |
| 33 | + |
| 34 | + beforeAll(async () => { |
| 35 | + stack = await bootStack(showcaseStack); |
| 36 | + await stack.signIn(); |
| 37 | + token = await stack.signUp('ro-worker@verify.test'); |
| 38 | + |
| 39 | + // INSERT exemption (documented contract, symmetric with `readonlyWhen`): |
| 40 | + // a create MAY seed a readonly column — the scoring pipeline, an import, |
| 41 | + // or a migration legitimately writes the initial value. |
| 42 | + const created = await stack.apiAs(token, 'POST', OBJ, { |
| 43 | + name: 'Readonly Probe', |
| 44 | + email: 'ro-probe@verify.test', |
| 45 | + lead_score: 10, |
| 46 | + }); |
| 47 | + expect(created.status).toBeLessThan(300); |
| 48 | + contactId = idOf(await created.json()); |
| 49 | + expect(contactId).toBeTruthy(); |
| 50 | + }, 60_000); |
| 51 | + |
| 52 | + afterAll(async () => { await stack?.stop(); }); |
| 53 | + |
| 54 | + it('INSERT may seed the readonly field (documented exemption)', async () => { |
| 55 | + const res = await stack.apiAs(token, 'GET', `${OBJ}/${contactId}`); |
| 56 | + expect(res.status).toBe(200); |
| 57 | + expect(recordOf(await res.json()).lead_score, 'insert-seeded value persisted').toBe(10); |
| 58 | + }); |
| 59 | + |
| 60 | + it('a direct PATCH forging the readonly field is silently stripped — sibling editable fields still land', async () => { |
| 61 | + // The #3003 move: same logged-in session, straight to the REST API with a |
| 62 | + // payload the UI would never produce. |
| 63 | + const forge = await stack.apiAs(token, 'PATCH', `${OBJ}/${contactId}`, { |
| 64 | + lead_score: 99999, |
| 65 | + notes: 'legitimate edit in the same payload', |
| 66 | + }); |
| 67 | + // The strip is SILENT by contract (like `readonlyWhen`): 200, not 4xx. |
| 68 | + expect(forge.status, 'strip is silent — the request succeeds').toBe(200); |
| 69 | + |
| 70 | + const after = recordOf(await (await stack.apiAs(token, 'GET', `${OBJ}/${contactId}`)).json()); |
| 71 | + expect(after.lead_score, 'forged readonly value must NOT persist').toBe(10); |
| 72 | + expect(after.notes, 'editable field from the same payload still lands').toBe( |
| 73 | + 'legitimate edit in the same payload', |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it('a PATCH carrying ONLY the forged readonly field is a no-op on the record', async () => { |
| 78 | + const forge = await stack.apiAs(token, 'PATCH', `${OBJ}/${contactId}`, { lead_score: -1 }); |
| 79 | + expect(forge.status).toBe(200); |
| 80 | + |
| 81 | + const after = recordOf(await (await stack.apiAs(token, 'GET', `${OBJ}/${contactId}`)).json()); |
| 82 | + expect(after.lead_score, 'readonly value survives an all-forged payload').toBe(10); |
| 83 | + }); |
| 84 | +}); |
0 commit comments