File size: 3,058 Bytes
447ebeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
import sys
import traceback

from dotenv import load_dotenv

load_dotenv()
import io
import os

# this file is to test litellm/proxy

sys.path.insert(
    0, os.path.abspath("../..")
)  # Adds the parent directory to the system path
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):
    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")
        print(f"token: {token}")
        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")
        assert e.code == "401"
        assert e.message == "Authentication Error, Failed custom auth"
        pass


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")
        assert e.code == "401"
        assert (
            e.message
            == "Authentication Error, CustomAuth - Malformed API Key passed in. Ensure Key has `Bearer` prefix"
        )
        pass