|
| 1 | +"""Tests for shared-secret authentication middleware.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import base64 |
| 6 | + |
| 7 | +from starlette.testclient import TestClient |
| 8 | + |
| 9 | +from app_factory import create_app |
| 10 | + |
| 11 | + |
| 12 | +def test_request_without_token_returns_401(test_state): |
| 13 | + app = create_app(handler=test_state, auth_token="test-secret") |
| 14 | + with TestClient(app) as client: |
| 15 | + response = client.get("/health") |
| 16 | + assert response.status_code == 401 |
| 17 | + assert response.json() == {"error": "Unauthorized"} |
| 18 | + |
| 19 | + |
| 20 | +def test_request_with_correct_bearer_token(test_state): |
| 21 | + app = create_app(handler=test_state, auth_token="test-secret") |
| 22 | + with TestClient(app) as client: |
| 23 | + response = client.get("/health", headers={"Authorization": "Bearer test-secret"}) |
| 24 | + assert response.status_code == 200 |
| 25 | + |
| 26 | + |
| 27 | +def test_request_with_correct_basic_auth(test_state): |
| 28 | + app = create_app(handler=test_state, auth_token="test-secret") |
| 29 | + credentials = base64.b64encode(b":test-secret").decode() |
| 30 | + with TestClient(app) as client: |
| 31 | + response = client.get("/health", headers={"Authorization": f"Basic {credentials}"}) |
| 32 | + assert response.status_code == 200 |
| 33 | + |
| 34 | + |
| 35 | +def test_request_with_wrong_token_returns_401(test_state): |
| 36 | + app = create_app(handler=test_state, auth_token="test-secret") |
| 37 | + with TestClient(app) as client: |
| 38 | + response = client.get("/health", headers={"Authorization": "Bearer wrong-token"}) |
| 39 | + assert response.status_code == 401 |
| 40 | + |
| 41 | + |
| 42 | +def test_health_without_token_returns_401(test_state): |
| 43 | + """Health endpoint is NOT exempt from auth.""" |
| 44 | + app = create_app(handler=test_state, auth_token="test-secret") |
| 45 | + with TestClient(app) as client: |
| 46 | + response = client.get("/health") |
| 47 | + assert response.status_code == 401 |
| 48 | + |
| 49 | + |
| 50 | +def test_no_auth_token_disables_middleware(test_state): |
| 51 | + """When auth_token is empty string, auth is disabled (dev/test mode).""" |
| 52 | + app = create_app(handler=test_state, auth_token="") |
| 53 | + with TestClient(app) as client: |
| 54 | + response = client.get("/health") |
| 55 | + assert response.status_code == 200 |
| 56 | + |
| 57 | + |
| 58 | +def test_websocket_with_token_query_param(test_state): |
| 59 | + app = create_app(handler=test_state, auth_token="test-secret") |
| 60 | + with TestClient(app) as client: |
| 61 | + # WebSocket upgrade without token should fail with 401 |
| 62 | + response = client.get( |
| 63 | + "/ws/download/test", |
| 64 | + headers={"upgrade": "websocket", "connection": "upgrade"}, |
| 65 | + ) |
| 66 | + assert response.status_code == 401 |
| 67 | + |
| 68 | + # WebSocket upgrade with correct token query param |
| 69 | + response = client.get( |
| 70 | + "/ws/download/test?token=test-secret", |
| 71 | + headers={"upgrade": "websocket", "connection": "upgrade"}, |
| 72 | + ) |
| 73 | + # The route may not exist, but auth should pass (not 401) |
| 74 | + assert response.status_code != 401 |
0 commit comments