| 1 | from typing import Dict, Tuple, Union |
| 2 | |
| 3 | import jinja2 |
| 4 | from aiohttp import web |
| 5 | |
| 6 | import aiohttp_jinja2 |
| 7 | |
| 8 | |
| 9 | async def test_context_processors(aiohttp_client): |
| 10 | @aiohttp_jinja2.template("tmpl.jinja2") |
| 11 | async def func(request): |
| 12 | return {"bar": 2} |
| 13 | |
| 14 | app = web.Application(middlewares=[aiohttp_jinja2.context_processors_middleware]) |
| 15 | aiohttp_jinja2.setup( |
| 16 | app, |
| 17 | loader=jinja2.DictLoader( |
| 18 | {"tmpl.jinja2": "foo: {{ foo }}, bar: {{ bar }}, path: {{ request.path }}"} |
| 19 | ), |
| 20 | ) |
| 21 | |
| 22 | async def processor(request: web.Request) -> Dict[str, Union[str, int]]: |
| 23 | return {"foo": 1, "bar": "should be overwriten"} |
| 24 | |
| 25 | f: Tuple[aiohttp_jinja2._ContextProcessor, ...] = ( |
| 26 | aiohttp_jinja2.request_processor, |
| 27 | processor, |
| 28 | ) |
| 29 | app[aiohttp_jinja2.APP_CONTEXT_PROCESSORS_KEY] = f |
| 30 | |
| 31 | app.router.add_get("/", func) |
| 32 | |
| 33 | client = await aiohttp_client(app) |
| 34 | |
| 35 | resp = await client.get("/") |
| 36 | assert 200 == resp.status |
| 37 | txt = await resp.text() |
| 38 | assert "foo: 1, bar: 2, path: /" == txt |
| 39 | |
| 40 | |
| 41 | async def test_nested_context_processors(aiohttp_client): |
| 42 | @aiohttp_jinja2.template("tmpl.jinja2") |
| 43 | async def func(request): |
| 44 | return {"bar": 2} |
| 45 | |
| 46 | subapp = web.Application(middlewares=[aiohttp_jinja2.context_processors_middleware]) |
| 47 | aiohttp_jinja2.setup( |
| 48 | subapp, |
| 49 | loader=jinja2.DictLoader( |
| 50 | { |
| 51 | "tmpl.jinja2": "foo: {{ foo }}, bar: {{ bar }}, " |
| 52 | "baz: {{ baz }}, path: {{ request.path }}" |
| 53 | } |
| 54 | ), |
| 55 | ) |
| 56 | |
| 57 | async def subprocessor(request): |
| 58 | return {"foo": 1, "bar": "should be overwriten"} |
| 59 | |
| 60 | subapp[aiohttp_jinja2.APP_CONTEXT_PROCESSORS_KEY] = ( |
| 61 | aiohttp_jinja2.request_processor, |
| 62 | subprocessor, |
| 63 | ) |
| 64 | |
| 65 | subapp.router.add_get("/", func) |
| 66 | |
| 67 | app = web.Application(middlewares=[aiohttp_jinja2.context_processors_middleware]) |
| 68 | aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({})) |
| 69 | |
| 70 | async def processor(request): |
| 71 | return {"baz": 5} |
| 72 | |
| 73 | app[aiohttp_jinja2.APP_CONTEXT_PROCESSORS_KEY] = ( |
| 74 | aiohttp_jinja2.request_processor, |
| 75 | processor, |
| 76 | ) |
| 77 | |
| 78 | app.add_subapp("/sub/", subapp) |
| 79 | |
| 80 | client = await aiohttp_client(app) |
| 81 | |
| 82 | resp = await client.get("/sub/") |
| 83 | assert 200 == resp.status |
| 84 | txt = await resp.text() |
| 85 | assert "foo: 1, bar: 2, baz: 5, path: /sub/" == txt |
| 86 | |
| 87 | |
| 88 | async def test_context_is_response(aiohttp_client): |
| 89 | @aiohttp_jinja2.template("tmpl.jinja2") |
| 90 | async def func(request): |
| 91 | raise web.HTTPForbidden() |
| 92 | |
| 93 | app = web.Application() |
| 94 | aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({"tmpl.jinja2": "template"})) |
| 95 | |
| 96 | app.router.add_route("GET", "/", func) |
| 97 | client = await aiohttp_client(app) |
| 98 | |
| 99 | resp = await client.get("/") |
| 100 | assert 403 == resp.status |
| 101 | |
| 102 | |
| 103 | async def test_context_processors_new_setup_style(aiohttp_client): |
| 104 | @aiohttp_jinja2.template("tmpl.jinja2") |
| 105 | async def func(request): |
| 106 | return {"bar": 2} |
| 107 | |
| 108 | async def processor(request): |
| 109 | return {"foo": 1, "bar": "should be overwriten"} |
| 110 | |
| 111 | app = web.Application() |
| 112 | aiohttp_jinja2.setup( |
| 113 | app, |
| 114 | loader=jinja2.DictLoader( |
| 115 | { |
| 116 | "tmpl.jinja2": "foo: {{ foo }}, bar: {{ bar }}, " |
| 117 | "path: {{ request.path }}" |
| 118 | } |
| 119 | ), |
| 120 | context_processors=(aiohttp_jinja2.request_processor, processor), |
| 121 | ) |
| 122 | |
| 123 | app.router.add_route("GET", "/", func) |
| 124 | client = await aiohttp_client(app) |
| 125 | |
| 126 | resp = await client.get("/") |
| 127 | assert 200 == resp.status |
| 128 | txt = await resp.text() |
| 129 | assert "foo: 1, bar: 2, path: /" == txt |
| 130 | |
| 131 | |
| 132 | async def test_context_not_tainted(aiohttp_client): |
| 133 | global_context = {"version": 1} |
| 134 | |
| 135 | @aiohttp_jinja2.template("tmpl.jinja2") |
| 136 | async def func(request): |
| 137 | return global_context |
| 138 | |
| 139 | async def processor(request): |
| 140 | return {"foo": 1} |
| 141 | |
| 142 | app = web.Application() |
| 143 | aiohttp_jinja2.setup( |
| 144 | app, |
| 145 | loader=jinja2.DictLoader({"tmpl.jinja2": "foo: {{ foo }}"}), |
| 146 | context_processors=[processor], |
| 147 | ) |
| 148 | |
| 149 | app.router.add_get("/", func) |
| 150 | client = await aiohttp_client(app) |
| 151 | |
| 152 | resp = await client.get("/") |
| 153 | assert 200 == resp.status |
| 154 | txt = await resp.text() |
| 155 | assert "foo: 1" == txt |
| 156 | |
| 157 | assert "foo" not in global_context |