litellm/tests/test_litellm/proxy/client/test_users.py
yuneng-jiang 6a0d03914c
test: drop the cwd-relative sys.path.insert calls from the test suite (#37802)
* test: drop the cwd-relative sys.path.insert calls from the test suite

TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.

Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.

Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.

* test: drop the duplicate imports the sys.path sweep exposed to F811

* test(pre-call-utils): restore the os import the new bedrock tests need
2026-08-22 09:25:58 -07:00

84 lines
2.4 KiB
Python

from unittest.mock import MagicMock, patch
import pytest
from litellm.proxy.client.users import (
NotFoundError,
UnauthorizedError,
UsersManagementClient,
)
@pytest.fixture
def client():
return UsersManagementClient(base_url="http://localhost:4000", api_key="sk-test")
@patch("requests.get")
def test_list_users_success(mock_get, client):
mock_get.return_value.status_code = 200
mock_get.return_value.json.return_value = {"users": [{"user_id": "u1"}]}
users = client.list_users()
assert users == [{"user_id": "u1"}]
mock_get.assert_called_once()
@patch("requests.get")
def test_list_users_unauthorized(mock_get, client):
mock_get.return_value.status_code = 401
mock_get.return_value.text = "unauthorized"
with pytest.raises(UnauthorizedError):
client.list_users()
@patch("requests.get")
def test_get_user_success(mock_get, client):
mock_get.return_value.status_code = 200
mock_get.return_value.json.return_value = {"user_id": "u1"}
user = client.get_user(user_id="u1")
assert user["user_id"] == "u1"
mock_get.assert_called_once()
@patch("requests.get")
def test_get_user_404(mock_get, client):
mock_get.return_value.status_code = 404
mock_get.return_value.text = "not found"
with pytest.raises(NotFoundError):
client.get_user(user_id="u1")
@patch("requests.post")
def test_create_user_success(mock_post, client):
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = {"user_id": "u1"}
user = client.create_user({"user_email": "a@b.com"})
assert user["user_id"] == "u1"
mock_post.assert_called_once()
@patch("requests.post")
def test_create_user_unauthorized(mock_post, client):
mock_post.return_value.status_code = 401
mock_post.return_value.text = "unauthorized"
with pytest.raises(UnauthorizedError):
client.create_user({"user_email": "a@b.com"})
@patch("requests.post")
def test_delete_user_success(mock_post, client):
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = {"deleted": 1}
result = client.delete_user(["u1"])
assert result["deleted"] == 1
mock_post.assert_called_once()
@patch("requests.post")
def test_delete_user_unauthorized(mock_post, client):
mock_post.return_value.status_code = 401
mock_post.return_value.text = "unauthorized"
with pytest.raises(UnauthorizedError):
client.delete_user(["u1"])