File: test_simple_renderer.py

package info (click to toggle)
aiohttp-jinja2 1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 264 kB
  • sloc: python: 884; makefile: 182
file content (314 lines) | stat: -rw-r--r-- 8,965 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
from typing import Dict

import jinja2
import pytest
from aiohttp import web
from aiohttp.test_utils import make_mocked_request

import aiohttp_jinja2


@pytest.mark.parametrize("enable_async", (False, True))
async def test_func(aiohttp_client, enable_async):
    @aiohttp_jinja2.template("tmpl.jinja2")
    async def func(request: web.Request) -> Dict[str, str]:
        return {"head": "HEAD", "text": "text"}

    template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"
    app = web.Application()
    aiohttp_jinja2.setup(
        app,
        enable_async=enable_async,
        loader=jinja2.DictLoader({"tmpl.jinja2": template}),
    )

    app.router.add_route("*", "/", func)

    client = await aiohttp_client(app)

    resp = await client.get("/")
    assert 200 == resp.status
    txt = await resp.text()
    assert "<html><body><h1>HEAD</h1>text</body></html>" == txt


async def test_render_class_based_view(aiohttp_client):
    class MyView(web.View):
        @aiohttp_jinja2.template("tmpl.jinja2")
        async def get(self) -> Dict[str, str]:
            return {"head": "HEAD", "text": "text"}

    template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": template}))

    app.router.add_route("*", "/", MyView)

    client = await aiohttp_client(app)

    resp = await client.get("/")

    assert 200 == resp.status
    txt = await resp.text()
    assert "<html><body><h1>HEAD</h1>text</body></html>" == txt


async def test_meth(aiohttp_client):
    class Handler:
        @aiohttp_jinja2.template("tmpl.jinja2")
        async def meth(self, request):
            return {"head": "HEAD", "text": "text"}

    template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"

    handler = Handler()

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": template}))

    app.router.add_route("*", "/", handler.meth)

    client = await aiohttp_client(app)

    resp = await client.get("/")

    assert 200 == resp.status
    txt = await resp.text()
    assert "<html><body><h1>HEAD</h1>text</body></html>" == txt


async def test_convert_func_to_coroutine(aiohttp_client):
    @aiohttp_jinja2.template("tmpl.jinja2")
    async def func(request):
        return {"head": "HEAD", "text": "text"}

    template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": template}))

    app.router.add_route("*", "/", func)

    client = await aiohttp_client(app)

    resp = await client.get("/")

    txt = await resp.text()
    assert "<html><body><h1>HEAD</h1>text</body></html>" == txt


async def test_render_not_initialized():
    async def func(request: web.Request) -> web.Response:
        return aiohttp_jinja2.render_template("template", request, {})

    app = web.Application()

    app.router.add_route("GET", "/", func)

    req = make_mocked_request("GET", "/", app=app)
    msg = "Template engine is not initialized, call aiohttp_jinja2.setup() first"

    with pytest.raises(web.HTTPInternalServerError) as ctx:
        await func(req)

    assert msg == ctx.value.text


async def test_set_status(aiohttp_client):
    @aiohttp_jinja2.template("tmpl.jinja2", status=201)
    async def func(request):
        return {"head": "HEAD", "text": "text"}

    template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": template}))

    app.router.add_route("*", "/", func)

    client = await aiohttp_client(app)

    resp = await client.get("/")

    assert 201 == resp.status
    txt = await resp.text()
    assert "<html><body><h1>HEAD</h1>text</body></html>" == txt


async def _test_render_template(func, aiohttp_client, enable_async):
    template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"

    app = web.Application()
    aiohttp_jinja2.setup(
        app,
        enable_async=enable_async,
        loader=jinja2.DictLoader({"tmpl.jinja2": template}),
    )

    app.router.add_route("*", "/", func)

    client = await aiohttp_client(app)

    resp = await client.get("/")

    assert 200 == resp.status
    txt = await resp.text()
    assert "<html><body><h1>HEAD</h1>text</body></html>" == txt


