|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +// The external population's stamp at the resolver's one provisioning boundary |
| 4 | +// (ruling of record 6355206139, letter B, stage 1, E1): `sys_member.role = |
| 5 | +// external` in the ACTIVE organization projects to `org_external`, and its |
| 6 | +// holder does not hold the `everyone` audience anchor — so the deployment's |
| 7 | +// default sets bound there (the platform baseline, an app's `isDefault` set, |
| 8 | +// an accepted package default) never reach it. What reaches it is what its own |
| 9 | +// identity position names, plus its explicit grants. An ordinary member is |
| 10 | +// unchanged, and the stamp is per organization. |
| 11 | + |
| 12 | +import { describe, it, expect } from 'vitest'; |
| 13 | +import { countPositionHolders, resolveUserAuthzGrants } from './resolve-authz-context.js'; |
| 14 | +import { bindStaticSecurityCatalog } from './__tests__/security-catalog.testkit.js'; |
| 15 | + |
| 16 | +const ALPHA = 'org_alpha'; |
| 17 | +const BETA = 'org_beta'; |
| 18 | + |
| 19 | +function makeQl(tables: Record<string, any[]>) { |
| 20 | + return { |
| 21 | + async find(object: string, opts: any) { |
| 22 | + const rows = tables[object] ?? []; |
| 23 | + const where = opts?.where ?? {}; |
| 24 | + const matched = rows.filter((r) => |
| 25 | + Object.entries(where).every(([k, v]) => { |
| 26 | + if (k.startsWith('$')) throw new Error(`test double: combinator ${k} is not implemented`); |
| 27 | + if (v && typeof v === 'object' && '$in' in (v as any)) return (v as any).$in.includes(r[k]); |
| 28 | + if (v === null) return (r[k] ?? null) === null; |
| 29 | + return r[k] === v; |
| 30 | + }), |
| 31 | + ); |
| 32 | + return typeof opts?.limit === 'number' ? matched.slice(0, opts.limit) : matched; |
| 33 | + }, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +/** The catalog every case reads: the anchor's baseline, the external identity's own set, one portal grant. */ |
| 38 | +const definitions = { |
| 39 | + positions: [ |
| 40 | + { name: 'everyone', permissionSets: ['member_default', 'app_default'] }, |
| 41 | + { name: 'org_external', permissionSets: ['external_default'] }, |
| 42 | + { name: 'supplier_a_portal', permissionSets: ['supplier_portal'] }, |
| 43 | + ], |
| 44 | + permissions: [ |
| 45 | + { name: 'member_default' }, |
| 46 | + { name: 'app_default' }, |
| 47 | + { name: 'external_default' }, |
| 48 | + { name: 'supplier_portal' }, |
| 49 | + ], |
| 50 | +}; |
| 51 | + |
| 52 | +const principal = (members: any[], positions: any[] = []) => |
| 53 | + bindStaticSecurityCatalog( |
| 54 | + makeQl({ |
| 55 | + sys_user: [{ id: 'u1', email: 'u1@x.test' }, { id: 'u2', email: 'u2@x.test' }, { id: 'u3', email: 'u3@x.test' }], |
| 56 | + sys_member: members, |
| 57 | + sys_user_position: positions, |
| 58 | + sys_user_permission_set: [], |
| 59 | + }), |
| 60 | + definitions, |
| 61 | + ); |
| 62 | + |
| 63 | +describe('the external population at the provisioning boundary', () => { |
| 64 | + it('an external member holds org_external and not everyone: only its own set and its explicit grants', async () => { |
| 65 | + const ql = principal( |
| 66 | + [{ user_id: 'u1', organization_id: ALPHA, role: 'external' }], |
| 67 | + [{ user_id: 'u1', position: 'supplier_a_portal', organization_id: ALPHA }], |
| 68 | + ); |
| 69 | + const grants = await resolveUserAuthzGrants(ql, 'u1', { tenantId: ALPHA }); |
| 70 | + expect(grants.positions).toContain('org_external'); |
| 71 | + expect(grants.positions).toContain('supplier_a_portal'); |
| 72 | + expect(grants.positions).not.toContain('everyone'); |
| 73 | + expect(grants.positions).not.toContain('org_member'); |
| 74 | + expect([...grants.permissions].sort()).toEqual(['external_default', 'supplier_portal']); |
| 75 | + }); |
| 76 | + |
| 77 | + it('an ordinary member is unchanged: everyone, and the baseline it names', async () => { |
| 78 | + const ql = principal([{ user_id: 'u2', organization_id: ALPHA, role: 'member' }]); |
| 79 | + const grants = await resolveUserAuthzGrants(ql, 'u2', { tenantId: ALPHA }); |
| 80 | + expect(grants.positions).toEqual(expect.arrayContaining(['org_member', 'everyone'])); |
| 81 | + expect(grants.positions).not.toContain('org_external'); |
| 82 | + expect([...grants.permissions].sort()).toEqual(['app_default', 'member_default']); |
| 83 | + }); |
| 84 | + |
| 85 | + it('the stamp is per organization: external in one, a member in the other', async () => { |
| 86 | + const ql = principal([ |
| 87 | + { user_id: 'u3', organization_id: ALPHA, role: 'external' }, |
| 88 | + { user_id: 'u3', organization_id: BETA, role: 'member' }, |
| 89 | + ]); |
| 90 | + const inAlpha = await resolveUserAuthzGrants(ql, 'u3', { tenantId: ALPHA }); |
| 91 | + expect(inAlpha.positions).not.toContain('everyone'); |
| 92 | + expect([...inAlpha.permissions].sort()).toEqual(['external_default']); |
| 93 | + const inBeta = await resolveUserAuthzGrants(ql, 'u3', { tenantId: BETA }); |
| 94 | + expect(inBeta.positions).toContain('everyone'); |
| 95 | + expect(inBeta.positions).not.toContain('org_external'); |
| 96 | + expect([...inBeta.permissions].sort()).toEqual(['app_default', 'member_default']); |
| 97 | + }); |
| 98 | + |
| 99 | + it('with no active organization an external membership withholds the anchor (fail closed)', async () => { |
| 100 | + const ql = principal([ |
| 101 | + { user_id: 'u3', organization_id: ALPHA, role: 'external' }, |
| 102 | + { user_id: 'u3', organization_id: BETA, role: 'member' }, |
| 103 | + ]); |
| 104 | + const grants = await resolveUserAuthzGrants(ql, 'u3', {}); |
| 105 | + expect(grants.positions).toContain('org_external'); |
| 106 | + expect(grants.positions).not.toContain('everyone'); |
| 107 | + expect(grants.permissions).not.toContain('member_default'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('the holder count agrees: an external is counted under org_external, never under everyone', async () => { |
| 111 | + const ql = principal([ |
| 112 | + { user_id: 'u1', organization_id: ALPHA, role: 'external' }, |
| 113 | + { user_id: 'u2', organization_id: ALPHA, role: 'member' }, |
| 114 | + ]); |
| 115 | + const counts = await countPositionHolders(ql, ALPHA, ['everyone', 'org_external', 'org_member']); |
| 116 | + expect(counts.get('everyone')).toBe(1); |
| 117 | + expect(counts.get('org_external')).toBe(1); |
| 118 | + expect(counts.get('org_member')).toBe(1); |
| 119 | + }); |
| 120 | +}); |
0 commit comments