test(proxy/client): isolate client tests from the developer's real CLI token

Client falls back to the key stored by lite login when no api_key is
given, so test_client_without_api_key failed on any machine whose
~/.litellm/token.json was issued for the URL the test uses. An autouse
conftest fixture now stubs load_cli_token for the whole client test
directory; the same latent leak existed in test_models.py and
test_model_groups.py. Also dedupes test functions in test_client.py
that shadowed each other and adds coverage for the CLI token fallback
and its base_url origin check, which had no tests at the Client level
This commit is contained in:
mateo-berri 2026-06-10 18:59:25 -07:00
parent 4def6916da
commit 3c8ae26e35
2 changed files with 37 additions and 13 deletions

View file

@ -0,0 +1,9 @@
import pytest
@pytest.fixture(autouse=True)
def isolate_from_real_cli_token(monkeypatch):
"""Never let tests read the developer's real ~/.litellm/token.json from `lite login`."""
monkeypatch.setattr(
"litellm.litellm_core_utils.cli_token_utils.load_cli_token", lambda: None
)

View file

@ -74,18 +74,40 @@ def test_client_without_api_key(base_url):
assert client.http._api_key is None
def test_client_initialization():
"""Test that the client is initialized correctly."""
def test_client_falls_back_to_stored_cli_token(base_url, monkeypatch):
"""Test that the client uses the `lite login` token issued for this server"""
monkeypatch.setattr(
"litellm.litellm_core_utils.cli_token_utils.load_cli_token",
lambda: {"key": "sk-cli-token", "base_url": base_url},
)
client = Client(base_url=base_url)
assert client._api_key == "sk-cli-token"
assert client.http._api_key == "sk-cli-token"
def test_client_ignores_cli_token_issued_for_other_server(base_url, monkeypatch):
"""Test that the client never sends a stored CLI token to a different server"""
monkeypatch.setattr(
"litellm.litellm_core_utils.cli_token_utils.load_cli_token",
lambda: {"key": "sk-cli-token", "base_url": "https://other-proxy.example"},
)
client = Client(base_url=base_url)
assert client._api_key is None
assert client.http._api_key is None
def test_client_custom_timeout():
"""Test that the client passes a custom timeout to the http client."""
client = Client(
base_url="http://localhost:4000",
api_key="test-key",
timeout=60,
)
# Check that http client is initialized correctly
assert isinstance(client.http, HTTPClient)
assert client.http._base_url == "http://localhost:4000"
assert client.http._api_key == "test-key"
assert client.http._timeout == 60
@ -97,10 +119,3 @@ def test_client_default_timeout():
)
assert client.http._timeout == 30
def test_client_without_api_key():
"""Test that the client works without an API key."""
client = Client(base_url="http://localhost:4000")
assert client.http._api_key is None