|
| 1 | +import asyncio |
| 2 | +import pathlib |
| 3 | +from typing import Any |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import pytest |
| 7 | +from aiohttp import web |
| 8 | +from aiohttp.test_utils import TestClient |
| 9 | + |
| 10 | +from openapi_core import openapi_request_validator |
| 11 | +from openapi_core import openapi_response_validator |
| 12 | +from openapi_core.contrib.aiohttp import AIOHTTPOpenAPIWebRequest |
| 13 | +from openapi_core.contrib.aiohttp import AIOHTTPOpenAPIWebResponse |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def spec(factory): |
| 18 | + directory = pathlib.Path(__file__).parent |
| 19 | + specfile = directory / "data" / "v3.0" / "aiohttp_factory.yaml" |
| 20 | + return factory.spec_from_file(str(specfile)) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def response_getter() -> mock.MagicMock: |
| 25 | + # Using a mock here allows us to control the return value for different scenarios. |
| 26 | + return mock.MagicMock(return_value={"data": "data"}) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def no_validation(response_getter): |
| 31 | + async def test_route(request: web.Request) -> web.Response: |
| 32 | + await asyncio.sleep(0) |
| 33 | + response = web.json_response( |
| 34 | + response_getter(), |
| 35 | + headers={"X-Rate-Limit": "12"}, |
| 36 | + status=200, |
| 37 | + ) |
| 38 | + return response |
| 39 | + |
| 40 | + return test_route |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +def request_validation(spec, response_getter): |
| 45 | + async def test_route(request: web.Request) -> web.Response: |
| 46 | + request_body = await request.text() |
| 47 | + openapi_request = AIOHTTPOpenAPIWebRequest(request, body=request_body) |
| 48 | + result = openapi_request_validator.validate(spec, openapi_request) |
| 49 | + response: dict[str, Any] = response_getter() |
| 50 | + status = 200 |
| 51 | + if result.errors: |
| 52 | + status = 400 |
| 53 | + response = {"errors": [{"message": str(e) for e in result.errors}]} |
| 54 | + return web.json_response( |
| 55 | + response, |
| 56 | + headers={"X-Rate-Limit": "12"}, |
| 57 | + status=status, |
| 58 | + ) |
| 59 | + |
| 60 | + return test_route |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture |
| 64 | +def response_validation(spec, response_getter): |
| 65 | + async def test_route(request: web.Request) -> web.Response: |
| 66 | + request_body = await request.text() |
| 67 | + openapi_request = AIOHTTPOpenAPIWebRequest(request, body=request_body) |
| 68 | + response_body = response_getter() |
| 69 | + response = web.json_response( |
| 70 | + response_body, |
| 71 | + headers={"X-Rate-Limit": "12"}, |
| 72 | + status=200, |
| 73 | + ) |
| 74 | + openapi_response = AIOHTTPOpenAPIWebResponse(response) |
| 75 | + result = openapi_response_validator.validate( |
| 76 | + spec, openapi_request, openapi_response |
| 77 | + ) |
| 78 | + if result.errors: |
| 79 | + response = web.json_response( |
| 80 | + {"errors": [{"message": str(e) for e in result.errors}]}, |
| 81 | + headers={"X-Rate-Limit": "12"}, |
| 82 | + status=400, |
| 83 | + ) |
| 84 | + return response |
| 85 | + |
| 86 | + return test_route |
| 87 | + |
| 88 | + |
| 89 | +@pytest.fixture( |
| 90 | + params=["no_validation", "request_validation", "response_validation"] |
| 91 | +) |
| 92 | +def router( |
| 93 | + request, |
| 94 | + no_validation, |
| 95 | + request_validation, |
| 96 | + response_validation, |
| 97 | +) -> web.RouteTableDef: |
| 98 | + test_routes = dict( |
| 99 | + no_validation=no_validation, |
| 100 | + request_validation=request_validation, |
| 101 | + response_validation=response_validation, |
| 102 | + ) |
| 103 | + router_ = web.RouteTableDef() |
| 104 | + handler = test_routes[request.param] |
| 105 | + route = router_.post("/browse/{id}/")(handler) |
| 106 | + return router_ |
| 107 | + |
| 108 | + |
| 109 | +@pytest.fixture |
| 110 | +def app(router): |
| 111 | + app = web.Application() |
| 112 | + app.add_routes(router) |
| 113 | + |
| 114 | + return app |
| 115 | + |
| 116 | + |
| 117 | +@pytest.fixture |
| 118 | +async def client(app, aiohttp_client) -> TestClient: |
| 119 | + return await aiohttp_client(app) |
0 commit comments