|
| 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_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 |
0 commit comments