/
test_config.py
254 lines (197 loc) · 7.51 KB
/
test_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import json
import os
import textwrap
from datetime import timedelta
import pytest
import flask
# config keys used for the TestConfig
TEST_KEY = "foo"
SECRET_KEY = "config"
def common_object_test(app):
assert app.secret_key == "config"
assert app.config["TEST_KEY"] == "foo"
assert "TestConfig" not in app.config
def test_config_from_pyfile():
app = flask.Flask(__name__)
app.config.from_pyfile(f"{__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_file():
app = flask.Flask(__name__)
current_dir = os.path.dirname(os.path.abspath(__file__))
app.config.from_file(os.path.join(current_dir, "static", "config.json"), json.load)
common_object_test(app)
def test_from_prefixed_env(monkeypatch):
monkeypatch.setenv("FLASK_STRING", "value")
monkeypatch.setenv("FLASK_BOOL", "true")
monkeypatch.setenv("FLASK_INT", "1")
monkeypatch.setenv("FLASK_FLOAT", "1.2")
monkeypatch.setenv("FLASK_LIST", "[1, 2]")
monkeypatch.setenv("FLASK_DICT", '{"k": "v"}')
monkeypatch.setenv("NOT_FLASK_OTHER", "other")
app = flask.Flask(__name__)
app.config.from_prefixed_env()
assert app.config["STRING"] == "value"
assert app.config["BOOL"] is True
assert app.config["INT"] == 1
assert app.config["FLOAT"] == 1.2
assert app.config["LIST"] == [1, 2]
assert app.config["DICT"] == {"k": "v"}
assert "OTHER" not in app.config
def test_from_prefixed_env_custom_prefix(monkeypatch):
monkeypatch.setenv("FLASK_A", "a")
monkeypatch.setenv("NOT_FLASK_A", "b")
app = flask.Flask(__name__)
app.config.from_prefixed_env("NOT_FLASK")
assert app.config["A"] == "b"
def test_from_prefixed_env_nested(monkeypatch):
monkeypatch.setenv("FLASK_EXIST__ok", "other")
monkeypatch.setenv("FLASK_EXIST__inner__ik", "2")
monkeypatch.setenv("FLASK_EXIST__new__more", '{"k": false}')
monkeypatch.setenv("FLASK_NEW__K", "v")
app = flask.Flask(__name__)
app.config["EXIST"] = {"ok": "value", "flag": True, "inner": {"ik": 1}}
app.config.from_prefixed_env()
if os.name != "nt":
assert app.config["EXIST"] == {
"ok": "other",
"flag": True,
"inner": {"ik": 2},
"new": {"more": {"k": False}},
}
else:
# Windows env var keys are always uppercase.
assert app.config["EXIST"] == {
"ok": "value",
"OK": "other",
"flag": True,
"inner": {"ik": 1},
"INNER": {"IK": 2},
"NEW": {"MORE": {"k": False}},
}
assert app.config["NEW"] == {"K": "v"}
def test_config_from_mapping():
app = flask.Flask(__name__)
app.config.from_mapping({"SECRET_KEY": "config", "TEST_KEY": "foo"})
common_object_test(app)
app = flask.Flask(__name__)
app.config.from_mapping([("SECRET_KEY", "config"), ("TEST_KEY", "foo")])
common_object_test(app)
app = flask.Flask(__name__)
app.config.from_mapping(SECRET_KEY="config", 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:
TEST_KEY = "foo"
class Test(Base):
SECRET_KEY = "config"
app = flask.Flask(__name__)
app.config.from_object(Test)
common_object_test(app)
def test_config_from_envvar(monkeypatch):
monkeypatch.setattr("os.environ", {})
app = flask.Flask(__name__)
with pytest.raises(RuntimeError) as e:
app.config.from_envvar("FOO_SETTINGS")
assert "'FOO_SETTINGS' is not set" in str(e.value)
assert not app.config.from_envvar("FOO_SETTINGS", silent=True)
monkeypatch.setattr(
"os.environ", {"FOO_SETTINGS": f"{__file__.rsplit('.', 1)[0]}.py"}
)
assert app.config.from_envvar("FOO_SETTINGS")
common_object_test(app)
def test_config_from_envvar_missing(monkeypatch):
monkeypatch.setattr("os.environ", {"FOO_SETTINGS": "missing.cfg"})
app = flask.Flask(__name__)
with pytest.raises(IOError) as e:
app.config.from_envvar("FOO_SETTINGS")
msg = str(e.value)
assert msg.startswith(
"[Errno 2] Unable to load configuration file (No such file or directory):"
)
assert msg.endswith("missing.cfg'")
assert not app.config.from_envvar("FOO_SETTINGS", silent=True)
def test_config_missing():
app = flask.Flask(__name__)
with pytest.raises(IOError) as e:
app.config.from_pyfile("missing.cfg")
msg = str(e.value)
assert msg.startswith(
"[Errno 2] Unable to load configuration file (No such file or directory):"
)
assert msg.endswith("missing.cfg'")
assert not app.config.from_pyfile("missing.cfg", silent=True)
def test_config_missing_file():
app = flask.Flask(__name__)
with pytest.raises(IOError) as e:
app.config.from_file("missing.json", load=json.load)
msg = str(e.value)
assert msg.startswith(
"[Errno 2] Unable to load configuration file (No such file or directory):"
)
assert msg.endswith("missing.json'")
assert not app.config.from_file("missing.json", load=json.load, 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_send_file_max_age():
app = flask.Flask(__name__)
app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 3600
assert app.send_file_max_age_default.seconds == 3600
app.config["SEND_FILE_MAX_AGE_DEFAULT"] = timedelta(hours=2)
assert app.send_file_max_age_default.seconds == 7200
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"]
foo_options = app.config.get_namespace("FOO_", trim_namespace=False)
assert 2 == len(foo_options)
assert "foo option 1" == foo_options["foo_option_1"]
assert "foo option 2" == foo_options["foo_option_2"]
bar_options = app.config.get_namespace(
"BAR_", lowercase=False, trim_namespace=False
)
assert 2 == len(bar_options)
assert "bar stuff 1" == bar_options["BAR_STUFF_1"]
assert "bar stuff 2" == bar_options["BAR_STUFF_2"]
@pytest.mark.parametrize("encoding", ["utf-8", "iso-8859-15", "latin-1"])
def test_from_pyfile_weird_encoding(tmpdir, encoding):
f = tmpdir.join("my_config.py")
f.write_binary(
textwrap.dedent(
f"""
# -*- coding: {encoding} -*-
TEST_VALUE = "föö"
"""
).encode(encoding)
)
app = flask.Flask(__name__)
app.config.from_pyfile(str(f))
value = app.config["TEST_VALUE"]
assert value == "föö"