mirror of https://github.com/mitsuhiko/flask.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
385 lines
14 KiB
385 lines
14 KiB
13 years ago
|
# -*- coding: utf-8 -*-
|
||
|
"""
|
||
10 years ago
|
tests.config
|
||
13 years ago
|
~~~~~~~~~~~~~~~~~~~~~~
|
||
|
|
||
|
Configuration and instances.
|
||
|
|
||
11 years ago
|
:copyright: (c) 2014 by Armin Ronacher.
|
||
13 years ago
|
:license: BSD, see LICENSE for more details.
|
||
|
"""
|
||
13 years ago
|
|
||
13 years ago
|
import os
|
||
|
import sys
|
||
|
import flask
|
||
13 years ago
|
import pkgutil
|
||
13 years ago
|
import unittest
|
||
13 years ago
|
from contextlib import contextmanager
|
||
10 years ago
|
from tests import FlaskTestCase
|
||
11 years ago
|
from flask._compat import PY2
|
||
13 years ago
|
|
||
|
|
||
|
# config keys used for the ConfigTestCase
|
||
|
TEST_KEY = 'foo'
|
||
|
SECRET_KEY = 'devkey'
|
||
|
|
||
|
|
||
|
class ConfigTestCase(FlaskTestCase):
|
||
|
|
||
|
def common_object_test(self, app):
|
||
13 years ago
|
self.assert_equal(app.secret_key, 'devkey')
|
||
|
self.assert_equal(app.config['TEST_KEY'], 'foo')
|
||
12 years ago
|
self.assert_not_in('ConfigTestCase', app.config)
|
||
13 years ago
|
|
||
|
def test_config_from_file(self):
|
||
|
app = flask.Flask(__name__)
|
||
13 years ago
|
app.config.from_pyfile(__file__.rsplit('.', 1)[0] + '.py')
|
||
13 years ago
|
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)
|
||
|
|
||
11 years ago
|
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)
|
||
|
|
||
10 years ago
|
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(
|
||
|
{}, {}
|
||
|
)
|
||
|
|
||
13 years ago
|
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')
|
||
12 years ago
|
except RuntimeError as e:
|
||
12 years ago
|
self.assert_true("'FOO_SETTINGS' is not set" in str(e))
|
||
13 years ago
|
else:
|
||
12 years ago
|
self.assert_true(0, 'expected exception')
|
||
12 years ago
|
self.assert_false(app.config.from_envvar('FOO_SETTINGS', silent=True))
|
||
13 years ago
|
|
||
13 years ago
|
os.environ = {'FOO_SETTINGS': __file__.rsplit('.', 1)[0] + '.py'}
|
||
12 years ago
|
self.assert_true(app.config.from_envvar('FOO_SETTINGS'))
|
||
13 years ago
|
self.common_object_test(app)
|
||
|
finally:
|
||
|
os.environ = env
|
||
|
|
||
13 years ago
|
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')
|
||
12 years ago
|
except IOError as e:
|
||
13 years ago
|
msg = str(e)
|
||
12 years ago
|
self.assert_true(msg.startswith('[Errno 2] Unable to load configuration '
|
||
13 years ago
|
'file (No such file or directory):'))
|
||
12 years ago
|
self.assert_true(msg.endswith("missing.cfg'"))
|
||
13 years ago
|
else:
|
||
13 years ago
|
self.fail('expected IOError')
|
||
|
self.assertFalse(app.config.from_envvar('FOO_SETTINGS', silent=True))
|
||
13 years ago
|
finally:
|
||
|
os.environ = env
|
||
|
|
||
13 years ago
|
def test_config_missing(self):
|
||
|
app = flask.Flask(__name__)
|
||
|
try:
|
||
|
app.config.from_pyfile('missing.cfg')
|
||
12 years ago
|
except IOError as e:
|
||
13 years ago
|
msg = str(e)
|
||
12 years ago
|
self.assert_true(msg.startswith('[Errno 2] Unable to load configuration '
|
||
13 years ago
|
'file (No such file or directory):'))
|
||
12 years ago
|
self.assert_true(msg.endswith("missing.cfg'"))
|
||
13 years ago
|
else:
|
||
12 years ago
|
self.assert_true(0, 'expected config')
|
||
12 years ago
|
self.assert_false(app.config.from_pyfile('missing.cfg', silent=True))
|
||
13 years ago
|
|
||
11 years ago
|
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))
|
||
|
|
||
12 years ago
|
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)
|
||
|
|
||
13 years ago
|
def test_session_lifetime(self):
|
||
|
app = flask.Flask(__name__)
|
||
|
app.config['PERMANENT_SESSION_LIFETIME'] = 42
|
||
13 years ago
|
self.assert_equal(app.permanent_session_lifetime.seconds, 42)
|
||
13 years ago
|
|
||
11 years ago
|
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'])
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
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
|
||
12 years ago
|
raise AttributeError(msg)
|
||
13 years ago
|
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
|
||
|
|
||
|
|
||
13 years ago
|
class InstanceTestCase(FlaskTestCase):
|
||
|
|
||
|
def test_explicit_instance_paths(self):
|
||
|
here = os.path.abspath(os.path.dirname(__file__))
|
||
|
try:
|
||
|
flask.Flask(__name__, instance_path='instance')
|
||
12 years ago
|
except ValueError as e:
|
||
12 years ago
|
self.assert_in('must be absolute', str(e))
|
||
13 years ago
|
else:
|
||
|
self.fail('Expected value error')
|
||
|
|
||
|
app = flask.Flask(__name__, instance_path=here)
|
||
|
self.assert_equal(app.instance_path, here)
|
||
|
|
||
13 years ago
|
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']
|
||
|
|
||
13 years ago
|
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):
|
||
13 years ago
|
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)
|
||
13 years ago
|
try:
|
||
13 years ago
|
import site_app
|
||
|
self.assert_equal(site_app.app.instance_path,
|
||
|
os.path.join(expected_prefix, 'var',
|
||
|
'site_app-instance'))
|
||
13 years ago
|
finally:
|
||
13 years ago
|
sys.prefix = real_prefix
|
||
|
sys.path.remove(site_packages)
|
||
|
if 'site_app' in sys.modules:
|
||
|
del sys.modules['site_app']
|
||
13 years ago
|
|
||
13 years ago
|
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']
|
||
|
|
||
13 years ago
|
def test_installed_package_paths(self):
|
||
13 years ago
|
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)
|
||
13 years ago
|
try:
|
||
13 years ago
|
import installed_package
|
||
|
self.assert_equal(installed_package.app.instance_path,
|
||
|
os.path.join(expected_prefix, 'var',
|
||
|
'installed_package-instance'))
|
||
13 years ago
|
finally:
|
||
13 years ago
|
sys.prefix = real_prefix
|
||
|
sys.path.remove(installed_path)
|
||
|
if 'installed_package' in sys.modules:
|
||
|
del sys.modules['installed_package']
|
||
|
|
||
13 years ago
|
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']
|
||
|
|
||
13 years ago
|
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)
|
||
13 years ago
|
try:
|
||
13 years ago
|
import site_package
|
||
|
self.assert_equal(site_package.app.instance_path,
|
||
|
os.path.join(expected_prefix, 'var',
|
||
|
'site_package-instance'))
|
||
13 years ago
|
finally:
|
||
13 years ago
|
sys.prefix = real_prefix
|
||
|
sys.path.remove(site_packages)
|
||
|
if 'site_package' in sys.modules:
|
||
|
del sys.modules['site_package']
|
||
13 years ago
|
|
||
13 years ago
|
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']
|
||
|
|
||
13 years ago
|
def test_egg_installed_paths(self):
|
||
13 years ago
|
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)
|
||
13 years ago
|
try:
|
||
13 years ago
|
import site_egg # in SiteEgg.egg
|
||
|
self.assert_equal(site_egg.app.instance_path,
|
||
|
os.path.join(expected_prefix, 'var',
|
||
|
'site_egg-instance'))
|
||
13 years ago
|
finally:
|
||
13 years ago
|
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']
|
||
13 years ago
|
|
||
11 years ago
|
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()
|
||
|
|
||
13 years ago
|
|
||
|
def suite():
|
||
|
suite = unittest.TestSuite()
|
||
|
suite.addTest(unittest.makeSuite(ConfigTestCase))
|
||
|
suite.addTest(unittest.makeSuite(InstanceTestCase))
|
||
|
return suite
|