diff --git a/litellm/proxy/management_endpoints/team_endpoints.py b/litellm/proxy/management_endpoints/team_endpoints.py index 11e83150d8c..1cec93f857b 100644 --- a/litellm/proxy/management_endpoints/team_endpoints.py +++ b/litellm/proxy/management_endpoints/team_endpoints.py @@ -650,7 +650,7 @@ class TeamMemberBudgetHandler: @staticmethod @with_service_target("team_member_budgets") - async def _evict_default_budget_cache( + async def evict_default_budget_cache( budget_id: str, user_api_key_cache: UserApiKeyCache | None, ) -> None: @@ -712,7 +712,7 @@ class TeamMemberBudgetHandler: budget_obj=budget_request, user_api_key_dict=user_api_key_dict, ) - await TeamMemberBudgetHandler._evict_default_budget_cache( + await TeamMemberBudgetHandler.evict_default_budget_cache( budget_id=team_member_budget_table.budget_id, user_api_key_cache=user_api_key_cache, ) @@ -2627,10 +2627,10 @@ async def update_team( verbose_proxy_logger.info("Successfully updated team - %s, info", team_row.team_id) if default_budget_write is not None: - await TeamMemberBudgetHandler._evict_default_budget_cache( + await TeamMemberBudgetHandler.evict_default_budget_cache( budget_id=default_budget_write.budget_id, user_api_key_cache=user_api_key_cache ) - await _evict_team_member_cache_keys( + await evict_team_member_cache_keys( team_id=team_row.team_id, member_user_ids=team_write.changed_member_user_ids(), cache_keys=tuple(team_membership_cache_keys(team_row.team_id, team_write.changed_member_user_ids())), @@ -2650,14 +2650,14 @@ async def update_team( if is_audit_logging_enabled(): await _create_team_update_audit_log( existing_team_row=existing_team_row, - updated_kv=updated_kv | team_write.backfill_audit(), + updated_kv={**updated_kv, **team_write.backfill_audit()}, team_id=data.team_id, litellm_changed_by=litellm_changed_by, user_api_key_dict=user_api_key_dict, litellm_proxy_admin_name=litellm_proxy_admin_name, ) - return {"team_id": team_row.team_id, "data": team_row} | team_write.backfill_response() + return {"team_id": team_row.team_id, "data": team_row, **team_write.backfill_response()} except Exception as e: raise handle_exception_on_proxy(e) @@ -4839,7 +4839,7 @@ async def _invalidate_deleted_team_member_cache( user_api_key_cache: UserApiKeyCache, ) -> None: for team_id, member_user_ids in member_ids_per_team: - await _evict_team_member_cache_keys( + await evict_team_member_cache_keys( team_id=team_id, member_user_ids=member_user_ids, cache_keys=tuple(member_cache_keys(team_id, member_user_ids)), @@ -4847,7 +4847,7 @@ async def _invalidate_deleted_team_member_cache( ) -async def _evict_team_member_cache_keys( +async def evict_team_member_cache_keys( team_id: str, member_user_ids: Sequence[str], cache_keys: Sequence[str], diff --git a/litellm/proxy/management_helpers/team_member_default_budget.py b/litellm/proxy/management_helpers/team_member_default_budget.py index a8d2721ae38..f49a38eb198 100644 --- a/litellm/proxy/management_helpers/team_member_default_budget.py +++ b/litellm/proxy/management_helpers/team_member_default_budget.py @@ -1,10 +1,9 @@ -import json import uuid from collections.abc import Mapping, Sequence from dataclasses import dataclass from datetime import datetime from types import MappingProxyType -from typing import TYPE_CHECKING, Final, assert_never +from typing import TYPE_CHECKING, Final from pydantic import BaseModel, ConfigDict, JsonValue, TypeAdapter, field_validator @@ -20,7 +19,6 @@ from litellm.proxy.management_endpoints.common_utils import ( has_meaningful_budget_limit, upsert_budget_and_membership, ) -from litellm.proxy.utils import jsonify_object if TYPE_CHECKING: from prisma import Prisma @@ -59,19 +57,24 @@ _SHARED_BUDGET_IDS_SQL: Final = """ SELECT budget_id FROM "LiteLLM_TeamMembership" WHERE budget_id = ANY($1::text[]) GROUP BY budget_id HAVING COUNT(*) > 1 """ -_CANDIDATES_BY_SCHEDULE_SQL: Final = """ -SELECT b.budget_duration AS duration, COUNT(*)::int AS members -FROM "LiteLLM_TeamMembership" m JOIN "LiteLLM_BudgetTable" b ON b.budget_id = m.budget_id -WHERE m.team_id = $1 AND m.budget_id <> $2 AND {direction} -GROUP BY b.budget_duration -""" -_CANDIDATES_SQL: Final = """ -SELECT m.user_id, b.budget_id, b.soft_budget, b.max_parallel_requests, b.tpm_limit, b.rpm_limit, - b.model_max_budget, b.budget_duration, b.allowed_models, b.temp_budget_increase, b.temp_budget_expiry -FROM "LiteLLM_TeamMembership" m JOIN "LiteLLM_BudgetTable" b ON b.budget_id = m.budget_id -WHERE m.team_id = $1 AND m.budget_id <> $2 AND {direction} - AND (b.budget_duration = ANY($4::text[]) OR ($5::boolean AND b.budget_duration IS NULL)) -""" +_ON_CHOSEN_SIDE_OF_DEFAULT: Final = """m.team_id = $1 AND m.budget_id <> $2 AND CASE $3::text + WHEN 'raise_to_default' THEN b.max_budget < $4::double precision + WHEN 'lower_to_default' THEN b.max_budget > $4::double precision + WHEN 'all_to_default' THEN b.max_budget <> $4::double precision + ELSE FALSE END""" +_CANDIDATES_BY_SCHEDULE_SQL: Final = ( + 'SELECT b.budget_duration AS duration, COUNT(*)::int AS members FROM "LiteLLM_TeamMembership" m ' + 'JOIN "LiteLLM_BudgetTable" b ON b.budget_id = m.budget_id WHERE ' + + _ON_CHOSEN_SIDE_OF_DEFAULT + + " GROUP BY b.budget_duration" +) +_CANDIDATES_SQL: Final = ( + "SELECT m.user_id, b.budget_id, b.soft_budget, b.max_parallel_requests, b.tpm_limit, b.rpm_limit, " + "b.model_max_budget, b.budget_duration, b.allowed_models, b.temp_budget_increase, b.temp_budget_expiry " + 'FROM "LiteLLM_TeamMembership" m JOIN "LiteLLM_BudgetTable" b ON b.budget_id = m.budget_id WHERE ' + + _ON_CHOSEN_SIDE_OF_DEFAULT + + " AND (b.budget_duration = ANY($5::text[]) OR ($6::boolean AND b.budget_duration IS NULL))" +) @dataclass(frozen=True, slots=True) @@ -121,12 +124,12 @@ class _BudgetLimits(BaseModel): @field_validator("model_max_budget", mode="before") @classmethod def _decode_json_column(cls, value: object) -> object: - return json.loads(value) if isinstance(value, str) else value + return _JSON_VALUE.validate_json(value) if isinstance(value, str) else value def has_own_limits_beyond(self, default: "_BudgetLimits") -> bool: - return has_meaningful_budget_limit( - {field: value for field, value in self.model_dump().items() if value != getattr(default, field)} - ) + own: Final = _JSON_OBJECT.validate_python(self.model_dump(mode="json")) + inherited: Final = _JSON_OBJECT.validate_python(default.model_dump(mode="json")) + return has_meaningful_budget_limit({field: value for field, value in own.items() if value != inherited[field]}) class _MemberBudgetRef(BaseModel): @@ -148,6 +151,8 @@ class _BudgetIdRow(BaseModel): budget_id: str +_JSON_VALUE: Final[TypeAdapter[JsonValue]] = TypeAdapter(JsonValue) +_JSON_OBJECT: Final = TypeAdapter(dict[str, JsonValue]) _SCHEDULE_COUNTS: Final = TypeAdapter(tuple[_ScheduleCount, ...]) _MEMBER_BUDGET_REFS: Final = TypeAdapter(tuple[_MemberBudgetRef, ...]) _MEMBER_BUDGET_LIMITS: Final = TypeAdapter(tuple[_BudgetLimits, ...]) @@ -160,12 +165,14 @@ def plan_team_member_default_budget_write( team_alias: str | None, team_budget_duration: str | None, ) -> TeamMemberDefaultBudgetWrite | None: - requested: Final = data.model_dump(include=frozenset(_DEFAULT_BUDGET_COLUMNS) & data.model_fields_set) + requested: Final = _JSON_OBJECT.validate_python( + data.model_dump(mode="json", include=data.model_fields_set & _DEFAULT_BUDGET_COLUMNS.keys()) + ) sent: Final = MappingProxyType({_DEFAULT_BUDGET_COLUMNS[field]: value for field, value in requested.items()}) links_members: Final = any(value is not None for value in sent.values()) if not sent or (existing_budget_id is None and not links_members): return None - create_duration: Final = sent["budget_duration"] if "budget_duration" in sent else team_budget_duration + create_duration: Final = sent.get("budget_duration", team_budget_duration) created: Final = {**sent, "budget_duration": create_duration} return TeamMemberDefaultBudgetWrite( budget_id=existing_budget_id or _new_default_budget_id(team_alias), @@ -190,10 +197,10 @@ async def write_team_member_default_budget( await _budget_tx_db(tx).upsert( where={"budget_id": write.budget_id}, data={ - "create": jsonify_object( + "create": _prisma_columns( {**write.create, **stamps, "budget_id": write.budget_id, "created_by": changed_by} ), - "update": jsonify_object({**write.update, **stamps}), + "update": _prisma_columns({**write.update, **stamps}), }, ) if not write.links_members: @@ -213,17 +220,14 @@ async def move_member_budgets_to_default( """Members compare on their permanent ``max_budget``, and only those resetting on the default's schedule move. A member whose other limits all match the default is linked to the default row, while one holding a limit of its own keeps its row with ``max_budget`` cleared.""" - direction: Final = _direction_sql(backfill) - if direction is None: + if backfill == "none": return MovedMemberBudgets(user_ids=(), skipped_other_reset_period=0) default: Final = await _budget_tx_db(tx).find_unique(where={"budget_id": default_budget_id}) if default is None or default.max_budget is None: return MovedMemberBudgets(user_ids=(), skipped_other_reset_period=0) schedule: Final = budget_reset_period_key(default.budget_duration) counts: Final = _SCHEDULE_COUNTS.validate_python( - await tx.query_raw( - _CANDIDATES_BY_SCHEDULE_SQL.format(direction=direction), team_id, default_budget_id, default.max_budget - ) + await tx.query_raw(_CANDIDATES_BY_SCHEDULE_SQL, team_id, default_budget_id, backfill, default.max_budget) ) matching: Final = tuple(count for count in counts if budget_reset_period_key(count.duration) == schedule) moving: Final = sum(count.members for count in matching) @@ -233,9 +237,10 @@ async def move_member_budgets_to_default( if moving == 0: return MovedMemberBudgets(user_ids=(), skipped_other_reset_period=skipped) rows: Final = await tx.query_raw( - _CANDIDATES_SQL.format(direction=direction), + _CANDIDATES_SQL, team_id, default_budget_id, + backfill, default.max_budget, [count.duration for count in matching if count.duration is not None], any(count.duration is None for count in matching), @@ -289,24 +294,16 @@ async def _move_to_default( ) +def _prisma_columns(columns: Mapping[str, object]) -> Mapping[str, object]: + from prisma import Json # noqa: PLC0415 # local import: prisma may be ungenerated at module load in some tools + + return {column: Json(value) if isinstance(value, dict) else value for column, value in columns.items()} + + def _budget_tx_db(tx: "Prisma") -> "TableActions[prisma_models.LiteLLM_BudgetTable]": return tx.litellm_budgettable # pyright: ignore[reportReturnType] # TableActions widens the generated inputs to Mapping, as the repositories do -def _direction_sql(backfill: TeamMemberBudgetBackfill) -> str | None: - match backfill: - case "none": - return None - case "raise_to_default": - return "b.max_budget < $3::double precision" - case "lower_to_default": - return "b.max_budget > $3::double precision" - case "all_to_default": - return "b.max_budget <> $3::double precision" - case _: - assert_never(backfill) - - def _with_reset_at(columns: Mapping[str, object]) -> Mapping[str, object]: if "budget_duration" not in columns: return MappingProxyType(dict(columns)) diff --git a/tests/unit/proxy/management_endpoints/test_team_endpoints.py b/tests/unit/proxy/management_endpoints/test_team_endpoints.py index 9e3946daaec..f99e20a02a7 100644 --- a/tests/unit/proxy/management_endpoints/test_team_endpoints.py +++ b/tests/unit/proxy/management_endpoints/test_team_endpoints.py @@ -121,7 +121,9 @@ def _team_admin_may_edit(*fields: str): "litellm.proxy.management_endpoints.team_endpoints.PROXY_CONTROLLED_TEAM_METADATA_KEYS", PROXY_CONTROLLED_TEAM_METADATA_KEYS - frozenset(fields), ), - patch("litellm.proxy.proxy_server.general_settings", {"team_admin_editable_team_fields": list(fields)}), # test-quality-ok: update_team reads general_settings as a proxy_server module global + patch( + "litellm.proxy.proxy_server.general_settings", {"team_admin_editable_team_fields": list(fields)} + ), # test-quality-ok: update_team reads general_settings as a proxy_server module global ): yield @@ -277,9 +279,7 @@ mock_prisma_client.db.litellm_auditlog.create = AsyncMock() # Fixture to provide the mock prisma client @pytest.fixture(autouse=True) def mock_db_client(): - with patch( - "litellm.proxy.proxy_server.prisma_client", mock_prisma_client - ): # Mock in both places if necessary + with patch("litellm.proxy.proxy_server.prisma_client", mock_prisma_client): # Mock in both places if necessary yield mock_prisma_client mock_prisma_client.reset_mock() @@ -322,27 +322,17 @@ async def test_validate_team_org_change_same_org_id(): organization.organization_id = org_id organization.models = [] organization.litellm_budget_table = MagicMock() - organization.litellm_budget_table.max_budget = ( - 50.0 # This would normally fail validation - ) - organization.litellm_budget_table.tpm_limit = ( - 500 # This would normally fail validation - ) - organization.litellm_budget_table.rpm_limit = ( - 50 # This would normally fail validation - ) + organization.litellm_budget_table.max_budget = 50.0 # This would normally fail validation + organization.litellm_budget_table.tpm_limit = 500 # This would normally fail validation + organization.litellm_budget_table.rpm_limit = 50 # This would normally fail validation organization.members = [] # Mock Router mock_router = MagicMock(spec=Router) # Use patch to ensure the model access check is never called - with patch( - "litellm.proxy.management_endpoints.team_endpoints.can_org_access_model" - ) as mock_access_check: - result = validate_team_org_change( - team=team, organization=organization, llm_router=mock_router - ) + with patch("litellm.proxy.management_endpoints.team_endpoints.can_org_access_model") as mock_access_check: + result = validate_team_org_change(team=team, organization=organization, llm_router=mock_router) # Assert the function returns True without checking anything assert result is True @@ -359,9 +349,7 @@ async def test_validate_team_org_change_same_org_id(): ], ) @pytest.mark.asyncio -async def test_validate_team_org_change_zero_org_budget_is_enforced( - org_max_budget, team_max_budget, expect_blocked -): +async def test_validate_team_org_change_zero_org_budget_is_enforced(org_max_budget, team_max_budget, expect_blocked): """An organization with an explicit max_budget of 0 must still block moving in a team with a larger budget, matching key/team/user zero-budget semantics. @@ -443,9 +431,7 @@ async def test_validate_team_org_change_members_in_org(): mock_router = MagicMock(spec=Router) # Test should pass - all team members are in org members - result = validate_team_org_change( - team=team, organization=organization, llm_router=mock_router - ) + result = validate_team_org_change(team=team, organization=organization, llm_router=mock_router) assert result is True @@ -497,9 +483,7 @@ async def test_validate_team_org_change_member_not_in_org(): # Test should fail - user_id_not_in_org is not in org members with pytest.raises(HTTPException) as exc_info: - validate_team_org_change( - team=team, organization=organization, llm_router=mock_router - ) + validate_team_org_change(team=team, organization=organization, llm_router=mock_router) assert exc_info.value.status_code == 403 assert "not a member of the organization" in str(exc_info.value.detail) @@ -543,10 +527,7 @@ async def test_get_team_permissions_list_success(mock_db_client, mock_admin_auth assert response.status_code == 200 response_data = response.json() assert response_data["team_id"] == test_team_id - assert ( - response_data["team_member_permissions"] - == mock_team_data["team_member_permissions"] - ) + assert response_data["team_member_permissions"] == mock_team_data["team_member_permissions"] assert ( response_data["all_available_permissions"] == TeamMemberPermissionChecks.get_all_available_team_member_permissions() @@ -609,9 +590,7 @@ async def test_update_team_permissions_success(mock_db_client, mock_admin_auth): return_value=mock_existing_team_row, ): # Mock the database update function - mock_db_client.db.litellm_teamtable.update = AsyncMock( - return_value=mock_updated_team_row - ) + mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=mock_updated_team_row) # Override the dependency for this test app.dependency_overrides[user_api_key_auth] = lambda: mock_admin_auth @@ -636,9 +615,7 @@ async def test_update_team_permissions_success(mock_db_client, mock_admin_auth): @pytest.mark.asyncio @pytest.mark.parametrize("field", ["budget_duration", "team_member_budget_duration"]) @pytest.mark.parametrize("bad_duration", ["0s", "-5m"]) -async def test_new_team_rejects_a_duration_that_never_advances( - mock_db_client, mock_admin_auth, field, bad_duration -): +async def test_new_team_rejects_a_duration_that_never_advances(mock_db_client, mock_admin_auth, field, bad_duration): """A zero-length window resets to "now", so the team row is due again the moment it is written. The reset job re-reads such rows on every tick, and a tenant with enough of them fills each batch and starves other tenants. @@ -668,9 +645,7 @@ async def test_new_team_rejects_a_duration_that_never_advances( @pytest.mark.asyncio @pytest.mark.parametrize("field", ["budget_duration", "team_member_budget_duration"]) -async def test_update_team_rejects_a_duration_that_never_advances( - mock_db_client, mock_admin_auth, field -): +async def test_update_team_rejects_a_duration_that_never_advances(mock_db_client, mock_admin_auth, field): """/team/update must reject the same never-advancing durations /team/new does.""" from fastapi import Request @@ -709,17 +684,13 @@ async def test_new_team_with_object_permission(mock_db_client, mock_admin_auth): mock_db_client.db = MagicMock() # Mock object permission table creation - mock_object_perm_create = AsyncMock( - return_value=MagicMock(object_permission_id="objperm123") - ) + mock_object_perm_create = AsyncMock(return_value=MagicMock(object_permission_id="objperm123")) mock_db_client.db.litellm_objectpermissiontable = MagicMock() mock_db_client.db.litellm_objectpermissiontable.create = mock_object_perm_create # Mock model table creation mock_db_client.db.litellm_modeltable = MagicMock() - mock_db_client.db.litellm_modeltable.create = AsyncMock( - return_value=MagicMock(id="model123") - ) + mock_db_client.db.litellm_modeltable.create = AsyncMock(return_value=MagicMock(id="model123")) # Capture team table creation team_create_result = MagicMock( @@ -736,9 +707,7 @@ async def test_new_team_with_object_permission(mock_db_client, mock_admin_auth): mock_db_client.db.litellm_teamtable.create = mock_team_create _wire_team_create_tx(mock_db_client) mock_db_client.db.litellm_teamtable.count = mock_team_count - mock_db_client.db.litellm_teamtable.update = AsyncMock( - return_value=team_create_result - ) + mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=team_create_result) # Mock user table mock_db_client.db.litellm_usertable = MagicMock() @@ -844,9 +813,7 @@ async def test_new_team_with_mcp_tool_permissions(mock_db_client, mock_admin_aut # Mock model table mock_db_client.db.litellm_modeltable = MagicMock() - mock_db_client.db.litellm_modeltable.create = AsyncMock( - return_value=MagicMock(id="model456") - ) + mock_db_client.db.litellm_modeltable.create = AsyncMock(return_value=MagicMock(id="model456")) # Mock team table team_create_result = MagicMock( @@ -858,14 +825,10 @@ async def test_new_team_with_mcp_tool_permissions(mock_db_client, mock_admin_aut "object_permission_id": "objperm_team_mcp_456", } mock_db_client.db.litellm_teamtable = MagicMock() - mock_db_client.db.litellm_teamtable.create = AsyncMock( - return_value=team_create_result - ) + mock_db_client.db.litellm_teamtable.create = AsyncMock(return_value=team_create_result) _wire_team_create_tx(mock_db_client) mock_db_client.db.litellm_teamtable.count = AsyncMock(return_value=0) - mock_db_client.db.litellm_teamtable.update = AsyncMock( - return_value=team_create_result - ) + mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=team_create_result) # Mock user table mock_db_client.db.litellm_usertable = MagicMock() @@ -924,20 +887,14 @@ def test_should_auto_add_team_creator(user_role, user_id, flag_value, expected): _should_auto_add_team_creator, ) - general_settings = ( - {} if flag_value is None else {"disable_auto_add_proxy_admin_to_teams": flag_value} - ) + general_settings = {} if flag_value is None else {"disable_auto_add_proxy_admin_to_teams": flag_value} auth = UserAPIKeyAuth(user_role=user_role, user_id=user_id) assert _should_auto_add_team_creator(auth, general_settings) is expected @pytest.mark.asyncio -@pytest.mark.parametrize( - "disable_flag,expect_creator_added", [(True, False), (False, True)] -) -async def test_new_team_disable_auto_add_proxy_admin_flag( - mock_db_client, disable_flag, expect_creator_added -): +@pytest.mark.parametrize("disable_flag,expect_creator_added", [(True, False), (False, True)]) +async def test_new_team_disable_auto_add_proxy_admin_flag(mock_db_client, disable_flag, expect_creator_added): """ When general_settings.disable_auto_add_proxy_admin_to_teams is True, a proxy admin calling /team/new must NOT be auto-added to the team's members. When @@ -952,9 +909,7 @@ async def test_new_team_disable_auto_add_proxy_admin_flag( team_create_result = MagicMock(team_id="team-789") team_create_result.model_dump.return_value = {"team_id": "team-789"} mock_db_client.db.litellm_teamtable = MagicMock() - mock_db_client.db.litellm_teamtable.create = AsyncMock( - return_value=team_create_result - ) + mock_db_client.db.litellm_teamtable.create = AsyncMock(return_value=team_create_result) _wire_team_create_tx(mock_db_client) mock_db_client.db.litellm_teamtable.count = AsyncMock(return_value=0) mock_db_client.db.litellm_usertable = MagicMock() @@ -965,17 +920,18 @@ async def test_new_team_disable_auto_add_proxy_admin_flag( from litellm.proxy._types import NewTeamRequest from litellm.proxy.management_endpoints.team_endpoints import new_team - admin_auth = UserAPIKeyAuth( - user_role=LitellmUserRoles.PROXY_ADMIN, user_id="admin-user-1" - ) + admin_auth = UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN, user_id="admin-user-1") - with patch( - "litellm.proxy.proxy_server.general_settings", - {"disable_auto_add_proxy_admin_to_teams": disable_flag}, - ), patch( - "litellm.proxy.management_endpoints.team_endpoints._add_team_members_to_team", - new_callable=AsyncMock, - ) as mock_add_members: + with ( + patch( + "litellm.proxy.proxy_server.general_settings", + {"disable_auto_add_proxy_admin_to_teams": disable_flag}, + ), + patch( + "litellm.proxy.management_endpoints.team_endpoints._add_team_members_to_team", + new_callable=AsyncMock, + ) as mock_add_members, + ): await new_team( data=NewTeamRequest(team_alias="flag-test-team"), http_request=MagicMock(spec=Request), @@ -1024,16 +980,12 @@ async def test_team_update_object_permissions_existing_permission(monkeypatch): "vector_stores": ["old_store_1", "old_store_2"], } - mock_prisma_client.db.litellm_objectpermissiontable.find_unique = AsyncMock( - return_value=existing_object_permission - ) + mock_prisma_client.db.litellm_objectpermissiontable.find_unique = AsyncMock(return_value=existing_object_permission) # Mock upsert operation updated_permission = MagicMock() updated_permission.object_permission_id = "existing_perm_id_123" - mock_prisma_client.db.litellm_objectpermissiontable.upsert = AsyncMock( - return_value=updated_permission - ) + mock_prisma_client.db.litellm_objectpermissiontable.upsert = AsyncMock(return_value=updated_permission) # Test data with new object permission data_json = { @@ -1089,21 +1041,17 @@ async def test_team_update_object_permissions_no_existing_permission(monkeypatch ) # Mock find_unique to return None (no existing permission) - mock_prisma_client.db.litellm_objectpermissiontable.find_unique = AsyncMock( - return_value=None - ) + mock_prisma_client.db.litellm_objectpermissiontable.find_unique = AsyncMock(return_value=None) # Mock upsert to create new record new_permission = MagicMock() new_permission.object_permission_id = "new_perm_id_456" - mock_prisma_client.db.litellm_objectpermissiontable.upsert = AsyncMock( - return_value=new_permission - ) + mock_prisma_client.db.litellm_objectpermissiontable.upsert = AsyncMock(return_value=new_permission) data_json = { - "object_permission": LiteLLM_ObjectPermissionBase( - vector_stores=["brand_new_store"] - ).model_dump(exclude_unset=True, exclude_none=True), + "object_permission": LiteLLM_ObjectPermissionBase(vector_stores=["brand_new_store"]).model_dump( + exclude_unset=True, exclude_none=True + ), "team_alias": "updated_team_2", } @@ -1149,21 +1097,17 @@ async def test_team_update_object_permissions_missing_permission_record(monkeypa ) # Mock find_unique to return None (permission record not found) - mock_prisma_client.db.litellm_objectpermissiontable.find_unique = AsyncMock( - return_value=None - ) + mock_prisma_client.db.litellm_objectpermissiontable.find_unique = AsyncMock(return_value=None) # Mock upsert to create new record new_permission = MagicMock() new_permission.object_permission_id = "recreated_perm_id_789" - mock_prisma_client.db.litellm_objectpermissiontable.upsert = AsyncMock( - return_value=new_permission - ) + mock_prisma_client.db.litellm_objectpermissiontable.upsert = AsyncMock(return_value=new_permission) data_json = { - "object_permission": LiteLLM_ObjectPermissionBase( - vector_stores=["recreated_store"] - ).model_dump(exclude_unset=True, exclude_none=True), + "object_permission": LiteLLM_ObjectPermissionBase(vector_stores=["recreated_store"]).model_dump( + exclude_unset=True, exclude_none=True + ), "team_alias": "updated_team_3", } @@ -1269,14 +1213,10 @@ async def test_add_team_member_budget_table_success(): mock_budget_record.budget_id = "budget-123" mock_budget_record.max_budget = 1000.0 - mock_prisma_client.db.litellm_budgettable.find_unique = AsyncMock( - return_value=mock_budget_record - ) + mock_prisma_client.db.litellm_budgettable.find_unique = AsyncMock(return_value=mock_budget_record) # Create team info response object - team_info_response = TeamInfoResponseObjectTeamTable( - team_id="test-team-123", team_alias="Test Team" - ) + team_info_response = TeamInfoResponseObjectTeamTable(team_id="test-team-123", team_alias="Test Team") # Call the function result = await _add_team_member_budget_table( @@ -1287,15 +1227,11 @@ async def test_add_team_member_budget_table_success(): # Verify the result assert result.team_member_budget_table == mock_budget_record - assert result == team_info_response.model_copy( - update={"team_member_budget_table": mock_budget_record} - ) + assert result == team_info_response.model_copy(update={"team_member_budget_table": mock_budget_record}) assert team_info_response.team_member_budget_table is None # Verify database call was made correctly - mock_prisma_client.db.litellm_budgettable.find_unique.assert_called_once_with( - where={"budget_id": "budget-123"} - ) + mock_prisma_client.db.litellm_budgettable.find_unique.assert_called_once_with(where={"budget_id": "budget-123"}) @pytest.mark.asyncio @@ -1315,14 +1251,10 @@ async def test_add_team_member_budget_table_exception_handling(): ) # Create team info response object - team_info_response = TeamInfoResponseObjectTeamTable( - team_id="test-team-456", team_alias="Test Team 2" - ) + team_info_response = TeamInfoResponseObjectTeamTable(team_id="test-team-456", team_alias="Test Team 2") # Mock the verbose_proxy_logger to capture log calls - with patch( - "litellm.proxy.management_endpoints.team_endpoints.verbose_proxy_logger" - ) as mock_logger: + with patch("litellm.proxy.management_endpoints.team_endpoints.verbose_proxy_logger") as mock_logger: # Call the function result = await _add_team_member_budget_table( team_member_budget_id="nonexistent-budget-456", @@ -1334,10 +1266,7 @@ async def test_add_team_member_budget_table_exception_handling(): assert result == team_info_response # Verify team_member_budget_table is not set when exception occurs - assert ( - not hasattr(result, "team_member_budget_table") - or result.team_member_budget_table is None - ) + assert not hasattr(result, "team_member_budget_table") or result.team_member_budget_table is None # Verify the error was logged mock_logger.info.assert_called_once_with( @@ -1366,9 +1295,7 @@ async def test_add_team_member_budget_table_budget_not_found(): mock_prisma_client.db.litellm_budgettable.find_unique = AsyncMock(return_value=None) # Create team info response object - team_info_response = TeamInfoResponseObjectTeamTable( - team_id="test-team-789", team_alias="Test Team 3" - ) + team_info_response = TeamInfoResponseObjectTeamTable(team_id="test-team-789", team_alias="Test Team 3") # Call the function result = await _add_team_member_budget_table( @@ -1569,9 +1496,7 @@ async def test_available_team_self_join_blocks_other_user_id(): await _validate_team_member_add_permissions( user_api_key_dict=user, complete_team_data=team, - data=_make_team_member_add_request( - member_user_id="bob-victim", role="user" - ), + data=_make_team_member_add_request(member_user_id="bob-victim", role="user"), ) assert exc_info.value.status_code == 403 @@ -1988,9 +1913,7 @@ async def test_update_team_members_list_duplicate_prevention(): # Create mock team with existing members mock_team = MagicMock(spec=LiteLLM_TeamTable) - mock_team.members_with_roles = [ - Member(user_id="existing-user", user_email="existing@example.com", role="admin") - ] + mock_team.members_with_roles = [Member(user_id="existing-user", user_email="existing@example.com", role="admin")] # Try to add the same member again duplicate_member = Member(user_id="existing-user", role="user") @@ -2121,9 +2044,7 @@ async def test_add_team_members_runs_member_writes_on_the_lock_holding_transacti tx = MagicMock() tx.query_raw = AsyncMock(return_value=[{"members_with_roles": []}]) - tx.litellm_teamtable.update = AsyncMock( - return_value=LiteLLM_TeamTable(team_id="team-pool", members_with_roles=[]) - ) + tx.litellm_teamtable.update = AsyncMock(return_value=LiteLLM_TeamTable(team_id="team-pool", members_with_roles=[])) tx.litellm_usertable.upsert = AsyncMock(return_value=added_user) tx.litellm_usertable.update_many = AsyncMock() tx.litellm_budgettable.create = AsyncMock(return_value=created_budget) @@ -2178,9 +2099,7 @@ async def test_add_team_members_locks_every_named_user_row_in_id_order_before_th tx = MagicMock() tx.query_raw = AsyncMock(side_effect=query_raw) tx.litellm_usertable.find_many = AsyncMock(return_value=[email_match]) - tx.litellm_teamtable.update = AsyncMock( - return_value=LiteLLM_TeamTable(team_id="team-order", members_with_roles=[]) - ) + tx.litellm_teamtable.update = AsyncMock(return_value=LiteLLM_TeamTable(team_id="team-order", members_with_roles=[])) tx_cm = MagicMock() tx_cm.__aenter__ = AsyncMock(return_value=tx) tx_cm.__aexit__ = AsyncMock(return_value=None) @@ -2241,9 +2160,7 @@ async def test_add_team_members_skips_budget_and_membership_writes_for_members_a tx.query_raw = AsyncMock( return_value=[{"members_with_roles": [{"user_id": "alice", "user_email": None, "role": "user"}]}] ) - tx.litellm_teamtable.update = AsyncMock( - return_value=LiteLLM_TeamTable(team_id="team-mixed", members_with_roles=[]) - ) + tx.litellm_teamtable.update = AsyncMock(return_value=LiteLLM_TeamTable(team_id="team-mixed", members_with_roles=[])) tx.litellm_usertable.upsert = AsyncMock(return_value=added_user) tx.litellm_usertable.update_many = AsyncMock() tx.litellm_budgettable.create = AsyncMock(return_value=created_budget) @@ -2275,7 +2192,9 @@ async def test_add_team_members_skips_budget_and_membership_writes_for_members_a } assert [user.user_id for user in updated_users] == ["bob"] assert [tm.user_id for tm in updated_team_memberships] == ["bob"] - written_ids = [m["user_id"] for m in json.loads(tx.litellm_teamtable.update.call_args.kwargs["data"]["members_with_roles"])] + written_ids = [ + m["user_id"] for m in json.loads(tx.litellm_teamtable.update.call_args.kwargs["data"]["members_with_roles"]) + ] assert written_ids == ["alice", "bob"] @@ -2383,9 +2302,7 @@ async def test_team_model_add_delete_refresh_team_cache(endpoint_name): ) mock_request = Mock(spec=Request) - mock_user_api_key_dict = UserAPIKeyAuth( - user_role=LitellmUserRoles.PROXY_ADMIN, user_id="test_user_id" - ) + mock_user_api_key_dict = UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN, user_id="test_user_id") existing_team = MagicMock() existing_team.model_dump.return_value = { @@ -2422,12 +2339,8 @@ async def test_team_model_add_delete_refresh_team_cache(endpoint_name): new_callable=AsyncMock, ) as mock_cache_team, ): - mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock( - return_value=existing_team - ) - mock_prisma_client.db.litellm_teamtable.update = AsyncMock( - return_value=updated_team - ) + mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=existing_team) + mock_prisma_client.db.litellm_teamtable.update = AsyncMock(return_value=updated_team) mock_prisma_client.db.execute_raw = AsyncMock(return_value=None) if endpoint_name == "team_model_add": @@ -2469,15 +2382,11 @@ async def test_team_model_add_delete_refresh_team_cache(endpoint_name): # "no team-level restriction" and stop enforcing the team's # search-tool allowlist on key issuance. assert call_kwargs["team_table"].object_permission is not None - assert call_kwargs["team_table"].object_permission.search_tools == [ - "allowed-tool-A" - ] + assert call_kwargs["team_table"].object_permission.search_tools == ["allowed-tool-A"] # Pin the Prisma call shape too — the regression is in *what the # update returns*, so the contract that the update asks for # `object_permission` belongs in this test. - update_call_kwargs = ( - mock_prisma_client.db.litellm_teamtable.update.call_args.kwargs - ) + update_call_kwargs = mock_prisma_client.db.litellm_teamtable.update.call_args.kwargs assert update_call_kwargs.get("include", {}).get("object_permission") is True @@ -2796,9 +2705,7 @@ async def test_team_write_404s_when_row_vanishes_before_update(endpoint_name): ) mock_request = Mock(spec=Request) - mock_user_api_key_dict = UserAPIKeyAuth( - user_role=LitellmUserRoles.PROXY_ADMIN, user_id="test_user_id" - ) + mock_user_api_key_dict = UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN, user_id="test_user_id") existing_team = MagicMock() existing_team.team_id = "team-1234" @@ -2841,9 +2748,15 @@ async def test_team_write_404s_when_row_vanishes_before_update(endpoint_name): }[endpoint_name] with ( - patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma_client, # test-quality-ok: proxy_server module global is the endpoint's only injection point - patch("litellm.proxy.proxy_server.user_api_key_cache"), # test-quality-ok: proxy_server module global is the endpoint's only injection point - patch("litellm.proxy.proxy_server.proxy_logging_obj"), # test-quality-ok: proxy_server module global is the endpoint's only injection point + patch( + "litellm.proxy.proxy_server.prisma_client" + ) as mock_prisma_client, # test-quality-ok: proxy_server module global is the endpoint's only injection point + patch( + "litellm.proxy.proxy_server.user_api_key_cache" + ), # test-quality-ok: proxy_server module global is the endpoint's only injection point + patch( + "litellm.proxy.proxy_server.proxy_logging_obj" + ), # test-quality-ok: proxy_server module global is the endpoint's only injection point patch( # test-quality-ok: stubs the cache write so the test observes only the DB result handling "litellm.proxy.management_endpoints.team_endpoints.cache_team_object", new_callable=AsyncMock, @@ -2854,9 +2767,7 @@ async def test_team_write_404s_when_row_vanishes_before_update(endpoint_name): return_value=existing_team, ), ): - mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock( - return_value=existing_team - ) + mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=existing_team) mock_prisma_client.db.litellm_teamtable.update = AsyncMock(return_value=None) mock_prisma_client.db.execute_raw = AsyncMock(return_value=None) @@ -2867,8 +2778,6 @@ async def test_team_write_404s_when_row_vanishes_before_update(endpoint_name): assert exc_info.value.detail == {"error": "Team not found, passed team_id=team-1234"} - - def test_clean_team_member_fields(): """ Test that _clean_team_member_fields removes all team member fields from a dictionary. @@ -2929,9 +2838,7 @@ async def test_create_team_member_budget_table(): TeamMemberBudgetHandler, ) - mock_user_api_key_dict = UserAPIKeyAuth( - user_role=LitellmUserRoles.PROXY_ADMIN, user_id="test_user_id" - ) + mock_user_api_key_dict = UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN, user_id="test_user_id") data = NewTeamRequest( team_id="test_team_id", @@ -2998,9 +2905,7 @@ async def test_create_team_member_budget_table_without_team_alias(): TeamMemberBudgetHandler, ) - mock_user_api_key_dict = UserAPIKeyAuth( - user_role=LitellmUserRoles.PROXY_ADMIN, user_id="test_user_id" - ) + mock_user_api_key_dict = UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN, user_id="test_user_id") data = NewTeamRequest(team_id="test_team_id") new_team_data_json = { @@ -3032,12 +2937,6 @@ async def test_create_team_member_budget_table_without_team_alias(): assert budget_request.budget_id.startswith("team-budget-") - - - - - - @pytest.mark.asyncio async def test_create_team_member_budget_table_explicit_null_duration_does_not_inherit_team_duration( mock_db_client, @@ -3050,18 +2949,14 @@ async def test_create_team_member_budget_table_explicit_null_duration_does_not_i TeamMemberBudgetHandler, ) - mock_user_api_key_dict = UserAPIKeyAuth( - user_role=LitellmUserRoles.PROXY_ADMIN, user_id="test_user_id" - ) + mock_user_api_key_dict = UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN, user_id="test_user_id") team_table = MagicMock(spec=LiteLLM_TeamTable) team_table.metadata = {} team_table.team_alias = "Test Team" team_table.budget_duration = "30d" - mock_db_client.db.litellm_budgettable.create = AsyncMock( - side_effect=lambda data: SimpleNamespace(**data) - ) + mock_db_client.db.litellm_budgettable.create = AsyncMock(side_effect=lambda data: SimpleNamespace(**data)) result = await TeamMemberBudgetHandler.create_team_member_budget_table( data=team_table, @@ -3095,18 +2990,14 @@ async def test_create_team_member_budget_table_inherits_team_duration_when_durat TeamMemberBudgetHandler, ) - mock_user_api_key_dict = UserAPIKeyAuth( - user_role=LitellmUserRoles.PROXY_ADMIN, user_id="test_user_id" - ) + mock_user_api_key_dict = UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN, user_id="test_user_id") team_table = MagicMock(spec=LiteLLM_TeamTable) team_table.metadata = {} team_table.team_alias = "Test Team" team_table.budget_duration = "30d" - mock_db_client.db.litellm_budgettable.create = AsyncMock( - side_effect=lambda data: SimpleNamespace(**data) - ) + mock_db_client.db.litellm_budgettable.create = AsyncMock(side_effect=lambda data: SimpleNamespace(**data)) result = await TeamMemberBudgetHandler.create_team_member_budget_table( data=team_table, @@ -3122,16 +3013,6 @@ async def test_create_team_member_budget_table_inherits_team_duration_when_durat assert result["metadata"]["team_member_budget_id"] == written["budget_id"] - - - - - - - - - - @pytest.mark.asyncio async def test_bulk_team_member_add_success(): """ @@ -3273,9 +3154,7 @@ async def test_bulk_team_member_add_batch_size_limit(): from litellm.proxy.management_endpoints.team_endpoints import bulk_team_member_add # Create more than 500 members (the max batch size) - large_member_list = [ - Member(user_email=f"user{i}@example.com", role="user") for i in range(501) - ] + large_member_list = [Member(user_email=f"user{i}@example.com", role="user") for i in range(501)] bulk_request = BulkTeamMemberAddRequest( team_id="test-team-123", @@ -3330,9 +3209,7 @@ async def test_bulk_team_member_add_all_users_flag(): ) as mock_team_member_add, ): # Mock the database find_many call - mock_prisma.db.litellm_usertable.find_many = AsyncMock( - return_value=mock_db_users - ) + mock_prisma.db.litellm_usertable.find_many = AsyncMock(return_value=mock_db_users) mock_auth = UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN) @@ -3342,9 +3219,7 @@ async def test_bulk_team_member_add_all_users_flag(): ) # Verify that find_many was called to get all users - mock_prisma.db.litellm_usertable.find_many.assert_called_once_with( - order={"created_at": "desc"} - ) + mock_prisma.db.litellm_usertable.find_many.assert_called_once_with(order={"created_at": "desc"}) # Verify team_member_add was called with users from database mock_team_member_add.assert_called_once() @@ -3467,9 +3342,7 @@ async def test_list_team_v2_security_check_non_admin_user(): ) assert exc_info.value.status_code == 401 - assert "Only admin users can query all teams/other teams" in str( - exc_info.value.detail - ) + assert "Only admin users can query all teams/other teams" in str(exc_info.value.detail) assert LitellmUserRoles.INTERNAL_USER.value in str(exc_info.value.detail) @@ -3517,9 +3390,7 @@ async def test_list_team_v2_security_check_non_admin_user_other_user(): ) assert exc_info.value.status_code == 401 - assert "Only admin users can query all teams/other teams" in str( - exc_info.value.detail - ) + assert "Only admin users can query all teams/other teams" in str(exc_info.value.detail) @pytest.mark.asyncio @@ -3669,17 +3540,11 @@ async def test_list_team_v2_with_status_deleted(): mock_prisma_client.db = mock_db # Mock deleted teams - mock_deleted_team1 = Mock( - model_dump=lambda: {"team_id": "team_1", "team_alias": "Deleted Team 1"} - ) - mock_deleted_team2 = Mock( - model_dump=lambda: {"team_id": "team_2", "team_alias": "Deleted Team 2"} - ) + mock_deleted_team1 = Mock(model_dump=lambda: {"team_id": "team_1", "team_alias": "Deleted Team 1"}) + mock_deleted_team2 = Mock(model_dump=lambda: {"team_id": "team_2", "team_alias": "Deleted Team 2"}) # Mock deleted teams table (should be called) - mock_db.litellm_deletedteamtable.find_many = AsyncMock( - return_value=[mock_deleted_team1, mock_deleted_team2] - ) + mock_db.litellm_deletedteamtable.find_many = AsyncMock(return_value=[mock_deleted_team1, mock_deleted_team2]) mock_db.litellm_deletedteamtable.count = AsyncMock(return_value=2) # Mock regular teams table (should NOT be called) @@ -3759,7 +3624,9 @@ async def test_list_team_v2_includes_litellm_model_table(): }, ) - with patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma_client: # test-quality-ok: this file's DB-mock convention + with patch( + "litellm.proxy.proxy_server.prisma_client" + ) as mock_prisma_client: # test-quality-ok: this file's DB-mock convention mock_db = Mock() mock_prisma_client.db = mock_db @@ -3956,9 +3823,7 @@ async def test_list_team_v2_org_admin_own_user_id_sees_all_org_teams(): "organization_id": "org_A", "members_with_roles": [{"user_id": "other_user", "role": "user"}], } - mock_db.litellm_teamtable.find_many = AsyncMock( - return_value=[mock_team_1, mock_team_2] - ) + mock_db.litellm_teamtable.find_many = AsyncMock(return_value=[mock_team_1, mock_team_2]) mock_db.litellm_teamtable.count = AsyncMock(return_value=2) mock_db.litellm_verificationtoken.group_by = AsyncMock(return_value=[]) mock_db.litellm_usertable.find_unique = AsyncMock(return_value=mock_user) @@ -3984,9 +3849,7 @@ async def test_list_team_v2_org_admin_own_user_id_sees_all_org_teams(): # Verify the where clause scopes by org OR own membership — no # top-level team_id filter that would hide org teams they aren't in where = mock_db.litellm_teamtable.find_many.call_args.kwargs["where"] - assert where["AND"] == [ - {"OR": [{"organization_id": {"in": ["org_A"]}}, {"team_id": {"in": ["team_1"]}}]} - ] + assert where["AND"] == [{"OR": [{"organization_id": {"in": ["org_A"]}}, {"team_id": {"in": ["team_1"]}}]}] assert "team_id" not in where assert "organization_id" not in where @@ -4262,10 +4125,7 @@ async def test_list_team_v2_org_admin_cannot_view_other_orgs(): ) assert exc_info.value.status_code == 403 - assert ( - "only view teams within your organizations" - in str(exc_info.value.detail).lower() - ) + assert "only view teams within your organizations" in str(exc_info.value.detail).lower() @pytest.mark.asyncio @@ -4425,9 +4285,7 @@ async def test_list_team_v2_search_builds_or_clause(): from litellm.proxy.management_endpoints.team_endpoints import list_team_v2 mock_request = Mock(spec=Request) - mock_admin = UserAPIKeyAuth( - user_role=LitellmUserRoles.PROXY_ADMIN, user_id="admin_user" - ) + mock_admin = UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN, user_id="admin_user") with patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma_client: mock_db = Mock() @@ -4472,9 +4330,7 @@ async def test_list_team_v2_search_team_id_match_prefix(): from litellm.proxy.management_endpoints.team_endpoints import list_team_v2 mock_request = Mock(spec=Request) - mock_admin = UserAPIKeyAuth( - user_role=LitellmUserRoles.PROXY_ADMIN, user_id="admin_user" - ) + mock_admin = UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN, user_id="admin_user") with patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma_client: mock_db = Mock() @@ -4525,9 +4381,7 @@ async def test_list_team_v2_search_composes_with_user_id_filter(): from litellm.proxy.management_endpoints.team_endpoints import list_team_v2 mock_request = Mock(spec=Request) - mock_user_api_key_dict = UserAPIKeyAuth( - user_role=LitellmUserRoles.INTERNAL_USER, user_id="member_user" - ) + mock_user_api_key_dict = UserAPIKeyAuth(user_role=LitellmUserRoles.INTERNAL_USER, user_id="member_user") mock_user = LiteLLM_UserTable( user_id="member_user", @@ -4712,9 +4566,7 @@ async def test_list_team_v2_keys_count_skipped_for_deleted_status(): "team_alias": "Deleted Team", } - mock_db.litellm_deletedteamtable.find_many = AsyncMock( - return_value=[mock_deleted] - ) + mock_db.litellm_deletedteamtable.find_many = AsyncMock(return_value=[mock_deleted]) mock_db.litellm_deletedteamtable.count = AsyncMock(return_value=1) mock_db.litellm_verificationtoken.group_by = AsyncMock(return_value=[]) @@ -4747,9 +4599,7 @@ async def test_team_member_delete_cleans_membership(mock_db_client, mock_admin_a mock_team_row = MagicMock() mock_team_row.model_dump.return_value = { "team_id": test_team_id, - "members_with_roles": [ - {"user_id": test_user_id, "user_email": None, "role": "user"} - ], + "members_with_roles": [{"user_id": test_user_id, "user_email": None, "role": "user"}], "team_member_permissions": [], "metadata": {}, "models": [], @@ -4757,32 +4607,24 @@ async def test_team_member_delete_cleans_membership(mock_db_client, mock_admin_a } # Configure DB mocks used by team_member_delete - mock_db_client.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_team_row - ) + mock_db_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_team_row) mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=mock_team_row) # User row to allow removal from user's teams list mock_user_row = MagicMock() mock_user_row.user_id = test_user_id mock_user_row.teams = [test_team_id] - mock_db_client.db.litellm_usertable.find_many = AsyncMock( - return_value=[mock_user_row] - ) + mock_db_client.db.litellm_usertable.find_many = AsyncMock(return_value=[mock_user_row]) mock_db_client.db.litellm_usertable.update = AsyncMock(return_value=MagicMock()) # Membership deletion should be called mock_db_client.db.litellm_teammembership = MagicMock() - mock_db_client.db.litellm_teammembership.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_teammembership.delete_many = AsyncMock(return_value=MagicMock()) # Verification token deletion should be called mock_db_client.db.litellm_verificationtoken = MagicMock() mock_db_client.db.litellm_verificationtoken.find_many = AsyncMock(return_value=[]) - mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock(return_value=MagicMock()) _wire_member_delete_tx(mock_db_client) @@ -4799,9 +4641,7 @@ async def test_team_member_delete_cleans_membership(mock_db_client, mock_admin_a @pytest.mark.asyncio -async def test_team_member_delete_cleans_verification_tokens( - mock_db_client, mock_admin_auth -): +async def test_team_member_delete_cleans_verification_tokens(mock_db_client, mock_admin_auth): from litellm.proxy._types import TeamMemberDeleteRequest from litellm.proxy.management_endpoints.team_endpoints import team_member_delete @@ -4811,38 +4651,28 @@ async def test_team_member_delete_cleans_verification_tokens( mock_team_row = MagicMock() mock_team_row.model_dump.return_value = { "team_id": test_team_id, - "members_with_roles": [ - {"user_id": test_user_id, "user_email": None, "role": "user"} - ], + "members_with_roles": [{"user_id": test_user_id, "user_email": None, "role": "user"}], "team_member_permissions": [], "metadata": {}, "models": [], "spend": 0.0, } - mock_db_client.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_team_row - ) + mock_db_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_team_row) mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=mock_team_row) mock_user_row = MagicMock() mock_user_row.user_id = test_user_id mock_user_row.teams = [test_team_id] - mock_db_client.db.litellm_usertable.find_many = AsyncMock( - return_value=[mock_user_row] - ) + mock_db_client.db.litellm_usertable.find_many = AsyncMock(return_value=[mock_user_row]) mock_db_client.db.litellm_usertable.update = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_teammembership = MagicMock() - mock_db_client.db.litellm_teammembership.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_teammembership.delete_many = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_verificationtoken = MagicMock() mock_db_client.db.litellm_verificationtoken.find_many = AsyncMock(return_value=[]) - mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock(return_value=MagicMock()) _wire_member_delete_tx(mock_db_client) @@ -4860,9 +4690,7 @@ async def test_team_member_delete_cleans_verification_tokens( @pytest.mark.asyncio -async def test_team_member_delete_writes_deleted_audit_log_for_member_keys( - mock_db_client, mock_admin_auth -): +async def test_team_member_delete_writes_deleted_audit_log_for_member_keys(mock_db_client, mock_admin_auth): from litellm.proxy._types import ( LiteLLM_VerificationToken, LitellmTableNames, @@ -4877,38 +4705,28 @@ async def test_team_member_delete_writes_deleted_audit_log_for_member_keys( mock_team_row = MagicMock() mock_team_row.model_dump.return_value = { "team_id": test_team_id, - "members_with_roles": [ - {"user_id": test_user_id, "user_email": None, "role": "user"} - ], + "members_with_roles": [{"user_id": test_user_id, "user_email": None, "role": "user"}], "team_member_permissions": [], "metadata": {}, "models": [], "spend": 0.0, } - mock_db_client.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_team_row - ) + mock_db_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_team_row) mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=mock_team_row) mock_user_row = MagicMock() mock_user_row.user_id = test_user_id mock_user_row.teams = [test_team_id] - mock_db_client.db.litellm_usertable.find_many = AsyncMock( - return_value=[mock_user_row] - ) + mock_db_client.db.litellm_usertable.find_many = AsyncMock(return_value=[mock_user_row]) mock_db_client.db.litellm_usertable.update = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_teammembership = MagicMock() - mock_db_client.db.litellm_teammembership.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_teammembership.delete_many = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_verificationtoken = MagicMock() mock_db_client.db.litellm_verificationtoken.find_many = AsyncMock(return_value=[member_key]) - mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_jwtkeymapping = MagicMock() mock_db_client.db.litellm_jwtkeymapping.find_many = AsyncMock(return_value=[]) mock_db_client.db.litellm_deletedverificationtoken = MagicMock() @@ -5022,9 +4840,7 @@ async def test_delete_team_writes_deleted_audit_log_for_team_keys( @pytest.mark.asyncio -async def test_team_member_delete_reads_on_the_lock_holding_transaction( - mock_db_client, mock_admin_auth -): +async def test_team_member_delete_reads_on_the_lock_holding_transaction(mock_db_client, mock_admin_auth): """ Regression pin against exhausting the connection pool with advisory-lock waiters. @@ -5050,9 +4866,7 @@ async def test_team_member_delete_reads_on_the_lock_holding_transaction( "models": [], "spend": 0.0, } - mock_db_client.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_team_row - ) + mock_db_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_team_row) user_row = MagicMock() user_row.user_id = test_user_id @@ -5084,18 +4898,14 @@ async def test_team_member_delete_reads_on_the_lock_holding_transaction( user_api_key_dict=mock_admin_auth, ) - tx.litellm_usertable.find_many.assert_awaited_once_with( - where={"user_id": {"in": [test_user_id]}} - ) + tx.litellm_usertable.find_many.assert_awaited_once_with(where={"user_id": {"in": [test_user_id]}}) tx.litellm_verificationtoken.find_many.assert_awaited_once_with( where={"user_id": {"in": [test_user_id]}, "team_id": test_team_id} ) pooled_user_read.assert_not_awaited() pooled_token_read.assert_not_awaited() - tx.litellm_usertable.update.assert_awaited_once_with( - where={"user_id": test_user_id}, data={"teams": {"set": []}} - ) + tx.litellm_usertable.update.assert_awaited_once_with(where={"user_id": test_user_id}, data={"teams": {"set": []}}) tx.litellm_teammembership.delete_many.assert_awaited_once_with( where={"team_id": test_team_id, "user_id": test_user_id} ) @@ -5136,18 +4946,14 @@ async def test_team_member_delete_by_email_the_user_row_does_not_carry( mock_team_row = MagicMock() mock_team_row.model_dump.return_value = { "team_id": test_team_id, - "members_with_roles": [ - {"user_id": test_user_id, "user_email": roster_email, "role": "user"} - ], + "members_with_roles": [{"user_id": test_user_id, "user_email": roster_email, "role": "user"}], "team_member_permissions": [], "metadata": {}, "models": [], "spend": 0.0, } - mock_db_client.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_team_row - ) + mock_db_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_team_row) mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=mock_team_row) mock_user_row = MagicMock() @@ -5159,29 +4965,21 @@ async def test_team_member_delete_by_email_the_user_row_does_not_carry( if not user_row_exists: return [] user_id_filter = where.get("user_id") - if isinstance(user_id_filter, dict) and test_user_id in user_id_filter.get( - "in", [] - ): + if isinstance(user_id_filter, dict) and test_user_id in user_id_filter.get("in", []): return [mock_user_row] if where.get("user_email") == user_row_email: return [mock_user_row] return [] - mock_db_client.db.litellm_usertable.find_many = AsyncMock( - side_effect=find_user_rows - ) + mock_db_client.db.litellm_usertable.find_many = AsyncMock(side_effect=find_user_rows) mock_db_client.db.litellm_usertable.update = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_teammembership = MagicMock() - mock_db_client.db.litellm_teammembership.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_teammembership.delete_many = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_verificationtoken = MagicMock() mock_db_client.db.litellm_verificationtoken.find_many = AsyncMock(return_value=[]) - mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock(return_value=MagicMock()) _wire_member_delete_tx(mock_db_client) @@ -5228,30 +5026,22 @@ async def test_team_member_delete_clears_team_left_on_the_user_row_without_a_ros "models": [], "spend": 0.0, } - mock_db_client.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_team_row - ) + mock_db_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_team_row) mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=mock_team_row) mock_user_row = MagicMock() mock_user_row.user_id = test_user_id mock_user_row.user_email = None mock_user_row.teams = [test_team_id, "other-team", test_team_id] - mock_db_client.db.litellm_usertable.find_many = AsyncMock( - return_value=[mock_user_row] - ) + mock_db_client.db.litellm_usertable.find_many = AsyncMock(return_value=[mock_user_row]) mock_db_client.db.litellm_usertable.update = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_teammembership = MagicMock() - mock_db_client.db.litellm_teammembership.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_teammembership.delete_many = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_verificationtoken = MagicMock() mock_db_client.db.litellm_verificationtoken.find_many = AsyncMock(return_value=[]) - mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock(return_value=MagicMock()) _wire_member_delete_tx(mock_db_client) @@ -5270,9 +5060,7 @@ async def test_team_member_delete_clears_team_left_on_the_user_row_without_a_ros @pytest.mark.asyncio -async def test_team_member_delete_still_rejects_a_user_the_team_has_no_trace_of( - mock_db_client, mock_admin_auth -): +async def test_team_member_delete_still_rejects_a_user_the_team_has_no_trace_of(mock_db_client, mock_admin_auth): from litellm.proxy._types import TeamMemberDeleteRequest from litellm.proxy.management_endpoints.team_endpoints import team_member_delete @@ -5288,24 +5076,18 @@ async def test_team_member_delete_still_rejects_a_user_the_team_has_no_trace_of( "models": [], "spend": 0.0, } - mock_db_client.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_team_row - ) + mock_db_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_team_row) mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=mock_team_row) mock_user_row = MagicMock() mock_user_row.user_id = test_user_id mock_user_row.user_email = None mock_user_row.teams = ["other-team"] - mock_db_client.db.litellm_usertable.find_many = AsyncMock( - return_value=[mock_user_row] - ) + mock_db_client.db.litellm_usertable.find_many = AsyncMock(return_value=[mock_user_row]) mock_db_client.db.litellm_usertable.update = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_teammembership = MagicMock() - mock_db_client.db.litellm_teammembership.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_teammembership.delete_many = AsyncMock(return_value=MagicMock()) _wire_member_delete_tx(mock_db_client) @@ -5342,17 +5124,13 @@ async def test_team_member_delete_leaves_a_bystander_named_by_a_conflicting_user mock_team_row = MagicMock() mock_team_row.model_dump.return_value = { "team_id": test_team_id, - "members_with_roles": [ - {"user_id": roster_user_id, "user_email": roster_email, "role": "user"} - ], + "members_with_roles": [{"user_id": roster_user_id, "user_email": roster_email, "role": "user"}], "team_member_permissions": [], "metadata": {}, "models": [], "spend": 0.0, } - mock_db_client.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_team_row - ) + mock_db_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_team_row) mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=mock_team_row) roster_user_row = MagicMock() @@ -5373,32 +5151,18 @@ async def test_team_member_delete_leaves_a_bystander_named_by_a_conflicting_user async def find_user_rows(where): user_id_filter = where.get("user_id") if isinstance(user_id_filter, dict): - return [ - rows_by_user_id[uid] - for uid in user_id_filter.get("in", []) - if uid in rows_by_user_id - ] - return [ - row - for row in rows_by_user_id.values() - if row.user_email == where.get("user_email") - ] + return [rows_by_user_id[uid] for uid in user_id_filter.get("in", []) if uid in rows_by_user_id] + return [row for row in rows_by_user_id.values() if row.user_email == where.get("user_email")] - mock_db_client.db.litellm_usertable.find_many = AsyncMock( - side_effect=find_user_rows - ) + mock_db_client.db.litellm_usertable.find_many = AsyncMock(side_effect=find_user_rows) mock_db_client.db.litellm_usertable.update = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_teammembership = MagicMock() - mock_db_client.db.litellm_teammembership.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_teammembership.delete_many = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_verificationtoken = MagicMock() mock_db_client.db.litellm_verificationtoken.find_many = AsyncMock(return_value=[]) - mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock(return_value=MagicMock()) _wire_member_delete_tx(mock_db_client) @@ -5449,9 +5213,7 @@ async def test_team_member_delete_by_email_only_touches_the_row_carrying_the_sta "models": [], "spend": 0.0, } - mock_db_client.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_team_row - ) + mock_db_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_team_row) mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=mock_team_row) stale_user_row = MagicMock() @@ -5464,21 +5226,15 @@ async def test_team_member_delete_by_email_only_touches_the_row_carrying_the_sta namesake_user_row.user_email = shared_email namesake_user_row.teams = ["other-team"] - mock_db_client.db.litellm_usertable.find_many = AsyncMock( - return_value=[stale_user_row, namesake_user_row] - ) + mock_db_client.db.litellm_usertable.find_many = AsyncMock(return_value=[stale_user_row, namesake_user_row]) mock_db_client.db.litellm_usertable.update = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_teammembership = MagicMock() - mock_db_client.db.litellm_teammembership.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_teammembership.delete_many = AsyncMock(return_value=MagicMock()) mock_db_client.db.litellm_verificationtoken = MagicMock() mock_db_client.db.litellm_verificationtoken.find_many = AsyncMock(return_value=[]) - mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock( - return_value=MagicMock() - ) + mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock(return_value=MagicMock()) _wire_member_delete_tx(mock_db_client) @@ -5504,9 +5260,7 @@ class _InjectedMemberDeleteFailure(Exception): @pytest.mark.asyncio -async def test_team_member_delete_is_atomic_across_its_four_writes( - mock_db_client, mock_admin_auth -): +async def test_team_member_delete_is_atomic_across_its_four_writes(mock_db_client, mock_admin_auth): """ /team/member_delete's four cleanups (team roster, user.teams, team membership, verification tokens) run as one transaction, so a failure @@ -5527,25 +5281,19 @@ async def test_team_member_delete_is_atomic_across_its_four_writes( mock_team_row = MagicMock() mock_team_row.model_dump.return_value = { "team_id": test_team_id, - "members_with_roles": [ - {"user_id": test_user_id, "user_email": None, "role": "user"} - ], + "members_with_roles": [{"user_id": test_user_id, "user_email": None, "role": "user"}], "team_member_permissions": [], "metadata": {}, "models": [], "spend": 0.0, } - mock_db_client.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_team_row - ) + mock_db_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_team_row) mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=mock_team_row) mock_user_row = MagicMock() mock_user_row.user_id = test_user_id mock_user_row.teams = [test_team_id] - mock_db_client.db.litellm_usertable.find_many = AsyncMock( - return_value=[mock_user_row] - ) + mock_db_client.db.litellm_usertable.find_many = AsyncMock(return_value=[mock_user_row]) mock_db_client.db.litellm_usertable.update = AsyncMock( side_effect=_InjectedMemberDeleteFailure("boom between writes 1 and 2") ) @@ -5609,9 +5357,7 @@ async def test_new_team_max_budget_exceeds_user_max_budget(): patch("litellm.proxy.proxy_server._license_check") as mock_license, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, ): # Setup basic mocks mock_prisma.db.litellm_teamtable.count = AsyncMock(return_value=0) @@ -5639,9 +5385,7 @@ async def test_new_team_max_budget_exceeds_user_max_budget(): # ProxyException stores status_code in 'code' attribute assert exc_info.value.code == "400" assert "max budget higher than user max" in str(exc_info.value.message) - assert "100.0" in str( - exc_info.value.message - ) # User's user_max_budget should be mentioned + assert "100.0" in str(exc_info.value.message) # User's user_max_budget should be mentioned assert LitellmUserRoles.INTERNAL_USER.value in str(exc_info.value.message) @@ -5678,9 +5422,7 @@ async def test_new_team_max_budget_within_user_limit(): patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server._license_check") as mock_license, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, ): # Setup mocks mock_prisma.db.litellm_teamtable.count = AsyncMock(return_value=0) @@ -5712,19 +5454,13 @@ async def test_new_team_max_budget_within_user_limit(): "max_budget": 50.0, "members_with_roles": [], } - mock_prisma.db.litellm_teamtable.create = AsyncMock( - return_value=mock_created_team - ) + mock_prisma.db.litellm_teamtable.create = AsyncMock(return_value=mock_created_team) _wire_team_create_tx(mock_prisma) - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_created_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_created_team) # Mock model table mock_prisma.db.litellm_modeltable = MagicMock() - mock_prisma.db.litellm_modeltable.create = AsyncMock( - return_value=MagicMock(id="model123") - ) + mock_prisma.db.litellm_modeltable.create = AsyncMock(return_value=MagicMock(id="model123")) # Mock user table operations for adding the creator as a member mock_user = MagicMock() @@ -5747,9 +5483,7 @@ async def test_new_team_max_budget_within_user_limit(): "budget_id": None, } mock_prisma.db.litellm_teammembership = MagicMock() - mock_prisma.db.litellm_teammembership.upsert = AsyncMock( - return_value=mock_membership - ) + mock_prisma.db.litellm_teammembership.upsert = AsyncMock(return_value=mock_membership) # Should NOT raise an exception result = await new_team( @@ -5810,12 +5544,8 @@ async def test_new_team_org_scoped_budget_bypasses_user_limit(): patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server._license_check") as mock_license, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, - patch( - "litellm.proxy.management_endpoints.team_endpoints.get_org_object" - ) as mock_get_org, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, + patch("litellm.proxy.management_endpoints.team_endpoints.get_org_object") as mock_get_org, ): # Setup mocks mock_prisma.db.litellm_teamtable.count = AsyncMock(return_value=0) @@ -5855,19 +5585,13 @@ async def test_new_team_org_scoped_budget_bypasses_user_limit(): "organization_id": "test-org-123", "members_with_roles": [], } - mock_prisma.db.litellm_teamtable.create = AsyncMock( - return_value=mock_created_team - ) + mock_prisma.db.litellm_teamtable.create = AsyncMock(return_value=mock_created_team) _wire_team_create_tx(mock_prisma) - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_created_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_created_team) # Mock model table mock_prisma.db.litellm_modeltable = MagicMock() - mock_prisma.db.litellm_modeltable.create = AsyncMock( - return_value=MagicMock(id="model123") - ) + mock_prisma.db.litellm_modeltable.create = AsyncMock(return_value=MagicMock(id="model123")) # Mock user table operations mock_user = MagicMock() @@ -5890,9 +5614,7 @@ async def test_new_team_org_scoped_budget_bypasses_user_limit(): "budget_id": None, } mock_prisma.db.litellm_teammembership = MagicMock() - mock_prisma.db.litellm_teammembership.upsert = AsyncMock( - return_value=mock_membership - ) + mock_prisma.db.litellm_teammembership.upsert = AsyncMock(return_value=mock_membership) # Should NOT raise an exception - the fix should bypass user budget validation for org-scoped teams result = await new_team( @@ -5943,9 +5665,7 @@ async def test_new_team_org_scoped_models_bypasses_user_limit(): # Create team request with models that are within org's allowed models but not user's team_request = NewTeamRequest( team_alias="org-scoped-models-team", - models=[ - "gpt-4" - ], # Within org's allowed models, but not in user's personal models + models=["gpt-4"], # Within org's allowed models, but not in user's personal models organization_id="test-org-456", # This makes it an org-scoped team ) @@ -5956,12 +5676,8 @@ async def test_new_team_org_scoped_models_bypasses_user_limit(): patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server._license_check") as mock_license, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, - patch( - "litellm.proxy.management_endpoints.team_endpoints.get_org_object" - ) as mock_get_org, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, + patch("litellm.proxy.management_endpoints.team_endpoints.get_org_object") as mock_get_org, ): # Setup mocks mock_prisma.db.litellm_teamtable.count = AsyncMock(return_value=0) @@ -6003,19 +5719,13 @@ async def test_new_team_org_scoped_models_bypasses_user_limit(): "models": ["gpt-4"], "members_with_roles": [], } - mock_prisma.db.litellm_teamtable.create = AsyncMock( - return_value=mock_created_team - ) + mock_prisma.db.litellm_teamtable.create = AsyncMock(return_value=mock_created_team) _wire_team_create_tx(mock_prisma) - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_created_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_created_team) # Mock model table mock_prisma.db.litellm_modeltable = MagicMock() - mock_prisma.db.litellm_modeltable.create = AsyncMock( - return_value=MagicMock(id="model123") - ) + mock_prisma.db.litellm_modeltable.create = AsyncMock(return_value=MagicMock(id="model123")) # Mock user table operations mock_user = MagicMock() @@ -6038,9 +5748,7 @@ async def test_new_team_org_scoped_models_bypasses_user_limit(): "budget_id": None, } mock_prisma.db.litellm_teammembership = MagicMock() - mock_prisma.db.litellm_teammembership.upsert = AsyncMock( - return_value=mock_membership - ) + mock_prisma.db.litellm_teammembership.upsert = AsyncMock(return_value=mock_membership) # Should NOT raise an exception - the fix should bypass user model validation for org-scoped teams result = await new_team( @@ -6100,9 +5808,7 @@ async def test_new_team_standalone_validates_against_user_models(monkeypatch): patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server._license_check") as mock_license, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, ): # Setup basic mocks mock_prisma.db.litellm_teamtable.count = AsyncMock(return_value=0) @@ -6169,9 +5875,7 @@ async def test_new_team_standalone_validates_against_user_budget(): patch("litellm.proxy.proxy_server._license_check") as mock_license, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, ): # Setup basic mocks mock_prisma.db.litellm_teamtable.count = AsyncMock(return_value=0) @@ -6196,9 +5900,7 @@ async def test_new_team_standalone_validates_against_user_budget(): # Verify exception details assert exc_info.value.code == "400" assert "max budget higher than user max" in str(exc_info.value.message) - assert "3.0" in str( - exc_info.value.message - ) # User's max_budget should be mentioned + assert "3.0" in str(exc_info.value.message) # User's max_budget should be mentioned @pytest.mark.asyncio @@ -6242,12 +5944,8 @@ async def test_new_team_org_scoped_budget_exceeds_org_limit(): patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server._license_check") as mock_license, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, - patch( - "litellm.proxy.management_endpoints.team_endpoints.get_org_object" - ) as mock_get_org, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, + patch("litellm.proxy.management_endpoints.team_endpoints.get_org_object") as mock_get_org, ): # Setup mocks mock_prisma.db.litellm_teamtable.count = AsyncMock(return_value=0) @@ -6321,12 +6019,8 @@ async def test_new_team_org_scoped_models_not_in_org_models(): patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server._license_check") as mock_license, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, - patch( - "litellm.proxy.management_endpoints.team_endpoints.get_org_object" - ) as mock_get_org, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, + patch("litellm.proxy.management_endpoints.team_endpoints.get_org_object") as mock_get_org, ): # Setup mocks mock_prisma.db.litellm_teamtable.count = AsyncMock(return_value=0) @@ -6350,10 +6044,7 @@ async def test_new_team_org_scoped_models_not_in_org_models(): # Verify exception details assert exc_info.value.code == "400" - assert ( - "claude-3-opus" in str(exc_info.value.message) - or "organization" in str(exc_info.value.message).lower() - ) + assert "claude-3-opus" in str(exc_info.value.message) or "organization" in str(exc_info.value.message).lower() @pytest.mark.asyncio @@ -6398,9 +6089,7 @@ async def test_update_team_standalone_budget_raise_blocked_for_team_admin(): patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ), + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()), ): mock_existing_team = MagicMock() mock_existing_team.team_id = "standalone-team-123" @@ -6411,13 +6100,9 @@ async def test_update_team_standalone_budget_raise_blocked_for_team_admin(): "team_id": "standalone-team-123", "organization_id": None, "max_budget": 30.0, - "members_with_roles": [ - {"user_id": "non-admin-update-test", "role": "admin"} - ], + "members_with_roles": [{"user_id": "non-admin-update-test", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) mock_cache.async_get_cache = AsyncMock(return_value=None) with pytest.raises(ProxyException) as exc_info: @@ -6466,9 +6151,7 @@ async def test_update_team_standalone_budget_raise_allowed_for_proxy_admin( patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ), + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()), ): mock_existing_team = MagicMock() mock_existing_team.team_id = "standalone-team-123" @@ -6479,13 +6162,9 @@ async def test_update_team_standalone_budget_raise_allowed_for_proxy_admin( "team_id": "standalone-team-123", "organization_id": None, "max_budget": 30.0, - "members_with_roles": [ - {"user_id": "proxy-admin-update-test", "role": "admin"} - ], + "members_with_roles": [{"user_id": "proxy-admin-update-test", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) mock_prisma.jsonify_team_object = lambda db_data: db_data mock_cache.async_get_cache = AsyncMock(return_value=None) mock_cache.async_set_cache = AsyncMock() @@ -6500,9 +6179,7 @@ async def test_update_team_standalone_budget_raise_allowed_for_proxy_admin( "organization_id": None, "max_budget": 100.0, } - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_updated_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_updated_team) result = await update_team( data=update_request, @@ -6555,9 +6232,7 @@ async def test_update_team_standalone_budget_removal_blocked_for_team_admin(): patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ), + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()), ): mock_existing_team = MagicMock() mock_existing_team.team_id = "standalone-team-123" @@ -6568,13 +6243,9 @@ async def test_update_team_standalone_budget_removal_blocked_for_team_admin(): "team_id": "standalone-team-123", "organization_id": None, "max_budget": 500.0, - "members_with_roles": [ - {"user_id": "budget-removal-admin", "role": "admin"} - ], + "members_with_roles": [{"user_id": "budget-removal-admin", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) mock_cache.async_get_cache = AsyncMock(return_value=None) with pytest.raises(ProxyException) as exc_info: @@ -6625,9 +6296,7 @@ async def test_update_team_standalone_uncapped_team_admin_sets_finite_allowed( patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ), + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()), ): _TeamRowStore( mock_prisma.db.litellm_teamtable, @@ -6698,9 +6367,7 @@ async def test_update_team_standalone_unchanged_budget_allowed( patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, ): # Mock existing standalone team (no organization_id) with budget=$500 mock_existing_team = MagicMock() @@ -6712,13 +6379,9 @@ async def test_update_team_standalone_unchanged_budget_allowed( "team_id": "standalone-unchanged-budget-123", "organization_id": None, "max_budget": 500.0, - "members_with_roles": [ - {"user_id": "standalone-unchanged-budget-admin", "role": "admin"} - ], + "members_with_roles": [{"user_id": "standalone-unchanged-budget-admin", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) mock_prisma.jsonify_team_object = lambda db_data: db_data # User has a restrictive personal budget that is lower than the team's. @@ -6740,9 +6403,7 @@ async def test_update_team_standalone_unchanged_budget_allowed( "max_budget": 500.0, "tpm_limit": 50000, } - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_updated_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_updated_team) # Should NOT raise - unchanged budget skips the personal-budget check. result = await update_team( @@ -6797,9 +6458,7 @@ async def test_update_team_standalone_lower_budget_allowed( patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, ): _TeamRowStore( mock_prisma.db.litellm_teamtable, @@ -6879,9 +6538,7 @@ async def test_update_team_org_scoped_budget_exceeds_org_limit(): patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, patch( "litellm.proxy.management_endpoints.team_endpoints.get_org_object", new=AsyncMock(return_value=mock_org), @@ -6896,13 +6553,9 @@ async def test_update_team_org_scoped_budget_exceeds_org_limit(): "team_id": "org-team-456", "organization_id": "test-org-update", "max_budget": 80.0, - "members_with_roles": [ - {"user_id": "org-admin-update-test", "role": "admin"} - ], + "members_with_roles": [{"user_id": "org-admin-update-test", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) # Should raise ProxyException because new budget exceeds org's max_budget with pytest.raises(ProxyException) as exc_info: @@ -6914,10 +6567,7 @@ async def test_update_team_org_scoped_budget_exceeds_org_limit(): # Verify exception details assert exc_info.value.code == "400" - assert ( - "organization" in str(exc_info.value.message).lower() - or "budget" in str(exc_info.value.message).lower() - ) + assert "organization" in str(exc_info.value.message).lower() or "budget" in str(exc_info.value.message).lower() @pytest.mark.asyncio @@ -6960,9 +6610,7 @@ async def test_update_team_standalone_models_not_gated_by_user_limit( patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, ): # Mock existing standalone team (no organization_id) mock_existing_team = MagicMock() @@ -6974,13 +6622,9 @@ async def test_update_team_standalone_models_not_gated_by_user_limit( "team_id": "standalone-team-models-123", "organization_id": None, "models": ["gpt-3.5-turbo"], - "members_with_roles": [ - {"user_id": "non-admin-update-models-test", "role": "admin"} - ], + "members_with_roles": [{"user_id": "non-admin-update-models-test", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) mock_prisma.jsonify_team_object = lambda db_data: db_data mock_cache.async_get_cache = AsyncMock(return_value=None) mock_cache.async_set_cache = AsyncMock() @@ -6994,9 +6638,7 @@ async def test_update_team_standalone_models_not_gated_by_user_limit( "organization_id": None, "models": ["gpt-4"], } - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_updated_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_updated_team) result = await update_team( data=update_request, @@ -7060,9 +6702,7 @@ async def test_update_team_org_scoped_budget_bypasses_user_limit( patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, patch( "litellm.proxy.management_endpoints.team_endpoints.get_org_object", new=AsyncMock(return_value=mock_org), @@ -7080,9 +6720,7 @@ async def test_update_team_org_scoped_budget_bypasses_user_limit( "max_budget": 30.0, "members_with_roles": [], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) mock_prisma.jsonify_team_object = lambda db_data: db_data # Mock user cache to return user with restrictive budget @@ -7091,9 +6729,7 @@ async def test_update_team_org_scoped_budget_bypasses_user_limit( max_budget=3.0, # Restrictive personal budget ) mock_cache.async_get_cache = AsyncMock(return_value=mock_user_obj) - mock_cache.async_set_cache = ( - AsyncMock() - ) # Mock cache set for _cache_team_object + mock_cache.async_set_cache = AsyncMock() # Mock cache set for _cache_team_object # Mock team update mock_updated_team = MagicMock() @@ -7106,9 +6742,7 @@ async def test_update_team_org_scoped_budget_bypasses_user_limit( "organization_id": "test-org-update-budget", "max_budget": 50.0, } - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_updated_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_updated_team) # Should NOT raise an exception - bypass user budget validation for org-scoped teams result = await update_team( @@ -7171,9 +6805,7 @@ async def test_update_team_org_scoped_models_bypasses_user_limit( patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, patch( "litellm.proxy.management_endpoints.team_endpoints.get_org_object", new=AsyncMock(return_value=mock_org), @@ -7189,17 +6821,11 @@ async def test_update_team_org_scoped_models_bypasses_user_limit( "team_id": "org-team-update-models-123", "organization_id": "test-org-update-models", "models": ["gpt-3.5-turbo"], - "members_with_roles": [ - {"user_id": "org-admin-update-models-test", "role": "admin"} - ], + "members_with_roles": [{"user_id": "org-admin-update-models-test", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) mock_prisma.jsonify_team_object = lambda db_data: db_data - mock_cache.async_set_cache = ( - AsyncMock() - ) # Mock cache set for _cache_team_object + mock_cache.async_set_cache = AsyncMock() # Mock cache set for _cache_team_object # Mock team update mock_updated_team = MagicMock() @@ -7212,9 +6838,7 @@ async def test_update_team_org_scoped_models_bypasses_user_limit( "organization_id": "test-org-update-models", "models": ["gpt-4"], } - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_updated_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_updated_team) # Should NOT raise an exception - bypass user models validation for org-scoped teams result = await update_team( @@ -7275,9 +6899,7 @@ async def test_update_team_org_scoped_models_not_in_org_models(): patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, patch( "litellm.proxy.management_endpoints.team_endpoints.get_org_object", new=AsyncMock(return_value=mock_org), @@ -7292,13 +6914,9 @@ async def test_update_team_org_scoped_models_not_in_org_models(): "team_id": "org-team-update-models-fail-123", "organization_id": "test-org-update-models-fail", "models": ["gpt-4"], - "members_with_roles": [ - {"user_id": "org-admin-update-models-fail-test", "role": "admin"} - ], + "members_with_roles": [{"user_id": "org-admin-update-models-fail-test", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) # Should raise ProxyException because claude-3-opus is not in org's allowed models with pytest.raises(ProxyException) as exc_info: @@ -7310,10 +6928,7 @@ async def test_update_team_org_scoped_models_not_in_org_models(): # Verify exception details assert exc_info.value.code == "400" - assert ( - "claude-3-opus" in str(exc_info.value.message) - or "organization" in str(exc_info.value.message).lower() - ) + assert "claude-3-opus" in str(exc_info.value.message) or "organization" in str(exc_info.value.message).lower() @pytest.mark.asyncio @@ -7366,9 +6981,7 @@ async def test_update_team_org_scoped_models_with_all_proxy_models( patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, patch( "litellm.proxy.management_endpoints.team_endpoints.get_org_object", new=AsyncMock(return_value=mock_org), @@ -7384,17 +6997,11 @@ async def test_update_team_org_scoped_models_with_all_proxy_models( "team_id": "org-team-all-proxy-models-123", "organization_id": "test-org-all-proxy-models", "models": ["gpt-4"], - "members_with_roles": [ - {"user_id": "org-admin-all-proxy-models-test", "role": "admin"} - ], + "members_with_roles": [{"user_id": "org-admin-all-proxy-models-test", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) mock_prisma.jsonify_team_object = lambda db_data: db_data - mock_cache.async_set_cache = ( - AsyncMock() - ) # Mock cache set for _cache_team_object + mock_cache.async_set_cache = AsyncMock() # Mock cache set for _cache_team_object # Mock team update mock_updated_team = MagicMock() @@ -7415,9 +7022,7 @@ async def test_update_team_org_scoped_models_with_all_proxy_models( "gpt-4o-mini-test", ], } - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_updated_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_updated_team) # Should NOT raise an exception - 'all-proxy-models' allows all models result = await update_team( @@ -7476,9 +7081,7 @@ async def test_update_team_tpm_limit_not_gated_by_user_limit( patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ), + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()), ): # Mock existing standalone team mock_existing_team = MagicMock() @@ -7492,9 +7095,7 @@ async def test_update_team_tpm_limit_not_gated_by_user_limit( "tpm_limit": 500, "members_with_roles": [{"user_id": "tpm-limit-user", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) mock_prisma.jsonify_team_object = lambda db_data: db_data mock_cache.async_get_cache = AsyncMock(return_value=None) mock_cache.async_set_cache = AsyncMock() @@ -7508,9 +7109,7 @@ async def test_update_team_tpm_limit_not_gated_by_user_limit( "organization_id": None, "tpm_limit": 5000, } - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_updated_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_updated_team) result = await update_team( data=update_request, @@ -7559,9 +7158,7 @@ async def test_update_team_rpm_limit_not_gated_by_user_limit( patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ), + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()), ): # Mock existing standalone team mock_existing_team = MagicMock() @@ -7575,9 +7172,7 @@ async def test_update_team_rpm_limit_not_gated_by_user_limit( "rpm_limit": 50, "members_with_roles": [{"user_id": "rpm-limit-user", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) mock_prisma.jsonify_team_object = lambda db_data: db_data mock_cache.async_get_cache = AsyncMock(return_value=None) mock_cache.async_set_cache = AsyncMock() @@ -7591,9 +7186,7 @@ async def test_update_team_rpm_limit_not_gated_by_user_limit( "organization_id": None, "rpm_limit": 500, } - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_updated_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_updated_team) result = await update_team( data=update_request, @@ -7852,9 +7445,7 @@ async def test_new_team_org_scoped_tpm_rpm_bypasses_user_limit(): patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), patch("litellm.proxy.proxy_server._license_check") as mock_license, - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ), + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()), patch( "litellm.proxy.management_endpoints.team_endpoints.get_org_object", new=AsyncMock(return_value=mock_org), @@ -7885,13 +7476,9 @@ async def test_new_team_org_scoped_tpm_rpm_bypasses_user_limit(): "metadata": None, "members_with_roles": [], } - mock_prisma.db.litellm_teamtable.create = AsyncMock( - return_value=mock_created_team - ) + mock_prisma.db.litellm_teamtable.create = AsyncMock(return_value=mock_created_team) _wire_team_create_tx(mock_prisma) - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_created_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_created_team) mock_prisma.jsonify_team_object = MagicMock(side_effect=lambda db_data: db_data) # Should succeed - bypasses user limits since org-scoped @@ -7972,13 +7559,9 @@ async def test_update_team_org_scoped_tpm_exceeds_org_limit(): "team_id": "org-team-update-tpm-123", "organization_id": "test-org-update-tpm", "tpm_limit": 5000, - "members_with_roles": [ - {"user_id": "org-admin-update-tpm-test", "role": "admin"} - ], + "members_with_roles": [{"user_id": "org-admin-update-tpm-test", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) # Should raise ProxyException because TPM exceeds org limit with pytest.raises(ProxyException) as exc_info: @@ -8060,13 +7643,9 @@ async def test_update_team_org_scoped_rpm_exceeds_org_limit(): "team_id": "org-team-update-rpm-123", "organization_id": "test-org-update-rpm", "rpm_limit": 500, - "members_with_roles": [ - {"user_id": "org-admin-update-rpm-test", "role": "admin"} - ], + "members_with_roles": [{"user_id": "org-admin-update-rpm-test", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) # Should raise ProxyException because RPM exceeds org limit with pytest.raises(ProxyException) as exc_info: @@ -8157,13 +7736,9 @@ async def test_update_team_org_scoped_tpm_rpm_bypasses_user_limit( "organization_id": "test-org-update-bypass", "tpm_limit": 5000, "rpm_limit": 500, - "members_with_roles": [ - {"user_id": "org-admin-update-bypass-test", "role": "admin"} - ], + "members_with_roles": [{"user_id": "org-admin-update-bypass-test", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) mock_cache.async_set_cache = AsyncMock() mock_logging.internal_usage_cache.dual_cache.async_delete_cache = AsyncMock() @@ -8179,9 +7754,7 @@ async def test_update_team_org_scoped_tpm_rpm_bypasses_user_limit( "tpm_limit": 10000, "rpm_limit": 1000, } - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_updated_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_updated_team) mock_prisma.jsonify_team_object = MagicMock(side_effect=lambda db_data: db_data) # Should succeed - bypasses user limits since org-scoped @@ -8269,9 +7842,7 @@ async def test_update_team_guardrails_with_org_id( patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ), + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()), patch( "litellm.proxy.proxy_server.premium_user", True, # Required for guardrails feature @@ -8296,20 +7867,14 @@ async def test_update_team_guardrails_with_org_id( "max_budget": None, "tpm_limit": None, "rpm_limit": None, - "members_with_roles": [ - {"user_id": "org-admin-guardrails-test", "role": "admin"} - ], + "members_with_roles": [{"user_id": "org-admin-guardrails-test", "role": "admin"}], } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) mock_cache.async_set_cache = AsyncMock() # Mock organization fetch - this is where the bug occurred # The fix ensures 'teams: True' is in the include clause - mock_prisma.db.litellm_organizationtable.find_unique = AsyncMock( - return_value=mock_org - ) + mock_prisma.db.litellm_organizationtable.find_unique = AsyncMock(return_value=mock_org) # Destination-org guard in update_team queries for the caller's # ORG_ADMIN membership on the destination org. Return a match so @@ -8317,18 +7882,14 @@ async def test_update_team_guardrails_with_org_id( mock_org_admin_membership = MagicMock() mock_org_admin_membership.user_id = "org-admin-guardrails-test" mock_org_admin_membership.organization_id = "test-org-guardrails" - mock_prisma.db.litellm_organizationmembership.find_many = AsyncMock( - return_value=[mock_org_admin_membership] - ) + mock_prisma.db.litellm_organizationmembership.find_many = AsyncMock(return_value=[mock_org_admin_membership]) # Mock team update mock_updated_team = MagicMock(spec=LiteLLM_TeamTable) mock_updated_team.team_id = "team-guardrails-123" mock_updated_team.object_permission_id = None mock_updated_team.organization_id = "test-org-guardrails" - mock_updated_team.metadata = { - "guardrails": ["aporia-pre-call", "aporia-post-call"] - } + mock_updated_team.metadata = {"guardrails": ["aporia-pre-call", "aporia-post-call"]} mock_updated_team.litellm_model_table = None mock_updated_team.access_group_ids = None mock_updated_team.model_dump.return_value = { @@ -8336,9 +7897,7 @@ async def test_update_team_guardrails_with_org_id( "organization_id": "test-org-guardrails", "metadata": {"guardrails": ["aporia-pre-call", "aporia-post-call"]}, } - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_updated_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_updated_team) mock_prisma.jsonify_team_object = MagicMock(side_effect=lambda db_data: db_data) # async_get_cache must be an AsyncMock so `await` in get_org_object works mock_cache.async_get_cache = AsyncMock(return_value=None) @@ -8368,11 +7927,7 @@ async def test_update_team_guardrails_with_org_id( assert mock_prisma.db.litellm_organizationtable.find_unique.call_count >= 1 # Get the first call (from fetch_and_validate_organization) - first_call_kwargs = ( - mock_prisma.db.litellm_organizationtable.find_unique.call_args_list[ - 0 - ].kwargs - ) + first_call_kwargs = mock_prisma.db.litellm_organizationtable.find_unique.call_args_list[0].kwargs # Verify that 'teams' is included in the fetch assert "include" in first_call_kwargs @@ -8423,9 +7978,7 @@ def test_transform_teams_to_deleted_records(): assert all("litellm_changed_by" in record for record in records) assert all(record["deleted_by"] == "user-123" for record in records) # UserAPIKeyAuth hashes the api_key, so we check against the hashed value - assert all( - record["deleted_by_api_key"] == user_api_key_dict.api_key for record in records - ) + assert all(record["deleted_by_api_key"] == user_api_key_dict.api_key for record in records) assert all(record["litellm_changed_by"] == "admin-user" for record in records) record1 = records[0] @@ -8570,9 +8123,7 @@ async def test_delete_team_persists_deleted_teams( mock_prisma_client.db.litellm_deletedteamtable.create_many = mock_create_many_teams mock_create_many_keys = AsyncMock() - mock_prisma_client.db.litellm_deletedverificationtoken.create_many = ( - mock_create_many_keys - ) + mock_prisma_client.db.litellm_deletedverificationtoken.create_many = mock_create_many_keys mock_find_many_keys = AsyncMock(return_value=[]) mock_prisma_client.db.litellm_verificationtoken.find_many = mock_find_many_keys @@ -8732,9 +8283,7 @@ async def test_delete_team_sweeps_references_outside_members_with_roles( ("team-doomed", "doomed-team"), ("team-kept", "kept-team"), ): - cached_obj = LiteLLM_TeamTableCachedObj( - team_id=cached_team_id, team_alias=cached_alias - ) + cached_obj = LiteLLM_TeamTableCachedObj(team_id=cached_team_id, team_alias=cached_alias) fresh_cache.set_cache(key=f"team_id:{cached_team_id}", value=cached_obj) fresh_cache.set_cache(key=f"team_alias:{cached_alias}", value=cached_obj) @@ -8999,7 +8548,11 @@ async def test_delete_validated_team_rows_deletes_only_the_keys_its_rostered_mem ) team_keys = tuple( LiteLLM_VerificationToken(token=token, team_id="team-doomed", user_id=user_id) - for token, user_id in (("hashed-member", "member-1"), ("hashed-service-account", None), ("hashed-outsider", "admin-made")) + for token, user_id in ( + ("hashed-member", "member-1"), + ("hashed-service-account", None), + ("hashed-outsider", "admin-made"), + ) ) mock_prisma_client = AsyncMock() mock_prisma_client.db.litellm_teammembership.delete_many = AsyncMock() @@ -9159,7 +8712,9 @@ async def test_delete_team_evicts_the_auth_cache_of_the_keys_it_deletes( _wire_team_delete_tx(mock_prisma_client) fresh_cache = UserApiKeyCache() - fresh_cache.set_cache(key="hashed-doomed-key", value=UserAPIKeyAuth(token="hashed-doomed-key", team_id="team-doomed")) + fresh_cache.set_cache( + key="hashed-doomed-key", value=UserAPIKeyAuth(token="hashed-doomed-key", team_id="team-doomed") + ) fresh_cache.set_cache(key="hashed-unrelated-key", value=UserAPIKeyAuth(token="hashed-unrelated-key")) monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", mock_prisma_client) @@ -9373,7 +8928,7 @@ async def test_deleting_a_500_member_team_reaches_every_member_cache_on_other_wo team_membership_auth_cache_key, team_membership_reservation_cache_key, ) - from litellm.proxy.management_endpoints.team_endpoints import _evict_team_member_cache_keys, member_cache_keys + from litellm.proxy.management_endpoints.team_endpoints import evict_team_member_cache_keys, member_cache_keys redis: Final = _BroadcastRecordingRedis(round_trip_seconds=0.001) monkeypatch.setattr(pubsub_module, "coordination_redis_cache", lambda: redis) @@ -9381,7 +8936,7 @@ async def test_deleting_a_500_member_team_reaches_every_member_cache_on_other_wo monkeypatch.setattr(pubsub_module, "_in_flight_publishes", asyncio.Semaphore(16)) members: Final = tuple(f"member-{index:03d}" for index in range(500)) - await _evict_team_member_cache_keys( + await evict_team_member_cache_keys( team_id="team-large", member_user_ids=members, cache_keys=tuple(member_cache_keys("team-large", members)), @@ -9563,9 +9118,7 @@ async def test_team_member_delete_persists_deleted_keys(monkeypatch): mock_prisma_client.db.litellm_verificationtoken.delete_many = mock_delete_keys mock_create_many_keys = AsyncMock() - mock_prisma_client.db.litellm_deletedverificationtoken.create_many = ( - mock_create_many_keys - ) + mock_prisma_client.db.litellm_deletedverificationtoken.create_many = mock_create_many_keys _wire_member_delete_tx(mock_prisma_client) @@ -9576,7 +9129,11 @@ async def test_team_member_delete_persists_deleted_keys(monkeypatch): cache: Final = UserApiKeyCache() revoked_cache_keys: Final = ( - "team_id:team-1", "team_alias:test-team", "user-123", "hashed-token-1", "hashed-token-2", + "team_id:team-1", + "team_alias:test-team", + "user-123", + "hashed-token-1", + "hashed-token-2", team_membership_auth_cache_key(user_id="user-123", team_id="team-1"), team_membership_reservation_cache_key(user_id="user-123", team_id="team-1"), ) @@ -9877,9 +9434,7 @@ async def test_new_team_soft_budget_validation( patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server._license_check") as mock_license, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, ): # Setup mocks mock_prisma.db.litellm_teamtable.count = AsyncMock(return_value=0) @@ -9913,19 +9468,13 @@ async def test_new_team_soft_budget_validation( "max_budget": expected_max_budget, "members_with_roles": [], } - mock_prisma.db.litellm_teamtable.create = AsyncMock( - return_value=mock_created_team - ) + mock_prisma.db.litellm_teamtable.create = AsyncMock(return_value=mock_created_team) _wire_team_create_tx(mock_prisma) - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_created_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_created_team) # Mock model table mock_prisma.db.litellm_modeltable = MagicMock() - mock_prisma.db.litellm_modeltable.create = AsyncMock( - return_value=MagicMock(id="model123") - ) + mock_prisma.db.litellm_modeltable.create = AsyncMock(return_value=MagicMock(id="model123")) # Mock user table operations mock_user = MagicMock() @@ -9948,9 +9497,7 @@ async def test_new_team_soft_budget_validation( "budget_id": None, } mock_prisma.db.litellm_teammembership = MagicMock() - mock_prisma.db.litellm_teammembership.upsert = AsyncMock( - return_value=mock_membership - ) + mock_prisma.db.litellm_teammembership.upsert = AsyncMock(return_value=mock_membership) if should_succeed: # Should NOT raise an exception @@ -10079,9 +9626,7 @@ async def test_update_team_soft_budget_validation( patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), - patch( - "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() - ) as mock_audit, + patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()) as mock_audit, ): # Mock existing team with existing budgets mock_existing_team = MagicMock() @@ -10095,9 +9640,7 @@ async def test_update_team_soft_budget_validation( "soft_budget": existing_soft_budget, "max_budget": existing_max_budget, } - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) # Mock user cache mock_user_obj = LiteLLM_UserTable( @@ -10107,14 +9650,8 @@ async def test_update_team_soft_budget_validation( mock_cache.async_get_cache = AsyncMock(return_value=mock_user_obj) # Mock updated team - preserve existing values if not being updated - final_soft_budget = ( - update_soft_budget - if update_soft_budget is not None - else existing_soft_budget - ) - final_max_budget = ( - update_max_budget if update_max_budget is not None else existing_max_budget - ) + final_soft_budget = update_soft_budget if update_soft_budget is not None else existing_soft_budget + final_max_budget = update_max_budget if update_max_budget is not None else existing_max_budget mock_updated_team = MagicMock() mock_updated_team.team_id = "test-team-123" @@ -10127,13 +9664,9 @@ async def test_update_team_soft_budget_validation( "soft_budget": final_soft_budget, "max_budget": final_max_budget, } - mock_prisma.db.litellm_teamtable.update = AsyncMock( - return_value=mock_updated_team - ) + mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=mock_updated_team) mock_prisma.jsonify_team_object = lambda db_data: db_data - mock_cache.async_set_cache = ( - AsyncMock() - ) # Mock cache set for _cache_team_object + mock_cache.async_set_cache = AsyncMock() # Mock cache set for _cache_team_object if should_succeed: # Should NOT raise an exception @@ -10179,9 +9712,7 @@ async def test_new_team_positive_budgets_accepted(): from litellm.proxy._types import NewTeamRequest # Should not raise any errors - request = NewTeamRequest( - team_alias="test-team", max_budget=100.0, team_member_budget=50.0 - ) + request = NewTeamRequest(team_alias="test-team", max_budget=100.0, team_member_budget=50.0) assert request.max_budget == 100.0 assert request.team_member_budget == 50.0 @@ -10199,15 +9730,13 @@ async def test_new_team_with_router_settings(mock_db_client, mock_admin_auth): mock_db_client.get_data = AsyncMock(return_value=None) mock_db_client.update_data = AsyncMock(return_value=MagicMock()) mock_db_client.db = MagicMock() - mock_db_client.db.litellm_proxymodeltable.find_many = AsyncMock(return_value=[ - SimpleNamespace(model_id="weighted-id", model_name="group", model_info={}) - ]) + mock_db_client.db.litellm_proxymodeltable.find_many = AsyncMock( + return_value=[SimpleNamespace(model_id="weighted-id", model_name="group", model_info={})] + ) # Mock model table creation mock_db_client.db.litellm_modeltable = MagicMock() - mock_db_client.db.litellm_modeltable.create = AsyncMock( - return_value=MagicMock(id="model123") - ) + mock_db_client.db.litellm_modeltable.create = AsyncMock(return_value=MagicMock(id="model123")) # Capture team table creation team_create_result = MagicMock( @@ -10222,9 +9751,7 @@ async def test_new_team_with_router_settings(mock_db_client, mock_admin_auth): mock_db_client.db.litellm_teamtable.create = mock_team_create _wire_team_create_tx(mock_db_client) mock_db_client.db.litellm_teamtable.count = mock_team_count - mock_db_client.db.litellm_teamtable.update = AsyncMock( - return_value=team_create_result - ) + mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=team_create_result) # Mock user table mock_db_client.db.litellm_usertable = MagicMock() @@ -10293,9 +9820,7 @@ async def test_get_team_daily_activity_member_with_permission_sees_all_spend( # Create a non-admin user user_id = "test_user_with_perm_123" team_id = "test_team_789" - user_api_key_dict = UserAPIKeyAuth( - user_id=user_id, user_role=LitellmUserRoles.INTERNAL_USER - ) + user_api_key_dict = UserAPIKeyAuth(user_id=user_id, user_role=LitellmUserRoles.INTERNAL_USER) # Mock user info mock_user_info = LiteLLM_UserTable( @@ -10380,9 +9905,7 @@ async def test_get_team_daily_activity_member_without_permission_filters_by_keys # Create a non-admin user user_id = "test_user_no_perm_123" team_id = "test_team_789" - user_api_key_dict = UserAPIKeyAuth( - user_id=user_id, user_role=LitellmUserRoles.INTERNAL_USER - ) + user_api_key_dict = UserAPIKeyAuth(user_id=user_id, user_role=LitellmUserRoles.INTERNAL_USER) # Mock user info mock_user_info = LiteLLM_UserTable( @@ -10416,9 +9939,7 @@ async def test_get_team_daily_activity_member_without_permission_filters_by_keys # Setup mocks mock_db_client.db.litellm_teamtable.find_many = AsyncMock(return_value=[mock_team]) - mock_db_client.db.litellm_verificationtoken.find_many = AsyncMock( - return_value=[user_api_key_1, user_api_key_2] - ) + mock_db_client.db.litellm_verificationtoken.find_many = AsyncMock(return_value=[user_api_key_1, user_api_key_2]) # Mock get_user_object with patch( @@ -10472,9 +9993,9 @@ async def test_update_team_with_router_settings( # Configure mocked prisma client mock_db_client.jsonify_team_object = lambda db_data: db_data mock_db_client.db = MagicMock() - mock_db_client.db.litellm_proxymodeltable.find_many = AsyncMock(return_value=[ - SimpleNamespace(model_id="weighted-id", model_name="group", model_info={}) - ]) + mock_db_client.db.litellm_proxymodeltable.find_many = AsyncMock( + return_value=[SimpleNamespace(model_id="weighted-id", model_name="group", model_info={})] + ) # Mock existing team row existing_team_mock = MagicMock() @@ -10564,9 +10085,7 @@ async def test_get_team_daily_activity_non_admin_filters_by_user_api_keys( # Create a non-admin user user_id = "test_user_123" team_id = "test_team_456" - user_api_key_dict = UserAPIKeyAuth( - user_id=user_id, user_role=LitellmUserRoles.INTERNAL_USER - ) + user_api_key_dict = UserAPIKeyAuth(user_id=user_id, user_role=LitellmUserRoles.INTERNAL_USER) # Mock user info mock_user_info = LiteLLM_UserTable( @@ -10598,9 +10117,7 @@ async def test_get_team_daily_activity_non_admin_filters_by_user_api_keys( # Setup mocks mock_db_client.db.litellm_teamtable.find_many = AsyncMock(return_value=[mock_team]) - mock_db_client.db.litellm_verificationtoken.find_many = AsyncMock( - return_value=[user_api_key_1, user_api_key_2] - ) + mock_db_client.db.litellm_verificationtoken.find_many = AsyncMock(return_value=[user_api_key_1, user_api_key_2]) # Mock get_user_object with patch( @@ -10637,9 +10154,7 @@ async def test_get_team_daily_activity_non_admin_filters_by_user_api_keys( # Verify user's API keys were fetched mock_db_client.db.litellm_verificationtoken.find_many.assert_called_once() - api_key_call_kwargs = ( - mock_db_client.db.litellm_verificationtoken.find_many.call_args[1] - ) + api_key_call_kwargs = mock_db_client.db.litellm_verificationtoken.find_many.call_args[1] assert api_key_call_kwargs["where"] == {"user_id": user_id} @@ -10656,9 +10171,7 @@ async def test_get_team_daily_activity_team_admin_sees_all_spend(mock_db_client) # Create a team admin user user_id = "test_admin_123" team_id = "test_team_456" - user_api_key_dict = UserAPIKeyAuth( - user_id=user_id, user_role=LitellmUserRoles.INTERNAL_USER - ) + user_api_key_dict = UserAPIKeyAuth(user_id=user_id, user_role=LitellmUserRoles.INTERNAL_USER) # Mock user info mock_user_info = LiteLLM_UserTable( @@ -10783,9 +10296,7 @@ async def test_validate_and_populate_member_user_info_only_email_provided(): mock_user_find_first.user_email = "test@example.com" # Mock find_first to return the user - mock_prisma_client.db.litellm_usertable.find_first = AsyncMock( - return_value=mock_user_find_first - ) + mock_prisma_client.db.litellm_usertable.find_first = AsyncMock(return_value=mock_user_find_first) # Mock get_data to return single user (no duplicates) mock_prisma_client.get_data = AsyncMock(return_value=[mock_user_find_first]) @@ -10841,9 +10352,7 @@ async def test_validate_and_populate_member_user_info_only_user_id_not_found(): assert result.role == "user" # Verify find_unique was called with correct parameters - mock_prisma_client.db.litellm_usertable.find_unique.assert_called_once_with( - where={"user_id": "nonexistent-user"} - ) + mock_prisma_client.db.litellm_usertable.find_unique.assert_called_once_with(where={"user_id": "nonexistent-user"}) @pytest.mark.asyncio @@ -10939,9 +10448,7 @@ async def test_list_team_v1_batches_key_queries(): return [key3] return [key1, key2, key3] - mock_prisma_client.db.litellm_verificationtoken.find_many = AsyncMock( - side_effect=filtered_find_many - ) + mock_prisma_client.db.litellm_verificationtoken.find_many = AsyncMock(side_effect=filtered_find_many) result = await list_team( http_request=mock_request, @@ -11010,8 +10517,6 @@ async def test_create_team_member_budget_table_with_duration(): assert result["metadata"]["team_member_budget_id"] == "budget-abc" - - @pytest.mark.asyncio async def test_update_team_clears_explicitly_null_member_model_budget_and_evicts_cache( disable_audit_logging_for_mocked_team, @@ -11056,7 +10561,7 @@ async def test_update_team_clears_explicitly_null_member_model_budget_and_evicts budget_upsert: Final = prisma.db.litellm_budgettable.upsert.call_args.kwargs assert budget_upsert["where"]["budget_id"] == "member-budget-1" - assert budget_upsert["data"]["update"]["model_max_budget"] == "{}" + assert budget_upsert["data"]["update"]["model_max_budget"].data == {} assert cache.async_delete_cache.await_args_list == [call(key="team_member_default_budget:member-budget-1")] @@ -11104,7 +10609,7 @@ async def test_update_team_persists_member_model_budget_to_default_budget_row( budget_upsert: Final = prisma.db.litellm_budgettable.upsert.call_args.kwargs assert budget_upsert["where"]["budget_id"] == "member-budget-1" - stored_member_model_caps: Final = json.loads(budget_upsert["data"]["update"]["model_max_budget"]) + stored_member_model_caps: Final = budget_upsert["data"]["update"]["model_max_budget"].data assert { model: { **budget, @@ -11154,9 +10659,7 @@ class TestBatchResolveAccessGroupResources: fake_row.access_agent_ids = ["agent-1", "agent-2"] fake_prisma = MagicMock() - fake_prisma.db.litellm_accessgrouptable.find_many = AsyncMock( - return_value=[fake_row] - ) + fake_prisma.db.litellm_accessgrouptable.find_many = AsyncMock(return_value=[fake_row]) with patch("litellm.proxy.proxy_server.prisma_client", fake_prisma): result = await _batch_resolve_access_group_resources(["ag-1"]) @@ -11185,9 +10688,7 @@ class TestBatchResolveAccessGroupResources: row2.access_agent_ids = ["agent-2"] fake_prisma = MagicMock() - fake_prisma.db.litellm_accessgrouptable.find_many = AsyncMock( - return_value=[row1, row2] - ) + fake_prisma.db.litellm_accessgrouptable.find_many = AsyncMock(return_value=[row1, row2]) with patch("litellm.proxy.proxy_server.prisma_client", fake_prisma): result = await _batch_resolve_access_group_resources(["ag-1", "ag-2"]) @@ -11209,9 +10710,7 @@ class TestBatchResolveAccessGroupResources: row1.access_agent_ids = [] fake_prisma = MagicMock() - fake_prisma.db.litellm_accessgrouptable.find_many = AsyncMock( - return_value=[row1] - ) + fake_prisma.db.litellm_accessgrouptable.find_many = AsyncMock(return_value=[row1]) with patch("litellm.proxy.proxy_server.prisma_client", fake_prisma): result = await _batch_resolve_access_group_resources(["ag-1", "ag-missing"]) @@ -11249,9 +10748,7 @@ class TestBatchResolveAccessGroupResources: fake_prisma.db.litellm_accessgrouptable.find_many = fake_find_many with patch("litellm.proxy.proxy_server.prisma_client", fake_prisma): - result = await _batch_resolve_access_group_resources( - ["ag-1", "ag-1", "ag-1"] - ) + result = await _batch_resolve_access_group_resources(["ag-1", "ag-1", "ag-1"]) # Should have been called with deduplicated list call_args = fake_find_many.call_args @@ -11290,9 +10787,7 @@ class TestResolveTeamAccessGroupResources: row2.access_skill_names = ["shared-plugin"] fake_prisma = MagicMock() - fake_prisma.db.litellm_accessgrouptable.find_many = AsyncMock( - return_value=[row1, row2] - ) + fake_prisma.db.litellm_accessgrouptable.find_many = AsyncMock(return_value=[row1, row2]) team_info = TeamInfoResponseObjectTeamTable( team_id="team-1", access_group_ids=["ag-1", "ag-2", "ag-1", "ag-missing"] @@ -11396,9 +10891,7 @@ async def test_update_team_rejects_unauthorized_caller(): ], "organization_id": "org-456", } - mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock( - return_value=mock_existing_team - ) + mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team) update_request = UpdateTeamRequest( team_id="team-123", @@ -11480,9 +10973,7 @@ async def test_team_member_me_returns_caller_membership(mock_db_client): team_id = "team-me-1" caller_id = "alice@example.com" other_id = "bob@example.com" - caller_auth = UserAPIKeyAuth( - user_role=LitellmUserRoles.INTERNAL_USER, user_id=caller_id - ) + caller_auth = UserAPIKeyAuth(user_role=LitellmUserRoles.INTERNAL_USER, user_id=caller_id) team = _build_team_for_me( team_id, @@ -11494,9 +10985,7 @@ async def test_team_member_me_returns_caller_membership(mock_db_client): membership = _build_membership_for_me(caller_id, team_id, spend=42.0) user = LiteLLM_UserTable(user_id=caller_id, user_email=caller_id, max_budget=None) - p_team, p_membership, p_user = _patch_member_me_helpers( - team=team, membership=membership, user=user - ) + p_team, p_membership, p_user = _patch_member_me_helpers(team=team, membership=membership, user=user) with p_team, p_membership as mock_get_membership, p_user: response = await team_member_me( http_request=MagicMock(spec=Request), @@ -11514,9 +11003,7 @@ async def test_team_member_me_returns_caller_membership(mock_db_client): # budget_reset_at must survive end-to-end — proves the BudgetTableFull # variant of the Union is selected (created_at is present), not the base # LiteLLM_BudgetTable which would silently strip this field. - assert response.litellm_budget_table.budget_reset_at == datetime( - 2026, 5, 1, tzinfo=timezone.utc - ) + assert response.litellm_budget_table.budget_reset_at == datetime(2026, 5, 1, tzinfo=timezone.utc) # Membership lookup must scope to caller_id, not just team_id — proves the # endpoint cannot return another member's row. @@ -11551,13 +11038,9 @@ async def test_team_member_me_matches_email_only_member(mock_db_client): [{"user_id": None, "user_email": caller_email, "role": "user"}], ) membership = _build_membership_for_me(caller_id, team_id, spend=7.0) - user = LiteLLM_UserTable( - user_id=caller_id, user_email=caller_email, max_budget=None - ) + user = LiteLLM_UserTable(user_id=caller_id, user_email=caller_email, max_budget=None) - p_team, p_membership, p_user = _patch_member_me_helpers( - team=team, membership=membership, user=user - ) + p_team, p_membership, p_user = _patch_member_me_helpers(team=team, membership=membership, user=user) with p_team, p_membership, p_user: response = await team_member_me( http_request=MagicMock(spec=Request), @@ -11579,9 +11062,7 @@ async def test_team_member_me_returns_404_for_non_member(mock_db_client): team_id = "team-me-2" caller_id = "outsider@example.com" - caller_auth = UserAPIKeyAuth( - user_role=LitellmUserRoles.INTERNAL_USER, user_id=caller_id - ) + caller_auth = UserAPIKeyAuth(user_role=LitellmUserRoles.INTERNAL_USER, user_id=caller_id) team = _build_team_for_me( team_id, @@ -11600,9 +11081,7 @@ async def test_team_member_me_returns_404_for_non_member(mock_db_client): @pytest.mark.asyncio -async def test_team_member_me_returns_404_for_proxy_admin_not_in_team( - mock_db_client, mock_admin_auth -): +async def test_team_member_me_returns_404_for_proxy_admin_not_in_team(mock_db_client, mock_admin_auth): """ Proxy admins get 404 if they are not actually a member of the team. `me` only resolves for actual team members; admins use /team/info instead. @@ -11642,9 +11121,7 @@ async def test_team_member_me_returns_defaults_when_no_membership_row(mock_db_cl team_id = "team-me-4" caller_id = "newmember@example.com" - caller_auth = UserAPIKeyAuth( - user_role=LitellmUserRoles.INTERNAL_USER, user_id=caller_id - ) + caller_auth = UserAPIKeyAuth(user_role=LitellmUserRoles.INTERNAL_USER, user_id=caller_id) team = _build_team_for_me( team_id, @@ -11690,18 +11167,12 @@ async def test_team_member_me_returns_404_for_unknown_team(mock_db_client): from litellm.proxy.management_endpoints.team_endpoints import team_member_me - caller_auth = UserAPIKeyAuth( - user_role=LitellmUserRoles.INTERNAL_USER, user_id="alice@example.com" - ) + caller_auth = UserAPIKeyAuth(user_role=LitellmUserRoles.INTERNAL_USER, user_id="alice@example.com") # get_team_object raises 404 directly when the team is missing. with patch( "litellm.proxy.management_endpoints.team_endpoints.get_team_object", - AsyncMock( - side_effect=HTTPException( - status_code=404, detail={"error": "Team doesn't exist in db."} - ) - ), + AsyncMock(side_effect=HTTPException(status_code=404, detail={"error": "Team doesn't exist in db."})), ): with pytest.raises(HTTPException) as exc_info: await team_member_me( @@ -11713,9 +11184,7 @@ async def test_team_member_me_returns_404_for_unknown_team(mock_db_client): @pytest.mark.asyncio -async def test_new_team_encrypts_callback_vars( - mock_db_client, mock_admin_auth, monkeypatch -): +async def test_new_team_encrypts_callback_vars(mock_db_client, mock_admin_auth, monkeypatch): """/team/new must encrypt callback_vars values before they reach the DB.""" from fastapi import Request @@ -11730,9 +11199,7 @@ async def test_new_team_encrypts_callback_vars( # actual JSON serialization production uses (catches non-serializable # ciphertext, missing fields, etc.). mock_db_client.jsonify_object = PrismaClient.jsonify_object.__get__(mock_db_client) - mock_db_client.jsonify_team_object = PrismaClient.jsonify_team_object.__get__( - mock_db_client - ) + mock_db_client.jsonify_team_object = PrismaClient.jsonify_team_object.__get__(mock_db_client) mock_db_client.get_data = AsyncMock(return_value=None) mock_db_client.db = MagicMock() mock_db_client.db.litellm_teamtable = MagicMock() @@ -11742,9 +11209,7 @@ async def test_new_team_encrypts_callback_vars( mock_db_client.db.litellm_teamtable.create = mock_team_create _wire_team_create_tx(mock_db_client) mock_db_client.db.litellm_teamtable.count = AsyncMock(return_value=0) - mock_db_client.db.litellm_teamtable.update = AsyncMock( - return_value=team_create_result - ) + mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=team_create_result) mock_db_client.db.litellm_usertable = MagicMock() mock_db_client.db.litellm_usertable.update = AsyncMock(return_value=MagicMock()) @@ -11781,9 +11246,7 @@ async def test_new_team_encrypts_callback_vars( def _non_admin_auth(): - return UserAPIKeyAuth( - user_id="u-team-admin", user_role=LitellmUserRoles.INTERNAL_USER - ) + return UserAPIKeyAuth(user_id="u-team-admin", user_role=LitellmUserRoles.INTERNAL_USER) def test_check_passthrough_routes_caller_permission_team(): @@ -11799,12 +11262,8 @@ def test_check_passthrough_routes_caller_permission_team(): NewTeamRequest(allowed_passthrough_routes=["/foo/*"]), admin, entity="team" ) - check_passthrough_routes_caller_permission( - NewTeamRequest(), non_admin, entity="team" - ) - check_passthrough_routes_caller_permission( - NewTeamRequest(allowed_passthrough_routes=[]), non_admin, entity="team" - ) + check_passthrough_routes_caller_permission(NewTeamRequest(), non_admin, entity="team") + check_passthrough_routes_caller_permission(NewTeamRequest(allowed_passthrough_routes=[]), non_admin, entity="team") with pytest.raises(HTTPException) as exc: check_passthrough_routes_caller_permission( @@ -11841,9 +11300,7 @@ async def test_new_team_blocks_non_admin_passthrough_routes(mock_db_client): ): with pytest.raises(ProxyException) as exc: await new_team( - data=NewTeamRequest( - team_alias="t", allowed_passthrough_routes=["/admin/*"] - ), + data=NewTeamRequest(team_alias="t", allowed_passthrough_routes=["/admin/*"]), http_request=MagicMock(spec=Request), user_api_key_dict=_non_admin_auth(), ) @@ -11867,9 +11324,7 @@ async def test_update_team_blocks_non_admin_passthrough_routes(mock_db_client): with _org_admins(("u-team-admin", "org-1")): with pytest.raises(ProxyException) as exc: await update_team( - data=UpdateTeamRequest( - team_id="t1", allowed_passthrough_routes=["/admin/*"] - ), + data=UpdateTeamRequest(team_id="t1", allowed_passthrough_routes=["/admin/*"]), http_request=MagicMock(spec=Request), user_api_key_dict=_non_admin_auth(), ) @@ -12142,14 +11597,6 @@ def test_set_budget_reset_at_sets_value_when_budget_duration_provided(): assert updated_kv["budget_reset_at"] is not None - - - - - - - - @pytest.mark.asyncio async def test_team_member_budget_duration_not_sent_does_not_update(): """ @@ -12181,8 +11628,6 @@ async def test_team_member_budget_duration_not_sent_does_not_update(): assert "team_member_budget" not in updated_kv - - @pytest.mark.asyncio async def test_team_info_forwards_key_limit_to_get_data(): """/team/info must thread its ``key_limit`` query param into the key @@ -12193,16 +11638,12 @@ async def test_team_info_forwards_key_limit_to_get_data(): from litellm.proxy.management_endpoints import team_endpoints mock_prisma = MagicMock() - mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( - return_value=LiteLLM_TeamTable(team_id="team-1") - ) + mock_prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=LiteLLM_TeamTable(team_id="team-1")) mock_prisma.get_data = AsyncMock(return_value=[]) with ( patch("litellm.proxy.proxy_server.prisma_client", mock_prisma), - patch.object( - team_endpoints, "get_all_team_memberships", AsyncMock(return_value=[]) - ), + patch.object(team_endpoints, "get_all_team_memberships", AsyncMock(return_value=[])), ): await team_endpoints.team_info( http_request=MagicMock(spec=Request), @@ -12240,9 +11681,7 @@ async def test_team_info_returns_model_aliases(): with ( patch("litellm.proxy.proxy_server.prisma_client", mock_prisma), - patch.object( - team_endpoints, "get_all_team_memberships", AsyncMock(return_value=[]) - ), + patch.object(team_endpoints, "get_all_team_memberships", AsyncMock(return_value=[])), ): response = await team_endpoints.team_info( http_request=MagicMock(spec=Request), @@ -12321,12 +11760,8 @@ async def test_update_model_table_clears_aliases_with_empty_map(): """ mock_prisma = MagicMock() mock_prisma.db.litellm_modeltable.create = AsyncMock() - mock_prisma.db.litellm_modeltable.upsert = AsyncMock( - return_value=MagicMock(id="model-123") - ) - user_api_key_dict = UserAPIKeyAuth( - user_role=LitellmUserRoles.PROXY_ADMIN, user_id="admin" - ) + mock_prisma.db.litellm_modeltable.upsert = AsyncMock(return_value=MagicMock(id="model-123")) + user_api_key_dict = UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN, user_id="admin") returned_model_id = await _update_model_table( data=UpdateTeamRequest(team_id="team-1", model_aliases={}), @@ -12374,9 +11809,7 @@ class TestEmitTeamMembersMetric: return LiteLLM_TeamTable( team_id="team-x", team_alias="X", - members_with_roles=[ - Member(user_id=f"u{i}", role="user") for i in range(member_count) - ], + members_with_roles=[Member(user_id=f"u{i}", role="user") for i in range(member_count)], ) def test_emits_with_team_when_logger_registered(self, restore_callbacks): @@ -12450,9 +11883,7 @@ async def test_new_team_rejects_reserved_ui_session_team_id(): await new_team( data=team_request, http_request=dummy_request, - user_api_key_dict=UserAPIKeyAuth( - user_role=LitellmUserRoles.PROXY_ADMIN - ), + user_api_key_dict=UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN), ) assert exc_info.value.code == "400" @@ -12540,9 +11971,7 @@ async def _drive_team_write( new=AsyncMock(), ), ): - pc.db.litellm_teamtable.find_unique = AsyncMock( - return_value=None if find_returns_none else existing - ) + pc.db.litellm_teamtable.find_unique = AsyncMock(return_value=None if find_returns_none else existing) pc.db.litellm_teamtable.update = AsyncMock( return_value=LiteLLM_TeamTable(team_id=_PATCH_TEAM_ID, team_alias="t") ) @@ -12644,9 +12073,7 @@ _METADATA_MAPPING = [ _METADATA_MAPPING, ids=[row[0] for row in _METADATA_MAPPING], ) -async def test_post_vs_patch_metadata_write_mapping( - label, existing_metadata, body, expected_post, expected_patch -): +async def test_post_vs_patch_metadata_write_mapping(label, existing_metadata, body, expected_post, expected_patch): """Exhaustive map: POST replaces metadata wholesale, PATCH merges per RFC 7386.""" post_meta = await _written_metadata("post", existing_metadata, body) patch_meta = await _written_metadata("patch", existing_metadata, body) @@ -12838,9 +12265,7 @@ async def test_patch_team_not_found_returns_404(): # metadata present -> patch_team does its own existence check with pytest.raises(ProxyException) as exc: - await _drive_team_write( - "patch", raw_body={"metadata": {"cost_center": "1"}}, find_returns_none=True - ) + await _drive_team_write("patch", raw_body={"metadata": {"cost_center": "1"}}, find_returns_none=True) assert exc.value.code == "404" or exc.value.code == 404 # metadata absent -> existence check happens in the delegated update_team @@ -12857,9 +12282,7 @@ async def test_patch_enforces_team_access_via_delegation(): outsider = UserAPIKeyAuth(user_role=LitellmUserRoles.INTERNAL_USER, user_id="outsider") with pytest.raises(ProxyException) as exc: - await _drive_team_write( - "patch", raw_body={"tpm_limit": 5}, user=outsider - ) + await _drive_team_write("patch", raw_body={"tpm_limit": 5}, user=outsider) assert exc.value.code == "403" or exc.value.code == 403 @@ -12869,9 +12292,7 @@ async def test_patch_returns_full_team_object_not_wrapper(): {"team_id", "data"} envelope.""" from litellm.proxy._types import LiteLLM_TeamTable - result, _ = await _drive_team_write( - "patch", existing_metadata={"a": 1}, raw_body={"metadata": {"b": 2}} - ) + result, _ = await _drive_team_write("patch", existing_metadata={"a": 1}, raw_body={"metadata": {"b": 2}}) assert isinstance(result, LiteLLM_TeamTable) assert result.team_id == _PATCH_TEAM_ID @@ -13188,9 +12609,7 @@ def test_patch_body_reshaping_adds_no_keys_the_caller_did_not_send(body): @pytest.mark.asyncio async def test_patch_ignores_unknown_body_keys(): """Unknown keys were silently dropped by the previous construction; keep that.""" - _, update_mock = await _drive_team_write( - "patch", raw_body={"tpm_limit": 5, "not_a_team_field": "x"} - ) + _, update_mock = await _drive_team_write("patch", raw_body={"tpm_limit": 5, "not_a_team_field": "x"}) written = update_mock.call_args.kwargs["data"] assert written["tpm_limit"] == 5 @@ -14927,16 +14346,12 @@ class _FakeMirrorDb: if "array_append" in sql: self.transactions[-1].append("attach") - changed = [ - g for g in desired if g in self._access_groups and team_id not in self._team_ids(g) - ] + changed = [g for g in desired if g in self._access_groups and team_id not in self._team_ids(g)] for group_id in changed: self._team_ids(group_id).append(team_id) else: self.transactions[-1].append("detach") - changed = [ - g for g in self._access_groups if team_id in self._team_ids(g) and g not in desired - ] + changed = [g for g in self._access_groups if team_id in self._team_ids(g) and g not in desired] for group_id in changed: self._team_ids(group_id).remove(team_id) return [{"access_group_id": group_id} for group_id in changed] @@ -15186,7 +14601,9 @@ async def test_new_team_and_delete_team_both_drive_the_mirror( patch("litellm.proxy.proxy_server.prisma_client") as prisma, patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), patch("litellm.proxy.proxy_server.llm_router", None), - patch("litellm.proxy.management_endpoints.team_endpoints._persist_deleted_team_records", new_callable=AsyncMock), + patch( + "litellm.proxy.management_endpoints.team_endpoints._persist_deleted_team_records", new_callable=AsyncMock + ), patch( "litellm.proxy.management_endpoints.team_endpoints.sync_team_access_group_membership", new_callable=AsyncMock, @@ -16128,7 +15545,9 @@ async def test_team_info_reports_parent_organization_models_only_to_team_manager with ( patch("litellm.proxy.proxy_server.prisma_client", mock_prisma), # test-quality-ok: no seam on team_info - patch.object(team_endpoints, "get_all_team_memberships", AsyncMock(return_value=[])), # test-quality-ok: no seam on team_info + patch.object( + team_endpoints, "get_all_team_memberships", AsyncMock(return_value=[]) + ), # test-quality-ok: no seam on team_info _not_org_admin(), ): response = await team_endpoints.team_info( @@ -16368,7 +15787,10 @@ async def test_new_team_rejects_unenforceable_model_max_budget(mock_db_client, m mock_db_client.db.litellm_teamtable.create = AsyncMock() - with patch("litellm.proxy.proxy_server.premium_user", True), pytest.raises(ProxyException) as exc: # test-quality-ok: proxy_server module global is the endpoint's only injection point + with ( + patch("litellm.proxy.proxy_server.premium_user", True), + pytest.raises(ProxyException) as exc, + ): # test-quality-ok: proxy_server module global is the endpoint's only injection point await new_team( data=NewTeamRequest(team_alias="model-caps", model_max_budget={"gpt-4o": {"max_budget": 10.0}}), http_request=MagicMock(spec=Request), @@ -16423,10 +15845,18 @@ async def test_update_team_clearing_model_max_budget_writes_an_empty_mapping( from litellm.proxy.management_endpoints.team_endpoints import update_team with ( - patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, # test-quality-ok: proxy_server module global is the endpoint's only injection point - patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, # test-quality-ok: proxy_server module global is the endpoint's only injection point - patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), # test-quality-ok: proxy_server module global is the endpoint's only injection point - patch("litellm.proxy.proxy_server.premium_user", True), # test-quality-ok: proxy_server module global is the endpoint's only injection point + patch( + "litellm.proxy.proxy_server.prisma_client" + ) as mock_prisma, # test-quality-ok: proxy_server module global is the endpoint's only injection point + patch( + "litellm.proxy.proxy_server.user_api_key_cache" + ) as mock_cache, # test-quality-ok: proxy_server module global is the endpoint's only injection point + patch( + "litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin" + ), # test-quality-ok: proxy_server module global is the endpoint's only injection point + patch( + "litellm.proxy.proxy_server.premium_user", True + ), # test-quality-ok: proxy_server module global is the endpoint's only injection point ): mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( return_value=_existing_team_with_model_caps(_EXISTING_TEAM_MODEL_CAPS) @@ -16455,11 +15885,21 @@ async def test_update_team_model_max_budget_raise_blocked_for_team_admin(): from litellm.proxy.management_endpoints.team_endpoints import update_team with ( - patch("litellm.proxy.proxy_server.prisma_client") as mock_prisma, # test-quality-ok: proxy_server module global is the endpoint's only injection point - patch("litellm.proxy.proxy_server.user_api_key_cache") as mock_cache, # test-quality-ok: proxy_server module global is the endpoint's only injection point - patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"), # test-quality-ok: proxy_server module global is the endpoint's only injection point - patch("litellm.proxy.proxy_server.premium_user", True), # test-quality-ok: proxy_server module global is the endpoint's only injection point - patch("litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock()), # test-quality-ok: stubs the audit write so the test observes only the team update result + patch( + "litellm.proxy.proxy_server.prisma_client" + ) as mock_prisma, # test-quality-ok: proxy_server module global is the endpoint's only injection point + patch( + "litellm.proxy.proxy_server.user_api_key_cache" + ) as mock_cache, # test-quality-ok: proxy_server module global is the endpoint's only injection point + patch( + "litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin" + ), # test-quality-ok: proxy_server module global is the endpoint's only injection point + patch( + "litellm.proxy.proxy_server.premium_user", True + ), # test-quality-ok: proxy_server module global is the endpoint's only injection point + patch( + "litellm.proxy.proxy_server.create_audit_log_for_update", new=AsyncMock() + ), # test-quality-ok: stubs the audit write so the test observes only the team update result ): mock_prisma.db.litellm_teamtable.find_unique = AsyncMock( return_value=_existing_team_with_model_caps(_EXISTING_TEAM_MODEL_CAPS) @@ -16582,7 +16022,9 @@ async def test_update_team_configured_but_unsupported_field_does_not_open_editin with contextlib.ExitStack() as stack: prisma = _wire_update_team(stack, {}) stack.enter_context( - patch("litellm.proxy.proxy_server.general_settings", {"team_admin_editable_team_fields": ["team_alias"]}) # test-quality-ok: update_team reads general_settings as a proxy_server module global + patch( + "litellm.proxy.proxy_server.general_settings", {"team_admin_editable_team_fields": ["team_alias"]} + ) # test-quality-ok: update_team reads general_settings as a proxy_server module global ) with pytest.raises(ProxyException) as exc: await update_team( @@ -16726,7 +16168,9 @@ async def test_update_team_team_admin_resending_stored_member_defaults_is_not_an ) ) stack.enter_context( - patch("litellm.proxy.proxy_server.general_settings", {"team_admin_editable_team_fields": ["tpm_limit"]}) # test-quality-ok: update_team reads general_settings as a proxy_server module global + patch( + "litellm.proxy.proxy_server.general_settings", {"team_admin_editable_team_fields": ["tpm_limit"]} + ) # test-quality-ok: update_team reads general_settings as a proxy_server module global ) result = await update_team( data=UpdateTeamRequest( @@ -16756,7 +16200,9 @@ async def test_update_team_team_admin_changes_tpm_limit_once_a_proxy_admin_enabl with contextlib.ExitStack() as stack: prisma = _wire_update_team(stack, {}) stack.enter_context( - patch("litellm.proxy.proxy_server.general_settings", {"team_admin_editable_team_fields": ["tpm_limit"]}) # test-quality-ok: update_team reads general_settings as a proxy_server module global + patch( + "litellm.proxy.proxy_server.general_settings", {"team_admin_editable_team_fields": ["tpm_limit"]} + ) # test-quality-ok: update_team reads general_settings as a proxy_server module global ) await update_team( data=UpdateTeamRequest(team_id="test_team_id", tpm_limit=5000), @@ -16826,8 +16272,10 @@ async def test_update_team_holds_a_team_admin_to_the_org_tpm_limit(disable_audit ) async def org_lookup(**kwargs): - return capped_org if kwargs.get("include_budget_table") else capped_org.model_copy( - update={"litellm_budget_table": None} + return ( + capped_org + if kwargs.get("include_budget_table") + else capped_org.model_copy(update={"litellm_budget_table": None}) ) org_team = MagicMock() @@ -17291,7 +16739,9 @@ async def test_team_info_reports_what_the_caller_may_edit(caller, org_admin, ena with ( patch("litellm.proxy.proxy_server.prisma_client", mock_prisma), # test-quality-ok: no seam on team_info - patch.object(team_endpoints, "get_all_team_memberships", AsyncMock(return_value=[])), # test-quality-ok: no seam on team_info + patch.object( + team_endpoints, "get_all_team_memberships", AsyncMock(return_value=[]) + ), # test-quality-ok: no seam on team_info _org_admins(("admin-1", "org-1")) if org_admin else _not_org_admin(), _team_admin_may_edit(*enabled_fields), ): @@ -17362,7 +16812,9 @@ def test_member_budget_patch_stores_model_max_budget_as_json_and_clears_on_empty user_id="user-1", model_max_budget={"gpt-4o": {"budget_limit": 5, "time_period": "30d"}}, ) - assert member_budget_patch(request) == {"model_max_budget": {"gpt-4o": {"max_budget": 5.0, "budget_duration": "30d"}}} + assert member_budget_patch(request) == { + "model_max_budget": {"gpt-4o": {"max_budget": 5.0, "budget_duration": "30d"}} + } cleared: Final = TeamMemberUpdateRequest(team_id="team-1", user_id="user-1", model_max_budget={}) assert member_budget_patch(cleared) == {"model_max_budget": {}} @@ -17728,7 +17180,7 @@ async def test_default_budget_cache_eviction_does_not_fail_after_redis_error() - user_api_key_cache = MagicMock() user_api_key_cache.async_delete_cache = AsyncMock(side_effect=RuntimeError("redis unavailable")) - await TeamMemberBudgetHandler._evict_default_budget_cache( + await TeamMemberBudgetHandler.evict_default_budget_cache( budget_id="budget_456", user_api_key_cache=user_api_key_cache, ) diff --git a/tests/unit/proxy/management_helpers/test_team_member_default_budget.py b/tests/unit/proxy/management_helpers/test_team_member_default_budget.py index a3a7fa5dcce..3fd741429a5 100644 --- a/tests/unit/proxy/management_helpers/test_team_member_default_budget.py +++ b/tests/unit/proxy/management_helpers/test_team_member_default_budget.py @@ -43,7 +43,8 @@ def test_a_first_default_gets_a_new_row_that_inherits_the_team_reset_period(): assert write.budget_id.startswith("team-My-Team-budget-") assert write.create["max_budget"] == 25.0 assert write.create["budget_duration"] == "30d" - assert write.create["budget_reset_at"] > before + reset_at = write.create["budget_reset_at"] + assert isinstance(reset_at, datetime) and reset_at > before assert write.links_members is True @@ -63,7 +64,8 @@ def test_changing_the_reset_period_restarts_the_window(): assert write is not None assert write.update["budget_duration"] == "7d" - assert write.update["budget_reset_at"] > before + reset_at = write.update["budget_reset_at"] + assert isinstance(reset_at, datetime) and reset_at > before @pytest.mark.parametrize("team_member_budget", [None, 0.0]) diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts index ae1b8fab924..7a3804d18fb 100644 --- a/ui/litellm-dashboard/src/lib/http/schema.d.ts +++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts @@ -17314,6 +17314,7 @@ export interface paths { * - team_member_model_max_budget: Optional[dict] - Per-model budgets shared by team members without an override. * - team_member_rpm_limit: Optional[int] - The RPM (Requests Per Minute) limit for individual team members. * - team_member_tpm_limit: Optional[int] - The TPM (Tokens Per Minute) limit for individual team members. + * - team_member_budget_backfill: Optional[str] - Moves members with their own budget onto the team_member_budget sent in the same request: "raise_to_default" (those below it), "lower_to_default" (those above it), "all_to_default" (both) or "none". Members on a different reset schedule are skipped, and the response's team_member_budget_backfill_result counts who moved. * - team_member_key_duration: Optional[str] - The duration for a team member's key. e.g. "1d", "1w", "1mo" * - allowed_passthrough_routes: Optional[List[str]] - List of allowed pass through routes for the team. * - denied_passthrough_routes: Optional[List[str]] - List of pass through routes the team's keys may not call, even if allowed. Applies together with each key's `denied_passthrough_routes`.