From 494aad4a688ac547cd8d76fee45ddbd735f82b61 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Sat, 21 Feb 2026 14:14:50 -0800 Subject: [PATCH] fix(tests): isolate auth in vertex passthrough and spend logs date range tests (#21824) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit test_vertex_passthrough_with_default_credentials and test_view_spend_logs_with_date_range_summarized fail intermittently when a prior xdist worker sets master_key — auth then rejects the unauthenticated test requests before the code under test is reached. - mock user_api_key_auth in test_vertex_passthrough_with_default_credentials (same pattern used for test_vertex_passthrough_with_no_default_credentials in #21810) - wrap test_view_spend_logs_with_date_range_summarized in app.dependency_overrides[ps.user_api_key_auth] with try/finally cleanup (same pattern used for the other spend log tests in #21810) --- .../test_llm_pass_through_endpoints.py | 6 ++- .../test_spend_management_endpoints.py | 44 +++++++++++-------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py b/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py index 922c374418..98161402c4 100644 --- a/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py +++ b/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py @@ -446,12 +446,16 @@ class TestVertexAIPassThroughHandler: "litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints.create_pass_through_route" ) as mock_create_route, mock.patch( "litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints.get_vertex_pass_through_handler" - ) as mock_get_handler: + ) as mock_get_handler, mock.patch( + "litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints.user_api_key_auth", + new_callable=AsyncMock, + ) as mock_auth: # Mock credentials object with necessary attributes mock_credentials = Mock() mock_credentials.token = default_credentials mock_load_auth.return_value = (mock_credentials, default_project) + mock_auth.return_value = MagicMock() # Mock the vertex handler mock_handler = Mock() 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 6e34d27548..c3639e88f6 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 @@ -1977,28 +1977,34 @@ async def test_view_spend_logs_with_date_range_summarized(client, monkeypatch): start_date = (datetime.now(timezone.utc) - timedelta(days=2)).strftime("%Y-%m-%d") end_date = datetime.now(timezone.utc).strftime("%Y-%m-%d") - # Call the endpoint with both start and end dates. - # We don't need `summarize=true` as it's the default. - response = client.get( - "/spend/logs", - params={ - "start_date": start_date, - "end_date": end_date, - }, - headers={"Authorization": "Bearer sk-test"}, + app.dependency_overrides[ps.user_api_key_auth] = lambda: UserAPIKeyAuth( + user_role=LitellmUserRoles.PROXY_ADMIN ) + try: + # Call the endpoint with both start and end dates. + # We don't need `summarize=true` as it's the default. + response = client.get( + "/spend/logs", + params={ + "start_date": start_date, + "end_date": end_date, + }, + headers={"Authorization": "Bearer sk-test"}, + ) - # ASSERTIONS - assert response.status_code == 200 - data = response.json() + # ASSERTIONS + assert response.status_code == 200 + data = response.json() - # Check that the response is not empty and has the summarized structure. - assert isinstance(data, list) - assert len(data) > 0 - assert "startTime" in data[0] - assert "spend" in data[0] - assert "users" in data[0] - assert "models" in data[0] + # Check that the response is not empty and has the summarized structure. + assert isinstance(data, list) + assert len(data) > 0 + assert "startTime" in data[0] + assert "spend" in data[0] + assert "users" in data[0] + assert "models" in data[0] + finally: + app.dependency_overrides.pop(ps.user_api_key_auth, None) @pytest.mark.asyncio