From 1ebdeb852c3273a804942e98c111e9a5f73be099 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Fri, 21 Mar 2025 18:06:20 -0700 Subject: [PATCH] test(test_internal_user_endpoints.py): add unit testing to handle user_email=None --- .../test_internal_user_endpoints.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/litellm/proxy/management_endpoints/test_internal_user_endpoints.py diff --git a/tests/litellm/proxy/management_endpoints/test_internal_user_endpoints.py b/tests/litellm/proxy/management_endpoints/test_internal_user_endpoints.py new file mode 100644 index 00000000000..697be8b3c9b --- /dev/null +++ b/tests/litellm/proxy/management_endpoints/test_internal_user_endpoints.py @@ -0,0 +1,57 @@ +import json +import os +import sys + +import pytest +from fastapi.testclient import TestClient + +sys.path.insert( + 0, os.path.abspath("../../../..") +) # Adds the parent directory to the system path + +from litellm.proxy._types import LiteLLM_UserTableFiltered, UserAPIKeyAuth +from litellm.proxy.management_endpoints.internal_user_endpoints import ui_view_users +from litellm.proxy.proxy_server import app + +client = TestClient(app) + + +@pytest.mark.asyncio +async def test_ui_view_users_with_null_email(mocker, caplog): + """ + Test that /user/filter/ui endpoint returns users even when they have null email fields + """ + # Mock the prisma client + mock_prisma_client = mocker.MagicMock() + + # Create mock user data with null email + mock_user = mocker.MagicMock() + mock_user.model_dump.return_value = { + "user_id": "test-user-null-email", + "user_email": None, + "user_role": "proxy_admin", + "created_at": "2024-01-01T00:00:00Z", + } + + # Setup the mock find_many response + # Setup the mock find_many response as an async function + async def mock_find_many(*args, **kwargs): + return [mock_user] + + mock_prisma_client.db.litellm_usertable.find_many = mock_find_many + + # Patch the prisma client import in the endpoint + mocker.patch("litellm.proxy.proxy_server.prisma_client", mock_prisma_client) + + # Call ui_view_users function directly + response = await ui_view_users( + user_api_key_dict=UserAPIKeyAuth(user_id="test_user"), + user_id="test_user", + user_email=None, + page=1, + page_size=50, + ) + + assert response == [ + LiteLLM_UserTableFiltered(user_id="test-user-null-email", user_email=None) + ]