|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// SHOWCASE proof for ADR-0056 D7 — the app-declared default profile, wired the |
| 4 | +// way the CLI wires it. The showcase declares `showcase_member_default` with |
| 5 | +// `isDefault: true`; `appDefaultProfileName(stack.permissions)` (the helper the |
| 6 | +// CLI calls) extracts its name, and passing it as the SecurityPlugin |
| 7 | +// `fallbackPermissionSet` makes a fresh sign-up governed by THAT profile instead |
| 8 | +// of the built-in `member_default` wildcard. Read-mostly default ⇒ the member |
| 9 | +// can read announcements but is DENIED the private-note object (which the |
| 10 | +// wildcard would have allowed) — proving the app's declared default is in force. |
| 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, appDefaultProfileName } from '@objectstack/plugin-security'; |
| 16 | +import { PermissionSetSchema, type PermissionSet } from '@objectstack/spec/security'; |
| 17 | + |
| 18 | +// Mirror the CLI: pull the app-declared default profile (name + object) off the |
| 19 | +// stack metadata via the same helper the CLI uses. |
| 20 | +const stackPerms = ((showcaseStack as { permissions?: unknown[] }).permissions ?? []) as Array<{ name?: string }>; |
| 21 | +const appDefault = appDefaultProfileName(stackPerms); |
| 22 | +const declaredDefault = stackPerms.find((p) => p?.name === appDefault) as unknown; |
| 23 | + |
| 24 | +describe('showcase: app-declared default profile, CLI-wired (ADR-0056 D7)', () => { |
| 25 | + let stack: VerifyStack; |
| 26 | + let memberToken: string; |
| 27 | + |
| 28 | + beforeAll(async () => { |
| 29 | + // The full CLI boot loads stack permission sets into the metadata service, so |
| 30 | + // `fallbackPermissionSet: <name>` resolves there. The lightweight harness does |
| 31 | + // not seed permission metadata, so we hand the declared default to the plugin |
| 32 | + // directly — then wire it by NAME exactly as the CLI's appDefaultProfileName |
| 33 | + // path does (constructor uses the explicit name, not its own isDefault scan). |
| 34 | + stack = await bootStack(showcaseStack, { |
| 35 | + security: new SecurityPlugin({ |
| 36 | + defaultPermissionSets: [...securityDefaultPermissionSets, PermissionSetSchema.parse(declaredDefault) as PermissionSet], |
| 37 | + fallbackPermissionSet: appDefault, |
| 38 | + }), |
| 39 | + }); |
| 40 | + await stack.signIn(); |
| 41 | + memberToken = await stack.signUp('d7-showcase-member@verify.test'); |
| 42 | + }, 60_000); |
| 43 | + |
| 44 | + afterAll(async () => { await stack?.stop(); }); |
| 45 | + |
| 46 | + it('appDefaultProfileName extracts the showcase default profile from stack metadata', () => { |
| 47 | + expect(appDefault).toBe('showcase_member_default'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('a fresh member is governed by the app-declared default (reads announcements)', async () => { |
| 51 | + const r = await stack.apiAs(memberToken, 'GET', '/data/showcase_announcement'); |
| 52 | + expect(r.status, 'declared default grants announcement read').toBe(200); |
| 53 | + }); |
| 54 | + |
| 55 | + it('and NOT by the built-in member_default wildcard (private_note is denied)', async () => { |
| 56 | + const r = await stack.apiAs(memberToken, 'GET', '/data/showcase_private_note'); |
| 57 | + // member_default has a wildcard grant → would be 200. The app default grants |
| 58 | + // no private_note access → denied, proving the declared default is in force. |
| 59 | + expect(r.status, 'declared default does NOT grant private_note').not.toBe(200); |
| 60 | + }); |
| 61 | +}); |
0 commit comments