diff options
| author | ashwinpvg <[email protected]> | 2025-07-24 10:37:39 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-07-24 17:37:39 +0000 |
| commit | d254d4ce007d446dbd246658345a30ff86413721 (patch) | |
| tree | dbac317ca2a8f8d2f9e11e9100491af77b7fcde4 /packages/core/src/mcp/google-auth-provider.test.ts | |
| parent | 3dd6e431df057af47b96990d0c9c6477ccfbe452 (diff) | |
Add Google credentials provider for authenticating with MCP servers (#4748)
Diffstat (limited to 'packages/core/src/mcp/google-auth-provider.test.ts')
| -rw-r--r-- | packages/core/src/mcp/google-auth-provider.test.ts | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/packages/core/src/mcp/google-auth-provider.test.ts b/packages/core/src/mcp/google-auth-provider.test.ts new file mode 100644 index 00000000..f481b9e2 --- /dev/null +++ b/packages/core/src/mcp/google-auth-provider.test.ts @@ -0,0 +1,67 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { GoogleAuth } from 'google-auth-library'; +import { GoogleCredentialProvider } from './google-auth-provider.js'; +import { vi, describe, beforeEach, it, expect, Mock } from 'vitest'; +import { MCPServerConfig } from '../config/config.js'; + +vi.mock('google-auth-library'); + +describe('GoogleCredentialProvider', () => { + it('should throw an error if no scopes are provided', () => { + expect(() => new GoogleCredentialProvider()).toThrow( + 'Scopes must be provided in the oauth config for Google Credentials provider', + ); + }); + + it('should use scopes from the config if provided', () => { + const config = { + oauth: { + scopes: ['scope1', 'scope2'], + }, + } as MCPServerConfig; + new GoogleCredentialProvider(config); + expect(GoogleAuth).toHaveBeenCalledWith({ + scopes: ['scope1', 'scope2'], + }); + }); + + describe('with provider instance', () => { + let provider: GoogleCredentialProvider; + + beforeEach(() => { + const config = { + oauth: { + scopes: ['scope1', 'scope2'], + }, + } as MCPServerConfig; + provider = new GoogleCredentialProvider(config); + vi.clearAllMocks(); + }); + + it('should return credentials', async () => { + const mockClient = { + getAccessToken: vi.fn().mockResolvedValue({ token: 'test-token' }), + }; + (GoogleAuth.prototype.getClient as Mock).mockResolvedValue(mockClient); + + const credentials = await provider.tokens(); + + expect(credentials?.access_token).toBe('test-token'); + }); + + it('should return undefined if access token is not available', async () => { + const mockClient = { + getAccessToken: vi.fn().mockResolvedValue({ token: null }), + }; + (GoogleAuth.prototype.getClient as Mock).mockResolvedValue(mockClient); + + const credentials = await provider.tokens(); + expect(credentials).toBeUndefined(); + }); + }); +}); |
