|
| 1 | +import pytest |
| 2 | +from unittest.mock import MagicMock |
| 3 | +from chromadb.api.shared_system_client import SharedSystemClient |
| 4 | +from chromadb.config import System |
| 5 | +from chromadb.api import ServerAPI |
| 6 | +from typing import Optional, Dict, Generator |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture(autouse=True) |
| 10 | +def clear_cache() -> Generator[None, None, None]: |
| 11 | + """Automatically clear the system cache before and after each test.""" |
| 12 | + SharedSystemClient.clear_system_cache() |
| 13 | + yield |
| 14 | + SharedSystemClient.clear_system_cache() |
| 15 | + |
| 16 | + |
| 17 | +def create_mock_server_api( |
| 18 | + api_url: Optional[str] = None, |
| 19 | + headers: Optional[Dict[str, str]] = None, |
| 20 | + has_session: bool = True, |
| 21 | + has_headers_attr: bool = True, |
| 22 | +) -> MagicMock: |
| 23 | + """Create a mock ServerAPI instance with the specified configuration.""" |
| 24 | + mock_server_api = MagicMock(spec=ServerAPI) |
| 25 | + |
| 26 | + if api_url: |
| 27 | + mock_server_api._api_url = api_url |
| 28 | + |
| 29 | + if has_session: |
| 30 | + mock_session = MagicMock() |
| 31 | + if has_headers_attr: |
| 32 | + mock_session.headers = headers or {} |
| 33 | + else: |
| 34 | + # Create a mock without headers attribute |
| 35 | + del mock_session.headers |
| 36 | + mock_server_api._session = mock_session |
| 37 | + else: |
| 38 | + if hasattr(mock_server_api, "_session"): |
| 39 | + del mock_server_api._session |
| 40 | + |
| 41 | + return mock_server_api |
| 42 | + |
| 43 | + |
| 44 | +def register_mock_system(system_id: str, mock_server_api: MagicMock) -> MagicMock: |
| 45 | + """Register a mock system with the given ID and server API.""" |
| 46 | + mock_system = MagicMock(spec=System) |
| 47 | + mock_system.instance.return_value = mock_server_api |
| 48 | + SharedSystemClient._identifier_to_system[system_id] = mock_system |
| 49 | + return mock_system |
| 50 | + |
| 51 | + |
| 52 | +def test_extracts_api_key_from_chroma_cloud_client() -> None: |
| 53 | + mock_server_api = create_mock_server_api( |
| 54 | + api_url="https://api.trychroma.com/api/v2", |
| 55 | + headers={"X-Chroma-Token": "test-api-key-123"}, |
| 56 | + ) |
| 57 | + register_mock_system("test-id", mock_server_api) |
| 58 | + |
| 59 | + api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() |
| 60 | + |
| 61 | + assert api_key == "test-api-key-123" |
| 62 | + |
| 63 | + |
| 64 | +def test_extracts_api_key_with_lowercase_header() -> None: |
| 65 | + mock_server_api = create_mock_server_api( |
| 66 | + api_url="https://api.trychroma.com/api/v2", |
| 67 | + headers={"x-chroma-token": "test-api-key-456"}, |
| 68 | + ) |
| 69 | + register_mock_system("test-id", mock_server_api) |
| 70 | + |
| 71 | + api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() |
| 72 | + |
| 73 | + assert api_key == "test-api-key-456" |
| 74 | + |
| 75 | + |
| 76 | +def test_skips_non_chroma_cloud_clients() -> None: |
| 77 | + mock_server_api = create_mock_server_api( |
| 78 | + api_url="https://localhost:8000/api/v2", |
| 79 | + headers={"X-Chroma-Token": "local-api-key"}, |
| 80 | + ) |
| 81 | + register_mock_system("test-id", mock_server_api) |
| 82 | + |
| 83 | + api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() |
| 84 | + |
| 85 | + assert api_key is None |
| 86 | + |
| 87 | + |
| 88 | +def test_skips_clients_without_session() -> None: |
| 89 | + mock_server_api = create_mock_server_api( |
| 90 | + api_url="https://api.trychroma.com/api/v2", |
| 91 | + has_session=False, |
| 92 | + ) |
| 93 | + register_mock_system("test-id", mock_server_api) |
| 94 | + |
| 95 | + api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() |
| 96 | + |
| 97 | + assert api_key is None |
| 98 | + |
| 99 | + |
| 100 | +def test_skips_clients_without_api_url() -> None: |
| 101 | + mock_server_api = create_mock_server_api( |
| 102 | + api_url=None, |
| 103 | + headers={"X-Chroma-Token": "test-api-key"}, |
| 104 | + ) |
| 105 | + register_mock_system("test-id", mock_server_api) |
| 106 | + |
| 107 | + api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() |
| 108 | + |
| 109 | + assert api_key is None |
| 110 | + |
| 111 | + |
| 112 | +def test_returns_none_when_no_api_key_in_headers() -> None: |
| 113 | + mock_server_api = create_mock_server_api( |
| 114 | + api_url="https://api.trychroma.com/api/v2", |
| 115 | + headers={}, |
| 116 | + ) |
| 117 | + register_mock_system("test-id", mock_server_api) |
| 118 | + |
| 119 | + api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() |
| 120 | + |
| 121 | + assert api_key is None |
| 122 | + |
| 123 | + |
| 124 | +def test_returns_first_api_key_found_from_multiple_clients() -> None: |
| 125 | + mock_server_api_1 = create_mock_server_api( |
| 126 | + api_url="https://api.trychroma.com/api/v2", |
| 127 | + headers={"X-Chroma-Token": "first-key"}, |
| 128 | + ) |
| 129 | + mock_server_api_2 = create_mock_server_api( |
| 130 | + api_url="https://api.trychroma.com/api/v2", |
| 131 | + headers={"X-Chroma-Token": "second-key"}, |
| 132 | + ) |
| 133 | + register_mock_system("test-id-1", mock_server_api_1) |
| 134 | + register_mock_system("test-id-2", mock_server_api_2) |
| 135 | + |
| 136 | + api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() |
| 137 | + |
| 138 | + assert api_key == "first-key" |
| 139 | + |
| 140 | + |
| 141 | +def test_handles_exception_gracefully() -> None: |
| 142 | + mock_system = MagicMock(spec=System) |
| 143 | + mock_system.instance.side_effect = Exception("Test exception") |
| 144 | + SharedSystemClient._identifier_to_system["test-id"] = mock_system |
| 145 | + |
| 146 | + api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() |
| 147 | + |
| 148 | + assert api_key is None |
| 149 | + |
| 150 | + |
| 151 | +def test_returns_none_when_no_clients_exist() -> None: |
| 152 | + api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() |
| 153 | + |
| 154 | + assert api_key is None |
| 155 | + |
| 156 | + |
| 157 | +def test_skips_chroma_cloud_client_without_headers_attribute() -> None: |
| 158 | + mock_server_api = create_mock_server_api( |
| 159 | + api_url="https://api.trychroma.com/api/v2", |
| 160 | + has_headers_attr=False, |
| 161 | + ) |
| 162 | + register_mock_system("test-id", mock_server_api) |
| 163 | + |
| 164 | + api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() |
| 165 | + |
| 166 | + assert api_key is None |
0 commit comments