Skip to content

Commit 22fd323

Browse files
authored
Calling team/permissions_list and team/permissions_update now returns 404 with non-existent team (#16835)
1 parent 4fb9e33 commit 22fd323

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

litellm/proxy/auth/auth_checks.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import time
1414
from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Union, cast
1515

16-
from fastapi import Request, status
16+
from fastapi import HTTPException, Request, status
1717
from pydantic import BaseModel
1818

1919
import litellm
@@ -1274,7 +1274,7 @@ async def get_team_object(
12741274
- if not, then raise an error
12751275
12761276
Raises:
1277-
- Exception: If team doesn't exist in db or cache
1277+
- HTTPException: If team doesn't exist in db or cache (status_code=404)
12781278
"""
12791279
if prisma_client is None:
12801280
raise Exception(
@@ -1296,8 +1296,11 @@ async def get_team_object(
12961296
return cached_team_obj
12971297

12981298
if check_cache_only:
1299-
raise Exception(
1300-
f"Team doesn't exist in cache + check_cache_only=True. Team={team_id}."
1299+
raise HTTPException(
1300+
status_code=404,
1301+
detail={
1302+
"error": f"Team doesn't exist in cache + check_cache_only=True. Team={team_id}."
1303+
},
13011304
)
13021305

13031306
# else, check db
@@ -1313,8 +1316,11 @@ async def get_team_object(
13131316
team_id_upsert=team_id_upsert,
13141317
)
13151318
except Exception:
1316-
raise Exception(
1317-
f"Team doesn't exist in db. Team={team_id}. Create team via `/team/new` call."
1319+
raise HTTPException(
1320+
status_code=404,
1321+
detail={
1322+
"error": f"Team doesn't exist in db. Team={team_id}. Create team via `/team/new` call."
1323+
},
13181324
)
13191325

13201326

litellm/proxy/management_endpoints/team_endpoints.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3247,11 +3247,6 @@ async def team_member_permissions(
32473247
check_cache_only=False,
32483248
check_db_only=True,
32493249
)
3250-
if existing_team_row is None:
3251-
raise HTTPException(
3252-
status_code=404,
3253-
detail={"error": f"Team not found for team_id={team_id}"},
3254-
)
32553250

32563251
complete_team_data = LiteLLM_TeamTable(**existing_team_row.model_dump())
32573252

@@ -3320,11 +3315,6 @@ async def update_team_member_permissions(
33203315
check_cache_only=False,
33213316
check_db_only=True,
33223317
)
3323-
if existing_team_row is None:
3324-
raise HTTPException(
3325-
status_code=404,
3326-
detail={"error": f"Team not found for team_id={data.team_id}"},
3327-
)
33283318

33293319
complete_team_data = LiteLLM_TeamTable(**existing_team_row.model_dump())
33303320

tests/proxy_unit_tests/test_proxy_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,8 @@ async def test_get_team_redis(client_no_auth):
953953

954954
redis_cache = RedisCache()
955955

956+
from fastapi import HTTPException
957+
956958
with patch.object(
957959
redis_cache,
958960
"async_get_cache",
@@ -966,7 +968,7 @@ async def test_get_team_redis(client_no_auth):
966968
proxy_logging_obj=proxy_logging_obj,
967969
prisma_client=AsyncMock(),
968970
)
969-
except Exception as e:
971+
except HTTPException:
970972
pass
971973

972974
mock_client.assert_called_once()

tests/test_litellm/proxy/auth/test_auth_checks.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,3 +705,30 @@ async def mock_get_cache(key):
705705
assert "tag:uncached-1" in cached_keys
706706
assert "tag:uncached-2" in cached_keys
707707
assert "tag:uncached-3" in cached_keys
708+
709+
710+
@pytest.mark.asyncio
711+
async def test_get_team_object_raises_404_when_not_found():
712+
from litellm.proxy.auth.auth_checks import get_team_object
713+
from fastapi import HTTPException
714+
from unittest.mock import AsyncMock, MagicMock
715+
716+
mock_prisma_client = MagicMock()
717+
mock_db = AsyncMock()
718+
mock_prisma_client.db = mock_db
719+
mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=None)
720+
721+
mock_cache = MagicMock()
722+
mock_cache.async_get_cache = AsyncMock(return_value=None)
723+
724+
with pytest.raises(HTTPException) as exc_info:
725+
await get_team_object(
726+
team_id="nonexistent-team",
727+
prisma_client=mock_prisma_client,
728+
user_api_key_cache=mock_cache,
729+
check_cache_only=False,
730+
check_db_only=True,
731+
)
732+
733+
assert exc_info.value.status_code == 404
734+
assert "Team doesn't exist in db" in str(exc_info.value.detail)

0 commit comments

Comments
 (0)