diff --git a/.travis-devel-requirements.txt b/.travis-devel-requirements.txt deleted file mode 100644 index 18ee00ec..00000000 --- a/.travis-devel-requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -git+git://github.com/mitsuhiko/werkzeug.git#egg=Werkzeug -git+git://github.com/mitsuhiko/jinja2.git#egg=Jinja2 -git+git://github.com/mitsuhiko/itsdangerous.git#egg=itsdangerous - -# extra dependencies -git+git://github.com/jek/blinker.git#egg=blinker diff --git a/.travis-lowest-requirements.txt b/.travis-lowest-requirements.txt deleted file mode 100644 index 5066d698..00000000 --- a/.travis-lowest-requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -Werkzeug==0.7 -Jinja2==2.4 -itsdangerous==0.21 - -# extra dependencies -blinker==1.0 diff --git a/.travis-release-requirements.txt b/.travis-release-requirements.txt deleted file mode 100644 index f2f097cb..00000000 --- a/.travis-release-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -# extra dependencies -blinker diff --git a/.travis.yml b/.travis.yml index 92f070c2..9a1a8a8c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ python: - "2.7" - "pypy" - "3.3" + - "3.4" env: - REQUIREMENTS=lowest @@ -16,12 +17,16 @@ matrix: # Python 3 support currently does not work with lowest requirements - python: "3.3" env: REQUIREMENTS=lowest + - python: "3.4" + env: REQUIREMENTS=lowest + install: - - pip install -r .travis-$REQUIREMENTS-requirements.txt - - pip install --editable . + - pip install hg+https://bitbucket.org/hpk42/tox -script: make test +script: + - tox -e \ + $(echo py$TRAVIS_PYTHON_VERSION | tr -d . | sed -e 's/pypypy/pypy/')-$REQUIREMENTS branches: except: diff --git a/MANIFEST.in b/MANIFEST.in index f82ed054..3fef8b5b 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,4 @@ -include Makefile CHANGES LICENSE AUTHORS run-tests.py +include Makefile CHANGES LICENSE AUTHORS recursive-include artwork * recursive-include tests * recursive-include examples * @@ -9,8 +9,5 @@ recursive-exclude tests *.pyc recursive-exclude tests *.pyo recursive-exclude examples *.pyc recursive-exclude examples *.pyo -recursive-include flask/testsuite/static * -recursive-include flask/testsuite/templates * -recursive-include flask/testsuite/test_apps * prune docs/_build prune docs/_themes/.git diff --git a/Makefile b/Makefile index 98e74135..e1835e8c 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ all: clean-pyc test test: - python run-tests.py + py.test tox-test: tox diff --git a/examples/blueprintexample/blueprintexample_test.py b/examples/blueprintexample/blueprintexample_test.py deleted file mode 100644 index b8f93414..00000000 --- a/examples/blueprintexample/blueprintexample_test.py +++ /dev/null @@ -1,36 +0,0 @@ -# -*- coding: utf-8 -*- -""" - Blueprint Example Tests - ~~~~~~~~~~~~~~ - - Tests the Blueprint example app -""" -import blueprintexample -import unittest - - -class BlueprintExampleTestCase(unittest.TestCase): - - def setUp(self): - self.app = blueprintexample.app.test_client() - - def test_urls(self): - r = self.app.get('/') - self.assertEquals(r.status_code, 200) - - r = self.app.get('/hello') - self.assertEquals(r.status_code, 200) - - r = self.app.get('/world') - self.assertEquals(r.status_code, 200) - - #second blueprint instance - r = self.app.get('/pages/hello') - self.assertEquals(r.status_code, 200) - - r = self.app.get('/pages/world') - self.assertEquals(r.status_code, 200) - - -if __name__ == '__main__': - unittest.main() diff --git a/examples/blueprintexample/test_blueprintexample.py b/examples/blueprintexample/test_blueprintexample.py new file mode 100644 index 00000000..2f3dd93f --- /dev/null +++ b/examples/blueprintexample/test_blueprintexample.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +""" + Blueprint Example Tests + ~~~~~~~~~~~~~~ + + Tests the Blueprint example app +""" +import pytest + +import blueprintexample + + +@pytest.fixture +def client(): + return blueprintexample.app.test_client() + + +def test_urls(client): + r = client.get('/') + assert r.status_code == 200 + + r = client.get('/hello') + assert r.status_code == 200 + + r = client.get('/world') + assert r.status_code == 200 + + # second blueprint instance + r = client.get('/pages/hello') + assert r.status_code == 200 + + r = client.get('/pages/world') + assert r.status_code == 200 diff --git a/examples/flaskr/flaskr_tests.py b/examples/flaskr/flaskr_tests.py deleted file mode 100644 index b90a7be7..00000000 --- a/examples/flaskr/flaskr_tests.py +++ /dev/null @@ -1,76 +0,0 @@ -# -*- coding: utf-8 -*- -""" - Flaskr Tests - ~~~~~~~~~~~~ - - Tests the Flaskr application. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" -import os -import flaskr -import unittest -import tempfile - - -class FlaskrTestCase(unittest.TestCase): - - def setUp(self): - """Before each test, set up a blank database""" - self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp() - flaskr.app.config['TESTING'] = True - self.app = flaskr.app.test_client() - with flaskr.app.app_context(): - flaskr.init_db() - - def tearDown(self): - """Get rid of the database again after each test.""" - os.close(self.db_fd) - os.unlink(flaskr.app.config['DATABASE']) - - def login(self, username, password): - return self.app.post('/login', data=dict( - username=username, - password=password - ), follow_redirects=True) - - def logout(self): - return self.app.get('/logout', follow_redirects=True) - - # testing functions - - def test_empty_db(self): - """Start with a blank database.""" - rv = self.app.get('/') - assert b'No entries here so far' in rv.data - - def test_login_logout(self): - """Make sure login and logout works""" - rv = self.login(flaskr.app.config['USERNAME'], - flaskr.app.config['PASSWORD']) - assert b'You were logged in' in rv.data - rv = self.logout() - assert b'You were logged out' in rv.data - rv = self.login(flaskr.app.config['USERNAME'] + 'x', - flaskr.app.config['PASSWORD']) - assert b'Invalid username' in rv.data - rv = self.login(flaskr.app.config['USERNAME'], - flaskr.app.config['PASSWORD'] + 'x') - assert b'Invalid password' in rv.data - - def test_messages(self): - """Test that messages work""" - self.login(flaskr.app.config['USERNAME'], - flaskr.app.config['PASSWORD']) - rv = self.app.post('/add', data=dict( - title='', - text='HTML allowed here' - ), follow_redirects=True) - assert b'No entries here so far' not in rv.data - assert b'<Hello>' in rv.data - assert b'HTML allowed here' in rv.data - - -if __name__ == '__main__': - unittest.main() diff --git a/examples/flaskr/test_flaskr.py b/examples/flaskr/test_flaskr.py new file mode 100644 index 00000000..07116f81 --- /dev/null +++ b/examples/flaskr/test_flaskr.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +""" + Flaskr Tests + ~~~~~~~~~~~~ + + Tests the Flaskr application. + + :copyright: (c) 2014 by Armin Ronacher. + :license: BSD, see LICENSE for more details. +""" + +import pytest + +import os +import flaskr +import tempfile + + +@pytest.fixture +def client(request): + db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp() + flaskr.app.config['TESTING'] = True + client = flaskr.app.test_client() + with flaskr.app.app_context(): + flaskr.init_db() + + def teardown(): + os.close(db_fd) + os.unlink(flaskr.app.config['DATABASE']) + request.addfinalizer(teardown) + + return client + + +def login(client, username, password): + return client.post('/login', data=dict( + username=username, + password=password + ), follow_redirects=True) + + +def logout(client): + return client.get('/logout', follow_redirects=True) + + +def test_empty_db(client): + """Start with a blank database.""" + rv = client.get('/') + assert b'No entries here so far' in rv.data + + +def test_login_logout(client): + """Make sure login and logout works""" + rv = login(client, flaskr.app.config['USERNAME'], + flaskr.app.config['PASSWORD']) + assert b'You were logged in' in rv.data + rv = logout(client) + assert b'You were logged out' in rv.data + rv = login(client, flaskr.app.config['USERNAME'] + 'x', + flaskr.app.config['PASSWORD']) + assert b'Invalid username' in rv.data + rv = login(client, flaskr.app.config['USERNAME'], + flaskr.app.config['PASSWORD'] + 'x') + assert b'Invalid password' in rv.data + + +def test_messages(client): + """Test that messages work""" + login(client, flaskr.app.config['USERNAME'], + flaskr.app.config['PASSWORD']) + rv = client.post('/add', data=dict( + title='', + text='HTML allowed here' + ), follow_redirects=True) + assert b'No entries here so far' not in rv.data + assert b'<Hello>' in rv.data + assert b'HTML allowed here' in rv.data diff --git a/examples/minitwit/minitwit_tests.py b/examples/minitwit/minitwit_tests.py deleted file mode 100644 index 0a1a3f67..00000000 --- a/examples/minitwit/minitwit_tests.py +++ /dev/null @@ -1,150 +0,0 @@ -# -*- coding: utf-8 -*- -""" - MiniTwit Tests - ~~~~~~~~~~~~~~ - - Tests the MiniTwit application. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" -import os -import minitwit -import unittest -import tempfile - - -class MiniTwitTestCase(unittest.TestCase): - - def setUp(self): - """Before each test, set up a blank database""" - self.db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp() - self.app = minitwit.app.test_client() - with minitwit.app.app_context(): - minitwit.init_db() - - def tearDown(self): - """Get rid of the database again after each test.""" - os.close(self.db_fd) - os.unlink(minitwit.app.config['DATABASE']) - - # helper functions - - def register(self, username, password, password2=None, email=None): - """Helper function to register a user""" - if password2 is None: - password2 = password - if email is None: - email = username + '@example.com' - return self.app.post('/register', data={ - 'username': username, - 'password': password, - 'password2': password2, - 'email': email, - }, follow_redirects=True) - - def login(self, username, password): - """Helper function to login""" - return self.app.post('/login', data={ - 'username': username, - 'password': password - }, follow_redirects=True) - - def register_and_login(self, username, password): - """Registers and logs in in one go""" - self.register(username, password) - return self.login(username, password) - - def logout(self): - """Helper function to logout""" - return self.app.get('/logout', follow_redirects=True) - - def add_message(self, text): - """Records a message""" - rv = self.app.post('/add_message', data={'text': text}, - follow_redirects=True) - if text: - assert b'Your message was recorded' in rv.data - return rv - - # testing functions - - def test_register(self): - """Make sure registering works""" - rv = self.register('user1', 'default') - assert b'You were successfully registered ' \ - b'and can login now' in rv.data - rv = self.register('user1', 'default') - assert b'The username is already taken' in rv.data - rv = self.register('', 'default') - assert b'You have to enter a username' in rv.data - rv = self.register('meh', '') - assert b'You have to enter a password' in rv.data - rv = self.register('meh', 'x', 'y') - assert b'The two passwords do not match' in rv.data - rv = self.register('meh', 'foo', email='broken') - assert b'You have to enter a valid email address' in rv.data - - def test_login_logout(self): - """Make sure logging in and logging out works""" - rv = self.register_and_login('user1', 'default') - assert b'You were logged in' in rv.data - rv = self.logout() - assert b'You were logged out' in rv.data - rv = self.login('user1', 'wrongpassword') - assert b'Invalid password' in rv.data - rv = self.login('user2', 'wrongpassword') - assert b'Invalid username' in rv.data - - def test_message_recording(self): - """Check if adding messages works""" - self.register_and_login('foo', 'default') - self.add_message('test message 1') - self.add_message('') - rv = self.app.get('/') - assert b'test message 1' in rv.data - assert b'<test message 2>' in rv.data - - def test_timelines(self): - """Make sure that timelines work""" - self.register_and_login('foo', 'default') - self.add_message('the message by foo') - self.logout() - self.register_and_login('bar', 'default') - self.add_message('the message by bar') - rv = self.app.get('/public') - assert b'the message by foo' in rv.data - assert b'the message by bar' in rv.data - - # bar's timeline should just show bar's message - rv = self.app.get('/') - assert b'the message by foo' not in rv.data - assert b'the message by bar' in rv.data - - # now let's follow foo - rv = self.app.get('/foo/follow', follow_redirects=True) - assert b'You are now following "foo"' in rv.data - - # we should now see foo's message - rv = self.app.get('/') - assert b'the message by foo' in rv.data - assert b'the message by bar' in rv.data - - # but on the user's page we only want the user's message - rv = self.app.get('/bar') - assert b'the message by foo' not in rv.data - assert b'the message by bar' in rv.data - rv = self.app.get('/foo') - assert b'the message by foo' in rv.data - assert b'the message by bar' not in rv.data - - # now unfollow and check if that worked - rv = self.app.get('/foo/unfollow', follow_redirects=True) - assert b'You are no longer following "foo"' in rv.data - rv = self.app.get('/') - assert b'the message by foo' not in rv.data - assert b'the message by bar' in rv.data - - -if __name__ == '__main__': - unittest.main() diff --git a/examples/minitwit/test_minitwit.py b/examples/minitwit/test_minitwit.py new file mode 100644 index 00000000..c9345e9d --- /dev/null +++ b/examples/minitwit/test_minitwit.py @@ -0,0 +1,151 @@ +# -*- coding: utf-8 -*- +""" + MiniTwit Tests + ~~~~~~~~~~~~~~ + + Tests the MiniTwit application. + + :copyright: (c) 2014 by Armin Ronacher. + :license: BSD, see LICENSE for more details. +""" +import os +import minitwit +import tempfile +import pytest + + +@pytest.fixture +def client(request): + db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp() + client = minitwit.app.test_client() + with minitwit.app.app_context(): + minitwit.init_db() + + def teardown(): + """Get rid of the database again after each test.""" + os.close(db_fd) + os.unlink(minitwit.app.config['DATABASE']) + request.addfinalizer(teardown) + return client + + +def register(client, username, password, password2=None, email=None): + """Helper function to register a user""" + if password2 is None: + password2 = password + if email is None: + email = username + '@example.com' + return client.post('/register', data={ + 'username': username, + 'password': password, + 'password2': password2, + 'email': email, + }, follow_redirects=True) + + +def login(client, username, password): + """Helper function to login""" + return client.post('/login', data={ + 'username': username, + 'password': password + }, follow_redirects=True) + + +def register_and_login(client, username, password): + """Registers and logs in in one go""" + register(client, username, password) + return login(client, username, password) + + +def logout(client): + """Helper function to logout""" + return client.get('/logout', follow_redirects=True) + + +def add_message(client, text): + """Records a message""" + rv = client.post('/add_message', data={'text': text}, + follow_redirects=True) + if text: + assert b'Your message was recorded' in rv.data + return rv + + +def test_register(client): + """Make sure registering works""" + rv = register(client, 'user1', 'default') + assert b'You were successfully registered ' \ + b'and can login now' in rv.data + rv = register(client, 'user1', 'default') + assert b'The username is already taken' in rv.data + rv = register(client, '', 'default') + assert b'You have to enter a username' in rv.data + rv = register(client, 'meh', '') + assert b'You have to enter a password' in rv.data + rv = register(client, 'meh', 'x', 'y') + assert b'The two passwords do not match' in rv.data + rv = register(client, 'meh', 'foo', email='broken') + assert b'You have to enter a valid email address' in rv.data + + +def test_login_logout(client): + """Make sure logging in and logging out works""" + rv = register_and_login(client, 'user1', 'default') + assert b'You were logged in' in rv.data + rv = logout(client) + assert b'You were logged out' in rv.data + rv = login(client, 'user1', 'wrongpassword') + assert b'Invalid password' in rv.data + rv = login(client, 'user2', 'wrongpassword') + assert b'Invalid username' in rv.data + + +def test_message_recording(client): + """Check if adding messages works""" + register_and_login(client, 'foo', 'default') + add_message(client, 'test message 1') + add_message(client, '') + rv = client.get('/') + assert b'test message 1' in rv.data + assert b'<test message 2>' in rv.data + + +def test_timelines(client): + """Make sure that timelines work""" + register_and_login(client, 'foo', 'default') + add_message(client, 'the message by foo') + logout(client) + register_and_login(client, 'bar', 'default') + add_message(client, 'the message by bar') + rv = client.get('/public') + assert b'the message by foo' in rv.data + assert b'the message by bar' in rv.data + + # bar's timeline should just show bar's message + rv = client.get('/') + assert b'the message by foo' not in rv.data + assert b'the message by bar' in rv.data + + # now let's follow foo + rv = client.get('/foo/follow', follow_redirects=True) + assert b'You are now following "foo"' in rv.data + + # we should now see foo's message + rv = client.get('/') + assert b'the message by foo' in rv.data + assert b'the message by bar' in rv.data + + # but on the user's page we only want the user's message + rv = client.get('/bar') + assert b'the message by foo' not in rv.data + assert b'the message by bar' in rv.data + rv = client.get('/foo') + assert b'the message by foo' in rv.data + assert b'the message by bar' not in rv.data + + # now unfollow and check if that worked + rv = client.get('/foo/unfollow', follow_redirects=True) + assert b'You are no longer following "foo"' in rv.data + rv = client.get('/') + assert b'the message by foo' not in rv.data + assert b'the message by bar' in rv.data diff --git a/flask/testsuite/__init__.py b/flask/testsuite/__init__.py deleted file mode 100644 index 0ed17c30..00000000 --- a/flask/testsuite/__init__.py +++ /dev/null @@ -1,253 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite - ~~~~~~~~~~~~~~~ - - Tests Flask itself. The majority of Flask is already tested - as part of Werkzeug. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -from __future__ import print_function - -import os -import sys -import flask -import warnings -import unittest -from functools import update_wrapper -from contextlib import contextmanager -from werkzeug.utils import import_string, find_modules -from flask._compat import reraise, StringIO - - -def add_to_path(path): - """Adds an entry to sys.path if it's not already there. This does - not append it but moves it to the front so that we can be sure it - is loaded. - """ - if not os.path.isdir(path): - raise RuntimeError('Tried to add nonexisting path') - - def _samefile(x, y): - if x == y: - return True - try: - return os.path.samefile(x, y) - except (IOError, OSError, AttributeError): - # Windows has no samefile - return False - sys.path[:] = [x for x in sys.path if not _samefile(path, x)] - sys.path.insert(0, path) - - -def iter_suites(): - """Yields all testsuites.""" - for module in find_modules(__name__): - mod = import_string(module) - if hasattr(mod, 'suite'): - yield mod.suite() - - -def find_all_tests(suite): - """Yields all the tests and their names from a given suite.""" - suites = [suite] - while suites: - s = suites.pop() - try: - suites.extend(s) - except TypeError: - yield s, '%s.%s.%s' % ( - s.__class__.__module__, - s.__class__.__name__, - s._testMethodName - ) - - -@contextmanager -def catch_warnings(): - """Catch warnings in a with block in a list""" - # make sure deprecation warnings are active in tests - warnings.simplefilter('default', category=DeprecationWarning) - - filters = warnings.filters - warnings.filters = filters[:] - old_showwarning = warnings.showwarning - log = [] - def showwarning(message, category, filename, lineno, file=None, line=None): - log.append(locals()) - try: - warnings.showwarning = showwarning - yield log - finally: - warnings.filters = filters - warnings.showwarning = old_showwarning - - -@contextmanager -def catch_stderr(): - """Catch stderr in a StringIO""" - old_stderr = sys.stderr - sys.stderr = rv = StringIO() - try: - yield rv - finally: - sys.stderr = old_stderr - - -def emits_module_deprecation_warning(f): - def new_f(self, *args, **kwargs): - with catch_warnings() as log: - f(self, *args, **kwargs) - self.assert_true(log, 'expected deprecation warning') - for entry in log: - self.assert_in('Modules are deprecated', str(entry['message'])) - return update_wrapper(new_f, f) - - -class FlaskTestCase(unittest.TestCase): - """Baseclass for all the tests that Flask uses. Use these methods - for testing instead of the camelcased ones in the baseclass for - consistency. - """ - - def ensure_clean_request_context(self): - # make sure we're not leaking a request context since we are - # testing flask internally in debug mode in a few cases - leaks = [] - while flask._request_ctx_stack.top is not None: - leaks.append(flask._request_ctx_stack.pop()) - self.assert_equal(leaks, []) - - def setup(self): - pass - - def teardown(self): - pass - - def setUp(self): - self.setup() - - def tearDown(self): - unittest.TestCase.tearDown(self) - self.ensure_clean_request_context() - self.teardown() - - def assert_equal(self, x, y): - return self.assertEqual(x, y) - - def assert_raises(self, exc_type, callable=None, *args, **kwargs): - catcher = _ExceptionCatcher(self, exc_type) - if callable is None: - return catcher - with catcher: - callable(*args, **kwargs) - - def assert_true(self, x, msg=None): - self.assertTrue(x, msg) - assert_ = assert_true - - def assert_false(self, x, msg=None): - self.assertFalse(x, msg) - - def assert_in(self, x, y): - self.assertIn(x, y) - - def assert_not_in(self, x, y): - self.assertNotIn(x, y) - - def assert_isinstance(self, obj, cls): - self.assertIsInstance(obj, cls) - - if sys.version_info[:2] == (2, 6): - def assertIn(self, x, y): - assert x in y, "%r unexpectedly not in %r" % (x, y) - - def assertNotIn(self, x, y): - assert x not in y, "%r unexpectedly in %r" % (x, y) - - def assertIsInstance(self, x, y): - assert isinstance(x, y), "not isinstance(%r, %r)" % (x, y) - - -class _ExceptionCatcher(object): - - def __init__(self, test_case, exc_type): - self.test_case = test_case - self.exc_type = exc_type - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc_value, tb): - exception_name = self.exc_type.__name__ - if exc_type is None: - self.test_case.fail('Expected exception of type %r' % - exception_name) - elif not issubclass(exc_type, self.exc_type): - reraise(exc_type, exc_value, tb) - return True - - -class BetterLoader(unittest.TestLoader): - """A nicer loader that solves two problems. First of all we are setting - up tests from different sources and we're doing this programmatically - which breaks the default loading logic so this is required anyways. - Secondly this loader has a nicer interpolation for test names than the - default one so you can just do ``run-tests.py ViewTestCase`` and it - will work. - """ - - def getRootSuite(self): - return suite() - - def loadTestsFromName(self, name, module=None): - root = self.getRootSuite() - if name == 'suite': - return root - - all_tests = [] - for testcase, testname in find_all_tests(root): - if testname == name or \ - testname.endswith('.' + name) or \ - ('.' + name + '.') in testname or \ - testname.startswith(name + '.'): - all_tests.append(testcase) - - if not all_tests: - raise LookupError('could not find test case for "%s"' % name) - - if len(all_tests) == 1: - return all_tests[0] - rv = unittest.TestSuite() - for test in all_tests: - rv.addTest(test) - return rv - - -def setup_path(): - add_to_path(os.path.abspath(os.path.join( - os.path.dirname(__file__), 'test_apps'))) - - -def suite(): - """A testsuite that has all the Flask tests. You can use this - function to integrate the Flask tests into your own testsuite - in case you want to test that monkeypatches to Flask do not - break it. - """ - setup_path() - suite = unittest.TestSuite() - for other_suite in iter_suites(): - suite.addTest(other_suite) - return suite - - -def main(): - """Runs the testsuite as command line application.""" - try: - unittest.main(testLoader=BetterLoader(), defaultTest='suite') - except Exception as e: - print('Error: %s' % e) diff --git a/flask/testsuite/appctx.py b/flask/testsuite/appctx.py deleted file mode 100644 index 55f7dcba..00000000 --- a/flask/testsuite/appctx.py +++ /dev/null @@ -1,118 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.appctx - ~~~~~~~~~~~~~~~~~~~~~~ - - Tests the application context. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -import flask -import unittest -from flask.testsuite import FlaskTestCase - - -class AppContextTestCase(FlaskTestCase): - - def test_basic_url_generation(self): - app = flask.Flask(__name__) - app.config['SERVER_NAME'] = 'localhost' - app.config['PREFERRED_URL_SCHEME'] = 'https' - - @app.route('/') - def index(): - pass - - with app.app_context(): - rv = flask.url_for('index') - self.assert_equal(rv, 'https://localhost/') - - def test_url_generation_requires_server_name(self): - app = flask.Flask(__name__) - with app.app_context(): - with self.assert_raises(RuntimeError): - flask.url_for('index') - - def test_url_generation_without_context_fails(self): - with self.assert_raises(RuntimeError): - flask.url_for('index') - - def test_request_context_means_app_context(self): - app = flask.Flask(__name__) - with app.test_request_context(): - self.assert_equal(flask.current_app._get_current_object(), app) - self.assert_equal(flask._app_ctx_stack.top, None) - - def test_app_context_provides_current_app(self): - app = flask.Flask(__name__) - with app.app_context(): - self.assert_equal(flask.current_app._get_current_object(), app) - self.assert_equal(flask._app_ctx_stack.top, None) - - def test_app_tearing_down(self): - cleanup_stuff = [] - app = flask.Flask(__name__) - @app.teardown_appcontext - def cleanup(exception): - cleanup_stuff.append(exception) - - with app.app_context(): - pass - - self.assert_equal(cleanup_stuff, [None]) - - def test_app_tearing_down_with_previous_exception(self): - cleanup_stuff = [] - app = flask.Flask(__name__) - @app.teardown_appcontext - def cleanup(exception): - cleanup_stuff.append(exception) - - try: - raise Exception('dummy') - except Exception: - pass - - with app.app_context(): - pass - - self.assert_equal(cleanup_stuff, [None]) - - def test_custom_app_ctx_globals_class(self): - class CustomRequestGlobals(object): - def __init__(self): - self.spam = 'eggs' - app = flask.Flask(__name__) - app.app_ctx_globals_class = CustomRequestGlobals - with app.app_context(): - self.assert_equal( - flask.render_template_string('{{ g.spam }}'), 'eggs') - - def test_context_refcounts(self): - called = [] - app = flask.Flask(__name__) - @app.teardown_request - def teardown_req(error=None): - called.append('request') - @app.teardown_appcontext - def teardown_app(error=None): - called.append('app') - @app.route('/') - def index(): - with flask._app_ctx_stack.top: - with flask._request_ctx_stack.top: - pass - self.assert_true(flask._request_ctx_stack.top.request.environ - ['werkzeug.request'] is not None) - return u'' - c = app.test_client() - c.get('/') - self.assertEqual(called, ['request', 'app']) - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(AppContextTestCase)) - return suite diff --git a/flask/testsuite/basic.py b/flask/testsuite/basic.py deleted file mode 100644 index 6c2525f9..00000000 --- a/flask/testsuite/basic.py +++ /dev/null @@ -1,1364 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.basic - ~~~~~~~~~~~~~~~~~~~~~ - - The basic functionality. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -import re -import uuid -import time -import flask -import pickle -import unittest -from datetime import datetime -from threading import Thread -from flask.testsuite import FlaskTestCase, emits_module_deprecation_warning -from flask._compat import text_type -from werkzeug.exceptions import BadRequest, NotFound, Forbidden -from werkzeug.http import parse_date -from werkzeug.routing import BuildError - - -class BasicFunctionalityTestCase(FlaskTestCase): - - def test_options_work(self): - app = flask.Flask(__name__) - @app.route('/', methods=['GET', 'POST']) - def index(): - return 'Hello World' - rv = app.test_client().open('/', method='OPTIONS') - self.assert_equal(sorted(rv.allow), ['GET', 'HEAD', 'OPTIONS', 'POST']) - self.assert_equal(rv.data, b'') - - def test_options_on_multiple_rules(self): - app = flask.Flask(__name__) - @app.route('/', methods=['GET', 'POST']) - def index(): - return 'Hello World' - @app.route('/', methods=['PUT']) - def index_put(): - return 'Aha!' - rv = app.test_client().open('/', method='OPTIONS') - self.assert_equal(sorted(rv.allow), ['GET', 'HEAD', 'OPTIONS', 'POST', 'PUT']) - - def test_options_handling_disabled(self): - app = flask.Flask(__name__) - def index(): - return 'Hello World!' - index.provide_automatic_options = False - app.route('/')(index) - rv = app.test_client().open('/', method='OPTIONS') - self.assert_equal(rv.status_code, 405) - - app = flask.Flask(__name__) - def index2(): - return 'Hello World!' - index2.provide_automatic_options = True - app.route('/', methods=['OPTIONS'])(index2) - rv = app.test_client().open('/', method='OPTIONS') - self.assert_equal(sorted(rv.allow), ['OPTIONS']) - - def test_request_dispatching(self): - app = flask.Flask(__name__) - @app.route('/') - def index(): - return flask.request.method - @app.route('/more', methods=['GET', 'POST']) - def more(): - return flask.request.method - - c = app.test_client() - self.assert_equal(c.get('/').data, b'GET') - rv = c.post('/') - self.assert_equal(rv.status_code, 405) - self.assert_equal(sorted(rv.allow), ['GET', 'HEAD', 'OPTIONS']) - rv = c.head('/') - self.assert_equal(rv.status_code, 200) - self.assert_false(rv.data) # head truncates - self.assert_equal(c.post('/more').data, b'POST') - self.assert_equal(c.get('/more').data, b'GET') - rv = c.delete('/more') - self.assert_equal(rv.status_code, 405) - self.assert_equal(sorted(rv.allow), ['GET', 'HEAD', 'OPTIONS', 'POST']) - - def test_disallow_string_for_allowed_methods(self): - app = flask.Flask(__name__) - with self.assert_raises(TypeError): - @app.route('/', methods='GET POST') - def index(): - return "Hey" - - def test_url_mapping(self): - app = flask.Flask(__name__) - def index(): - return flask.request.method - def more(): - return flask.request.method - - app.add_url_rule('/', 'index', index) - app.add_url_rule('/more', 'more', more, methods=['GET', 'POST']) - - c = app.test_client() - self.assert_equal(c.get('/').data, b'GET') - rv = c.post('/') - self.assert_equal(rv.status_code, 405) - self.assert_equal(sorted(rv.allow), ['GET', 'HEAD', 'OPTIONS']) - rv = c.head('/') - self.assert_equal(rv.status_code, 200) - self.assert_false(rv.data) # head truncates - self.assert_equal(c.post('/more').data, b'POST') - self.assert_equal(c.get('/more').data, b'GET') - rv = c.delete('/more') - self.assert_equal(rv.status_code, 405) - self.assert_equal(sorted(rv.allow), ['GET', 'HEAD', 'OPTIONS', 'POST']) - - def test_werkzeug_routing(self): - from werkzeug.routing import Submount, Rule - app = flask.Flask(__name__) - app.url_map.add(Submount('/foo', [ - Rule('/bar', endpoint='bar'), - Rule('/', endpoint='index') - ])) - def bar(): - return 'bar' - def index(): - return 'index' - app.view_functions['bar'] = bar - app.view_functions['index'] = index - - c = app.test_client() - self.assert_equal(c.get('/foo/').data, b'index') - self.assert_equal(c.get('/foo/bar').data, b'bar') - - def test_endpoint_decorator(self): - from werkzeug.routing import Submount, Rule - app = flask.Flask(__name__) - app.url_map.add(Submount('/foo', [ - Rule('/bar', endpoint='bar'), - Rule('/', endpoint='index') - ])) - - @app.endpoint('bar') - def bar(): - return 'bar' - - @app.endpoint('index') - def index(): - return 'index' - - c = app.test_client() - self.assert_equal(c.get('/foo/').data, b'index') - self.assert_equal(c.get('/foo/bar').data, b'bar') - - def test_session(self): - app = flask.Flask(__name__) - app.secret_key = 'testkey' - @app.route('/set', methods=['POST']) - def set(): - flask.session['value'] = flask.request.form['value'] - return 'value set' - @app.route('/get') - def get(): - return flask.session['value'] - - c = app.test_client() - self.assert_equal(c.post('/set', data={'value': '42'}).data, b'value set') - self.assert_equal(c.get('/get').data, b'42') - - def test_session_using_server_name(self): - app = flask.Flask(__name__) - app.config.update( - SECRET_KEY='foo', - SERVER_NAME='example.com' - ) - @app.route('/') - def index(): - flask.session['testing'] = 42 - return 'Hello World' - rv = app.test_client().get('/', 'http://example.com/') - self.assert_in('domain=.example.com', rv.headers['set-cookie'].lower()) - self.assert_in('httponly', rv.headers['set-cookie'].lower()) - - def test_session_using_server_name_and_port(self): - app = flask.Flask(__name__) - app.config.update( - SECRET_KEY='foo', - SERVER_NAME='example.com:8080' - ) - @app.route('/') - def index(): - flask.session['testing'] = 42 - return 'Hello World' - rv = app.test_client().get('/', 'http://example.com:8080/') - self.assert_in('domain=.example.com', rv.headers['set-cookie'].lower()) - self.assert_in('httponly', rv.headers['set-cookie'].lower()) - - def test_session_using_server_name_port_and_path(self): - app = flask.Flask(__name__) - app.config.update( - SECRET_KEY='foo', - SERVER_NAME='example.com:8080', - APPLICATION_ROOT='/foo' - ) - @app.route('/') - def index(): - flask.session['testing'] = 42 - return 'Hello World' - rv = app.test_client().get('/', 'http://example.com:8080/foo') - self.assert_in('domain=example.com', rv.headers['set-cookie'].lower()) - self.assert_in('path=/foo', rv.headers['set-cookie'].lower()) - self.assert_in('httponly', rv.headers['set-cookie'].lower()) - - def test_session_using_application_root(self): - class PrefixPathMiddleware(object): - def __init__(self, app, prefix): - self.app = app - self.prefix = prefix - def __call__(self, environ, start_response): - environ['SCRIPT_NAME'] = self.prefix - return self.app(environ, start_response) - - app = flask.Flask(__name__) - app.wsgi_app = PrefixPathMiddleware(app.wsgi_app, '/bar') - app.config.update( - SECRET_KEY='foo', - APPLICATION_ROOT='/bar' - ) - @app.route('/') - def index(): - flask.session['testing'] = 42 - return 'Hello World' - rv = app.test_client().get('/', 'http://example.com:8080/') - self.assert_in('path=/bar', rv.headers['set-cookie'].lower()) - - def test_session_using_session_settings(self): - app = flask.Flask(__name__) - app.config.update( - SECRET_KEY='foo', - SERVER_NAME='www.example.com:8080', - APPLICATION_ROOT='/test', - SESSION_COOKIE_DOMAIN='.example.com', - SESSION_COOKIE_HTTPONLY=False, - SESSION_COOKIE_SECURE=True, - SESSION_COOKIE_PATH='/' - ) - @app.route('/') - def index(): - flask.session['testing'] = 42 - return 'Hello World' - rv = app.test_client().get('/', 'http://www.example.com:8080/test/') - cookie = rv.headers['set-cookie'].lower() - self.assert_in('domain=.example.com', cookie) - self.assert_in('path=/', cookie) - self.assert_in('secure', cookie) - self.assert_not_in('httponly', cookie) - - def test_missing_session(self): - app = flask.Flask(__name__) - def expect_exception(f, *args, **kwargs): - try: - f(*args, **kwargs) - except RuntimeError as e: - self.assert_true(e.args and 'session is unavailable' in e.args[0]) - else: - self.assert_true(False, 'expected exception') - with app.test_request_context(): - self.assert_true(flask.session.get('missing_key') is None) - expect_exception(flask.session.__setitem__, 'foo', 42) - expect_exception(flask.session.pop, 'foo') - - def test_session_expiration(self): - permanent = True - app = flask.Flask(__name__) - app.secret_key = 'testkey' - @app.route('/') - def index(): - flask.session['test'] = 42 - flask.session.permanent = permanent - return '' - - @app.route('/test') - def test(): - return text_type(flask.session.permanent) - - client = app.test_client() - rv = client.get('/') - self.assert_in('set-cookie', rv.headers) - match = re.search(r'\bexpires=([^;]+)(?i)', rv.headers['set-cookie']) - expires = parse_date(match.group()) - expected = datetime.utcnow() + app.permanent_session_lifetime - self.assert_equal(expires.year, expected.year) - self.assert_equal(expires.month, expected.month) - self.assert_equal(expires.day, expected.day) - - rv = client.get('/test') - self.assert_equal(rv.data, b'True') - - permanent = False - rv = app.test_client().get('/') - self.assert_in('set-cookie', rv.headers) - match = re.search(r'\bexpires=([^;]+)', rv.headers['set-cookie']) - self.assert_true(match is None) - - def test_session_stored_last(self): - app = flask.Flask(__name__) - app.secret_key = 'development-key' - app.testing = True - - @app.after_request - def modify_session(response): - flask.session['foo'] = 42 - return response - @app.route('/') - def dump_session_contents(): - return repr(flask.session.get('foo')) - - c = app.test_client() - self.assert_equal(c.get('/').data, b'None') - self.assert_equal(c.get('/').data, b'42') - - def test_session_special_types(self): - app = flask.Flask(__name__) - app.secret_key = 'development-key' - app.testing = True - now = datetime.utcnow().replace(microsecond=0) - the_uuid = uuid.uuid4() - - @app.after_request - def modify_session(response): - flask.session['m'] = flask.Markup('Hello!') - flask.session['u'] = the_uuid - flask.session['dt'] = now - flask.session['b'] = b'\xff' - flask.session['t'] = (1, 2, 3) - return response - - @app.route('/') - def dump_session_contents(): - return pickle.dumps(dict(flask.session)) - - c = app.test_client() - c.get('/') - rv = pickle.loads(c.get('/').data) - self.assert_equal(rv['m'], flask.Markup('Hello!')) - self.assert_equal(type(rv['m']), flask.Markup) - self.assert_equal(rv['dt'], now) - self.assert_equal(rv['u'], the_uuid) - self.assert_equal(rv['b'], b'\xff') - self.assert_equal(type(rv['b']), bytes) - self.assert_equal(rv['t'], (1, 2, 3)) - - def test_session_cookie_setting(self): - app = flask.Flask(__name__) - app.testing = True - app.secret_key = 'dev key' - is_permanent = True - - @app.route('/bump') - def bump(): - rv = flask.session['foo'] = flask.session.get('foo', 0) + 1 - flask.session.permanent = is_permanent - return str(rv) - - @app.route('/read') - def read(): - return str(flask.session.get('foo', 0)) - - def run_test(expect_header): - with app.test_client() as c: - self.assert_equal(c.get('/bump').data, b'1') - self.assert_equal(c.get('/bump').data, b'2') - self.assert_equal(c.get('/bump').data, b'3') - - rv = c.get('/read') - set_cookie = rv.headers.get('set-cookie') - self.assert_equal(set_cookie is not None, expect_header) - self.assert_equal(rv.data, b'3') - - is_permanent = True - app.config['SESSION_REFRESH_EACH_REQUEST'] = True - run_test(expect_header=True) - - is_permanent = True - app.config['SESSION_REFRESH_EACH_REQUEST'] = False - run_test(expect_header=False) - - is_permanent = False - app.config['SESSION_REFRESH_EACH_REQUEST'] = True - run_test(expect_header=False) - - is_permanent = False - app.config['SESSION_REFRESH_EACH_REQUEST'] = False - run_test(expect_header=False) - - def test_flashes(self): - app = flask.Flask(__name__) - app.secret_key = 'testkey' - - with app.test_request_context(): - self.assert_false(flask.session.modified) - flask.flash('Zap') - flask.session.modified = False - flask.flash('Zip') - self.assert_true(flask.session.modified) - self.assert_equal(list(flask.get_flashed_messages()), ['Zap', 'Zip']) - - def test_extended_flashing(self): - # Be sure app.testing=True below, else tests can fail silently. - # - # Specifically, if app.testing is not set to True, the AssertionErrors - # in the view functions will cause a 500 response to the test client - # instead of propagating exceptions. - - app = flask.Flask(__name__) - app.secret_key = 'testkey' - app.testing = True - - @app.route('/') - def index(): - flask.flash(u'Hello World') - flask.flash(u'Hello World', 'error') - flask.flash(flask.Markup(u'Testing'), 'warning') - return '' - - @app.route('/test/') - def test(): - messages = flask.get_flashed_messages() - self.assert_equal(len(messages), 3) - self.assert_equal(messages[0], u'Hello World') - self.assert_equal(messages[1], u'Hello World') - self.assert_equal(messages[2], flask.Markup(u'Testing')) - return '' - - @app.route('/test_with_categories/') - def test_with_categories(): - messages = flask.get_flashed_messages(with_categories=True) - self.assert_equal(len(messages), 3) - self.assert_equal(messages[0], ('message', u'Hello World')) - self.assert_equal(messages[1], ('error', u'Hello World')) - self.assert_equal(messages[2], ('warning', flask.Markup(u'Testing'))) - return '' - - @app.route('/test_filter/') - def test_filter(): - messages = flask.get_flashed_messages(category_filter=['message'], with_categories=True) - self.assert_equal(len(messages), 1) - self.assert_equal(messages[0], ('message', u'Hello World')) - return '' - - @app.route('/test_filters/') - def test_filters(): - messages = flask.get_flashed_messages(category_filter=['message', 'warning'], with_categories=True) - self.assert_equal(len(messages), 2) - self.assert_equal(messages[0], ('message', u'Hello World')) - self.assert_equal(messages[1], ('warning', flask.Markup(u'Testing'))) - return '' - - @app.route('/test_filters_without_returning_categories/') - def test_filters2(): - messages = flask.get_flashed_messages(category_filter=['message', 'warning']) - self.assert_equal(len(messages), 2) - self.assert_equal(messages[0], u'Hello World') - self.assert_equal(messages[1], flask.Markup(u'Testing')) - return '' - - # Create new test client on each test to clean flashed messages. - - c = app.test_client() - c.get('/') - c.get('/test/') - - c = app.test_client() - c.get('/') - c.get('/test_with_categories/') - - c = app.test_client() - c.get('/') - c.get('/test_filter/') - - c = app.test_client() - c.get('/') - c.get('/test_filters/') - - c = app.test_client() - c.get('/') - c.get('/test_filters_without_returning_categories/') - - def test_request_processing(self): - app = flask.Flask(__name__) - evts = [] - @app.before_request - def before_request(): - evts.append('before') - @app.after_request - def after_request(response): - response.data += b'|after' - evts.append('after') - return response - @app.route('/') - def index(): - self.assert_in('before', evts) - self.assert_not_in('after', evts) - return 'request' - self.assert_not_in('after', evts) - rv = app.test_client().get('/').data - self.assert_in('after', evts) - self.assert_equal(rv, b'request|after') - - def test_after_request_processing(self): - app = flask.Flask(__name__) - app.testing = True - @app.route('/') - def index(): - @flask.after_this_request - def foo(response): - response.headers['X-Foo'] = 'a header' - return response - return 'Test' - c = app.test_client() - resp = c.get('/') - self.assertEqual(resp.status_code, 200) - self.assertEqual(resp.headers['X-Foo'], 'a header') - - def test_teardown_request_handler(self): - called = [] - app = flask.Flask(__name__) - @app.teardown_request - def teardown_request(exc): - called.append(True) - return "Ignored" - @app.route('/') - def root(): - return "Response" - rv = app.test_client().get('/') - self.assert_equal(rv.status_code, 200) - self.assert_in(b'Response', rv.data) - self.assert_equal(len(called), 1) - - def test_teardown_request_handler_debug_mode(self): - called = [] - app = flask.Flask(__name__) - app.testing = True - @app.teardown_request - def teardown_request(exc): - called.append(True) - return "Ignored" - @app.route('/') - def root(): - return "Response" - rv = app.test_client().get('/') - self.assert_equal(rv.status_code, 200) - self.assert_in(b'Response', rv.data) - self.assert_equal(len(called), 1) - - def test_teardown_request_handler_error(self): - called = [] - app = flask.Flask(__name__) - app.config['LOGGER_HANDLER_POLICY'] = 'never' - @app.teardown_request - def teardown_request1(exc): - self.assert_equal(type(exc), ZeroDivisionError) - called.append(True) - # This raises a new error and blows away sys.exc_info(), so we can - # test that all teardown_requests get passed the same original - # exception. - try: - raise TypeError() - except: - pass - @app.teardown_request - def teardown_request2(exc): - self.assert_equal(type(exc), ZeroDivisionError) - called.append(True) - # This raises a new error and blows away sys.exc_info(), so we can - # test that all teardown_requests get passed the same original - # exception. - try: - raise TypeError() - except: - pass - @app.route('/') - def fails(): - 1 // 0 - rv = app.test_client().get('/') - self.assert_equal(rv.status_code, 500) - self.assert_in(b'Internal Server Error', rv.data) - self.assert_equal(len(called), 2) - - def test_before_after_request_order(self): - called = [] - app = flask.Flask(__name__) - @app.before_request - def before1(): - called.append(1) - @app.before_request - def before2(): - called.append(2) - @app.after_request - def after1(response): - called.append(4) - return response - @app.after_request - def after2(response): - called.append(3) - return response - @app.teardown_request - def finish1(exc): - called.append(6) - @app.teardown_request - def finish2(exc): - called.append(5) - @app.route('/') - def index(): - return '42' - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'42') - self.assert_equal(called, [1, 2, 3, 4, 5, 6]) - - def test_error_handling(self): - app = flask.Flask(__name__) - app.config['LOGGER_HANDLER_POLICY'] = 'never' - @app.errorhandler(404) - def not_found(e): - return 'not found', 404 - @app.errorhandler(500) - def internal_server_error(e): - return 'internal server error', 500 - @app.errorhandler(Forbidden) - def forbidden(e): - return 'forbidden', 403 - @app.route('/') - def index(): - flask.abort(404) - @app.route('/error') - def error(): - 1 // 0 - @app.route('/forbidden') - def error2(): - flask.abort(403) - 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) - rv = c.get('/forbidden') - self.assert_equal(rv.status_code, 403) - self.assert_equal(b'forbidden', rv.data) - - def test_before_request_and_routing_errors(self): - app = flask.Flask(__name__) - @app.before_request - def attach_something(): - flask.g.something = 'value' - @app.errorhandler(404) - def return_something(error): - return flask.g.something, 404 - rv = app.test_client().get('/') - self.assert_equal(rv.status_code, 404) - self.assert_equal(rv.data, b'value') - - def test_user_error_handling(self): - class MyException(Exception): - pass - - app = flask.Flask(__name__) - @app.errorhandler(MyException) - def handle_my_exception(e): - self.assert_true(isinstance(e, MyException)) - return '42' - @app.route('/') - def index(): - raise MyException() - - c = app.test_client() - self.assert_equal(c.get('/').data, b'42') - - def test_http_error_subclass_handling(self): - class ForbiddenSubclass(Forbidden): - pass - - app = flask.Flask(__name__) - @app.errorhandler(ForbiddenSubclass) - def handle_forbidden_subclass(e): - self.assert_true(isinstance(e, ForbiddenSubclass)) - return 'banana' - @app.errorhandler(403) - def handle_forbidden_subclass(e): - self.assert_false(isinstance(e, ForbiddenSubclass)) - self.assert_true(isinstance(e, Forbidden)) - return 'apple' - - @app.route('/1') - def index1(): - raise ForbiddenSubclass() - @app.route('/2') - def index2(): - flask.abort(403) - @app.route('/3') - def index3(): - raise Forbidden() - - c = app.test_client() - self.assert_equal(c.get('/1').data, b'banana') - self.assert_equal(c.get('/2').data, b'apple') - self.assert_equal(c.get('/3').data, b'apple') - - def test_trapping_of_bad_request_key_errors(self): - app = flask.Flask(__name__) - app.testing = True - @app.route('/fail') - def fail(): - flask.request.form['missing_key'] - c = app.test_client() - self.assert_equal(c.get('/fail').status_code, 400) - - app.config['TRAP_BAD_REQUEST_ERRORS'] = True - c = app.test_client() - try: - c.get('/fail') - except KeyError as e: - self.assert_true(isinstance(e, BadRequest)) - else: - self.fail('Expected exception') - - def test_trapping_of_all_http_exceptions(self): - app = flask.Flask(__name__) - app.testing = True - app.config['TRAP_HTTP_EXCEPTIONS'] = True - @app.route('/fail') - def fail(): - flask.abort(404) - - c = app.test_client() - try: - c.get('/fail') - except NotFound as e: - pass - else: - self.fail('Expected exception') - - def test_enctype_debug_helper(self): - from flask.debughelpers import DebugFilesKeyError - app = flask.Flask(__name__) - app.debug = True - @app.route('/fail', methods=['POST']) - def index(): - return flask.request.files['foo'].filename - - # with statement is important because we leave an exception on the - # stack otherwise and we want to ensure that this is not the case - # to not negatively affect other tests. - with app.test_client() as c: - try: - c.post('/fail', data={'foo': 'index.txt'}) - except DebugFilesKeyError as e: - self.assert_in('no file contents were transmitted', str(e)) - self.assert_in('This was submitted: "index.txt"', str(e)) - else: - self.fail('Expected exception') - - def test_response_creation(self): - app = flask.Flask(__name__) - @app.route('/unicode') - def from_unicode(): - return u'Hällo Wörld' - @app.route('/string') - def from_string(): - return u'Hällo Wörld'.encode('utf-8') - @app.route('/args') - def from_tuple(): - return 'Meh', 400, { - 'X-Foo': 'Testing', - 'Content-Type': 'text/plain; charset=utf-8' - } - @app.route('/two_args') - def from_two_args_tuple(): - return 'Hello', { - 'X-Foo': 'Test', - 'Content-Type': 'text/plain; charset=utf-8' - } - @app.route('/args_status') - def from_status_tuple(): - return 'Hi, status!', 400 - @app.route('/args_header') - def from_response_instance_status_tuple(): - return flask.Response('Hello world', 404), { - "X-Foo": "Bar", - "X-Bar": "Foo" - } - - c = app.test_client() - self.assert_equal(c.get('/unicode').data, u'Hällo Wörld'.encode('utf-8')) - self.assert_equal(c.get('/string').data, u'Hällo Wörld'.encode('utf-8')) - rv = c.get('/args') - self.assert_equal(rv.data, b'Meh') - self.assert_equal(rv.headers['X-Foo'], 'Testing') - self.assert_equal(rv.status_code, 400) - self.assert_equal(rv.mimetype, 'text/plain') - rv2 = c.get('/two_args') - self.assert_equal(rv2.data, b'Hello') - self.assert_equal(rv2.headers['X-Foo'], 'Test') - self.assert_equal(rv2.status_code, 200) - self.assert_equal(rv2.mimetype, 'text/plain') - rv3 = c.get('/args_status') - self.assert_equal(rv3.data, b'Hi, status!') - self.assert_equal(rv3.status_code, 400) - self.assert_equal(rv3.mimetype, 'text/html') - rv4 = c.get('/args_header') - self.assert_equal(rv4.data, b'Hello world') - self.assert_equal(rv4.headers['X-Foo'], 'Bar') - self.assert_equal(rv4.headers['X-Bar'], 'Foo') - self.assert_equal(rv4.status_code, 404) - - def test_make_response(self): - app = flask.Flask(__name__) - with app.test_request_context(): - rv = flask.make_response() - self.assert_equal(rv.status_code, 200) - self.assert_equal(rv.data, b'') - self.assert_equal(rv.mimetype, 'text/html') - - rv = flask.make_response('Awesome') - self.assert_equal(rv.status_code, 200) - self.assert_equal(rv.data, b'Awesome') - self.assert_equal(rv.mimetype, 'text/html') - - rv = flask.make_response('W00t', 404) - self.assert_equal(rv.status_code, 404) - self.assert_equal(rv.data, b'W00t') - self.assert_equal(rv.mimetype, 'text/html') - - def test_make_response_with_response_instance(self): - app = flask.Flask(__name__) - with app.test_request_context(): - rv = flask.make_response( - flask.jsonify({'msg': 'W00t'}), 400) - self.assertEqual(rv.status_code, 400) - self.assertEqual(rv.data, b'{\n "msg": "W00t"\n}') - self.assertEqual(rv.mimetype, 'application/json') - - rv = flask.make_response( - flask.Response(''), 400) - self.assertEqual(rv.status_code, 400) - self.assertEqual(rv.data, b'') - self.assertEqual(rv.mimetype, 'text/html') - - rv = flask.make_response( - flask.Response('', headers={'Content-Type': 'text/html'}), - 400, [('X-Foo', 'bar')]) - self.assertEqual(rv.status_code, 400) - self.assertEqual(rv.headers['Content-Type'], 'text/html') - self.assertEqual(rv.headers['X-Foo'], 'bar') - - def test_url_generation(self): - app = flask.Flask(__name__) - @app.route('/hello/', methods=['POST']) - def hello(): - pass - with app.test_request_context(): - self.assert_equal(flask.url_for('hello', name='test x'), '/hello/test%20x') - self.assert_equal(flask.url_for('hello', name='test x', _external=True), - 'http://localhost/hello/test%20x') - - def test_build_error_handler(self): - app = flask.Flask(__name__) - - # Test base case, a URL which results in a BuildError. - with app.test_request_context(): - self.assertRaises(BuildError, flask.url_for, 'spam') - - # Verify the error is re-raised if not the current exception. - try: - with app.test_request_context(): - flask.url_for('spam') - except BuildError as err: - error = err - try: - raise RuntimeError('Test case where BuildError is not current.') - except RuntimeError: - self.assertRaises(BuildError, app.handle_url_build_error, error, 'spam', {}) - - # Test a custom handler. - def handler(error, endpoint, values): - # Just a test. - return '/test_handler/' - app.url_build_error_handlers.append(handler) - with app.test_request_context(): - self.assert_equal(flask.url_for('spam'), '/test_handler/') - - def test_custom_converters(self): - from werkzeug.routing import BaseConverter - class ListConverter(BaseConverter): - def to_python(self, value): - return value.split(',') - def to_url(self, value): - base_to_url = super(ListConverter, self).to_url - return ','.join(base_to_url(x) for x in value) - app = flask.Flask(__name__) - app.url_map.converters['list'] = ListConverter - @app.route('/') - def index(args): - return '|'.join(args) - c = app.test_client() - self.assert_equal(c.get('/1,2,3').data, b'1|2|3') - - def test_static_files(self): - app = flask.Flask(__name__) - app.testing = True - rv = app.test_client().get('/static/index.html') - self.assert_equal(rv.status_code, 200) - self.assert_equal(rv.data.strip(), b'

