Download tests/test_api_auth.py from MERLx/Aperture: direct link, hf CLI and curl.
- Browser
- Download file 1.6 kB
-
https://huggingface.co/spaces/MERLx/Aperture/resolve/fcb9daa4b394d997780098b3706e4f52bb70e072/tests/test_api_auth.py
- Command line
-
hf download hf://spaces/MERLx/Aperture@fcb9daa4b394d997780098b3706e4f52bb70e072/tests/test_api_auth.py
-
curl -L -o test_api_auth.py https://huggingface.co/spaces/MERLx/Aperture/resolve/fcb9daa4b394d997780098b3706e4f52bb70e072/tests/test_api_auth.py
1.6 kB
| """Tests for auth middleware — get_current_user dependency.""" | |
| import time | |
| import hashlib | |
| import pytest | |
| from httpx import AsyncClient, ASGITransport | |
| from app.main import create_app | |
| def _make_token(email: str) -> str: | |
| """Mirror the backend token generation for test fixtures.""" | |
| secret = "aperture-mvp-secret-change-in-production" | |
| hour = int(time.time() // 3600) | |
| payload = f"{email}:{hour}:{secret}" | |
| return hashlib.sha256(payload.encode()).hexdigest()[:32] | |
| async def client(tmp_path): | |
| app = create_app(db_path=str(tmp_path / "test.db"), run_worker=False) | |
| transport = ASGITransport(app=app) | |
| async with AsyncClient(transport=transport, base_url="http://test") as c: | |
| yield c | |
| async def test_auth_header_valid(client): | |
| email = "user@example.com" | |
| token = _make_token(email) | |
| resp = await client.get( | |
| "/api/jobs", | |
| headers={"Authorization": f"Bearer {email}:{token}"}, | |
| ) | |
| assert resp.status_code == 200 | |
| async def test_auth_header_missing(client): | |
| resp = await client.get("/api/jobs") | |
| assert resp.status_code == 401 | |
| async def test_auth_header_bad_token(client): | |
| resp = await client.get( | |
| "/api/jobs", | |
| headers={"Authorization": "Bearer user@example.com:badtoken"}, | |
| ) | |
| assert resp.status_code == 401 | |
| async def test_auth_header_malformed(client): | |
| resp = await client.get( | |
| "/api/jobs", | |
| headers={"Authorization": "Bearer garbage"}, | |
| ) | |
| assert resp.status_code == 401 | |