Spaces:
Runtime error
Runtime error
File size: 1,395 Bytes
16c19b8 | 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 | from fastapi.testclient import TestClient
from api.main import app
client = TestClient(app)
def test_open_without_api_key_configured():
response = client.get("/health")
assert response.status_code == 200
def test_health_live_always_public(monkeypatch):
monkeypatch.setattr("config.settings.api_key", "secret-key")
response = client.get("/health/live")
assert response.status_code == 200
def test_requires_key_when_configured(monkeypatch):
monkeypatch.setattr("config.settings.api_key", "secret-key")
assert client.get("/health").status_code == 401
assert client.get("/news/all").status_code == 401
def test_accepts_x_api_key_header(monkeypatch):
monkeypatch.setattr("config.settings.api_key", "secret-key")
response = client.get("/health", headers={"X-API-Key": "secret-key"})
assert response.status_code == 200
def test_accepts_bearer_token(monkeypatch):
monkeypatch.setattr("config.settings.api_key", "secret-key")
response = client.get(
"/health",
headers={"Authorization": "Bearer secret-key"},
)
assert response.status_code == 200
def test_multiple_keys(monkeypatch):
monkeypatch.setattr("config.settings.api_key", "alpha,beta")
assert client.get("/health", headers={"X-API-Key": "beta"}).status_code == 200
assert client.get("/health", headers={"X-API-Key": "gamma"}).status_code == 401
|