jarvis-assist/Claude outputs/tests/test_auth.py

38 lines
878 B
Python

import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import pytest
from fastapi.testclient import TestClient
import main
main.API_KEY_ADMIN = "test-secret"
@pytest.fixture()
def client():
with TestClient(main.app) as c:
yield c
def test_missing_key_returns_401(client):
response = client.get("/api/v1/admin/stats")
assert response.status_code == 401
def test_wrong_key_returns_401(client):
response = client.get("/api/v1/admin/stats", headers={"X-Admin-Key": "nope"})
assert response.status_code == 401
def test_correct_key_passes_auth_gate(client):
response = client.get("/api/v1/admin/stats", headers={"X-Admin-Key": "test-secret"})
assert response.status_code != 401
def test_health_stays_open_without_key(client):
response = client.get("/health")
assert response.status_code != 401