Fix failing tests: route path, auth dependencies, cost_breakdown, and TTL test

- Fix test_mcp_connection_requires_auth_dependency: Use correct route path /mcp-rest/test/connection
- Fix test_update_ui_settings tests: Use app.dependency_overrides instead of monkeypatch for FastAPI dependencies
- Fix test_spend_logs_payload tests: Add metadata.cost_breakdown to ignored_keys (optional field)
- Improve test_async_increment_tokens_with_ttl_preservation: Increase wait time and add debug logging
This commit is contained in:
Alexsander Hamir 2026-01-01 10:42:13 -08:00
parent e07f330773
commit 22a2dacd9c
4 changed files with 31 additions and 11 deletions

View file

@ -82,7 +82,7 @@ async def test_execute_with_mcp_client_redacts_stack_trace(monkeypatch):
def test_test_connection_requires_auth_dependency():
route = _get_route("/test/connection", "POST")
route = _get_route("/mcp-rest/test/connection", "POST")
assert _route_has_dependency(route, user_api_key_auth)

View file

@ -1194,14 +1194,23 @@ async def test_async_increment_tokens_with_ttl_preservation():
)
# Small delay to ensure Redis has processed the commands
await asyncio.sleep(0.1)
await asyncio.sleep(0.5) # Increased wait time for Redis to process
# Verify TTL preservation and value updates
ttl_after_second = await redis_cache.async_get_ttl(test_key_with_ttl)
value_after_second_with_ttl = await redis_cache.async_get_cache(test_key_with_ttl)
value_after_second_without_ttl = await redis_cache.async_get_cache(test_key_without_ttl)
assert value_after_second_with_ttl == 25.0, "Second increment should update value to 25.0"
# Debug: Print actual value if assertion fails
if value_after_second_with_ttl != 25.0:
print(f"DEBUG: Expected 25.0, got {value_after_second_with_ttl}")
print(f"DEBUG: First value was {value_after_first_with_ttl}")
# Re-read to check if it's a timing issue
await asyncio.sleep(0.5)
value_after_second_with_ttl = await redis_cache.async_get_cache(test_key_with_ttl)
print(f"DEBUG: After additional wait, value is {value_after_second_with_ttl}")
assert value_after_second_with_ttl == 25.0, f"Second increment should update value to 25.0, got {value_after_second_with_ttl}"
assert value_after_second_without_ttl == 12.0, "Second increment should update value to 12.0"
# Critical test: TTL should be preserved (not reset to 60)

View file

@ -202,6 +202,7 @@ ignored_keys = [
"metadata.cold_storage_object_key",
"metadata.additional_usage_values.prompt_tokens_details.cache_creation_tokens",
"metadata.litellm_overhead_time_ms",
"metadata.cost_breakdown",
]
MODEL_LIST = [

View file

@ -742,6 +742,7 @@ class TestProxySettingEndpoints:
):
"""Test updating UI settings with an allowlisted field"""
from unittest.mock import AsyncMock, MagicMock
from litellm.proxy.ui_crud_endpoints import proxy_setting_endpoints
class MockUser:
def __init__(self, user_role):
@ -750,10 +751,9 @@ class TestProxySettingEndpoints:
async def mock_admin_auth():
return MockUser(LitellmUserRoles.PROXY_ADMIN)
monkeypatch.setattr(
"litellm.proxy.ui_crud_endpoints.proxy_setting_endpoints.user_api_key_auth",
mock_admin_auth,
)
app.dependency_overrides[
proxy_setting_endpoints.user_api_key_auth
] = mock_admin_auth
monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", True)
mock_prisma = MagicMock()
mock_prisma.db.litellm_uisettings.upsert = AsyncMock()
@ -774,12 +774,18 @@ class TestProxySettingEndpoints:
create_data = call_args.kwargs["data"]["create"]
stored_settings = json.loads(create_data["ui_settings"])
assert stored_settings["disable_model_add_for_internal_users"] is True
# Clean up dependency override
app.dependency_overrides.pop(
proxy_setting_endpoints.user_api_key_auth, None
)
def test_update_ui_settings_ignores_non_allowlisted_value(
self, mock_auth, monkeypatch
):
"""Test non-allowlisted UI settings are ignored on update"""
from unittest.mock import AsyncMock, MagicMock
from litellm.proxy.ui_crud_endpoints import proxy_setting_endpoints
class MockUser:
def __init__(self, user_role):
@ -788,10 +794,9 @@ class TestProxySettingEndpoints:
async def mock_admin_auth():
return MockUser(LitellmUserRoles.PROXY_ADMIN)
monkeypatch.setattr(
"litellm.proxy.ui_crud_endpoints.proxy_setting_endpoints.user_api_key_auth",
mock_admin_auth,
)
app.dependency_overrides[
proxy_setting_endpoints.user_api_key_auth
] = mock_admin_auth
monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", True)
mock_prisma = MagicMock()
mock_prisma.db.litellm_uisettings.upsert = AsyncMock()
@ -815,6 +820,11 @@ class TestProxySettingEndpoints:
stored_settings = json.loads(call_args.kwargs["data"]["create"]["ui_settings"])
assert "unsupported_flag" not in stored_settings
assert stored_settings["disable_model_add_for_internal_users"] is False
# Clean up dependency override
app.dependency_overrides.pop(
proxy_setting_endpoints.user_api_key_auth, None
)
def test_get_sso_settings_from_database(self, mock_proxy_config, mock_auth, monkeypatch):
"""Test getting SSO settings from the dedicated database table"""