|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The list under the Connect an Agent setup steps: the AI assistants the |
| 5 | + * signed-in person authorized, and their API keys. |
| 6 | + * |
| 7 | + * - §1 assistants come from `oauth.authorizedClients.list()`: name (or the |
| 8 | + * client id when the client declares none), the granted scopes in the |
| 9 | + * consent page's own words, when authorized, and "in the hour from" the |
| 10 | + * recorded last use or "not recorded"; the organization only when it is |
| 11 | + * not the active one; and no Revoke control on an assistant row; |
| 12 | + * - §2 keys are the person's OWN `sys_api_key` rows: the read filters on the |
| 13 | + * signed-in user (a platform administrator's unfiltered read answers every |
| 14 | + * person's keys) and never selects the secret; revoked and expired keys |
| 15 | + * carry their state and no Revoke; |
| 16 | + * - §3 Revoke on an active key asks first, naming the key; Cancel writes |
| 17 | + * nothing; confirming writes `revoked: true` and the list re-reads in place; |
| 18 | + * - §4 empty, failed and signed-out states. |
| 19 | + * |
| 20 | + * `t` echoes the key with any interpolation values appended, so assertions |
| 21 | + * read which string was asked for and with what. |
| 22 | + */ |
| 23 | + |
| 24 | +import '@testing-library/jest-dom/vitest'; |
| 25 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 26 | +import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'; |
| 27 | + |
| 28 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 29 | + ...(await importOriginal<Record<string, unknown>>()), |
| 30 | + useObjectTranslation: () => ({ |
| 31 | + t: (key: string, opts?: Record<string, unknown>) => { |
| 32 | + const vals = opts ? Object.values(opts).filter((v) => typeof v === 'string' || typeof v === 'number') : []; |
| 33 | + return vals.length ? `«${key}|${vals.join('|')}»` : `«${key}»`; |
| 34 | + }, |
| 35 | + language: 'en', |
| 36 | + }), |
| 37 | +})); |
| 38 | + |
| 39 | +let adapter: unknown = null; |
| 40 | +vi.mock('@object-ui/react', async (importOriginal) => ({ |
| 41 | + ...(await importOriginal<Record<string, unknown>>()), |
| 42 | + useAdapter: () => adapter, |
| 43 | +})); |
| 44 | + |
| 45 | +let authUser: { id: string } | null = { id: 'user_me' }; |
| 46 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 47 | + ...(await importOriginal<Record<string, unknown>>()), |
| 48 | + useAuth: () => ({ |
| 49 | + user: authUser, |
| 50 | + activeOrganization: { id: 'org_here', name: 'Here Inc' }, |
| 51 | + organizations: [ |
| 52 | + { id: 'org_here', name: 'Here Inc' }, |
| 53 | + { id: 'org_other', name: 'Other Ltd' }, |
| 54 | + ], |
| 55 | + }), |
| 56 | +})); |
| 57 | + |
| 58 | +import { notifyDataChanged } from '@object-ui/react'; |
| 59 | +import { AuthorizedAccessList } from './AuthorizedAccessList'; |
| 60 | + |
| 61 | +type Row = Record<string, unknown>; |
| 62 | + |
| 63 | +const CLIENTS = [ |
| 64 | + { |
| 65 | + id: 'consent_1', |
| 66 | + clientId: 'client_cursor', |
| 67 | + name: 'Cursor', |
| 68 | + scopes: ['data:read', 'offline_access'], |
| 69 | + organizationId: 'org_here', |
| 70 | + authorizedAt: '2026-11-06T21:12:21.007Z', |
| 71 | + lastUsedAt: null, |
| 72 | + }, |
| 73 | + { |
| 74 | + id: 'consent_2', |
| 75 | + clientId: 'client_desktop', |
| 76 | + name: 'Claude Desktop', |
| 77 | + scopes: ['data:read', 'data:write', 'made:up'], |
| 78 | + organizationId: 'org_other', |
| 79 | + authorizedAt: '2026-11-06T21:12:20.843Z', |
| 80 | + lastUsedAt: '2026-11-06T21:00:00.000Z', |
| 81 | + }, |
| 82 | + { |
| 83 | + id: 'consent_3', |
| 84 | + clientId: 'client_nameless', |
| 85 | + name: null, |
| 86 | + scopes: [], |
| 87 | + organizationId: null, |
| 88 | + authorizedAt: null, |
| 89 | + lastUsedAt: null, |
| 90 | + }, |
| 91 | +]; |
| 92 | + |
| 93 | +const FAR_FUTURE = '2999-01-01T00:00:00.000Z'; |
| 94 | +let keyRows: Row[]; |
| 95 | +let listMock: ReturnType<typeof vi.fn>; |
| 96 | +let findMock: ReturnType<typeof vi.fn>; |
| 97 | +let updateMock: ReturnType<typeof vi.fn>; |
| 98 | + |
| 99 | +beforeEach(() => { |
| 100 | + authUser = { id: 'user_me' }; |
| 101 | + keyRows = [ |
| 102 | + { id: 'k_active', name: 'ci key', prefix: 'osk_abc', created_at: '2026-11-01T10:00:00.000Z', last_used_at: '2026-11-06T21:12:47.191Z', expires_at: FAR_FUTURE, revoked: false }, |
| 103 | + { id: 'k_unused', name: 'spare key', prefix: 'osk_def', created_at: '2026-11-02T10:00:00.000Z', last_used_at: null, expires_at: null, revoked: false }, |
| 104 | + { id: 'k_revoked', name: 'old key', prefix: 'osk_ghi', created_at: '2026-10-01T10:00:00.000Z', last_used_at: null, expires_at: FAR_FUTURE, revoked: true }, |
| 105 | + { id: 'k_expired', name: 'stale key', prefix: 'osk_jkl', created_at: '2026-01-01T10:00:00.000Z', last_used_at: null, expires_at: '2026-02-01T00:00:00.000Z', revoked: false }, |
| 106 | + ]; |
| 107 | + listMock = vi.fn(async () => ({ clients: CLIENTS, truncated: false })); |
| 108 | + findMock = vi.fn(async () => ({ data: keyRows.map((r) => ({ ...r })) })); |
| 109 | + updateMock = vi.fn(async (resource: string, id: string, patch: Row) => { |
| 110 | + keyRows = keyRows.map((r) => (r.id === id ? { ...r, ...patch } : r)); |
| 111 | + // What the console's mutation bridge does for every adapter write. |
| 112 | + notifyDataChanged({ objectName: resource, recordId: id }); |
| 113 | + return keyRows.find((r) => r.id === id); |
| 114 | + }); |
| 115 | + adapter = { |
| 116 | + getClient: () => ({ oauth: { authorizedClients: { list: listMock } } }), |
| 117 | + find: findMock, |
| 118 | + update: updateMock, |
| 119 | + }; |
| 120 | +}); |
| 121 | + |
| 122 | +afterEach(() => { |
| 123 | + cleanup(); |
| 124 | +}); |
| 125 | + |
| 126 | +async function renderSettled() { |
| 127 | + render(<AuthorizedAccessList />); |
| 128 | + await screen.findAllByTestId('authorized-assistant'); |
| 129 | + await screen.findAllByTestId('api-key-row'); |
| 130 | +} |
| 131 | + |
| 132 | +function assistantRow(name: string): HTMLElement { |
| 133 | + const row = screen.getAllByTestId('authorized-assistant').find((el) => el.textContent?.includes(name)); |
| 134 | + if (!row) throw new Error(`no assistant row for ${name}`); |
| 135 | + return row; |
| 136 | +} |
| 137 | + |
| 138 | +function keyRow(name: string): HTMLElement { |
| 139 | + const row = screen.getAllByTestId('api-key-row').find((el) => el.textContent?.includes(name)); |
| 140 | + if (!row) throw new Error(`no key row for ${name}`); |
| 141 | + return row; |
| 142 | +} |
| 143 | + |
| 144 | +describe('§1 authorized assistants', () => { |
| 145 | + it('lists each grant with its name, scopes in consent words, when authorized and last used', async () => { |
| 146 | + await renderSettled(); |
| 147 | + expect(listMock).toHaveBeenCalledTimes(1); |
| 148 | + expect(screen.getAllByTestId('authorized-assistant')).toHaveLength(3); |
| 149 | + |
| 150 | + const desktop = assistantRow('Claude Desktop'); |
| 151 | + expect(within(desktop).getByText(/«oauth\.consent\.scope\.dataRead»/)).toBeInTheDocument(); |
| 152 | + expect(within(desktop).getByText(/«oauth\.consent\.scope\.dataWrite»/)).toBeInTheDocument(); |
| 153 | + // A scope the page cannot name is shown as unrecognised, never dropped. |
| 154 | + expect(within(desktop).getByText('«oauth.consent.scope.unrecognised|made:up»')).toBeInTheDocument(); |
| 155 | + expect(within(desktop).getByText(/^«connectAgent\.access\.authorized\|/)).toBeInTheDocument(); |
| 156 | + // An hour-coarse use reads as "in the hour from", never as a moment. |
| 157 | + expect(within(desktop).getByText(/^«connectAgent\.access\.lastUsedHour\|/)).toBeInTheDocument(); |
| 158 | + |
| 159 | + const cursor = assistantRow('Cursor'); |
| 160 | + expect(within(cursor).getByText('«connectAgent.access.notRecorded»')).toBeInTheDocument(); |
| 161 | + expect(within(cursor).getByText('«oauth.consent.scope.offlineAccess»')).toBeInTheDocument(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('names the organization only when it is not the active one', async () => { |
| 165 | + await renderSettled(); |
| 166 | + expect(within(assistantRow('Claude Desktop')).getByText('«connectAgent.access.inOrganization|Other Ltd»')).toBeInTheDocument(); |
| 167 | + expect(within(assistantRow('Cursor')).queryByText(/inOrganization/)).toBeNull(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('shows the client id beside the fallback when the client declares no name', async () => { |
| 171 | + await renderSettled(); |
| 172 | + const nameless = assistantRow('client_nameless'); |
| 173 | + expect(within(nameless).getByText('«connectAgent.access.unnamed»')).toBeInTheDocument(); |
| 174 | + }); |
| 175 | + |
| 176 | + it('offers no Revoke on any assistant row, and says withdrawing is not available here yet', async () => { |
| 177 | + await renderSettled(); |
| 178 | + for (const row of screen.getAllByTestId('authorized-assistant')) { |
| 179 | + expect(within(row).queryByRole('button')).toBeNull(); |
| 180 | + } |
| 181 | + expect(screen.getByText('«connectAgent.access.withdrawLater»')).toBeInTheDocument(); |
| 182 | + expect(screen.queryByText(/connectAgent\.access\.truncated/)).toBeNull(); |
| 183 | + }); |
| 184 | + |
| 185 | + it('says when the server answered only the newest grants', async () => { |
| 186 | + listMock.mockResolvedValueOnce({ clients: CLIENTS, truncated: true }); |
| 187 | + await renderSettled(); |
| 188 | + expect(screen.getByText('«connectAgent.access.truncated|3»')).toBeInTheDocument(); |
| 189 | + }); |
| 190 | +}); |
| 191 | + |
| 192 | +describe('§2 API keys', () => { |
| 193 | + it("reads only the signed-in person's own keys, and never the secret", async () => { |
| 194 | + await renderSettled(); |
| 195 | + expect(findMock).toHaveBeenCalledTimes(1); |
| 196 | + const [resource, params] = findMock.mock.calls[0] as [string, Row]; |
| 197 | + expect(resource).toBe('sys_api_key'); |
| 198 | + expect(params.$filter).toEqual({ user_id: 'user_me' }); |
| 199 | + expect(params.$select).toEqual(['id', 'name', 'prefix', 'created_at', 'last_used_at', 'expires_at', 'revoked']); |
| 200 | + expect(params.$select).not.toContain('key'); |
| 201 | + expect(params.$select).not.toContain('key_hash'); |
| 202 | + }); |
| 203 | + |
| 204 | + it('shows created, last used or never used, and the revoked and expired states', async () => { |
| 205 | + await renderSettled(); |
| 206 | + expect(within(keyRow('ci key')).getByText(/^«connectAgent\.access\.keyLastUsed\|/)).toBeInTheDocument(); |
| 207 | + expect(within(keyRow('ci key')).getByText(/^«connectAgent\.access\.created\|/)).toBeInTheDocument(); |
| 208 | + expect(within(keyRow('spare key')).getByText('«connectAgent.access.neverUsed»')).toBeInTheDocument(); |
| 209 | + expect(within(keyRow('old key')).getByText('«connectAgent.access.revoked»')).toBeInTheDocument(); |
| 210 | + expect(within(keyRow('stale key')).getByText('«connectAgent.access.expired»')).toBeInTheDocument(); |
| 211 | + |
| 212 | + expect(within(keyRow('ci key')).getByRole('button', { name: '«connectAgent.access.revoke»' })).toBeInTheDocument(); |
| 213 | + expect(within(keyRow('spare key')).getByRole('button', { name: '«connectAgent.access.revoke»' })).toBeInTheDocument(); |
| 214 | + expect(within(keyRow('old key')).queryByRole('button')).toBeNull(); |
| 215 | + expect(within(keyRow('stale key')).queryByRole('button')).toBeNull(); |
| 216 | + }); |
| 217 | +}); |
| 218 | + |
| 219 | +describe('§3 revoking a key', () => { |
| 220 | + it('asks first, naming the key; Cancel writes nothing', async () => { |
| 221 | + await renderSettled(); |
| 222 | + fireEvent.click(within(keyRow('ci key')).getByRole('button', { name: '«connectAgent.access.revoke»' })); |
| 223 | + const dialog = await screen.findByRole('alertdialog'); |
| 224 | + expect(within(dialog).getByText('«connectAgent.access.revokeTitle|ci key»')).toBeInTheDocument(); |
| 225 | + fireEvent.click(within(dialog).getByRole('button', { name: '«common.cancel»' })); |
| 226 | + await waitFor(() => expect(screen.queryByRole('alertdialog')).toBeNull()); |
| 227 | + expect(updateMock).not.toHaveBeenCalled(); |
| 228 | + }); |
| 229 | + |
| 230 | + it('confirming writes revoked: true and re-reads the list in place', async () => { |
| 231 | + await renderSettled(); |
| 232 | + fireEvent.click(within(keyRow('ci key')).getByRole('button', { name: '«connectAgent.access.revoke»' })); |
| 233 | + const dialog = await screen.findByRole('alertdialog'); |
| 234 | + fireEvent.click(within(dialog).getByRole('button', { name: '«connectAgent.access.revoke»' })); |
| 235 | + |
| 236 | + await waitFor(() => expect(updateMock).toHaveBeenCalledWith('sys_api_key', 'k_active', { revoked: true })); |
| 237 | + await waitFor(() => expect(findMock).toHaveBeenCalledTimes(2)); |
| 238 | + await waitFor(() => |
| 239 | + expect(within(keyRow('ci key')).getByText('«connectAgent.access.revoked»')).toBeInTheDocument(), |
| 240 | + ); |
| 241 | + expect(within(keyRow('ci key')).queryByRole('button')).toBeNull(); |
| 242 | + // The assistants are not re-read by a key write. |
| 243 | + expect(listMock).toHaveBeenCalledTimes(1); |
| 244 | + }); |
| 245 | +}); |
| 246 | + |
| 247 | +describe('§4 empty, failed and signed-out states', () => { |
| 248 | + it('says so when there is nothing to list', async () => { |
| 249 | + listMock.mockResolvedValueOnce({ clients: [], truncated: false }); |
| 250 | + findMock.mockResolvedValueOnce({ data: [] }); |
| 251 | + render(<AuthorizedAccessList />); |
| 252 | + expect(await screen.findByText('«connectAgent.access.assistantsEmpty»')).toBeInTheDocument(); |
| 253 | + expect(await screen.findByText('«connectAgent.access.keysEmpty»')).toBeInTheDocument(); |
| 254 | + }); |
| 255 | + |
| 256 | + it('names a failed read instead of showing an empty list', async () => { |
| 257 | + listMock.mockRejectedValueOnce(new Error('boom')); |
| 258 | + findMock.mockRejectedValueOnce(new Error('nope')); |
| 259 | + render(<AuthorizedAccessList />); |
| 260 | + expect(await screen.findByText('«connectAgent.access.loadFailed|boom»')).toBeInTheDocument(); |
| 261 | + expect(await screen.findByText('«connectAgent.access.loadFailed|nope»')).toBeInTheDocument(); |
| 262 | + expect(screen.queryByText('«connectAgent.access.assistantsEmpty»')).toBeNull(); |
| 263 | + }); |
| 264 | + |
| 265 | + it('renders nothing and reads nothing without a signed-in person or an adapter', () => { |
| 266 | + authUser = null; |
| 267 | + const { container, unmount } = render(<AuthorizedAccessList />); |
| 268 | + expect(container).toBeEmptyDOMElement(); |
| 269 | + unmount(); |
| 270 | + authUser = { id: 'user_me' }; |
| 271 | + adapter = null; |
| 272 | + const second = render(<AuthorizedAccessList />); |
| 273 | + expect(second.container).toBeEmptyDOMElement(); |
| 274 | + expect(listMock).not.toHaveBeenCalled(); |
| 275 | + expect(findMock).not.toHaveBeenCalled(); |
| 276 | + }); |
| 277 | +}); |
0 commit comments