Hello World!

') - with app.test_request_context(): - self.assert_equal(flask.url_for('static', filename='index.html'), - '/static/index.html') - rv.close() - - def test_none_response(self): - app = flask.Flask(__name__) - app.testing = True - @app.route('/') - def test(): - return None - try: - app.test_client().get('/') - except ValueError as e: - self.assert_equal(str(e), 'View function did not return a response') - pass - else: - self.assert_true("Expected ValueError") - - def test_request_locals(self): - self.assert_equal(repr(flask.g), '') - self.assertFalse(flask.g) - - def test_test_app_proper_environ(self): - app = flask.Flask(__name__) - app.config.update( - SERVER_NAME='localhost.localdomain:5000' - ) - @app.route('/') - def index(): - return 'Foo' - - @app.route('/', subdomain='foo') - def subdomain(): - return 'Foo SubDomain' - - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'Foo') - - rv = app.test_client().get('/', 'http://localhost.localdomain:5000') - self.assert_equal(rv.data, b'Foo') - - rv = app.test_client().get('/', 'https://localhost.localdomain:5000') - self.assert_equal(rv.data, b'Foo') - - app.config.update(SERVER_NAME='localhost.localdomain') - rv = app.test_client().get('/', 'https://localhost.localdomain') - self.assert_equal(rv.data, b'Foo') - - try: - app.config.update(SERVER_NAME='localhost.localdomain:443') - rv = app.test_client().get('/', 'https://localhost.localdomain') - # Werkzeug 0.8 - self.assert_equal(rv.status_code, 404) - except ValueError as e: - # Werkzeug 0.7 - self.assert_equal(str(e), "the server name provided " + - "('localhost.localdomain:443') does not match the " + \ - "server name from the WSGI environment ('localhost.localdomain')") - - try: - app.config.update(SERVER_NAME='localhost.localdomain') - rv = app.test_client().get('/', 'http://foo.localhost') - # Werkzeug 0.8 - self.assert_equal(rv.status_code, 404) - except ValueError as e: - # Werkzeug 0.7 - self.assert_equal(str(e), "the server name provided " + \ - "('localhost.localdomain') does not match the " + \ - "server name from the WSGI environment ('foo.localhost')") - - rv = app.test_client().get('/', 'http://foo.localhost.localdomain') - self.assert_equal(rv.data, b'Foo SubDomain') - - def test_exception_propagation(self): - def apprunner(configkey): - app = flask.Flask(__name__) - app.config['LOGGER_HANDLER_POLICY'] = 'never' - @app.route('/') - def index(): - 1 // 0 - c = app.test_client() - if config_key is not None: - app.config[config_key] = True - try: - c.get('/') - except Exception: - pass - else: - self.fail('expected exception') - else: - self.assert_equal(c.get('/').status_code, 500) - - # we have to run this test in an isolated thread because if the - # debug flag is set to true and an exception happens the context is - # not torn down. This causes other tests that run after this fail - # when they expect no exception on the stack. - for config_key in 'TESTING', 'PROPAGATE_EXCEPTIONS', 'DEBUG', None: - t = Thread(target=apprunner, args=(config_key,)) - t.start() - t.join() - - def test_max_content_length(self): - app = flask.Flask(__name__) - app.config['MAX_CONTENT_LENGTH'] = 64 - @app.before_request - def always_first(): - flask.request.form['myfile'] - self.assert_true(False) - @app.route('/accept', methods=['POST']) - def accept_file(): - flask.request.form['myfile'] - self.assert_true(False) - @app.errorhandler(413) - def catcher(error): - return '42' - - c = app.test_client() - rv = c.post('/accept', data={'myfile': 'foo' * 100}) - self.assert_equal(rv.data, b'42') - - def test_url_processors(self): - app = flask.Flask(__name__) - - @app.url_defaults - def add_language_code(endpoint, values): - if flask.g.lang_code is not None and \ - app.url_map.is_endpoint_expecting(endpoint, 'lang_code'): - values.setdefault('lang_code', flask.g.lang_code) - - @app.url_value_preprocessor - def pull_lang_code(endpoint, values): - flask.g.lang_code = values.pop('lang_code', None) - - @app.route('//') - def index(): - return flask.url_for('about') - - @app.route('//about') - def about(): - return flask.url_for('something_else') - - @app.route('/foo') - def something_else(): - return flask.url_for('about', lang_code='en') - - c = app.test_client() - - self.assert_equal(c.get('/de/').data, b'/de/about') - self.assert_equal(c.get('/de/about').data, b'/foo') - self.assert_equal(c.get('/foo').data, b'/en/about') - - def test_inject_blueprint_url_defaults(self): - app = flask.Flask(__name__) - bp = flask.Blueprint('foo.bar.baz', __name__, - template_folder='template') - - @bp.url_defaults - def bp_defaults(endpoint, values): - values['page'] = 'login' - @bp.route('/') - def view(page): pass - - app.register_blueprint(bp) - - values = dict() - app.inject_url_defaults('foo.bar.baz.view', values) - expected = dict(page='login') - self.assert_equal(values, expected) - - with app.test_request_context('/somepage'): - url = flask.url_for('foo.bar.baz.view') - expected = '/login' - self.assert_equal(url, expected) - - def test_nonascii_pathinfo(self): - app = flask.Flask(__name__) - app.testing = True - - @app.route(u'/киртест') - def index(): - return 'Hello World!' - - c = app.test_client() - rv = c.get(u'/киртест') - self.assert_equal(rv.data, b'Hello World!') - - def test_debug_mode_complains_after_first_request(self): - app = flask.Flask(__name__) - app.debug = True - @app.route('/') - def index(): - return 'Awesome' - self.assert_false(app.got_first_request) - self.assert_equal(app.test_client().get('/').data, b'Awesome') - try: - @app.route('/foo') - def broken(): - return 'Meh' - except AssertionError as e: - self.assert_in('A setup function was called', str(e)) - else: - self.fail('Expected exception') - - app.debug = False - @app.route('/foo') - def working(): - return 'Meh' - self.assert_equal(app.test_client().get('/foo').data, b'Meh') - self.assert_true(app.got_first_request) - - def test_before_first_request_functions(self): - got = [] - app = flask.Flask(__name__) - @app.before_first_request - def foo(): - got.append(42) - c = app.test_client() - c.get('/') - self.assert_equal(got, [42]) - c.get('/') - self.assert_equal(got, [42]) - self.assert_true(app.got_first_request) - - def test_before_first_request_functions_concurrent(self): - got = [] - app = flask.Flask(__name__) - - @app.before_first_request - def foo(): - time.sleep(0.2) - got.append(42) - - c = app.test_client() - def get_and_assert(): - c.get("/") - self.assert_equal(got, [42]) - - t = Thread(target=get_and_assert) - t.start() - get_and_assert() - t.join() - self.assert_true(app.got_first_request) - - def test_routing_redirect_debugging(self): - app = flask.Flask(__name__) - app.debug = True - @app.route('/foo/', methods=['GET', 'POST']) - def foo(): - return 'success' - with app.test_client() as c: - try: - c.post('/foo', data={}) - except AssertionError as e: - self.assert_in('http://localhost/foo/', str(e)) - self.assert_in('Make sure to directly send your POST-request ' - 'to this URL', str(e)) - else: - self.fail('Expected exception') - - rv = c.get('/foo', data={}, follow_redirects=True) - self.assert_equal(rv.data, b'success') - - app.debug = False - with app.test_client() as c: - rv = c.post('/foo', data={}, follow_redirects=True) - self.assert_equal(rv.data, b'success') - - def test_route_decorator_custom_endpoint(self): - app = flask.Flask(__name__) - app.debug = True - - @app.route('/foo/') - def foo(): - return flask.request.endpoint - - @app.route('/bar/', endpoint='bar') - def for_bar(): - return flask.request.endpoint - - @app.route('/bar/123', endpoint='123') - def for_bar_foo(): - return flask.request.endpoint - - with app.test_request_context(): - assert flask.url_for('foo') == '/foo/' - assert flask.url_for('bar') == '/bar/' - assert flask.url_for('123') == '/bar/123' - - c = app.test_client() - self.assertEqual(c.get('/foo/').data, b'foo') - self.assertEqual(c.get('/bar/').data, b'bar') - self.assertEqual(c.get('/bar/123').data, b'123') - - def test_preserve_only_once(self): - app = flask.Flask(__name__) - app.debug = True - - @app.route('/fail') - def fail_func(): - 1 // 0 - - c = app.test_client() - for x in range(3): - with self.assert_raises(ZeroDivisionError): - c.get('/fail') - - self.assert_true(flask._request_ctx_stack.top is not None) - self.assert_true(flask._app_ctx_stack.top is not None) - # implicit appctx disappears too - flask._request_ctx_stack.top.pop() - self.assert_true(flask._request_ctx_stack.top is None) - self.assert_true(flask._app_ctx_stack.top is None) - - def test_preserve_remembers_exception(self): - app = flask.Flask(__name__) - app.debug = True - errors = [] - - @app.route('/fail') - def fail_func(): - 1 // 0 - - @app.route('/success') - def success_func(): - return 'Okay' - - @app.teardown_request - def teardown_handler(exc): - errors.append(exc) - - c = app.test_client() - - # After this failure we did not yet call the teardown handler - with self.assert_raises(ZeroDivisionError): - c.get('/fail') - self.assert_equal(errors, []) - - # But this request triggers it, and it's an error - c.get('/success') - self.assert_equal(len(errors), 2) - self.assert_true(isinstance(errors[0], ZeroDivisionError)) - - # At this point another request does nothing. - c.get('/success') - self.assert_equal(len(errors), 3) - self.assert_equal(errors[1], None) - - def test_get_method_on_g(self): - app = flask.Flask(__name__) - app.testing = True - - with app.app_context(): - self.assert_equal(flask.g.get('x'), None) - self.assert_equal(flask.g.get('x', 11), 11) - flask.g.x = 42 - self.assert_equal(flask.g.get('x'), 42) - self.assert_equal(flask.g.x, 42) - - def test_g_iteration_protocol(self): - app = flask.Flask(__name__) - app.testing = True - - with app.app_context(): - flask.g.foo = 23 - flask.g.bar = 42 - self.assert_equal('foo' in flask.g, True) - self.assert_equal('foos' in flask.g, False) - self.assert_equal(sorted(flask.g), ['bar', 'foo']) - - -class SubdomainTestCase(FlaskTestCase): - - def test_basic_support(self): - app = flask.Flask(__name__) - app.config['SERVER_NAME'] = 'localhost' - @app.route('/') - def normal_index(): - return 'normal index' - @app.route('/', subdomain='test') - def test_index(): - return 'test index' - - c = app.test_client() - rv = c.get('/', 'http://localhost/') - self.assert_equal(rv.data, b'normal index') - - rv = c.get('/', 'http://test.localhost/') - self.assert_equal(rv.data, b'test index') - - def test_subdomain_matching(self): - app = flask.Flask(__name__) - app.config['SERVER_NAME'] = 'localhost' - @app.route('/', subdomain='') - def index(user): - return 'index for %s' % user - - c = app.test_client() - rv = c.get('/', 'http://mitsuhiko.localhost/') - self.assert_equal(rv.data, b'index for mitsuhiko') - - def test_subdomain_matching_with_ports(self): - app = flask.Flask(__name__) - app.config['SERVER_NAME'] = 'localhost:3000' - @app.route('/', subdomain='') - def index(user): - return 'index for %s' % user - - c = app.test_client() - rv = c.get('/', 'http://mitsuhiko.localhost:3000/') - self.assert_equal(rv.data, b'index for mitsuhiko') - - def test_multi_route_rules(self): - app = flask.Flask(__name__) - - @app.route('/') - @app.route('//') - def index(test='a'): - return test - - rv = app.test_client().open('/') - self.assert_equal(rv.data, b'a') - rv = app.test_client().open('/b/') - self.assert_equal(rv.data, b'b') - - def test_multi_route_class_views(self): - class View(object): - def __init__(self, app): - app.add_url_rule('/', 'index', self.index) - app.add_url_rule('//', 'index', self.index) - - def index(self, test='a'): - return test - - app = flask.Flask(__name__) - _ = View(app) - rv = app.test_client().open('/') - self.assert_equal(rv.data, b'a') - rv = app.test_client().open('/b/') - self.assert_equal(rv.data, b'b') - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(BasicFunctionalityTestCase)) - suite.addTest(unittest.makeSuite(SubdomainTestCase)) - return suite diff --git a/flask/testsuite/blueprints.py b/flask/testsuite/blueprints.py deleted file mode 100644 index 8d3224a3..00000000 --- a/flask/testsuite/blueprints.py +++ /dev/null @@ -1,585 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.blueprints - ~~~~~~~~~~~~~~~~~~~~~~~~~~ - - Blueprints (and currently modules) - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -import flask -import unittest -from flask.testsuite import FlaskTestCase -from flask._compat import text_type -from werkzeug.http import parse_cache_control_header -from jinja2 import TemplateNotFound - - -class BlueprintTestCase(FlaskTestCase): - - def test_blueprint_specific_error_handling(self): - frontend = flask.Blueprint('frontend', __name__) - backend = flask.Blueprint('backend', __name__) - sideend = flask.Blueprint('sideend', __name__) - - @frontend.errorhandler(403) - def frontend_forbidden(e): - return 'frontend says no', 403 - - @frontend.route('/frontend-no') - def frontend_no(): - flask.abort(403) - - @backend.errorhandler(403) - def backend_forbidden(e): - return 'backend says no', 403 - - @backend.route('/backend-no') - def backend_no(): - flask.abort(403) - - @sideend.route('/what-is-a-sideend') - def sideend_no(): - flask.abort(403) - - app = flask.Flask(__name__) - app.register_blueprint(frontend) - app.register_blueprint(backend) - app.register_blueprint(sideend) - - @app.errorhandler(403) - def app_forbidden(e): - return 'application itself says no', 403 - - c = app.test_client() - - self.assert_equal(c.get('/frontend-no').data, b'frontend says no') - self.assert_equal(c.get('/backend-no').data, b'backend says no') - self.assert_equal(c.get('/what-is-a-sideend').data, b'application itself says no') - - def test_blueprint_specific_user_error_handling(self): - class MyDecoratorException(Exception): - pass - class MyFunctionException(Exception): - pass - - blue = flask.Blueprint('blue', __name__) - - @blue.errorhandler(MyDecoratorException) - def my_decorator_exception_handler(e): - self.assert_true(isinstance(e, MyDecoratorException)) - return 'boom' - - def my_function_exception_handler(e): - self.assert_true(isinstance(e, MyFunctionException)) - return 'bam' - blue.register_error_handler(MyFunctionException, my_function_exception_handler) - - @blue.route('/decorator') - def blue_deco_test(): - raise MyDecoratorException() - @blue.route('/function') - def blue_func_test(): - raise MyFunctionException() - - app = flask.Flask(__name__) - app.register_blueprint(blue) - - c = app.test_client() - - self.assert_equal(c.get('/decorator').data, b'boom') - self.assert_equal(c.get('/function').data, b'bam') - - def test_blueprint_url_definitions(self): - bp = flask.Blueprint('test', __name__) - - @bp.route('/foo', defaults={'baz': 42}) - def foo(bar, baz): - return '%s/%d' % (bar, baz) - - @bp.route('/bar') - def bar(bar): - return text_type(bar) - - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/1', url_defaults={'bar': 23}) - app.register_blueprint(bp, url_prefix='/2', url_defaults={'bar': 19}) - - c = app.test_client() - self.assert_equal(c.get('/1/foo').data, b'23/42') - self.assert_equal(c.get('/2/foo').data, b'19/42') - self.assert_equal(c.get('/1/bar').data, b'23') - self.assert_equal(c.get('/2/bar').data, b'19') - - def test_blueprint_url_processors(self): - bp = flask.Blueprint('frontend', __name__, url_prefix='/') - - @bp.url_defaults - def add_language_code(endpoint, values): - values.setdefault('lang_code', flask.g.lang_code) - - @bp.url_value_preprocessor - def pull_lang_code(endpoint, values): - flask.g.lang_code = values.pop('lang_code') - - @bp.route('/') - def index(): - return flask.url_for('.about') - - @bp.route('/about') - def about(): - return flask.url_for('.index') - - app = flask.Flask(__name__) - app.register_blueprint(bp) - - c = app.test_client() - - self.assert_equal(c.get('/de/').data, b'/de/about') - self.assert_equal(c.get('/de/about').data, b'/de/') - - def test_templates_and_static(self): - from blueprintapp import app - 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() - - # try/finally, in case other tests use this app for Blueprint tests. - max_age_default = app.config['SEND_FILE_MAX_AGE_DEFAULT'] - try: - expected_max_age = 3600 - if app.config['SEND_FILE_MAX_AGE_DEFAULT'] == expected_max_age: - expected_max_age = 7200 - app.config['SEND_FILE_MAX_AGE_DEFAULT'] = expected_max_age - rv = c.get('/admin/static/css/test.css') - cc = parse_cache_control_header(rv.headers['Cache-Control']) - self.assert_equal(cc.max_age, expected_max_age) - rv.close() - finally: - app.config['SEND_FILE_MAX_AGE_DEFAULT'] = max_age_default - - 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_default_static_cache_timeout(self): - app = flask.Flask(__name__) - class MyBlueprint(flask.Blueprint): - def get_send_file_max_age(self, filename): - return 100 - - blueprint = MyBlueprint('blueprint', __name__, static_folder='static') - app.register_blueprint(blueprint) - - # try/finally, in case other tests use this app for Blueprint tests. - max_age_default = app.config['SEND_FILE_MAX_AGE_DEFAULT'] - try: - with app.test_request_context(): - unexpected_max_age = 3600 - if app.config['SEND_FILE_MAX_AGE_DEFAULT'] == unexpected_max_age: - unexpected_max_age = 7200 - app.config['SEND_FILE_MAX_AGE_DEFAULT'] = unexpected_max_age - rv = blueprint.send_static_file('index.html') - cc = parse_cache_control_header(rv.headers['Cache-Control']) - self.assert_equal(cc.max_age, 100) - rv.close() - finally: - app.config['SEND_FILE_MAX_AGE_DEFAULT'] = max_age_default - - def test_templates_list(self): - from blueprintapp import app - templates = sorted(app.jinja_env.list_templates()) - self.assert_equal(templates, ['admin/index.html', - 'frontend/index.html']) - - def test_dotted_names(self): - frontend = flask.Blueprint('myapp.frontend', __name__) - backend = flask.Blueprint('myapp.backend', __name__) - - @frontend.route('/fe') - def frontend_index(): - return flask.url_for('myapp.backend.backend_index') - - @frontend.route('/fe2') - def frontend_page2(): - return flask.url_for('.frontend_index') - - @backend.route('/be') - def backend_index(): - return flask.url_for('myapp.frontend.frontend_index') - - app = flask.Flask(__name__) - app.register_blueprint(frontend) - app.register_blueprint(backend) - - c = app.test_client() - self.assert_equal(c.get('/fe').data.strip(), b'/be') - self.assert_equal(c.get('/fe2').data.strip(), b'/fe') - self.assert_equal(c.get('/be').data.strip(), b'/fe') - - def test_dotted_names_from_app(self): - app = flask.Flask(__name__) - app.testing = True - test = flask.Blueprint('test', __name__) - - @app.route('/') - def app_index(): - return flask.url_for('test.index') - - @test.route('/test/') - def index(): - return flask.url_for('app_index') - - app.register_blueprint(test) - - with app.test_client() as c: - rv = c.get('/') - self.assert_equal(rv.data, b'/test/') - - def test_empty_url_defaults(self): - bp = flask.Blueprint('bp', __name__) - - @bp.route('/', defaults={'page': 1}) - @bp.route('/page/') - def something(page): - return str(page) - - app = flask.Flask(__name__) - app.register_blueprint(bp) - - c = app.test_client() - self.assert_equal(c.get('/').data, b'1') - self.assert_equal(c.get('/page/2').data, b'2') - - def test_route_decorator_custom_endpoint(self): - - bp = flask.Blueprint('bp', __name__) - - @bp.route('/foo') - def foo(): - return flask.request.endpoint - - @bp.route('/bar', endpoint='bar') - def foo_bar(): - return flask.request.endpoint - - @bp.route('/bar/123', endpoint='123') - def foo_bar_foo(): - return flask.request.endpoint - - @bp.route('/bar/foo') - def bar_foo(): - return flask.request.endpoint - - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - - @app.route('/') - def index(): - return flask.request.endpoint - - c = app.test_client() - self.assertEqual(c.get('/').data, b'index') - self.assertEqual(c.get('/py/foo').data, b'bp.foo') - self.assertEqual(c.get('/py/bar').data, b'bp.bar') - self.assertEqual(c.get('/py/bar/123').data, b'bp.123') - self.assertEqual(c.get('/py/bar/foo').data, b'bp.bar_foo') - - def test_route_decorator_custom_endpoint_with_dots(self): - bp = flask.Blueprint('bp', __name__) - - @bp.route('/foo') - def foo(): - return flask.request.endpoint - - try: - @bp.route('/bar', endpoint='bar.bar') - def foo_bar(): - return flask.request.endpoint - except AssertionError: - pass - else: - raise AssertionError('expected AssertionError not raised') - - try: - @bp.route('/bar/123', endpoint='bar.123') - def foo_bar_foo(): - return flask.request.endpoint - except AssertionError: - pass - else: - raise AssertionError('expected AssertionError not raised') - - def foo_foo_foo(): - pass - - self.assertRaises( - AssertionError, - lambda: bp.add_url_rule( - '/bar/123', endpoint='bar.123', view_func=foo_foo_foo - ) - ) - - self.assertRaises( - AssertionError, - bp.route('/bar/123', endpoint='bar.123'), - lambda: None - ) - - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - - c = app.test_client() - self.assertEqual(c.get('/py/foo').data, b'bp.foo') - # The rule's didn't actually made it through - rv = c.get('/py/bar') - assert rv.status_code == 404 - rv = c.get('/py/bar/123') - assert rv.status_code == 404 - - def test_template_filter(self): - bp = flask.Blueprint('bp', __name__) - @bp.app_template_filter() - def my_reverse(s): - return s[::-1] - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - self.assert_in('my_reverse', app.jinja_env.filters.keys()) - self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse) - self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba') - - def test_add_template_filter(self): - bp = flask.Blueprint('bp', __name__) - def my_reverse(s): - return s[::-1] - bp.add_app_template_filter(my_reverse) - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - self.assert_in('my_reverse', app.jinja_env.filters.keys()) - self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse) - self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba') - - def test_template_filter_with_name(self): - bp = flask.Blueprint('bp', __name__) - @bp.app_template_filter('strrev') - def my_reverse(s): - return s[::-1] - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - self.assert_in('strrev', app.jinja_env.filters.keys()) - self.assert_equal(app.jinja_env.filters['strrev'], my_reverse) - self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba') - - def test_add_template_filter_with_name(self): - bp = flask.Blueprint('bp', __name__) - def my_reverse(s): - return s[::-1] - bp.add_app_template_filter(my_reverse, 'strrev') - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - self.assert_in('strrev', app.jinja_env.filters.keys()) - self.assert_equal(app.jinja_env.filters['strrev'], my_reverse) - self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba') - - def test_template_filter_with_template(self): - bp = flask.Blueprint('bp', __name__) - @bp.app_template_filter() - def super_reverse(s): - return s[::-1] - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - @app.route('/') - def index(): - return flask.render_template('template_filter.html', value='abcd') - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') - - def test_template_filter_after_route_with_template(self): - app = flask.Flask(__name__) - @app.route('/') - def index(): - return flask.render_template('template_filter.html', value='abcd') - bp = flask.Blueprint('bp', __name__) - @bp.app_template_filter() - def super_reverse(s): - return s[::-1] - app.register_blueprint(bp, url_prefix='/py') - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') - - def test_add_template_filter_with_template(self): - bp = flask.Blueprint('bp', __name__) - def super_reverse(s): - return s[::-1] - bp.add_app_template_filter(super_reverse) - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - @app.route('/') - def index(): - return flask.render_template('template_filter.html', value='abcd') - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') - - def test_template_filter_with_name_and_template(self): - bp = flask.Blueprint('bp', __name__) - @bp.app_template_filter('super_reverse') - def my_reverse(s): - return s[::-1] - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - @app.route('/') - def index(): - return flask.render_template('template_filter.html', value='abcd') - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') - - def test_add_template_filter_with_name_and_template(self): - bp = flask.Blueprint('bp', __name__) - def my_reverse(s): - return s[::-1] - bp.add_app_template_filter(my_reverse, 'super_reverse') - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - @app.route('/') - def index(): - return flask.render_template('template_filter.html', value='abcd') - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') - - def test_template_test(self): - bp = flask.Blueprint('bp', __name__) - @bp.app_template_test() - def is_boolean(value): - return isinstance(value, bool) - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - self.assert_in('is_boolean', app.jinja_env.tests.keys()) - self.assert_equal(app.jinja_env.tests['is_boolean'], is_boolean) - self.assert_true(app.jinja_env.tests['is_boolean'](False)) - - def test_add_template_test(self): - bp = flask.Blueprint('bp', __name__) - def is_boolean(value): - return isinstance(value, bool) - bp.add_app_template_test(is_boolean) - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - self.assert_in('is_boolean', app.jinja_env.tests.keys()) - self.assert_equal(app.jinja_env.tests['is_boolean'], is_boolean) - self.assert_true(app.jinja_env.tests['is_boolean'](False)) - - def test_template_test_with_name(self): - bp = flask.Blueprint('bp', __name__) - @bp.app_template_test('boolean') - def is_boolean(value): - return isinstance(value, bool) - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - self.assert_in('boolean', app.jinja_env.tests.keys()) - self.assert_equal(app.jinja_env.tests['boolean'], is_boolean) - self.assert_true(app.jinja_env.tests['boolean'](False)) - - def test_add_template_test_with_name(self): - bp = flask.Blueprint('bp', __name__) - def is_boolean(value): - return isinstance(value, bool) - bp.add_app_template_test(is_boolean, 'boolean') - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - self.assert_in('boolean', app.jinja_env.tests.keys()) - self.assert_equal(app.jinja_env.tests['boolean'], is_boolean) - self.assert_true(app.jinja_env.tests['boolean'](False)) - - def test_template_test_with_template(self): - bp = flask.Blueprint('bp', __name__) - @bp.app_template_test() - def boolean(value): - return isinstance(value, bool) - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - @app.route('/') - def index(): - return flask.render_template('template_test.html', value=False) - rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) - - def test_template_test_after_route_with_template(self): - app = flask.Flask(__name__) - @app.route('/') - def index(): - return flask.render_template('template_test.html', value=False) - bp = flask.Blueprint('bp', __name__) - @bp.app_template_test() - def boolean(value): - return isinstance(value, bool) - app.register_blueprint(bp, url_prefix='/py') - rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) - - def test_add_template_test_with_template(self): - bp = flask.Blueprint('bp', __name__) - def boolean(value): - return isinstance(value, bool) - bp.add_app_template_test(boolean) - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - @app.route('/') - def index(): - return flask.render_template('template_test.html', value=False) - rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) - - def test_template_test_with_name_and_template(self): - bp = flask.Blueprint('bp', __name__) - @bp.app_template_test('boolean') - def is_boolean(value): - return isinstance(value, bool) - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - @app.route('/') - def index(): - return flask.render_template('template_test.html', value=False) - rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) - - def test_add_template_test_with_name_and_template(self): - bp = flask.Blueprint('bp', __name__) - def is_boolean(value): - return isinstance(value, bool) - bp.add_app_template_test(is_boolean, 'boolean') - app = flask.Flask(__name__) - app.register_blueprint(bp, url_prefix='/py') - @app.route('/') - def index(): - return flask.render_template('template_test.html', value=False) - rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(BlueprintTestCase)) - return suite diff --git a/flask/testsuite/config.py b/flask/testsuite/config.py deleted file mode 100644 index 4772fa77..00000000 --- a/flask/testsuite/config.py +++ /dev/null @@ -1,384 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.config - ~~~~~~~~~~~~~~~~~~~~~~ - - Configuration and instances. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -import os -import sys -import flask -import pkgutil -import unittest -from contextlib import contextmanager -from flask.testsuite import FlaskTestCase -from flask._compat import PY2 - - -# config keys used for the ConfigTestCase -TEST_KEY = 'foo' -SECRET_KEY = 'devkey' - - -class ConfigTestCase(FlaskTestCase): - - def common_object_test(self, app): - self.assert_equal(app.secret_key, 'devkey') - self.assert_equal(app.config['TEST_KEY'], 'foo') - self.assert_not_in('ConfigTestCase', app.config) - - def test_config_from_file(self): - app = flask.Flask(__name__) - app.config.from_pyfile(__file__.rsplit('.', 1)[0] + '.py') - self.common_object_test(app) - - def test_config_from_object(self): - app = flask.Flask(__name__) - app.config.from_object(__name__) - self.common_object_test(app) - - def test_config_from_json(self): - app = flask.Flask(__name__) - current_dir = os.path.dirname(os.path.abspath(__file__)) - app.config.from_json(os.path.join(current_dir, 'static', 'config.json')) - self.common_object_test(app) - - def test_config_from_mapping(self): - app = flask.Flask(__name__) - app.config.from_mapping({ - 'SECRET_KEY': 'devkey', - 'TEST_KEY': 'foo' - }) - self.common_object_test(app) - - app = flask.Flask(__name__) - app.config.from_mapping([ - ('SECRET_KEY', 'devkey'), - ('TEST_KEY', 'foo') - ]) - self.common_object_test(app) - - app = flask.Flask(__name__) - app.config.from_mapping( - SECRET_KEY='devkey', - TEST_KEY='foo' - ) - self.common_object_test(app) - - app = flask.Flask(__name__) - with self.assert_raises(TypeError): - app.config.from_mapping( - {}, {} - ) - - def test_config_from_class(self): - class Base(object): - TEST_KEY = 'foo' - class Test(Base): - SECRET_KEY = 'devkey' - app = flask.Flask(__name__) - app.config.from_object(Test) - self.common_object_test(app) - - def test_config_from_envvar(self): - env = os.environ - try: - os.environ = {} - app = flask.Flask(__name__) - try: - app.config.from_envvar('FOO_SETTINGS') - except RuntimeError as e: - self.assert_true("'FOO_SETTINGS' is not set" in str(e)) - else: - self.assert_true(0, 'expected exception') - self.assert_false(app.config.from_envvar('FOO_SETTINGS', silent=True)) - - os.environ = {'FOO_SETTINGS': __file__.rsplit('.', 1)[0] + '.py'} - self.assert_true(app.config.from_envvar('FOO_SETTINGS')) - self.common_object_test(app) - finally: - os.environ = env - - def test_config_from_envvar_missing(self): - env = os.environ - try: - os.environ = {'FOO_SETTINGS': 'missing.cfg'} - try: - app = flask.Flask(__name__) - app.config.from_envvar('FOO_SETTINGS') - except IOError as e: - msg = str(e) - self.assert_true(msg.startswith('[Errno 2] Unable to load configuration ' - 'file (No such file or directory):')) - self.assert_true(msg.endswith("missing.cfg'")) - else: - self.fail('expected IOError') - self.assertFalse(app.config.from_envvar('FOO_SETTINGS', silent=True)) - finally: - os.environ = env - - def test_config_missing(self): - app = flask.Flask(__name__) - try: - app.config.from_pyfile('missing.cfg') - except IOError as e: - msg = str(e) - self.assert_true(msg.startswith('[Errno 2] Unable to load configuration ' - 'file (No such file or directory):')) - self.assert_true(msg.endswith("missing.cfg'")) - else: - self.assert_true(0, 'expected config') - self.assert_false(app.config.from_pyfile('missing.cfg', silent=True)) - - def test_config_missing_json(self): - app = flask.Flask(__name__) - try: - app.config.from_json('missing.json') - except IOError as e: - msg = str(e) - self.assert_true(msg.startswith('[Errno 2] Unable to load configuration ' - 'file (No such file or directory):')) - self.assert_true(msg.endswith("missing.json'")) - else: - self.assert_true(0, 'expected config') - self.assert_false(app.config.from_json('missing.json', silent=True)) - - def test_custom_config_class(self): - class Config(flask.Config): - pass - class Flask(flask.Flask): - config_class = Config - app = Flask(__name__) - self.assert_isinstance(app.config, Config) - app.config.from_object(__name__) - self.common_object_test(app) - - def test_session_lifetime(self): - app = flask.Flask(__name__) - app.config['PERMANENT_SESSION_LIFETIME'] = 42 - self.assert_equal(app.permanent_session_lifetime.seconds, 42) - - def test_get_namespace(self): - app = flask.Flask(__name__) - app.config['FOO_OPTION_1'] = 'foo option 1' - app.config['FOO_OPTION_2'] = 'foo option 2' - app.config['BAR_STUFF_1'] = 'bar stuff 1' - app.config['BAR_STUFF_2'] = 'bar stuff 2' - foo_options = app.config.get_namespace('FOO_') - self.assert_equal(2, len(foo_options)) - self.assert_equal('foo option 1', foo_options['option_1']) - self.assert_equal('foo option 2', foo_options['option_2']) - bar_options = app.config.get_namespace('BAR_', lowercase=False) - self.assert_equal(2, len(bar_options)) - self.assert_equal('bar stuff 1', bar_options['STUFF_1']) - self.assert_equal('bar stuff 2', bar_options['STUFF_2']) - - -class LimitedLoaderMockWrapper(object): - def __init__(self, loader): - self.loader = loader - - def __getattr__(self, name): - if name in ('archive', 'get_filename'): - msg = 'Mocking a loader which does not have `%s.`' % name - raise AttributeError(msg) - return getattr(self.loader, name) - - -@contextmanager -def patch_pkgutil_get_loader(wrapper_class=LimitedLoaderMockWrapper): - """Patch pkgutil.get_loader to give loader without get_filename or archive. - - This provides for tests where a system has custom loaders, e.g. Google App - Engine's HardenedModulesHook, which have neither the `get_filename` method - nor the `archive` attribute. - """ - old_get_loader = pkgutil.get_loader - def get_loader(*args, **kwargs): - return wrapper_class(old_get_loader(*args, **kwargs)) - try: - pkgutil.get_loader = get_loader - yield - finally: - pkgutil.get_loader = old_get_loader - - -class InstanceTestCase(FlaskTestCase): - - def test_explicit_instance_paths(self): - here = os.path.abspath(os.path.dirname(__file__)) - try: - flask.Flask(__name__, instance_path='instance') - except ValueError as e: - self.assert_in('must be absolute', str(e)) - else: - self.fail('Expected value error') - - app = flask.Flask(__name__, instance_path=here) - self.assert_equal(app.instance_path, here) - - def test_main_module_paths(self): - # Test an app with '__main__' as the import name, uses cwd. - from main_app import app - here = os.path.abspath(os.getcwd()) - self.assert_equal(app.instance_path, os.path.join(here, 'instance')) - if 'main_app' in sys.modules: - del sys.modules['main_app'] - - def test_uninstalled_module_paths(self): - from config_module_app import app - here = os.path.abspath(os.path.dirname(__file__)) - self.assert_equal(app.instance_path, os.path.join(here, 'test_apps', 'instance')) - - def test_uninstalled_package_paths(self): - from config_package_app import app - here = os.path.abspath(os.path.dirname(__file__)) - self.assert_equal(app.instance_path, os.path.join(here, 'test_apps', 'instance')) - - def test_installed_module_paths(self): - here = os.path.abspath(os.path.dirname(__file__)) - expected_prefix = os.path.join(here, 'test_apps') - real_prefix, sys.prefix = sys.prefix, expected_prefix - site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages') - sys.path.append(site_packages) - try: - import site_app - self.assert_equal(site_app.app.instance_path, - os.path.join(expected_prefix, 'var', - 'site_app-instance')) - finally: - sys.prefix = real_prefix - sys.path.remove(site_packages) - if 'site_app' in sys.modules: - del sys.modules['site_app'] - - def test_installed_module_paths_with_limited_loader(self): - here = os.path.abspath(os.path.dirname(__file__)) - expected_prefix = os.path.join(here, 'test_apps') - real_prefix, sys.prefix = sys.prefix, expected_prefix - site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages') - sys.path.append(site_packages) - with patch_pkgutil_get_loader(): - try: - import site_app - self.assert_equal(site_app.app.instance_path, - os.path.join(expected_prefix, 'var', - 'site_app-instance')) - finally: - sys.prefix = real_prefix - sys.path.remove(site_packages) - if 'site_app' in sys.modules: - del sys.modules['site_app'] - - def test_installed_package_paths(self): - here = os.path.abspath(os.path.dirname(__file__)) - expected_prefix = os.path.join(here, 'test_apps') - real_prefix, sys.prefix = sys.prefix, expected_prefix - installed_path = os.path.join(expected_prefix, 'path') - sys.path.append(installed_path) - try: - import installed_package - self.assert_equal(installed_package.app.instance_path, - os.path.join(expected_prefix, 'var', - 'installed_package-instance')) - finally: - sys.prefix = real_prefix - sys.path.remove(installed_path) - if 'installed_package' in sys.modules: - del sys.modules['installed_package'] - - def test_installed_package_paths_with_limited_loader(self): - here = os.path.abspath(os.path.dirname(__file__)) - expected_prefix = os.path.join(here, 'test_apps') - real_prefix, sys.prefix = sys.prefix, expected_prefix - installed_path = os.path.join(expected_prefix, 'path') - sys.path.append(installed_path) - with patch_pkgutil_get_loader(): - try: - import installed_package - self.assert_equal(installed_package.app.instance_path, - os.path.join(expected_prefix, 'var', - 'installed_package-instance')) - finally: - sys.prefix = real_prefix - sys.path.remove(installed_path) - if 'installed_package' in sys.modules: - del sys.modules['installed_package'] - - def test_prefix_package_paths(self): - here = os.path.abspath(os.path.dirname(__file__)) - expected_prefix = os.path.join(here, 'test_apps') - real_prefix, sys.prefix = sys.prefix, expected_prefix - site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages') - sys.path.append(site_packages) - try: - import site_package - self.assert_equal(site_package.app.instance_path, - os.path.join(expected_prefix, 'var', - 'site_package-instance')) - finally: - sys.prefix = real_prefix - sys.path.remove(site_packages) - if 'site_package' in sys.modules: - del sys.modules['site_package'] - - def test_prefix_package_paths_with_limited_loader(self): - here = os.path.abspath(os.path.dirname(__file__)) - expected_prefix = os.path.join(here, 'test_apps') - real_prefix, sys.prefix = sys.prefix, expected_prefix - site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages') - sys.path.append(site_packages) - with patch_pkgutil_get_loader(): - try: - import site_package - self.assert_equal(site_package.app.instance_path, - os.path.join(expected_prefix, 'var', - 'site_package-instance')) - finally: - sys.prefix = real_prefix - sys.path.remove(site_packages) - if 'site_package' in sys.modules: - del sys.modules['site_package'] - - def test_egg_installed_paths(self): - here = os.path.abspath(os.path.dirname(__file__)) - expected_prefix = os.path.join(here, 'test_apps') - real_prefix, sys.prefix = sys.prefix, expected_prefix - site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages') - egg_path = os.path.join(site_packages, 'SiteEgg.egg') - sys.path.append(site_packages) - sys.path.append(egg_path) - try: - import site_egg # in SiteEgg.egg - self.assert_equal(site_egg.app.instance_path, - os.path.join(expected_prefix, 'var', - 'site_egg-instance')) - finally: - sys.prefix = real_prefix - sys.path.remove(site_packages) - sys.path.remove(egg_path) - if 'site_egg' in sys.modules: - del sys.modules['site_egg'] - - if PY2: - def test_meta_path_loader_without_is_package(self): - class Loader(object): - def find_module(self, name): - return self - sys.meta_path.append(Loader()) - try: - with self.assert_raises(AttributeError): - flask.Flask(__name__) - finally: - sys.meta_path.pop() - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(ConfigTestCase)) - suite.addTest(unittest.makeSuite(InstanceTestCase)) - return suite diff --git a/flask/testsuite/deprecations.py b/flask/testsuite/deprecations.py deleted file mode 100644 index 2d77925e..00000000 --- a/flask/testsuite/deprecations.py +++ /dev/null @@ -1,24 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.deprecations - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - Tests deprecation support. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -import flask -import unittest -from flask.testsuite import FlaskTestCase, catch_warnings - - -class DeprecationsTestCase(FlaskTestCase): - """not used currently""" - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(DeprecationsTestCase)) - return suite diff --git a/flask/testsuite/examples.py b/flask/testsuite/examples.py deleted file mode 100644 index 1ea3e6cc..00000000 --- a/flask/testsuite/examples.py +++ /dev/null @@ -1,38 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.examples - ~~~~~~~~~~~~~~~~~~~~~~~~ - - Tests the examples. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" -import os -import unittest -from flask.testsuite import add_to_path - - -def setup_path(): - example_path = os.path.join(os.path.dirname(__file__), - os.pardir, os.pardir, 'examples') - add_to_path(os.path.join(example_path, 'flaskr')) - add_to_path(os.path.join(example_path, 'minitwit')) - - -def suite(): - setup_path() - suite = unittest.TestSuite() - try: - from minitwit_tests import MiniTwitTestCase - except ImportError: - pass - else: - suite.addTest(unittest.makeSuite(MiniTwitTestCase)) - try: - from flaskr_tests import FlaskrTestCase - except ImportError: - pass - else: - suite.addTest(unittest.makeSuite(FlaskrTestCase)) - return suite diff --git a/flask/testsuite/ext.py b/flask/testsuite/ext.py deleted file mode 100644 index 5ec6ec63..00000000 --- a/flask/testsuite/ext.py +++ /dev/null @@ -1,136 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.ext - ~~~~~~~~~~~~~~~~~~~ - - Tests the extension import thing. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -import sys -import unittest -try: - from imp import reload as reload_module -except ImportError: - reload_module = reload -from flask.testsuite import FlaskTestCase -from flask._compat import PY2 - -class ExtImportHookTestCase(FlaskTestCase): - - def setup(self): - # we clear this out for various reasons. The most important one is - # that a real flaskext could be in there which would disable our - # fake package. Secondly we want to make sure that the flaskext - # import hook does not break on reloading. - for entry, value in list(sys.modules.items()): - if (entry.startswith('flask.ext.') or - entry.startswith('flask_') or - entry.startswith('flaskext.') or - entry == 'flaskext') and value is not None: - sys.modules.pop(entry, None) - from flask import ext - reload_module(ext) - - # reloading must not add more hooks - import_hooks = 0 - for item in sys.meta_path: - cls = type(item) - if cls.__module__ == 'flask.exthook' and \ - cls.__name__ == 'ExtensionImporter': - import_hooks += 1 - self.assert_equal(import_hooks, 1) - - def teardown(self): - from flask import ext - for key in ext.__dict__: - self.assert_not_in('.', key) - - def test_flaskext_new_simple_import_normal(self): - from flask.ext.newext_simple import ext_id - self.assert_equal(ext_id, 'newext_simple') - - def test_flaskext_new_simple_import_module(self): - from flask.ext import newext_simple - self.assert_equal(newext_simple.ext_id, 'newext_simple') - self.assert_equal(newext_simple.__name__, 'flask_newext_simple') - - def test_flaskext_new_package_import_normal(self): - from flask.ext.newext_package import ext_id - self.assert_equal(ext_id, 'newext_package') - - def test_flaskext_new_package_import_module(self): - from flask.ext import newext_package - self.assert_equal(newext_package.ext_id, 'newext_package') - self.assert_equal(newext_package.__name__, 'flask_newext_package') - - def test_flaskext_new_package_import_submodule_function(self): - from flask.ext.newext_package.submodule import test_function - self.assert_equal(test_function(), 42) - - def test_flaskext_new_package_import_submodule(self): - from flask.ext.newext_package import submodule - self.assert_equal(submodule.__name__, 'flask_newext_package.submodule') - self.assert_equal(submodule.test_function(), 42) - - def test_flaskext_old_simple_import_normal(self): - from flask.ext.oldext_simple import ext_id - self.assert_equal(ext_id, 'oldext_simple') - - def test_flaskext_old_simple_import_module(self): - from flask.ext import oldext_simple - self.assert_equal(oldext_simple.ext_id, 'oldext_simple') - self.assert_equal(oldext_simple.__name__, 'flaskext.oldext_simple') - - def test_flaskext_old_package_import_normal(self): - from flask.ext.oldext_package import ext_id - self.assert_equal(ext_id, 'oldext_package') - - def test_flaskext_old_package_import_module(self): - from flask.ext import oldext_package - self.assert_equal(oldext_package.ext_id, 'oldext_package') - self.assert_equal(oldext_package.__name__, 'flaskext.oldext_package') - - def test_flaskext_old_package_import_submodule(self): - from flask.ext.oldext_package import submodule - self.assert_equal(submodule.__name__, 'flaskext.oldext_package.submodule') - self.assert_equal(submodule.test_function(), 42) - - def test_flaskext_old_package_import_submodule_function(self): - from flask.ext.oldext_package.submodule import test_function - self.assert_equal(test_function(), 42) - - def test_flaskext_broken_package_no_module_caching(self): - for x in range(2): - with self.assert_raises(ImportError): - import flask.ext.broken - - def test_no_error_swallowing(self): - try: - import flask.ext.broken - except ImportError: - exc_type, exc_value, tb = sys.exc_info() - self.assert_true(exc_type is ImportError) - if PY2: - message = 'No module named missing_module' - else: - message = 'No module named \'missing_module\'' - self.assert_equal(str(exc_value), message) - self.assert_true(tb.tb_frame.f_globals is globals()) - - # reraise() adds a second frame so we need to skip that one too. - # On PY3 we even have another one :( - next = tb.tb_next.tb_next - if not PY2: - next = next.tb_next - - import os.path - self.assert_in(os.path.join('flask_broken', '__init__.py'), next.tb_frame.f_code.co_filename) - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(ExtImportHookTestCase)) - return suite diff --git a/flask/testsuite/regression.py b/flask/testsuite/regression.py deleted file mode 100644 index ad06aa59..00000000 --- a/flask/testsuite/regression.py +++ /dev/null @@ -1,116 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.regression - ~~~~~~~~~~~~~~~~~~~~~~~~~~ - - Tests regressions. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -import os -import gc -import sys -import flask -import threading -import unittest -from werkzeug.exceptions import NotFound -from flask.testsuite import FlaskTestCase - - -_gc_lock = threading.Lock() - - -class _NoLeakAsserter(object): - - def __init__(self, testcase): - self.testcase = testcase - - def __enter__(self): - gc.disable() - _gc_lock.acquire() - loc = flask._request_ctx_stack._local - - # Force Python to track this dictionary at all times. - # This is necessary since Python only starts tracking - # dicts if they contain mutable objects. It's a horrible, - # horrible hack but makes this kinda testable. - loc.__storage__['FOOO'] = [1, 2, 3] - - gc.collect() - self.old_objects = len(gc.get_objects()) - - def __exit__(self, exc_type, exc_value, tb): - if not hasattr(sys, 'getrefcount'): - gc.collect() - new_objects = len(gc.get_objects()) - if new_objects > self.old_objects: - self.testcase.fail('Example code leaked') - _gc_lock.release() - gc.enable() - - -class MemoryTestCase(FlaskTestCase): - - def assert_no_leak(self): - return _NoLeakAsserter(self) - - def test_memory_consumption(self): - app = flask.Flask(__name__) - @app.route('/') - def index(): - return flask.render_template('simple_template.html', whiskey=42) - - def fire(): - with app.test_client() as c: - rv = c.get('/') - self.assert_equal(rv.status_code, 200) - self.assert_equal(rv.data, b'

