|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#30918] `oauth.authorizedClients` — the URL each method builds and the |
| 5 | + * answer it hands back. The server half (who may read whose grants) is pinned |
| 6 | + * in plugin-auth's `authorized-clients-read.real-plugin.test.ts`; this file |
| 7 | + * pins only what the SDK puts on the wire and what it returns. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it, expect, expectTypeOf, vi } from 'vitest'; |
| 11 | +import { ObjectStackClient, type AuthorizedClientsList } from './index'; |
| 12 | + |
| 13 | +const BASE = 'http://localhost:3000'; |
| 14 | + |
| 15 | +const LIST: AuthorizedClientsList = { |
| 16 | + clients: [ |
| 17 | + { |
| 18 | + id: 'consent_1', |
| 19 | + clientId: 'client_1', |
| 20 | + name: 'Desk Assistant', |
| 21 | + scopes: ['openid', 'mcp:read'], |
| 22 | + organizationId: 'org_1', |
| 23 | + authorizedAt: '2026-11-01T09:12:00.000Z', |
| 24 | + lastUsedAt: '2026-11-06T10:00:00.000Z', |
| 25 | + }, |
| 26 | + ], |
| 27 | + truncated: false, |
| 28 | +}; |
| 29 | + |
| 30 | +function clientAnswering(body: unknown, status = 200) { |
| 31 | + const fetchMock = vi.fn(async () => |
| 32 | + new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } }), |
| 33 | + ); |
| 34 | + const client = new ObjectStackClient({ baseUrl: BASE, fetch: fetchMock as never }); |
| 35 | + return { client, fetchMock }; |
| 36 | +} |
| 37 | + |
| 38 | +describe('#30918 oauth.authorizedClients', () => { |
| 39 | + it('list() reads the caller\'s own grants with NO parameter and unwraps the envelope', async () => { |
| 40 | + const { client, fetchMock } = clientAnswering({ success: true, data: LIST }); |
| 41 | + const answer = await client.oauth.authorizedClients.list(); |
| 42 | + expect(answer).toEqual(LIST); |
| 43 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 44 | + const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit | undefined]; |
| 45 | + // The route refuses any parameter, so the method sends none. |
| 46 | + expect(url).toBe(`${BASE}/api/v1/auth/authorized-clients`); |
| 47 | + expect(init?.method ?? 'GET').toBe('GET'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('listForUser(id) names the person in the one parameter the admin route takes, encoded', async () => { |
| 51 | + const { client, fetchMock } = clientAnswering({ success: true, data: LIST }); |
| 52 | + const answer = await client.oauth.authorizedClients.listForUser('user a&b'); |
| 53 | + expect(answer).toEqual(LIST); |
| 54 | + const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit | undefined]; |
| 55 | + expect(url).toBe(`${BASE}/api/v1/auth/admin/authorized-clients?userId=user%20a%26b`); |
| 56 | + expect(init?.method ?? 'GET').toBe('GET'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('a refusal reaches the caller as a throw, not as an empty list', async () => { |
| 60 | + const { client } = clientAnswering( |
| 61 | + { success: false, error: { code: 'PERMISSION_DENIED', message: 'Platform administrator access required.' } }, |
| 62 | + 403, |
| 63 | + ); |
| 64 | + await expect(client.oauth.authorizedClients.listForUser('user_2')).rejects.toThrow(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('both methods are typed with the answer they unwrap', () => { |
| 68 | + const client = new ObjectStackClient({ baseUrl: BASE, fetch: vi.fn() as never }); |
| 69 | + expectTypeOf(client.oauth.authorizedClients.list).returns.toEqualTypeOf<Promise<AuthorizedClientsList>>(); |
| 70 | + expectTypeOf(client.oauth.authorizedClients.listForUser).returns.toEqualTypeOf<Promise<AuthorizedClientsList>>(); |
| 71 | + }); |
| 72 | +}); |
0 commit comments