|
| 1 | +"""Tests for Claude Code config file reader.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from pyfakefs.fake_filesystem import FakeFilesystem |
| 7 | + |
| 8 | +from cycode.cli.apps.ai_guardrails.scan.claude_config import get_mcp_servers, get_user_email, load_claude_config |
| 9 | + |
| 10 | + |
| 11 | +def test_load_claude_config_valid(fs: FakeFilesystem) -> None: |
| 12 | + """Test loading a valid ~/.claude.json file.""" |
| 13 | + config = {'oauthAccount': {'emailAddress': 'user@example.com'}} |
| 14 | + config_path = Path.home() / '.claude.json' |
| 15 | + fs.create_file(config_path, contents=json.dumps(config)) |
| 16 | + |
| 17 | + result = load_claude_config(config_path) |
| 18 | + assert result == config |
| 19 | + |
| 20 | + |
| 21 | +def test_load_claude_config_missing_file(fs: FakeFilesystem) -> None: |
| 22 | + """Test loading when ~/.claude.json does not exist.""" |
| 23 | + fs.create_dir(Path.home()) |
| 24 | + config_path = Path.home() / '.claude.json' |
| 25 | + |
| 26 | + result = load_claude_config(config_path) |
| 27 | + assert result is None |
| 28 | + |
| 29 | + |
| 30 | +def test_load_claude_config_corrupt_file(fs: FakeFilesystem) -> None: |
| 31 | + """Test loading when ~/.claude.json contains invalid JSON.""" |
| 32 | + config_path = Path.home() / '.claude.json' |
| 33 | + fs.create_file(config_path, contents='not valid json {{{') |
| 34 | + |
| 35 | + result = load_claude_config(config_path) |
| 36 | + assert result is None |
| 37 | + |
| 38 | + |
| 39 | +def test_get_user_email_present() -> None: |
| 40 | + """Test extracting email when oauthAccount.emailAddress exists.""" |
| 41 | + config = {'oauthAccount': {'emailAddress': 'user@example.com'}} |
| 42 | + assert get_user_email(config) == 'user@example.com' |
| 43 | + |
| 44 | + |
| 45 | +def test_get_user_email_missing_oauth_account() -> None: |
| 46 | + """Test extracting email when oauthAccount key is missing.""" |
| 47 | + config = {'someOtherKey': 'value'} |
| 48 | + assert get_user_email(config) is None |
| 49 | + |
| 50 | + |
| 51 | +def test_get_user_email_missing_email_address() -> None: |
| 52 | + """Test extracting email when oauthAccount exists but emailAddress is missing.""" |
| 53 | + config = {'oauthAccount': {'someOtherField': 'value'}} |
| 54 | + assert get_user_email(config) is None |
| 55 | + |
| 56 | + |
| 57 | +def test_get_mcp_servers_stdio_and_http() -> None: |
| 58 | + """Test extracting MCP servers with both stdio and http types.""" |
| 59 | + config = { |
| 60 | + 'mcpServers': { |
| 61 | + 'gitlab': { |
| 62 | + 'type': 'stdio', |
| 63 | + 'command': '/opt/homebrew/bin/gitlab-mcp', |
| 64 | + 'args': ['--verbose'], |
| 65 | + }, |
| 66 | + 'atlassian': { |
| 67 | + 'type': 'http', |
| 68 | + 'url': 'https://mcp.atlassian.com/v1/mcp', |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + result = get_mcp_servers(config) |
| 74 | + assert len(result) == 2 |
| 75 | + |
| 76 | + gitlab = next(s for s in result if s['name'] == 'gitlab') |
| 77 | + assert gitlab['server_type'] == 'Local' |
| 78 | + assert gitlab['command'] == '/opt/homebrew/bin/gitlab-mcp' |
| 79 | + assert gitlab['args'] == ['--verbose'] |
| 80 | + assert gitlab['url'] is None |
| 81 | + |
| 82 | + atlassian = next(s for s in result if s['name'] == 'atlassian') |
| 83 | + assert atlassian['server_type'] == 'Remote' |
| 84 | + assert atlassian['command'] is None |
| 85 | + assert atlassian['url'] == 'https://mcp.atlassian.com/v1/mcp' |
| 86 | + |
| 87 | + |
| 88 | +def test_get_mcp_servers_empty() -> None: |
| 89 | + """Test extracting MCP servers when mcpServers is empty.""" |
| 90 | + config = {'mcpServers': {}} |
| 91 | + assert get_mcp_servers(config) == [] |
| 92 | + |
| 93 | + |
| 94 | +def test_get_mcp_servers_missing_key() -> None: |
| 95 | + """Test extracting MCP servers when mcpServers key is missing.""" |
| 96 | + config = {'someOtherKey': 'value'} |
| 97 | + assert get_mcp_servers(config) == [] |
| 98 | + |
| 99 | + |
| 100 | +def test_get_mcp_servers_invalid_type() -> None: |
| 101 | + """Test extracting MCP servers when mcpServers is not a dict.""" |
| 102 | + config = {'mcpServers': 'not a dict'} |
| 103 | + assert get_mcp_servers(config) == [] |
| 104 | + |
| 105 | + |
| 106 | +def test_get_mcp_servers_unknown_server_type() -> None: |
| 107 | + """Test that unknown server types are passed through as-is.""" |
| 108 | + config = { |
| 109 | + 'mcpServers': { |
| 110 | + 'custom': { |
| 111 | + 'type': 'sse', |
| 112 | + 'url': 'https://example.com/sse', |
| 113 | + }, |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + result = get_mcp_servers(config) |
| 118 | + assert len(result) == 1 |
| 119 | + assert result[0]['server_type'] == 'sse' |
0 commit comments