litellm/tests/proxy_unit_tests/test_proxy_custom_auth.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 lines
3.0 KiB
Python
Raw Permalink Normal View History

2024-07-31 07:50:55 +08:00
import os
import sys
import traceback
2024-07-31 07:50:55 +08:00
from dotenv import load_dotenv
load_dotenv()
2024-07-31 07:50:55 +08:00
import io
import os
# this file is to test litellm/proxy
sys.path.insert(
0, os.path.abspath("../..")
2023-12-25 16:40:38 +08:00
) # Adds the parent directory to the system path
2024-07-31 07:50:55 +08:00
import asyncio
import pytest
from fastapi import FastAPI
# test /chat/completion request to the proxy
from fastapi.testclient import TestClient
2024-07-31 07:50:55 +08:00
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,
2023-12-25 16:40:38 +08:00
router,
save_worker_config,
2024-07-31 07:50:55 +08:00
)
2023-12-12 09:50:31 +08:00
# Here you create a fixture that will be used by your tests
# Make sure the fixture returns TestClient(app)
2023-12-12 13:30:02 +08:00
@pytest.fixture(scope="function")
def client():
2023-12-12 14:11:11 +08:00
from litellm.proxy.proxy_server import cleanup_router_config_variables
2023-12-25 16:40:38 +08:00
2023-12-12 14:11:11 +08:00
cleanup_router_config_variables()
2023-12-12 09:50:31 +08:00
filepath = os.path.dirname(os.path.abspath(__file__))
2023-12-12 13:30:02 +08:00
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
2023-12-12 09:50:31 +08:00
app = FastAPI()
asyncio.run(initialize(config=config_fp))
2023-12-12 14:11:11 +08:00
2023-12-12 09:50:31 +08:00
app.include_router(router) # Include your router in the test app
return TestClient(app)
2023-12-12 13:30:02 +08:00
def test_custom_auth(client):
try:
2023-12-25 16:40:38 +08:00
# Your test data
test_data = {
"model": "openai-model",
"messages": [
2023-12-25 16:40:38 +08:00
{"role": "user", "content": "hi"},
],
"max_tokens": 10,
}
# Your bearer token
token = os.getenv("PROXY_MASTER_KEY")
print(f"token: {token}")
2023-12-25 16:40:38 +08:00
headers = {"Authorization": f"Bearer {token}"}
response = client.post("/chat/completions", json=test_data, headers=headers)
pytest.fail("LiteLLM Proxy test failed. This request should have been rejected")
except Exception as e:
print(vars(e))
print("got an exception")
2024-07-31 07:50:55 +08:00
assert e.code == "401"
assert e.message == "Authentication Error, Failed custom auth"
pass
2024-02-02 05:55:50 +08:00
def test_custom_auth_bearer(client):
try:
# 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}"}
response = client.post("/chat/completions", json=test_data, headers=headers)
pytest.fail("LiteLLM Proxy test failed. This request should have been rejected")
except Exception as e:
print(vars(e))
print("got an exception")
2024-07-31 07:50:55 +08:00
assert e.code == "401"
2024-02-02 05:55:50 +08:00
assert (
e.message
== "Authentication Error, CustomAuth - Malformed API Key passed in. Ensure Key has `Bearer` prefix"
)
pass