-
Notifications
You must be signed in to change notification settings - Fork 606
/
Copy pathtest_config.py
352 lines (296 loc) · 12.4 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import os
import unittest
from argparse import Namespace
from tempfile import NamedTemporaryFile
import tabpy
from tabpy.tabpy_server.app.util import validate_cert
from tabpy.tabpy_server.app.app import TabPyApp
from unittest.mock import patch
class TestConfigEnvironmentCalls(unittest.TestCase):
def test_config_file_does_not_exist(self):
app = TabPyApp("/folder_does_not_exit/file_does_not_exist.conf")
self.assertEqual(app.settings["port"], 9004)
self.assertEqual(
app.settings["server_version"], open("tabpy/VERSION").read().strip()
)
self.assertEqual(app.settings["transfer_protocol"], "http")
self.assertTrue("certificate_file" not in app.settings)
self.assertTrue("key_file" not in app.settings)
self.assertEqual(app.settings["log_request_context"], False)
self.assertEqual(app.settings["evaluate_timeout"], 30)
@patch(
"tabpy.tabpy_server.app.app.TabPyApp._parse_cli_arguments",
return_value=Namespace(config=None),
)
@patch("tabpy.tabpy_server.app.app.TabPyState")
@patch("tabpy.tabpy_server.app.app._get_state_from_file")
@patch("tabpy.tabpy_server.app.app.PythonServiceHandler")
@patch("tabpy.tabpy_server.app.app.os.path.exists", return_value=True)
@patch("tabpy.tabpy_server.app.app.os")
def test_no_config_file(
self,
mock_os,
mock_path_exists,
mock_psws,
mock_management_util,
mock_tabpy_state,
mock_parse_arguments,
):
pkg_path = os.path.dirname(tabpy.__file__)
obj_path = os.path.join(pkg_path, "tmp", "query_objects")
state_path = os.path.join(pkg_path, "tabpy_server")
mock_os.environ = {
"TABPY_PORT": "9004",
"TABPY_QUERY_OBJECT_PATH": obj_path,
"TABPY_STATE_PATH": state_path,
}
TabPyApp(None)
self.assertEqual(len(mock_psws.mock_calls), 1)
self.assertEqual(len(mock_tabpy_state.mock_calls), 1)
self.assertEqual(len(mock_path_exists.mock_calls), 1)
self.assertTrue(len(mock_management_util.mock_calls) > 0)
mock_os.makedirs.assert_not_called()
@patch(
"tabpy.tabpy_server.app.app.TabPyApp._parse_cli_arguments",
return_value=Namespace(config=None),
)
@patch("tabpy.tabpy_server.app.app.TabPyState")
@patch("tabpy.tabpy_server.app.app._get_state_from_file")
@patch("tabpy.tabpy_server.app.app.PythonServiceHandler")
@patch("tabpy.tabpy_server.app.app.os.path.exists", return_value=False)
@patch("tabpy.tabpy_server.app.app.os")
def test_no_state_ini_file_or_state_dir(
self,
mock_os,
mock_path_exists,
mock_psws,
mock_management_util,
mock_tabpy_state,
mock_parse_arguments,
):
TabPyApp(None)
self.assertEqual(len(mock_os.makedirs.mock_calls), 1)
class TestPartialConfigFile(unittest.TestCase):
def setUp(self):
self.config_file = NamedTemporaryFile(delete=False)
def tearDown(self):
os.remove(self.config_file.name)
self.config_file = None
@patch("tabpy.tabpy_server.app.app.TabPyApp._parse_cli_arguments")
@patch("tabpy.tabpy_server.app.app.TabPyState")
@patch("tabpy.tabpy_server.app.app._get_state_from_file")
@patch("tabpy.tabpy_server.app.app.PythonServiceHandler")
@patch("tabpy.tabpy_server.app.app.os.path.exists", return_value=True)
@patch("tabpy.tabpy_server.app.app.os")
def test_config_file_present(
self,
mock_os,
mock_path_exists,
mock_psws,
mock_management_util,
mock_tabpy_state,
mock_parse_arguments,
):
self.assertTrue(self.config_file is not None)
config_file = self.config_file
config_file.write(
"[TabPy]\n"
"TABPY_QUERY_OBJECT_PATH = foo\n"
"TABPY_STATE_PATH = bar\n".encode()
)
config_file.close()
mock_parse_arguments.return_value = Namespace(config=config_file.name)
mock_os.path.realpath.return_value = "bar"
mock_os.environ = {"TABPY_PORT": "1234"}
app = TabPyApp(config_file.name)
self.assertEqual(app.settings["port"], "1234")
self.assertEqual(
app.settings["server_version"], open("tabpy/VERSION").read().strip()
)
self.assertEqual(app.settings["upload_dir"], "foo")
self.assertEqual(app.settings["state_file_path"], "bar")
self.assertEqual(app.settings["transfer_protocol"], "http")
self.assertTrue("certificate_file" not in app.settings)
self.assertTrue("key_file" not in app.settings)
self.assertEqual(app.settings["log_request_context"], False)
self.assertEqual(app.settings["evaluate_timeout"], 30)
@patch("tabpy.tabpy_server.app.app.os.path.exists", return_value=True)
@patch("tabpy.tabpy_server.app.app._get_state_from_file")
@patch("tabpy.tabpy_server.app.app.TabPyState")
def test_custom_evaluate_timeout_valid(
self, mock_state, mock_get_state_from_file, mock_path_exists
):
self.assertTrue(self.config_file is not None)
config_file = self.config_file
config_file.write("[TabPy]\n" "TABPY_EVALUATE_TIMEOUT = 1996".encode())
config_file.close()
app = TabPyApp(self.config_file.name)
self.assertEqual(app.settings["evaluate_timeout"], 1996.0)
@patch("tabpy.tabpy_server.app.app.os.path.exists", return_value=True)
@patch("tabpy.tabpy_server.app.app._get_state_from_file")
@patch("tabpy.tabpy_server.app.app.TabPyState")
def test_custom_evaluate_timeout_invalid(
self, mock_state, mock_get_state_from_file, mock_path_exists
):
self.assertTrue(self.config_file is not None)
config_file = self.config_file
config_file.write(
"[TabPy]\n" 'TABPY_EVALUATE_TIMEOUT = "im not a float"'.encode()
)
config_file.close()
app = TabPyApp(self.config_file.name)
self.assertEqual(app.settings["evaluate_timeout"], 30.0)
@patch("tabpy.tabpy_server.app.app.os")
@patch("tabpy.tabpy_server.app.app.os.path.exists", return_value=True)
@patch("tabpy.tabpy_server.app.app._get_state_from_file")
@patch("tabpy.tabpy_server.app.app.TabPyState")
def test_env_variables_in_config(
self, mock_state, mock_get_state, mock_path_exists, mock_os
):
mock_os.environ = {"foo": "baz"}
config_file = self.config_file
config_file.write("[TabPy]\n" "TABPY_PORT = %(foo)sbar".encode())
config_file.close()
app = TabPyApp(self.config_file.name)
self.assertEqual(app.settings["port"], "bazbar")
class TestTransferProtocolValidation(unittest.TestCase):
def assertTabPyAppRaisesRuntimeError(self, expected_message):
with self.assertRaises(RuntimeError) as err:
TabPyApp(self.fp.name)
self.assertEqual(err.exception.args[0], expected_message)
@staticmethod
def mock_isfile(target_file, existing_files):
if target_file in existing_files:
return True
return False
@staticmethod
def raise_attribute_error():
raise AttributeError()
def __init__(self, *args, **kwargs):
super(TestTransferProtocolValidation, self).__init__(*args, **kwargs)
self.fp = None
def setUp(self):
self.fp = NamedTemporaryFile(mode="w+t", delete=False)
def tearDown(self):
os.remove(self.fp.name)
self.fp = None
def test_invalid_protocol(self):
self.fp.write("[TabPy]\n" "TABPY_TRANSFER_PROTOCOL = gopher")
self.fp.close()
self.assertTabPyAppRaisesRuntimeError("Unsupported transfer protocol: gopher")
def test_http(self):
self.fp.write("[TabPy]\n" "TABPY_TRANSFER_PROTOCOL = http")
self.fp.close()
app = TabPyApp(self.fp.name)
self.assertEqual(app.settings["transfer_protocol"], "http")
def test_https_without_cert_and_key(self):
self.fp.write("[TabPy]\n" "TABPY_TRANSFER_PROTOCOL = https")
self.fp.close()
self.assertTabPyAppRaisesRuntimeError(
"Error using HTTPS: The paramete"
"r(s) TABPY_CERTIFICATE_FILE and"
" TABPY_KEY_FILE must be set."
)
def test_https_without_cert(self):
self.fp.write(
"[TabPy]\n" "TABPY_TRANSFER_PROTOCOL = https\n" "TABPY_KEY_FILE = foo"
)
self.fp.close()
self.assertTabPyAppRaisesRuntimeError(
"Error using HTTPS: The parameter(s) TABPY_CERTIFICATE_FILE must " "be set."
)
def test_https_without_key(self):
self.fp.write(
"[TabPy]\n"
"TABPY_TRANSFER_PROTOCOL = https\n"
"TABPY_CERTIFICATE_FILE = foo"
)
self.fp.close()
self.assertTabPyAppRaisesRuntimeError(
"Error using HTTPS: The parameter(s) TABPY_KEY_FILE must be set."
)
@patch("tabpy.tabpy_server.app.app.os.path")
def test_https_cert_and_key_file_not_found(self, mock_path):
self.fp.write(
"[TabPy]\n"
"TABPY_TRANSFER_PROTOCOL = https\n"
"TABPY_CERTIFICATE_FILE = foo\n"
"TABPY_KEY_FILE = bar"
)
self.fp.close()
mock_path.isfile.side_effect = lambda x: self.mock_isfile(x, {self.fp.name})
self.assertTabPyAppRaisesRuntimeError(
"Error using HTTPS: The parameter(s) TABPY_CERTIFICATE_FILE and "
"TABPY_KEY_FILE must point to an existing file."
)
@patch("tabpy.tabpy_server.app.app.os.path")
def test_https_cert_file_not_found(self, mock_path):
self.fp.write(
"[TabPy]\n"
"TABPY_TRANSFER_PROTOCOL = https\n"
"TABPY_CERTIFICATE_FILE = foo\n"
"TABPY_KEY_FILE = bar"
)
self.fp.close()
mock_path.isfile.side_effect = lambda x: self.mock_isfile(
x, {self.fp.name, "bar"}
)
self.assertTabPyAppRaisesRuntimeError(
"Error using HTTPS: The parameter(s) TABPY_CERTIFICATE_FILE "
"must point to an existing file."
)
@patch("tabpy.tabpy_server.app.app.os.path")
def test_https_key_file_not_found(self, mock_path):
self.fp.write(
"[TabPy]\n"
"TABPY_TRANSFER_PROTOCOL = https\n"
"TABPY_CERTIFICATE_FILE = foo\n"
"TABPY_KEY_FILE = bar"
)
self.fp.close()
mock_path.isfile.side_effect = lambda x: self.mock_isfile(
x, {self.fp.name, "foo"}
)
self.assertTabPyAppRaisesRuntimeError(
"Error using HTTPS: The parameter(s) TABPY_KEY_FILE "
"must point to an existing file."
)
@patch("tabpy.tabpy_server.app.app.os.path.isfile", return_value=True)
@patch("tabpy.tabpy_server.app.util.validate_cert")
def test_https_success(self, mock_isfile, mock_validate_cert):
self.fp.write(
"[TabPy]\n"
"TABPY_TRANSFER_PROTOCOL = HtTpS\n"
"TABPY_CERTIFICATE_FILE = foo\n"
"TABPY_KEY_FILE = bar"
)
self.fp.close()
app = TabPyApp(self.fp.name)
self.assertEqual(app.settings["transfer_protocol"], "https")
self.assertEqual(app.settings["certificate_file"], "foo")
self.assertEqual(app.settings["key_file"], "bar")
class TestCertificateValidation(unittest.TestCase):
def assertValidateCertRaisesRuntimeError(self, expected_message, path):
with self.assertRaises(RuntimeError) as err:
validate_cert(path)
self.assertEqual(err.exception.args[0], expected_message)
def __init__(self, *args, **kwargs):
super(TestCertificateValidation, self).__init__(*args, **kwargs)
self.resources_path = os.path.join(os.path.dirname(__file__), "resources")
def test_expired_cert(self):
path = os.path.join(self.resources_path, "expired.crt")
message = (
"Error using HTTPS: The certificate provided expired "
"on 2018-08-18 19:47:18."
)
self.assertValidateCertRaisesRuntimeError(message, path)
def test_future_cert(self):
path = os.path.join(self.resources_path, "future.crt")
message = (
"Error using HTTPS: The certificate provided is not valid "
"until 3001-01-01 00:00:00."
)
self.assertValidateCertRaisesRuntimeError(message, path)
def test_valid_cert(self):
path = os.path.join(self.resources_path, "valid.crt")
validate_cert(path)