|
| 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 { ResourcePolicyDocument } from '@/lib/resource-policies/types' |
| 8 | + |
| 9 | +const mocks = vi.hoisted(() => ({ |
| 10 | + mutationError: null as Error | null, |
| 11 | + mutateAsync: vi.fn(), |
| 12 | + reset: vi.fn(), |
| 13 | + toastError: vi.fn(), |
| 14 | + toastSuccess: vi.fn(), |
| 15 | + useAccess: vi.fn(), |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock('@sim/emcn', () => ({ |
| 19 | + ChipTextarea: () => null, |
| 20 | + Info: () => null, |
| 21 | + Label: () => null, |
| 22 | + toast: { error: mocks.toastError, success: mocks.toastSuccess }, |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock('@/hooks/queries/credential-groups', () => ({ |
| 26 | + useCredentialGroupAccess: mocks.useAccess, |
| 27 | + useUpdateCredentialGroupAccess: () => ({ |
| 28 | + error: mocks.mutationError, |
| 29 | + isPending: false, |
| 30 | + mutateAsync: mocks.mutateAsync, |
| 31 | + reset: mocks.reset, |
| 32 | + }), |
| 33 | +})) |
| 34 | + |
| 35 | +import { useCredentialGroupAccessEditor } from '@/ee/credential-groups/components/credential-group-access' |
| 36 | + |
| 37 | +const GROUP_ID = 'group-1' |
| 38 | +const EMPTY_DOCUMENT: ResourcePolicyDocument = { |
| 39 | + version: 1, |
| 40 | + resource: { type: 'credential_group', id: GROUP_ID }, |
| 41 | + statements: [], |
| 42 | +} |
| 43 | +const EDITED_DOCUMENT: ResourcePolicyDocument = { |
| 44 | + ...EMPTY_DOCUMENT, |
| 45 | + statements: [ |
| 46 | + { |
| 47 | + sid: 'SupportWorkflow', |
| 48 | + effect: 'allow', |
| 49 | + actions: ['credential_groups.credentials.use'], |
| 50 | + principals: [{ type: 'workflow', workflowId: 'workflow-1' }], |
| 51 | + }, |
| 52 | + ], |
| 53 | +} |
| 54 | + |
| 55 | +const mountedRoots: Root[] = [] |
| 56 | + |
| 57 | +function renderEditor() { |
| 58 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 59 | + const container = document.createElement('div') |
| 60 | + const root = createRoot(container) |
| 61 | + mountedRoots.push(root) |
| 62 | + let result: ReturnType<typeof useCredentialGroupAccessEditor> | undefined |
| 63 | + |
| 64 | + function Probe() { |
| 65 | + result = useCredentialGroupAccessEditor({ workspaceId: 'workspace-1', groupId: GROUP_ID }) |
| 66 | + return null |
| 67 | + } |
| 68 | + |
| 69 | + const render = () => { |
| 70 | + act(() => root.render(<Probe />)) |
| 71 | + } |
| 72 | + render() |
| 73 | + |
| 74 | + return { |
| 75 | + getResult: () => { |
| 76 | + if (!result) throw new Error('Editor hook did not render') |
| 77 | + return result |
| 78 | + }, |
| 79 | + rerender: render, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +beforeEach(() => { |
| 84 | + vi.clearAllMocks() |
| 85 | + mocks.mutationError = null |
| 86 | + mocks.reset.mockImplementation(() => { |
| 87 | + mocks.mutationError = null |
| 88 | + }) |
| 89 | + mocks.useAccess.mockReturnValue({ |
| 90 | + data: { revision: 3, document: EMPTY_DOCUMENT }, |
| 91 | + error: null, |
| 92 | + isPending: false, |
| 93 | + }) |
| 94 | + mocks.mutateAsync.mockResolvedValue({ revision: 4, document: EDITED_DOCUMENT }) |
| 95 | +}) |
| 96 | + |
| 97 | +afterEach(() => { |
| 98 | + act(() => { |
| 99 | + for (const root of mountedRoots.splice(0)) root.unmount() |
| 100 | + }) |
| 101 | +}) |
| 102 | + |
| 103 | +describe('Credential Group access editor', () => { |
| 104 | + it('pretty-prints the policy and rejects invalid JSON before mutation', async () => { |
| 105 | + const editor = renderEditor() |
| 106 | + |
| 107 | + expect(editor.getResult().value).toBe(JSON.stringify(EMPTY_DOCUMENT, null, 2)) |
| 108 | + expect(editor.getResult().dirty).toBe(false) |
| 109 | + |
| 110 | + act(() => editor.getResult().setValue('{')) |
| 111 | + |
| 112 | + expect(editor.getResult().dirty).toBe(true) |
| 113 | + expect(editor.getResult().validationError).toBe('Policy must be valid JSON') |
| 114 | + await act(async () => editor.getResult().save()) |
| 115 | + expect(mocks.mutateAsync).not.toHaveBeenCalled() |
| 116 | + }) |
| 117 | + |
| 118 | + it('pins the revision and preserves the draft when a concurrent update conflicts', async () => { |
| 119 | + const editor = renderEditor() |
| 120 | + const editedValue = JSON.stringify(EDITED_DOCUMENT, null, 2) |
| 121 | + act(() => editor.getResult().setValue(editedValue)) |
| 122 | + |
| 123 | + mocks.useAccess.mockReturnValue({ |
| 124 | + data: { revision: 4, document: EMPTY_DOCUMENT }, |
| 125 | + error: null, |
| 126 | + isPending: false, |
| 127 | + }) |
| 128 | + const conflict = new Error('Resource policy changed while being edited') |
| 129 | + mocks.mutateAsync.mockImplementation(async () => { |
| 130 | + mocks.mutationError = conflict |
| 131 | + throw conflict |
| 132 | + }) |
| 133 | + editor.rerender() |
| 134 | + |
| 135 | + await act(async () => editor.getResult().save()) |
| 136 | + editor.rerender() |
| 137 | + |
| 138 | + expect(mocks.mutateAsync).toHaveBeenCalledWith({ |
| 139 | + workspaceId: 'workspace-1', |
| 140 | + groupId: GROUP_ID, |
| 141 | + body: { expectedRevision: 3, document: EDITED_DOCUMENT }, |
| 142 | + }) |
| 143 | + expect(editor.getResult().value).toBe(editedValue) |
| 144 | + expect(editor.getResult().dirty).toBe(true) |
| 145 | + expect(editor.getResult().error).toBe('Resource policy changed while being edited') |
| 146 | + }) |
| 147 | + |
| 148 | + it('discards the local draft back to the query document', () => { |
| 149 | + const editor = renderEditor() |
| 150 | + act(() => editor.getResult().setValue(JSON.stringify(EDITED_DOCUMENT, null, 2))) |
| 151 | + |
| 152 | + act(() => editor.getResult().discard()) |
| 153 | + |
| 154 | + expect(editor.getResult().dirty).toBe(false) |
| 155 | + expect(editor.getResult().value).toBe(JSON.stringify(EMPTY_DOCUMENT, null, 2)) |
| 156 | + }) |
| 157 | +}) |
0 commit comments