42

') - - # Trigger caches - fire() - - # This test only works on CPython 2.7. - if sys.version_info >= (2, 7) and \ - not hasattr(sys, 'pypy_translation_info'): - with self.assert_no_leak(): - for x in range(10): - fire() - - def test_safe_join_toplevel_pardir(self): - from flask.helpers import safe_join - with self.assert_raises(NotFound): - safe_join('/foo', '..') - - -class ExceptionTestCase(FlaskTestCase): - - def test_aborting(self): - class Foo(Exception): - whatever = 42 - app = flask.Flask(__name__) - app.testing = True - @app.errorhandler(Foo) - def handle_foo(e): - return str(e.whatever) - @app.route('/') - def index(): - raise flask.abort(flask.redirect(flask.url_for('test'))) - @app.route('/test') - def test(): - raise Foo() - - with app.test_client() as c: - rv = c.get('/') - self.assertEqual(rv.headers['Location'], 'http://localhost/test') - rv = c.get('/test') - self.assertEqual(rv.data, b'42') - - -def suite(): - suite = unittest.TestSuite() - if os.environ.get('RUN_FLASK_MEMORY_TESTS') == '1': - suite.addTest(unittest.makeSuite(MemoryTestCase)) - suite.addTest(unittest.makeSuite(ExceptionTestCase)) - return suite diff --git a/flask/testsuite/reqctx.py b/flask/testsuite/reqctx.py deleted file mode 100644 index c8954824..00000000 --- a/flask/testsuite/reqctx.py +++ /dev/null @@ -1,201 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.reqctx - ~~~~~~~~~~~~~~~~~~~~~~ - - Tests the request context. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -import flask -import unittest -try: - from greenlet import greenlet -except ImportError: - greenlet = None -from flask.testsuite import FlaskTestCase - - -class RequestContextTestCase(FlaskTestCase): - - def test_teardown_on_pop(self): - buffer = [] - app = flask.Flask(__name__) - @app.teardown_request - def end_of_request(exception): - buffer.append(exception) - - ctx = app.test_request_context() - ctx.push() - self.assert_equal(buffer, []) - ctx.pop() - self.assert_equal(buffer, [None]) - - def test_teardown_with_previous_exception(self): - buffer = [] - app = flask.Flask(__name__) - @app.teardown_request - def end_of_request(exception): - buffer.append(exception) - - try: - raise Exception('dummy') - except Exception: - pass - - with app.test_request_context(): - self.assert_equal(buffer, []) - self.assert_equal(buffer, [None]) - - def test_proper_test_request_context(self): - app = flask.Flask(__name__) - app.config.update( - SERVER_NAME='localhost.localdomain:5000' - ) - - @app.route('/') - def index(): - return None - - @app.route('/', subdomain='foo') - def sub(): - return None - - with app.test_request_context('/'): - self.assert_equal(flask.url_for('index', _external=True), 'http://localhost.localdomain:5000/') - - with app.test_request_context('/'): - self.assert_equal(flask.url_for('sub', _external=True), 'http://foo.localhost.localdomain:5000/') - - try: - with app.test_request_context('/', environ_overrides={'HTTP_HOST': 'localhost'}): - pass - except Exception as e: - self.assert_true(isinstance(e, ValueError)) - self.assert_equal(str(e), "the server name provided " + - "('localhost.localdomain:5000') does not match the " + \ - "server name from the WSGI environment ('localhost')") - - try: - app.config.update(SERVER_NAME='localhost') - with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost'}): - pass - except ValueError as e: - raise ValueError( - "No ValueError exception should have been raised \"%s\"" % e - ) - - try: - app.config.update(SERVER_NAME='localhost:80') - with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost:80'}): - pass - except ValueError as e: - raise ValueError( - "No ValueError exception should have been raised \"%s\"" % e - ) - - def test_context_binding(self): - app = flask.Flask(__name__) - @app.route('/') - def index(): - return 'Hello %s!' % flask.request.args['name'] - @app.route('/meh') - def meh(): - return flask.request.url - - with app.test_request_context('/?name=World'): - self.assert_equal(index(), 'Hello World!') - with app.test_request_context('/meh'): - self.assert_equal(meh(), 'http://localhost/meh') - self.assert_true(flask._request_ctx_stack.top is None) - - def test_context_test(self): - app = flask.Flask(__name__) - self.assert_false(flask.request) - self.assert_false(flask.has_request_context()) - ctx = app.test_request_context() - ctx.push() - try: - self.assert_true(flask.request) - self.assert_true(flask.has_request_context()) - finally: - ctx.pop() - - def test_manual_context_binding(self): - app = flask.Flask(__name__) - @app.route('/') - def index(): - return 'Hello %s!' % flask.request.args['name'] - - ctx = app.test_request_context('/?name=World') - ctx.push() - self.assert_equal(index(), 'Hello World!') - ctx.pop() - try: - index() - except RuntimeError: - pass - else: - self.assert_true(0, 'expected runtime error') - - def test_greenlet_context_copying(self): - app = flask.Flask(__name__) - greenlets = [] - - @app.route('/') - def index(): - reqctx = flask._request_ctx_stack.top.copy() - def g(): - self.assert_false(flask.request) - self.assert_false(flask.current_app) - with reqctx: - self.assert_true(flask.request) - self.assert_equal(flask.current_app, app) - self.assert_equal(flask.request.path, '/') - self.assert_equal(flask.request.args['foo'], 'bar') - self.assert_false(flask.request) - return 42 - greenlets.append(greenlet(g)) - return 'Hello World!' - - rv = app.test_client().get('/?foo=bar') - self.assert_equal(rv.data, b'Hello World!') - - result = greenlets[0].run() - self.assert_equal(result, 42) - - def test_greenlet_context_copying_api(self): - app = flask.Flask(__name__) - greenlets = [] - - @app.route('/') - def index(): - reqctx = flask._request_ctx_stack.top.copy() - @flask.copy_current_request_context - def g(): - self.assert_true(flask.request) - self.assert_equal(flask.current_app, app) - self.assert_equal(flask.request.path, '/') - self.assert_equal(flask.request.args['foo'], 'bar') - return 42 - greenlets.append(greenlet(g)) - return 'Hello World!' - - rv = app.test_client().get('/?foo=bar') - self.assert_equal(rv.data, b'Hello World!') - - result = greenlets[0].run() - self.assert_equal(result, 42) - - # Disable test if we don't have greenlets available - if greenlet is None: - test_greenlet_context_copying = None - test_greenlet_context_copying_api = None - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(RequestContextTestCase)) - return suite diff --git a/flask/testsuite/signals.py b/flask/testsuite/signals.py deleted file mode 100644 index 93f1c7c8..00000000 --- a/flask/testsuite/signals.py +++ /dev/null @@ -1,153 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.signals - ~~~~~~~~~~~~~~~~~~~~~~~ - - Signalling. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -import flask -import unittest -from flask.testsuite import FlaskTestCase - - -class SignalsTestCase(FlaskTestCase): - - def test_template_rendered(self): - app = flask.Flask(__name__) - - @app.route('/') - def index(): - return flask.render_template('simple_template.html', whiskey=42) - - recorded = [] - def record(sender, template, context): - recorded.append((template, context)) - - flask.template_rendered.connect(record, app) - try: - app.test_client().get('/') - self.assert_equal(len(recorded), 1) - template, context = recorded[0] - self.assert_equal(template.name, 'simple_template.html') - self.assert_equal(context['whiskey'], 42) - finally: - flask.template_rendered.disconnect(record, app) - - def test_request_signals(self): - app = flask.Flask(__name__) - calls = [] - - def before_request_signal(sender): - calls.append('before-signal') - - def after_request_signal(sender, response): - self.assert_equal(response.data, b'stuff') - calls.append('after-signal') - - @app.before_request - def before_request_handler(): - calls.append('before-handler') - - @app.after_request - def after_request_handler(response): - calls.append('after-handler') - response.data = 'stuff' - return response - - @app.route('/') - def index(): - calls.append('handler') - return 'ignored anyway' - - flask.request_started.connect(before_request_signal, app) - flask.request_finished.connect(after_request_signal, app) - - try: - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'stuff') - - self.assert_equal(calls, ['before-signal', 'before-handler', - 'handler', 'after-handler', - 'after-signal']) - finally: - flask.request_started.disconnect(before_request_signal, app) - flask.request_finished.disconnect(after_request_signal, app) - - def test_request_exception_signal(self): - app = flask.Flask(__name__) - recorded = [] - - @app.route('/') - def index(): - 1 // 0 - - def record(sender, exception): - recorded.append(exception) - - flask.got_request_exception.connect(record, app) - try: - self.assert_equal(app.test_client().get('/').status_code, 500) - self.assert_equal(len(recorded), 1) - self.assert_true(isinstance(recorded[0], ZeroDivisionError)) - finally: - flask.got_request_exception.disconnect(record, app) - - def test_appcontext_signals(self): - app = flask.Flask(__name__) - recorded = [] - def record_push(sender, **kwargs): - recorded.append('push') - def record_pop(sender, **kwargs): - recorded.append('pop') - - @app.route('/') - def index(): - return 'Hello' - - flask.appcontext_pushed.connect(record_push, app) - flask.appcontext_popped.connect(record_pop, app) - try: - with app.test_client() as c: - rv = c.get('/') - self.assert_equal(rv.data, b'Hello') - self.assert_equal(recorded, ['push']) - self.assert_equal(recorded, ['push', 'pop']) - finally: - flask.appcontext_pushed.disconnect(record_push, app) - flask.appcontext_popped.disconnect(record_pop, app) - - def test_flash_signal(self): - app = flask.Flask(__name__) - app.config['SECRET_KEY'] = 'secret' - - @app.route('/') - def index(): - flask.flash('This is a flash message', category='notice') - return flask.redirect('/other') - - recorded = [] - def record(sender, message, category): - recorded.append((message, category)) - - flask.message_flashed.connect(record, app) - try: - client = app.test_client() - with client.session_transaction(): - client.get('/') - self.assert_equal(len(recorded), 1) - message, category = recorded[0] - self.assert_equal(message, 'This is a flash message') - self.assert_equal(category, 'notice') - finally: - flask.message_flashed.disconnect(record, app) - - -def suite(): - suite = unittest.TestSuite() - if flask.signals_available: - suite.addTest(unittest.makeSuite(SignalsTestCase)) - return suite diff --git a/flask/testsuite/subclassing.py b/flask/testsuite/subclassing.py deleted file mode 100644 index 24c823ae..00000000 --- a/flask/testsuite/subclassing.py +++ /dev/null @@ -1,46 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.subclassing - ~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - Test that certain behavior of flask can be customized by - subclasses. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" -import flask -import unittest -from logging import StreamHandler -from flask.testsuite import FlaskTestCase -from flask._compat import StringIO - - -class FlaskSubclassingTestCase(FlaskTestCase): - - def test_suppressed_exception_logging(self): - class SuppressedFlask(flask.Flask): - def log_exception(self, exc_info): - pass - - out = StringIO() - app = SuppressedFlask(__name__) - app.logger_name = 'flask_tests/test_suppressed_exception_logging' - app.logger.addHandler(StreamHandler(out)) - - @app.route('/') - def index(): - 1 // 0 - - rv = app.test_client().get('/') - self.assert_equal(rv.status_code, 500) - self.assert_in(b'Internal Server Error', rv.data) - - err = out.getvalue() - self.assert_equal(err, '') - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(FlaskSubclassingTestCase)) - return suite diff --git a/flask/testsuite/templating.py b/flask/testsuite/templating.py deleted file mode 100644 index 435ace21..00000000 --- a/flask/testsuite/templating.py +++ /dev/null @@ -1,352 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.templating - ~~~~~~~~~~~~~~~~~~~~~~~~~~ - - Template functionality - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -import flask -import unittest -import logging -from jinja2 import TemplateNotFound - -from flask.testsuite import FlaskTestCase - - -class TemplatingTestCase(FlaskTestCase): - - def test_context_processing(self): - app = flask.Flask(__name__) - @app.context_processor - def context_processor(): - return {'injected_value': 42} - @app.route('/') - def index(): - return flask.render_template('context_template.html', value=23) - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'

