|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * ADR-0066 D4 — UI half of the action dual-surface gate. The server is the |
| 11 | + * source of truth (403); the ActionEngine derives the same gate from |
| 12 | + * `action.requiredPermissions` so a button the user can't use is hidden. |
| 13 | + * Fail-OPEN when the user's systemPermissions are unknown. |
| 14 | + */ |
| 15 | + |
| 16 | +import { describe, it, expect } from 'vitest'; |
| 17 | +import { ActionEngine } from '../ActionEngine'; |
| 18 | + |
| 19 | +const engineWith = (systemPermissions?: string[]) => |
| 20 | + new ActionEngine({ |
| 21 | + user: systemPermissions === undefined ? { id: 'u1' } : { id: 'u1', systemPermissions }, |
| 22 | + } as any); |
| 23 | + |
| 24 | +describe('ActionEngine.getActionsForLocation — ADR-0066 D4 requiredPermissions filter', () => { |
| 25 | + it('shows actions with no requiredPermissions', () => { |
| 26 | + const e = engineWith([]); |
| 27 | + e.registerAction({ name: 'open', type: 'api' } as any, { locations: ['record_section'] }); |
| 28 | + expect(e.getActionsForLocation('record_section')).toHaveLength(1); |
| 29 | + }); |
| 30 | + |
| 31 | + it('shows an action whose requiredPermissions are all held', () => { |
| 32 | + const e = engineWith(['manage_platform_settings']); |
| 33 | + e.registerAction({ name: 'issue', type: 'api', requiredPermissions: ['manage_platform_settings'] } as any, { locations: ['record_section'] }); |
| 34 | + expect(e.getActionsForLocation('record_section')).toHaveLength(1); |
| 35 | + }); |
| 36 | + |
| 37 | + it('hides an action whose requiredPermissions are NOT all held', () => { |
| 38 | + const e = engineWith(['setup.access']); |
| 39 | + e.registerAction({ name: 'issue', type: 'api', requiredPermissions: ['manage_platform_settings'] } as any, { locations: ['record_section'] }); |
| 40 | + expect(e.getActionsForLocation('record_section')).toHaveLength(0); |
| 41 | + }); |
| 42 | + |
| 43 | + it('requires ALL listed capabilities (AND, not OR)', () => { |
| 44 | + const e = engineWith(['a']); |
| 45 | + e.registerAction({ name: 'x', type: 'api', requiredPermissions: ['a', 'b'] } as any, { locations: ['record_section'] }); |
| 46 | + expect(e.getActionsForLocation('record_section')).toHaveLength(0); |
| 47 | + }); |
| 48 | + |
| 49 | + it('fails OPEN when systemPermissions is unknown (server still enforces)', () => { |
| 50 | + const e = engineWith(undefined); |
| 51 | + e.registerAction({ name: 'issue', type: 'api', requiredPermissions: ['manage_platform_settings'] } as any, { locations: ['record_section'] }); |
| 52 | + expect(e.getActionsForLocation('record_section')).toHaveLength(1); |
| 53 | + }); |
| 54 | +}); |
0 commit comments