|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `security:effective-access` — a person's effective permission sets on their |
| 5 | + * `sys_user` record page, each with every origin the server's answer carries. |
| 6 | + * |
| 7 | + * Driven through the real input pipeline: the node a page authors |
| 8 | + * (`{ type: 'security:effective-access', properties: {} }`) is rendered by |
| 9 | + * `SchemaRenderer` inside the record context the page binds, and the widget |
| 10 | + * is fed `security.describeEffectiveAccess(userId)`'s answer through the |
| 11 | + * adapter's client. |
| 12 | + * |
| 13 | + * - §1 a person with access only through the default binding and a position |
| 14 | + * shows those sets, each with its origin, and the set label; |
| 15 | + * - §2 CONTROL: a person with one direct grant shows exactly that set; |
| 16 | + * - §3 a set reached two ways is one row with both origins; derived origins |
| 17 | + * say which standing; |
| 18 | + * - §4 a 403 and a 501 show no list, each a quiet line; another failure |
| 19 | + * names itself; an empty answer says so; |
| 20 | + * - §5 off a `sys_user` record it renders nothing and reads nothing; a grant |
| 21 | + * written on the page reads the answer again. |
| 22 | + * |
| 23 | + * `t` echoes the key with any interpolation values appended, so assertions |
| 24 | + * read which string was asked for and with what. |
| 25 | + */ |
| 26 | + |
| 27 | +import '@testing-library/jest-dom/vitest'; |
| 28 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 29 | +import { cleanup, render, screen, waitFor, within } from '@testing-library/react'; |
| 30 | + |
| 31 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 32 | + ...(await importOriginal<Record<string, unknown>>()), |
| 33 | + useObjectTranslation: () => ({ |
| 34 | + t: (key: string, opts?: Record<string, unknown>) => { |
| 35 | + const vals = opts ? Object.values(opts).filter((v) => typeof v === 'string' || typeof v === 'number') : []; |
| 36 | + return vals.length ? `«${key}|${vals.join('|')}»` : `«${key}»`; |
| 37 | + }, |
| 38 | + language: 'en', |
| 39 | + }), |
| 40 | +})); |
| 41 | + |
| 42 | +let adapter: unknown = null; |
| 43 | +vi.mock('@object-ui/react', async (importOriginal) => ({ |
| 44 | + ...(await importOriginal<Record<string, unknown>>()), |
| 45 | + useAdapter: () => adapter, |
| 46 | +})); |
| 47 | + |
| 48 | +import '@object-ui/components'; |
| 49 | +import { ComponentRegistry } from '@object-ui/core'; |
| 50 | +import { |
| 51 | + MetadataCtx, |
| 52 | + notifyDataChanged, |
| 53 | + RecordContextProvider, |
| 54 | + SchemaRenderer, |
| 55 | + type MetadataContextValue, |
| 56 | +} from '@object-ui/react'; |
| 57 | +import './UserEffectiveAccess'; |
| 58 | +import type { SecurityEffectiveAccessSchema } from '@object-ui/types'; |
| 59 | +import { describeOrigin, type EffectiveAccessAnswer } from './effectiveAccess'; |
| 60 | + |
| 61 | +/** The node a page authors, typed on its `@object-ui/types` twin. */ |
| 62 | +const NODE: SecurityEffectiveAccessSchema = { type: 'security:effective-access', properties: {} }; |
| 63 | + |
| 64 | +/** The permission-set catalog the labels come from (`/meta/permission`). */ |
| 65 | +const CATALOG = [ |
| 66 | + { name: 'member_default', label: 'Member default' }, |
| 67 | + { name: 'sales_rep', label: 'Sales rep' }, |
| 68 | + { name: 'admin_full_access', label: 'Full administrator' }, |
| 69 | +]; |
| 70 | + |
| 71 | +const describeMock = vi.fn<(userId: string) => Promise<EffectiveAccessAnswer>>(); |
| 72 | +const ensureTypeMock = vi.fn(async (_type: string): Promise<unknown[]> => CATALOG); |
| 73 | + |
| 74 | +function refusal(status: number, message: string): Error { |
| 75 | + return Object.assign(new Error(message), { httpStatus: status }); |
| 76 | +} |
| 77 | + |
| 78 | +const metadata = { |
| 79 | + ensureType: (type: string) => ensureTypeMock(type), |
| 80 | +} as unknown as MetadataContextValue; |
| 81 | + |
| 82 | +function renderOn(objectName: string, recordId: string | null) { |
| 83 | + return render( |
| 84 | + <MetadataCtx.Provider value={metadata}> |
| 85 | + <RecordContextProvider objectName={objectName} recordId={recordId}> |
| 86 | + <SchemaRenderer schema={NODE} /> |
| 87 | + </RecordContextProvider> |
| 88 | + </MetadataCtx.Provider>, |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +/** The rendered rows, as set name → origin badge texts. */ |
| 93 | +async function rowsShown(): Promise<Record<string, string[]>> { |
| 94 | + const list = await screen.findByTestId('effective-access-list'); |
| 95 | + const out: Record<string, string[]> = {}; |
| 96 | + for (const row of within(list).getAllByRole('listitem').filter((li) => li.dataset.testid?.startsWith('effective-access-row-'))) { |
| 97 | + const name = row.dataset.testid!.slice('effective-access-row-'.length); |
| 98 | + const origins = within(row).getByRole('list'); |
| 99 | + out[name] = within(origins).getAllByRole('listitem').map((li) => li.textContent ?? ''); |
| 100 | + } |
| 101 | + return out; |
| 102 | +} |
| 103 | + |
| 104 | +beforeEach(() => { |
| 105 | + describeMock.mockReset(); |
| 106 | + ensureTypeMock.mockClear(); |
| 107 | + adapter = { getClient: () => ({ security: { describeEffectiveAccess: describeMock } }) }; |
| 108 | +}); |
| 109 | + |
| 110 | +afterEach(() => { |
| 111 | + cleanup(); |
| 112 | +}); |
| 113 | + |
| 114 | +describe('§0 registration', () => { |
| 115 | + it('is registered as security:effective-access, with no bare fallback', () => { |
| 116 | + expect(ComponentRegistry.get('security:effective-access')).toBeDefined(); |
| 117 | + expect(ComponentRegistry.get('effective-access')).toBeUndefined(); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('§1 access only through the default binding and a position', () => { |
| 122 | + it('shows those sets with their origins, not an empty list', async () => { |
| 123 | + describeMock.mockResolvedValueOnce({ |
| 124 | + userId: 'user_mia', |
| 125 | + organizationId: 'org_here', |
| 126 | + positions: ['org_member', 'sales', 'everyone'], |
| 127 | + permissionSets: [ |
| 128 | + { name: 'sales_rep', origins: [{ kind: 'position', position: 'sales' }] }, |
| 129 | + { name: 'member_default', origins: [{ kind: 'default' }] }, |
| 130 | + ], |
| 131 | + }); |
| 132 | + renderOn('sys_user', 'user_mia'); |
| 133 | + |
| 134 | + expect(await rowsShown()).toEqual({ |
| 135 | + sales_rep: ['«effectiveAccess.origin.position|sales»'], |
| 136 | + member_default: ['«effectiveAccess.origin.default»'], |
| 137 | + }); |
| 138 | + expect(describeMock).toHaveBeenCalledWith('user_mia'); |
| 139 | + expect(screen.queryByTestId('effective-access-empty')).toBeNull(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('shows each set by its catalog label, with the name beside it', async () => { |
| 143 | + describeMock.mockResolvedValueOnce({ |
| 144 | + userId: 'user_mia', |
| 145 | + organizationId: 'org_here', |
| 146 | + positions: ['everyone'], |
| 147 | + permissionSets: [{ name: 'member_default', origins: [{ kind: 'default' }] }], |
| 148 | + }); |
| 149 | + renderOn('sys_user', 'user_mia'); |
| 150 | + const row = await screen.findByTestId('effective-access-row-member_default'); |
| 151 | + await waitFor(() => expect(within(row).getByText('Member default')).toBeInTheDocument()); |
| 152 | + expect(within(row).getByText('member_default')).toBeInTheDocument(); |
| 153 | + expect(ensureTypeMock).toHaveBeenCalledWith('permission'); |
| 154 | + }); |
| 155 | + |
| 156 | + it('shows a set the catalog does not list by its name, never drops it', async () => { |
| 157 | + ensureTypeMock.mockResolvedValueOnce([]); |
| 158 | + describeMock.mockResolvedValueOnce({ |
| 159 | + userId: 'user_mia', |
| 160 | + organizationId: 'org_here', |
| 161 | + positions: [], |
| 162 | + permissionSets: [{ name: 'uncatalogued_set', origins: [{ kind: 'default' }] }], |
| 163 | + }); |
| 164 | + renderOn('sys_user', 'user_mia'); |
| 165 | + const row = await screen.findByTestId('effective-access-row-uncatalogued_set'); |
| 166 | + expect(within(row).getAllByText('uncatalogued_set')).toHaveLength(1); |
| 167 | + }); |
| 168 | +}); |
| 169 | + |
| 170 | +describe('§2 CONTROL — one direct grant', () => { |
| 171 | + it('shows exactly that set, as a direct grant', async () => { |
| 172 | + describeMock.mockResolvedValueOnce({ |
| 173 | + userId: 'user_ana', |
| 174 | + organizationId: 'org_here', |
| 175 | + positions: [], |
| 176 | + permissionSets: [{ name: 'sales_rep', origins: [{ kind: 'direct' }] }], |
| 177 | + }); |
| 178 | + renderOn('sys_user', 'user_ana'); |
| 179 | + expect(await rowsShown()).toEqual({ sales_rep: ['«effectiveAccess.origin.direct»'] }); |
| 180 | + }); |
| 181 | +}); |
| 182 | + |
| 183 | +describe('§3 a set reached two ways', () => { |
| 184 | + it('is one row carrying both origins, in the answer\'s order', async () => { |
| 185 | + describeMock.mockResolvedValueOnce({ |
| 186 | + userId: 'user_mia', |
| 187 | + organizationId: 'org_here', |
| 188 | + positions: ['sales'], |
| 189 | + permissionSets: [ |
| 190 | + { name: 'sales_rep', origins: [{ kind: 'direct' }, { kind: 'position', position: 'sales' }] }, |
| 191 | + { |
| 192 | + name: 'admin_full_access', |
| 193 | + origins: [{ kind: 'direct' }, { kind: 'derived', source: 'platform_admin_config' }], |
| 194 | + }, |
| 195 | + { name: 'ai_seat', origins: [{ kind: 'derived', source: 'ai_access' }] }, |
| 196 | + ], |
| 197 | + }); |
| 198 | + renderOn('sys_user', 'user_mia'); |
| 199 | + expect(await rowsShown()).toEqual({ |
| 200 | + sales_rep: ['«effectiveAccess.origin.direct»', '«effectiveAccess.origin.position|sales»'], |
| 201 | + admin_full_access: ['«effectiveAccess.origin.direct»', '«effectiveAccess.origin.derivedPlatformAdmin»'], |
| 202 | + ai_seat: ['«effectiveAccess.origin.derivedAiAccess»'], |
| 203 | + }); |
| 204 | + expect(screen.getAllByTestId(/^effective-access-row-/)).toHaveLength(3); |
| 205 | + }); |
| 206 | + |
| 207 | + it('describes every origin kind the contract declares', () => { |
| 208 | + const t = (key: string, opts?: Record<string, unknown>) => (opts ? `${key}|${Object.values(opts).join('|')}` : key); |
| 209 | + expect(describeOrigin({ kind: 'direct' }, t)).toBe('effectiveAccess.origin.direct'); |
| 210 | + expect(describeOrigin({ kind: 'position', position: 'org_external' }, t)).toBe('effectiveAccess.origin.position|org_external'); |
| 211 | + expect(describeOrigin({ kind: 'default' }, t)).toBe('effectiveAccess.origin.default'); |
| 212 | + expect(describeOrigin({ kind: 'derived', source: 'platform_admin_config' }, t)).toBe('effectiveAccess.origin.derivedPlatformAdmin'); |
| 213 | + expect(describeOrigin({ kind: 'derived', source: 'ai_access' }, t)).toBe('effectiveAccess.origin.derivedAiAccess'); |
| 214 | + }); |
| 215 | +}); |
| 216 | + |
| 217 | +describe('§4 refusals never draw a list', () => { |
| 218 | + it('a 403 (a person the caller may not manage) leaves one quiet line and no list', async () => { |
| 219 | + describeMock.mockRejectedValueOnce(refusal(403, 'Forbidden')); |
| 220 | + renderOn('sys_user', 'user_other'); |
| 221 | + expect(await screen.findByTestId('effective-access-refused')).toHaveTextContent('«effectiveAccess.refused»'); |
| 222 | + expect(screen.queryByTestId('effective-access-list')).toBeNull(); |
| 223 | + expect(screen.queryByTestId('effective-access-empty')).toBeNull(); |
| 224 | + }); |
| 225 | + |
| 226 | + it('a 501 (a server without the read) leaves one quiet line and no list', async () => { |
| 227 | + describeMock.mockRejectedValueOnce(refusal(501, 'Not implemented')); |
| 228 | + renderOn('sys_user', 'user_mia'); |
| 229 | + expect(await screen.findByTestId('effective-access-unserved')).toHaveTextContent('«effectiveAccess.unserved»'); |
| 230 | + expect(screen.queryByTestId('effective-access-list')).toBeNull(); |
| 231 | + expect(screen.queryByTestId('effective-access-empty')).toBeNull(); |
| 232 | + }); |
| 233 | + |
| 234 | + it('any other failure names itself, and draws no list', async () => { |
| 235 | + describeMock.mockRejectedValueOnce(refusal(500, 'authorization store could not be read')); |
| 236 | + renderOn('sys_user', 'user_mia'); |
| 237 | + expect(await screen.findByTestId('effective-access-failed')).toHaveTextContent( |
| 238 | + '«effectiveAccess.loadFailed|authorization store could not be read»', |
| 239 | + ); |
| 240 | + expect(screen.queryByTestId('effective-access-list')).toBeNull(); |
| 241 | + }); |
| 242 | + |
| 243 | + it('an answer with no set says so', async () => { |
| 244 | + describeMock.mockResolvedValueOnce({ userId: 'user_mia', organizationId: null, positions: [], permissionSets: [] }); |
| 245 | + renderOn('sys_user', 'user_mia'); |
| 246 | + expect(await screen.findByTestId('effective-access-empty')).toHaveTextContent('«effectiveAccess.empty»'); |
| 247 | + }); |
| 248 | +}); |
| 249 | + |
| 250 | +describe('§5 where it draws, and when it reads again', () => { |
| 251 | + it('renders nothing and reads nothing off a sys_user record', () => { |
| 252 | + const other = renderOn('crm_account', 'acc_1'); |
| 253 | + expect(other.container).toBeEmptyDOMElement(); |
| 254 | + other.unmount(); |
| 255 | + const noId = renderOn('sys_user', null); |
| 256 | + expect(noId.container).toBeEmptyDOMElement(); |
| 257 | + expect(describeMock).not.toHaveBeenCalled(); |
| 258 | + }); |
| 259 | + |
| 260 | + it('reads the answer again when a grant on the page changes', async () => { |
| 261 | + describeMock.mockResolvedValue({ |
| 262 | + userId: 'user_mia', |
| 263 | + organizationId: 'org_here', |
| 264 | + positions: [], |
| 265 | + permissionSets: [{ name: 'sales_rep', origins: [{ kind: 'direct' }] }], |
| 266 | + }); |
| 267 | + renderOn('sys_user', 'user_mia'); |
| 268 | + await screen.findByTestId('effective-access-list'); |
| 269 | + expect(describeMock).toHaveBeenCalledTimes(1); |
| 270 | + notifyDataChanged({ objectName: 'sys_user_permission_set' }); |
| 271 | + await waitFor(() => expect(describeMock).toHaveBeenCalledTimes(2)); |
| 272 | + }); |
| 273 | +}); |
0 commit comments