mirror of
https://github.com/Alishahryar1/free-claude-code.git
synced 2026-04-26 10:31:07 +00:00
Some checks are pending
CI / checks (push) Waiting to run
Consolidates the incremental refactor work into a single change set: modular web tools (api/web_tools), native Anthropic request building and SSE block policy, OpenAI conversion and error handling, provider transports and rate limiting, messaging handler and tree queue, safe logging, smoke tests, and broad test coverage.
75 lines
2.2 KiB
Python
75 lines
2.2 KiB
Python
from unittest.mock import patch
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from api.app import create_app
|
|
from api.dependencies import get_settings
|
|
from config.settings import Settings
|
|
|
|
app = create_app()
|
|
|
|
|
|
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()
|
|
|
|
|
|
def test_anthropic_auth_token_applies_to_models_endpoint():
|
|
client = TestClient(app)
|
|
settings = Settings()
|
|
settings.anthropic_auth_token = "models-token"
|
|
app.dependency_overrides[get_settings] = lambda: settings
|
|
|
|
r = client.get("/v1/models")
|
|
assert r.status_code == 401
|
|
|
|
r = client.get("/v1/models", headers={"X-API-Key": "models-token"})
|
|
assert r.status_code == 200
|
|
assert "data" in r.json()
|
|
|
|
app.dependency_overrides.clear()
|