23|42') - - def test_original_win(self): - app = flask.Flask(__name__) - @app.route('/') - def index(): - return flask.render_template_string('{{ config }}', config=42) - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'42') - - def test_request_less_rendering(self): - app = flask.Flask(__name__) - app.config['WORLD_NAME'] = 'Special World' - @app.context_processor - def context_processor(): - return dict(foo=42) - - with app.app_context(): - rv = flask.render_template_string('Hello {{ config.WORLD_NAME }} ' - '{{ foo }}') - self.assert_equal(rv, 'Hello Special World 42') - - def test_standard_context(self): - app = flask.Flask(__name__) - app.secret_key = 'development key' - @app.route('/') - def index(): - flask.g.foo = 23 - flask.session['test'] = 'aha' - return flask.render_template_string(''' - {{ request.args.foo }} - {{ g.foo }} - {{ config.DEBUG }} - {{ session.test }} - ''') - rv = app.test_client().get('/?foo=42') - self.assert_equal(rv.data.split(), [b'42', b'23', b'False', b'aha']) - - def test_escaping(self): - text = '

Hello World!' - app = flask.Flask(__name__) - @app.route('/') - def index(): - return flask.render_template('escaping_template.html', text=text, - html=flask.Markup(text)) - lines = app.test_client().get('/').data.splitlines() - self.assert_equal(lines, [ - b'<p>Hello World!', - b'

Hello World!', - b'

Hello World!', - b'

Hello World!', - b'<p>Hello World!', - b'

Hello World!' - ]) - - def test_no_escaping(self): - app = flask.Flask(__name__) - with app.test_request_context(): - self.assert_equal(flask.render_template_string('{{ foo }}', - foo=''), '') - self.assert_equal(flask.render_template('mail.txt', foo=''), - ' Mail') - - def test_macros(self): - app = flask.Flask(__name__) - with app.test_request_context(): - macro = flask.get_template_attribute('_macro.html', 'hello') - self.assert_equal(macro('World'), 'Hello World!') - - def test_template_filter(self): - app = flask.Flask(__name__) - @app.template_filter() - def my_reverse(s): - return s[::-1] - self.assert_in('my_reverse', app.jinja_env.filters.keys()) - self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse) - self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba') - - def test_add_template_filter(self): - app = flask.Flask(__name__) - def my_reverse(s): - return s[::-1] - app.add_template_filter(my_reverse) - self.assert_in('my_reverse', app.jinja_env.filters.keys()) - self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse) - self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba') - - def test_template_filter_with_name(self): - app = flask.Flask(__name__) - @app.template_filter('strrev') - def my_reverse(s): - return s[::-1] - self.assert_in('strrev', app.jinja_env.filters.keys()) - self.assert_equal(app.jinja_env.filters['strrev'], my_reverse) - self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba') - - def test_add_template_filter_with_name(self): - app = flask.Flask(__name__) - def my_reverse(s): - return s[::-1] - app.add_template_filter(my_reverse, 'strrev') - self.assert_in('strrev', app.jinja_env.filters.keys()) - self.assert_equal(app.jinja_env.filters['strrev'], my_reverse) - self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba') - - def test_template_filter_with_template(self): - app = flask.Flask(__name__) - @app.template_filter() - def super_reverse(s): - return s[::-1] - @app.route('/') - def index(): - return flask.render_template('template_filter.html', value='abcd') - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') - - def test_add_template_filter_with_template(self): - app = flask.Flask(__name__) - def super_reverse(s): - return s[::-1] - app.add_template_filter(super_reverse) - @app.route('/') - def index(): - return flask.render_template('template_filter.html', value='abcd') - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') - - def test_template_filter_with_name_and_template(self): - app = flask.Flask(__name__) - @app.template_filter('super_reverse') - def my_reverse(s): - return s[::-1] - @app.route('/') - def index(): - return flask.render_template('template_filter.html', value='abcd') - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') - - def test_add_template_filter_with_name_and_template(self): - app = flask.Flask(__name__) - def my_reverse(s): - return s[::-1] - app.add_template_filter(my_reverse, 'super_reverse') - @app.route('/') - def index(): - return flask.render_template('template_filter.html', value='abcd') - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') - - def test_template_test(self): - app = flask.Flask(__name__) - @app.template_test() - def boolean(value): - return isinstance(value, bool) - self.assert_in('boolean', app.jinja_env.tests.keys()) - self.assert_equal(app.jinja_env.tests['boolean'], boolean) - self.assert_true(app.jinja_env.tests['boolean'](False)) - - def test_add_template_test(self): - app = flask.Flask(__name__) - def boolean(value): - return isinstance(value, bool) - app.add_template_test(boolean) - self.assert_in('boolean', app.jinja_env.tests.keys()) - self.assert_equal(app.jinja_env.tests['boolean'], boolean) - self.assert_true(app.jinja_env.tests['boolean'](False)) - - def test_template_test_with_name(self): - app = flask.Flask(__name__) - @app.template_test('boolean') - def is_boolean(value): - return isinstance(value, bool) - self.assert_in('boolean', app.jinja_env.tests.keys()) - self.assert_equal(app.jinja_env.tests['boolean'], is_boolean) - self.assert_true(app.jinja_env.tests['boolean'](False)) - - def test_add_template_test_with_name(self): - app = flask.Flask(__name__) - def is_boolean(value): - return isinstance(value, bool) - app.add_template_test(is_boolean, 'boolean') - self.assert_in('boolean', app.jinja_env.tests.keys()) - self.assert_equal(app.jinja_env.tests['boolean'], is_boolean) - self.assert_true(app.jinja_env.tests['boolean'](False)) - - def test_template_test_with_template(self): - app = flask.Flask(__name__) - @app.template_test() - def boolean(value): - return isinstance(value, bool) - @app.route('/') - def index(): - return flask.render_template('template_test.html', value=False) - rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) - - def test_add_template_test_with_template(self): - app = flask.Flask(__name__) - def boolean(value): - return isinstance(value, bool) - app.add_template_test(boolean) - @app.route('/') - def index(): - return flask.render_template('template_test.html', value=False) - rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) - - def test_template_test_with_name_and_template(self): - app = flask.Flask(__name__) - @app.template_test('boolean') - def is_boolean(value): - return isinstance(value, bool) - @app.route('/') - def index(): - return flask.render_template('template_test.html', value=False) - rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) - - def test_add_template_test_with_name_and_template(self): - app = flask.Flask(__name__) - def is_boolean(value): - return isinstance(value, bool) - app.add_template_test(is_boolean, 'boolean') - @app.route('/') - def index(): - return flask.render_template('template_test.html', value=False) - rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) - - def test_add_template_global(self): - app = flask.Flask(__name__) - @app.template_global() - def get_stuff(): - return 42 - self.assert_in('get_stuff', app.jinja_env.globals.keys()) - self.assert_equal(app.jinja_env.globals['get_stuff'], get_stuff) - self.assert_true(app.jinja_env.globals['get_stuff'](), 42) - with app.app_context(): - rv = flask.render_template_string('{{ get_stuff() }}') - self.assert_equal(rv, '42') - - def test_custom_template_loader(self): - class MyFlask(flask.Flask): - def create_global_jinja_loader(self): - from jinja2 import DictLoader - return DictLoader({'index.html': 'Hello Custom World!'}) - app = MyFlask(__name__) - @app.route('/') - def index(): - return flask.render_template('index.html') - c = app.test_client() - rv = c.get('/') - self.assert_equal(rv.data, b'Hello Custom World!') - - def test_iterable_loader(self): - app = flask.Flask(__name__) - @app.context_processor - def context_processor(): - return {'whiskey': 'Jameson'} - @app.route('/') - def index(): - return flask.render_template( - ['no_template.xml', # should skip this one - 'simple_template.html', # should render this - 'context_template.html'], - value=23) - - rv = app.test_client().get('/') - self.assert_equal(rv.data, b'

Jameson

') - - def test_templates_auto_reload(self): - app = flask.Flask(__name__) - self.assert_true(app.config['TEMPLATES_AUTO_RELOAD']) - self.assert_true(app.jinja_env.auto_reload) - app = flask.Flask(__name__) - app.config['TEMPLATES_AUTO_RELOAD'] = False - self.assert_false(app.jinja_env.auto_reload) - - def test_template_loader_debugging(self): - from blueprintapp import app - - called = [] - class _TestHandler(logging.Handler): - def handle(x, record): - called.append(True) - text = str(record.msg) - self.assert_('1: trying loader of application ' - '"blueprintapp"' in text) - self.assert_('2: trying loader of blueprint "admin" ' - '(blueprintapp.apps.admin)' in text) - self.assert_('trying loader of blueprint "frontend" ' - '(blueprintapp.apps.frontend)' in text) - self.assert_('Error: the template could not be found' in text) - self.assert_('looked up from an endpoint that belongs to ' - 'the blueprint "frontend"' in text) - self.assert_( - 'See http://flask.pocoo.org/docs/blueprints/#templates' in text) - - with app.test_client() as c: - try: - old_load_setting = app.config['EXPLAIN_TEMPLATE_LOADING'] - old_handlers = app.logger.handlers[:] - app.logger.handlers = [_TestHandler()] - app.config['EXPLAIN_TEMPLATE_LOADING'] = True - - try: - c.get('/missing') - except TemplateNotFound as e: - self.assert_('missing_template.html' in str(e)) - else: - self.fail('Expected template not found exception.') - finally: - app.logger.handlers[:] = old_handlers - app.config['EXPLAIN_TEMPLATE_LOADING'] = old_load_setting - - self.assert_equal(len(called), 1) - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(TemplatingTestCase)) - return suite diff --git a/flask/testsuite/test_apps/config_module_app.py b/flask/testsuite/test_apps/config_module_app.py deleted file mode 100644 index 380d46bf..00000000 --- a/flask/testsuite/test_apps/config_module_app.py +++ /dev/null @@ -1,4 +0,0 @@ -import os -import flask -here = os.path.abspath(os.path.dirname(__file__)) -app = flask.Flask(__name__) diff --git a/flask/testsuite/test_apps/config_package_app/__init__.py b/flask/testsuite/test_apps/config_package_app/__init__.py deleted file mode 100644 index 380d46bf..00000000 --- a/flask/testsuite/test_apps/config_package_app/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -import os -import flask -here = os.path.abspath(os.path.dirname(__file__)) -app = flask.Flask(__name__) diff --git a/flask/testsuite/test_apps/flask_broken/__init__.py b/flask/testsuite/test_apps/flask_broken/__init__.py deleted file mode 100644 index c194c04f..00000000 --- a/flask/testsuite/test_apps/flask_broken/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -import flask.ext.broken.b -import missing_module diff --git a/flask/testsuite/test_apps/flask_broken/b.py b/flask/testsuite/test_apps/flask_broken/b.py deleted file mode 100644 index e69de29b..00000000 diff --git a/flask/testsuite/test_apps/flask_newext_package/__init__.py b/flask/testsuite/test_apps/flask_newext_package/__init__.py deleted file mode 100644 index 3fd13e17..00000000 --- a/flask/testsuite/test_apps/flask_newext_package/__init__.py +++ /dev/null @@ -1 +0,0 @@ -ext_id = 'newext_package' diff --git a/flask/testsuite/test_apps/flask_newext_package/submodule.py b/flask/testsuite/test_apps/flask_newext_package/submodule.py deleted file mode 100644 index 26ad56b7..00000000 --- a/flask/testsuite/test_apps/flask_newext_package/submodule.py +++ /dev/null @@ -1,2 +0,0 @@ -def test_function(): - return 42 diff --git a/flask/testsuite/test_apps/flask_newext_simple.py b/flask/testsuite/test_apps/flask_newext_simple.py deleted file mode 100644 index dc4a3628..00000000 --- a/flask/testsuite/test_apps/flask_newext_simple.py +++ /dev/null @@ -1 +0,0 @@ -ext_id = 'newext_simple' diff --git a/flask/testsuite/test_apps/flaskext/__init__.py b/flask/testsuite/test_apps/flaskext/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/flask/testsuite/test_apps/flaskext/oldext_package/__init__.py b/flask/testsuite/test_apps/flaskext/oldext_package/__init__.py deleted file mode 100644 index 7c462065..00000000 --- a/flask/testsuite/test_apps/flaskext/oldext_package/__init__.py +++ /dev/null @@ -1 +0,0 @@ -ext_id = 'oldext_package' diff --git a/flask/testsuite/test_apps/flaskext/oldext_package/submodule.py b/flask/testsuite/test_apps/flaskext/oldext_package/submodule.py deleted file mode 100644 index 26ad56b7..00000000 --- a/flask/testsuite/test_apps/flaskext/oldext_package/submodule.py +++ /dev/null @@ -1,2 +0,0 @@ -def test_function(): - return 42 diff --git a/flask/testsuite/test_apps/flaskext/oldext_simple.py b/flask/testsuite/test_apps/flaskext/oldext_simple.py deleted file mode 100644 index c6664a78..00000000 --- a/flask/testsuite/test_apps/flaskext/oldext_simple.py +++ /dev/null @@ -1 +0,0 @@ -ext_id = 'oldext_simple' diff --git a/flask/testsuite/test_apps/importerror.py b/flask/testsuite/test_apps/importerror.py deleted file mode 100644 index eb298b9b..00000000 --- a/flask/testsuite/test_apps/importerror.py +++ /dev/null @@ -1,2 +0,0 @@ -# NoImportsTestCase -raise NotImplementedError diff --git a/flask/testsuite/test_apps/lib/python2.5/site-packages/SiteEgg.egg b/flask/testsuite/test_apps/lib/python2.5/site-packages/SiteEgg.egg deleted file mode 100644 index d80abe93..00000000 Binary files a/flask/testsuite/test_apps/lib/python2.5/site-packages/SiteEgg.egg and /dev/null differ diff --git a/flask/testsuite/test_apps/lib/python2.5/site-packages/site_app.py b/flask/testsuite/test_apps/lib/python2.5/site-packages/site_app.py deleted file mode 100644 index 06271108..00000000 --- a/flask/testsuite/test_apps/lib/python2.5/site-packages/site_app.py +++ /dev/null @@ -1,3 +0,0 @@ -import flask - -app = flask.Flask(__name__) diff --git a/flask/testsuite/test_apps/lib/python2.5/site-packages/site_package/__init__.py b/flask/testsuite/test_apps/lib/python2.5/site-packages/site_package/__init__.py deleted file mode 100644 index 06271108..00000000 --- a/flask/testsuite/test_apps/lib/python2.5/site-packages/site_package/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -import flask - -app = flask.Flask(__name__) diff --git a/flask/testsuite/test_apps/main_app.py b/flask/testsuite/test_apps/main_app.py deleted file mode 100644 index a5f372c8..00000000 --- a/flask/testsuite/test_apps/main_app.py +++ /dev/null @@ -1,4 +0,0 @@ -import flask - -# Test Flask initialization with main module. -app = flask.Flask('__main__') diff --git a/flask/testsuite/test_apps/path/installed_package/__init__.py b/flask/testsuite/test_apps/path/installed_package/__init__.py deleted file mode 100644 index 06271108..00000000 --- a/flask/testsuite/test_apps/path/installed_package/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -import flask - -app = flask.Flask(__name__) diff --git a/flask/testsuite/testing.py b/flask/testsuite/testing.py deleted file mode 100644 index 25924839..00000000 --- a/flask/testsuite/testing.py +++ /dev/null @@ -1,257 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.testing - ~~~~~~~~~~~~~~~~~~~~~~~ - - Test client and more. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -import flask -import unittest -from flask.testsuite import FlaskTestCase -from flask._compat import text_type - - -class TestToolsTestCase(FlaskTestCase): - - def test_environ_defaults_from_config(self): - app = flask.Flask(__name__) - app.testing = True - app.config['SERVER_NAME'] = 'example.com:1234' - app.config['APPLICATION_ROOT'] = '/foo' - @app.route('/') - def index(): - return flask.request.url - - ctx = app.test_request_context() - self.assert_equal(ctx.request.url, 'http://example.com:1234/foo/') - with app.test_client() as c: - rv = c.get('/') - self.assert_equal(rv.data, b'http://example.com:1234/foo/') - - def test_environ_defaults(self): - app = flask.Flask(__name__) - app.testing = True - @app.route('/') - def index(): - return flask.request.url - - ctx = app.test_request_context() - self.assert_equal(ctx.request.url, 'http://localhost/') - with app.test_client() as c: - rv = c.get('/') - self.assert_equal(rv.data, b'http://localhost/') - - def test_redirect_keep_session(self): - app = flask.Flask(__name__) - app.secret_key = 'testing' - - @app.route('/', methods=['GET', 'POST']) - def index(): - if flask.request.method == 'POST': - return flask.redirect('/getsession') - flask.session['data'] = 'foo' - return 'index' - - @app.route('/getsession') - def get_session(): - return flask.session.get('data', '') - - with app.test_client() as c: - rv = c.get('/getsession') - assert rv.data == b'' - - rv = c.get('/') - assert rv.data == b'index' - assert flask.session.get('data') == 'foo' - rv = c.post('/', data={}, follow_redirects=True) - assert rv.data == b'foo' - - # This support requires a new Werkzeug version - if not hasattr(c, 'redirect_client'): - assert flask.session.get('data') == 'foo' - - rv = c.get('/getsession') - assert rv.data == b'foo' - - def test_session_transactions(self): - app = flask.Flask(__name__) - app.testing = True - app.secret_key = 'testing' - - @app.route('/') - def index(): - return text_type(flask.session['foo']) - - with app.test_client() as c: - with c.session_transaction() as sess: - self.assert_equal(len(sess), 0) - sess['foo'] = [42] - self.assert_equal(len(sess), 1) - rv = c.get('/') - self.assert_equal(rv.data, b'[42]') - with c.session_transaction() as sess: - self.assert_equal(len(sess), 1) - self.assert_equal(sess['foo'], [42]) - - def test_session_transactions_no_null_sessions(self): - app = flask.Flask(__name__) - app.testing = True - - with app.test_client() as c: - try: - with c.session_transaction() as sess: - pass - except RuntimeError as e: - self.assert_in('Session backend did not open a session', str(e)) - else: - self.fail('Expected runtime error') - - def test_session_transactions_keep_context(self): - app = flask.Flask(__name__) - app.testing = True - app.secret_key = 'testing' - - with app.test_client() as c: - rv = c.get('/') - req = flask.request._get_current_object() - self.assert_true(req is not None) - with c.session_transaction(): - self.assert_true(req is flask.request._get_current_object()) - - def test_session_transaction_needs_cookies(self): - app = flask.Flask(__name__) - app.testing = True - c = app.test_client(use_cookies=False) - try: - with c.session_transaction() as s: - pass - except RuntimeError as e: - self.assert_in('cookies', str(e)) - else: - self.fail('Expected runtime error') - - def test_test_client_context_binding(self): - app = flask.Flask(__name__) - app.config['LOGGER_HANDLER_POLICY'] = 'never' - @app.route('/') - def index(): - flask.g.value = 42 - return 'Hello World!' - - @app.route('/other') - def other(): - 1 // 0 - - with app.test_client() as c: - resp = c.get('/') - self.assert_equal(flask.g.value, 42) - self.assert_equal(resp.data, b'Hello World!') - self.assert_equal(resp.status_code, 200) - - resp = c.get('/other') - self.assert_false(hasattr(flask.g, 'value')) - self.assert_in(b'Internal Server Error', resp.data) - self.assert_equal(resp.status_code, 500) - flask.g.value = 23 - - try: - flask.g.value - except (AttributeError, RuntimeError): - pass - else: - raise AssertionError('some kind of exception expected') - - def test_reuse_client(self): - app = flask.Flask(__name__) - c = app.test_client() - - with c: - self.assert_equal(c.get('/').status_code, 404) - - with c: - self.assert_equal(c.get('/').status_code, 404) - - def test_test_client_calls_teardown_handlers(self): - app = flask.Flask(__name__) - called = [] - @app.teardown_request - def remember(error): - called.append(error) - - with app.test_client() as c: - self.assert_equal(called, []) - c.get('/') - self.assert_equal(called, []) - self.assert_equal(called, [None]) - - del called[:] - with app.test_client() as c: - self.assert_equal(called, []) - c.get('/') - self.assert_equal(called, []) - c.get('/') - self.assert_equal(called, [None]) - self.assert_equal(called, [None, None]) - - def test_full_url_request(self): - app = flask.Flask(__name__) - app.testing = True - - @app.route('/action', methods=['POST']) - def action(): - return 'x' - - with app.test_client() as c: - rv = c.post('http://domain.com/action?vodka=42', data={'gin': 43}) - self.assert_equal(rv.status_code, 200) - self.assert_true('gin' in flask.request.form) - self.assert_true('vodka' in flask.request.args) - - -class SubdomainTestCase(FlaskTestCase): - - def setUp(self): - self.app = flask.Flask(__name__) - self.app.config['SERVER_NAME'] = 'example.com' - self.client = self.app.test_client() - - self._ctx = self.app.test_request_context() - self._ctx.push() - - def tearDown(self): - if self._ctx is not None: - self._ctx.pop() - - def test_subdomain(self): - @self.app.route('/', subdomain='') - def view(company_id): - return company_id - - url = flask.url_for('view', company_id='xxx') - response = self.client.get(url) - - self.assert_equal(200, response.status_code) - self.assert_equal(b'xxx', response.data) - - - def test_nosubdomain(self): - @self.app.route('/') - def view(company_id): - return company_id - - url = flask.url_for('view', company_id='xxx') - response = self.client.get(url) - - self.assert_equal(200, response.status_code) - self.assert_equal(b'xxx', response.data) - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(TestToolsTestCase)) - suite.addTest(unittest.makeSuite(SubdomainTestCase)) - return suite diff --git a/flask/testsuite/views.py b/flask/testsuite/views.py deleted file mode 100644 index bd572b24..00000000 --- a/flask/testsuite/views.py +++ /dev/null @@ -1,169 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.testsuite.views - ~~~~~~~~~~~~~~~~~~~~~ - - Pluggable views. - - :copyright: (c) 2014 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" - -import flask -import flask.views -import unittest -from flask.testsuite import FlaskTestCase -from werkzeug.http import parse_set_header - -class ViewTestCase(FlaskTestCase): - - def common_test(self, app): - c = app.test_client() - - self.assert_equal(c.get('/').data, b'GET') - self.assert_equal(c.post('/').data, b'POST') - self.assert_equal(c.put('/').status_code, 405) - meths = parse_set_header(c.open('/', method='OPTIONS').headers['Allow']) - self.assert_equal(sorted(meths), ['GET', 'HEAD', 'OPTIONS', 'POST']) - - def test_basic_view(self): - app = flask.Flask(__name__) - - class Index(flask.views.View): - methods = ['GET', 'POST'] - def dispatch_request(self): - return flask.request.method - - app.add_url_rule('/', view_func=Index.as_view('index')) - self.common_test(app) - - def test_method_based_view(self): - app = flask.Flask(__name__) - - class Index(flask.views.MethodView): - def get(self): - return 'GET' - def post(self): - return 'POST' - - app.add_url_rule('/', view_func=Index.as_view('index')) - - self.common_test(app) - - def test_view_patching(self): - app = flask.Flask(__name__) - - class Index(flask.views.MethodView): - def get(self): - 1 // 0 - def post(self): - 1 // 0 - - class Other(Index): - def get(self): - return 'GET' - def post(self): - return 'POST' - - view = Index.as_view('index') - view.view_class = Other - app.add_url_rule('/', view_func=view) - self.common_test(app) - - def test_view_inheritance(self): - app = flask.Flask(__name__) - - class Index(flask.views.MethodView): - def get(self): - return 'GET' - def post(self): - return 'POST' - - class BetterIndex(Index): - def delete(self): - return 'DELETE' - - app.add_url_rule('/', view_func=BetterIndex.as_view('index')) - c = app.test_client() - - meths = parse_set_header(c.open('/', method='OPTIONS').headers['Allow']) - self.assert_equal(sorted(meths), ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST']) - - def test_view_decorators(self): - app = flask.Flask(__name__) - - def add_x_parachute(f): - def new_function(*args, **kwargs): - resp = flask.make_response(f(*args, **kwargs)) - resp.headers['X-Parachute'] = 'awesome' - return resp - return new_function - - class Index(flask.views.View): - decorators = [add_x_parachute] - def dispatch_request(self): - return 'Awesome' - - app.add_url_rule('/', view_func=Index.as_view('index')) - c = app.test_client() - rv = c.get('/') - self.assert_equal(rv.headers['X-Parachute'], 'awesome') - self.assert_equal(rv.data, b'Awesome') - - def test_implicit_head(self): - app = flask.Flask(__name__) - - class Index(flask.views.MethodView): - def get(self): - return flask.Response('Blub', headers={ - 'X-Method': flask.request.method - }) - - app.add_url_rule('/', view_func=Index.as_view('index')) - c = app.test_client() - rv = c.get('/') - self.assert_equal(rv.data, b'Blub') - self.assert_equal(rv.headers['X-Method'], 'GET') - rv = c.head('/') - self.assert_equal(rv.data, b'') - self.assert_equal(rv.headers['X-Method'], 'HEAD') - - def test_explicit_head(self): - app = flask.Flask(__name__) - - class Index(flask.views.MethodView): - def get(self): - return 'GET' - def head(self): - return flask.Response('', headers={'X-Method': 'HEAD'}) - - app.add_url_rule('/', view_func=Index.as_view('index')) - c = app.test_client() - rv = c.get('/') - self.assert_equal(rv.data, b'GET') - rv = c.head('/') - self.assert_equal(rv.data, b'') - self.assert_equal(rv.headers['X-Method'], 'HEAD') - - def test_endpoint_override(self): - app = flask.Flask(__name__) - app.debug = True - - class Index(flask.views.View): - methods = ['GET', 'POST'] - def dispatch_request(self): - return flask.request.method - - app.add_url_rule('/', view_func=Index.as_view('index')) - - with self.assert_raises(AssertionError): - app.add_url_rule('/', view_func=Index.as_view('index')) - - # But these tests should still pass. We just log a warning. - self.common_test(app) - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(ViewTestCase)) - return suite diff --git a/run-tests.py b/run-tests.py deleted file mode 100644 index c1345848..00000000 --- a/run-tests.py +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env python -import sys, os -sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) -from flask.testsuite import main -main() diff --git a/scripts/flaskext_test.py b/scripts/flaskext_tester.py similarity index 100% rename from scripts/flaskext_test.py rename to scripts/flaskext_tester.py diff --git a/setup.py b/setup.py index 3500ad8c..550b24a4 100644 --- a/setup.py +++ b/setup.py @@ -46,42 +46,6 @@ from __future__ import print_function from setuptools import Command, setup -class run_audit(Command): - """Audits source code using PyFlakes for following issues: - - Names which are used but not defined or used before they are defined. - - Names which are redefined without having been used. - """ - description = "Audit source code with PyFlakes" - user_options = [] - - def initialize_options(self): - pass - - def finalize_options(self): - pass - - def run(self): - import os - import sys - try: - import pyflakes.scripts.pyflakes as flakes - except ImportError: - print("Audit requires PyFlakes installed in your system.") - sys.exit(-1) - - warns = 0 - # Define top-level directories - dirs = ('flask', 'examples', 'scripts') - for dir in dirs: - for root, _, files in os.walk(dir): - for file in files: - if file != '__init__.py' and file.endswith('.py'): - warns += flakes.checkPath(os.path.join(root, file)) - if warns > 0: - print("Audit finished with total %d warnings." % warns) - else: - print("No problems found in sourcecode.") - setup( name='Flask', version='0.11-dev', @@ -92,7 +56,7 @@ setup( description='A microframework based on Werkzeug, Jinja2 ' 'and good intentions', long_description=__doc__, - packages=['flask', 'flask.ext', 'flask.testsuite'], + packages=['flask', 'flask.ext'], include_package_data=True, zip_safe=False, platforms='any', @@ -116,7 +80,5 @@ setup( entry_points=''' [console_scripts] flask=flask.cli:main - ''', - cmdclass={'audit': run_audit}, - test_suite='flask.testsuite.suite' + ''' ) diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..adbf3629 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,134 @@ +# -*- coding: utf-8 -*- +""" + tests.conftest + ~~~~~~~~~~~~~~ + + :copyright: (c) 2014 by the Flask Team, see AUTHORS for more details. + :license: BSD, see LICENSE for more details. +""" +import flask +import os +import sys +import pkgutil +import pytest +import textwrap + + +@pytest.fixture +def test_apps(monkeypatch): + monkeypatch.syspath_prepend( + os.path.abspath(os.path.join( + os.path.dirname(__file__), 'test_apps')) + ) + +@pytest.fixture(autouse=True) +def leak_detector(request): + def ensure_clean_request_context(): + # make sure we're not leaking a request context since we are + # testing flask internally in debug mode in a few cases + leaks = [] + while flask._request_ctx_stack.top is not None: + leaks.append(flask._request_ctx_stack.pop()) + assert leaks == [] + request.addfinalizer(ensure_clean_request_context) + + + +@pytest.fixture(params=(True, False)) +def limit_loader(request, monkeypatch): + """Patch pkgutil.get_loader to give loader without get_filename or archive. + + This provides for tests where a system has custom loaders, e.g. Google App + Engine's HardenedModulesHook, which have neither the `get_filename` method + nor the `archive` attribute. + + This fixture will run the testcase twice, once with and once without the + limitation/mock. + """ + if not request.param: + return + + class LimitedLoader(object): + def __init__(self, loader): + self.loader = loader + + def __getattr__(self, name): + if name in ('archive', 'get_filename'): + msg = 'Mocking a loader which does not have `%s.`' % name + raise AttributeError(msg) + return getattr(self.loader, name) + + old_get_loader = pkgutil.get_loader + + def get_loader(*args, **kwargs): + return LimitedLoader(old_get_loader(*args, **kwargs)) + monkeypatch.setattr(pkgutil, 'get_loader', get_loader) + + +@pytest.fixture +def modules_tmpdir(tmpdir, monkeypatch): + '''A tmpdir added to sys.path''' + rv = tmpdir.mkdir('modules_tmpdir') + monkeypatch.syspath_prepend(str(rv)) + return rv + + +@pytest.fixture +def modules_tmpdir_prefix(modules_tmpdir, monkeypatch): + monkeypatch.setattr(sys, 'prefix', str(modules_tmpdir)) + return modules_tmpdir + + +@pytest.fixture +def site_packages(modules_tmpdir, monkeypatch): + '''Create a fake site-packages''' + rv = modules_tmpdir \ + .mkdir('lib')\ + .mkdir('python{x[0]}.{x[1]}'.format(x=sys.version_info))\ + .mkdir('site-packages') + monkeypatch.syspath_prepend(str(rv)) + return rv + + +@pytest.fixture +def install_egg(modules_tmpdir, monkeypatch): + '''Generate egg from package name inside base and put the egg into + sys.path''' + def inner(name, base=modules_tmpdir): + if not isinstance(name, str): + raise ValueError(name) + base.join(name).ensure_dir() + base.join(name).join('__init__.py').ensure() + + egg_setup = base.join('setup.py') + egg_setup.write(textwrap.dedent(""" + from setuptools import setup + setup(name='{0}', + version='1.0', + packages=['site_egg'], + zip_safe=True) + """.format(name))) + + import subprocess + subprocess.check_call( + [sys.executable, 'setup.py', 'bdist_egg'], + cwd=str(modules_tmpdir) + ) + egg_path, = modules_tmpdir.join('dist/').listdir() + monkeypatch.syspath_prepend(str(egg_path)) + return egg_path + return inner + + +@pytest.fixture +def purge_module(request): + def inner(name): + request.addfinalizer(lambda: sys.modules.pop(name, None)) + return inner + + +@pytest.fixture +def catch_deprecation_warnings(): + import warnings + warnings.simplefilter('default', category=DeprecationWarning) + return lambda: warnings.catch_warnings(record=True) diff --git a/flask/testsuite/static/config.json b/tests/static/config.json similarity index 100% rename from flask/testsuite/static/config.json rename to tests/static/config.json diff --git a/flask/testsuite/static/index.html b/tests/static/index.html similarity index 100% rename from flask/testsuite/static/index.html rename to tests/static/index.html diff --git a/flask/testsuite/templates/_macro.html b/tests/templates/_macro.html similarity index 100% rename from flask/testsuite/templates/_macro.html rename to tests/templates/_macro.html diff --git a/flask/testsuite/templates/context_template.html b/tests/templates/context_template.html similarity index 100% rename from flask/testsuite/templates/context_template.html rename to tests/templates/context_template.html diff --git a/flask/testsuite/templates/escaping_template.html b/tests/templates/escaping_template.html similarity index 100% rename from flask/testsuite/templates/escaping_template.html rename to tests/templates/escaping_template.html diff --git a/flask/testsuite/templates/mail.txt b/tests/templates/mail.txt similarity index 100% rename from flask/testsuite/templates/mail.txt rename to tests/templates/mail.txt diff --git a/flask/testsuite/templates/nested/nested.txt b/tests/templates/nested/nested.txt similarity index 100% rename from flask/testsuite/templates/nested/nested.txt rename to tests/templates/nested/nested.txt diff --git a/flask/testsuite/templates/simple_template.html b/tests/templates/simple_template.html similarity index 100% rename from flask/testsuite/templates/simple_template.html rename to tests/templates/simple_template.html diff --git a/flask/testsuite/templates/template_filter.html b/tests/templates/template_filter.html similarity index 100% rename from flask/testsuite/templates/template_filter.html rename to tests/templates/template_filter.html diff --git a/flask/testsuite/templates/template_test.html b/tests/templates/template_test.html similarity index 100% rename from flask/testsuite/templates/template_test.html rename to tests/templates/template_test.html diff --git a/tests/test_appctx.py b/tests/test_appctx.py new file mode 100644 index 00000000..b4d77bfc --- /dev/null +++ b/tests/test_appctx.py @@ -0,0 +1,113 @@ +# -*- coding: utf-8 -*- +""" + tests.appctx + ~~~~~~~~~~~~~~~~~~~~~~ + + Tests the application context. + + :copyright: (c) 2014 by Armin Ronacher. + :license: BSD, see LICENSE for more details. +""" + +import pytest + +import flask +import unittest + + + +def test_basic_url_generation(): + app = flask.Flask(__name__) + app.config['SERVER_NAME'] = 'localhost' + app.config['PREFERRED_URL_SCHEME'] = 'https' + + @app.route('/') + def index(): + pass + + with app.app_context(): + rv = flask.url_for('index') + assert rv == 'https://localhost/' + +def test_url_generation_requires_server_name(): + app = flask.Flask(__name__) + with app.app_context(): + with pytest.raises(RuntimeError): + flask.url_for('index') + +def test_url_generation_without_context_fails(): + with pytest.raises(RuntimeError): + flask.url_for('index') + +def test_request_context_means_app_context(): + app = flask.Flask(__name__) + with app.test_request_context(): + assert flask.current_app._get_current_object() == app + assert flask._app_ctx_stack.top == None + +def test_app_context_provides_current_app(): + app = flask.Flask(__name__) + with app.app_context(): + assert flask.current_app._get_current_object() == app + assert flask._app_ctx_stack.top == None + +def test_app_tearing_down(): + cleanup_stuff = [] + app = flask.Flask(__name__) + @app.teardown_appcontext + def cleanup(exception): + cleanup_stuff.append(exception) + + with app.app_context(): + pass + + assert cleanup_stuff == [None] + +def test_app_tearing_down_with_previous_exception(): + cleanup_stuff = [] + app = flask.Flask(__name__) + @app.teardown_appcontext + def cleanup(exception): + cleanup_stuff.append(exception) + + try: + raise Exception('dummy') + except Exception: + pass + + with app.app_context(): + pass + + assert cleanup_stuff == [None] + +def test_custom_app_ctx_globals_class(): + class CustomRequestGlobals(object): + def __init__(self): + self.spam = 'eggs' + app = flask.Flask(__name__) + app.app_ctx_globals_class = CustomRequestGlobals + with app.app_context(): + assert flask.render_template_string('{{ g.spam }}') == 'eggs' + +def test_context_refcounts(): + called = [] + app = flask.Flask(__name__) + @app.teardown_request + def teardown_req(error=None): + called.append('request') + @app.teardown_appcontext + def teardown_app(error=None): + called.append('app') + @app.route('/') + def index(): + with flask._app_ctx_stack.top: + with flask._request_ctx_stack.top: + pass + env = flask._request_ctx_stack.top.request.environ + assert env['werkzeug.request'] is not None + return u'' + c = app.test_client() + res = c.get('/') + assert res.status_code == 200 + assert res.data == b'' + assert called == ['request', 'app'] diff --git a/flask/testsuite/test_apps/blueprintapp/__init__.py b/tests/test_apps/blueprintapp/__init__.py similarity index 100% rename from flask/testsuite/test_apps/blueprintapp/__init__.py rename to tests/test_apps/blueprintapp/__init__.py diff --git a/flask/testsuite/test_apps/blueprintapp/apps/__init__.py b/tests/test_apps/blueprintapp/apps/__init__.py similarity index 100% rename from flask/testsuite/test_apps/blueprintapp/apps/__init__.py rename to tests/test_apps/blueprintapp/apps/__init__.py diff --git a/flask/testsuite/test_apps/blueprintapp/apps/admin/__init__.py b/tests/test_apps/blueprintapp/apps/admin/__init__.py similarity index 100% rename from flask/testsuite/test_apps/blueprintapp/apps/admin/__init__.py rename to tests/test_apps/blueprintapp/apps/admin/__init__.py diff --git a/flask/testsuite/test_apps/blueprintapp/apps/admin/static/css/test.css b/tests/test_apps/blueprintapp/apps/admin/static/css/test.css similarity index 100% rename from flask/testsuite/test_apps/blueprintapp/apps/admin/static/css/test.css rename to tests/test_apps/blueprintapp/apps/admin/static/css/test.css diff --git a/flask/testsuite/test_apps/blueprintapp/apps/admin/static/test.txt b/tests/test_apps/blueprintapp/apps/admin/static/test.txt similarity index 100% rename from flask/testsuite/test_apps/blueprintapp/apps/admin/static/test.txt rename to tests/test_apps/blueprintapp/apps/admin/static/test.txt diff --git a/flask/testsuite/test_apps/blueprintapp/apps/admin/templates/admin/index.html b/tests/test_apps/blueprintapp/apps/admin/templates/admin/index.html similarity index 100% rename from flask/testsuite/test_apps/blueprintapp/apps/admin/templates/admin/index.html rename to tests/test_apps/blueprintapp/apps/admin/templates/admin/index.html diff --git a/flask/testsuite/test_apps/blueprintapp/apps/frontend/__init__.py b/tests/test_apps/blueprintapp/apps/frontend/__init__.py similarity index 100% rename from flask/testsuite/test_apps/blueprintapp/apps/frontend/__init__.py rename to tests/test_apps/blueprintapp/apps/frontend/__init__.py diff --git a/flask/testsuite/test_apps/blueprintapp/apps/frontend/templates/frontend/index.html b/tests/test_apps/blueprintapp/apps/frontend/templates/frontend/index.html similarity index 100% rename from flask/testsuite/test_apps/blueprintapp/apps/frontend/templates/frontend/index.html rename to tests/test_apps/blueprintapp/apps/frontend/templates/frontend/index.html diff --git a/flask/testsuite/test_apps/subdomaintestmodule/__init__.py b/tests/test_apps/subdomaintestmodule/__init__.py similarity index 100% rename from flask/testsuite/test_apps/subdomaintestmodule/__init__.py rename to tests/test_apps/subdomaintestmodule/__init__.py diff --git a/flask/testsuite/test_apps/subdomaintestmodule/static/hello.txt b/tests/test_apps/subdomaintestmodule/static/hello.txt similarity index 100% rename from flask/testsuite/test_apps/subdomaintestmodule/static/hello.txt rename to tests/test_apps/subdomaintestmodule/static/hello.txt diff --git a/tests/test_basic.py b/tests/test_basic.py new file mode 100644 index 00000000..c66e8eac --- /dev/null +++ b/tests/test_basic.py @@ -0,0 +1,1508 @@ +# -*- coding: utf-8 -*- +""" + tests.basic + ~~~~~~~~~~~~~~~~~~~~~ + + The basic functionality. + + :copyright: (c) 2014 by Armin Ronacher. + :license: BSD, see LICENSE for more details. +""" + +import pytest + +import re +import uuid +import time +import flask +import pickle +from datetime import datetime +from threading import Thread +from flask._compat import text_type +from werkzeug.exceptions import BadRequest, NotFound, Forbidden +from werkzeug.http import parse_date +from werkzeug.routing import BuildError + + +def test_options_work(): + app = flask.Flask(__name__) + + @app.route('/', methods=['GET', 'POST']) + def index(): + return 'Hello World' + rv = app.test_client().open('/', method='OPTIONS') + assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST'] + assert rv.data == b'' + + +def test_options_on_multiple_rules(): + app = flask.Flask(__name__) + + @app.route('/', methods=['GET', 'POST']) + def index(): + return 'Hello World' + + @app.route('/', methods=['PUT']) + def index_put(): + return 'Aha!' + rv = app.test_client().open('/', method='OPTIONS') + assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST', 'PUT'] + + +def test_options_handling_disabled(): + app = flask.Flask(__name__) + + def index(): + return 'Hello World!' + index.provide_automatic_options = False + app.route('/')(index) + rv = app.test_client().open('/', method='OPTIONS') + assert rv.status_code == 405 + + app = flask.Flask(__name__) + + def index2(): + return 'Hello World!' + index2.provide_automatic_options = True + app.route('/', methods=['OPTIONS'])(index2) + rv = app.test_client().open('/', method='OPTIONS') + assert sorted(rv.allow) == ['OPTIONS'] + + +def test_request_dispatching(): + app = flask.Flask(__name__) + + @app.route('/') + def index(): + return flask.request.method + + @app.route('/more', methods=['GET', 'POST']) + def more(): + return flask.request.method + + c = app.test_client() + assert c.get('/').data == b'GET' + rv = c.post('/') + assert rv.status_code == 405 + assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS'] + rv = c.head('/') + assert rv.status_code == 200 + assert not rv.data # head truncates + assert c.post('/more').data == b'POST' + assert c.get('/more').data == b'GET' + rv = c.delete('/more') + assert rv.status_code == 405 + assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST'] + + +def test_disallow_string_for_allowed_methods(): + app = flask.Flask(__name__) + with pytest.raises(TypeError): + @app.route('/', methods='GET POST') + def index(): + return "Hey" + + +def test_url_mapping(): + app = flask.Flask(__name__) + + def index(): + return flask.request.method + + def more(): + return flask.request.method + + app.add_url_rule('/', 'index', index) + app.add_url_rule('/more', 'more', more, methods=['GET', 'POST']) + + c = app.test_client() + assert c.get('/').data == b'GET' + rv = c.post('/') + assert rv.status_code == 405 + assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS'] + rv = c.head('/') + assert rv.status_code == 200 + assert not rv.data # head truncates + assert c.post('/more').data == b'POST' + assert c.get('/more').data == b'GET' + rv = c.delete('/more') + assert rv.status_code == 405 + assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST'] + + +def test_werkzeug_routing(): + from werkzeug.routing import Submount, Rule + app = flask.Flask(__name__) + app.url_map.add(Submount('/foo', [ + Rule('/bar', endpoint='bar'), + Rule('/', endpoint='index') + ])) + + def bar(): + return 'bar' + + def index(): + return 'index' + app.view_functions['bar'] = bar + app.view_functions['index'] = index + + c = app.test_client() + assert c.get('/foo/').data == b'index' + assert c.get('/foo/bar').data == b'bar' + + +def test_endpoint_decorator(): + from werkzeug.routing import Submount, Rule + app = flask.Flask(__name__) + app.url_map.add(Submount('/foo', [ + Rule('/bar', endpoint='bar'), + Rule('/', endpoint='index') + ])) + + @app.endpoint('bar') + def bar(): + return 'bar' + + @app.endpoint('index') + def index(): + return 'index' + + c = app.test_client() + assert c.get('/foo/').data == b'index' + assert c.get('/foo/bar').data == b'bar' + + +def test_session(): + app = flask.Flask(__name__) + app.secret_key = 'testkey' + + @app.route('/set', methods=['POST']) + def set(): + flask.session['value'] = flask.request.form['value'] + return 'value set' + + @app.route('/get') + def get(): + return flask.session['value'] + + c = app.test_client() + assert c.post('/set', data={'value': '42'}).data == b'value set' + assert c.get('/get').data == b'42' + + +def test_session_using_server_name(): + app = flask.Flask(__name__) + app.config.update( + SECRET_KEY='foo', + SERVER_NAME='example.com' + ) + + @app.route('/') + def index(): + flask.session['testing'] = 42 + return 'Hello World' + rv = app.test_client().get('/', 'http://example.com/') + assert 'domain=.example.com' in rv.headers['set-cookie'].lower() + assert 'httponly' in rv.headers['set-cookie'].lower() + + +def test_session_using_server_name_and_port(): + app = flask.Flask(__name__) + app.config.update( + SECRET_KEY='foo', + SERVER_NAME='example.com:8080' + ) + + @app.route('/') + def index(): + flask.session['testing'] = 42 + return 'Hello World' + rv = app.test_client().get('/', 'http://example.com:8080/') + assert 'domain=.example.com' in rv.headers['set-cookie'].lower() + assert 'httponly' in rv.headers['set-cookie'].lower() + + +def test_session_using_server_name_port_and_path(): + app = flask.Flask(__name__) + app.config.update( + SECRET_KEY='foo', + SERVER_NAME='example.com:8080', + APPLICATION_ROOT='/foo' + ) + + @app.route('/') + def index(): + flask.session['testing'] = 42 + return 'Hello World' + rv = app.test_client().get('/', 'http://example.com:8080/foo') + assert 'domain=example.com' in rv.headers['set-cookie'].lower() + assert 'path=/foo' in rv.headers['set-cookie'].lower() + assert 'httponly' in rv.headers['set-cookie'].lower() + + +def test_session_using_application_root(): + class PrefixPathMiddleware(object): + + def __init__(self, app, prefix): + self.app = app + self.prefix = prefix + + def __call__(self, environ, start_response): + environ['SCRIPT_NAME'] = self.prefix + return self.app(environ, start_response) + + app = flask.Flask(__name__) + app.wsgi_app = PrefixPathMiddleware(app.wsgi_app, '/bar') + app.config.update( + SECRET_KEY='foo', + APPLICATION_ROOT='/bar' + ) + + @app.route('/') + def index(): + flask.session['testing'] = 42 + return 'Hello World' + rv = app.test_client().get('/', 'http://example.com:8080/') + assert 'path=/bar' in rv.headers['set-cookie'].lower() + + +def test_session_using_session_settings(): + app = flask.Flask(__name__) + app.config.update( + SECRET_KEY='foo', + SERVER_NAME='www.example.com:8080', + APPLICATION_ROOT='/test', + SESSION_COOKIE_DOMAIN='.example.com', + SESSION_COOKIE_HTTPONLY=False, + SESSION_COOKIE_SECURE=True, + SESSION_COOKIE_PATH='/' + ) + + @app.route('/') + def index(): + flask.session['testing'] = 42 + return 'Hello World' + rv = app.test_client().get('/', 'http://www.example.com:8080/test/') + cookie = rv.headers['set-cookie'].lower() + assert 'domain=.example.com' in cookie + assert 'path=/' in cookie + assert 'secure' in cookie + assert 'httponly' not in cookie + + +def test_missing_session(): + app = flask.Flask(__name__) + + def expect_exception(f, *args, **kwargs): + try: + f(*args, **kwargs) + except RuntimeError as e: + assert e.args and 'session is unavailable' in e.args[0] + else: + assert False, 'expected exception' + with app.test_request_context(): + assert flask.session.get('missing_key') is None + expect_exception(flask.session.__setitem__, 'foo', 42) + expect_exception(flask.session.pop, 'foo') + + +def test_session_expiration(): + permanent = True + app = flask.Flask(__name__) + app.secret_key = 'testkey' + + @app.route('/') + def index(): + flask.session['test'] = 42 + flask.session.permanent = permanent + return '' + + @app.route('/test') + def test(): + return text_type(flask.session.permanent) + + client = app.test_client() + rv = client.get('/') + assert 'set-cookie' in rv.headers + match = re.search(r'\bexpires=([^;]+)(?i)', rv.headers['set-cookie']) + expires = parse_date(match.group()) + expected = datetime.utcnow() + app.permanent_session_lifetime + assert expires.year == expected.year + assert expires.month == expected.month + assert expires.day == expected.day + + rv = client.get('/test') + assert rv.data == b'True' + + permanent = False + rv = app.test_client().get('/') + assert 'set-cookie' in rv.headers + match = re.search(r'\bexpires=([^;]+)', rv.headers['set-cookie']) + assert match is None + + +def test_session_stored_last(): + app = flask.Flask(__name__) + app.secret_key = 'development-key' + app.testing = True + + @app.after_request + def modify_session(response): + flask.session['foo'] = 42 + return response + + @app.route('/') + def dump_session_contents(): + return repr(flask.session.get('foo')) + + c = app.test_client() + assert c.get('/').data == b'None' + assert c.get('/').data == b'42' + + +def test_session_special_types(): + app = flask.Flask(__name__) + app.secret_key = 'development-key' + app.testing = True + now = datetime.utcnow().replace(microsecond=0) + the_uuid = uuid.uuid4() + + @app.after_request + def modify_session(response): + flask.session['m'] = flask.Markup('Hello!') + flask.session['u'] = the_uuid + flask.session['dt'] = now + flask.session['b'] = b'\xff' + flask.session['t'] = (1, 2, 3) + return response + + @app.route('/') + def dump_session_contents(): + return pickle.dumps(dict(flask.session)) + + c = app.test_client() + c.get('/') + rv = pickle.loads(c.get('/').data) + assert rv['m'] == flask.Markup('Hello!') + assert type(rv['m']) == flask.Markup + assert rv['dt'] == now + assert rv['u'] == the_uuid + assert rv['b'] == b'\xff' + assert type(rv['b']) == bytes + assert rv['t'] == (1, 2, 3) + + +def test_session_cookie_setting(): + app = flask.Flask(__name__) + app.testing = True + app.secret_key = 'dev key' + is_permanent = True + + @app.route('/bump') + def bump(): + rv = flask.session['foo'] = flask.session.get('foo', 0) + 1 + flask.session.permanent = is_permanent + return str(rv) + + @app.route('/read') + def read(): + return str(flask.session.get('foo', 0)) + + def run_test(expect_header): + with app.test_client() as c: + assert c.get('/bump').data == b'1' + assert c.get('/bump').data == b'2' + assert c.get('/bump').data == b'3' + + rv = c.get('/read') + set_cookie = rv.headers.get('set-cookie') + assert (set_cookie is not None) == expect_header + assert rv.data == b'3' + + is_permanent = True + app.config['SESSION_REFRESH_EACH_REQUEST'] = True + run_test(expect_header=True) + + is_permanent = True + app.config['SESSION_REFRESH_EACH_REQUEST'] = False + run_test(expect_header=False) + + is_permanent = False + app.config['SESSION_REFRESH_EACH_REQUEST'] = True + run_test(expect_header=False) + + is_permanent = False + app.config['SESSION_REFRESH_EACH_REQUEST'] = False + run_test(expect_header=False) + + +def test_flashes(): + app = flask.Flask(__name__) + app.secret_key = 'testkey' + + with app.test_request_context(): + assert not flask.session.modified + flask.flash('Zap') + flask.session.modified = False + flask.flash('Zip') + assert flask.session.modified + assert list(flask.get_flashed_messages()) == ['Zap', 'Zip'] + + +def test_extended_flashing(): + # Be sure app.testing=True below, else tests can fail silently. + # + # Specifically, if app.testing is not set to True, the AssertionErrors + # in the view functions will cause a 500 response to the test client + # instead of propagating exceptions. + + app = flask.Flask(__name__) + app.secret_key = 'testkey' + app.testing = True + + @app.route('/') + def index(): + flask.flash(u'Hello World') + flask.flash(u'Hello World', 'error') + flask.flash(flask.Markup(u'Testing'), 'warning') + return '' + + @app.route('/test/') + def test(): + messages = flask.get_flashed_messages() + assert list(messages) == [ + u'Hello World', + u'Hello World', + flask.Markup(u'Testing') + ] + return '' + + @app.route('/test_with_categories/') + def test_with_categories(): + messages = flask.get_flashed_messages(with_categories=True) + assert len(messages) == 3 + assert list(messages) == [ + ('message', u'Hello World'), + ('error', u'Hello World'), + ('warning', flask.Markup(u'Testing')) + ] + return '' + + @app.route('/test_filter/') + def test_filter(): + messages = flask.get_flashed_messages( + category_filter=['message'], with_categories=True) + assert list(messages) == [('message', u'Hello World')] + return '' + + @app.route('/test_filters/') + def test_filters(): + messages = flask.get_flashed_messages( + category_filter=['message', 'warning'], with_categories=True) + assert list(messages) == [ + ('message', u'Hello World'), + ('warning', flask.Markup(u'Testing')) + ] + return '' + + @app.route('/test_filters_without_returning_categories/') + def test_filters2(): + messages = flask.get_flashed_messages( + category_filter=['message', 'warning']) + assert len(messages) == 2 + assert messages[0] == u'Hello World' + assert messages[1] == flask.Markup(u'Testing') + return '' + + # Create new test client on each test to clean flashed messages. + + c = app.test_client() + c.get('/') + c.get('/test/') + + c = app.test_client() + c.get('/') + c.get('/test_with_categories/') + + c = app.test_client() + c.get('/') + c.get('/test_filter/') + + c = app.test_client() + c.get('/') + c.get('/test_filters/') + + c = app.test_client() + c.get('/') + c.get('/test_filters_without_returning_categories/') + + +def test_request_processing(): + app = flask.Flask(__name__) + evts = [] + + @app.before_request + def before_request(): + evts.append('before') + + @app.after_request + def after_request(response): + response.data += b'|after' + evts.append('after') + return response + + @app.route('/') + def index(): + assert 'before' in evts + assert 'after' not in evts + return 'request' + assert 'after' not in evts + rv = app.test_client().get('/').data + assert 'after' in evts + assert rv == b'request|after' + + +def test_after_request_processing(): + app = flask.Flask(__name__) + app.testing = True + + @app.route('/') + def index(): + @flask.after_this_request + def foo(response): + response.headers['X-Foo'] = 'a header' + return response + return 'Test' + c = app.test_client() + resp = c.get('/') + assert resp.status_code == 200 + assert resp.headers['X-Foo'] == 'a header' + + +def test_teardown_request_handler(): + called = [] + app = flask.Flask(__name__) + + @app.teardown_request + def teardown_request(exc): + called.append(True) + return "Ignored" + + @app.route('/') + def root(): + return "Response" + rv = app.test_client().get('/') + assert rv.status_code == 200 + assert b'Response' in rv.data + assert len(called) == 1 + + +def test_teardown_request_handler_debug_mode(): + called = [] + app = flask.Flask(__name__) + app.testing = True + + @app.teardown_request + def teardown_request(exc): + called.append(True) + return "Ignored" + + @app.route('/') + def root(): + return "Response" + rv = app.test_client().get('/') + assert rv.status_code == 200 + assert b'Response' in rv.data + assert len(called) == 1 + + +def test_teardown_request_handler_error(): + called = [] + app = flask.Flask(__name__) + app.config['LOGGER_HANDLER_POLICY'] = 'never' + + @app.teardown_request + def teardown_request1(exc): + assert type(exc) == ZeroDivisionError + called.append(True) + # This raises a new error and blows away sys.exc_info(), so we can + # test that all teardown_requests get passed the same original + # exception. + try: + raise TypeError() + except: + pass + + @app.teardown_request + def teardown_request2(exc): + assert type(exc) == ZeroDivisionError + called.append(True) + # This raises a new error and blows away sys.exc_info(), so we can + # test that all teardown_requests get passed the same original + # exception. + try: + raise TypeError() + except: + pass + + @app.route('/') + def fails(): + 1 // 0 + rv = app.test_client().get('/') + assert rv.status_code == 500 + assert b'Internal Server Error' in rv.data + assert len(called) == 2 + + +def test_before_after_request_order(): + called = [] + app = flask.Flask(__name__) + + @app.before_request + def before1(): + called.append(1) + + @app.before_request + def before2(): + called.append(2) + + @app.after_request + def after1(response): + called.append(4) + return response + + @app.after_request + def after2(response): + called.append(3) + return response + + @app.teardown_request + def finish1(exc): + called.append(6) + + @app.teardown_request + def finish2(exc): + called.append(5) + + @app.route('/') + def index(): + return '42' + rv = app.test_client().get('/') + assert rv.data == b'42' + assert called == [1, 2, 3, 4, 5, 6] + + +def test_error_handling(): + app = flask.Flask(__name__) + app.config['LOGGER_HANDLER_POLICY'] = 'never' + + @app.errorhandler(404) + def not_found(e): + return 'not found', 404 + + @app.errorhandler(500) + def internal_server_error(e): + return 'internal server error', 500 + + @app.errorhandler(Forbidden) + def forbidden(e): + return 'forbidden', 403 + + @app.route('/') + def index(): + flask.abort(404) + + @app.route('/error') + def error(): + 1 // 0 + + @app.route('/forbidden') + def error2(): + flask.abort(403) + c = app.test_client() + rv = c.get('/') + assert rv.status_code == 404 + assert rv.data == b'not found' + rv = c.get('/error') + assert rv.status_code == 500 + assert b'internal server error' == rv.data + rv = c.get('/forbidden') + assert rv.status_code == 403 + assert b'forbidden' == rv.data + + +def test_before_request_and_routing_errors(): + app = flask.Flask(__name__) + + @app.before_request + def attach_something(): + flask.g.something = 'value' + + @app.errorhandler(404) + def return_something(error): + return flask.g.something, 404 + rv = app.test_client().get('/') + assert rv.status_code == 404 + assert rv.data == b'value' + + +def test_user_error_handling(): + class MyException(Exception): + pass + + app = flask.Flask(__name__) + + @app.errorhandler(MyException) + def handle_my_exception(e): + assert isinstance(e, MyException) + return '42' + + @app.route('/') + def index(): + raise MyException() + + c = app.test_client() + assert c.get('/').data == b'42' + + +def test_http_error_subclass_handling(): + class ForbiddenSubclass(Forbidden): + pass + + app = flask.Flask(__name__) + + @app.errorhandler(ForbiddenSubclass) + def handle_forbidden_subclass(e): + assert isinstance(e, ForbiddenSubclass) + return 'banana' + + @app.errorhandler(403) + def handle_forbidden_subclass(e): + assert not isinstance(e, ForbiddenSubclass) + assert isinstance(e, Forbidden) + return 'apple' + + @app.route('/1') + def index1(): + raise ForbiddenSubclass() + + @app.route('/2') + def index2(): + flask.abort(403) + + @app.route('/3') + def index3(): + raise Forbidden() + + c = app.test_client() + assert c.get('/1').data == b'banana' + assert c.get('/2').data == b'apple' + assert c.get('/3').data == b'apple' + + +def test_trapping_of_bad_request_key_errors(): + app = flask.Flask(__name__) + app.testing = True + + @app.route('/fail') + def fail(): + flask.request.form['missing_key'] + c = app.test_client() + assert c.get('/fail').status_code == 400 + + app.config['TRAP_BAD_REQUEST_ERRORS'] = True + c = app.test_client() + try: + c.get('/fail') + except KeyError as e: + assert isinstance(e, BadRequest) + else: + assert False, 'Expected exception' + + +def test_trapping_of_all_http_exceptions(): + app = flask.Flask(__name__) + app.testing = True + app.config['TRAP_HTTP_EXCEPTIONS'] = True + + @app.route('/fail') + def fail(): + flask.abort(404) + + c = app.test_client() + with pytest.raises(NotFound): + c.get('/fail') + + +def test_enctype_debug_helper(): + from flask.debughelpers import DebugFilesKeyError + app = flask.Flask(__name__) + app.debug = True + + @app.route('/fail', methods=['POST']) + def index(): + return flask.request.files['foo'].filename + + # with statement is important because we leave an exception on the + # stack otherwise and we want to ensure that this is not the case + # to not negatively affect other tests. + with app.test_client() as c: + try: + c.post('/fail', data={'foo': 'index.txt'}) + except DebugFilesKeyError as e: + assert 'no file contents were transmitted' in str(e) + assert 'This was submitted: "index.txt"' in str(e) + else: + assert False, 'Expected exception' + + +def test_response_creation(): + app = flask.Flask(__name__) + + @app.route('/unicode') + def from_unicode(): + return u'Hällo Wörld' + + @app.route('/string') + def from_string(): + return u'Hällo Wörld'.encode('utf-8') + + @app.route('/args') + def from_tuple(): + return 'Meh', 400, { + 'X-Foo': 'Testing', + 'Content-Type': 'text/plain; charset=utf-8' + } + + @app.route('/two_args') + def from_two_args_tuple(): + return 'Hello', { + 'X-Foo': 'Test', + 'Content-Type': 'text/plain; charset=utf-8' + } + + @app.route('/args_status') + def from_status_tuple(): + return 'Hi, status!', 400 + + @app.route('/args_header') + def from_response_instance_status_tuple(): + return flask.Response('Hello world', 404), { + "X-Foo": "Bar", + "X-Bar": "Foo" + } + + c = app.test_client() + assert c.get('/unicode').data == u'Hällo Wörld'.encode('utf-8') + assert c.get('/string').data == u'Hällo Wörld'.encode('utf-8') + rv = c.get('/args') + assert rv.data == b'Meh' + assert rv.headers['X-Foo'] == 'Testing' + assert rv.status_code == 400 + assert rv.mimetype == 'text/plain' + rv2 = c.get('/two_args') + assert rv2.data == b'Hello' + assert rv2.headers['X-Foo'] == 'Test' + assert rv2.status_code == 200 + assert rv2.mimetype == 'text/plain' + rv3 = c.get('/args_status') + assert rv3.data == b'Hi, status!' + assert rv3.status_code == 400 + assert rv3.mimetype == 'text/html' + rv4 = c.get('/args_header') + assert rv4.data == b'Hello world' + assert rv4.headers['X-Foo'] == 'Bar' + assert rv4.headers['X-Bar'] == 'Foo' + assert rv4.status_code == 404 + + +def test_make_response(): + app = flask.Flask(__name__) + with app.test_request_context(): + rv = flask.make_response() + assert rv.status_code == 200 + assert rv.data == b'' + assert rv.mimetype == 'text/html' + + rv = flask.make_response('Awesome') + assert rv.status_code == 200 + assert rv.data == b'Awesome' + assert rv.mimetype == 'text/html' + + rv = flask.make_response('W00t', 404) + assert rv.status_code == 404 + assert rv.data == b'W00t' + assert rv.mimetype == 'text/html' + + +def test_make_response_with_response_instance(): + app = flask.Flask(__name__) + with app.test_request_context(): + rv = flask.make_response( + flask.jsonify({'msg': 'W00t'}), 400) + assert rv.status_code == 400 + assert rv.data == b'{\n "msg": "W00t"\n}' + assert rv.mimetype == 'application/json' + + rv = flask.make_response( + flask.Response(''), 400) + assert rv.status_code == 400 + assert rv.data == b'' + assert rv.mimetype == 'text/html' + + rv = flask.make_response( + flask.Response('', headers={'Content-Type': 'text/html'}), + 400, [('X-Foo', 'bar')]) + assert rv.status_code == 400 + assert rv.headers['Content-Type'] == 'text/html' + assert rv.headers['X-Foo'] == 'bar' + + +def test_url_generation(): + app = flask.Flask(__name__) + + @app.route('/hello/', methods=['POST']) + def hello(): + pass + with app.test_request_context(): + assert flask.url_for('hello', name='test x') == '/hello/test%20x' + assert flask.url_for('hello', name='test x', _external=True) == \ + 'http://localhost/hello/test%20x' + + +def test_build_error_handler(): + app = flask.Flask(__name__) + + # Test base case, a URL which results in a BuildError. + with app.test_request_context(): + pytest.raises(BuildError, flask.url_for, 'spam') + + # Verify the error is re-raised if not the current exception. + try: + with app.test_request_context(): + flask.url_for('spam') + except BuildError as err: + error = err + try: + raise RuntimeError('Test case where BuildError is not current.') + except RuntimeError: + pytest.raises( + BuildError, app.handle_url_build_error, error, 'spam', {}) + + # Test a custom handler. + def handler(error, endpoint, values): + # Just a test. + return '/test_handler/' + app.url_build_error_handlers.append(handler) + with app.test_request_context(): + assert flask.url_for('spam') == '/test_handler/' + + +def test_custom_converters(): + from werkzeug.routing import BaseConverter + + class ListConverter(BaseConverter): + + def to_python(self, value): + return value.split(',') + + def to_url(self, value): + base_to_url = super(ListConverter, self).to_url + return ','.join(base_to_url(x) for x in value) + app = flask.Flask(__name__) + app.url_map.converters['list'] = ListConverter + + @app.route('/') + def index(args): + return '|'.join(args) + c = app.test_client() + assert c.get('/1,2,3').data == b'1|2|3' + + +def test_static_files(): + app = flask.Flask(__name__) + app.testing = True + rv = app.test_client().get('/static/index.html') + assert rv.status_code == 200 + assert rv.data.strip() == b'

