test_api_auth.py 1.16 KB
Newer Older
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
import pytest
import requests


@pytest.mark.integration
def test_router_api_key_enforcement(router_manager, mock_workers):
    # Start backend requiring API key; router should forward Authorization header transparently
    _, urls, _ = mock_workers(
        n=1, args=["--require-api-key", "--api-key", "correct_api_key"]
    )
    rh = router_manager.start_router(
        worker_urls=urls,
        policy="round_robin",
        extra={},
    )

    # No auth -> 401
    r = requests.post(
        f"{rh.url}/v1/completions",
        json={"model": "test-model", "prompt": "x", "max_tokens": 1, "stream": False},
    )
    assert r.status_code == 401

    # Invalid auth -> 401
    r = requests.post(
        f"{rh.url}/v1/completions",
        json={"model": "test-model", "prompt": "x", "max_tokens": 1, "stream": False},
        headers={"Authorization": "Bearer wrong"},
    )
    assert r.status_code == 401

    # Correct auth -> 200
    r = requests.post(
        f"{rh.url}/v1/completions",
        json={"model": "test-model", "prompt": "x", "max_tokens": 1, "stream": False},
        headers={"Authorization": "Bearer correct_api_key"},
    )
    assert r.status_code == 200