/
tests.py
441 lines (367 loc) · 16.3 KB
/
tests.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
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
# coding: utf8
"""
flask_frozen.tests
~~~~~~~~~~~~~~~~~~
Automated test suite for Frozen-Flask
:copyright: (c) 2010-2012 by Simon Sapin.
:license: BSD, see LICENSE for more details.
"""
import unittest
import tempfile
import shutil
import os.path
import warnings
import hashlib
from contextlib import contextmanager
from unicodedata import normalize
from warnings import catch_warnings
from flask_frozen import (Freezer, walk_directory,
MissingURLGeneratorWarning, MimetypeMismatchWarning, NotFoundWarning)
from flask_frozen import test_app
try:
unicode
except NameError: # Python 3
unicode = str
@contextmanager
def temp_directory():
"""This context manager gives the path to a new temporary directory that
is deleted (with all it's content) at the end of the with block.
"""
directory = tempfile.mkdtemp()
try:
yield directory
finally:
shutil.rmtree(directory)
def read_file(filename):
with open(filename, 'rb') as fd:
content = fd.read()
if len(content) < 200:
return content
else:
return hashlib.md5(content).hexdigest()
def read_all(directory):
return set(
(filename, read_file(os.path.join(directory, *filename.split('/'))))
for filename in walk_directory(directory))
class TestTempDirectory(unittest.TestCase):
def test_removed(self):
with temp_directory() as temp:
assert os.path.isdir(temp)
# should be removed now
assert not os.path.exists(temp)
def test_exception(self):
try:
with temp_directory() as temp:
assert os.path.isdir(temp)
1 / 0
except ZeroDivisionError:
pass
else:
assert False, 'Exception did not propagate'
assert not os.path.exists(temp)
def test_writing(self):
with temp_directory() as temp:
filename = os.path.join(temp, 'foo')
with open(filename, 'w') as fd:
fd.write('foo')
assert os.path.isfile(filename)
assert not os.path.exists(temp)
assert not os.path.exists(filename)
class TestWalkDirectory(unittest.TestCase):
def test_walk_directory(self):
self.assertEqual(
set(f for f in walk_directory(os.path.dirname(test_app.__file__))
if not f.endswith(('.pyc', '.pyo'))),
set(['__init__.py', 'static/style.css', 'static/favicon.ico',
'static/main.js', 'admin/__init__.py',
'admin/admin_static/style.css', 'admin/templates/admin.html'])
)
class TestFreezer(unittest.TestCase):
# URL -> expected bytes content of the generated file
expected_output = {
u'/': b'Main index /product_5/?revision=b12ef20',
u'/admin/': b'Admin index\n'
b'<a href="/page/I%20l%C3%B8v%C3%AB%20Unicode/">Unicode test</a>\n'
b'<a href="/page/octothorp/?query_foo=bar#introduction">'
b'URL parsing test</a>',
u'/robots.txt': b'User-agent: *\nDisallow: /',
u'/favicon.ico': read_file(test_app.FAVICON),
u'/product_0/': b'Product num 0',
u'/product_1/': b'Product num 1',
u'/product_2/': b'Product num 2',
u'/product_3/': b'Product num 3',
u'/product_4/': b'Product num 4',
u'/product_5/': b'Product num 5',
u'/static/favicon.ico': read_file(test_app.FAVICON),
u'/static/style.css': b'/* Main CSS */\n',
u'/static/main.js': b'/* Main JS */\n',
u'/admin/css/style.css': b'/* Admin CSS */\n',
u'/where_am_i/': b'/where_am_i/ http://localhost/where_am_i/',
u'/page/foo/': u'Hello\xa0World! foo'.encode('utf8'),
u'/page/I løvë Unicode/':
u'Hello\xa0World! I løvë Unicode'.encode('utf8'),
u'/page/octothorp/':
u'Hello\xa0World! octothorp'.encode('utf8'),
}
# URL -> path to the generated file, relative to the build destination root
filenames = {
u'/': u'index.html',
u'/admin/': u'admin/index.html',
u'/robots.txt': u'robots.txt',
u'/favicon.ico': u'favicon.ico',
u'/product_0/': u'product_0/index.html',
u'/product_1/': u'product_1/index.html',
u'/product_2/': u'product_2/index.html',
u'/product_3/': u'product_3/index.html',
u'/product_4/': u'product_4/index.html',
u'/product_5/': u'product_5/index.html',
u'/static/style.css': u'static/style.css',
u'/static/main.js': u'static/main.js',
u'/static/favicon.ico': u'static/favicon.ico',
u'/admin/css/style.css': u'admin/css/style.css',
u'/where_am_i/': u'where_am_i/index.html',
u'/page/foo/': u'page/foo/index.html',
u'/page/I løvë Unicode/': u'page/I løvë Unicode/index.html',
u'/page/octothorp/': u'page/octothorp/index.html',
}
assert set(expected_output.keys()) == set(filenames.keys())
generated_by_url_for = [u'/product_3/', u'/product_4/', u'/product_5/',
u'/page/I løvë Unicode/',
u'/page/octothorp/']
defer_init_app = True
freezer_kwargs = None
maxDiff = None
def do_extra_config(self, app, freezer):
pass # To be overriden
@contextmanager
def make_app(self):
with temp_directory() as temp:
app, freezer = test_app.create_app(self.defer_init_app,
self.freezer_kwargs)
app.config['FREEZER_DESTINATION'] = temp
app.debug = True
self.do_extra_config(app, freezer)
yield temp, app, freezer
@contextmanager
def make_app_with_404(self):
# Build an app with a link to a non-existent route
with self.make_app() as (temp, app, freezer):
@freezer.register_generator
def non_existent_url():
yield '/404/'
yield temp, app, freezer
@contextmanager
def built_app(self):
with self.make_app() as (temp, app, freezer):
urls = freezer.freeze()
yield temp, app, freezer, urls
def assertFilenamesEqual(self, set1, set2):
# Fix for https://github.com/SimonSapin/Frozen-Flask/issues/5
set1 = sorted(normalize('NFC', name) for name in set1)
set2 = sorted(normalize('NFC', name) for name in set2)
self.assertEqual(set1, set2)
def test_without_app(self):
freezer = Freezer()
self.assertRaises(Exception, freezer.freeze)
def test_all_urls_method(self):
with self.built_app() as (temp, app, freezer, urls):
expected = sorted(self.expected_output)
# url_for() calls are not logged when just calling .all_urls()
for url in self.generated_by_url_for:
if url in expected:
expected.remove(url)
# Do not use set() here: also test that URLs are not duplicated.
self.assertEqual(sorted(freezer.all_urls()), expected)
def test_built_urls(self):
with self.built_app() as (temp, app, freezer, urls):
self.assertEqual(set(urls), set(self.expected_output))
# Make sure it was not accidently used as a destination
default = os.path.join(os.path.dirname(__file__), 'build')
self.assertTrue(not os.path.exists(default))
def test_contents(self):
with self.built_app() as (temp, app, freezer, urls):
for url, filename in self.filenames.items():
filename = os.path.join(freezer.root, *filename.split('/'))
content = read_file(filename)
self.assertEqual(content, self.expected_output[url])
def test_nothing_else_matters(self):
self._extra_files(removed=True)
def test_something_else_matters(self):
self._extra_files(remove_extra=False, removed=False)
def test_ignore_pattern(self):
self._extra_files(ignore=['extraa'], removed=True) # Not a match
self._extra_files(ignore=['extr*'], removed=False) # Match
def _extra_files(self, removed, remove_extra=True, ignore=()):
with self.built_app() as (temp, app, freezer, urls):
app.config['FREEZER_REMOVE_EXTRA_FILES'] = remove_extra
app.config['FREEZER_DESTINATION_IGNORE'] = ignore
dest = unicode(app.config['FREEZER_DESTINATION'])
expected_files = set(self.filenames.values())
# No other files
self.assertFilenamesEqual(walk_directory(dest), expected_files)
# create an empty file
os.mkdir(os.path.join(dest, 'extra'))
open(os.path.join(dest, 'extra', 'extra.txt'), 'wb').close()
# Verify that files in destination persist.
freezer.freeze()
exists = os.path.exists(os.path.join(dest, 'extra'))
if removed:
self.assertTrue(not exists)
else:
self.assertTrue(exists)
expected_files.add(u'extra/extra.txt')
self.assertFilenamesEqual(walk_directory(dest), expected_files)
def test_transitivity(self):
with self.built_app() as (temp, app, freezer, urls):
with temp_directory() as temp2:
# Run the freezer on it's own output
app2 = freezer.make_static_app()
app2.config['FREEZER_DESTINATION'] = temp2
app2.debug = True
freezer2 = Freezer(app2)
freezer2.register_generator(self.filenames.keys)
freezer2.freeze()
destination = app.config['FREEZER_DESTINATION']
self.assertEqual(read_all(destination), read_all(temp2))
def test_error_on_external_url(self):
for url in ['http://example.com/foo', '//example.com/foo',
'file:///foo']:
with self.make_app() as (temp, app, freezer):
@freezer.register_generator
def external_url():
yield url
try:
freezer.freeze()
except ValueError as e:
assert 'External URLs not supported' in e.args[0]
else:
assert False, 'Expected ValueError'
def test_error_on_internal_404(self):
with self.make_app_with_404() as (temp, app, freezer):
# Test standard behaviour with 404 errors (freeze failure)
try:
freezer.freeze()
except ValueError as e:
error_msg = "Unexpected status '404 NOT FOUND' on URL /404/"
assert error_msg in e.args[0]
else:
assert False, 'Expected ValueError'
def test_warn_on_internal_404(self):
with self.make_app_with_404() as (temp, app, freezer):
# Enable 404 erros ignoring
app.config['FREEZER_IGNORE_404_NOT_FOUND'] = True
# Test warning with 404 errors when we choose to ignore them
with catch_warnings(record=True) as logged_warnings:
warnings.simplefilter("always")
freezer.freeze()
self.assertEqual(len(logged_warnings), 1)
self.assertEqual(logged_warnings[0].category,
NotFoundWarning)
def test_warn_on_missing_generator(self):
with self.make_app() as (temp, app, freezer):
# Add a new endpoint without URL generator
@app.route('/extra/<some_argument>')
def external_url(some_argument):
return some_argument
with catch_warnings(record=True) as logged_warnings:
warnings.simplefilter("always")
freezer.freeze()
self.assertEqual(len(logged_warnings), 1)
self.assertEqual(logged_warnings[0].category,
MissingURLGeneratorWarning)
def test_wrong_default_mimetype(self):
with self.make_app() as (temp, app, freezer):
@app.route(u'/no-file-extension')
def no_extension():
return '42', 200, {'Content-Type': 'image/png'}
with catch_warnings(record=True) as logged_warnings:
warnings.simplefilter("always")
freezer.freeze()
self.assertEqual(len(logged_warnings), 1)
self.assertEqual(logged_warnings[0].category,
MimetypeMismatchWarning)
def test_default_mimetype(self):
with self.make_app() as (temp, app, freezer):
@app.route(u'/no-file-extension')
def no_extension():
return '42', 200, {'Content-Type': 'application/octet-stream'}
freezer.freeze()
def test_unknown_extension(self):
with self.make_app() as (temp, app, freezer):
@app.route(u'/unkown-extension.fuu')
def no_extension():
return '42', 200, {'Content-Type': 'application/octet-stream'}
freezer.freeze()
def test_configured_default_mimetype(self):
with self.make_app() as (temp, app, freezer):
app.config['FREEZER_DEFAULT_MIMETYPE'] = 'image/png'
@app.route(u'/no-file-extension')
def no_extension():
return '42', 200, {'Content-Type': 'image/png'}
freezer.freeze()
def test_wrong_configured_mimetype(self):
with self.make_app() as (temp, app, freezer):
app.config['FREEZER_DEFAULT_MIMETYPE'] = 'image/png'
@app.route(u'/no-file-extension')
def no_extension():
return '42', 200, {'Content-Type': 'application/octet-stream'}
with catch_warnings(record=True) as logged_warnings:
warnings.simplefilter("always")
freezer.freeze()
self.assertEqual(len(logged_warnings), 1)
self.assertEqual(logged_warnings[0].category,
MimetypeMismatchWarning)
class TestInitApp(TestFreezer):
defer_init_app = True
class TestBaseURL(TestFreezer):
expected_output = TestFreezer.expected_output.copy()
expected_output['/'] = b'Main index /myapp/product_5/?revision=b12ef20'
expected_output['/where_am_i/'] = \
b'/myapp/where_am_i/ http://example/myapp/where_am_i/'
expected_output['/admin/'] = (
b'Admin index\n'
b'<a href="/myapp/page/I%20l%C3%B8v%C3%AB%20Unicode/">Unicode test</a>\n'
b'<a href="/myapp/page/octothorp/?query_foo=bar#introduction">'
b'URL parsing test</a>')
def do_extra_config(self, app, freezer):
app.config['FREEZER_BASE_URL'] = 'http://example/myapp/'
class TestNonexsistentDestination(TestFreezer):
def do_extra_config(self, app, freezer):
# frozen/htdocs does not exsist in the newly created temp directory,
# the Freezer has to create it.
app.config['FREEZER_DESTINATION'] = os.path.join(
app.config['FREEZER_DESTINATION'], 'frozen', 'htdocs')
class TestServerName(TestFreezer):
def do_extra_config(self, app, freezer):
app.config['SERVER_NAME'] = 'example.net'
expected_output = TestFreezer.expected_output.copy()
expected_output[u'/where_am_i/'] = (
b'/where_am_i/ http://example.net/where_am_i/')
class TestWithoutUrlForLog(TestFreezer):
freezer_kwargs = dict(log_url_for=False)
expected_output = TestFreezer.expected_output.copy()
filenames = TestFreezer.filenames.copy()
for url in TestFreezer.generated_by_url_for:
del expected_output[url]
del filenames[url]
class TestRelativeUrlFor(TestFreezer):
def do_extra_config(self, app, freezer):
app.config['FREEZER_RELATIVE_URLS'] = True
expected_output = TestFreezer.expected_output.copy()
expected_output['/admin/'] = (
b'Admin index\n'
b'<a href="../page/I%20l%C3%B8v%C3%AB%20Unicode/index.html">'
b'Unicode test</a>\n'
b'<a href="../page/octothorp/index.html?query_foo=bar#introduction">'
b'URL parsing test</a>')
class TestStaticIgnore(TestFreezer):
def do_extra_config(self, app, freezer):
app.config['FREEZER_STATIC_IGNORE'] = ['*.js',]
expected_output = TestFreezer.expected_output.copy()
filenames = TestFreezer.filenames.copy()
del expected_output[u'/static/main.js']
del filenames[u'/static/main.js']
# with_no_argument_rules=False and with_static_files=False are
# not tested as they produces (expected!) warnings
if __name__ == '__main__':
unittest.main()