|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { type TableLocks, UNLOCKED_TABLE_LOCKS } from '@/lib/table/types' |
| 8 | +import { LockSettingsModal } from '@/app/workspace/[workspaceId]/tables/[tableId]/components/lock-settings-modal/lock-settings-modal' |
| 9 | +import { useTableSecurityStore } from '@/stores/table/security/store' |
| 10 | + |
| 11 | +const { mutateAsync } = vi.hoisted(() => ({ mutateAsync: vi.fn() })) |
| 12 | +vi.mock('@/hooks/queries/tables', () => ({ |
| 13 | + useUpdateTableLocks: () => ({ mutateAsync, isPending: false }), |
| 14 | +})) |
| 15 | + |
| 16 | +const LABELS = ['Inserting Rows', 'Updating Rows', 'Deleting Rows', 'Changing Table Schema'] |
| 17 | +let container: HTMLDivElement |
| 18 | +let root: Root |
| 19 | +const onClose = vi.fn() |
| 20 | + |
| 21 | +function render(locks: TableLocks = UNLOCKED_TABLE_LOCKS, isOpen = true) { |
| 22 | + act(() => { |
| 23 | + root.render( |
| 24 | + <LockSettingsModal |
| 25 | + isOpen={isOpen} |
| 26 | + onClose={onClose} |
| 27 | + workspaceId='workspace-1' |
| 28 | + tableId='table-1' |
| 29 | + locks={locks} |
| 30 | + /> |
| 31 | + ) |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +function getSwitch(label: string): HTMLButtonElement { |
| 36 | + const element = document.querySelector<HTMLButtonElement>( |
| 37 | + `button[role="switch"][aria-label="${label}"]` |
| 38 | + ) |
| 39 | + if (!element) throw new Error(`Missing switch: ${label}`) |
| 40 | + return element |
| 41 | +} |
| 42 | + |
| 43 | +function clickSwitch(label: string) { |
| 44 | + act(() => getSwitch(label).click()) |
| 45 | +} |
| 46 | + |
| 47 | +function getPermission(label: string, choice: 'Deny' | 'Allow'): HTMLButtonElement { |
| 48 | + const group = document.querySelector(`[role="radiogroup"][aria-label="${label}"]`) |
| 49 | + const button = [ |
| 50 | + ...(group?.querySelectorAll<HTMLButtonElement>('button[role="radio"]') ?? []), |
| 51 | + ].find((element) => element.textContent === choice) |
| 52 | + if (!button) throw new Error(`Missing permission: ${label} ${choice}`) |
| 53 | + return button |
| 54 | +} |
| 55 | + |
| 56 | +function selectPermission(label: string, choice: 'Deny' | 'Allow') { |
| 57 | + act(() => getPermission(label, choice).click()) |
| 58 | +} |
| 59 | + |
| 60 | +function save() { |
| 61 | + const button = [...document.querySelectorAll<HTMLButtonElement>('button')].find( |
| 62 | + (element) => element.textContent === 'Save' |
| 63 | + ) |
| 64 | + if (!button) throw new Error('Missing Save button') |
| 65 | + act(() => button.click()) |
| 66 | +} |
| 67 | + |
| 68 | +beforeEach(() => { |
| 69 | + globalThis.IS_REACT_ACT_ENVIRONMENT = true |
| 70 | + vi.clearAllMocks() |
| 71 | + mutateAsync.mockReturnValue(new Promise(() => {})) |
| 72 | + useTableSecurityStore.getState().reset() |
| 73 | + container = document.createElement('div') |
| 74 | + document.body.appendChild(container) |
| 75 | + root = createRoot(container) |
| 76 | +}) |
| 77 | + |
| 78 | +afterEach(() => { |
| 79 | + act(() => root.unmount()) |
| 80 | + container.remove() |
| 81 | +}) |
| 82 | + |
| 83 | +describe('Table Security', () => { |
| 84 | + it('hides permissions while disabled and enables all four backend locks by default', () => { |
| 85 | + render() |
| 86 | + expect(getSwitch('Enable Table Security').getAttribute('aria-checked')).toBe('false') |
| 87 | + expect(document.querySelector('[role="radiogroup"]')).toBeNull() |
| 88 | + |
| 89 | + clickSwitch('Enable Table Security') |
| 90 | + for (const label of LABELS) { |
| 91 | + expect(getPermission(label, 'Deny').disabled).toBe(false) |
| 92 | + expect(getPermission(label, 'Allow').disabled).toBe(false) |
| 93 | + expect(getPermission(label, 'Deny').getAttribute('aria-checked')).toBe('true') |
| 94 | + expect(getPermission(label, 'Allow').getAttribute('aria-checked')).toBe('false') |
| 95 | + } |
| 96 | + save() |
| 97 | + |
| 98 | + expect(mutateAsync.mock.calls[0][0]).toEqual({ |
| 99 | + tableId: 'table-1', |
| 100 | + locks: { insertLocked: true, updateLocked: true, deleteLocked: true, schemaLocked: true }, |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + it('inverts existing locks and remembers permissions after disabling, saving, and reopening', async () => { |
| 105 | + render({ insertLocked: true, updateLocked: true, deleteLocked: false, schemaLocked: true }) |
| 106 | + expect(getSwitch('Enable Table Security').getAttribute('aria-checked')).toBe('true') |
| 107 | + expect(getPermission('Deleting Rows', 'Allow').getAttribute('aria-checked')).toBe('true') |
| 108 | + expect(getPermission('Updating Rows', 'Deny').getAttribute('aria-checked')).toBe('true') |
| 109 | + |
| 110 | + selectPermission('Inserting Rows', 'Allow') |
| 111 | + clickSwitch('Enable Table Security') |
| 112 | + expect(document.querySelector('[role="radiogroup"]')).toBeNull() |
| 113 | + let resolveSave!: () => void |
| 114 | + mutateAsync.mockReturnValueOnce( |
| 115 | + new Promise<void>((resolve) => { |
| 116 | + resolveSave = resolve |
| 117 | + }) |
| 118 | + ) |
| 119 | + save() |
| 120 | + expect(mutateAsync.mock.calls[0][0]).toEqual({ |
| 121 | + tableId: 'table-1', |
| 122 | + locks: UNLOCKED_TABLE_LOCKS, |
| 123 | + }) |
| 124 | + await act(async () => resolveSave()) |
| 125 | + |
| 126 | + render(UNLOCKED_TABLE_LOCKS, false) |
| 127 | + render() |
| 128 | + expect(getSwitch('Enable Table Security').getAttribute('aria-checked')).toBe('false') |
| 129 | + expect(document.querySelector('[role="radiogroup"]')).toBeNull() |
| 130 | + clickSwitch('Enable Table Security') |
| 131 | + expect(getPermission('Inserting Rows', 'Allow').getAttribute('aria-checked')).toBe('true') |
| 132 | + expect(getPermission('Updating Rows', 'Deny').getAttribute('aria-checked')).toBe('true') |
| 133 | + save() |
| 134 | + expect(mutateAsync.mock.calls[1][0]).toEqual({ |
| 135 | + tableId: 'table-1', |
| 136 | + locks: { insertLocked: false, updateLocked: true, deleteLocked: false, schemaLocked: true }, |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + it('does not remember unsuccessful changes and discards them on reopen', () => { |
| 141 | + render() |
| 142 | + clickSwitch('Enable Table Security') |
| 143 | + selectPermission('Inserting Rows', 'Allow') |
| 144 | + save() |
| 145 | + expect(useTableSecurityStore.getState().preferences['table-1']).toBeUndefined() |
| 146 | + expect(onClose).not.toHaveBeenCalled() |
| 147 | + |
| 148 | + render(UNLOCKED_TABLE_LOCKS, false) |
| 149 | + render() |
| 150 | + expect(getSwitch('Enable Table Security').getAttribute('aria-checked')).toBe('false') |
| 151 | + expect(document.querySelector('[role="radiogroup"]')).toBeNull() |
| 152 | + clickSwitch('Enable Table Security') |
| 153 | + expect(getPermission('Inserting Rows', 'Deny').getAttribute('aria-checked')).toBe('true') |
| 154 | + }) |
| 155 | +}) |
0 commit comments