mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-29 01:03:17 +00:00
* refactor(auth_cache): type auth cache keys and keep the cache always native Reads and writes of the auth cache now name an AuthObject, whose variant picks the memory partition in place of the 64-hex rule. Evictions keep wire keys, because main's spellings cannot be parsed back UserApiKeyCache is always on the native store, memory only until enable_redis_auth_cache attaches Redis, and is no longer a DualCache. Without Redis a read answers from memory without suspending the caller, and it reuses the model a previous read of the same payload validated, as the Python memory path did * test: use typed auth objects in reservation ledger scenarios * test: migrate spend ledger cache seeds to auth objects * test(audit): avoid publicly known master key prefix in fixture --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
53 lines
2.1 KiB
Python
53 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
from typing import Final
|
|
|
|
import pytest
|
|
|
|
from litellm.proxy._types import LiteLLM_OrganizationMembershipTable, LiteLLM_UserTable, LitellmUserRoles
|
|
from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache
|
|
from litellm.proxy.management.users.service import PrismaOrgRoles, holds_org_admin
|
|
from litellm.proxy.utils import ProxyLogging
|
|
from litellm.redis.stores.auth_cache import AuthObject
|
|
|
|
NOW: Final = datetime.now(timezone.utc)
|
|
|
|
|
|
def user_in(*memberships: tuple[str, str]) -> LiteLLM_UserTable:
|
|
return LiteLLM_UserTable(
|
|
user_id="u1",
|
|
organization_memberships=[
|
|
LiteLLM_OrganizationMembershipTable(
|
|
user_id="u1", organization_id=organization_id, user_role=role, created_at=NOW, updated_at=NOW
|
|
)
|
|
for organization_id, role in memberships
|
|
],
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("user", "expected"),
|
|
[
|
|
(user_in(("org-1", LitellmUserRoles.ORG_ADMIN.value)), True),
|
|
(user_in(("org-2", LitellmUserRoles.ORG_ADMIN.value)), False),
|
|
(user_in(("org-1", LitellmUserRoles.INTERNAL_USER.value)), False),
|
|
(user_in(("org-2", LitellmUserRoles.ORG_ADMIN.value), ("org-1", LitellmUserRoles.ORG_ADMIN.value)), True),
|
|
(user_in(), False),
|
|
(LiteLLM_UserTable(user_id="u1", organization_memberships=None), False),
|
|
(None, False),
|
|
],
|
|
)
|
|
def test_holds_org_admin_needs_the_org_admin_role_in_that_org(user: LiteLLM_UserTable | None, expected: bool) -> None:
|
|
assert holds_org_admin(user, "org-1") is expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("organization_id", "expected"),
|
|
[("org-1", True), ("org-2", False)],
|
|
)
|
|
async def test_prisma_org_roles_answers_from_the_cached_user_row(organization_id: str, expected: bool) -> None:
|
|
cache: Final = UserApiKeyCache()
|
|
await cache.async_set_cache(key=AuthObject.user("u1"), value=user_in(("org-1", LitellmUserRoles.ORG_ADMIN.value)))
|
|
roles: Final = PrismaOrgRoles(None, cache, ProxyLogging(user_api_key_cache=UserApiKeyCache()))
|
|
assert await roles.is_org_admin("u1", organization_id) is expected
|