|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, vi } from 'vitest'; |
| 8 | +import { |
| 9 | + MCPOAuthClientProvider, |
| 10 | + type OAuthAuthorizationResponse, |
| 11 | +} from './mcp-oauth-provider.js'; |
| 12 | +import type { |
| 13 | + OAuthClientInformation, |
| 14 | + OAuthClientMetadata, |
| 15 | + OAuthTokens, |
| 16 | +} from '@modelcontextprotocol/sdk/shared/auth.js'; |
| 17 | + |
| 18 | +describe('MCPOAuthClientProvider', () => { |
| 19 | + const mockRedirectUrl = 'http://localhost:8090/callback'; |
| 20 | + const mockClientMetadata: OAuthClientMetadata = { |
| 21 | + client_name: 'Test Client', |
| 22 | + redirect_uris: [mockRedirectUrl], |
| 23 | + grant_types: ['authorization_code', 'refresh_token'], |
| 24 | + response_types: ['code'], |
| 25 | + token_endpoint_auth_method: 'client_secret_post', |
| 26 | + scope: 'test-scope', |
| 27 | + }; |
| 28 | + const mockState = 'test-state-123'; |
| 29 | + |
| 30 | + describe('oauth flow', () => { |
| 31 | + it('should support full OAuth flow', async () => { |
| 32 | + const onRedirectMock = vi.fn(); |
| 33 | + const provider = new MCPOAuthClientProvider( |
| 34 | + mockRedirectUrl, |
| 35 | + mockClientMetadata, |
| 36 | + mockState, |
| 37 | + onRedirectMock, |
| 38 | + ); |
| 39 | + |
| 40 | + // Step 1: Save client information |
| 41 | + const clientInfo: OAuthClientInformation = { |
| 42 | + client_id: 'my-client-id', |
| 43 | + client_secret: 'my-client-secret', |
| 44 | + }; |
| 45 | + provider.saveClientInformation(clientInfo); |
| 46 | + |
| 47 | + // Step 2: Save code verifier |
| 48 | + provider.saveCodeVerifier('my-code-verifier'); |
| 49 | + |
| 50 | + // Step 3: Set up callback server |
| 51 | + const mockAuthResponse: OAuthAuthorizationResponse = { |
| 52 | + code: 'authorization-code', |
| 53 | + state: mockState, |
| 54 | + }; |
| 55 | + const mockServer = { |
| 56 | + port: Promise.resolve(8090), |
| 57 | + waitForResponse: vi.fn().mockResolvedValue(mockAuthResponse), |
| 58 | + close: vi.fn().mockResolvedValue(undefined), |
| 59 | + }; |
| 60 | + provider.saveCallbackServer(mockServer); |
| 61 | + |
| 62 | + // Step 4: Redirect to authorization |
| 63 | + const authUrl = new URL('http://auth.example.com/authorize'); |
| 64 | + await provider.redirectToAuthorization(authUrl); |
| 65 | + |
| 66 | + // Step 5: Save tokens after exchange |
| 67 | + const tokens: OAuthTokens = { |
| 68 | + access_token: 'final-access-token', |
| 69 | + token_type: 'Bearer', |
| 70 | + expires_in: 3600, |
| 71 | + refresh_token: 'final-refresh-token', |
| 72 | + }; |
| 73 | + provider.saveTokens(tokens); |
| 74 | + |
| 75 | + // Verify all data is stored correctly |
| 76 | + expect(provider.clientInformation()).toEqual(clientInfo); |
| 77 | + expect(provider.codeVerifier()).toBe('my-code-verifier'); |
| 78 | + expect(provider.state()).toBe(mockState); |
| 79 | + expect(provider.tokens()).toEqual(tokens); |
| 80 | + expect(onRedirectMock).toHaveBeenCalledWith(authUrl); |
| 81 | + expect(provider.getSavedCallbackServer()).toBe(mockServer); |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
0 commit comments