| 1 |
import jinja2 |
| 2 |
import pytest |
| 3 |
from aiohttp import web |
| 4 |
|
| 5 |
import aiohttp_jinja2 |
| 6 |
|
| 7 |
|
| 8 |
def test_get_env(): |
| 9 |
app = web.Application() |
| 10 |
aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": "tmpl"})) |
| 11 |
|
| 12 |
env = aiohttp_jinja2.get_env(app) |
| 13 |
assert isinstance(env, jinja2.Environment) |
| 14 |
assert env is aiohttp_jinja2.get_env(app) |
| 15 |
|
| 16 |
|
| 17 |
async def test_url(aiohttp_client): |
| 18 |
@aiohttp_jinja2.template("tmpl.jinja2") |
| 19 |
async def index(request): |
| 20 |
return {} |
| 21 |
|
| 22 |
async def other(request): |
| 23 |
"""Dummy handler.""" |
| 24 |
|
| 25 |
app = web.Application() |
| 26 |
aiohttp_jinja2.setup( |
| 27 |
app, |
| 28 |
loader=jinja2.DictLoader({"tmpl.jinja2": "{{ url('other', name='John_Doe')}}"}), |
| 29 |
) |
| 30 |
|
| 31 |
app.router.add_route("GET", "/", index) |
| 32 |
app.router.add_route("GET", "/user/{name}", other, name="other") |
| 33 |
client = await aiohttp_client(app) |
| 34 |
|
| 35 |
resp = await client.get("/") |
| 36 |
assert 200 == resp.status |
| 37 |
txt = await resp.text() |
| 38 |
assert "/user/John_Doe" == txt |
| 39 |
|
| 40 |
|
| 41 |
async def test_url_with_query(aiohttp_client): |
| 42 |
@aiohttp_jinja2.template("tmpl.jinja2") |
| 43 |
async def index(request): |
| 44 |
return {} |
| 45 |
|
| 46 |
app = web.Application() |
| 47 |
aiohttp_jinja2.setup( |
| 48 |
app, |
| 49 |
loader=jinja2.DictLoader( |
| 50 |
{"tmpl.jinja2": "{{ url('index', query_={'foo': 'bar'})}}"} |
| 51 |
), |
| 52 |
) |
| 53 |
|
| 54 |
app.router.add_get("/", index, name="index") |
| 55 |
client = await aiohttp_client(app) |
| 56 |
|
| 57 |
resp = await client.get("/") |
| 58 |
assert 200 == resp.status |
| 59 |
txt = await resp.text() |
| 60 |
assert "/?foo=bar" == txt |
| 61 |
|
| 62 |
|
| 63 |
async def test_url_int_param(aiohttp_client): |
| 64 |
@aiohttp_jinja2.template("tmpl.jinja2") |
| 65 |
async def index(request): |
| 66 |
return {} |
| 67 |
|
| 68 |
async def other(request): |
| 69 |
"""Dummy handler.""" |
| 70 |
|
| 71 |
app = web.Application() |
| 72 |
aiohttp_jinja2.setup( |
| 73 |
app, loader=jinja2.DictLoader({"tmpl.jinja2": "{{ url('other', arg=1)}}"}) |
| 74 |
) |
| 75 |
|
| 76 |
app.router.add_route("GET", "/", index) |
| 77 |
app.router.add_route("GET", "/uid/{arg}", other, name="other") |
| 78 |
client = await aiohttp_client(app) |
| 79 |
|
| 80 |
resp = await client.get("/") |
| 81 |
assert 200 == resp.status |
| 82 |
txt = await resp.text() |
| 83 |
assert "/uid/1" == txt |
| 84 |
|
| 85 |
|
| 86 |
async def test_url_param_forbidden_type(aiohttp_client): |
| 87 |
async def index(request): |
| 88 |
with pytest.raises( |
| 89 |
TypeError, |
| 90 |
match=( |
| 91 |
r"argument value should be str or int, " |
| 92 |
r"got arg -> \[<class 'bool'>\] True" |
| 93 |
), |
| 94 |
): |
| 95 |
aiohttp_jinja2.render_template("tmpl.jinja2", request, {}) |
| 96 |
return web.Response() |
| 97 |
|
| 98 |
async def other(request): |
| 99 |
"""Dummy handler.""" |
| 100 |
|
| 101 |
app = web.Application() |
| 102 |
aiohttp_jinja2.setup( |
| 103 |
app, loader=jinja2.DictLoader({"tmpl.jinja2": "{{ url('other', arg=True)}}"}) |
| 104 |
) |
| 105 |
|
| 106 |
app.router.add_route("GET", "/", index) |
| 107 |
app.router.add_route("GET", "/uid/{arg}", other, name="other") |
| 108 |
client = await aiohttp_client(app) |
| 109 |
|
| 110 |
resp = await client.get("/") |
| 111 |
assert 200 == resp.status |
| 112 |
|
| 113 |
|
| 114 |
async def test_helpers_disabled(aiohttp_client): |
| 115 |
async def index(request): |
| 116 |
with pytest.raises(jinja2.UndefinedError, match="'url' is undefined"): |
| 117 |
aiohttp_jinja2.render_template("tmpl.jinja2", request, {}) |
| 118 |
return web.Response() |
| 119 |
|
| 120 |
app = web.Application() |
| 121 |
aiohttp_jinja2.setup( |
| 122 |
app, |
| 123 |
default_helpers=False, |
| 124 |
loader=jinja2.DictLoader({"tmpl.jinja2": "{{ url('index')}}"}), |
| 125 |
) |
| 126 |
|
| 127 |
app.router.add_route("GET", "/", index) |
| 128 |
client = await aiohttp_client(app) |
| 129 |
|
| 130 |
resp = await client.get("/") |
| 131 |
assert 200 == resp.status |
| 132 |
|
| 133 |
|
| 134 |
async def test_static(aiohttp_client): |
| 135 |
@aiohttp_jinja2.template("tmpl.jinja2") |
| 136 |
async def index(request): |
| 137 |
return {} |
| 138 |
|
| 139 |
app = web.Application() |
| 140 |
aiohttp_jinja2.setup( |
| 141 |
app, loader=jinja2.DictLoader({"tmpl.jinja2": "{{ static('whatever.js') }}"}) |
| 142 |
) |
| 143 |
|
| 144 |
app[aiohttp_jinja2.static_root_key] = "/static" |
| 145 |
app.router.add_route("GET", "/", index) |
| 146 |
client = await aiohttp_client(app) |
| 147 |
|
| 148 |
resp = await client.get("/") |
| 149 |
assert 200 == resp.status |
| 150 |
txt = await resp.text() |
| 151 |
assert "/static/whatever.js" == txt |
| 152 |
|
| 153 |
|
| 154 |
async def test_static_var_missing(aiohttp_client, caplog): |
| 155 |
async def index(request): |
| 156 |
with pytest.raises(RuntimeError, match="static_root_key"): |
| 157 |
aiohttp_jinja2.render_template("tmpl.jinja2", request, {}) |
| 158 |
return web.Response() |
| 159 |
|
| 160 |
app = web.Application() |
| 161 |
aiohttp_jinja2.setup( |
| 162 |
app, loader=jinja2.DictLoader({"tmpl.jinja2": "{{ static('whatever.js') }}"}) |
| 163 |
) |
| 164 |
|
| 165 |
app.router.add_route("GET", "/", index) |
| 166 |
client = await aiohttp_client(app) |
| 167 |
|
| 168 |
resp = await client.get("/") |
| 169 |
assert 200 == resp.status |