From 22a2dacd9c96d302088ac34b87757823b6a940d3 Mon Sep 17 00:00:00 2001 From: Alexsander Hamir Date: Thu, 1 Jan 2026 10:42:13 -0800 Subject: [PATCH] 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 --- .../mcp_server/test_rest_endpoints.py | 2 +- .../hooks/test_parallel_request_limiter_v3.py | 13 ++++++++-- .../test_spend_management_endpoints.py | 1 + .../test_proxy_setting_endpoints.py | 26 +++++++++++++------ 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_rest_endpoints.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_rest_endpoints.py index d9aba73de8f..0c6d0921952 100644 --- a/tests/test_litellm/proxy/_experimental/mcp_server/test_rest_endpoints.py +++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_rest_endpoints.py @@ -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) diff --git a/tests/test_litellm/proxy/hooks/test_parallel_request_limiter_v3.py b/tests/test_litellm/proxy/hooks/test_parallel_request_limiter_v3.py index b76957dbf39..e190bd1bc52 100644 --- a/tests/test_litellm/proxy/hooks/test_parallel_request_limiter_v3.py +++ b/tests/test_litellm/proxy/hooks/test_parallel_request_limiter_v3.py @@ -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) diff --git a/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py b/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py index 5e3652c6d9d..56bba39e6c3 100644 --- a/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py +++ b/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py @@ -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 = [ diff --git a/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py b/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py index 8fdfd6897a8..b906bd58a23 100644 --- a/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py +++ b/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py @@ -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"""