Hello World!

' + with app.test_request_context(): + assert flask.url_for('static', filename='index.html') == \ + '/static/index.html' + rv.close() + + +def test_none_response(): + app = flask.Flask(__name__) + app.testing = True + + @app.route('/') + def test(): + return None + try: + app.test_client().get('/') + except ValueError as e: + assert str(e) == 'View function did not return a response' + pass + else: + assert "Expected ValueError" + + +def test_request_locals(): + assert repr(flask.g) == '' + assert not flask.g + + +def test_test_app_proper_environ(): + app = flask.Flask(__name__) + app.config.update( + SERVER_NAME='localhost.localdomain:5000' + ) + + @app.route('/') + def index(): + return 'Foo' + + @app.route('/', subdomain='foo') + def subdomain(): + return 'Foo SubDomain' + + rv = app.test_client().get('/') + assert rv.data == b'Foo' + + rv = app.test_client().get('/', 'http://localhost.localdomain:5000') + assert rv.data == b'Foo' + + rv = app.test_client().get('/', 'https://localhost.localdomain:5000') + assert rv.data == b'Foo' + + app.config.update(SERVER_NAME='localhost.localdomain') + rv = app.test_client().get('/', 'https://localhost.localdomain') + assert rv.data == b'Foo' + + try: + app.config.update(SERVER_NAME='localhost.localdomain:443') + rv = app.test_client().get('/', 'https://localhost.localdomain') + # Werkzeug 0.8 + assert rv.status_code == 404 + except ValueError as e: + # Werkzeug 0.7 + assert str(e) == ( + "the server name provided " + "('localhost.localdomain:443') does not match the " + "server name from the WSGI environment ('localhost.localdomain')" + ) + + try: + app.config.update(SERVER_NAME='localhost.localdomain') + rv = app.test_client().get('/', 'http://foo.localhost') + # Werkzeug 0.8 + assert rv.status_code == 404 + except ValueError as e: + # Werkzeug 0.7 + assert str(e) == ( + "the server name provided " + "('localhost.localdomain') does not match the " + "server name from the WSGI environment ('foo.localhost')" + ) + + rv = app.test_client().get('/', 'http://foo.localhost.localdomain') + assert rv.data == b'Foo SubDomain' + + +def test_exception_propagation(): + def apprunner(configkey): + app = flask.Flask(__name__) + app.config['LOGGER_HANDLER_POLICY'] = 'never' + + @app.route('/') + def index(): + 1 // 0 + c = app.test_client() + if config_key is not None: + app.config[config_key] = True + try: + c.get('/') + except Exception: + pass + else: + assert False, 'expected exception' + else: + assert c.get('/').status_code == 500 + + # we have to run this test in an isolated thread because if the + # debug flag is set to true and an exception happens the context is + # not torn down. This causes other tests that run after this fail + # when they expect no exception on the stack. + for config_key in 'TESTING', 'PROPAGATE_EXCEPTIONS', 'DEBUG', None: + t = Thread(target=apprunner, args=(config_key,)) + t.start() + t.join() + + +def test_max_content_length(): + app = flask.Flask(__name__) + app.config['MAX_CONTENT_LENGTH'] = 64 + + @app.before_request + def always_first(): + flask.request.form['myfile'] + assert False + + @app.route('/accept', methods=['POST']) + def accept_file(): + flask.request.form['myfile'] + assert False + + @app.errorhandler(413) + def catcher(error): + return '42' + + c = app.test_client() + rv = c.post('/accept', data={'myfile': 'foo' * 100}) + assert rv.data == b'42' + + +def test_url_processors(): + app = flask.Flask(__name__) + + @app.url_defaults + def add_language_code(endpoint, values): + if flask.g.lang_code is not None and \ + app.url_map.is_endpoint_expecting(endpoint, 'lang_code'): + values.setdefault('lang_code', flask.g.lang_code) + + @app.url_value_preprocessor + def pull_lang_code(endpoint, values): + flask.g.lang_code = values.pop('lang_code', None) + + @app.route('//') + def index(): + return flask.url_for('about') + + @app.route('//about') + def about(): + return flask.url_for('something_else') + + @app.route('/foo') + def something_else(): + return flask.url_for('about', lang_code='en') + + c = app.test_client() + + assert c.get('/de/').data == b'/de/about' + assert c.get('/de/about').data == b'/foo' + assert c.get('/foo').data == b'/en/about' + + +def test_inject_blueprint_url_defaults(): + app = flask.Flask(__name__) + bp = flask.Blueprint('foo.bar.baz', __name__, + template_folder='template') + + @bp.url_defaults + def bp_defaults(endpoint, values): + values['page'] = 'login' + + @bp.route('/') + def view(page): + pass + + app.register_blueprint(bp) + + values = dict() + app.inject_url_defaults('foo.bar.baz.view', values) + expected = dict(page='login') + assert values == expected + + with app.test_request_context('/somepage'): + url = flask.url_for('foo.bar.baz.view') + expected = '/login' + assert url == expected + + +def test_nonascii_pathinfo(): + app = flask.Flask(__name__) + app.testing = True + + @app.route(u'/киртест') + def index(): + return 'Hello World!' + + c = app.test_client() + rv = c.get(u'/киртест') + assert rv.data == b'Hello World!' + + +def test_debug_mode_complains_after_first_request(): + app = flask.Flask(__name__) + app.debug = True + + @app.route('/') + def index(): + return 'Awesome' + assert not app.got_first_request + assert app.test_client().get('/').data == b'Awesome' + try: + @app.route('/foo') + def broken(): + return 'Meh' + except AssertionError as e: + assert 'A setup function was called' in str(e) + else: + assert False, 'Expected exception' + + app.debug = False + + @app.route('/foo') + def working(): + return 'Meh' + assert app.test_client().get('/foo').data == b'Meh' + assert app.got_first_request + + +def test_before_first_request_functions(): + got = [] + app = flask.Flask(__name__) + + @app.before_first_request + def foo(): + got.append(42) + c = app.test_client() + c.get('/') + assert got == [42] + c.get('/') + assert got == [42] + assert app.got_first_request + + +def test_before_first_request_functions_concurrent(): + got = [] + app = flask.Flask(__name__) + + @app.before_first_request + def foo(): + time.sleep(0.2) + got.append(42) + + c = app.test_client() + + def get_and_assert(): + c.get("/") + assert got == [42] + + t = Thread(target=get_and_assert) + t.start() + get_and_assert() + t.join() + assert app.got_first_request + + +def test_routing_redirect_debugging(): + app = flask.Flask(__name__) + app.debug = True + + @app.route('/foo/', methods=['GET', 'POST']) + def foo(): + return 'success' + with app.test_client() as c: + try: + c.post('/foo', data={}) + except AssertionError as e: + assert 'http://localhost/foo/' in str(e) + assert ('Make sure to directly send ' + 'your POST-request to this URL') in str(e) + else: + assert False, 'Expected exception' + + rv = c.get('/foo', data={}, follow_redirects=True) + assert rv.data == b'success' + + app.debug = False + with app.test_client() as c: + rv = c.post('/foo', data={}, follow_redirects=True) + assert rv.data == b'success' + + +def test_route_decorator_custom_endpoint(): + app = flask.Flask(__name__) + app.debug = True + + @app.route('/foo/') + def foo(): + return flask.request.endpoint + + @app.route('/bar/', endpoint='bar') + def for_bar(): + return flask.request.endpoint + + @app.route('/bar/123', endpoint='123') + def for_bar_foo(): + return flask.request.endpoint + + with app.test_request_context(): + assert flask.url_for('foo') == '/foo/' + assert flask.url_for('bar') == '/bar/' + assert flask.url_for('123') == '/bar/123' + + c = app.test_client() + assert c.get('/foo/').data == b'foo' + assert c.get('/bar/').data == b'bar' + assert c.get('/bar/123').data == b'123' + + +def test_preserve_only_once(): + app = flask.Flask(__name__) + app.debug = True + + @app.route('/fail') + def fail_func(): + 1 // 0 + + c = app.test_client() + for x in range(3): + with pytest.raises(ZeroDivisionError): + c.get('/fail') + + assert flask._request_ctx_stack.top is not None + assert flask._app_ctx_stack.top is not None + # implicit appctx disappears too + flask._request_ctx_stack.top.pop() + assert flask._request_ctx_stack.top is None + assert flask._app_ctx_stack.top is None + + +def test_preserve_remembers_exception(): + app = flask.Flask(__name__) + app.debug = True + errors = [] + + @app.route('/fail') + def fail_func(): + 1 // 0 + + @app.route('/success') + def success_func(): + return 'Okay' + + @app.teardown_request + def teardown_handler(exc): + errors.append(exc) + + c = app.test_client() + + # After this failure we did not yet call the teardown handler + with pytest.raises(ZeroDivisionError): + c.get('/fail') + assert errors == [] + + # But this request triggers it, and it's an error + c.get('/success') + assert len(errors) == 2 + assert isinstance(errors[0], ZeroDivisionError) + + # At this point another request does nothing. + c.get('/success') + assert len(errors) == 3 + assert errors[1] is None + + +def test_get_method_on_g(): + app = flask.Flask(__name__) + app.testing = True + + with app.app_context(): + assert flask.g.get('x') is None + assert flask.g.get('x', 11) == 11 + flask.g.x = 42 + assert flask.g.get('x') == 42 + assert flask.g.x == 42 + + +def test_g_iteration_protocol(): + app = flask.Flask(__name__) + app.testing = True + + with app.app_context(): + flask.g.foo = 23 + flask.g.bar = 42 + assert 'foo' in flask.g + assert 'foos' not in flask.g + assert sorted(flask.g) == ['bar', 'foo'] + + +def test_subdomain_basic_support(): + app = flask.Flask(__name__) + app.config['SERVER_NAME'] = 'localhost' + + @app.route('/') + def normal_index(): + return 'normal index' + + @app.route('/', subdomain='test') + def test_index(): + return 'test index' + + c = app.test_client() + rv = c.get('/', 'http://localhost/') + assert rv.data == b'normal index' + + rv = c.get('/', 'http://test.localhost/') + assert rv.data == b'test index' + + +def test_subdomain_matching(): + app = flask.Flask(__name__) + app.config['SERVER_NAME'] = 'localhost' + + @app.route('/', subdomain='') + def index(user): + return 'index for %s' % user + + c = app.test_client() + rv = c.get('/', 'http://mitsuhiko.localhost/') + assert rv.data == b'index for mitsuhiko' + + +def test_subdomain_matching_with_ports(): + app = flask.Flask(__name__) + app.config['SERVER_NAME'] = 'localhost:3000' + + @app.route('/', subdomain='') + def index(user): + return 'index for %s' % user + + c = app.test_client() + rv = c.get('/', 'http://mitsuhiko.localhost:3000/') + assert rv.data == b'index for mitsuhiko' + + +def test_multi_route_rules(): + app = flask.Flask(__name__) + + @app.route('/') + @app.route('//') + def index(test='a'): + return test + + rv = app.test_client().open('/') + assert rv.data == b'a' + rv = app.test_client().open('/b/') + assert rv.data == b'b' + + +def test_multi_route_class_views(): + class View(object): + + def __init__(self, app): + app.add_url_rule('/', 'index', self.index) + app.add_url_rule('//', 'index', self.index) + + def index(self, test='a'): + return test + + app = flask.Flask(__name__) + _ = View(app) + rv = app.test_client().open('/') + assert rv.data == b'a' + rv = app.test_client().open('/b/') + assert rv.data == b'b' diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py new file mode 100644 index 00000000..775d0472 --- /dev/null +++ b/tests/test_blueprints.py @@ -0,0 +1,578 @@ +# -*- coding: utf-8 -*- +""" + tests.blueprints + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + + Blueprints (and currently modules) + + :copyright: (c) 2014 by Armin Ronacher. + :license: BSD, see LICENSE for more details. +""" + +import pytest + +import flask +import unittest + +from flask._compat import text_type +from werkzeug.http import parse_cache_control_header +from jinja2 import TemplateNotFound + + +def test_blueprint_specific_error_handling(): + frontend = flask.Blueprint('frontend', __name__) + backend = flask.Blueprint('backend', __name__) + sideend = flask.Blueprint('sideend', __name__) + + @frontend.errorhandler(403) + def frontend_forbidden(e): + return 'frontend says no', 403 + + @frontend.route('/frontend-no') + def frontend_no(): + flask.abort(403) + + @backend.errorhandler(403) + def backend_forbidden(e): + return 'backend says no', 403 + + @backend.route('/backend-no') + def backend_no(): + flask.abort(403) + + @sideend.route('/what-is-a-sideend') + def sideend_no(): + flask.abort(403) + + app = flask.Flask(__name__) + app.register_blueprint(frontend) + app.register_blueprint(backend) + app.register_blueprint(sideend) + + @app.errorhandler(403) + def app_forbidden(e): + return 'application itself says no', 403 + + c = app.test_client() + + assert c.get('/frontend-no').data == b'frontend says no' + assert c.get('/backend-no').data == b'backend says no' + assert c.get('/what-is-a-sideend').data == b'application itself says no' + +def test_blueprint_specific_user_error_handling(): + class MyDecoratorException(Exception): + pass + class MyFunctionException(Exception): + pass + + blue = flask.Blueprint('blue', __name__) + + @blue.errorhandler(MyDecoratorException) + def my_decorator_exception_handler(e): + assert isinstance(e, MyDecoratorException) + return 'boom' + + def my_function_exception_handler(e): + assert isinstance(e, MyFunctionException) + return 'bam' + blue.register_error_handler(MyFunctionException, my_function_exception_handler) + + @blue.route('/decorator') + def blue_deco_test(): + raise MyDecoratorException() + @blue.route('/function') + def blue_func_test(): + raise MyFunctionException() + + app = flask.Flask(__name__) + app.register_blueprint(blue) + + c = app.test_client() + + assert c.get('/decorator').data == b'boom' + assert c.get('/function').data == b'bam' + +def test_blueprint_url_definitions(): + bp = flask.Blueprint('test', __name__) + + @bp.route('/foo', defaults={'baz': 42}) + def foo(bar, baz): + return '%s/%d' % (bar, baz) + + @bp.route('/bar') + def bar(bar): + return text_type(bar) + + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/1', url_defaults={'bar': 23}) + app.register_blueprint(bp, url_prefix='/2', url_defaults={'bar': 19}) + + c = app.test_client() + assert c.get('/1/foo').data == b'23/42' + assert c.get('/2/foo').data == b'19/42' + assert c.get('/1/bar').data == b'23' + assert c.get('/2/bar').data == b'19' + +def test_blueprint_url_processors(): + bp = flask.Blueprint('frontend', __name__, url_prefix='/') + + @bp.url_defaults + def add_language_code(endpoint, values): + values.setdefault('lang_code', flask.g.lang_code) + + @bp.url_value_preprocessor + def pull_lang_code(endpoint, values): + flask.g.lang_code = values.pop('lang_code') + + @bp.route('/') + def index(): + return flask.url_for('.about') + + @bp.route('/about') + def about(): + return flask.url_for('.index') + + app = flask.Flask(__name__) + app.register_blueprint(bp) + + c = app.test_client() + + assert c.get('/de/').data == b'/de/about' + assert c.get('/de/about').data == b'/de/' + +def test_templates_and_static(test_apps): + from blueprintapp import app + c = app.test_client() + + rv = c.get('/') + assert rv.data == b'Hello from the Frontend' + rv = c.get('/admin/') + assert rv.data == b'Hello from the Admin' + rv = c.get('/admin/index2') + assert rv.data == b'Hello from the Admin' + rv = c.get('/admin/static/test.txt') + assert rv.data.strip() == b'Admin File' + rv.close() + rv = c.get('/admin/static/css/test.css') + assert rv.data.strip() == b'/* nested file */' + rv.close() + + # try/finally, in case other tests use this app for Blueprint tests. + max_age_default = app.config['SEND_FILE_MAX_AGE_DEFAULT'] + try: + expected_max_age = 3600 + if app.config['SEND_FILE_MAX_AGE_DEFAULT'] == expected_max_age: + expected_max_age = 7200 + app.config['SEND_FILE_MAX_AGE_DEFAULT'] = expected_max_age + rv = c.get('/admin/static/css/test.css') + cc = parse_cache_control_header(rv.headers['Cache-Control']) + assert cc.max_age == expected_max_age + rv.close() + finally: + app.config['SEND_FILE_MAX_AGE_DEFAULT'] = max_age_default + + with app.test_request_context(): + assert 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: + assert e.name == 'missing.html' + else: + assert 0, 'expected exception' + + with flask.Flask(__name__).test_request_context(): + assert flask.render_template('nested/nested.txt') == 'I\'m nested' + +def test_default_static_cache_timeout(): + app = flask.Flask(__name__) + class MyBlueprint(flask.Blueprint): + def get_send_file_max_age(self, filename): + return 100 + + blueprint = MyBlueprint('blueprint', __name__, static_folder='static') + app.register_blueprint(blueprint) + + # try/finally, in case other tests use this app for Blueprint tests. + max_age_default = app.config['SEND_FILE_MAX_AGE_DEFAULT'] + try: + with app.test_request_context(): + unexpected_max_age = 3600 + if app.config['SEND_FILE_MAX_AGE_DEFAULT'] == unexpected_max_age: + unexpected_max_age = 7200 + app.config['SEND_FILE_MAX_AGE_DEFAULT'] = unexpected_max_age + rv = blueprint.send_static_file('index.html') + cc = parse_cache_control_header(rv.headers['Cache-Control']) + assert cc.max_age == 100 + rv.close() + finally: + app.config['SEND_FILE_MAX_AGE_DEFAULT'] = max_age_default + +def test_templates_list(test_apps): + from blueprintapp import app + templates = sorted(app.jinja_env.list_templates()) + assert templates == ['admin/index.html', 'frontend/index.html'] + +def test_dotted_names(): + frontend = flask.Blueprint('myapp.frontend', __name__) + backend = flask.Blueprint('myapp.backend', __name__) + + @frontend.route('/fe') + def frontend_index(): + return flask.url_for('myapp.backend.backend_index') + + @frontend.route('/fe2') + def frontend_page2(): + return flask.url_for('.frontend_index') + + @backend.route('/be') + def backend_index(): + return flask.url_for('myapp.frontend.frontend_index') + + app = flask.Flask(__name__) + app.register_blueprint(frontend) + app.register_blueprint(backend) + + c = app.test_client() + assert c.get('/fe').data.strip() == b'/be' + assert c.get('/fe2').data.strip() == b'/fe' + assert c.get('/be').data.strip() == b'/fe' + +def test_dotted_names_from_app(): + app = flask.Flask(__name__) + app.testing = True + test = flask.Blueprint('test', __name__) + + @app.route('/') + def app_index(): + return flask.url_for('test.index') + + @test.route('/test/') + def index(): + return flask.url_for('app_index') + + app.register_blueprint(test) + + with app.test_client() as c: + rv = c.get('/') + assert rv.data == b'/test/' + +def test_empty_url_defaults(): + bp = flask.Blueprint('bp', __name__) + + @bp.route('/', defaults={'page': 1}) + @bp.route('/page/') + def something(page): + return str(page) + + app = flask.Flask(__name__) + app.register_blueprint(bp) + + c = app.test_client() + assert c.get('/').data == b'1' + assert c.get('/page/2').data == b'2' + +def test_route_decorator_custom_endpoint(): + + bp = flask.Blueprint('bp', __name__) + + @bp.route('/foo') + def foo(): + return flask.request.endpoint + + @bp.route('/bar', endpoint='bar') + def foo_bar(): + return flask.request.endpoint + + @bp.route('/bar/123', endpoint='123') + def foo_bar_foo(): + return flask.request.endpoint + + @bp.route('/bar/foo') + def bar_foo(): + return flask.request.endpoint + + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + + @app.route('/') + def index(): + return flask.request.endpoint + + c = app.test_client() + assert c.get('/').data == b'index' + assert c.get('/py/foo').data == b'bp.foo' + assert c.get('/py/bar').data == b'bp.bar' + assert c.get('/py/bar/123').data == b'bp.123' + assert c.get('/py/bar/foo').data == b'bp.bar_foo' + +def test_route_decorator_custom_endpoint_with_dots(): + bp = flask.Blueprint('bp', __name__) + + @bp.route('/foo') + def foo(): + return flask.request.endpoint + + try: + @bp.route('/bar', endpoint='bar.bar') + def foo_bar(): + return flask.request.endpoint + except AssertionError: + pass + else: + raise AssertionError('expected AssertionError not raised') + + try: + @bp.route('/bar/123', endpoint='bar.123') + def foo_bar_foo(): + return flask.request.endpoint + except AssertionError: + pass + else: + raise AssertionError('expected AssertionError not raised') + + def foo_foo_foo(): + pass + + pytest.raises( + AssertionError, + lambda: bp.add_url_rule( + '/bar/123', endpoint='bar.123', view_func=foo_foo_foo + ) + ) + + pytest.raises( + AssertionError, + bp.route('/bar/123', endpoint='bar.123'), + lambda: None + ) + + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + + c = app.test_client() + assert c.get('/py/foo').data == b'bp.foo' + # The rule's didn't actually made it through + rv = c.get('/py/bar') + assert rv.status_code == 404 + rv = c.get('/py/bar/123') + assert rv.status_code == 404 + +def test_template_filter(): + bp = flask.Blueprint('bp', __name__) + @bp.app_template_filter() + def my_reverse(s): + return s[::-1] + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + assert 'my_reverse' in app.jinja_env.filters.keys() + assert app.jinja_env.filters['my_reverse'] == my_reverse + assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba' + +def test_add_template_filter(): + bp = flask.Blueprint('bp', __name__) + def my_reverse(s): + return s[::-1] + bp.add_app_template_filter(my_reverse) + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + assert 'my_reverse' in app.jinja_env.filters.keys() + assert app.jinja_env.filters['my_reverse'] == my_reverse + assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba' + +def test_template_filter_with_name(): + bp = flask.Blueprint('bp', __name__) + @bp.app_template_filter('strrev') + def my_reverse(s): + return s[::-1] + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + assert 'strrev' in app.jinja_env.filters.keys() + assert app.jinja_env.filters['strrev'] == my_reverse + assert app.jinja_env.filters['strrev']('abcd') == 'dcba' + +def test_add_template_filter_with_name(): + bp = flask.Blueprint('bp', __name__) + def my_reverse(s): + return s[::-1] + bp.add_app_template_filter(my_reverse, 'strrev') + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + assert 'strrev' in app.jinja_env.filters.keys() + assert app.jinja_env.filters['strrev'] == my_reverse + assert app.jinja_env.filters['strrev']('abcd') == 'dcba' + +def test_template_filter_with_template(): + bp = flask.Blueprint('bp', __name__) + @bp.app_template_filter() + def super_reverse(s): + return s[::-1] + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + @app.route('/') + def index(): + return flask.render_template('template_filter.html', value='abcd') + rv = app.test_client().get('/') + assert rv.data == b'dcba' + +def test_template_filter_after_route_with_template(): + app = flask.Flask(__name__) + @app.route('/') + def index(): + return flask.render_template('template_filter.html', value='abcd') + bp = flask.Blueprint('bp', __name__) + @bp.app_template_filter() + def super_reverse(s): + return s[::-1] + app.register_blueprint(bp, url_prefix='/py') + rv = app.test_client().get('/') + assert rv.data == b'dcba' + +def test_add_template_filter_with_template(): + bp = flask.Blueprint('bp', __name__) + def super_reverse(s): + return s[::-1] + bp.add_app_template_filter(super_reverse) + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + @app.route('/') + def index(): + return flask.render_template('template_filter.html', value='abcd') + rv = app.test_client().get('/') + assert rv.data == b'dcba' + +def test_template_filter_with_name_and_template(): + bp = flask.Blueprint('bp', __name__) + @bp.app_template_filter('super_reverse') + def my_reverse(s): + return s[::-1] + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + @app.route('/') + def index(): + return flask.render_template('template_filter.html', value='abcd') + rv = app.test_client().get('/') + assert rv.data == b'dcba' + +def test_add_template_filter_with_name_and_template(): + bp = flask.Blueprint('bp', __name__) + def my_reverse(s): + return s[::-1] + bp.add_app_template_filter(my_reverse, 'super_reverse') + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + @app.route('/') + def index(): + return flask.render_template('template_filter.html', value='abcd') + rv = app.test_client().get('/') + assert rv.data == b'dcba' + +def test_template_test(): + bp = flask.Blueprint('bp', __name__) + @bp.app_template_test() + def is_boolean(value): + return isinstance(value, bool) + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + assert 'is_boolean' in app.jinja_env.tests.keys() + assert app.jinja_env.tests['is_boolean'] == is_boolean + assert app.jinja_env.tests['is_boolean'](False) + +def test_add_template_test(): + bp = flask.Blueprint('bp', __name__) + def is_boolean(value): + return isinstance(value, bool) + bp.add_app_template_test(is_boolean) + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + assert 'is_boolean' in app.jinja_env.tests.keys() + assert app.jinja_env.tests['is_boolean'] == is_boolean + assert app.jinja_env.tests['is_boolean'](False) + +def test_template_test_with_name(): + bp = flask.Blueprint('bp', __name__) + @bp.app_template_test('boolean') + def is_boolean(value): + return isinstance(value, bool) + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + assert 'boolean' in app.jinja_env.tests.keys() + assert app.jinja_env.tests['boolean'] == is_boolean + assert app.jinja_env.tests['boolean'](False) + +def test_add_template_test_with_name(): + bp = flask.Blueprint('bp', __name__) + def is_boolean(value): + return isinstance(value, bool) + bp.add_app_template_test(is_boolean, 'boolean') + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + assert 'boolean' in app.jinja_env.tests.keys() + assert app.jinja_env.tests['boolean'] == is_boolean + assert app.jinja_env.tests['boolean'](False) + +def test_template_test_with_template(): + bp = flask.Blueprint('bp', __name__) + @bp.app_template_test() + def boolean(value): + return isinstance(value, bool) + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + @app.route('/') + def index(): + return flask.render_template('template_test.html', value=False) + rv = app.test_client().get('/') + assert b'Success!' in rv.data + +def test_template_test_after_route_with_template(): + app = flask.Flask(__name__) + @app.route('/') + def index(): + return flask.render_template('template_test.html', value=False) + bp = flask.Blueprint('bp', __name__) + @bp.app_template_test() + def boolean(value): + return isinstance(value, bool) + app.register_blueprint(bp, url_prefix='/py') + rv = app.test_client().get('/') + assert b'Success!' in rv.data + +def test_add_template_test_with_template(): + bp = flask.Blueprint('bp', __name__) + def boolean(value): + return isinstance(value, bool) + bp.add_app_template_test(boolean) + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + @app.route('/') + def index(): + return flask.render_template('template_test.html', value=False) + rv = app.test_client().get('/') + assert b'Success!' in rv.data + +def test_template_test_with_name_and_template(): + bp = flask.Blueprint('bp', __name__) + @bp.app_template_test('boolean') + def is_boolean(value): + return isinstance(value, bool) + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + @app.route('/') + def index(): + return flask.render_template('template_test.html', value=False) + rv = app.test_client().get('/') + assert b'Success!' in rv.data + +def test_add_template_test_with_name_and_template(): + bp = flask.Blueprint('bp', __name__) + def is_boolean(value): + return isinstance(value, bool) + bp.add_app_template_test(is_boolean, 'boolean') + app = flask.Flask(__name__) + app.register_blueprint(bp, url_prefix='/py') + @app.route('/') + def index(): + return flask.render_template('template_test.html', value=False) + rv = app.test_client().get('/') + assert b'Success!' in rv.data diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 00000000..7487cb1d --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,184 @@ +# -*- coding: utf-8 -*- +""" + tests.test_config + ~~~~~~~~~~~~~~~~~ + + :copyright: (c) 2014 by the Flask Team, see AUTHORS for more details. + :license: BSD, see LICENSE for more details. +""" + +import pytest + +import os +import flask + + +# config keys used for the TestConfig +TEST_KEY = 'foo' +SECRET_KEY = 'devkey' + + +def common_object_test(app): + assert app.secret_key == 'devkey' + assert app.config['TEST_KEY'] == 'foo' + assert 'TestConfig' not in app.config + + +def test_config_from_file(): + app = flask.Flask(__name__) + app.config.from_pyfile(__file__.rsplit('.', 1)[0] + '.py') + common_object_test(app) + + +def test_config_from_object(): + app = flask.Flask(__name__) + app.config.from_object(__name__) + common_object_test(app) + + +def test_config_from_json(): + app = flask.Flask(__name__) + current_dir = os.path.dirname(os.path.abspath(__file__)) + app.config.from_json(os.path.join(current_dir, 'static', 'config.json')) + common_object_test(app) + + +def test_config_from_mapping(): + app = flask.Flask(__name__) + app.config.from_mapping({ + 'SECRET_KEY': 'devkey', + 'TEST_KEY': 'foo' + }) + common_object_test(app) + + app = flask.Flask(__name__) + app.config.from_mapping([ + ('SECRET_KEY', 'devkey'), + ('TEST_KEY', 'foo') + ]) + common_object_test(app) + + app = flask.Flask(__name__) + app.config.from_mapping( + SECRET_KEY='devkey', + TEST_KEY='foo' + ) + common_object_test(app) + + app = flask.Flask(__name__) + with pytest.raises(TypeError): + app.config.from_mapping( + {}, {} + ) + + +def test_config_from_class(): + class Base(object): + TEST_KEY = 'foo' + + class Test(Base): + SECRET_KEY = 'devkey' + app = flask.Flask(__name__) + app.config.from_object(Test) + common_object_test(app) + + +def test_config_from_envvar(): + env = os.environ + try: + os.environ = {} + app = flask.Flask(__name__) + try: + app.config.from_envvar('FOO_SETTINGS') + except RuntimeError as e: + assert "'FOO_SETTINGS' is not set" in str(e) + else: + assert 0, 'expected exception' + assert not app.config.from_envvar('FOO_SETTINGS', silent=True) + + os.environ = {'FOO_SETTINGS': __file__.rsplit('.', 1)[0] + '.py'} + assert app.config.from_envvar('FOO_SETTINGS') + common_object_test(app) + finally: + os.environ = env + + +def test_config_from_envvar_missing(): + env = os.environ + try: + os.environ = {'FOO_SETTINGS': 'missing.cfg'} + try: + app = flask.Flask(__name__) + app.config.from_envvar('FOO_SETTINGS') + except IOError as e: + msg = str(e) + assert msg.startswith('[Errno 2] Unable to load configuration ' + 'file (No such file or directory):') + assert msg.endswith("missing.cfg'") + else: + assert False, 'expected IOError' + assert not app.config.from_envvar('FOO_SETTINGS', silent=True) + finally: + os.environ = env + + +def test_config_missing(): + app = flask.Flask(__name__) + try: + app.config.from_pyfile('missing.cfg') + except IOError as e: + msg = str(e) + assert msg.startswith('[Errno 2] Unable to load configuration ' + 'file (No such file or directory):') + assert msg.endswith("missing.cfg'") + else: + assert 0, 'expected config' + assert not app.config.from_pyfile('missing.cfg', silent=True) + + +def test_config_missing_json(): + app = flask.Flask(__name__) + try: + app.config.from_json('missing.json') + except IOError as e: + msg = str(e) + assert msg.startswith('[Errno 2] Unable to load configuration ' + 'file (No such file or directory):') + assert msg.endswith("missing.json'") + else: + assert 0, 'expected config' + assert not app.config.from_json('missing.json', silent=True) + + +def test_custom_config_class(): + class Config(flask.Config): + pass + + class Flask(flask.Flask): + config_class = Config + app = Flask(__name__) + assert isinstance(app.config, Config) + app.config.from_object(__name__) + common_object_test(app) + + +def test_session_lifetime(): + app = flask.Flask(__name__) + app.config['PERMANENT_SESSION_LIFETIME'] = 42 + assert app.permanent_session_lifetime.seconds == 42 + + +def test_get_namespace(): + app = flask.Flask(__name__) + app.config['FOO_OPTION_1'] = 'foo option 1' + app.config['FOO_OPTION_2'] = 'foo option 2' + app.config['BAR_STUFF_1'] = 'bar stuff 1' + app.config['BAR_STUFF_2'] = 'bar stuff 2' + foo_options = app.config.get_namespace('FOO_') + assert 2 == len(foo_options) + assert 'foo option 1' == foo_options['option_1'] + assert 'foo option 2' == foo_options['option_2'] + bar_options = app.config.get_namespace('BAR_', lowercase=False) + assert 2 == len(bar_options) + assert 'bar stuff 1' == bar_options['STUFF_1'] + assert 'bar stuff 2' == bar_options['STUFF_2'] diff --git a/tests/test_deprecations.py b/tests/test_deprecations.py new file mode 100644 index 00000000..bbbe10ad --- /dev/null +++ b/tests/test_deprecations.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +""" + tests.deprecations + ~~~~~~~~~~~~~~~~~~ + + Tests deprecation support. Not used currently. + + :copyright: (c) 2014 by Armin Ronacher. + :license: BSD, see LICENSE for more details. +""" diff --git a/tests/test_ext.py b/tests/test_ext.py new file mode 100644 index 00000000..68431ecf --- /dev/null +++ b/tests/test_ext.py @@ -0,0 +1,187 @@ +# -*- coding: utf-8 -*- +""" + tests.ext + ~~~~~~~~~~~~~~~~~~~ + + Tests the extension import thing. + + :copyright: (c) 2014 by Armin Ronacher. + :license: BSD, see LICENSE for more details. +""" + +import sys +import pytest + +try: + from imp import reload as reload_module +except ImportError: + reload_module = reload + +from flask._compat import PY2 + + +@pytest.fixture(autouse=True) +def importhook_setup(monkeypatch, request): + # we clear this out for various reasons. The most important one is + # that a real flaskext could be in there which would disable our + # fake package. Secondly we want to make sure that the flaskext + # import hook does not break on reloading. + for entry, value in list(sys.modules.items()): + if ( + entry.startswith('flask.ext.') or + entry.startswith('flask_') or + entry.startswith('flaskext.') or + entry == 'flaskext' + ) and value is not None: + monkeypatch.delitem(sys.modules, entry) + from flask import ext + reload_module(ext) + + # reloading must not add more hooks + import_hooks = 0 + for item in sys.meta_path: + cls = type(item) + if cls.__module__ == 'flask.exthook' and \ + cls.__name__ == 'ExtensionImporter': + import_hooks += 1 + assert import_hooks == 1 + + def teardown(): + from flask import ext + for key in ext.__dict__: + assert '.' not in key + + request.addfinalizer(teardown) + + +@pytest.fixture +def newext_simple(modules_tmpdir): + x = modules_tmpdir.join('flask_newext_simple.py') + x.write('ext_id = "newext_simple"') + + +@pytest.fixture +def oldext_simple(modules_tmpdir): + flaskext = modules_tmpdir.mkdir('flaskext') + flaskext.join('__init__.py').write('\n') + flaskext.join('oldext_simple.py').write('ext_id = "oldext_simple"') + + +@pytest.fixture +def newext_package(modules_tmpdir): + pkg = modules_tmpdir.mkdir('flask_newext_package') + pkg.join('__init__.py').write('ext_id = "newext_package"') + pkg.join('submodule.py').write('def test_function():\n return 42\n') + + +@pytest.fixture +def oldext_package(modules_tmpdir): + flaskext = modules_tmpdir.mkdir('flaskext') + flaskext.join('__init__.py').write('\n') + oldext = flaskext.mkdir('oldext_package') + oldext.join('__init__.py').write('ext_id = "oldext_package"') + oldext.join('submodule.py').write('def test_function():\n' + ' return 42') + + +@pytest.fixture +def flaskext_broken(modules_tmpdir): + ext = modules_tmpdir.mkdir('flask_broken') + ext.join('b.py').write('\n') + ext.join('__init__.py').write('import flask.ext.broken.b\n' + 'import missing_module') + + +def test_flaskext_new_simple_import_normal(newext_simple): + from flask.ext.newext_simple import ext_id + assert ext_id == 'newext_simple' + + +def test_flaskext_new_simple_import_module(newext_simple): + from flask.ext import newext_simple + assert newext_simple.ext_id == 'newext_simple' + assert newext_simple.__name__ == 'flask_newext_simple' + + +def test_flaskext_new_package_import_normal(newext_package): + from flask.ext.newext_package import ext_id + assert ext_id == 'newext_package' + + +def test_flaskext_new_package_import_module(newext_package): + from flask.ext import newext_package + assert newext_package.ext_id == 'newext_package' + assert newext_package.__name__ == 'flask_newext_package' + + +def test_flaskext_new_package_import_submodule_function(newext_package): + from flask.ext.newext_package.submodule import test_function + assert test_function() == 42 + + +def test_flaskext_new_package_import_submodule(newext_package): + from flask.ext.newext_package import submodule + assert submodule.__name__ == 'flask_newext_package.submodule' + assert submodule.test_function() == 42 + + +def test_flaskext_old_simple_import_normal(oldext_simple): + from flask.ext.oldext_simple import ext_id + assert ext_id == 'oldext_simple' + + +def test_flaskext_old_simple_import_module(oldext_simple): + from flask.ext import oldext_simple + assert oldext_simple.ext_id == 'oldext_simple' + assert oldext_simple.__name__ == 'flaskext.oldext_simple' + + +def test_flaskext_old_package_import_normal(oldext_package): + from flask.ext.oldext_package import ext_id + assert ext_id == 'oldext_package' + + +def test_flaskext_old_package_import_module(oldext_package): + from flask.ext import oldext_package + assert oldext_package.ext_id == 'oldext_package' + assert oldext_package.__name__ == 'flaskext.oldext_package' + + +def test_flaskext_old_package_import_submodule(oldext_package): + from flask.ext.oldext_package import submodule + assert submodule.__name__ == 'flaskext.oldext_package.submodule' + assert submodule.test_function() == 42 + + +def test_flaskext_old_package_import_submodule_function(oldext_package): + from flask.ext.oldext_package.submodule import test_function + assert test_function() == 42 + + +def test_flaskext_broken_package_no_module_caching(flaskext_broken): + for x in range(2): + with pytest.raises(ImportError): + import flask.ext.broken + + +def test_no_error_swallowing(flaskext_broken): + with pytest.raises(ImportError) as excinfo: + import flask.ext.broken + + assert excinfo.type is ImportError + if PY2: + message = 'No module named missing_module' + else: + message = 'No module named \'missing_module\'' + assert str(excinfo.value) == message + assert excinfo.tb.tb_frame.f_globals is globals() + + # reraise() adds a second frame so we need to skip that one too. + # On PY3 we even have another one :( + next = excinfo.tb.tb_next.tb_next + if not PY2: + next = next.tb_next + + import os.path + assert os.path.join('flask_broken', '__init__.py') in \ + next.tb_frame.f_code.co_filename diff --git a/flask/testsuite/helpers.py b/tests/test_helpers.py similarity index 71% rename from flask/testsuite/helpers.py rename to tests/test_helpers.py index e08a2283..e9738f76 100644 --- a/flask/testsuite/helpers.py +++ b/tests/test_helpers.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- """ - flask.testsuite.helpers + tests.helpers ~~~~~~~~~~~~~~~~~~~~~~~ Various helpers. @@ -9,11 +9,11 @@ :license: BSD, see LICENSE for more details. """ +import pytest + import os import flask -import unittest from logging import StreamHandler -from flask.testsuite import FlaskTestCase, catch_warnings, catch_stderr from werkzeug.http import parse_cache_control_header, parse_options_header from flask._compat import StringIO, text_type @@ -27,7 +27,7 @@ def has_encoding(name): return False -class JSONTestCase(FlaskTestCase): +class TestJSON(object): def test_json_bad_requests(self): app = flask.Flask(__name__) @@ -36,7 +36,7 @@ class JSONTestCase(FlaskTestCase): return flask.jsonify(foo=text_type(flask.request.get_json())) c = app.test_client() rv = c.post('/json', data='malformed', content_type='application/json') - self.assert_equal(rv.status_code, 400) + assert rv.status_code == 400 def test_json_custom_mimetypes(self): app = flask.Flask(__name__) @@ -45,7 +45,7 @@ class JSONTestCase(FlaskTestCase): return flask.request.get_json() c = app.test_client() rv = c.post('/json', data='"foo"', content_type='application/x+json') - self.assert_equal(rv.data, b'foo') + assert rv.data == b'foo' def test_json_body_encoding(self): app = flask.Flask(__name__) @@ -57,7 +57,7 @@ class JSONTestCase(FlaskTestCase): c = app.test_client() resp = c.get('/', data=u'"Hällo Wörld"'.encode('iso-8859-15'), content_type='application/json; charset=iso-8859-15') - self.assert_equal(resp.data, u'Hällo Wörld'.encode('utf-8')) + assert resp.data == u'Hällo Wörld'.encode('utf-8') def test_jsonify(self): d = dict(a=23, b=42, c=[1, 2, 3]) @@ -71,8 +71,8 @@ class JSONTestCase(FlaskTestCase): c = app.test_client() for url in '/kw', '/dict': rv = c.get(url) - self.assert_equal(rv.mimetype, 'application/json') - self.assert_equal(flask.json.loads(rv.data), d) + assert rv.mimetype == 'application/json' + assert flask.json.loads(rv.data) == d def test_json_as_unicode(self): app = flask.Flask(__name__) @@ -80,12 +80,12 @@ class JSONTestCase(FlaskTestCase): app.config['JSON_AS_ASCII'] = True with app.app_context(): rv = flask.json.dumps(u'\N{SNOWMAN}') - self.assert_equal(rv, '"\\u2603"') + assert rv == '"\\u2603"' app.config['JSON_AS_ASCII'] = False with app.app_context(): rv = flask.json.dumps(u'\N{SNOWMAN}') - self.assert_equal(rv, u'"\u2603"') + assert rv == u'"\u2603"' def test_json_attr(self): app = flask.Flask(__name__) @@ -96,29 +96,28 @@ class JSONTestCase(FlaskTestCase): c = app.test_client() rv = c.post('/add', data=flask.json.dumps({'a': 1, 'b': 2}), content_type='application/json') - self.assert_equal(rv.data, b'3') + assert rv.data == b'3' def test_template_escaping(self): app = flask.Flask(__name__) render = flask.render_template_string with app.test_request_context(): rv = flask.json.htmlsafe_dumps('') - self.assert_equal(rv, u'"\\u003c/script\\u003e"') - self.assert_equal(type(rv), text_type) + assert rv == u'"\\u003c/script\\u003e"' + assert type(rv) == text_type rv = render('{{ ""|tojson }}') - self.assert_equal(rv, '"\\u003c/script\\u003e"') + assert rv == '"\\u003c/script\\u003e"' rv = render('{{ "<\0/script>"|tojson }}') - self.assert_equal(rv, '"\\u003c\\u0000/script\\u003e"') + assert rv == '"\\u003c\\u0000/script\\u003e"' rv = render('{{ "