|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#25866 — a field permission entry may carry a row condition on |
| 5 | + * its EDIT vote (`when`, the CEL of `rowLevelSecurity[].using`). The matrix |
| 6 | + * offers the key beside the Read / Edit boxes and shows an authored one, and |
| 7 | + * it never authors the shape the spec refuses (`when` beside |
| 8 | + * `readable: false`): Read off clears the condition, the bulk shortcuts keep |
| 9 | + * it, blank text removes the key, and a condition the publish gate would |
| 10 | + * refuse holds the save the way an RLS clause does. |
| 11 | + */ |
| 12 | + |
| 13 | +import '@testing-library/jest-dom/vitest'; |
| 14 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 15 | +import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 16 | +import { MemoryRouter } from 'react-router-dom'; |
| 17 | + |
| 18 | +interface FieldEntry { |
| 19 | + readable?: boolean; |
| 20 | + editable?: boolean; |
| 21 | + when?: string; |
| 22 | +} |
| 23 | +interface SavedSet { |
| 24 | + fields?: Record<string, FieldEntry>; |
| 25 | +} |
| 26 | + |
| 27 | +/** The slice of the metadata client the editor calls here. */ |
| 28 | +type FakeClient = Record<string, (...args: never[]) => Promise<unknown>>; |
| 29 | + |
| 30 | +let clientImpl: FakeClient; |
| 31 | +let saved: SavedSet[] = []; |
| 32 | + |
| 33 | +vi.mock('./useMetadata', () => ({ |
| 34 | + useMetadataClient: () => clientImpl, |
| 35 | + useMetadataTypes: () => ({ |
| 36 | + loading: false, |
| 37 | + error: null, |
| 38 | + entries: [{ type: 'permission', label: 'Permission', allowEnvironmentOverlay: true }], |
| 39 | + }), |
| 40 | +})); |
| 41 | + |
| 42 | +vi.mock('./AssignedUsersSection', () => ({ AssignedUsersSection: () => null })); |
| 43 | + |
| 44 | +import { PermissionMatrixEditPage } from './PermissionMatrixEditor'; |
| 45 | + |
| 46 | +afterEach(() => { |
| 47 | + cleanup(); |
| 48 | + saved = []; |
| 49 | +}); |
| 50 | + |
| 51 | +// Eight fields: the same wide object the bulk cases use. |
| 52 | +const FIELDS = ['amount', 'email', 'name', 'owner', 'phone', 'region', 'revenue', 'status']; |
| 53 | + |
| 54 | +function makeClient(storedFields: Record<string, FieldEntry>): FakeClient { |
| 55 | + return { |
| 56 | + layered: async () => ({ |
| 57 | + effective: { |
| 58 | + name: 'sales_perms', |
| 59 | + label: 'Sales', |
| 60 | + objects: { a_account: { allowRead: true } }, |
| 61 | + fields: storedFields, |
| 62 | + }, |
| 63 | + code: null, |
| 64 | + overlay: null, |
| 65 | + overlayScope: null, |
| 66 | + }), |
| 67 | + getDraft: async () => null, |
| 68 | + list: async (type: string) => (type === 'object' ? [{ item: { name: 'a_account' } }] : []), |
| 69 | + get: async (type: string) => (type === 'object' ? { fields: FIELDS.map((name) => ({ name })) } : null), |
| 70 | + save: async (_type: string, _name: string, payload: Record<string, unknown>) => { |
| 71 | + saved.push(JSON.parse(JSON.stringify(payload)) as SavedSet); |
| 72 | + return payload; |
| 73 | + }, |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +/** The package door autosaves 1.5s after the last edit (objectui#11787). */ |
| 78 | +const AUTOSAVE = { timeout: 4000 }; |
| 79 | +const WHEN_LABEL = 'WHEN (edit vote — row condition)'; |
| 80 | + |
| 81 | +async function renderExpanded(storedFields: Record<string, FieldEntry> = {}) { |
| 82 | + clientImpl = makeClient(storedFields); |
| 83 | + render( |
| 84 | + <MemoryRouter> |
| 85 | + <PermissionMatrixEditPage type="permission" name="sales_perms" packageId="app.a" /> |
| 86 | + </MemoryRouter>, |
| 87 | + ); |
| 88 | + fireEvent.click(await screen.findByRole('button', { name: /a_account/ })); |
| 89 | + await screen.findByLabelText('a_account.amount readable'); |
| 90 | +} |
| 91 | + |
| 92 | +const whenButton = (field: string) => screen.getByRole('button', { name: `a_account.${field} when` }); |
| 93 | + |
| 94 | +async function authorWhen(field: string, expression: string) { |
| 95 | + fireEvent.click(whenButton(field)); |
| 96 | + const editor = await screen.findByLabelText(WHEN_LABEL); |
| 97 | + fireEvent.change(editor, { target: { value: expression } }); |
| 98 | + return editor; |
| 99 | +} |
| 100 | + |
| 101 | +/** The next autosave to land after `n` already have. */ |
| 102 | +async function saveAfter(n: number): Promise<SavedSet> { |
| 103 | + await waitFor(() => expect(saved.length).toBeGreaterThan(n), AUTOSAVE); |
| 104 | + return saved[saved.length - 1]; |
| 105 | +} |
| 106 | + |
| 107 | +describe('a field row offers a row condition on its edit vote (objectstack#25866)', () => { |
| 108 | + it('offers the key, shows the authored condition, and the saved entry carries it', async () => { |
| 109 | + await renderExpanded(); |
| 110 | + expect(whenButton('amount')).toHaveTextContent('Add condition'); |
| 111 | + |
| 112 | + await authorWhen('amount', "status == 'draft'"); |
| 113 | + // The chip beside the boxes shows the condition as authored. |
| 114 | + expect(whenButton('amount')).toHaveTextContent("status == 'draft'"); |
| 115 | + |
| 116 | + const set = await saveAfter(0); |
| 117 | + expect(set.fields?.['a_account.amount']).toEqual({ readable: true, editable: false, when: "status == 'draft'" }); |
| 118 | + }); |
| 119 | + |
| 120 | + it('shows a stored condition on load; the other rows still offer the key', async () => { |
| 121 | + await renderExpanded({ 'a_account.amount': { readable: true, editable: false, when: "status == 'approved'" } }); |
| 122 | + expect(whenButton('amount')).toHaveTextContent("status == 'approved'"); |
| 123 | + expect(whenButton('email')).toHaveTextContent('Add condition'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('Read off clears the condition: the spec refuses `when` beside readable:false', async () => { |
| 127 | + await renderExpanded({ 'a_account.amount': { readable: true, editable: true, when: 'owner == current_user.id' } }); |
| 128 | + |
| 129 | + fireEvent.click(screen.getByLabelText('a_account.amount readable')); |
| 130 | + expect(screen.getByLabelText('a_account.amount readable')).not.toBeChecked(); |
| 131 | + expect(screen.getByLabelText('a_account.amount editable')).not.toBeChecked(); |
| 132 | + // No affordance on a row that cannot carry a condition. |
| 133 | + expect(screen.queryByRole('button', { name: 'a_account.amount when' })).toBeNull(); |
| 134 | + |
| 135 | + const set = await saveAfter(0); |
| 136 | + expect(set.fields?.['a_account.amount']).toEqual({ readable: false, editable: false }); |
| 137 | + }); |
| 138 | + |
| 139 | + it('the Edit toggle and the bulk Read all / Write all keep the condition; Clear drops it', async () => { |
| 140 | + await renderExpanded({ 'a_account.amount': { readable: true, editable: false, when: "status == 'draft'" } }); |
| 141 | + |
| 142 | + fireEvent.click(screen.getByLabelText('a_account.amount editable')); |
| 143 | + expect(whenButton('amount')).toHaveTextContent("status == 'draft'"); |
| 144 | + |
| 145 | + fireEvent.click(screen.getByRole('button', { name: 'Write all' })); |
| 146 | + expect(screen.getByLabelText('a_account.amount editable')).toBeChecked(); |
| 147 | + expect(whenButton('amount')).toHaveTextContent("status == 'draft'"); |
| 148 | + |
| 149 | + fireEvent.click(screen.getByRole('button', { name: 'Read all' })); |
| 150 | + expect(screen.getByLabelText('a_account.amount editable')).not.toBeChecked(); |
| 151 | + expect(whenButton('amount')).toHaveTextContent("status == 'draft'"); |
| 152 | + |
| 153 | + const first = await saveAfter(0); |
| 154 | + expect(first.fields?.['a_account.amount']).toEqual({ readable: true, editable: false, when: "status == 'draft'" }); |
| 155 | + expect(first.fields?.['a_account.email']).toEqual({ readable: true, editable: false }); |
| 156 | + |
| 157 | + fireEvent.click(screen.getByRole('button', { name: 'Clear' })); |
| 158 | + expect(whenButton('amount')).toHaveTextContent('Add condition'); |
| 159 | + const second = await saveAfter(1); |
| 160 | + expect(second.fields?.['a_account.amount']).toBeUndefined(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('blank text removes the key rather than saving a blank condition', async () => { |
| 164 | + await renderExpanded({ 'a_account.amount': { readable: true, editable: false, when: "status == 'draft'" } }); |
| 165 | + |
| 166 | + const editor = await authorWhen('amount', ' '); |
| 167 | + expect(editor).toHaveValue(''); |
| 168 | + expect(whenButton('amount')).toHaveTextContent('Add condition'); |
| 169 | + |
| 170 | + const set = await saveAfter(0); |
| 171 | + expect(set.fields?.['a_account.amount']).toEqual({ readable: true, editable: false }); |
| 172 | + }); |
| 173 | + |
| 174 | + it('a condition the publish gate would refuse holds the save; a fixed one releases it', async () => { |
| 175 | + await renderExpanded(); |
| 176 | + |
| 177 | + // A valid condition first: it lands, and warms the lint engine, so the |
| 178 | + // fault below is judged well inside the autosave pause. |
| 179 | + await authorWhen('amount', "status == 'draft'"); |
| 180 | + await screen.findByText('Valid CEL', {}, { timeout: 3000 }); |
| 181 | + await saveAfter(0); |
| 182 | + |
| 183 | + fireEvent.change(screen.getByLabelText(WHEN_LABEL), { target: { value: 'status ==' } }); |
| 184 | + const held = await screen.findByTestId('perm-autosave-held', {}, { timeout: 3000 }); |
| 185 | + expect(held).toHaveTextContent('Fix the CEL syntax errors before saving.'); |
| 186 | + // Longer than the autosave's pause: the held edit does not land. |
| 187 | + await new Promise((r) => setTimeout(r, 2000)); |
| 188 | + expect(saved).toHaveLength(1); |
| 189 | + |
| 190 | + fireEvent.change(screen.getByLabelText(WHEN_LABEL), { target: { value: "status == 'approved'" } }); |
| 191 | + await waitFor(() => expect(screen.queryByTestId('perm-autosave-held')).toBeNull(), { timeout: 3000 }); |
| 192 | + const set = await saveAfter(1); |
| 193 | + expect(set.fields?.['a_account.amount']).toEqual({ readable: true, editable: false, when: "status == 'approved'" }); |
| 194 | + }); |
| 195 | +}); |
0 commit comments