-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathutils.py
More file actions
557 lines (418 loc) · 16.1 KB
/
utils.py
File metadata and controls
557 lines (418 loc) · 16.1 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# -*- coding: utf-8 -*-
"""
flask_testing.utils
~~~~~~~~~~~~~~~~~~~
Flask unittest integration.
:copyright: (c) 2010 by Dan Jacob.
:license: BSD, see LICENSE for more details.
"""
from __future__ import absolute_import, with_statement
import gc
import multiprocessing
import socket
import time
try:
import socketserver
except ImportError:
# Python 2 SocketServer fallback
import SocketServer as socketserver
try:
import unittest2 as unittest
except ImportError:
import unittest
try:
from urllib.parse import urlparse, urljoin
except ImportError:
# Python 2 urlparse fallback
from urlparse import urlparse, urljoin
from werkzeug.utils import cached_property
# Use Flask's preferred JSON module so that our runtime behavior matches.
from flask import templating, template_rendered
try:
from flask import message_flashed
_is_message_flashed = True
except ImportError:
message_flashed = None
_is_message_flashed = False
json_available = True
try:
from flask import json
except ImportError:
json_available = False
# we'll use signals for template-related tests if
# available in this version of Flask
try:
import blinker
_is_signals = True
except ImportError: # pragma: no cover
_is_signals = False
__all__ = ["TestCase"]
class ContextVariableDoesNotExist(Exception):
pass
class JsonResponseMixin(object):
"""
Mixin with testing helper methods
"""
@cached_property
def json(self):
if not json_available: # pragma: no cover
raise NotImplementedError
return json.loads(self.data)
def _make_test_response(response_class):
class TestResponse(response_class, JsonResponseMixin):
pass
return TestResponse
def _empty_render(app, template, context):
"""
Used to monkey patch the render_template flask method when
the render_templates property is set to False in the TestCase
"""
if _is_signals:
template_rendered.send(app, template=template, context=context)
return ""
def _check_for_message_flashed_support():
if not _is_signals or not _is_message_flashed:
raise RuntimeError(
"Your version of Flask doesn't support message_flashed. "
"This requires Flask 0.10+ with the blinker module installed."
)
def _check_for_signals_support():
if not _is_signals:
raise RuntimeError(
"Your version of Flask doesn't support signals. "
"This requires Flask 0.6+ with the blinker module installed."
)
class TestCase(unittest.TestCase):
render_templates = True
run_gc_after_test = False
def create_app(self):
"""
Create your Flask app here, with any
configuration you need.
"""
raise NotImplementedError
def __call__(self, result=None):
"""
Does the required setup, doing it here
means you don't have to call super.setUp
in subclasses.
"""
try:
self._pre_setup()
super(TestCase, self).__call__(result)
finally:
self._post_teardown()
def debug(self):
try:
self._pre_setup()
super(TestCase, self).debug()
finally:
self._post_teardown()
def _pre_setup(self):
self.app = self.create_app()
self._orig_response_class = self.app.response_class
self.app.response_class = _make_test_response(self.app.response_class)
self.client = self.app.test_client()
self._ctx = self.app.test_request_context()
self._ctx.push()
if not self.render_templates:
# Monkey patch the original template render with a empty render
self._original_template_render = templating._render
templating._render = _empty_render
self.templates = []
self.flashed_messages = []
if _is_signals:
template_rendered.connect(self._add_template)
if _is_message_flashed:
message_flashed.connect(self._add_flash_message)
def _add_flash_message(self, app, message, category):
self.flashed_messages.append((message, category))
def _add_template(self, app, template, context):
if len(self.templates) > 0:
self.templates = []
self.templates.append((template, context))
def _post_teardown(self):
if getattr(self, '_ctx', None) is not None:
self._ctx.pop()
del self._ctx
if getattr(self, 'app', None) is not None:
if getattr(self, '_orig_response_class', None) is not None:
self.app.response_class = self._orig_response_class
del self.app
if hasattr(self, 'client'):
del self.client
if hasattr(self, 'templates'):
del self.templates
if hasattr(self, 'flashed_messages'):
del self.flashed_messages
if _is_signals:
template_rendered.disconnect(self._add_template)
if _is_message_flashed:
message_flashed.disconnect(self._add_flash_message)
if hasattr(self, '_original_template_render'):
templating._render = self._original_template_render
if self.run_gc_after_test:
gc.collect()
def assertMessageFlashed(self, message, category='message'):
"""
Checks if a given message was flashed.
Only works if your version of Flask has message_flashed
signal support (0.10+) and blinker is installed.
:param message: expected message
:param category: expected message category
"""
_check_for_message_flashed_support()
for _message, _category in self.flashed_messages:
if _message == message and _category == category:
return True
raise AssertionError("Message '%s' in category '%s' wasn't flashed" % (message, category))
assert_message_flashed = assertMessageFlashed
def assertTemplateUsed(self, name, tmpl_name_attribute='name'):
"""
Checks if a given template is used in the request.
Only works if your version of Flask has signals
support (0.6+) and blinker is installed.
If the template engine used is not Jinja2, provide
``tmpl_name_attribute`` with a value of its `Template`
class attribute name which contains the provided ``name`` value.
:versionadded: 0.2
:param name: template name
:param tmpl_name_attribute: template engine specific attribute name
"""
_check_for_signals_support()
used_templates = []
for template, context in self.templates:
if getattr(template, tmpl_name_attribute) == name:
return True
used_templates.append(template)
raise AssertionError("Template %s not used. Templates were used: %s" % (name, ' '.join(repr(used_templates))))
assert_template_used = assertTemplateUsed
def get_context_variable(self, name):
"""
Returns a variable from the context passed to the
template. Only works if your version of Flask
has signals support (0.6+) and blinker is installed.
Raises a ContextVariableDoesNotExist exception if does
not exist in context.
:versionadded: 0.2
:param name: name of variable
"""
_check_for_signals_support()
for template, context in self.templates:
if name in context:
return context[name]
raise ContextVariableDoesNotExist
def assertContext(self, name, value, message=None):
"""
Checks if given name exists in the template context
and equals the given value.
:versionadded: 0.2
:param name: name of context variable
:param value: value to check against
"""
try:
self.assertEqual(self.get_context_variable(name), value, message)
except ContextVariableDoesNotExist:
self.fail(message or "Context variable does not exist: %s" % name)
assert_context = assertContext
def assertRedirects(self, response, location, message=None):
"""
Checks if response is an HTTP redirect to the
given location.
:param response: Flask response
:param location: relative URL path to SERVER_NAME or an absolute URL
"""
parts = urlparse(location)
if parts.netloc:
expected_location = location
else:
server_name = self.app.config.get('SERVER_NAME') or 'localhost'
expected_location = urljoin("http://%s" % server_name, location)
valid_status_codes = (301, 302, 303, 305, 307)
valid_status_code_str = ', '.join(str(code) for code in valid_status_codes)
not_redirect = "HTTP Status %s expected but got %d" % (valid_status_code_str, response.status_code)
self.assertTrue(response.status_code in valid_status_codes, message or not_redirect)
self.assertEqual(response.location, expected_location, message)
assert_redirects = assertRedirects
def assertStatus(self, response, status_code, message=None):
"""
Helper method to check matching response status.
:param response: Flask response
:param status_code: response status code (e.g. 200)
:param message: Message to display on test failure
"""
message = message or 'HTTP Status %s expected but got %s' \
% (status_code, response.status_code)
self.assertEqual(response.status_code, status_code, message)
assert_status = assertStatus
def assert200(self, response, message=None):
"""
Checks if response status code is 200
:param response: Flask response
:param message: Message to display on test failure
"""
self.assertStatus(response, 200, message)
assert_200 = assert200
def assert400(self, response, message=None):
"""
Checks if response status code is 400
:versionadded: 0.2.5
:param response: Flask response
:param message: Message to display on test failure
"""
self.assertStatus(response, 400, message)
assert_400 = assert400
def assert401(self, response, message=None):
"""
Checks if response status code is 401
:versionadded: 0.2.1
:param response: Flask response
:param message: Message to display on test failure
"""
self.assertStatus(response, 401, message)
assert_401 = assert401
def assert403(self, response, message=None):
"""
Checks if response status code is 403
:versionadded: 0.2
:param response: Flask response
:param message: Message to display on test failure
"""
self.assertStatus(response, 403, message)
assert_403 = assert403
def assert404(self, response, message=None):
"""
Checks if response status code is 404
:param response: Flask response
:param message: Message to display on test failure
"""
self.assertStatus(response, 404, message)
assert_404 = assert404
def assert405(self, response, message=None):
"""
Checks if response status code is 405
:versionadded: 0.2
:param response: Flask response
:param message: Message to display on test failure
"""
self.assertStatus(response, 405, message)
assert_405 = assert405
def assert500(self, response, message=None):
"""
Checks if response status code is 500
:versionadded: 0.4.1
:param response: Flask response
:param message: Message to display on test failure
"""
self.assertStatus(response, 500, message)
assert_500 = assert500
# A LiveServerTestCase useful with Selenium or headless browsers
# Inspired by https://docs.djangoproject.com/en/dev/topics/testing/#django.test.LiveServerTestCase
class LiveServerTestCase(unittest.TestCase):
def create_app(self):
"""
Create your Flask app here, with any
configuration you need.
"""
raise NotImplementedError
def __call__(self, result=None):
"""
Does the required setup, doing it here means you don't have to
call super.setUp in subclasses.
"""
# Get the app
self.app = self.create_app()
self._configured_port = self.app.config.get('LIVESERVER_PORT', 5000)
self._port_value = multiprocessing.Value('i', self._configured_port)
# We need to create a context in order for extensions to catch up
self._ctx = self.app.test_request_context()
self._ctx.push()
try:
self._spawn_live_server()
super(LiveServerTestCase, self).__call__(result)
finally:
self._post_teardown()
self._terminate_live_server()
def get_server_url(self):
"""
Return the url of the test server
"""
return 'http://localhost:%s' % self._port_value.value
def _spawn_live_server(self):
self._process = None
port_value = self._port_value
def worker(app, port):
# Based on solution: http://stackoverflow.com/a/27598916
# Monkey-patch the server_bind so we can determine the port bound by Flask.
# This handles the case where the port specified is `0`, which means that
# the OS chooses the port. This is the only known way (currently) of getting
# the port out of Flask once we call `run`.
original_socket_bind = socketserver.TCPServer.server_bind
def socket_bind_wrapper(self):
ret = original_socket_bind(self)
# Get the port and save it into the port_value, so the parent process
# can read it.
(_, port) = self.socket.getsockname()
port_value.value = port
socketserver.TCPServer.server_bind = original_socket_bind
return ret
socketserver.TCPServer.server_bind = socket_bind_wrapper
app.run(port=port, use_reloader=False)
self._process = multiprocessing.Process(
target=worker, args=(self.app, self._configured_port)
)
self._process.start()
# We must wait for the server to start listening, but give up
# after a specified maximum timeout
timeout = self.app.config.get('LIVESERVER_TIMEOUT', 5)
start_time = time.time()
while True:
elapsed_time = (time.time() - start_time)
if elapsed_time > timeout:
raise RuntimeError(
"Failed to start the server after %d seconds. " % timeout
)
if self._can_ping_server():
break
def _can_ping_server(self):
host, port = self._get_server_address()
if port == 0:
# Port specified by the user was 0, and the OS has not yet assigned
# the proper port.
return False
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((host, port))
except socket.error as e:
success = False
else:
success = True
finally:
sock.close()
return success
def _get_server_address(self):
"""
Gets the server address used to test the connection with a socket.
Respects both the LIVESERVER_PORT config value and overriding
get_server_url()
"""
parts = urlparse(self.get_server_url())
host = parts.hostname
port = parts.port
if port is None:
if parts.scheme == 'http':
port = 80
elif parts.scheme == 'https':
port = 443
else:
raise RuntimeError(
"Unsupported server url scheme: %s" % parts.scheme
)
return host, port
def _post_teardown(self):
if getattr(self, '_ctx', None) is not None:
self._ctx.pop()
del self._ctx
def _terminate_live_server(self):
if self._process:
self._process.terminate()