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
|
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import tempfile
import pytest
from werkzeug.utils import secure_filename
from mediagoblin import storage
################
# Test utilities
################
def test_clean_listy_filepath():
expected = ['dir1', 'dir2', 'linooks.jpg']
assert storage.clean_listy_filepath(
['dir1', 'dir2', 'linooks.jpg']) == expected
expected = ['dir1', 'foo_.._nasty', 'linooks.jpg']
assert storage.clean_listy_filepath(
['/dir1/', 'foo/../nasty', 'linooks.jpg']) == expected
expected = ['etc', 'passwd']
assert storage.clean_listy_filepath(
['../../../etc/', 'passwd']) == expected
with pytest.raises(storage.InvalidFilepath):
storage.clean_listy_filepath(['../../', 'linooks.jpg'])
class FakeStorageSystem:
def __init__(self, foobie, blech, **kwargs):
self.foobie = foobie
self.blech = blech
class FakeRemoteStorage(storage.filestorage.BasicFileStorage):
# Theoretically despite this, all the methods should work but it
# should force copying to the workbench
local_storage = False
def copy_local_to_storage(self, *args, **kwargs):
return storage.StorageInterface.copy_local_to_storage(
self, *args, **kwargs)
def test_storage_system_from_config():
this_storage = storage.storage_system_from_config(
{'base_url': 'http://example.org/moodia/',
'base_dir': '/tmp/',
'garbage_arg': 'garbage_arg',
'garbage_arg': 'trash'})
assert this_storage.base_url == 'http://example.org/moodia/'
assert this_storage.base_dir == '/tmp/'
assert this_storage.__class__ is storage.filestorage.BasicFileStorage
this_storage = storage.storage_system_from_config(
{'foobie': 'eiboof',
'blech': 'hcelb',
'garbage_arg': 'garbage_arg',
'storage_class':
'mediagoblin.tests.test_storage:FakeStorageSystem'})
assert this_storage.foobie == 'eiboof'
assert this_storage.blech == 'hcelb'
assert str(this_storage.__class__) == \
"<class 'mediagoblin.tests.test_storage.FakeStorageSystem'>"
##########################
# Basic file storage tests
##########################
def get_tmp_filestorage(mount_url=None, fake_remote=False):
tmpdir = tempfile.mkdtemp(prefix="test_gmg_storage")
if fake_remote:
this_storage = FakeRemoteStorage(tmpdir, mount_url)
else:
this_storage = storage.filestorage.BasicFileStorage(tmpdir, mount_url)
return tmpdir, this_storage
def cleanup_storage(this_storage, tmpdir, *paths):
for p in paths:
while p:
assert this_storage.delete_dir(p) == True
p.pop(-1)
os.rmdir(tmpdir)
def test_basic_storage__resolve_filepath():
tmpdir, this_storage = get_tmp_filestorage()
result = this_storage._resolve_filepath(['dir1', 'dir2', 'filename.jpg'])
assert result == os.path.join(
tmpdir, 'dir1/dir2/filename.jpg')
result = this_storage._resolve_filepath(['../../etc/', 'passwd'])
assert result == os.path.join(
tmpdir, 'etc/passwd')
pytest.raises(
storage.InvalidFilepath,
this_storage._resolve_filepath,
['../../', 'etc', 'passwd'])
cleanup_storage(this_storage, tmpdir)
def test_basic_storage_file_exists():
tmpdir, this_storage = get_tmp_filestorage()
os.makedirs(os.path.join(tmpdir, 'dir1', 'dir2'))
filename = os.path.join(tmpdir, 'dir1', 'dir2', 'filename.txt')
with open(filename, 'w') as ourfile:
ourfile.write("I'm having a lovely day!")
assert this_storage.file_exists(['dir1', 'dir2', 'filename.txt'])
assert not this_storage.file_exists(['dir1', 'dir2', 'thisfile.lol'])
assert not this_storage.file_exists(['dnedir1', 'dnedir2', 'somefile.lol'])
this_storage.delete_file(['dir1', 'dir2', 'filename.txt'])
cleanup_storage(this_storage, tmpdir, ['dir1', 'dir2'])
def test_basic_storage_get_unique_filepath():
tmpdir, this_storage = get_tmp_filestorage()
# write something that exists
os.makedirs(os.path.join(tmpdir, 'dir1', 'dir2'))
filename = os.path.join(tmpdir, 'dir1', 'dir2', 'filename.txt')
with open(filename, 'w') as ourfile:
ourfile.write("I'm having a lovely day!")
# now we want something new, with the same name!
new_filepath = this_storage.get_unique_filepath(
['dir1', 'dir2', 'filename.txt'])
assert new_filepath[:-1] == ['dir1', 'dir2']
new_filename = new_filepath[-1]
assert new_filename.endswith('filename.txt')
assert len(new_filename) > len('filename.txt')
assert new_filename == secure_filename(new_filename)
os.remove(filename)
cleanup_storage(this_storage, tmpdir, ['dir1', 'dir2'])
def test_basic_storage_get_file():
tmpdir, this_storage = get_tmp_filestorage()
# Write a brand new file
filepath = ['dir1', 'dir2', 'ourfile.txt']
with this_storage.get_file(filepath, 'w') as our_file:
our_file.write(b'First file')
with this_storage.get_file(filepath, 'r') as our_file:
assert our_file.read() == b'First file'
assert os.path.exists(os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'))
with open(os.path.join(tmpdir, 'dir1/dir2/ourfile.txt')) as our_file:
assert our_file.read() == 'First file'
# Write to the same path but try to get a unique file.
new_filepath = this_storage.get_unique_filepath(filepath)
assert not os.path.exists(os.path.join(tmpdir, *new_filepath))
with this_storage.get_file(new_filepath, 'w') as our_file:
our_file.write(b'Second file')
with this_storage.get_file(new_filepath, 'r') as our_file:
assert our_file.read() == b'Second file'
assert os.path.exists(os.path.join(tmpdir, *new_filepath))
with open(os.path.join(tmpdir, *new_filepath)) as our_file:
assert our_file.read() == 'Second file'
# Read from an existing file
manually_written_file = os.makedirs(
os.path.join(tmpdir, 'testydir'))
with open(os.path.join(tmpdir, 'testydir/testyfile.txt'), 'w') as testyfile:
testyfile.write('testy file! so testy.')
with this_storage.get_file(['testydir', 'testyfile.txt']) as testyfile:
assert testyfile.read() == b'testy file! so testy.'
this_storage.delete_file(filepath)
this_storage.delete_file(new_filepath)
this_storage.delete_file(['testydir', 'testyfile.txt'])
cleanup_storage(this_storage, tmpdir, ['dir1', 'dir2'], ['testydir'])
def test_basic_storage_delete_file():
tmpdir, this_storage = get_tmp_filestorage()
assert not os.path.exists(
os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'))
filepath = ['dir1', 'dir2', 'ourfile.txt']
with this_storage.get_file(filepath, 'w') as our_file:
our_file.write(b'Testing this file')
assert os.path.exists(
os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'))
assert this_storage.delete_dir(['dir1', 'dir2']) == False
this_storage.delete_file(filepath)
assert this_storage.delete_dir(['dir1', 'dir2']) == True
assert not os.path.exists(
os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'))
cleanup_storage(this_storage, tmpdir, ['dir1'])
def test_basic_storage_url_for_file():
# Not supplying a base_url should actually just bork.
tmpdir, this_storage = get_tmp_filestorage()
pytest.raises(
storage.NoWebServing,
this_storage.file_url,
['dir1', 'dir2', 'filename.txt'])
cleanup_storage(this_storage, tmpdir)
# base_url without domain
tmpdir, this_storage = get_tmp_filestorage('/media/')
result = this_storage.file_url(
['dir1', 'dir2', 'filename.txt'])
expected = '/media/dir1/dir2/filename.txt'
assert result == expected
cleanup_storage(this_storage, tmpdir)
# base_url with domain
tmpdir, this_storage = get_tmp_filestorage(
'http://media.example.org/ourmedia/')
result = this_storage.file_url(
['dir1', 'dir2', 'filename.txt'])
expected = 'http://media.example.org/ourmedia/dir1/dir2/filename.txt'
assert result == expected
cleanup_storage(this_storage, tmpdir)
def test_basic_storage_get_local_path():
tmpdir, this_storage = get_tmp_filestorage()
result = this_storage.get_local_path(
['dir1', 'dir2', 'filename.txt'])
expected = os.path.join(
tmpdir, 'dir1/dir2/filename.txt')
assert result == expected
cleanup_storage(this_storage, tmpdir)
def test_basic_storage_is_local():
tmpdir, this_storage = get_tmp_filestorage()
assert this_storage.local_storage is True
cleanup_storage(this_storage, tmpdir)
def test_basic_storage_copy_locally():
tmpdir, this_storage = get_tmp_filestorage()
dest_tmpdir = tempfile.mkdtemp()
filepath = ['dir1', 'dir2', 'ourfile.txt']
with this_storage.get_file(filepath, 'w') as our_file:
our_file.write(b'Testing this file')
new_file_dest = os.path.join(dest_tmpdir, 'file2.txt')
this_storage.copy_locally(filepath, new_file_dest)
this_storage.delete_file(filepath)
assert open(new_file_dest).read() == 'Testing this file'
os.remove(new_file_dest)
os.rmdir(dest_tmpdir)
cleanup_storage(this_storage, tmpdir, ['dir1', 'dir2'])
def _test_copy_local_to_storage_works(tmpdir, this_storage):
local_filename = tempfile.mktemp()
with open(local_filename, 'w') as tmpfile:
tmpfile.write('haha')
this_storage.copy_local_to_storage(
local_filename, ['dir1', 'dir2', 'copiedto.txt'])
os.remove(local_filename)
assert open(
os.path.join(tmpdir, 'dir1/dir2/copiedto.txt')).read() == 'haha'
this_storage.delete_file(['dir1', 'dir2', 'copiedto.txt'])
cleanup_storage(this_storage, tmpdir, ['dir1', 'dir2'])
def test_basic_storage_copy_local_to_storage():
tmpdir, this_storage = get_tmp_filestorage()
_test_copy_local_to_storage_works(tmpdir, this_storage)
def test_general_storage_copy_local_to_storage():
tmpdir, this_storage = get_tmp_filestorage(fake_remote=True)
_test_copy_local_to_storage_works(tmpdir, this_storage)
|