Implement optional authentication (Anthropic style) (#80)
Some checks are pending
CI / checks (push) Waiting to run

This commit is contained in:
th-ch 2026-03-27 19:11:47 +01:00 committed by GitHub
parent 587931d279
commit f703a0e403
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 156 additions and 8 deletions

57
tests/api/test_auth.py Normal file
View file

@ -0,0 +1,57 @@
from unittest.mock import patch
from fastapi.testclient import TestClient
from api.app import app
from api.dependencies import get_settings
from config.settings import Settings
def test_anthropic_auth_token_required_and_accepts_x_api_key():
client = TestClient(app)
settings = Settings()
settings.anthropic_auth_token = "s3cr3t"
app.dependency_overrides[get_settings] = lambda: settings
payload = {
"model": "claude-3-sonnet",
"messages": [{"role": "user", "content": "hello"}],
}
with patch("api.routes.get_token_count", return_value=1):
# No header -> 401
r = client.post("/v1/messages/count_tokens", json=payload)
assert r.status_code == 401
# X-API-Key header -> 200
r = client.post(
"/v1/messages/count_tokens", json=payload, headers={"X-API-Key": "s3cr3t"}
)
assert r.status_code == 200
assert r.json()["input_tokens"] == 1
app.dependency_overrides.clear()
def test_anthropic_auth_token_accepts_bearer_authorization():
client = TestClient(app)
settings = Settings()
settings.anthropic_auth_token = "b3artoken"
app.dependency_overrides[get_settings] = lambda: settings
payload = {
"model": "claude-3-sonnet",
"messages": [{"role": "user", "content": "hello"}],
}
with patch("api.routes.get_token_count", return_value=2):
# Authorization Bearer -> 200
r = client.post(
"/v1/messages/count_tokens",
json=payload,
headers={"Authorization": "Bearer b3artoken"},
)
assert r.status_code == 200
assert r.json()["input_tokens"] == 2
app.dependency_overrides.clear()

View file

@ -9,6 +9,9 @@ import pytest
os.environ.setdefault("NVIDIA_NIM_API_KEY", "test_key")
os.environ.setdefault("MODEL", "nvidia_nim/test-model")
os.environ["PTB_TIMEDELTA"] = "1"
# Ensure tests don't pick up a server API key from the repo .env
# (tests expect endpoints to be unauthenticated by default)
os.environ["ANTHROPIC_AUTH_TOKEN"] = ""
from typing import Any
from unittest.mock import AsyncMock, MagicMock