|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Setup → Integrations → Connector Connections (ADR-0143 D5, item 7 of |
| 5 | + * objectstack#26702) — behaviour, over stubbed connection, registry and |
| 6 | + * declaration doors. |
| 7 | + * |
| 8 | + * 1. **the status view** — one row per declared `oauth2` |
| 9 | + * `authorization_code` instance (other auth kinds are not listed), with |
| 10 | + * this organization's connection status and the server's reason, and a |
| 11 | + * `degraded` instance's reason beside the connection that caused it. |
| 12 | + * 2. **connect** — the sign-in window opens on the click, is detached from |
| 13 | + * the page, and is sent to the URL the begin leg answered; the URL is |
| 14 | + * never rendered. The page re-reads the status until the connect lands, |
| 15 | + * then closes the window and shows the new status. |
| 16 | + * 3. **refusals** — a blocked window sends nothing; a refused begin closes |
| 17 | + * the window and shows the server's words; a URL that is not http(s) is |
| 18 | + * never navigated to. |
| 19 | + * 4. **disconnect** — only behind a confirmation; Cancel sends nothing. |
| 20 | + */ |
| 21 | + |
| 22 | +import '@testing-library/jest-dom/vitest'; |
| 23 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 24 | +import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'; |
| 25 | +import React from 'react'; |
| 26 | +import { MemoryRouter } from 'react-router-dom'; |
| 27 | +import type { ConnectorConnectionState } from '@objectstack/spec/contracts'; |
| 28 | + |
| 29 | +import { ConnectorConnectionsPage } from './ConnectorConnectionsPage'; |
| 30 | + |
| 31 | +const META = '/api/v1/meta/connector'; |
| 32 | +const REGISTRY = '/api/v1/automation/connectors'; |
| 33 | +const door = (name: string) => `/api/v1/automation/connectors/${name}/connection`; |
| 34 | +/** What a begin answers. Its `state` value must never reach the page's text. */ |
| 35 | +const AUTH_URL = 'https://vendor.example.test/authorize?client_id=c1&state=STATE-NEVER-RENDERED&code_challenge=x'; |
| 36 | +const REASON = "connector instance 'hubspot' needs to be reconnected for organization 'org_1': the refresh token was refused"; |
| 37 | + |
| 38 | +const declared = [ |
| 39 | + { name: 'hubspot', label: 'HubSpot', provider: 'mcp', auth: { type: 'oauth2', grant: 'authorization_code' } }, |
| 40 | + { name: 'notion', label: 'Notion', provider: 'mcp', auth: { type: 'oauth2', grant: 'authorization_code', clientId: 'n1' } }, |
| 41 | + { name: 'stripe', label: 'Stripe', provider: 'openapi', auth: { type: 'bearer', credentialRef: 'STRIPE_KEY' } }, |
| 42 | + { name: 'feishu', label: 'Feishu', provider: 'rest', auth: { type: 'oauth2', grant: 'client_credentials', clientId: 'f1' } }, |
| 43 | +]; |
| 44 | + |
| 45 | +const state = (connector: string, over: Partial<ConnectorConnectionState> = {}): ConnectorConnectionState => ({ |
| 46 | + connector, |
| 47 | + organizationId: 'org_1', |
| 48 | + status: 'not_connected', |
| 49 | + ...over, |
| 50 | +}); |
| 51 | + |
| 52 | +const envelope = (data: unknown) => ({ success: true, data }); |
| 53 | +const errorEnvelope = (code: string, message: string) => ({ success: false, error: { code, message } }); |
| 54 | + |
| 55 | +type Answer = { status: number; body: unknown }; |
| 56 | +let calls: Array<{ url: string; method: string; body?: unknown }>; |
| 57 | +let answers: Map<string, Answer[]>; |
| 58 | +let fakeWindow: { closed: boolean; opener: unknown; location: { href: string }; close: ReturnType<typeof vi.fn> } | null; |
| 59 | +let openSpy: ReturnType<typeof vi.fn>; |
| 60 | + |
| 61 | +/** Queue answers for `METHOD url`; the last one repeats. */ |
| 62 | +function answer(method: string, url: string, ...list: Answer[]) { |
| 63 | + answers.set(`${method} ${url}`, list); |
| 64 | +} |
| 65 | + |
| 66 | +beforeEach(() => { |
| 67 | + calls = []; |
| 68 | + answers = new Map(); |
| 69 | + answer('GET', META, { status: 200, body: { type: 'connector', items: declared } }); |
| 70 | + answer('GET', REGISTRY, { |
| 71 | + status: 200, |
| 72 | + body: envelope({ |
| 73 | + connectors: [ |
| 74 | + { name: 'hubspot', label: 'HubSpot', type: 'api', origin: 'declarative', state: 'degraded', degradedReason: `reconnect required: ${REASON}`, actions: [] }, |
| 75 | + { name: 'notion', label: 'Notion', type: 'api', origin: 'declarative', state: 'ready', actions: [] }, |
| 76 | + ], |
| 77 | + total: 2, |
| 78 | + }), |
| 79 | + }); |
| 80 | + answer('GET', door('hubspot'), { status: 200, body: envelope(state('hubspot', { status: 'reconnect_required', statusReason: REASON, connectedAt: '2026-10-30T08:00:00.000Z', connectedBy: 'ada@acme.test' })) }); |
| 81 | + answer('GET', door('notion'), { status: 200, body: envelope(state('notion')) }); |
| 82 | + vi.stubGlobal( |
| 83 | + 'fetch', |
| 84 | + vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { |
| 85 | + const url = String(input); |
| 86 | + const method = (init?.method ?? 'GET').toUpperCase(); |
| 87 | + calls.push({ url, method, body: init?.body }); |
| 88 | + const queue = answers.get(`${method} ${url}`); |
| 89 | + const next = queue ? (queue.length > 1 ? queue.shift()! : queue[0]) : { status: 500, body: errorEnvelope('X', `unexpected ${method} ${url}`) }; |
| 90 | + return { ok: next.status >= 200 && next.status < 300, status: next.status, json: async () => next.body } as unknown as Response; |
| 91 | + }), |
| 92 | + ); |
| 93 | + fakeWindow = { closed: false, opener: window, location: { href: 'about:blank' }, close: vi.fn(() => { if (fakeWindow) fakeWindow.closed = true; }) }; |
| 94 | + openSpy = vi.fn(() => fakeWindow); |
| 95 | + vi.stubGlobal('open', openSpy); |
| 96 | +}); |
| 97 | + |
| 98 | +afterEach(() => { |
| 99 | + cleanup(); |
| 100 | + vi.unstubAllGlobals(); |
| 101 | +}); |
| 102 | + |
| 103 | +function renderPage() { |
| 104 | + return render( |
| 105 | + <MemoryRouter> |
| 106 | + <ConnectorConnectionsPage pollMs={10} /> |
| 107 | + </MemoryRouter>, |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +const row = (name: string) => screen.getByTestId(`connection-row-${name}`); |
| 112 | +const sent = (method: string, url: string) => calls.filter((c) => c.method === method && c.url === url); |
| 113 | + |
| 114 | +describe('the status view', () => { |
| 115 | + it('lists only the instances an administrator connects, with this organization\'s status', async () => { |
| 116 | + renderPage(); |
| 117 | + expect(await screen.findByRole('heading', { name: 'Connector connections' })).toBeInTheDocument(); |
| 118 | + await screen.findByTestId('connection-row-hubspot'); |
| 119 | + |
| 120 | + expect(screen.queryByTestId('connection-row-stripe')).toBeNull(); |
| 121 | + expect(screen.queryByTestId('connection-row-feishu')).toBeNull(); |
| 122 | + expect(within(row('notion')).getByText('Not connected')).toBeInTheDocument(); |
| 123 | + expect(within(row('notion')).getByRole('button', { name: 'Connect' })).toBeInTheDocument(); |
| 124 | + expect(within(row('notion')).queryByRole('button', { name: 'Disconnect' })).toBeNull(); |
| 125 | + |
| 126 | + expect(within(row('hubspot')).getByText('Reconnect required')).toBeInTheDocument(); |
| 127 | + expect(screen.getByTestId('connection-reason-hubspot')).toHaveTextContent(REASON); |
| 128 | + expect(within(row('hubspot')).getByText(/by ada@acme\.test$/)).toBeInTheDocument(); |
| 129 | + expect(within(row('hubspot')).getByRole('button', { name: 'Reconnect' })).toBeInTheDocument(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('a degraded instance shows the server\'s reason beside the connection that caused it', async () => { |
| 133 | + renderPage(); |
| 134 | + const degraded = await screen.findByTestId('connection-degraded-hubspot'); |
| 135 | + expect(degraded).toHaveTextContent('Degraded'); |
| 136 | + expect(degraded).toHaveTextContent(`reconnect required: ${REASON}`); |
| 137 | + expect(screen.queryByTestId('connection-degraded-notion')).toBeNull(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('a refused status read shows the server\'s words and offers no action', async () => { |
| 141 | + answer('GET', door('notion'), { status: 403, body: errorEnvelope('PERMISSION_DENIED', 'Connecting … requires the `manage_connector_connections` capability.') }); |
| 142 | + renderPage(); |
| 143 | + const refused = await screen.findByTestId('connection-refused-notion'); |
| 144 | + expect(refused).toHaveTextContent('manage_connector_connections'); |
| 145 | + expect(within(row('notion')).queryByRole('button')).toBeNull(); |
| 146 | + }); |
| 147 | + |
| 148 | + it('a failed declaration read is a failure with Try again; no connectable instance is the empty state', async () => { |
| 149 | + answer('GET', META, { status: 500, body: errorEnvelope('INTERNAL_ERROR', 'boom') }, { status: 200, body: { items: [declared[2]] } }); |
| 150 | + renderPage(); |
| 151 | + expect(await screen.findByText('Could not load connector connections.')).toBeInTheDocument(); |
| 152 | + fireEvent.click(screen.getByRole('button', { name: 'Try again' })); |
| 153 | + expect(await screen.findByTestId('connector-connections-empty')).toBeInTheDocument(); |
| 154 | + }); |
| 155 | +}); |
| 156 | + |
| 157 | +describe('connect', () => { |
| 158 | + it('opens a detached window on the click, sends it to the begun URL, and shows the connect once it lands', async () => { |
| 159 | + answer('POST', door('notion'), { status: 200, body: envelope({ connector: 'notion', organizationId: 'org_1', authorizationUrl: AUTH_URL, expiresAt: new Date(Date.now() + 600_000).toISOString() }) }); |
| 160 | + renderPage(); |
| 161 | + await screen.findByTestId('connection-row-notion'); |
| 162 | + answer('GET', door('notion'), |
| 163 | + { status: 200, body: envelope(state('notion')) }, |
| 164 | + { status: 200, body: envelope(state('notion', { status: 'connected', connectedAt: '2026-11-01T10:00:00.000Z', connectedBy: 'ada@acme.test', scopes: 'read write' })) }); |
| 165 | + |
| 166 | + fireEvent.click(within(row('notion')).getByRole('button', { name: 'Connect' })); |
| 167 | + expect(openSpy).toHaveBeenCalledTimes(1); |
| 168 | + expect(openSpy.mock.calls[0][0]).toBe('about:blank'); |
| 169 | + await waitFor(() => expect(fakeWindow!.location.href).toBe(AUTH_URL)); |
| 170 | + expect(fakeWindow!.opener).toBeNull(); |
| 171 | + expect(sent('POST', door('notion'))).toHaveLength(1); |
| 172 | + expect(sent('POST', door('notion'))[0].body).toBeUndefined(); |
| 173 | + |
| 174 | + await waitFor(() => expect(within(row('notion')).getByText('Connected')).toBeInTheDocument()); |
| 175 | + expect(fakeWindow!.close).toHaveBeenCalled(); |
| 176 | + expect(within(row('notion')).getByText('Scopes: read write')).toBeInTheDocument(); |
| 177 | + expect(screen.getByText('Connected notion.')).toBeInTheDocument(); |
| 178 | + expect(document.body.textContent).not.toContain('STATE-NEVER-RENDERED'); |
| 179 | + }); |
| 180 | + |
| 181 | + it('a window closed before the connect lands stops waiting and shows the status read then', async () => { |
| 182 | + answer('POST', door('notion'), { status: 200, body: envelope({ connector: 'notion', organizationId: 'org_1', authorizationUrl: AUTH_URL, expiresAt: new Date(Date.now() + 600_000).toISOString() }) }); |
| 183 | + renderPage(); |
| 184 | + await screen.findByTestId('connection-row-notion'); |
| 185 | + fireEvent.click(within(row('notion')).getByRole('button', { name: 'Connect' })); |
| 186 | + expect(await screen.findByText('Finish signing in in the window that opened.')).toBeInTheDocument(); |
| 187 | + fakeWindow!.closed = true; |
| 188 | + await waitFor(() => expect(within(row('notion')).getByRole('button', { name: 'Connect' })).toBeEnabled()); |
| 189 | + expect(within(row('notion')).getByText('Not connected')).toBeInTheDocument(); |
| 190 | + }); |
| 191 | +}); |
| 192 | + |
| 193 | +describe('refusals', () => { |
| 194 | + it('a blocked window sends nothing', async () => { |
| 195 | + openSpy.mockReturnValue(null); |
| 196 | + renderPage(); |
| 197 | + await screen.findByTestId('connection-row-notion'); |
| 198 | + fireEvent.click(within(row('notion')).getByRole('button', { name: 'Connect' })); |
| 199 | + expect(await screen.findByRole('alert')).toHaveTextContent('The browser blocked the sign-in window.'); |
| 200 | + expect(sent('POST', door('notion'))).toHaveLength(0); |
| 201 | + }); |
| 202 | + |
| 203 | + it('a refused begin closes the window and shows the server\'s words', async () => { |
| 204 | + const refusal = 'This deployment has no fixed public origin for the OAuth redirect, so a connector instance cannot be connected.'; |
| 205 | + answer('POST', door('notion'), { status: 503, body: errorEnvelope('SERVICE_UNAVAILABLE', refusal) }); |
| 206 | + renderPage(); |
| 207 | + await screen.findByTestId('connection-row-notion'); |
| 208 | + fireEvent.click(within(row('notion')).getByRole('button', { name: 'Connect' })); |
| 209 | + expect(await screen.findByRole('alert')).toHaveTextContent(refusal); |
| 210 | + expect(fakeWindow!.close).toHaveBeenCalled(); |
| 211 | + expect(fakeWindow!.location.href).toBe('about:blank'); |
| 212 | + }); |
| 213 | + |
| 214 | + it('an authorization URL that is not http(s) is never navigated to', async () => { |
| 215 | + answer('POST', door('notion'), { status: 200, body: envelope({ connector: 'notion', organizationId: 'org_1', authorizationUrl: 'javascript:alert(1)', expiresAt: '' }) }); |
| 216 | + renderPage(); |
| 217 | + await screen.findByTestId('connection-row-notion'); |
| 218 | + fireEvent.click(within(row('notion')).getByRole('button', { name: 'Connect' })); |
| 219 | + expect(await screen.findByRole('alert')).toHaveTextContent('without an authorization URL to open'); |
| 220 | + expect(fakeWindow!.location.href).toBe('about:blank'); |
| 221 | + expect(fakeWindow!.close).toHaveBeenCalled(); |
| 222 | + }); |
| 223 | +}); |
| 224 | + |
| 225 | +describe('disconnect', () => { |
| 226 | + it('is behind a confirmation; Cancel sends nothing, confirming sends DELETE and shows the answer', async () => { |
| 227 | + answer('DELETE', door('hubspot'), { status: 200, body: envelope(state('hubspot')) }); |
| 228 | + renderPage(); |
| 229 | + await screen.findByTestId('connection-row-hubspot'); |
| 230 | + |
| 231 | + fireEvent.click(within(row('hubspot')).getByRole('button', { name: 'Disconnect' })); |
| 232 | + fireEvent.click(await screen.findByRole('button', { name: 'Cancel' })); |
| 233 | + expect(sent('DELETE', door('hubspot'))).toHaveLength(0); |
| 234 | + |
| 235 | + fireEvent.click(within(row('hubspot')).getByRole('button', { name: 'Disconnect' })); |
| 236 | + const dialog = await screen.findByRole('dialog'); |
| 237 | + fireEvent.click(within(dialog).getByRole('button', { name: 'Disconnect' })); |
| 238 | + await waitFor(() => expect(within(row('hubspot')).getByText('Not connected')).toBeInTheDocument()); |
| 239 | + expect(sent('DELETE', door('hubspot'))).toHaveLength(1); |
| 240 | + expect(screen.getByText('Disconnected hubspot.')).toBeInTheDocument(); |
| 241 | + }); |
| 242 | + |
| 243 | + it('a refused disconnect keeps the dialog open with the server\'s words', async () => { |
| 244 | + answer('DELETE', door('hubspot'), { status: 404, body: errorEnvelope('RESOURCE_NOT_FOUND', "Connector instance 'hubspot' is not connected for organization 'org_1', so there is nothing to revoke.") }); |
| 245 | + renderPage(); |
| 246 | + await screen.findByTestId('connection-row-hubspot'); |
| 247 | + fireEvent.click(within(row('hubspot')).getByRole('button', { name: 'Disconnect' })); |
| 248 | + const dialog = await screen.findByRole('dialog'); |
| 249 | + fireEvent.click(within(dialog).getByRole('button', { name: 'Disconnect' })); |
| 250 | + expect(await within(dialog).findByRole('alert')).toHaveTextContent('nothing to revoke'); |
| 251 | + }); |
| 252 | +}); |
0 commit comments