litellm/tests/test_litellm/proxy
yuneng-jiang 2e76be019a
Cherry-pick #29358 onto patch/v1.84.3 (#29363)
* fix(reset_budget): write only {spend, budget_reset_at} and stop pre-zeroing counter

ResetBudgetJob's batched update_data path shipped the full key/user/team
model on each reset. Prisma rejects object_permission_id and budget_limits
on the update input type, so any row carrying those fields detonated the
entire batch -- spend never reset, budget_reset_at never advanced. After
v1.84.0 started populating object_permission_id on UI-created keys, this
fires routinely.

_reset_budget_common also zeroed the cross-pod spend counter before the
DB write, so failed resets left enforcement reading 0 from the counter
while the DB still held the over-budget spend, admitting requests past
the cap until the counter naturally re-saturated from new reservations.

Switch the write to per-row narrow updates ({spend, budget_reset_at})
via db.batch_, and move the counter invalidation out of
_reset_budget_common so it only fires after the DB write commits. On
DB-write failure the counter is left untouched, enforcement continues
to block, and the next scheduler tick can retry without leaving a
bypass window.

Fixes #27730.

* fix(reset_budget): address Greptile review on #29358

- Strengthen the bypass-half regression test: replace the for-loop over
  call_args_list (vacuously true when empty) with assert_not_called(),
  so the test would actually flag a re-introduction of counter-zeroing
  via any code path.
- Add the same explanatory docstring on _write_user_reset_updates and
  _write_team_reset_updates that _write_key_reset_updates already has,
  so all three helpers point future maintainers at #27730.

* test(reset_budget): update test_proxy_budget_reset for new batch-write path

Same shape as the previous test_reset_budget_job.py update: keys/users/teams
now write through prisma.db.batch_().<table>.update, not update_data, so the
tests need a batcher mock and updated assertions. Adds:

- _wire_batcher_for_test helper that returns a list which accumulates per-row
  batch updates captured from prisma_client.db.batch_().
- _attrify helper that wraps dict fixtures so getattr(item, "token") works
  alongside the dict item-access the fake_reset_* mocks rely on. The new
  narrow-write helpers use getattr to pull out the row's id, and would
  silently skip plain dicts otherwise.
- Updates 3 partial_failure tests to assert against the batch-call list
  (rows by id, payload contains only {spend, budget_reset_at}) instead of
  update_data.assert_awaited_once + data_list inspection.
- Updates test_reset_budget_continues_other_categories_on_failure: only
  budget + enduser still flow through update_data; key/user/team go through
  the batch path now.
- Wires the batcher mock into 3 service_logger_*_success tests so commit()
  is actually awaitable and the success hook fires.

