|
| 1 | +"""Tests for middleware modules to boost coverage. |
| 2 | +
|
| 3 | +Tests pure ASGI middleware without a running server using mock scope/receive/send. |
| 4 | +""" |
| 5 | + |
| 6 | +import asyncio |
| 7 | +import pytest |
| 8 | + |
| 9 | + |
| 10 | +# --------------------------------------------------------------------------- |
| 11 | +# Security Headers Middleware |
| 12 | +# --------------------------------------------------------------------------- |
| 13 | + |
| 14 | + |
| 15 | +class TestSecurityHeadersMiddleware: |
| 16 | + @pytest.mark.asyncio |
| 17 | + async def test_adds_headers_to_http_response(self): |
| 18 | + from app.middleware.security_headers import SecurityHeadersMiddleware |
| 19 | + |
| 20 | + captured_headers = {} |
| 21 | + |
| 22 | + async def mock_app(scope, receive, send): |
| 23 | + await send({ |
| 24 | + "type": "http.response.start", |
| 25 | + "status": 200, |
| 26 | + "headers": [(b"content-type", b"text/plain")], |
| 27 | + }) |
| 28 | + await send({"type": "http.response.body", "body": b"ok"}) |
| 29 | + |
| 30 | + async def mock_send(message): |
| 31 | + if message["type"] == "http.response.start": |
| 32 | + for k, v in message.get("headers", []): |
| 33 | + captured_headers[k.decode()] = v.decode() |
| 34 | + |
| 35 | + mw = SecurityHeadersMiddleware(mock_app) |
| 36 | + scope = {"type": "http", "scheme": "https"} |
| 37 | + await mw(scope, None, mock_send) |
| 38 | + |
| 39 | + assert "x-content-type-options" in captured_headers |
| 40 | + assert captured_headers["x-content-type-options"] == "nosniff" |
| 41 | + assert "x-frame-options" in captured_headers |
| 42 | + assert captured_headers["x-frame-options"] == "DENY" |
| 43 | + assert "referrer-policy" in captured_headers |
| 44 | + assert "permissions-policy" in captured_headers |
| 45 | + assert "content-security-policy" in captured_headers |
| 46 | + |
| 47 | + @pytest.mark.asyncio |
| 48 | + async def test_hsts_only_on_https(self): |
| 49 | + from app.middleware.security_headers import SecurityHeadersMiddleware |
| 50 | + |
| 51 | + headers_http = {} |
| 52 | + headers_https = {} |
| 53 | + |
| 54 | + async def mock_app(scope, receive, send): |
| 55 | + await send({"type": "http.response.start", "status": 200, "headers": []}) |
| 56 | + |
| 57 | + async def capture_http(message): |
| 58 | + if message["type"] == "http.response.start": |
| 59 | + for k, v in message.get("headers", []): |
| 60 | + headers_http[k.decode()] = v.decode() |
| 61 | + |
| 62 | + async def capture_https(message): |
| 63 | + if message["type"] == "http.response.start": |
| 64 | + for k, v in message.get("headers", []): |
| 65 | + headers_https[k.decode()] = v.decode() |
| 66 | + |
| 67 | + mw = SecurityHeadersMiddleware(mock_app) |
| 68 | + |
| 69 | + await mw({"type": "http", "scheme": "http"}, None, capture_http) |
| 70 | + await mw({"type": "http", "scheme": "https"}, None, capture_https) |
| 71 | + |
| 72 | + assert "strict-transport-security" not in headers_http |
| 73 | + assert "strict-transport-security" in headers_https |
| 74 | + |
| 75 | + @pytest.mark.asyncio |
| 76 | + async def test_passthrough_non_http(self): |
| 77 | + from app.middleware.security_headers import SecurityHeadersMiddleware |
| 78 | + |
| 79 | + called = [] |
| 80 | + |
| 81 | + async def mock_app(scope, receive, send): |
| 82 | + called.append(scope["type"]) |
| 83 | + |
| 84 | + mw = SecurityHeadersMiddleware(mock_app) |
| 85 | + await mw({"type": "websocket"}, None, None) |
| 86 | + |
| 87 | + assert called == ["websocket"] |
| 88 | + |
| 89 | + |
| 90 | +# --------------------------------------------------------------------------- |
| 91 | +# Request ID Middleware |
| 92 | +# --------------------------------------------------------------------------- |
| 93 | + |
| 94 | + |
| 95 | +class TestRequestIDMiddleware: |
| 96 | + @pytest.mark.asyncio |
| 97 | + async def test_adds_request_id_header(self): |
| 98 | + from app.middleware.request_id import RequestIDMiddleware |
| 99 | + |
| 100 | + captured = {} |
| 101 | + |
| 102 | + async def mock_app(scope, receive, send): |
| 103 | + await send({"type": "http.response.start", "status": 200, "headers": []}) |
| 104 | + |
| 105 | + async def capture(message): |
| 106 | + if message["type"] == "http.response.start": |
| 107 | + for k, v in message.get("headers", []): |
| 108 | + captured[k.decode()] = v.decode() |
| 109 | + |
| 110 | + mw = RequestIDMiddleware(mock_app) |
| 111 | + await mw({"type": "http"}, None, capture) |
| 112 | + |
| 113 | + assert "x-request-id" in captured |
| 114 | + assert len(captured["x-request-id"]) == 32 # hex UUID without dashes |
| 115 | + |
| 116 | + @pytest.mark.asyncio |
| 117 | + async def test_unique_ids_per_request(self): |
| 118 | + from app.middleware.request_id import RequestIDMiddleware |
| 119 | + |
| 120 | + ids = [] |
| 121 | + |
| 122 | + async def mock_app(scope, receive, send): |
| 123 | + await send({"type": "http.response.start", "status": 200, "headers": []}) |
| 124 | + |
| 125 | + async def capture(message): |
| 126 | + if message["type"] == "http.response.start": |
| 127 | + for k, v in message.get("headers", []): |
| 128 | + if k == b"x-request-id": |
| 129 | + ids.append(v.decode()) |
| 130 | + |
| 131 | + mw = RequestIDMiddleware(mock_app) |
| 132 | + for _ in range(3): |
| 133 | + await mw({"type": "http"}, None, capture) |
| 134 | + |
| 135 | + assert len(ids) == 3 |
| 136 | + assert len(set(ids)) == 3 # all unique |
| 137 | + |
| 138 | + @pytest.mark.asyncio |
| 139 | + async def test_websocket_passthrough(self): |
| 140 | + from app.middleware.request_id import RequestIDMiddleware |
| 141 | + |
| 142 | + called = [] |
| 143 | + |
| 144 | + async def mock_app(scope, receive, send): |
| 145 | + called.append(True) |
| 146 | + |
| 147 | + mw = RequestIDMiddleware(mock_app) |
| 148 | + await mw({"type": "websocket"}, None, None) |
| 149 | + |
| 150 | + assert len(called) == 1 |
| 151 | + |
| 152 | + |
| 153 | +# --------------------------------------------------------------------------- |
| 154 | +# Redis client |
| 155 | +# --------------------------------------------------------------------------- |
| 156 | + |
| 157 | + |
| 158 | +class TestRedisClientModule: |
| 159 | + @pytest.mark.asyncio |
| 160 | + async def test_check_redis_health_false_when_not_initialized(self): |
| 161 | + from app.core.redis_client import check_redis_health |
| 162 | + |
| 163 | + result = await check_redis_health() |
| 164 | + assert result is False |
| 165 | + |
| 166 | + def test_get_redis_client_returns_none_initially(self): |
| 167 | + from app.core.redis_client import get_redis_client |
| 168 | + |
| 169 | + # In test env without init, should be None or fakeredis |
| 170 | + client = get_redis_client() |
| 171 | + # Can be None or fakeredis depending on test ordering |
| 172 | + assert client is None or client is not None # just verify no crash |
| 173 | + |
| 174 | + @pytest.mark.asyncio |
| 175 | + async def test_close_redis_safe_when_not_initialized(self): |
| 176 | + from app.core.redis_client import close_redis |
| 177 | + |
| 178 | + # Should not raise |
| 179 | + await close_redis() |
0 commit comments