|
| 1 | +/** @vitest-environment happy-dom */ |
| 2 | + |
| 3 | +import { act } from 'react' |
| 4 | +import { createRoot, type Root } from 'react-dom/client' |
| 5 | +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +import { OAuthAuthorize } from '@/routes/OAuthAuthorize' |
| 8 | +import { type FakeApi, fakeApi, type FakeRoute, jsonResponse, neverResponds } from '@/test/fake-api' |
| 9 | +import { enableReactActEnvironment } from '@/test/react-act' |
| 10 | + |
| 11 | +let container: HTMLDivElement |
| 12 | +let root: Root |
| 13 | +let restoreActEnvironment: () => void |
| 14 | +let assignedLocation: string | undefined |
| 15 | + |
| 16 | +const query = |
| 17 | + '?response_type=code&client_id=https%3A%2F%2Fclient.example%2Fclient.json&redirect_uri=http%3A%2F%2F127.0.0.1%3A3000%2Fcallback&state=abc' |
| 18 | + |
| 19 | +function oauthAuthorizeApi({ |
| 20 | + pending = () => |
| 21 | + jsonResponse({ |
| 22 | + client_id: 'https://client.example/client.json', |
| 23 | + client_name: 'Example MCP Client', |
| 24 | + client_uri: 'https://client.example', |
| 25 | + redirect_uri: 'http://127.0.0.1:3000/callback', |
| 26 | + redirect_host: '127.0.0.1:3000', |
| 27 | + loopback: true, |
| 28 | + resource: 'https://omnara.test/api/mcp', |
| 29 | + }), |
| 30 | + approve = () => jsonResponse({ redirect_url: 'http://127.0.0.1:3000/callback?code=abc' }), |
| 31 | + deny = neverResponds, |
| 32 | +}: { |
| 33 | + pending?: FakeRoute['respond'] |
| 34 | + approve?: FakeRoute['respond'] |
| 35 | + deny?: FakeRoute['respond'] |
| 36 | +} = {}): FakeApi { |
| 37 | + const api = fakeApi([ |
| 38 | + { method: 'GET', path: '/api/auth/oauth/authorize/pending', respond: pending }, |
| 39 | + { method: 'POST', path: '/api/auth/oauth/authorize/approve', respond: approve }, |
| 40 | + { method: 'POST', path: '/api/auth/oauth/authorize/deny', respond: deny }, |
| 41 | + ]) |
| 42 | + vi.stubGlobal('fetch', api.fetch) |
| 43 | + return api |
| 44 | +} |
| 45 | + |
| 46 | +async function renderOAuthAuthorize() { |
| 47 | + await act(async () => { |
| 48 | + root.render(<OAuthAuthorize />) |
| 49 | + await new Promise((resolve) => setTimeout(resolve, 0)) |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +function button(label: string): HTMLButtonElement { |
| 54 | + const match = Array.from(container.querySelectorAll('button')).find( |
| 55 | + (candidate) => candidate.textContent.trim() === label, |
| 56 | + ) |
| 57 | + if (!match) throw new Error(`Missing button: ${label}`) |
| 58 | + return match |
| 59 | +} |
| 60 | + |
| 61 | +beforeAll(() => { |
| 62 | + restoreActEnvironment = enableReactActEnvironment() |
| 63 | +}) |
| 64 | + |
| 65 | +afterAll(() => { |
| 66 | + restoreActEnvironment() |
| 67 | +}) |
| 68 | + |
| 69 | +beforeEach(() => { |
| 70 | + window.history.replaceState(null, '', `/oauth/authorize${query}`) |
| 71 | + assignedLocation = undefined |
| 72 | + vi.spyOn(window.location, 'assign').mockImplementation((url: string | URL) => { |
| 73 | + assignedLocation = String(url) |
| 74 | + }) |
| 75 | + container = document.createElement('div') |
| 76 | + document.body.append(container) |
| 77 | + root = createRoot(container) |
| 78 | +}) |
| 79 | + |
| 80 | +afterEach(() => { |
| 81 | + act(() => { |
| 82 | + root.unmount() |
| 83 | + }) |
| 84 | + container.remove() |
| 85 | + vi.unstubAllGlobals() |
| 86 | + vi.restoreAllMocks() |
| 87 | +}) |
| 88 | + |
| 89 | +describe('OAuth authorization decisions', () => { |
| 90 | + it('shows the client and warns about loopback redirects', async () => { |
| 91 | + oauthAuthorizeApi() |
| 92 | + await renderOAuthAuthorize() |
| 93 | + |
| 94 | + expect(container.textContent).toContain('Example MCP Client') |
| 95 | + expect(container.textContent).toContain('127.0.0.1:3000') |
| 96 | + expect(container.textContent).toContain('redirects to a program running on your own computer') |
| 97 | + }) |
| 98 | + |
| 99 | + it('approves with the original query and follows the redirect', async () => { |
| 100 | + const api = oauthAuthorizeApi() |
| 101 | + await renderOAuthAuthorize() |
| 102 | + |
| 103 | + await act(async () => { |
| 104 | + button('Approve').click() |
| 105 | + await new Promise((resolve) => setTimeout(resolve, 0)) |
| 106 | + }) |
| 107 | + |
| 108 | + expect( |
| 109 | + api.requestsTo('POST', '/api/auth/oauth/authorize/approve').map((request) => request.body), |
| 110 | + ).toEqual([{ query }]) |
| 111 | + expect(assignedLocation).toBe('http://127.0.0.1:3000/callback?code=abc') |
| 112 | + expect(container.textContent).toContain('Returning to the application') |
| 113 | + }) |
| 114 | + |
| 115 | + it('follows the error redirect when the request is rejected for the client', async () => { |
| 116 | + oauthAuthorizeApi({ |
| 117 | + pending: () => |
| 118 | + jsonResponse( |
| 119 | + { |
| 120 | + error: 'invalid_target', |
| 121 | + error_description: 'resource is required', |
| 122 | + redirect_url: 'http://127.0.0.1:3000/callback?error=invalid_target', |
| 123 | + }, |
| 124 | + 400, |
| 125 | + ), |
| 126 | + }) |
| 127 | + await renderOAuthAuthorize() |
| 128 | + |
| 129 | + expect(assignedLocation).toBe('http://127.0.0.1:3000/callback?error=invalid_target') |
| 130 | + }) |
| 131 | + |
| 132 | + it('shows client errors that cannot be redirected', async () => { |
| 133 | + oauthAuthorizeApi({ |
| 134 | + pending: () => |
| 135 | + jsonResponse( |
| 136 | + { error: 'invalid_client', error_description: 'client metadata returned status 404' }, |
| 137 | + 400, |
| 138 | + ), |
| 139 | + }) |
| 140 | + await renderOAuthAuthorize() |
| 141 | + |
| 142 | + expect(assignedLocation).toBeUndefined() |
| 143 | + expect(container.textContent).toContain('client metadata returned status 404') |
| 144 | + }) |
| 145 | +}) |
0 commit comments