2024-07-31 07:50:55 +08:00
import os
import sys
2023-12-05 10:32:47 +08:00
import traceback
2024-07-31 07:50:55 +08:00
2023-12-05 10:32:47 +08:00
from dotenv import load_dotenv
load_dotenv ( )
2024-07-31 07:50:55 +08:00
import io
import os
2023-12-05 10:32:47 +08:00
# 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
2023-12-05 10:32:47 +08:00
# 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
2023-12-05 10:32:47 +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 ( )
2024-01-04 20:58:18 +08:00
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-05 10:32:47 +08:00
2023-12-12 13:30:02 +08:00
def test_custom_auth ( client ) :
2023-12-05 10:32:47 +08:00
try :
2023-12-25 16:40:38 +08:00
# Your test data
2023-12-05 10:32:47 +08:00
test_data = {
" model " : " openai-model " ,
" messages " : [
2023-12-25 16:40:38 +08:00
{ " role " : " user " , " content " : " hi " } ,
2023-12-05 10:32:47 +08:00
] ,
" max_tokens " : 10 ,
}
# Your bearer token
token = os . getenv ( " PROXY_MASTER_KEY " )
2024-03-10 08:51:11 +08:00
print ( f " token: { token } " )
2023-12-25 16:40:38 +08:00
headers = { " Authorization " : f " Bearer { token } " }
2023-12-05 10:32:47 +08:00
response = client . post ( " /chat/completions " , json = test_data , headers = headers )
2024-01-24 09:36:13 +08:00
pytest . fail ( " LiteLLM Proxy test failed. This request should have been rejected " )
2023-12-05 10:32:47 +08:00
except Exception as e :
2024-01-24 09:36:13 +08:00
print ( vars ( e ) )
print ( " got an exception " )
2024-07-31 07:50:55 +08:00
assert e . code == " 401 "
2024-01-24 09:36:13 +08:00
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