test_config.py
This file
Path
tests/test_config.py
209 lines · 6.0 KiB · Python
Last changed by
Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
History of this fileTake it
1# -*- coding: utf-8 -*-2"""3 tests.test_config4 ~~~~~~~~~~~~~~~~~56 :copyright: © 2010 by the Pallets team.7 :license: BSD, see LICENSE for more details.8"""910from datetime import timedelta11import os12import textwrap1314import flask15from flask._compat import PY216import pytest171819# config keys used for the TestConfig20TEST_KEY = "foo"21SECRET_KEY = "config"222324def common_object_test(app):25 assert app.secret_key == "config"26 assert app.config["TEST_KEY"] == "foo"27 assert "TestConfig" not in app.config282930def test_config_from_file():31 app = flask.Flask(__name__)32 app.config.from_pyfile(__file__.rsplit(".", 1)[0] + ".py")33 common_object_test(app)343536def test_config_from_object():37 app = flask.Flask(__name__)38 app.config.from_object(__name__)39 common_object_test(app)404142def test_config_from_json():43 app = flask.Flask(__name__)44 current_dir = os.path.dirname(os.path.abspath(__file__))45 app.config.from_json(os.path.join(current_dir, "static", "config.json"))46 common_object_test(app)474849def test_config_from_mapping():50 app = flask.Flask(__name__)51 app.config.from_mapping({"SECRET_KEY": "config", "TEST_KEY": "foo"})52 common_object_test(app)5354 app = flask.Flask(__name__)55 app.config.from_mapping([("SECRET_KEY", "config"), ("TEST_KEY", "foo")])56 common_object_test(app)5758 app = flask.Flask(__name__)59 app.config.from_mapping(SECRET_KEY="config", TEST_KEY="foo")60 common_object_test(app)6162 app = flask.Flask(__name__)63 with pytest.raises(TypeError):64 app.config.from_mapping({}, {})656667def test_config_from_class():68 class Base(object):69 TEST_KEY = "foo"7071 class Test(Base):72 SECRET_KEY = "config"7374 app = flask.Flask(__name__)75 app.config.from_object(Test)76 common_object_test(app)777879def test_config_from_envvar():80 env = os.environ81 try:82 os.environ = {}83 app = flask.Flask(__name__)84 with pytest.raises(RuntimeError) as e:85 app.config.from_envvar("FOO_SETTINGS")86 assert "'FOO_SETTINGS' is not set" in str(e.value)87 assert not app.config.from_envvar("FOO_SETTINGS", silent=True)8889 os.environ = {"FOO_SETTINGS": __file__.rsplit(".", 1)[0] + ".py"}90 assert app.config.from_envvar("FOO_SETTINGS")91 common_object_test(app)92 finally:93 os.environ = env949596def test_config_from_envvar_missing():97 env = os.environ98 try:99 os.environ = {"FOO_SETTINGS": "missing.cfg"}100 with pytest.raises(IOError) as e:101 app = flask.Flask(__name__)102 app.config.from_envvar("FOO_SETTINGS")103 msg = str(e.value)104 assert msg.startswith(105 "[Errno 2] Unable to load configuration "106 "file (No such file or directory):"107 )108 assert msg.endswith("missing.cfg'")109 assert not app.config.from_envvar("FOO_SETTINGS", silent=True)110 finally:111 os.environ = env112113114def test_config_missing():115 app = flask.Flask(__name__)116 with pytest.raises(IOError) as e:117 app.config.from_pyfile("missing.cfg")118 msg = str(e.value)119 assert msg.startswith(120 "[Errno 2] Unable to load configuration " "file (No such file or directory):"121 )122 assert msg.endswith("missing.cfg'")123 assert not app.config.from_pyfile("missing.cfg", silent=True)124125126def test_config_missing_json():127 app = flask.Flask(__name__)128 with pytest.raises(IOError) as e:129 app.config.from_json("missing.json")130 msg = str(e.value)131 assert msg.startswith(132 "[Errno 2] Unable to load configuration " "file (No such file or directory):"133 )134 assert msg.endswith("missing.json'")135 assert not app.config.from_json("missing.json", silent=True)136137138def test_custom_config_class():139 class Config(flask.Config):140 pass141142 class Flask(flask.Flask):143 config_class = Config144145 app = Flask(__name__)146 assert isinstance(app.config, Config)147 app.config.from_object(__name__)148 common_object_test(app)149150151def test_session_lifetime():152 app = flask.Flask(__name__)153 app.config["PERMANENT_SESSION_LIFETIME"] = 42154 assert app.permanent_session_lifetime.seconds == 42155156157def test_send_file_max_age():158 app = flask.Flask(__name__)159 app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 3600160 assert app.send_file_max_age_default.seconds == 3600161 app.config["SEND_FILE_MAX_AGE_DEFAULT"] = timedelta(hours=2)162 assert app.send_file_max_age_default.seconds == 7200163164165def test_get_namespace():166 app = flask.Flask(__name__)167 app.config["FOO_OPTION_1"] = "foo option 1"168 app.config["FOO_OPTION_2"] = "foo option 2"169 app.config["BAR_STUFF_1"] = "bar stuff 1"170 app.config["BAR_STUFF_2"] = "bar stuff 2"171 foo_options = app.config.get_namespace("FOO_")172 assert 2 == len(foo_options)173 assert "foo option 1" == foo_options["option_1"]174 assert "foo option 2" == foo_options["option_2"]175 bar_options = app.config.get_namespace("BAR_", lowercase=False)176 assert 2 == len(bar_options)177 assert "bar stuff 1" == bar_options["STUFF_1"]178 assert "bar stuff 2" == bar_options["STUFF_2"]179 foo_options = app.config.get_namespace("FOO_", trim_namespace=False)180 assert 2 == len(foo_options)181 assert "foo option 1" == foo_options["foo_option_1"]182 assert "foo option 2" == foo_options["foo_option_2"]183 bar_options = app.config.get_namespace(184 "BAR_", lowercase=False, trim_namespace=False185 )186 assert 2 == len(bar_options)187 assert "bar stuff 1" == bar_options["BAR_STUFF_1"]188 assert "bar stuff 2" == bar_options["BAR_STUFF_2"]189190191@pytest.mark.parametrize("encoding", ["utf-8", "iso-8859-15", "latin-1"])192def test_from_pyfile_weird_encoding(tmpdir, encoding):193 f = tmpdir.join("my_config.py")194 f.write_binary(195 textwrap.dedent(196 u"""197 # -*- coding: {0} -*-198 TEST_VALUE = "föö"199 """.format(200 encoding201 )202 ).encode(encoding)203 )204 app = flask.Flask(__name__)205 app.config.from_pyfile(str(f))206 value = app.config["TEST_VALUE"]207 if PY2:208 value = value.decode(encoding)209 assert value == u"föö"