litellm/tests/unit/proxy/auth/test_multi_budget_windows.py
quietjules 007d1c1b5e
feat(proxy): support budget_limits windows on internal users (#51763)
* feat(proxy): support internal user budget windows

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(proxy): make user budget migration repeatable and sync API types

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(proxy): bound lock wait for user budget migration

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(proxy): omit unset JSON windows when creating users

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(auth): retain personal budget windows for proxy admins

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(proxy): give user budget windows their own counters and keep them on the user

User window counters move to spend:user_window:<id>:<duration> in Python and Rust, so a user whose id ends in 🪟<duration> can no longer fill another user's window. /user/new stores the windows on the user only, clearing them stores null like keys, edits keep each window's stored reset, and the all-users bulk update goes through the per-user path

* fix(proxy): keep windows on team auto keys and write all users' windows in one transaction

* fix(proxy): clear spend counters on bulk spend reset and read bulk windows under the lock

An all-users bulk update that resets spend now clears each user's spend counters, so users are not left blocked by the old counter. The bulk path reads each user's stored windows after it takes the users lock, so a concurrent edit cannot be overwritten with stale resets.

JWT users created from default_internal_user_params get a reset_at on each default window, and the user window error names the spend and the limit like key and team errors do

* fix(proxy): give member-add users the default budget windows and bulk-update the users read under the lock

Team and org member add now copy default_internal_user_params.budget_limits onto the users they create, each window with its own reset_at, through the same helper the JWT upsert uses

The all-users bulk update now writes, evicts and reports the users it read after taking the users lock, so a user created before the lock gets the new limits

* test(proxy): check default window resets against the reset helper instead of the wall clock

* fix(proxy): return parsed windows from the all-users bulk update and chunk its window writes

* fix(proxy): fail an all-users bulk update whole when its transaction fails

The per-user fallback after a failed all-users transaction wrote the newest
user and then crashed on a missing user_email attribute, so the call answered
500 with one user changed. Racing window bulk updates past the 5s transaction
limit reached it. Answer 500 with no user changed instead

---------

Co-authored-by: mateo <277851410+mateo-berri@users.noreply.github.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-14 07:44:35 -05:00

183 lines
6.2 KiB
Python

"""
Unit tests for multi-budget-window enforcement on API keys.
"""
import re
from collections.abc import Mapping
from unittest.mock import AsyncMock, patch
from typing import Final
import pytest
import litellm
from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.auth.auth_checks import _virtual_key_multi_budget_check
from litellm.proxy.auth import auth_checks
from litellm.proxy import proxy_server
from litellm.models.user import LiteLLM_UserTable
from litellm.redis.stores.spend_counters import user_counter, user_window_counter
from tests.unit.redis.stores.spend_counters_double import FakeSpendCounters
@pytest.mark.asyncio
@pytest.mark.parametrize(
"spend, blocked_by",
[
({user_window_counter("u", "1h"): 4.0, user_window_counter("u", "30d"): 99.0}, None),
(
{user_window_counter("u", "1h"): 5.0, user_window_counter("u", "30d"): 1.0},
"over 1h budget. Spend=$5.0000, Limit=$5.00",
),
(
{user_window_counter("u", "1h"): 1.0, user_window_counter("u", "30d"): 100.0},
"over 30d budget. Spend=$100.0000, Limit=$100.00",
),
({user_counter("u:window:1h"): 50.0, user_counter("u:window:30d"): 500.0}, None),
],
ids=["both_open", "hour_full", "month_full", "lookalike_user_spend"],
)
async def test_user_is_blocked_by_either_window(
monkeypatch: pytest.MonkeyPatch, spend: Mapping[str, float], blocked_by: str | None
) -> None:
counters: Final = FakeSpendCounters(redis=dict(spend))
monkeypatch.setattr(proxy_server, "spend_counter_store", counters.store())
monkeypatch.setattr(proxy_server, "prisma_client", None)
user: Final = LiteLLM_UserTable(
user_id="u",
budget_limits=[
{"budget_duration": "1h", "max_budget": 5.0, "reset_at": "2026-08-01T01:00:00Z"},
{"budget_duration": "30d", "max_budget": 100.0, "reset_at": "2026-08-31T00:00:00Z"},
],
)
if blocked_by is not None:
with pytest.raises(litellm.BudgetExceededError, match=re.escape(blocked_by)):
await auth_checks._user_multi_budget_check(user)
return
await auth_checks._user_multi_budget_check(user)
assert {key: counters.redis[key] for key in spend} == spend
def _make_valid_token(**kwargs) -> UserAPIKeyAuth:
defaults = dict(
token="sk-test-token",
key_name="test",
spend=0.0,
max_budget=None,
budget_limits=[],
)
defaults.update(kwargs)
return UserAPIKeyAuth(**defaults)
@pytest.mark.asyncio
async def test_no_budget_limits_passes():
"""Keys with empty budget_limits should pass without raising."""
token = _make_valid_token(budget_limits=[])
# Should not raise
await _virtual_key_multi_budget_check(valid_token=token)
@pytest.mark.asyncio
async def test_under_budget_passes():
"""Key with spend under all windows should pass."""
token = _make_valid_token(
budget_limits=[
{"budget_duration": "24h", "max_budget": 10.0, "reset_at": None},
{"budget_duration": "30d", "max_budget": 100.0, "reset_at": None},
]
)
with patch(
"litellm.proxy.proxy_server.get_current_spend",
new_callable=AsyncMock,
return_value=1.0, # well under both windows
):
await _virtual_key_multi_budget_check(valid_token=token)
@pytest.mark.asyncio
async def test_over_first_window_raises():
"""Key exceeding the first (daily) window should raise BudgetExceededError."""
token = _make_valid_token(
budget_limits=[
{"budget_duration": "24h", "max_budget": 5.0, "reset_at": None},
{"budget_duration": "30d", "max_budget": 100.0, "reset_at": None},
]
)
spend_by_window = [6.0, 6.0] # over daily, under monthly
call_count = 0
async def fake_get_spend(counter_key, fallback_spend, max_budget=None, **kwargs):
nonlocal call_count
val = spend_by_window[call_count]
call_count += 1
return val
with patch(
"litellm.proxy.proxy_server.get_current_spend", side_effect=fake_get_spend
):
with pytest.raises(litellm.BudgetExceededError) as exc_info:
await _virtual_key_multi_budget_check(valid_token=token)
err = exc_info.value
assert err.status_code == 422
assert "24h" in str(err)
assert "Key over" in str(err)
@pytest.mark.asyncio
async def test_over_second_window_raises():
"""Key exceeding only the monthly window should raise BudgetExceededError referencing 30d."""
token = _make_valid_token(
budget_limits=[
{"budget_duration": "24h", "max_budget": 50.0, "reset_at": None},
{"budget_duration": "30d", "max_budget": 5.0, "reset_at": None},
]
)
spend_by_window = [1.0, 10.0] # under daily, over monthly
call_count = 0
async def fake_get_spend(counter_key, fallback_spend, max_budget=None, **kwargs):
nonlocal call_count
val = spend_by_window[call_count]
call_count += 1
return val
with patch(
"litellm.proxy.proxy_server.get_current_spend", side_effect=fake_get_spend
):
with pytest.raises(litellm.BudgetExceededError) as exc_info:
await _virtual_key_multi_budget_check(valid_token=token)
err = exc_info.value
assert err.status_code == 422
assert "30d" in str(err)
@pytest.mark.asyncio
async def test_budget_limit_entry_objects_coerced():
"""BudgetLimitEntry Pydantic objects (not dicts) must be handled without KeyError.
While budget_limits is normally serialized as List[dict], the auth check must
tolerate BudgetLimitEntry objects in case they arrive without prior serialization.
"""
from litellm.proxy._types import BudgetLimitEntry
token = _make_valid_token(budget_limits=[])
# Bypass Pydantic validation to simulate BudgetLimitEntry objects reaching the check
object.__setattr__(
token,
"budget_limits",
[BudgetLimitEntry(budget_duration="24h", max_budget=10.0)],
)
with patch(
"litellm.proxy.proxy_server.get_current_spend",
new_callable=AsyncMock,
return_value=1.0,
):
# Should not raise TypeError / KeyError — model_dump() coerces the object
await _virtual_key_multi_budget_check(valid_token=token)