|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// SHOWCASE proof for ADR-0056 Option A — the `showcase_inquiry` web-to-lead |
| 4 | +// PUBLIC FORM. `views/inquiry.view.ts` declares a FormView with |
| 5 | +// `sharing.allowAnonymous: true` + `publicLink: '/forms/contact-us'`, which |
| 6 | +// wires the anonymous REST endpoints. This exercises them end-to-end over the |
| 7 | +// real HTTP stack — the harness boots with `requireAuth: true`, so a passing |
| 8 | +// anonymous submit proves the route works under SECURE-BY-DEFAULT auth, with |
| 9 | +// NO `guest_portal` profile, authorized solely by the declaration-derived |
| 10 | +// `publicFormGrant` (create + read-back on `showcase_inquiry` ONLY). |
| 11 | + |
| 12 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 13 | +import showcaseStack from '@objectstack/example-showcase'; |
| 14 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 15 | +import { SecurityPlugin, securityDefaultPermissionSets } from '@objectstack/plugin-security'; |
| 16 | + |
| 17 | +describe('showcase: web-to-lead public form (ADR-0056 Option A)', () => { |
| 18 | + let stack: VerifyStack; |
| 19 | + |
| 20 | + beforeAll(async () => { |
| 21 | + stack = await bootStack(showcaseStack, { |
| 22 | + security: new SecurityPlugin({ |
| 23 | + defaultPermissionSets: [...securityDefaultPermissionSets], |
| 24 | + }), |
| 25 | + }); |
| 26 | + }, 60_000); |
| 27 | + |
| 28 | + afterAll(async () => { |
| 29 | + await stack?.stop(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('GET /forms/:slug returns the form + a whitelisted schema (no auth)', async () => { |
| 33 | + const r = await stack.api('/forms/contact-us'); |
| 34 | + expect(r.status, 'anonymous form resolve must succeed').toBe(200); |
| 35 | + const body = (await r.json()) as { object: string; form: unknown; objectSchema: { fields: Record<string, unknown> } }; |
| 36 | + expect(body.object).toBe('showcase_inquiry'); |
| 37 | + // Only the whitelisted form fields are exposed — server-controlled fields are absent. |
| 38 | + const fields = Object.keys(body.objectSchema.fields); |
| 39 | + expect(fields.sort()).toEqual(['company', 'email', 'message', 'name']); |
| 40 | + expect(fields).not.toContain('status'); |
| 41 | + expect(fields).not.toContain('source'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('POST /forms/:slug/submit creates an inquiry anonymously under requireAuth (Option A)', async () => { |
| 45 | + const r = await stack.api('/forms/contact-us/submit', { |
| 46 | + method: 'POST', |
| 47 | + headers: { 'content-type': 'application/json' }, |
| 48 | + body: JSON.stringify({ |
| 49 | + name: 'Ada Lovelace', |
| 50 | + email: 'ada@example.com', |
| 51 | + company: 'Analytical Engines Ltd', |
| 52 | + message: 'Please contact me about a pilot.', |
| 53 | + status: 'closed', // ← not in whitelist → stripped; hook stamps 'new' |
| 54 | + }), |
| 55 | + }); |
| 56 | + expect(r.status, 'anonymous submit must succeed under requireAuth=true').toBe(201); |
| 57 | + const body = (await r.json()) as { object: string; id: string; record: Record<string, unknown> }; |
| 58 | + expect(body.object).toBe('showcase_inquiry'); |
| 59 | + expect(body.record.name).toBe('Ada Lovelace'); |
| 60 | + // Server-controlled: whitelist stripped the client `status`, the hook stamped defaults. |
| 61 | + expect(body.record.status, 'status is server-stamped, not client-set').toBe('new'); |
| 62 | + expect(body.record.source).toBe('web'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('the public grant is create + read-back ONLY — anonymous cannot list inquiries', async () => { |
| 66 | + const r = await stack.api('/data/showcase_inquiry'); |
| 67 | + expect(r.status, 'general anonymous read must NOT be opened by the form grant').not.toBe(200); |
| 68 | + }); |
| 69 | +}); |
0 commit comments