-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
343 lines (293 loc) · 12.1 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
# -*- coding: utf-8 -*-
"""
tests/test-uploads.py
=====================
This is a Nose testing file for Flask-Uploads.
NOTE: While Flask-Uploads will probably work on Windows, all the filenames in
this testing file are in the POSIX style. So, no guarantees that the tests
will pass.
:copyright: 2010 Matthew "LeafStorm" Frazier
:license: MIT/X11, see LICENSE for details
"""
import os.path
from flask import Flask, url_for
from flask_uploads import (UploadSet, UploadConfiguration, extension,
lowercase_ext, TestingFileStorage, patch_request_class, configure_uploads,
addslash, ALL, AllExcept, UploadsManager)
class TestMiscellaneous(object):
def test_tfs(self):
tfs = TestingFileStorage(filename='foo.bar')
assert tfs.filename == 'foo.bar'
assert tfs.name is None
assert tfs.saved is None
tfs.save('foo_bar.txt')
assert tfs.saved == 'foo_bar.txt'
def test_extension(self):
assert extension('foo.txt') == 'txt'
assert extension('foo') == ''
assert extension('archive.tar.gz') == 'gz'
assert extension('audio.m4a') == 'm4a'
def test_lowercase_ext(self):
assert lowercase_ext('foo.txt') == 'foo.txt'
assert lowercase_ext('FOO.TXT') == 'FOO.txt'
assert lowercase_ext('foo') == 'foo'
assert lowercase_ext('FOO') == 'FOO'
assert lowercase_ext('archive.tar.gz') == 'archive.tar.gz'
assert lowercase_ext('ARCHIVE.TAR.GZ') == 'ARCHIVE.TAR.gz'
assert lowercase_ext('audio.m4a') == 'audio.m4a'
assert lowercase_ext('AUDIO.M4A') == 'AUDIO.m4a'
def test_addslash(self):
assert (addslash('http://localhost:4000') ==
'http://localhost:4000/')
assert (addslash('http://localhost/uploads') ==
'http://localhost/uploads/')
assert (addslash('http://localhost:4000/') ==
'http://localhost:4000/')
assert (addslash('http://localhost/uploads/') ==
'http://localhost/uploads/')
def test_custom_iterables(self):
assert 'txt' in ALL
assert 'exe' in ALL
ax = AllExcept(['exe'])
assert 'txt' in ax
assert 'exe' not in ax
Config = UploadConfiguration
class TestConfiguration(object):
def setup(self):
self.app = Flask(__name__)
def teardown(self):
del self.app
def configure(self, *sets, **options):
self.app.config.update(options)
configure_uploads(self.app, sets)
return self.app.upload_set_config
def test_manual(self):
f, p = UploadSet('files'), UploadSet('photos')
setconfig = self.configure(f, p,
UPLOADED_FILES_DEST = '/var/files',
UPLOADED_FILES_URL = 'http://localhost:6001/',
UPLOADED_PHOTOS_DEST = '/mnt/photos',
UPLOADED_PHOTOS_URL = 'http://localhost:6002/'
)
fconf, pconf = setconfig['files'], setconfig['photos']
assert fconf == Config('/var/files', 'http://localhost:6001/')
assert pconf == Config('/mnt/photos', 'http://localhost:6002/')
def test_selfserve(self):
f, p = UploadSet('files'), UploadSet('photos')
setconfig = self.configure(f, p,
UPLOADED_FILES_DEST = '/var/files',
UPLOADED_PHOTOS_DEST = '/mnt/photos'
)
fconf, pconf = setconfig['files'], setconfig['photos']
assert fconf == Config('/var/files', None)
assert pconf == Config('/mnt/photos', None)
def test_defaults(self):
f, p = UploadSet('files'), UploadSet('photos')
setconfig = self.configure(f, p,
UPLOADS_DEFAULT_DEST = '/var/uploads',
UPLOADS_DEFAULT_URL = 'http://localhost:6000/'
)
fconf, pconf = setconfig['files'], setconfig['photos']
assert fconf == Config('/var/uploads/files',
'http://localhost:6000/files/')
assert pconf == Config('/var/uploads/photos',
'http://localhost:6000/photos/')
def test_default_selfserve(self):
f, p = UploadSet('files'), UploadSet('photos')
setconfig = self.configure(f, p,
UPLOADS_DEFAULT_DEST = '/var/uploads'
)
fconf, pconf = setconfig['files'], setconfig['photos']
assert fconf == Config('/var/uploads/files', None)
assert pconf == Config('/var/uploads/photos', None)
def test_mixed_defaults(self):
f, p = UploadSet('files'), UploadSet('photos')
setconfig = self.configure(f, p,
UPLOADS_DEFAULT_DEST = '/var/uploads',
UPLOADS_DEFAULT_URL = 'http://localhost:6001/',
UPLOADED_PHOTOS_DEST = '/mnt/photos',
UPLOADED_PHOTOS_URL = 'http://localhost:6002/'
)
fconf, pconf = setconfig['files'], setconfig['photos']
assert fconf == Config('/var/uploads/files',
'http://localhost:6001/files/')
assert pconf == Config('/mnt/photos', 'http://localhost:6002/')
def test_defaultdest_callable(self):
f = UploadSet('files', default_dest=lambda app: os.path.join(
app.config['INSTANCE'], 'files'
))
p = UploadSet('photos')
setconfig = self.configure(f, p,
INSTANCE = '/home/me/webapps/thisapp',
UPLOADED_PHOTOS_DEST = '/mnt/photos',
UPLOADED_PHOTOS_URL = 'http://localhost:6002/'
)
fconf, pconf = setconfig['files'], setconfig['photos']
assert fconf == Config('/home/me/webapps/thisapp/files', None)
assert pconf == Config('/mnt/photos', 'http://localhost:6002/')
class TestPreconditions(object):
def test_filenames(self):
uset = UploadSet('files')
uset._config = Config('/uploads')
namepairs = (
('foo.txt', True),
('boat.jpg', True),
('warez.exe', False)
)
for name, result in namepairs:
tfs = TestingFileStorage(filename=name)
assert uset.file_allowed(tfs, name) is result
def test_default_extensions(self):
uset = UploadSet('files')
uset._config = Config('/uploads')
extpairs = (('txt', True), ('jpg', True), ('exe', False))
for ext, result in extpairs:
assert uset.extension_allowed(ext) is result
class TestSaving(object):
def setup(self):
self.old_makedirs = os.makedirs
os.makedirs = lambda v: None
def teardown(self):
os.makedirs = self.old_makedirs
del self.old_makedirs
def test_saved(self):
uset = UploadSet('files')
uset._config = Config('/uploads')
tfs = TestingFileStorage(filename='foo.txt')
res = uset.save(tfs)
assert res == 'foo.txt'
assert tfs.saved == '/uploads/foo.txt'
def test_save_folders(self):
uset = UploadSet('files')
uset._config = Config('/uploads')
tfs = TestingFileStorage(filename='foo.txt')
res = uset.save(tfs, folder='someguy')
assert res == 'someguy/foo.txt'
assert tfs.saved == '/uploads/someguy/foo.txt'
def test_save_named(self):
uset = UploadSet('files')
uset._config = Config('/uploads')
tfs = TestingFileStorage(filename='foo.txt')
res = uset.save(tfs, name='file_123.txt')
assert res == 'file_123.txt'
assert tfs.saved == '/uploads/file_123.txt'
def test_save_namedext(self):
uset = UploadSet('files')
uset._config = Config('/uploads')
tfs = TestingFileStorage(filename='boat.jpg')
res = uset.save(tfs, name='photo_123.')
assert res == 'photo_123.jpg'
assert tfs.saved == '/uploads/photo_123.jpg'
def test_folder_namedext(self):
uset = UploadSet('files')
uset._config = Config('/uploads')
tfs = TestingFileStorage(filename='boat.jpg')
res = uset.save(tfs, folder='someguy', name='photo_123.')
assert res == 'someguy/photo_123.jpg'
assert tfs.saved == '/uploads/someguy/photo_123.jpg'
def test_implicit_folder(self):
uset = UploadSet('files')
uset._config = Config('/uploads')
tfs = TestingFileStorage(filename='boat.jpg')
res = uset.save(tfs, name='someguy/photo_123.')
assert res == 'someguy/photo_123.jpg'
assert tfs.saved == '/uploads/someguy/photo_123.jpg'
def test_secured_filename(self):
uset = UploadSet('files', ALL)
uset._config = Config('/uploads')
tfs1 = TestingFileStorage(filename='/etc/passwd')
tfs2 = TestingFileStorage(filename='../../myapp.wsgi')
res1 = uset.save(tfs1)
assert res1 == 'etc_passwd'
assert tfs1.saved == '/uploads/etc_passwd'
res2 = uset.save(tfs2)
assert res2 == 'myapp.wsgi'
assert tfs2.saved == '/uploads/myapp.wsgi'
class TestConflictResolution(object):
def setup(self):
self.extant_files = []
self.old_exists = os.path.exists
os.path.exists = self.exists
self.old_makedirs = os.makedirs
os.makedirs = lambda v: None
def teardown(self):
os.path.exists = self.old_exists
del self.extant_files, self.old_exists
os.makedirs = self.old_makedirs
del self.old_makedirs
def extant(self, *files):
self.extant_files.extend(files)
def exists(self, fname):
return fname in self.extant_files
def test_self(self):
assert not os.path.exists('/uploads/foo.txt')
self.extant('/uploads/foo.txt')
assert os.path.exists('/uploads/foo.txt')
def test_conflict(self):
uset = UploadSet('files')
uset._config = Config('/uploads')
tfs = TestingFileStorage(filename='foo.txt')
self.extant('/uploads/foo.txt')
res = uset.save(tfs)
assert res == 'foo_1.txt'
def test_multi_conflict(self):
uset = UploadSet('files')
uset._config = Config('/uploads')
tfs = TestingFileStorage(filename='foo.txt')
self.extant('/uploads/foo.txt',
*('/uploads/foo_%d.txt' % n for n in range(1, 6)))
res = uset.save(tfs)
assert res == 'foo_6.txt'
def test_conflict_without_extension(self):
# Test case for issue #7.
uset = UploadSet('files', extensions=(''))
uset._config = Config('/uploads')
tfs = TestingFileStorage(filename='foo')
self.extant('/uploads/foo')
res = uset.save(tfs)
assert res == 'foo_1'
class TestPathsAndURLs(object):
def test_path(self):
uset = UploadSet('files')
uset._config = Config('/uploads')
assert uset.path('foo.txt') == '/uploads/foo.txt'
assert uset.path('someguy/foo.txt') == '/uploads/someguy/foo.txt'
assert (uset.path('foo.txt', folder='someguy') ==
'/uploads/someguy/foo.txt')
assert (uset.path('foo/bar.txt', folder='someguy') ==
'/uploads/someguy/foo/bar.txt')
def test_url_generated(self):
app = Flask(__name__)
app.config.update(
UPLOADED_FILES_DEST='/uploads'
)
uset = UploadSet('files')
configure_uploads(app, uset)
with app.test_request_context():
url = uset.url('foo.txt')
gen = url_for('_uploads.uploaded_file', setname='files',
filename='foo.txt', _external=True)
assert url == gen
def test_url_based(self):
app = Flask(__name__)
app.config.update(
UPLOADED_FILES_DEST='/uploads',
UPLOADED_FILES_URL='http://localhost:5001/'
)
uset = UploadSet('files')
configure_uploads(app, uset)
with app.test_request_context():
url = uset.url('foo.txt')
assert url == 'http://localhost:5001/foo.txt'
assert '_uploads' not in app.blueprints
class TestConfigurationWithManger(TestConfiguration):
def setup(self):
super(TestConfigurationWithManger, self).setup()
self.uploads_manager = UploadsManager(self.app)
def configure(self, *sets, **options):
self.app.config.update(options)
self.uploads_manager.register(sets)
return self.app.upload_set_config
def teardown(self):
del self.uploads_manager
super(TestConfigurationWithManger, self).teardown()