litellm/tests/proxy_unit_tests/test_proxy_custom_auth.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

83 lines
2.5 KiB
Python

import os
import traceback
from dotenv import load_dotenv
load_dotenv()
import io
# this file is to test litellm/proxy
import asyncio
import pytest
from fastapi import FastAPI
# test /chat/completion request to the proxy
from fastapi.testclient import TestClient
import litellm
from litellm import RateLimitError, Timeout, completion, completion_cost, embedding
from litellm.proxy.proxy_server import ( # Replace with the actual module where your FastAPI router is defined
ProxyConfig,
initialize,
router,
save_worker_config,
)
# Here you create a fixture that will be used by your tests
# Make sure the fixture returns TestClient(app)
@pytest.fixture(scope="function")
def client():
from litellm.proxy.proxy_server import cleanup_router_config_variables
cleanup_router_config_variables()
filepath = os.path.dirname(os.path.abspath(__file__))
config_fp = f"{filepath}/test_configs/test_config_custom_auth.yaml"
# initialize can get run in parallel, it sets specific variables for the fast api app, sinc eit gets run in parallel different tests use the wrong variables
app = FastAPI()
asyncio.run(initialize(config=config_fp))
app.include_router(router) # Include your router in the test app
return TestClient(app)
def test_custom_auth(client):
# Your test data
test_data = {
"model": "openai-model",
"messages": [
{"role": "user", "content": "hi"},
],
"max_tokens": 10,
}
# Your bearer token
token = os.getenv("PROXY_MASTER_KEY")
print(f"token: {token}")
headers = {"Authorization": f"Bearer {token}"}
with pytest.raises(Exception, match="Authentication Error, Failed custom auth") as exc_info:
client.post("/chat/completions", json=test_data, headers=headers)
assert exc_info.value.code == "401"
def test_custom_auth_bearer(client):
# Your test data
test_data = {
"model": "openai-model",
"messages": [
{"role": "user", "content": "hi"},
],
"max_tokens": 10,
}
# Your bearer token
token = os.getenv("PROXY_MASTER_KEY")
headers = {"Authorization": f"WITHOUT BEAR Er {token}"}
with pytest.raises(Exception, match="CustomAuth - Malformed API Key passed in") as exc_info:
client.post("/chat/completions", json=test_data, headers=headers)
assert exc_info.value.code == "401"
assert (
exc_info.value.message
== "Authentication Error, CustomAuth - Malformed API Key passed in. Ensure Key has `Bearer` prefix"
)