Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Deleting a user from team deletes key user created for team (#17057)

yuneng-jiang3f5a34d72c12
diff --git a/litellm/proxy/management_endpoints/team_endpoints.py b/litellm/proxy/management_endpoints/team_endpoints.py
index 3a66f95e812..6d4faae5fd8 100644
--- a/litellm/proxy/management_endpoints/team_endpoints.py
+++ b/litellm/proxy/management_endpoints/team_endpoints.py
@@ -1878,6 +1878,15 @@ async def team_member_delete(
where={"team_id": data.team_id, "user_id": _uid}
)
+ ## DELETE KEYS CREATED BY USER FOR THIS TEAM
+ if user_ids_to_delete:
+ await prisma_client.db.litellm_verificationtoken.delete_many(
+ where={
+ "user_id": {"in": list(user_ids_to_delete)},
+ "team_id": data.team_id,
+ }
+ )
+
return existing_team_row
diff --git a/tests/test_litellm/proxy/management_endpoints/test_team_endpoints.py b/tests/test_litellm/proxy/management_endpoints/test_team_endpoints.py
index c9b7e057904..86b23c98ba5 100644
--- a/tests/test_litellm/proxy/management_endpoints/test_team_endpoints.py
+++ b/tests/test_litellm/proxy/management_endpoints/test_team_endpoints.py
@@ -1783,6 +1783,10 @@ async def test_team_member_delete_cleans_membership(mock_db_client, mock_admin_a
mock_db_client.db.litellm_teammembership = 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.delete_many = AsyncMock(return_value=MagicMock())
+
# Execute
await team_member_delete(
data=TeamMemberDeleteRequest(team_id=test_team_id, user_id=test_user_id),
@@ -1795,6 +1799,54 @@ async def test_team_member_delete_cleans_membership(mock_db_client, mock_admin_a
)
+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
+
+ test_team_id = "team-del-tokens-123"
+ test_user_id = "[email protected]"
+
+ 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"}
+ ],
+ "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.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.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_verificationtoken = MagicMock()
+ mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock(return_value=MagicMock())
+
+ await team_member_delete(
+ data=TeamMemberDeleteRequest(team_id=test_team_id, user_id=test_user_id),
+ user_api_key_dict=mock_admin_auth,
+ )
+
+ mock_db_client.db.litellm_verificationtoken.delete_many.assert_awaited_once_with(
+ where={
+ "user_id": {"in": [test_user_id]},
+ "team_id": test_team_id,
+ }
+ )
+
+
@pytest.mark.asyncio
async def test_new_team_max_budget_exceeds_user_max_budget():
"""