|
|
|
@ -11,250 +11,12 @@
|
|
|
|
|
|
|
|
|
|
import flask |
|
|
|
|
import unittest |
|
|
|
|
import warnings |
|
|
|
|
from flask.testsuite import FlaskTestCase, emits_module_deprecation_warning |
|
|
|
|
from flask.testsuite import FlaskTestCase |
|
|
|
|
from flask._compat import text_type |
|
|
|
|
from werkzeug.exceptions import NotFound |
|
|
|
|
from werkzeug.http import parse_cache_control_header |
|
|
|
|
from jinja2 import TemplateNotFound |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# import moduleapp here because it uses deprecated features and we don't |
|
|
|
|
# want to see the warnings |
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning) |
|
|
|
|
from moduleapp import app as moduleapp |
|
|
|
|
warnings.simplefilter('default', DeprecationWarning) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ModuleTestCase(FlaskTestCase): |
|
|
|
|
|
|
|
|
|
@emits_module_deprecation_warning |
|
|
|
|
def test_basic_module(self): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
admin = flask.Module(__name__, 'admin', url_prefix='/admin') |
|
|
|
|
@admin.route('/') |
|
|
|
|
def admin_index(): |
|
|
|
|
return 'admin index' |
|
|
|
|
@admin.route('/login') |
|
|
|
|
def admin_login(): |
|
|
|
|
return 'admin login' |
|
|
|
|
@admin.route('/logout') |
|
|
|
|
def admin_logout(): |
|
|
|
|
return 'admin logout' |
|
|
|
|
@app.route('/') |
|
|
|
|
def index(): |
|
|
|
|
return 'the index' |
|
|
|
|
app.register_module(admin) |
|
|
|
|
c = app.test_client() |
|
|
|
|
self.assert_equal(c.get('/').data, b'the index') |
|
|
|
|
self.assert_equal(c.get('/admin/').data, b'admin index') |
|
|
|
|
self.assert_equal(c.get('/admin/login').data, b'admin login') |
|
|
|
|
self.assert_equal(c.get('/admin/logout').data, b'admin logout') |
|
|
|
|
|
|
|
|
|
@emits_module_deprecation_warning |
|
|
|
|
def test_default_endpoint_name(self): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
mod = flask.Module(__name__, 'frontend') |
|
|
|
|
def index(): |
|
|
|
|
return 'Awesome' |
|
|
|
|
mod.add_url_rule('/', view_func=index) |
|
|
|
|
app.register_module(mod) |
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
|
self.assert_equal(rv.data, b'Awesome') |
|
|
|
|
with app.test_request_context(): |
|
|
|
|
self.assert_equal(flask.url_for('frontend.index'), '/') |
|
|
|
|
|
|
|
|
|
@emits_module_deprecation_warning |
|
|
|
|
def test_request_processing(self): |
|
|
|
|
catched = [] |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
admin = flask.Module(__name__, 'admin', url_prefix='/admin') |
|
|
|
|
@admin.before_request |
|
|
|
|
def before_admin_request(): |
|
|
|
|
catched.append('before-admin') |
|
|
|
|
@admin.after_request |
|
|
|
|
def after_admin_request(response): |
|
|
|
|
catched.append('after-admin') |
|
|
|
|
return response |
|
|
|
|
@admin.route('/') |
|
|
|
|
def admin_index(): |
|
|
|
|
return 'the admin' |
|
|
|
|
@app.before_request |
|
|
|
|
def before_request(): |
|
|
|
|
catched.append('before-app') |
|
|
|
|
@app.after_request |
|
|
|
|
def after_request(response): |
|
|
|
|
catched.append('after-app') |
|
|
|
|
return response |
|
|
|
|
@app.route('/') |
|
|
|
|
def index(): |
|
|
|
|
return 'the index' |
|
|
|
|
app.register_module(admin) |
|
|
|
|
c = app.test_client() |
|
|
|
|
|
|
|
|
|
self.assert_equal(c.get('/').data, b'the index') |
|
|
|
|
self.assert_equal(catched, ['before-app', 'after-app']) |
|
|
|
|
del catched[:] |
|
|
|
|
|
|
|
|
|
self.assert_equal(c.get('/admin/').data, b'the admin') |
|
|
|
|
self.assert_equal(catched, ['before-app', 'before-admin', |
|
|
|
|
'after-admin', 'after-app']) |
|
|
|
|
|
|
|
|
|
@emits_module_deprecation_warning |
|
|
|
|
def test_context_processors(self): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
admin = flask.Module(__name__, 'admin', url_prefix='/admin') |
|
|
|
|
@app.context_processor |
|
|
|
|
def inject_all_regular(): |
|
|
|
|
return {'a': 1} |
|
|
|
|
@admin.context_processor |
|
|
|
|
def inject_admin(): |
|
|
|
|
return {'b': 2} |
|
|
|
|
@admin.app_context_processor |
|
|
|
|
def inject_all_module(): |
|
|
|
|
return {'c': 3} |
|
|
|
|
@app.route('/') |
|
|
|
|
def index(): |
|
|
|
|
return flask.render_template_string('{{ a }}{{ b }}{{ c }}') |
|
|
|
|
@admin.route('/') |
|
|
|
|
def admin_index(): |
|
|
|
|
return flask.render_template_string('{{ a }}{{ b }}{{ c }}') |
|
|
|
|
app.register_module(admin) |
|
|
|
|
c = app.test_client() |
|
|
|
|
self.assert_equal(c.get('/').data, b'13') |
|
|
|
|
self.assert_equal(c.get('/admin/').data, b'123') |
|
|
|
|
|
|
|
|
|
@emits_module_deprecation_warning |
|
|
|
|
def test_late_binding(self): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
admin = flask.Module(__name__, 'admin') |
|
|
|
|
@admin.route('/') |
|
|
|
|
def index(): |
|
|
|
|
return '42' |
|
|
|
|
app.register_module(admin, url_prefix='/admin') |
|
|
|
|
self.assert_equal(app.test_client().get('/admin/').data, b'42') |
|
|
|
|
|
|
|
|
|
@emits_module_deprecation_warning |
|
|
|
|
def test_error_handling(self): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
app.config['LOGGER_HANDLER_POLICY'] = 'never' |
|
|
|
|
admin = flask.Module(__name__, 'admin') |
|
|
|
|
@admin.app_errorhandler(404) |
|
|
|
|
def not_found(e): |
|
|
|
|
return 'not found', 404 |
|
|
|
|
@admin.app_errorhandler(500) |
|
|
|
|
def internal_server_error(e): |
|
|
|
|
return 'internal server error', 500 |
|
|
|
|
@admin.route('/') |
|
|
|
|
def index(): |
|
|
|
|
flask.abort(404) |
|
|
|
|
@admin.route('/error') |
|
|
|
|
def error(): |
|
|
|
|
1 // 0 |
|
|
|
|
app.register_module(admin) |
|
|
|
|
c = app.test_client() |
|
|
|
|
rv = c.get('/') |
|
|
|
|
self.assert_equal(rv.status_code, 404) |
|
|
|
|
self.assert_equal(rv.data, b'not found') |
|
|
|
|
rv = c.get('/error') |
|
|
|
|
self.assert_equal(rv.status_code, 500) |
|
|
|
|
self.assert_equal(b'internal server error', rv.data) |
|
|
|
|
|
|
|
|
|
def test_templates_and_static(self): |
|
|
|
|
app = moduleapp |
|
|
|
|
app.testing = True |
|
|
|
|
c = app.test_client() |
|
|
|
|
|
|
|
|
|
rv = c.get('/') |
|
|
|
|
self.assert_equal(rv.data, b'Hello from the Frontend') |
|
|
|
|
rv = c.get('/admin/') |
|
|
|
|
self.assert_equal(rv.data, b'Hello from the Admin') |
|
|
|
|
rv = c.get('/admin/index2') |
|
|
|
|
self.assert_equal(rv.data, b'Hello from the Admin') |
|
|
|
|
rv = c.get('/admin/static/test.txt') |
|
|
|
|
self.assert_equal(rv.data.strip(), b'Admin File') |
|
|
|
|
rv.close() |
|
|
|
|
rv = c.get('/admin/static/css/test.css') |
|
|
|
|
self.assert_equal(rv.data.strip(), b'/* nested file */') |
|
|
|
|
rv.close() |
|
|
|
|
|
|
|
|
|
with app.test_request_context(): |
|
|
|
|
self.assert_equal(flask.url_for('admin.static', filename='test.txt'), |
|
|
|
|
'/admin/static/test.txt') |
|
|
|
|
|
|
|
|
|
with app.test_request_context(): |
|
|
|
|
try: |
|
|
|
|
flask.render_template('missing.html') |
|
|
|
|
except TemplateNotFound as e: |
|
|
|
|
self.assert_equal(e.name, 'missing.html') |
|
|
|
|
else: |
|
|
|
|
self.assert_true(0, 'expected exception') |
|
|
|
|
|
|
|
|
|
with flask.Flask(__name__).test_request_context(): |
|
|
|
|
self.assert_equal(flask.render_template('nested/nested.txt'), 'I\'m nested') |
|
|
|
|
|
|
|
|
|
def test_safe_access(self): |
|
|
|
|
app = moduleapp |
|
|
|
|
|
|
|
|
|
with app.test_request_context(): |
|
|
|
|
f = app.view_functions['admin.static'] |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
f('/etc/passwd') |
|
|
|
|
except NotFound: |
|
|
|
|
pass |
|
|
|
|
else: |
|
|
|
|
self.assert_true(0, 'expected exception') |
|
|
|
|
try: |
|
|
|
|
f('../__init__.py') |
|
|
|
|
except NotFound: |
|
|
|
|
pass |
|
|
|
|
else: |
|
|
|
|
self.assert_true(0, 'expected exception') |
|
|
|
|
|
|
|
|
|
# testcase for a security issue that may exist on windows systems |
|
|
|
|
import os |
|
|
|
|
import ntpath |
|
|
|
|
old_path = os.path |
|
|
|
|
os.path = ntpath |
|
|
|
|
try: |
|
|
|
|
try: |
|
|
|
|
f('..\\__init__.py') |
|
|
|
|
except NotFound: |
|
|
|
|
pass |
|
|
|
|
else: |
|
|
|
|
self.assert_true(0, 'expected exception') |
|
|
|
|
finally: |
|
|
|
|
os.path = old_path |
|
|
|
|
|
|
|
|
|
@emits_module_deprecation_warning |
|
|
|
|
def test_endpoint_decorator(self): |
|
|
|
|
from werkzeug.routing import Submount, Rule |
|
|
|
|
from flask import Module |
|
|
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
app.testing = True |
|
|
|
|
app.url_map.add(Submount('/foo', [ |
|
|
|
|
Rule('/bar', endpoint='bar'), |
|
|
|
|
Rule('/', endpoint='index') |
|
|
|
|
])) |
|
|
|
|
module = Module(__name__, __name__) |
|
|
|
|
|
|
|
|
|
@module.endpoint('bar') |
|
|
|
|
def bar(): |
|
|
|
|
return 'bar' |
|
|
|
|
|
|
|
|
|
@module.endpoint('index') |
|
|
|
|
def index(): |
|
|
|
|
return 'index' |
|
|
|
|
|
|
|
|
|
app.register_module(module) |
|
|
|
|
|
|
|
|
|
c = app.test_client() |
|
|
|
|
self.assert_equal(c.get('/foo/').data, b'index') |
|
|
|
|
self.assert_equal(c.get('/foo/bar').data, b'bar') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BlueprintTestCase(FlaskTestCase): |
|
|
|
|
|
|
|
|
|
def test_blueprint_specific_error_handling(self): |
|
|
|
@ -820,5 +582,4 @@ class BlueprintTestCase(FlaskTestCase):
|
|
|
|
|
def suite(): |
|
|
|
|
suite = unittest.TestSuite() |
|
|
|
|
suite.addTest(unittest.makeSuite(BlueprintTestCase)) |
|
|
|
|
suite.addTest(unittest.makeSuite(ModuleTestCase)) |
|
|
|
|
return suite |
|
|
|
|