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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from fastapi import Request
from litellm_enterprise.proxy.auth.user_api_key_auth import enterprise_custom_auth
from litellm.proxy._types import UserAPIKeyAuth
from tests._master_key import MASTER_KEY
@pytest.mark.asyncio
async def test_enterprise_custom_auth_none_user_auth():
# Test when user_custom_auth is None
request = MagicMock(spec=Request)
result = await enterprise_custom_auth(request, "test-api-key", None)
assert result is None
@pytest.mark.asyncio
async def test_enterprise_custom_auth_mode_on():
# Test when mode is "on"
mock_user_auth = AsyncMock(return_value={"user_id": "test-user"})
request = MagicMock(spec=Request)
with patch("litellm_enterprise.proxy.proxy_server.custom_auth_settings", {"mode": "on"}):
result = await enterprise_custom_auth(request, "test-api-key", mock_user_auth)
assert result == {"user_id": "test-user"}
mock_user_auth.assert_called_once_with(request, "test-api-key")
@pytest.mark.asyncio
async def test_enterprise_custom_auth_mode_auto_with_error():
# Test when mode is "auto" and user_auth raises an exception
mock_user_auth = AsyncMock(side_effect=Exception("Auth failed"))
request = MagicMock(spec=Request)
with patch("litellm_enterprise.proxy.proxy_server.custom_auth_settings", {"mode": "auto"}):
result = await enterprise_custom_auth(request, "test-api-key", mock_user_auth)
assert result is None
mock_user_auth.assert_called_once_with(request, "test-api-key")
@pytest.mark.asyncio
async def test_enterprise_custom_auth_returns_string():
from litellm.proxy._types import hash_token
# Test when enterprise_custom_auth returns a string (LiteLLM virtual key)
mock_user_auth = AsyncMock(return_value="sk-test-key")
request = MagicMock(spec=Request)
with (
patch(
"litellm.proxy.auth.user_api_key_auth.enterprise_custom_auth",
mock_user_auth,
),
patch("litellm.proxy.proxy_server.master_key", MASTER_KEY),
patch("litellm.proxy.proxy_server.prisma_client", MagicMock()),
):
# Verify the key is correctly handled in _user_api_key_auth_builder
with patch("litellm.proxy.auth.resolvers.store.IdentityStore._resolve_key") as mock_get_key_object:
mock_get_key_object.return_value = UserAPIKeyAuth(
token="sk-test-key",
user_role="internal_user",
team_id=None,
user_id="test-user",
)
# Call _user_api_key_auth_builder with the returned key
from litellm.proxy.auth.user_api_key_auth import user_api_key_auth_builder
try:
auth_obj = await user_api_key_auth_builder(
request=request,
api_key="my-custom-key",
azure_api_key_header="",
anthropic_api_key_header=None,
google_ai_studio_api_key_header=None,
azure_apim_header=None,
request_data={},
custom_litellm_key_header=None,
)
except Exception as e:
print("error:", e)
# Verify the key lookup was called with the correct hashed key
mock_get_key_object.assert_called_once()
# The key should be hashed before being passed to the resolver
assert mock_get_key_object.call_args[0][0] == hash_token("sk-test-key")