These tests were silently passing locally only because the editable install
in .venv pointed at the main repo, not the worktree — running pytest with
PYTHONPATH overridden to the worktree (matching CI) reproduces the failures.
2026-05-30 18:47:47 -07:00
..
_experimental/mcp_server Cherry-pick #29311 and #29343 onto patch/v1.84.3 (#29352) 2026-05-30 17:52:11 -07:00
agent_endpoints style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
anthropic_endpoints test: isolate proxy master_key/prisma_client module globals between tests 2026-04-23 15:31:16 -07:00
auth Merge remote-tracking branch 'origin/litellm_1.84.0rc2' into backport/27878-litellm_1.84.0rc2 2026-05-13 21:39:17 -07:00
client [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
common_utils Cherry-pick #29358 onto patch/v1.84.3 (#29363) 2026-05-30 18:47:47 -07:00
db [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
discovery_endpoints style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
experimental/mcp_server
google_endpoints run pre_call_hook on Google generateContent endpoints 2026-04-30 16:43:42 -07:00
guardrails Cherry-pick #29311 and #29343 onto patch/v1.84.3 (#29352) 2026-05-30 17:52:11 -07:00
health_endpoints fix(proxy): expose db status on public /health/readiness 2026-05-13 13:18:54 -07:00
hooks [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
image_endpoints style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
management_endpoints fix: harden /key/update authorization checks (#27878) 2026-05-13 21:33:14 -07:00
management_helpers feat(proxy): move search tool access to object permissions 2026-04-29 12:29:20 +05:30
memory Litellm memory improvements v2 (#26541) 2026-04-25 19:03:43 -07:00
middleware fix(proxy): point /metrics 401 at the opt-out flag 2026-05-08 18:42:14 -07:00
openai_files_endpoint Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_yj_apr17 2026-04-17 17:36:40 -07:00
pass_through_endpoints Merge pull request #26827 from stuxf/fix/passthrough-auth-default 2026-04-30 17:06:37 -07:00
policy_engine style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
prompts style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
public_endpoints [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
rag_endpoints Merge remote-tracking branch 'origin/litellm_1.84.0rc2' into backport/27878-litellm_1.84.0rc2 2026-05-13 21:39:17 -07:00
realtime_endpoints test: isolate proxy master_key/prisma_client module globals between tests 2026-04-23 15:31:16 -07:00
response_api_endpoints style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
spend_tracking [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
test_configs
types_utils Merge pull request #27801 from stuxf/chore/get-instance-fn-runtime-s3-gate 2026-05-13 20:57:24 -07:00
ui_crud_endpoints [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
vector_store_endpoints [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
__init__.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
conftest.py test: isolate proxy master_key/prisma_client module globals between tests 2026-04-23 15:31:16 -07:00
test_aiohttp_cleanup_closed.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_aiohttp_session_recovery.py fix: use AsyncMock for concurrent test consistency 2026-03-18 00:54:23 +00:00
test_api_key_masking_in_errors.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_audio_speech_prometheus_hooks.py fix req changes 2026-02-28 21:32:57 +05:30
test_batch_expiry.py fix(proxy): improve team expiry enforcement validation 2026-03-03 17:29:39 -08:00
test_batch_metadata_none_fix.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_batch_retrieve_bedrock.py fix(proxy/batches): forward model to retrieve_batch for bedrock 2026-04-29 22:48:03 +02:00
test_budget_reservation.py fix(proxy): gate image-gen reservation strictly on model mode 2026-05-09 10:29:55 -07:00
test_caching_routes.py
test_chat_completion_metadata.py fix: propagate JWT auth metadata to OTEL spans (#19627) 2026-01-23 21:21:23 -08:00
test_common_request_processing.py feat(proxy): LiteLLM headers on Google native generateContent routes (#25500) 2026-04-29 12:34:14 -07:00
test_cors_config.py refactor: extract _get_cors_config() for testability, fix no-op CORS tests 2026-04-11 22:24:04 +05:30
test_custom_proxy.py
test_empty_model_list.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_enforce_user_param.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_fallback_management_endpoints.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_fastapi_offline_routes.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_filter_models_by_team_access_group.py [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
test_health_check_functions.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_health_check_max_tokens.py [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
test_langfuse_passthrough_security.py [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
test_lazy_openapi_snapshot.py [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
test_litellm_pre_call_utils.py fix(proxy): always merge caller-supplied tags into request metadata 2026-05-12 16:34:42 -07:00
test_max_budget_env_var.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_model_dump_with_preserved_fields.py Fix_mapped tests part 2 2026-02-26 12:43:39 +05:30
test_model_id_header_propagation.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_model_info_default_limits.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_model_level_guardrails.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_openapi_schema_validation.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_pricing_field_strip.py [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
test_prometheus_cleanup.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_provider_url_destination_guard.py [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
test_proxy_cli.py feat(proxy): add --timeout_worker_healthcheck flag for uvicorn worker triage 2026-04-27 11:06:56 -07:00
test_proxy_server.py fix(spend_counter): seed Redis counter via SET NX to prevent cross-pod double-seed (#27854) 2026-05-20 18:42:35 -07:00
test_proxy_types.py Cherry-pick #29311 and #29343 onto patch/v1.84.3 (#29352) 2026-05-30 17:52:11 -07:00
test_proxy_utils.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_pyroscope.py [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
test_redis_auth_cache_flag.py [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
test_response_model_sanitization.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_route_a2a_models.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_route_llm_request.py [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
test_sensitive_route_auth.py [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
test_shared_health_check.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_spend_log_cleanup.py fix: harden CORS, create_views exception handling, and spend log cleanup loop 2026-04-11 18:58:04 +05:30
test_swagger_chat_completions.py [Infra] Promote internal staging to main (#27245) 2026-05-05 16:15:03 -07:00
test_team_member_update.py
test_team_org_move.py fix(team_endpoints): auto-add SSO team members to org on move (proxy admin only) (#26377) 2026-04-24 08:36:25 -07:00
test_tools_allowlist_enforcement.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_update_llm_router_resilience.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_utils.py add NO_OPENAPI env var to disable /openapi.json endpoint (#25547) 2026-04-14 23:37:49 +05:30