/
__init__.py
155 lines (110 loc) · 3.79 KB
/
__init__.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
from attest import Tests, assert_hook, AssertImportHook, raises
# disable assert hook before load flask app, while pull-request will be
# accepted https://github.com/dag/attest/pull/136
AssertImportHook.disable()
from flaskext.staticutils import StaticUtils, Rev, FileChecksumRev
from flaskext.staticutils.utils import (checksum, key, abspath, make_key,
get_config)
from tests.test_app import create_app
utils = Tests()
@utils.test
def existing_file_checksum():
path = os.path.join(os.getcwd(), 'requirements.dev.txt')
must_be = '0cef80c690b7529d44b332fc'
h = checksum(path, 24)
assert h == must_be
assert len(h) <= 24
@utils.test
def nonexisting_file_checksum():
assert checksum('nosuchfile', 10) is None
@utils.test
def _abspath():
root = '/root'
assert abspath('foo', root) == '/root/foo'
assert abspath('/foo', root) == '/root/foo'
assert abspath('//foo', root) == '/root/foo'
assert abspath('/root', root) == root
assert abspath('/root/foo', root) == '/root/foo'
@utils.test
def _make_key():
key1 = make_key('foo')
key2 = make_key('foo', a=1, b=2)
assert 'foo' in key1
assert 'foo' in key2
assert key1 != key2
@utils.test
def _get_config():
config = {'foo_a': 'a', 'foo_b': 'b', 'bar_c': 'c'}
assert get_config(config, 'foo_') == {'a': 'a', 'b': 'b'}
assert get_config(config, 'bar_') == {'c': 'c'}
assert get_config(config, 'baz_') == {}
staticutils = Tests()
def setup(app):
rev = StaticUtils(app)
return app, rev
@staticutils.context
def app_context():
app = create_app({})
with app.test_request_context():
yield app
@staticutils.test
def rev_init(app):
rev = Rev(None)
assert rev.formatstr == None
with raises(NotImplementedError):
rev(None)
@staticutils.test
def file_checksum_rev_init(app):
setup(app)
rev = FileChecksumRev(formatstr=app.config[key('FORMATSTR')],
rev_length=app.config[key('REV_LENGTH')],
root_path=app.root_path)
assert rev('static/js/app.js') == '/static/js/app-1b42a11f5f64.js'
@staticutils.test
def instance(app):
_, rev = setup(app)
assert app is not None
assert app.jinja_env.filters[app.config[key('FILTER_NAME')]] is rev
@staticutils.test
def existing_file_default_formatstr(app):
_, rev = setup(app)
assert rev('static/js/app.js') == '/static/js/app-1b42a11f5f64.js'
@staticutils.test
def existing_file_custom_formatstr(app):
app.config[key('FORMATSTR')] = '/rev-%(rev)s/%(path)s%(ext)s'
_, rev = setup(app)
assert rev('static/js/app.js') == '/rev-1b42a11f5f64/static/js/app.js'
@staticutils.test
def nonexisting_file(app):
_, rev = setup(app)
assert rev('nosuchfile') == 'nosuchfile'
@staticutils.test
def template_render(app):
setup(app)
with app.test_client() as c:
rv = c.get('/')
assert 'static/js/app-1b42a11f5f64.js' in rv.data
@staticutils.test
def rewrite_rule(app):
setup(app)
with app.test_client() as c:
assert app.testing
rv = c.get('static/js/app-d41d8cd98f00.js')
assert rv.status_code == 302
assert rv.headers['Location'] == 'http://localhost/static/js/app.js'
rv = c.get('static/js/app-d41d8cd98f00.js', follow_redirects=True)
assert rv.data == 'well\n'
@staticutils.test
def custom_rewrite_rule(app):
app.config[key('FORMATSTR')] = '/%(path)s-rev:%(rev)s%(ext)s'
setup(app)
with app.test_client() as c:
rv = c.get('/static/js/app-rev:d41d8cd98f00.js')
assert rv.status_code == 302
assert rv.headers['Location'] == 'http://localhost/static/js/app.js'
suite = Tests(tests=(utils, staticutils))
if __name__ == '__main__':
suite.main()