async def test_render_template(aiohttp_client):
    async def func(request):
        return aiohttp_jinja2.render_template(
            "tmpl.jinja2", request, {"head": "HEAD", "text": "text"}
        )

    await _test_render_template(func, aiohttp_client, enable_async=False)


async def test_render_template_async(aiohttp_client):
    async def func(request):
        return await aiohttp_jinja2.render_template_async(
            "tmpl.jinja2", request, {"head": "HEAD", "text": "text"}
        )

    await _test_render_template(func, aiohttp_client, enable_async=True)


async def test_render_template_custom_status(aiohttp_client):
    async def func(request):
        return aiohttp_jinja2.render_template(
            "tmpl.jinja2", request, {"head": "HEAD", "text": "text"}, status=404
        )

    template = "<html><body><h1>{{head}}</h1>{{text}}</body></html>"

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": template}))

    app.router.add_route("*", "/", func)

    client = await aiohttp_client(app)

    resp = await client.get("/")

    assert 404 == resp.status
    txt = await resp.text()
    assert "<html><body><h1>HEAD</h1>text</body></html>" == txt


async def test_template_not_found():
    async def func(request: web.Request) -> web.Response:
        return aiohttp_jinja2.render_template("template", request, {})

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({}))

    app.router.add_route("GET", "/", func)

    req = make_mocked_request("GET", "/", app=app)

    with pytest.raises(web.HTTPInternalServerError) as ctx:
        await func(req)

    t = "Template 'template' not found"
    assert t == ctx.value.text
    assert t == ctx.value.reason


async def test_render_not_mapping():
    @aiohttp_jinja2.template("tmpl.jinja2")  # type: ignore[arg-type]
    async def func(request: web.Request) -> int:
        return 123

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": "tmpl"}))

    app.router.add_route("GET", "/", func)

    req = make_mocked_request("GET", "/", app=app)
    msg = "context should be mapping, not <class 'int'>"
    with pytest.raises(web.HTTPInternalServerError) as ctx:
        await func(req)

    assert msg == ctx.value.text


async def test_render_without_context(aiohttp_client):
    @aiohttp_jinja2.template("tmpl.jinja2")
    async def func(request):
        pass

    template = "<html><body><p>{{text}}</p></body></html>"

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": template}))

    app.router.add_route("GET", "/", func)

    client = await aiohttp_client(app)
    resp = await client.get("/")

    assert 200 == resp.status
    txt = await resp.text()
    assert "<html><body><p></p></body></html>" == txt


async def test_render_default_is_autoescaped(aiohttp_client):
    @aiohttp_jinja2.template("tmpl.jinja2")
    async def func(request):
        return {"text": "<script>alert(1)</script>"}

    app = web.Application()
    aiohttp_jinja2.setup(
        app, loader=jinja2.DictLoader({"tmpl.jinja2": "<html>{{text}}</html>"})
    )

    app.router.add_route("GET", "/", func)

    client = await aiohttp_client(app)
    resp = await client.get("/")

    assert 200 == resp.status
    txt = await resp.text()
    assert "<html>&lt;script&gt;alert(1)&lt;/script&gt;</html>" == txt


async def test_render_can_disable_autoescape(aiohttp_client):
    @aiohttp_jinja2.template("tmpl.jinja2")
    async def func(request):
        return {"text": "<script>alert(1)</script>"}

    app = web.Application()
    aiohttp_jinja2.setup(
        app,
        loader=jinja2.DictLoader({"tmpl.jinja2": "<html>{{text}}</html>"}),
        autoescape=False,
    )

    app.router.add_route("GET", "/", func)

    client = await aiohttp_client(app)
    resp = await client.get("/")

    assert 200 == resp.status
    txt = await resp.text()
    assert "<html><script>alert(1)</script></html>" == txt


async def test_skip_render_for_response_from_handler(aiohttp_client):
    @aiohttp_jinja2.template("tmpl.jinja2")
    async def func(request):
        return web.Response(text="OK")

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": "{{text}}"}))

    app.router.add_route("GET", "/", func)

    client = await aiohttp_client(app)
    resp = await client.get("/")

    assert 200 == resp.status
    txt = await resp.text()
    assert "OK" == txt