2021-08-05 20:55:42 +01:00
|
|
|
import asyncio
|
2023-02-05 13:09:04 +00:00
|
|
|
import inspect
|
2021-08-05 20:55:42 +01:00
|
|
|
import logging
|
2023-02-05 13:09:04 +00:00
|
|
|
import os
|
2019-02-28 14:56:41 +00:00
|
|
|
import random
|
|
|
|
import re
|
|
|
|
import string
|
2018-10-09 03:40:36 +01:00
|
|
|
import sys
|
2019-02-28 14:56:41 +00:00
|
|
|
import uuid
|
2021-12-25 20:20:06 +00:00
|
|
|
from contextlib import suppress
|
2021-12-09 10:00:18 +00:00
|
|
|
from logging import LogRecord
|
2022-12-15 09:49:26 +00:00
|
|
|
from typing import Any, Dict, List, Tuple
|
2022-12-18 08:29:58 +00:00
|
|
|
from unittest.mock import MagicMock, Mock, patch
|
2021-02-08 10:18:29 +00:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
import pytest
|
2021-02-08 10:18:29 +00:00
|
|
|
from sanic_routing.exceptions import RouteExists
|
2021-08-05 20:55:42 +01:00
|
|
|
from sanic_testing.testing import PORT
|
2021-01-19 14:34:52 +00:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
from sanic import Sanic
|
2021-02-08 10:18:29 +00:00
|
|
|
from sanic.constants import HTTP_METHODS
|
|
|
|
from sanic.router import Router
|
2021-08-05 20:55:42 +01:00
|
|
|
from sanic.touchup.service import TouchUp
|
2019-02-28 14:56:41 +00:00
|
|
|
|
2021-05-31 20:41:41 +01:00
|
|
|
slugify = re.compile(r"[^a-zA-Z0-9_\-]")
|
2019-02-28 14:56:41 +00:00
|
|
|
random.seed("Pack my box with five dozen liquor jugs.")
|
2020-12-28 20:47:31 +00:00
|
|
|
Sanic.test_mode = True
|
2018-08-26 15:43:14 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
if sys.platform in ["win32", "cygwin"]:
|
2018-10-09 03:40:36 +01:00
|
|
|
collect_ignore = ["test_worker.py"]
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
|
2019-02-28 14:56:41 +00:00
|
|
|
async def _handler(request):
|
|
|
|
"""
|
|
|
|
Dummy placeholder method used for route resolver when creating a new
|
|
|
|
route into the sanic router. This router is not actually called by the
|
|
|
|
sanic app. So do not worry about the arguments to this method.
|
|
|
|
|
|
|
|
If you change the return value of this method, make sure to propagate the
|
|
|
|
change to any test case that leverages RouteStringGenerator.
|
|
|
|
"""
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
|
|
TYPE_TO_GENERATOR_MAP = {
|
2023-10-25 04:13:13 +01:00
|
|
|
"str": lambda: "".join([random.choice(string.ascii_lowercase) for _ in range(4)]),
|
2019-02-28 14:56:41 +00:00
|
|
|
"int": lambda: random.choice(range(1000000)),
|
2021-08-05 20:55:42 +01:00
|
|
|
"float": lambda: random.random(),
|
2023-10-25 04:13:13 +01:00
|
|
|
"alpha": lambda: "".join([random.choice(string.ascii_lowercase) for _ in range(4)]),
|
2019-02-28 14:56:41 +00:00
|
|
|
"uuid": lambda: str(uuid.uuid1()),
|
|
|
|
}
|
|
|
|
|
2022-12-15 09:49:26 +00:00
|
|
|
CACHE: Dict[str, Any] = {}
|
2021-08-05 20:55:42 +01:00
|
|
|
|
2019-02-28 14:56:41 +00:00
|
|
|
|
|
|
|
class RouteStringGenerator:
|
|
|
|
ROUTE_COUNT_PER_DEPTH = 100
|
2021-02-08 10:18:29 +00:00
|
|
|
HTTP_METHODS = HTTP_METHODS
|
2021-08-05 20:55:42 +01:00
|
|
|
ROUTE_PARAM_TYPES = ["str", "int", "float", "alpha", "uuid"]
|
2019-02-28 14:56:41 +00:00
|
|
|
|
|
|
|
def generate_random_direct_route(self, max_route_depth=4):
|
|
|
|
routes = []
|
|
|
|
for depth in range(1, max_route_depth + 1):
|
|
|
|
for _ in range(self.ROUTE_COUNT_PER_DEPTH):
|
|
|
|
route = "/".join(
|
2021-08-05 20:55:42 +01:00
|
|
|
[TYPE_TO_GENERATOR_MAP.get("str")() for _ in range(depth)]
|
2019-02-28 14:56:41 +00:00
|
|
|
)
|
|
|
|
route = route.replace(".", "", -1)
|
|
|
|
route_detail = (random.choice(self.HTTP_METHODS), route)
|
|
|
|
|
|
|
|
if route_detail not in routes:
|
|
|
|
routes.append(route_detail)
|
|
|
|
return routes
|
|
|
|
|
|
|
|
def add_typed_parameters(self, current_routes, max_route_depth=8):
|
|
|
|
routes = []
|
|
|
|
for method, route in current_routes:
|
|
|
|
current_length = len(route.split("/"))
|
|
|
|
new_route_part = "/".join(
|
|
|
|
[
|
|
|
|
"<{}:{}>".format(
|
2021-08-05 20:55:42 +01:00
|
|
|
TYPE_TO_GENERATOR_MAP.get("str")(),
|
2019-02-28 14:56:41 +00:00
|
|
|
random.choice(self.ROUTE_PARAM_TYPES),
|
|
|
|
)
|
|
|
|
for _ in range(max_route_depth - current_length)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
route = "/".join([route, new_route_part])
|
|
|
|
route = route.replace(".", "", -1)
|
|
|
|
routes.append((method, route))
|
|
|
|
return routes
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def generate_url_for_template(template):
|
|
|
|
url = template
|
|
|
|
for pattern, param_type in re.findall(
|
2021-08-05 20:55:42 +01:00
|
|
|
re.compile(r"((?:<\w+:(str|int|float|alpha|uuid)>)+)"),
|
2019-02-28 14:56:41 +00:00
|
|
|
template,
|
|
|
|
):
|
|
|
|
value = TYPE_TO_GENERATOR_MAP.get(param_type)()
|
|
|
|
url = url.replace(pattern, str(value), -1)
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
2020-11-29 21:26:12 +00:00
|
|
|
def sanic_router(app):
|
2021-02-08 10:18:29 +00:00
|
|
|
# noinspection PyProtectedMember
|
|
|
|
def _setup(route_details: tuple) -> Tuple[Router, tuple]:
|
|
|
|
router = Router()
|
2021-09-29 21:53:49 +01:00
|
|
|
router.ctx.app = app
|
2021-02-08 10:18:29 +00:00
|
|
|
added_router = []
|
|
|
|
for method, route in route_details:
|
|
|
|
try:
|
|
|
|
router.add(
|
|
|
|
uri=f"/{route}",
|
|
|
|
methods=frozenset({method}),
|
|
|
|
host="localhost",
|
|
|
|
handler=_handler,
|
|
|
|
)
|
|
|
|
added_router.append((method, route))
|
|
|
|
except RouteExists:
|
|
|
|
pass
|
|
|
|
router.finalize()
|
2022-09-18 15:17:23 +01:00
|
|
|
return router, tuple(added_router)
|
2021-02-08 10:18:29 +00:00
|
|
|
|
|
|
|
return _setup
|
2019-02-28 14:56:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def route_generator() -> RouteStringGenerator:
|
|
|
|
return RouteStringGenerator()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def url_param_generator():
|
|
|
|
return TYPE_TO_GENERATOR_MAP
|
|
|
|
|
|
|
|
|
2021-01-19 14:17:07 +00:00
|
|
|
@pytest.fixture(scope="function")
|
2018-08-26 15:43:14 +01:00
|
|
|
def app(request):
|
2021-08-05 20:55:42 +01:00
|
|
|
if not CACHE:
|
|
|
|
for target, method_name in TouchUp._registry:
|
|
|
|
CACHE[method_name] = getattr(target, method_name)
|
2021-05-31 20:41:41 +01:00
|
|
|
app = Sanic(slugify.sub("-", request.node.name))
|
2022-12-15 09:49:26 +00:00
|
|
|
|
2021-08-05 20:55:42 +01:00
|
|
|
yield app
|
|
|
|
for target, method_name in TouchUp._registry:
|
|
|
|
setattr(target, method_name, CACHE[method_name])
|
2022-06-27 09:19:26 +01:00
|
|
|
Sanic._app_registry.clear()
|
2021-08-05 20:55:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def run_startup(caplog):
|
|
|
|
def run(app):
|
|
|
|
nonlocal caplog
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
with caplog.at_level(logging.DEBUG):
|
|
|
|
server = app.create_server(
|
|
|
|
debug=True, return_asyncio_server=True, port=PORT
|
|
|
|
)
|
|
|
|
loop._stopping = False
|
|
|
|
|
|
|
|
_server = loop.run_until_complete(server)
|
|
|
|
|
|
|
|
_server.close()
|
|
|
|
loop.run_until_complete(_server.wait_closed())
|
|
|
|
app.stop()
|
|
|
|
|
|
|
|
return caplog.record_tuples
|
|
|
|
|
|
|
|
return run
|
2021-12-09 10:00:18 +00:00
|
|
|
|
|
|
|
|
2022-01-16 07:03:04 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def run_multi(caplog):
|
|
|
|
def run(app, level=logging.DEBUG):
|
|
|
|
@app.after_server_start
|
|
|
|
async def stop(app, _):
|
|
|
|
app.stop()
|
|
|
|
|
|
|
|
with caplog.at_level(level):
|
|
|
|
Sanic.serve()
|
|
|
|
|
|
|
|
return caplog.record_tuples
|
|
|
|
|
|
|
|
return run
|
|
|
|
|
|
|
|
|
2021-12-09 10:00:18 +00:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def message_in_records():
|
|
|
|
def msg_in_log(records: List[LogRecord], msg: str):
|
|
|
|
error_captured = False
|
|
|
|
for record in records:
|
|
|
|
if msg in record.message:
|
|
|
|
error_captured = True
|
|
|
|
break
|
|
|
|
return error_captured
|
|
|
|
|
|
|
|
return msg_in_log
|
2021-12-25 20:20:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def ext_instance():
|
|
|
|
ext_instance = MagicMock()
|
|
|
|
ext_instance.injection = MagicMock()
|
|
|
|
return ext_instance
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True) # type: ignore
|
|
|
|
def sanic_ext(ext_instance): # noqa
|
|
|
|
sanic_ext = MagicMock(__version__="1.2.3")
|
|
|
|
sanic_ext.Extend = MagicMock()
|
|
|
|
sanic_ext.Extend.return_value = ext_instance
|
|
|
|
sys.modules["sanic_ext"] = sanic_ext
|
|
|
|
yield sanic_ext
|
|
|
|
with suppress(KeyError):
|
|
|
|
del sys.modules["sanic_ext"]
|
2022-12-18 08:29:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def urlopen():
|
|
|
|
urlopen = Mock()
|
|
|
|
urlopen.return_value = urlopen
|
|
|
|
urlopen.__enter__ = Mock(return_value=urlopen)
|
|
|
|
urlopen.__exit__ = Mock()
|
|
|
|
urlopen.read = Mock()
|
|
|
|
with patch("sanic.cli.inspector_client.urlopen", urlopen):
|
|
|
|
yield urlopen
|
2023-02-05 13:09:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def static_file_directory():
|
|
|
|
"""The static directory to serve"""
|
|
|
|
current_file = inspect.getfile(inspect.currentframe())
|
|
|
|
current_directory = os.path.dirname(os.path.abspath(current_file))
|
|
|
|
static_directory = os.path.join(current_directory, "static")
|
|
|
|
return static_directory
|