File: test_jinja_globals.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 (169 lines) | stat: -rw-r--r-- 4,666 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
import jinja2
import pytest
from aiohttp import web

import aiohttp_jinja2


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

    env = aiohttp_jinja2.get_env(app)
    assert isinstance(env, jinja2.Environment)
    assert env is aiohttp_jinja2.get_env(app)


async def test_url(aiohttp_client):
    @aiohttp_jinja2.template("tmpl.jinja2")
    async def index(request):
        return {}

    async def other(request):
        """Dummy handler."""

    app = web.Application()
    aiohttp_jinja2.setup(
        app,
        loader=jinja2.DictLoader({"tmpl.jinja2": "{{ url('other', name='John_Doe')}}"}),
    )

    app.router.add_route("GET", "/", index)
    app.router.add_route("GET", "/user/{name}", other, name="other")
    client = await aiohttp_client(app)

    resp = await client.get("/")
    assert 200 == resp.status
    txt = await resp.text()
    assert "/user/John_Doe" == txt


async def test_url_with_query(aiohttp_client):
    @aiohttp_jinja2.template("tmpl.jinja2")
    async def index(request):
        return {}

    app = web.Application()
    aiohttp_jinja2.setup(
        app,
        loader=jinja2.DictLoader(
            {"tmpl.jinja2": "{{ url('index', query_={'foo': 'bar'})}}"}
        ),
    )

    app.router.add_get("/", index, name="index")
    client = await aiohttp_client(app)

    resp = await client.get("/")
    assert 200 == resp.status
    txt = await resp.text()
    assert "/?foo=bar" == txt


async def test_url_int_param(aiohttp_client):
    @aiohttp_jinja2.template("tmpl.jinja2")
    async def index(request):
        return {}

    async def other(request):
        """Dummy handler."""

    app = web.Application()
    aiohttp_jinja2.setup(
        app, loader=jinja2.DictLoader({"tmpl.jinja2": "{{ url('other', arg=1)}}"})
    )

    app.router.add_route("GET", "/", index)
    app.router.add_route("GET", "/uid/{arg}", other, name="other")
    client = await aiohttp_client(app)

    resp = await client.get("/")
    assert 200 == resp.status
    txt = await resp.text()
    assert "/uid/1" == txt


async def test_url_param_forbidden_type(aiohttp_client):
    async def index(request):
        with pytest.raises(
            TypeError,
            match=(
                r"argument value should be str or int, "
                r"got arg -> \[<class 'bool'>\] True"
            ),
        ):
            aiohttp_jinja2.render_template("tmpl.jinja2", request, {})
        return web.Response()

    async def other(request):
        """Dummy handler."""

    app = web.Application()
    aiohttp_jinja2.setup(
        app, loader=jinja2.DictLoader({"tmpl.jinja2": "{{ url('other', arg=True)}}"})
    )

    app.router.add_route("GET", "/", index)
    app.router.add_route("GET", "/uid/{arg}", other, name="other")
    client = await aiohttp_client(app)

    resp = await client.get("/")
    assert 200 == resp.status


async def test_helpers_disabled(aiohttp_client):
    async def index(request):
        with pytest.raises(jinja2.UndefinedError, match="'url' is undefined"):
            aiohttp_jinja2.render_template("tmpl.jinja2", request, {})
        return web.Response()

    app = web.Application()
    aiohttp_jinja2.setup(
        app,
        default_helpers=False,
        loader=jinja2.DictLoader({"tmpl.jinja2": "{{ url('index')}}"}),
    )

    app.router.add_route("GET", "/", index)
    client = await aiohttp_client(app)

    resp = await client.get("/")
    assert 200 == resp.status


async def test_static(aiohttp_client):
    @aiohttp_jinja2.template("tmpl.jinja2")
    async def index(request):
        return {}

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

    app[aiohttp_jinja2.static_root_key] = "/static"
    app.router.add_route("GET", "/", index)
    client = await aiohttp_client(app)

    resp = await client.get("/")
    assert 200 == resp.status
    txt = await resp.text()
    assert "/static/whatever.js" == txt


async def test_static_var_missing(aiohttp_client, caplog):
    async def index(request):
        with pytest.raises(RuntimeError, match="static_root_key"):
            aiohttp_jinja2.render_template("tmpl.jinja2", request, {})
        return web.Response()

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

    app.router.add_route("GET", "/", index)
    client = await aiohttp_client(app)

    resp = await client.get("/")
    assert 200 == resp.status  # static_root_key is not set