|
|
|
@ -9,6 +9,8 @@
|
|
|
|
|
:license: BSD, see LICENSE for more details. |
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
import pytest |
|
|
|
|
|
|
|
|
|
import flask |
|
|
|
|
import unittest |
|
|
|
|
import logging |
|
|
|
@ -17,9 +19,7 @@ from jinja2 import TemplateNotFound
|
|
|
|
|
from tests import TestFlask |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestTemplating(TestFlask): |
|
|
|
|
|
|
|
|
|
def test_context_processing(self): |
|
|
|
|
def test_context_processing(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.context_processor |
|
|
|
|
def context_processor(): |
|
|
|
@ -30,7 +30,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert rv.data == b'<p>23|42' |
|
|
|
|
|
|
|
|
|
def test_original_win(self): |
|
|
|
|
def test_original_win(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.route('/') |
|
|
|
|
def index(): |
|
|
|
@ -38,7 +38,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert rv.data == b'42' |
|
|
|
|
|
|
|
|
|
def test_request_less_rendering(self): |
|
|
|
|
def test_request_less_rendering(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
app.config['WORLD_NAME'] = 'Special World' |
|
|
|
|
@app.context_processor |
|
|
|
@ -50,7 +50,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
'{{ foo }}') |
|
|
|
|
assert rv == 'Hello Special World 42' |
|
|
|
|
|
|
|
|
|
def test_standard_context(self): |
|
|
|
|
def test_standard_context(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
app.secret_key = 'development key' |
|
|
|
|
@app.route('/') |
|
|
|
@ -66,7 +66,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/?foo=42') |
|
|
|
|
assert rv.data.split() == [b'42', b'23', b'False', b'aha'] |
|
|
|
|
|
|
|
|
|
def test_escaping(self): |
|
|
|
|
def test_escaping(): |
|
|
|
|
text = '<p>Hello World!' |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.route('/') |
|
|
|
@ -83,7 +83,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
b'<p>Hello World!' |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
def test_no_escaping(self): |
|
|
|
|
def test_no_escaping(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
with app.test_request_context(): |
|
|
|
|
assert flask.render_template_string( |
|
|
|
@ -91,13 +91,13 @@ class TestTemplating(TestFlask):
|
|
|
|
|
assert flask.render_template('mail.txt', foo='<test>') == \ |
|
|
|
|
'<test> Mail' |
|
|
|
|
|
|
|
|
|
def test_macros(self): |
|
|
|
|
def test_macros(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
with app.test_request_context(): |
|
|
|
|
macro = flask.get_template_attribute('_macro.html', 'hello') |
|
|
|
|
assert macro('World') == 'Hello World!' |
|
|
|
|
|
|
|
|
|
def test_template_filter(self): |
|
|
|
|
def test_template_filter(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.template_filter() |
|
|
|
|
def my_reverse(s): |
|
|
|
@ -106,7 +106,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
assert app.jinja_env.filters['my_reverse'] == my_reverse |
|
|
|
|
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba' |
|
|
|
|
|
|
|
|
|
def test_add_template_filter(self): |
|
|
|
|
def test_add_template_filter(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
def my_reverse(s): |
|
|
|
|
return s[::-1] |
|
|
|
@ -115,7 +115,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
assert app.jinja_env.filters['my_reverse'] == my_reverse |
|
|
|
|
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba' |
|
|
|
|
|
|
|
|
|
def test_template_filter_with_name(self): |
|
|
|
|
def test_template_filter_with_name(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.template_filter('strrev') |
|
|
|
|
def my_reverse(s): |
|
|
|
@ -124,7 +124,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
assert app.jinja_env.filters['strrev'] == my_reverse |
|
|
|
|
assert app.jinja_env.filters['strrev']('abcd') == 'dcba' |
|
|
|
|
|
|
|
|
|
def test_add_template_filter_with_name(self): |
|
|
|
|
def test_add_template_filter_with_name(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
def my_reverse(s): |
|
|
|
|
return s[::-1] |
|
|
|
@ -133,7 +133,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
assert app.jinja_env.filters['strrev'] == my_reverse |
|
|
|
|
assert app.jinja_env.filters['strrev']('abcd') == 'dcba' |
|
|
|
|
|
|
|
|
|
def test_template_filter_with_template(self): |
|
|
|
|
def test_template_filter_with_template(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.template_filter() |
|
|
|
|
def super_reverse(s): |
|
|
|
@ -144,7 +144,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert rv.data == b'dcba' |
|
|
|
|
|
|
|
|
|
def test_add_template_filter_with_template(self): |
|
|
|
|
def test_add_template_filter_with_template(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
def super_reverse(s): |
|
|
|
|
return s[::-1] |
|
|
|
@ -155,7 +155,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert rv.data == b'dcba' |
|
|
|
|
|
|
|
|
|
def test_template_filter_with_name_and_template(self): |
|
|
|
|
def test_template_filter_with_name_and_template(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.template_filter('super_reverse') |
|
|
|
|
def my_reverse(s): |
|
|
|
@ -166,7 +166,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert rv.data == b'dcba' |
|
|
|
|
|
|
|
|
|
def test_add_template_filter_with_name_and_template(self): |
|
|
|
|
def test_add_template_filter_with_name_and_template(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
def my_reverse(s): |
|
|
|
|
return s[::-1] |
|
|
|
@ -177,7 +177,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert rv.data == b'dcba' |
|
|
|
|
|
|
|
|
|
def test_template_test(self): |
|
|
|
|
def test_template_test(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.template_test() |
|
|
|
|
def boolean(value): |
|
|
|
@ -186,7 +186,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
assert app.jinja_env.tests['boolean'] == boolean |
|
|
|
|
assert app.jinja_env.tests['boolean'](False) |
|
|
|
|
|
|
|
|
|
def test_add_template_test(self): |
|
|
|
|
def test_add_template_test(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
def boolean(value): |
|
|
|
|
return isinstance(value, bool) |
|
|
|
@ -195,7 +195,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
assert app.jinja_env.tests['boolean'] == boolean |
|
|
|
|
assert app.jinja_env.tests['boolean'](False) |
|
|
|
|
|
|
|
|
|
def test_template_test_with_name(self): |
|
|
|
|
def test_template_test_with_name(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.template_test('boolean') |
|
|
|
|
def is_boolean(value): |
|
|
|
@ -204,7 +204,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
assert app.jinja_env.tests['boolean'] == is_boolean |
|
|
|
|
assert app.jinja_env.tests['boolean'](False) |
|
|
|
|
|
|
|
|
|
def test_add_template_test_with_name(self): |
|
|
|
|
def test_add_template_test_with_name(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
def is_boolean(value): |
|
|
|
|
return isinstance(value, bool) |
|
|
|
@ -213,7 +213,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
assert app.jinja_env.tests['boolean'] == is_boolean |
|
|
|
|
assert app.jinja_env.tests['boolean'](False) |
|
|
|
|
|
|
|
|
|
def test_template_test_with_template(self): |
|
|
|
|
def test_template_test_with_template(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.template_test() |
|
|
|
|
def boolean(value): |
|
|
|
@ -224,7 +224,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert b'Success!' in rv.data |
|
|
|
|
|
|
|
|
|
def test_add_template_test_with_template(self): |
|
|
|
|
def test_add_template_test_with_template(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
def boolean(value): |
|
|
|
|
return isinstance(value, bool) |
|
|
|
@ -235,7 +235,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert b'Success!' in rv.data |
|
|
|
|
|
|
|
|
|
def test_template_test_with_name_and_template(self): |
|
|
|
|
def test_template_test_with_name_and_template(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.template_test('boolean') |
|
|
|
|
def is_boolean(value): |
|
|
|
@ -246,7 +246,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert b'Success!' in rv.data |
|
|
|
|
|
|
|
|
|
def test_add_template_test_with_name_and_template(self): |
|
|
|
|
def test_add_template_test_with_name_and_template(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
def is_boolean(value): |
|
|
|
|
return isinstance(value, bool) |
|
|
|
@ -257,7 +257,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert b'Success!' in rv.data |
|
|
|
|
|
|
|
|
|
def test_add_template_global(self): |
|
|
|
|
def test_add_template_global(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.template_global() |
|
|
|
|
def get_stuff(): |
|
|
|
@ -269,7 +269,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = flask.render_template_string('{{ get_stuff() }}') |
|
|
|
|
assert rv == '42' |
|
|
|
|
|
|
|
|
|
def test_custom_template_loader(self): |
|
|
|
|
def test_custom_template_loader(): |
|
|
|
|
class MyFlask(flask.Flask): |
|
|
|
|
def create_global_jinja_loader(self): |
|
|
|
|
from jinja2 import DictLoader |
|
|
|
@ -282,7 +282,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = c.get('/') |
|
|
|
|
assert rv.data == b'Hello Custom World!' |
|
|
|
|
|
|
|
|
|
def test_iterable_loader(self): |
|
|
|
|
def test_iterable_loader(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
@app.context_processor |
|
|
|
|
def context_processor(): |
|
|
|
@ -298,7 +298,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
assert rv.data == b'<h1>Jameson</h1>' |
|
|
|
|
|
|
|
|
|
def test_templates_auto_reload(self): |
|
|
|
|
def test_templates_auto_reload(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
assert app.config['TEMPLATES_AUTO_RELOAD'] |
|
|
|
|
assert app.jinja_env.auto_reload |
|
|
|
@ -306,7 +306,7 @@ class TestTemplating(TestFlask):
|
|
|
|
|
app.config['TEMPLATES_AUTO_RELOAD'] = False |
|
|
|
|
assert not app.jinja_env.auto_reload |
|
|
|
|
|
|
|
|
|
def test_template_loader_debugging(self): |
|
|
|
|
def test_template_loader_debugging(): |
|
|
|
|
from blueprintapp import app |
|
|
|
|
|
|
|
|
|
called = [] |
|
|
|
@ -331,20 +331,12 @@ class TestTemplating(TestFlask):
|
|
|
|
|
app.logger.handlers = [_TestHandler()] |
|
|
|
|
app.config['EXPLAIN_TEMPLATE_LOADING'] = True |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
with pytest.raises(TemplateNotFound) as excinfo: |
|
|
|
|
c.get('/missing') |
|
|
|
|
except TemplateNotFound as e: |
|
|
|
|
assert 'missing_template.html' in str(e) |
|
|
|
|
else: |
|
|
|
|
self.fail('Expected template not found exception.') |
|
|
|
|
|
|
|
|
|
assert 'missing_template.html' in str(excinfo.value) |
|
|
|
|
finally: |
|
|
|
|
app.logger.handlers[:] = old_handlers |
|
|
|
|
app.config['EXPLAIN_TEMPLATE_LOADING'] = old_load_setting |
|
|
|
|
|
|
|
|
|
assert len(called) == 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def suite(): |
|
|
|
|
suite = unittest.TestSuite() |
|
|
|
|
suite.addTest(unittest.makeSuite(TestTemplating)) |
|
|
|
|
return suite |
|
|
|
|