async def test_context_processors(aiohttp_client): @aiohttp_tal.template('tmpl.pt') async def func(request): return {'bar': 2} app = web.Application( middlewares=[aiohttp_tal.context_processors_middleware]) aiohttp_tal.setup( app, loader={'tmpl.pt': 'foo: ${foo}, bar: ${bar}, path: ${request.path}'}) async def processor(request): return {'foo': 1, 'bar': 'should be overwriten'} app['aiohttp_tal_context_processors'] = ( aiohttp_tal.request_processor, processor, ) app.router.add_get('/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert 'foo: 1, bar: 2, path: /' == txt
async def test_render_bare_funcs_deprecated(aiohttp_client): def wrapper(func): async def wrapped(request): with pytest.warns(DeprecationWarning, match='Bare functions are deprecated'): return await func(request) return wrapped @wrapper @aiohttp_tal.template('tmpl.pt') def func(request): return {'text': 'OK'} app = web.Application() aiohttp_tal.setup(app, loader={'tmpl.pt': '${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
async def test_context_not_tainted(aiohttp_client): global_context = {'version': 1} @aiohttp_tal.template('tmpl.pt') async def func(request): return global_context async def processor(request): return {'foo': 1} app = web.Application() aiohttp_tal.setup(app, loader={'tmpl.pt': 'foo: ${ foo }'}, context_processors=[processor]) app.router.add_get('/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert 'foo: 1' == txt assert 'foo' not in global_context
def test_get_env(): app = web.Application() aiohttp_tal.setup(app, loader={'tmpl.pt': "tmpl"}) env = aiohttp_tal.get_env(app) assert isinstance(env, aiohttp_tal.Environment) assert env is aiohttp_tal.get_env(app)
async def test_context_processors_new_setup_style(aiohttp_client): @aiohttp_tal.template('tmpl.pt') async def func(request): return {'bar': 2} async def processor(request): return {'foo': 1, 'bar': 'should be overwriten'} app = web.Application() aiohttp_tal.setup(app, loader={ 'tmpl.pt': 'foo: ${foo}, bar: ${bar}, ' 'path: ${request.path}' }, context_processors=(aiohttp_tal.request_processor, processor)) app.router.add_route('GET', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert 'foo: 1, bar: 2, path: /' == txt
async def test_context_is_response(aiohttp_client): @aiohttp_tal.template('tmpl.pt') async def func(request): raise web.HTTPForbidden() app = web.Application() aiohttp_tal.setup(app, loader={'tmpl.pt': "template"}) app.router.add_route('GET', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 403 == resp.status
async def test_static_var_missing(aiohttp_client, caplog): async def index(request): with pytest.raises(RuntimeError, match='static_root_url'): aiohttp_tal.render_template('tmpl.pt', request, {}) return web.Response() app = web.Application() aiohttp_tal.setup(app, loader={'tmpl.pt': "${ static('whatever.js') }"}) app.router.add_route('GET', '/', index) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status # static_root_url is not set
async def test_skip_render_for_response_from_handler(aiohttp_client): @aiohttp_tal.template('tmpl.pt') async def func(request): return web.Response(text='OK') app = web.Application() aiohttp_tal.setup(app, loader={'tmpl.pt': '${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
async def test_render_default_is_autoescaped(aiohttp_client): @aiohttp_tal.template('tmpl.pt') async def func(request): return {'text': '<script>alert(1)</script>'} app = web.Application() aiohttp_tal.setup(app, loader={'tmpl.pt': '<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><script>alert(1)</script></html>' == txt
async def test_render_not_mapping(): @aiohttp_tal.template('tmpl.pt') async def func(request): return 123 app = web.Application() aiohttp_tal.setup(app, loader={'tmpl.pt': '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_helpers_disabled(aiohttp_client): async def index(request): with pytest.raises(NameError, match="url"): aiohttp_tal.render_template('tmpl.pt', request, {}) return web.Response() app = web.Application() aiohttp_tal.setup(app, default_helpers=False, loader={'tmpl.pt': "${url('index')}"}) app.router.add_route('GET', '/', index) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status
async def test_url_with_query(aiohttp_client): @aiohttp_tal.template('tmpl.pt') async def index(request): return {} app = web.Application() aiohttp_tal.setup( app, loader={'tmpl.pt': "${ 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_static(aiohttp_client): @aiohttp_tal.template('tmpl.pt') async def index(request): return {} app = web.Application() aiohttp_tal.setup(app, loader={'tmpl.pt': "${ static('whatever.js') }"}) app['static_root_url'] = '/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_template_not_found(): async def func(request): return aiohttp_tal.render_template('template', request, {}) app = web.Application() aiohttp_tal.setup(app, loader={}) 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 create_app(): app = web.Application(middlewares=[babel_middleware]) app.update(name='Testing aiohttp TAL') tal_loader = PageTemplateLoader( str(THIS_DIR / 'templates'), translate=translate, auto_reload=True # debugging ) aiohttp_tal.setup(app, loader=tal_loader) app.add_routes([web.static('/static', str(THIS_DIR / 'static'))]) app['static_root_url'] = '/static' app.router.add_get('/', index, name='index') app.router.add_get('/translation', translation, name='translation') return app
async def create_app(): app = web.Application() app.update(name='Testing aiohttp TAL') tal_loader = PageTemplateLoader( str(THIS_DIR / 'templates'), enable_data_attributes=True, # data-tal-* auto_reload=True # debugging ) aiohttp_tal.setup(app, loader=tal_loader) app.add_routes([web.static('/static', str(THIS_DIR / 'static'))]) app['static_root_url'] = '/static' app.router.add_get('/', index, name='index') app.router.add_get('/page', page, name='translation') return app
async def test_func(aiohttp_client): @aiohttp_tal.template('tmpl.pt') async def func(request): return {'head': 'HEAD', 'text': 'text'} template = '<html><body><h1>${head}</h1>${text}</body></html>' app = web.Application() aiohttp_tal.setup(app, loader={'tmpl.pt': chameleon.PageTemplate(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_url_int_param(aiohttp_client): @aiohttp_tal.template('tmpl.pt') async def index(request): return {} async def other(request): return app = web.Application() aiohttp_tal.setup(app, loader={'tmpl.pt': "${ 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_filters(aiohttp_client): @aiohttp_tal.template('tmpl.pt') async def index(request): return {} def add_2(value): return value + 2 app = web.Application() aiohttp_tal.setup(app, loader={'tmpl.pt': "${ add_2(5) }"}, filters={'add_2': add_2}) app.router.add_route('GET', '/', index) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '7' == 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_tal.render_template('tmpl.pt', request, {}) return web.Response() async def other(request): return app = web.Application() aiohttp_tal.setup(app, loader={'tmpl.pt': "${ 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