|
| 1 | +import type { ClientField } from 'payload' |
| 2 | + |
| 3 | +import { describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +import type { FieldOption } from './reduceFieldOptions.js' |
| 6 | + |
| 7 | +// Mock the JSX label builder: irrelevant to permission logic and avoids needing a React runtime here. |
| 8 | +vi.mock('../../utilities/combineFieldLabel.js', () => ({ |
| 9 | + combineFieldLabel: () => null, |
| 10 | +})) |
| 11 | + |
| 12 | +import { reduceFieldOptions } from './reduceFieldOptions.js' |
| 13 | + |
| 14 | +const text = (name: string): ClientField => ({ name, type: 'text' }) as ClientField |
| 15 | + |
| 16 | +const namesOf = (options: FieldOption[]): (string | undefined)[] => |
| 17 | + options.map((option) => ('name' in option.value.field ? option.value.field.name : undefined)) |
| 18 | + |
| 19 | +// A denied field omits the operation key at runtime, which `SanitizedFieldPermissions` can't express — cast at the boundary. |
| 20 | +type Permissions = Parameters<typeof reduceFieldOptions>[0]['permissions'] |
| 21 | +const optionsFor = (fields: ClientField[], permissions: unknown): FieldOption[] => |
| 22 | + reduceFieldOptions({ fields, permissions: permissions as Permissions }) |
| 23 | + |
| 24 | +// Granted sets the operation to `true`; denied omits the key, mirroring `populateFieldPermissions`. |
| 25 | +const granted = { create: true, read: true, update: true } |
| 26 | +const readOnly = { create: true, read: true } |
| 27 | + |
| 28 | +describe('reduceFieldOptions', () => { |
| 29 | + it('excludes a restricted field inside a named tab while keeping its clean siblings', () => { |
| 30 | + const fields: ClientField[] = [ |
| 31 | + { |
| 32 | + type: 'tabs', |
| 33 | + tabs: [{ name: 'namedTab', fields: [text('okInTab'), text('restrictedInTab')] }], |
| 34 | + } as ClientField, |
| 35 | + ] |
| 36 | + |
| 37 | + const permissions = { |
| 38 | + namedTab: { |
| 39 | + ...granted, |
| 40 | + fields: { |
| 41 | + okInTab: granted, |
| 42 | + restrictedInTab: readOnly, |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + const options = optionsFor(fields, permissions) |
| 48 | + const names = namesOf(options) |
| 49 | + |
| 50 | + expect(names).toContain('okInTab') |
| 51 | + expect(names).not.toContain('restrictedInTab') |
| 52 | + }) |
| 53 | + |
| 54 | + it('excludes a restricted field inside a nested named tab', () => { |
| 55 | + const fields: ClientField[] = [ |
| 56 | + { |
| 57 | + type: 'tabs', |
| 58 | + tabs: [ |
| 59 | + { |
| 60 | + name: 'outerTab', |
| 61 | + fields: [ |
| 62 | + { |
| 63 | + type: 'tabs', |
| 64 | + tabs: [{ name: 'innerTab', fields: [text('deepOk'), text('deepRestricted')] }], |
| 65 | + }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + ], |
| 69 | + } as ClientField, |
| 70 | + ] |
| 71 | + |
| 72 | + const permissions = { |
| 73 | + outerTab: { |
| 74 | + ...granted, |
| 75 | + fields: { |
| 76 | + innerTab: { |
| 77 | + ...granted, |
| 78 | + fields: { |
| 79 | + deepOk: granted, |
| 80 | + deepRestricted: readOnly, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + const options = optionsFor(fields, permissions) |
| 88 | + const names = namesOf(options) |
| 89 | + |
| 90 | + expect(names).toContain('deepOk') |
| 91 | + expect(names).not.toContain('deepRestricted') |
| 92 | + }) |
| 93 | + |
| 94 | + it('only affects the named tab that contains the restricted field', () => { |
| 95 | + const fields: ClientField[] = [ |
| 96 | + { |
| 97 | + type: 'tabs', |
| 98 | + tabs: [ |
| 99 | + { name: 'cleanTab', fields: [text('cleanField')] }, |
| 100 | + { name: 'restrictedHostTab', fields: [text('restrictedField')] }, |
| 101 | + ], |
| 102 | + } as ClientField, |
| 103 | + ] |
| 104 | + |
| 105 | + const permissions = { |
| 106 | + cleanTab: { ...granted, fields: { cleanField: granted } }, |
| 107 | + restrictedHostTab: { ...granted, fields: { restrictedField: readOnly } }, |
| 108 | + } |
| 109 | + |
| 110 | + const options = optionsFor(fields, permissions) |
| 111 | + const names = namesOf(options) |
| 112 | + |
| 113 | + expect(names).toContain('cleanField') |
| 114 | + expect(names).not.toContain('restrictedField') |
| 115 | + }) |
| 116 | + |
| 117 | + it('includes every named-tab field when permissions are fully granted', () => { |
| 118 | + const fields: ClientField[] = [ |
| 119 | + { |
| 120 | + type: 'tabs', |
| 121 | + tabs: [{ name: 'namedTab', fields: [text('a'), text('b')] }], |
| 122 | + } as ClientField, |
| 123 | + ] |
| 124 | + |
| 125 | + const options = optionsFor(fields, true) |
| 126 | + const names = namesOf(options) |
| 127 | + |
| 128 | + expect(names).toEqual(expect.arrayContaining(['a', 'b'])) |
| 129 | + }) |
| 130 | + |
| 131 | + it('applies parent permissions to an unnamed tab and excludes its restricted fields', () => { |
| 132 | + const fields: ClientField[] = [ |
| 133 | + { |
| 134 | + type: 'tabs', |
| 135 | + tabs: [{ label: 'Unnamed', fields: [text('okAtRoot'), text('restrictedAtRoot')] }], |
| 136 | + } as ClientField, |
| 137 | + ] |
| 138 | + |
| 139 | + // Unnamed tabs share the parent's permission map, so subfields resolve at the parent level. |
| 140 | + const permissions = { |
| 141 | + okAtRoot: granted, |
| 142 | + restrictedAtRoot: readOnly, |
| 143 | + } |
| 144 | + |
| 145 | + const options = optionsFor(fields, permissions) |
| 146 | + const names = namesOf(options) |
| 147 | + |
| 148 | + expect(names).toContain('okAtRoot') |
| 149 | + expect(names).not.toContain('restrictedAtRoot') |
| 150 | + }) |
| 151 | +}) |
0 commit comments