From 566f2ed6fcdc6e99df9cb68385c5b28056b2cfd4 Mon Sep 17 00:00:00 2001 From: Filipe Andujar Date: Thu, 22 Oct 2026 15:18:57 -0300 Subject: [PATCH] test(proxy): cover the cache key strip on a request without litellm_params Codecov flagged the early return when the body carries no litellm_params as the one uncovered line. The test also pins that the strip never adds a litellm_params slot the caller did not send --- .../unit/proxy/test_litellm_pre_call_utils.py | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/unit/proxy/test_litellm_pre_call_utils.py b/tests/unit/proxy/test_litellm_pre_call_utils.py index c0f0d6287f6..d4d3ba09dde 100644 --- a/tests/unit/proxy/test_litellm_pre_call_utils.py +++ b/tests/unit/proxy/test_litellm_pre_call_utils.py @@ -3,6 +3,7 @@ import copy import json import os import time +from collections.abc import Mapping from datetime import datetime, timezone from types import SimpleNamespace from typing import Final @@ -1193,13 +1194,18 @@ _FORGED_CACHE_KEY_FIELDS: Final = { } -async def _add_data_with_forged_cache_key(monkeypatch: pytest.MonkeyPatch, cache_scope: str) -> dict: +_FORGED_TOP_LEVEL_CACHE_KEY_ONLY: Final = {"cache_key": "ns:victim-hash", "preset_cache_key": "ns:victim-hash"} + + +async def _add_data_with_forged_cache_key( + monkeypatch: pytest.MonkeyPatch, cache_scope: str, forged: Mapping[str, object] = _FORGED_CACHE_KEY_FIELDS +) -> dict: monkeypatch.setattr(litellm, "cache", Cache(type=LiteLLMCacheType.LOCAL, cache_scope=cache_scope)) return await add_litellm_data_to_request( data={ "model": "gpt-4o", "messages": [{"role": "user", "content": "hi"}], - **copy.deepcopy(_FORGED_CACHE_KEY_FIELDS), + **copy.deepcopy(dict(forged)), }, request=_make_request_mock("/v1/chat/completions", {"Content-Type": "application/json"}), user_api_key_dict=UserAPIKeyAuth(api_key="hashed-key", team_id="caller-team"), @@ -1222,6 +1228,18 @@ async def test_scoped_cache_ignores_caller_supplied_cache_keys(monkeypatch: pyte assert updated["metadata"]["user_api_key_team_id"] == "caller-team" +@pytest.mark.asyncio +@pytest.mark.parametrize("cache_scope", ["team", "key"]) +async def test_scoped_cache_strips_a_forged_cache_key_on_a_request_without_litellm_params( + monkeypatch: pytest.MonkeyPatch, cache_scope: str +): + updated: Final = await _add_data_with_forged_cache_key(monkeypatch, cache_scope, _FORGED_TOP_LEVEL_CACHE_KEY_ONLY) + + assert "cache_key" not in updated + assert "preset_cache_key" not in updated + assert "litellm_params" not in updated + + @pytest.mark.asyncio async def test_global_cache_scope_keeps_caller_supplied_cache_key(monkeypatch: pytest.MonkeyPatch): updated: Final = await _add_data_with_forged_cache_key(monkeypatch, "global")