|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { foldWildcardSuperUser } from './hono-plugin.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * ADR-0057 D10 / ADR-0092 D5 — the `/me/permissions` per-object FLS map must |
| 8 | + * mirror the server's actual enforcement, which grants writes via a `'*'` |
| 9 | + * modifyAll super-user bypass regardless of another set's explicit per-object |
| 10 | + * deny (most-permissive merge, no deny-wins). |
| 11 | + */ |
| 12 | +describe('foldWildcardSuperUser', () => { |
| 13 | + it('lifts an explicit per-object deny when the wildcard is a modifyAll super-user grant', () => { |
| 14 | + const objects: Record<string, any> = { |
| 15 | + '*': { allowRead: true, allowEdit: true, viewAllRecords: true, modifyAllRecords: true }, |
| 16 | + // As produced when admin_full_access ('*') composes with organization_admin |
| 17 | + // (explicit sys_user deny) — the naive merge leaves allowEdit:false. |
| 18 | + sys_user: { allowRead: true, allowEdit: false, allowCreate: false, allowDelete: false }, |
| 19 | + }; |
| 20 | + foldWildcardSuperUser(objects); |
| 21 | + expect(objects.sys_user).toMatchObject({ allowRead: true, allowEdit: true, allowCreate: true, allowDelete: true }); |
| 22 | + // The wildcard entry itself is untouched. |
| 23 | + expect(objects['*'].modifyAllRecords).toBe(true); |
| 24 | + }); |
| 25 | + |
| 26 | + it('viewAll-only wildcard lifts read but NOT write', () => { |
| 27 | + const objects: Record<string, any> = { |
| 28 | + '*': { allowRead: true, viewAllRecords: true }, |
| 29 | + sys_session: { allowRead: false, allowEdit: false }, |
| 30 | + }; |
| 31 | + foldWildcardSuperUser(objects); |
| 32 | + expect(objects.sys_session.allowRead).toBe(true); |
| 33 | + expect(objects.sys_session.allowEdit).toBe(false); |
| 34 | + }); |
| 35 | + |
| 36 | + it('no-ops when the wildcard is not a super-user grant', () => { |
| 37 | + const objects: Record<string, any> = { |
| 38 | + '*': { allowRead: true, allowEdit: true }, // plain allow, no view/modifyAll |
| 39 | + sys_user: { allowRead: true, allowEdit: false }, |
| 40 | + }; |
| 41 | + foldWildcardSuperUser(objects); |
| 42 | + expect(objects.sys_user.allowEdit).toBe(false); // untouched — no super-user bypass |
| 43 | + }); |
| 44 | + |
| 45 | + it('no-ops when there is no wildcard entry', () => { |
| 46 | + const objects: Record<string, any> = { sys_user: { allowEdit: false } }; |
| 47 | + foldWildcardSuperUser(objects); |
| 48 | + expect(objects.sys_user.allowEdit).toBe(false); |
| 49 | + }); |
| 50 | +}); |
0 commit comments