|
| 1 | +from unittest.mock import Mock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +from pydantic import BaseModel |
| 5 | +from starlette.requests import Request |
| 6 | + |
| 7 | +from socketio_app.web_routes.docs import ( |
| 8 | + ASYNCAPI_CSS_DEFAULT_URL, |
| 9 | + ASYNCAPI_JS_DEFAULT_URL, |
| 10 | + NORMALIZE_CSS_DEFAULT_URL, |
| 11 | + PydanticResponse, |
| 12 | + asyncapi_json, |
| 13 | + get_asyncapi_html, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +# Test model |
| 18 | +class TestModel(BaseModel): |
| 19 | + name: str |
| 20 | + value: int |
| 21 | + |
| 22 | + |
| 23 | +# Fixtures |
| 24 | +@pytest.fixture |
| 25 | +def test_model(): |
| 26 | + return TestModel(name="test", value=42) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def mock_request(): |
| 31 | + return Mock(spec=Request) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def mock_app_config(): |
| 36 | + with patch("socketio_app.web_routes.docs.AppConfig") as mock: |
| 37 | + mock.return_value.APP_NAME = "Test App" |
| 38 | + yield mock |
| 39 | + |
| 40 | + |
| 41 | +# Tests for PydanticResponse |
| 42 | +def test_pydantic_response_render(test_model): |
| 43 | + response = PydanticResponse(test_model) |
| 44 | + expected = b'{"name":"test","value":42}' |
| 45 | + assert response.render(test_model) == expected |
| 46 | + |
| 47 | + |
| 48 | +# Tests for asyncapi_json endpoint |
| 49 | +async def test_asyncapi_json(mock_request, test_model): |
| 50 | + with patch("socketio_app.web_routes.docs.get_schema") as mock_get_schema: |
| 51 | + mock_get_schema.return_value = test_model |
| 52 | + response = await asyncapi_json(mock_request) |
| 53 | + assert isinstance(response, PydanticResponse) |
| 54 | + assert response.body == b'{"name":"test","value":42}' |
| 55 | + |
| 56 | + |
| 57 | +# Tests for get_asyncapi_html endpoint |
| 58 | +async def test_get_asyncapi_html_default_params(mock_request, mock_app_config): |
| 59 | + mock_request.query_params = {} |
| 60 | + response = await get_asyncapi_html(mock_request) |
| 61 | + |
| 62 | + assert response.status_code == 200 |
| 63 | + assert response.headers["content-type"] == "text/html; charset=utf-8" |
| 64 | + |
| 65 | + content = response.body.decode() |
| 66 | + assert "Test App AsyncAPI" in content |
| 67 | + assert ASYNCAPI_JS_DEFAULT_URL in content |
| 68 | + assert NORMALIZE_CSS_DEFAULT_URL in content |
| 69 | + assert ASYNCAPI_CSS_DEFAULT_URL in content |
| 70 | + assert '"sidebar": true' in content |
| 71 | + assert '"info": true' in content |
| 72 | + |
| 73 | + |
| 74 | +async def test_get_asyncapi_html_custom_params(mock_request, mock_app_config): |
| 75 | + mock_request.query_params = { |
| 76 | + "sidebar": "false", |
| 77 | + "info": "false", |
| 78 | + "servers": "false", |
| 79 | + "operations": "false", |
| 80 | + "messages": "false", |
| 81 | + "schemas": "false", |
| 82 | + "errors": "false", |
| 83 | + "expand_message_examples": "true", |
| 84 | + } |
| 85 | + |
| 86 | + response = await get_asyncapi_html(mock_request) |
| 87 | + content = response.body.decode() |
| 88 | + |
| 89 | + assert '"sidebar": false' in content |
| 90 | + assert '"info": false' in content |
| 91 | + assert '"servers": false' in content |
| 92 | + assert '"operations": false' in content |
| 93 | + assert '"messages": false' in content |
| 94 | + assert '"schemas": false' in content |
| 95 | + assert '"errors": false' in content |
| 96 | + assert '"messageExamples": true' in content |
0 commit comments