forked from reactive-python/reactpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_middleware.py
More file actions
143 lines (104 loc) · 4.69 KB
/
test_middleware.py
File metadata and controls
143 lines (104 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# ruff: noqa: S701
import asyncio
from pathlib import Path
import pytest
from jinja2 import Environment as JinjaEnvironment
from jinja2 import FileSystemLoader as JinjaFileSystemLoader
from requests import request
from starlette.applications import Starlette
from starlette.routing import Route
from starlette.templating import Jinja2Templates
import reactpy
from reactpy.config import REACTPY_PATH_PREFIX, REACTPY_TESTS_DEFAULT_TIMEOUT
from reactpy.executors.asgi.middleware import ReactPyMiddleware
from reactpy.testing import BackendFixture, DisplayFixture
@pytest.fixture(scope="module")
async def display(browser):
"""Override for the display fixture that uses ReactPyMiddleware."""
templates = Jinja2Templates(
env=JinjaEnvironment(
loader=JinjaFileSystemLoader("tests/templates"),
extensions=["reactpy.templatetags.ReactPyJinja"],
)
)
async def homepage(request):
return templates.TemplateResponse(request, "index.html")
app = Starlette(routes=[Route("/", homepage)])
async with BackendFixture(app) as server:
async with DisplayFixture(backend=server, browser=browser) as new_display:
yield new_display
def test_invalid_path_prefix():
with pytest.raises(ValueError, match=r"Invalid `path_prefix`*"):
async def app(scope, receive, send):
pass
ReactPyMiddleware(app, root_components=["abc"], path_prefix="invalid")
def test_invalid_web_modules_dir():
with pytest.raises(
ValueError, match=r'Web modules directory "invalid" does not exist.'
):
async def app(scope, receive, send):
pass
ReactPyMiddleware(app, root_components=["abc"], web_modules_dir=Path("invalid"))
async def test_unregistered_root_component(browser):
templates = Jinja2Templates(
env=JinjaEnvironment(
loader=JinjaFileSystemLoader("tests/templates"),
extensions=["reactpy.templatetags.ReactPyJinja"],
)
)
async def homepage(request):
return templates.TemplateResponse(request, "index.html")
@reactpy.component
def Stub():
return reactpy.html.p("Hello")
app = Starlette(routes=[Route("/", homepage)])
app = ReactPyMiddleware(app, root_components=["tests.sample.SampleApp"])
async with BackendFixture(app) as server:
async with DisplayFixture(backend=server, browser=browser) as new_display:
await new_display.show(Stub)
# Wait for the log record to be populated
for _ in range(10):
if "Attempting to use an unregistered root component" in " ".join(
x.message for x in server.log_records
):
break
await asyncio.sleep(0.25)
# Check that the log record was populated with the "unregistered component" message
assert "Attempting to use an unregistered root component" in " ".join(
x.message for x in server.log_records
)
async def test_display_simple_hello_world(display: DisplayFixture):
@reactpy.component
def Hello():
return reactpy.html.p({"id": "hello"}, ["Hello World"])
await display.show(Hello)
await display.page.wait_for_selector("#hello")
# test that we can reconnect successfully
await display.page.reload()
await display.page.wait_for_selector("#hello")
async def test_static_file_not_found():
async def app(scope, receive, send): ...
app = ReactPyMiddleware(app, [])
async with BackendFixture(app) as server:
url = f"http://{server.host}:{server.port}{REACTPY_PATH_PREFIX.current}static/invalid.js"
response = await asyncio.to_thread(
request, "GET", url, timeout=REACTPY_TESTS_DEFAULT_TIMEOUT.current
)
assert response.status_code == 404
async def test_templatetag_bad_kwargs(browser):
"""Override for the display fixture that uses ReactPyMiddleware."""
templates = Jinja2Templates(
env=JinjaEnvironment(
loader=JinjaFileSystemLoader("tests/templates"),
extensions=["reactpy.templatetags.ReactPyJinja"],
)
)
async def homepage(request):
return templates.TemplateResponse(request, "jinja_bad_kwargs.html")
app = Starlette(routes=[Route("/", homepage)])
async with BackendFixture(app) as server:
async with DisplayFixture(backend=server, browser=browser) as new_display:
await new_display.goto("/")
# This test could be improved by actually checking if `bad kwargs` error message is shown in
# `stderr`, but I was struggling to get that to work.
assert "internal server error" in (await new_display.page.content()).lower()