Debian Codegraph @

tests/test_simple_renderer.py

raw symbols SCIP
1 from typing import Dict
2
3 import jinja2
4 import pytest
5 from aiohttp import web
6 from aiohttp.test_utils import make_mocked_request
7
8 import aiohttp_jinja2
9
10
11 @pytest.mark.parametrize("enable_async", (False, True))
12 async def test_func(aiohttp_client, enable_async):
13 @aiohttp_jinja2.template("tmpl.jinja2")
14 async def func(request: web.Request) -> Dict[str, str]:
15 return {"head": "HEAD", "text": "text"}
16
17 template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"
18 app = web.Application()
19 aiohttp_jinja2.setup(
20 app,
21 enable_async=enable_async,
22 loader=jinja2.DictLoader({"tmpl.jinja2": template}),
23 )
24
25 app.router.add_route("*", "/", func)
26
27 client = await aiohttp_client(app)
28
29 resp = await client.get("/")
30 assert 200 == resp.status
31 txt = await resp.text()
32 assert "<html><body><h1>HEAD</h1>text</body></html>" == txt
33
34
35 async def test_render_class_based_view(aiohttp_client):
36 class MyView(web.View):
37 @aiohttp_jinja2.template("tmpl.jinja2")
38 async def get(self) -> Dict[str, str]:
39 return {"head": "HEAD", "text": "text"}
40
41 template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"
42
43 app = web.Application()
44 aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": template}))
45
46 app.router.add_route("*", "/", MyView)
47
48 client = await aiohttp_client(app)
49
50 resp = await client.get("/")
51
52 assert 200 == resp.status
53 txt = await resp.text()
54 assert "<html><body><h1>HEAD</h1>text</body></html>" == txt
55
56
57 async def test_meth(aiohttp_client):
58 class Handler:
59 @aiohttp_jinja2.template("tmpl.jinja2")
60 async def meth(self, request):
61 return {"head": "HEAD", "text": "text"}
62
63 template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"
64
65 handler = Handler()
66
67 app = web.Application()
68 aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": template}))
69
70 app.router.add_route("*", "/", handler.meth)
71
72 client = await aiohttp_client(app)
73
74 resp = await client.get("/")
75
76 assert 200 == resp.status
77 txt = await resp.text()
78 assert "<html><body><h1>HEAD</h1>text</body></html>" == txt
79
80
81 async def test_convert_func_to_coroutine(aiohttp_client):
82 @aiohttp_jinja2.template("tmpl.jinja2")
83 async def func(request):
84 return {"head": "HEAD", "text": "text"}
85
86 template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"
87
88 app = web.Application()
89 aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": template}))
90
91 app.router.add_route("*", "/", func)
92
93 client = await aiohttp_client(app)
94
95 resp = await client.get("/")
96
97 txt = await resp.text()
98 assert "<html><body><h1>HEAD</h1>text</body></html>" == txt
99
100
101 async def test_render_not_initialized():
102 async def func(request: web.Request) -> web.Response:
103 return aiohttp_jinja2.render_template("template", request, {})
104
105 app = web.Application()
106
107 app.router.add_route("GET", "/", func)
108
109 req = make_mocked_request("GET", "/", app=app)
110 msg = "Template engine is not initialized, call aiohttp_jinja2.setup() first"
111
112 with pytest.raises(web.HTTPInternalServerError) as ctx:
113 await func(req)
114
115 assert msg == ctx.value.text
116
117
118 async def test_set_status(aiohttp_client):
119 @aiohttp_jinja2.template("tmpl.jinja2", status=201)
120 async def func(request):
121 return {"head": "HEAD", "text": "text"}
122
123 template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"
124
125 app = web.Application()
126 aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": template}))
127
128 app.router.add_route("*", "/", func)
129
130 client = await aiohttp_client(app)
131
132 resp = await client.get("/")
133
134 assert 201 == resp.status
135 txt = await resp.text()
136 assert "<html><body><h1>HEAD</h1>text</body></html>" == txt
137
138
139 async def _test_render_template(func, aiohttp_client, enable_async):
140 template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"
141
142 app = web.Application()
143 aiohttp_jinja2.setup(
144 app,
145 enable_async=enable_async,
146 loader=jinja2.DictLoader({"tmpl.jinja2": template}),
147 )
148
149 app.router.add_route("*", "/", func)
150
151 client = await aiohttp_client(app)
152
153 resp = await client.get("/")
154
155 assert 200 == resp.status
156 txt = await resp.text()
157 assert "<html><body><h1>HEAD</h1>text</body></html>" == txt
158
159
160 async def test_render_template(aiohttp_client):
161 async def func(request):
162 return aiohttp_jinja2.render_template(
163 "tmpl.jinja2", request, {"head": "HEAD", "text": "text"}
164 )
165
166 await _test_render_template(func, aiohttp_client, enable_async=False)
167
168
169 async def test_render_template_async(aiohttp_client):
170 async def func(request):
171 return await aiohttp_jinja2.render_template_async(
172 "tmpl.jinja2", request, {"head": "HEAD", "text": "text"}
173 )
174
175 await _test_render_template(func, aiohttp_client, enable_async=True)
176
177
178 async def test_render_template_custom_status(aiohttp_client):
179 async def func(request):
180 return aiohttp_jinja2.render_template(
181 "tmpl.jinja2", request, {"head": "HEAD", "text": "text"}, status=404
182 )
183
184 template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"
185
186 app = web.Application()
187 aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": template}))
188
189 app.router.add_route("*", "/", func)
190
191 client = await aiohttp_client(app)
192
193 resp = await client.get("/")
194
195 assert 404 == resp.status
196 txt = await resp.text()
197 assert "<html><body><h1>HEAD</h1>text</body></html>" == txt
198
199
200 async def test_template_not_found():
201 async def func(request: web.Request) -> web.Response:
202 return aiohttp_jinja2.render_template("template", request, {})
203
204 app = web.Application()
205 aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({}))
206
207 app.router.add_route("GET", "/", func)
208
209 req = make_mocked_request("GET", "/", app=app)
210
211 with pytest.raises(web.HTTPInternalServerError) as ctx:
212 await func(req)
213
214 t = "Template 'template' not found"
215 assert t == ctx.value.text
216 assert t == ctx.value.reason
217
218
219 async def test_render_not_mapping():
220 @aiohttp_jinja2.template("tmpl.jinja2") # type: ignore[arg-type]
221 async def func(request: web.Request) -> int:
222 return 123
223
224 app = web.Application()
225 aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": "tmpl"}))
226
227 app.router.add_route("GET", "/", func)
228
229 req = make_mocked_request("GET", "/", app=app)
230 msg = "context should be mapping, not <class 'int'>"
231 with pytest.raises(web.HTTPInternalServerError) as ctx:
232 await func(req)
233
234 assert msg == ctx.value.text
235
236
237 async def test_render_without_context(aiohttp_client):
238 @aiohttp_jinja2.template("tmpl.jinja2")
239 async def func(request):
240 pass
241
242 template = "<html><body><p>{{text}}</p></body></html>"
243
244 app = web.Application()
245 aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": template}))
246
247 app.router.add_route("GET", "/", func)
248
249 client = await aiohttp_client(app)
250 resp = await client.get("/")
251
252 assert 200 == resp.status
253 txt = await resp.text()
254 assert "<html><body><p></p></body></html>" == txt
255
256
257 async def test_render_default_is_autoescaped(aiohttp_client):
258 @aiohttp_jinja2.template("tmpl.jinja2")
259 async def func(request):
260 return {"text": "<script>alert(1)</script>"}
261
262 app = web.Application()
263 aiohttp_jinja2.setup(
264 app, loader=jinja2.DictLoader({"tmpl.jinja2": "<html>{{text}}</html>"})
265 )
266
267 app.router.add_route("GET", "/", func)
268
269 client = await aiohttp_client(app)
270 resp = await client.get("/")
271
272 assert 200 == resp.status
273 txt = await resp.text()
274 assert "<html>&lt;script&gt;alert(1)&lt;/script&gt;</html>" == txt
275
276
277 async def test_render_can_disable_autoescape(aiohttp_client):
278 @aiohttp_jinja2.template("tmpl.jinja2")
279 async def func(request):
280 return {"text": "<script>alert(1)</script>"}
281
282 app = web.Application()
283 aiohttp_jinja2.setup(
284 app,
285 loader=jinja2.DictLoader({"tmpl.jinja2": "<html>{{text}}</html>"}),
286 autoescape=False,
287 )
288
289 app.router.add_route("GET", "/", func)
290
291 client = await aiohttp_client(app)
292 resp = await client.get("/")
293
294 assert 200 == resp.status
295 txt = await resp.text()
296 assert "<html><script>alert(1)</script></html>" == txt
297
298
299 async def test_skip_render_for_response_from_handler(aiohttp_client):
300 @aiohttp_jinja2.template("tmpl.jinja2")
301 async def func(request):
302 return web.Response(text="OK")
303
304 app = web.Application()
305 aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": "{{text}}"}))
306
307 app.router.add_route("GET", "/", func)
308
309 client = await aiohttp_client(app)
310 resp = await client.get("/")
311
312 assert 200 == resp.status
313 txt = await resp.text()
314 assert "OK" == txt