Adding a copy_locally() method to the StorageInterface and giving it a test.
[mediagoblin.git] / mediagoblin / tests / test_storage.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 import os
19 import tempfile
20
21 from nose.tools import assert_raises
22 from werkzeug.utils import secure_filename
23
24 from mediagoblin import storage
25
26
27 ################
28 # Test utilities
29 ################
30
31 def test_clean_listy_filepath():
32 expected = [u'dir1', u'dir2', u'linooks.jpg']
33 assert storage.clean_listy_filepath(
34 ['dir1', 'dir2', 'linooks.jpg']) == expected
35
36 expected = [u'dir1', u'foo_.._nasty', u'linooks.jpg']
37 assert storage.clean_listy_filepath(
38 ['/dir1/', 'foo/../nasty', 'linooks.jpg']) == expected
39
40 expected = [u'etc', u'passwd']
41 assert storage.clean_listy_filepath(
42 ['../../../etc/', 'passwd']) == expected
43
44 assert_raises(
45 storage.InvalidFilepath,
46 storage.clean_listy_filepath,
47 ['../../', 'linooks.jpg'])
48
49
50 class FakeStorageSystem():
51 def __init__(self, foobie, blech, **kwargs):
52 self.foobie = foobie
53 self.blech = blech
54
55
56 def test_storage_system_from_paste_config():
57 this_storage = storage.storage_system_from_paste_config(
58 {'somestorage_base_url': 'http://example.org/moodia/',
59 'somestorage_base_dir': '/tmp/',
60 'somestorage_garbage_arg': 'garbage_arg',
61 'garbage_arg': 'trash'},
62 'somestorage')
63 assert this_storage.base_url == 'http://example.org/moodia/'
64 assert this_storage.base_dir == '/tmp/'
65 assert this_storage.__class__ is storage.BasicFileStorage
66
67 this_storage = storage.storage_system_from_paste_config(
68 {'somestorage_foobie': 'eiboof',
69 'somestorage_blech': 'hcelb',
70 'somestorage_garbage_arg': 'garbage_arg',
71 'garbage_arg': 'trash',
72 'somestorage_storage_class':
73 'mediagoblin.tests.test_storage:FakeStorageSystem'},
74 'somestorage')
75 assert this_storage.foobie == 'eiboof'
76 assert this_storage.blech == 'hcelb'
77 assert this_storage.__class__ is FakeStorageSystem
78
79
80 ##########################
81 # Basic file storage tests
82 ##########################
83
84 def get_tmp_filestorage(mount_url=None):
85 tmpdir = tempfile.mkdtemp()
86 this_storage = storage.BasicFileStorage(tmpdir, mount_url)
87 return tmpdir, this_storage
88
89
90 def test_basic_storage__resolve_filepath():
91 tmpdir, this_storage = get_tmp_filestorage()
92
93 result = this_storage._resolve_filepath(['dir1', 'dir2', 'filename.jpg'])
94 assert result == os.path.join(
95 tmpdir, 'dir1/dir2/filename.jpg')
96
97 result = this_storage._resolve_filepath(['../../etc/', 'passwd'])
98 assert result == os.path.join(
99 tmpdir, 'etc/passwd')
100
101 assert_raises(
102 storage.InvalidFilepath,
103 this_storage._resolve_filepath,
104 ['../../', 'etc', 'passwd'])
105
106
107 def test_basic_storage_file_exists():
108 tmpdir, this_storage = get_tmp_filestorage()
109
110 os.makedirs(os.path.join(tmpdir, 'dir1', 'dir2'))
111 filename = os.path.join(tmpdir, 'dir1', 'dir2', 'filename.txt')
112 with open(filename, 'w') as ourfile:
113 ourfile.write("I'm having a lovely day!")
114
115 assert this_storage.file_exists(['dir1', 'dir2', 'filename.txt'])
116 assert not this_storage.file_exists(['dir1', 'dir2', 'thisfile.lol'])
117 assert not this_storage.file_exists(['dnedir1', 'dnedir2', 'somefile.lol'])
118
119
120 def test_basic_storage_get_unique_filepath():
121 tmpdir, this_storage = get_tmp_filestorage()
122
123 # write something that exists
124 os.makedirs(os.path.join(tmpdir, 'dir1', 'dir2'))
125 filename = os.path.join(tmpdir, 'dir1', 'dir2', 'filename.txt')
126 with open(filename, 'w') as ourfile:
127 ourfile.write("I'm having a lovely day!")
128
129 # now we want something new, with the same name!
130 new_filepath = this_storage.get_unique_filepath(
131 ['dir1', 'dir2', 'filename.txt'])
132 assert new_filepath[:-1] == [u'dir1', u'dir2']
133
134 new_filename = new_filepath[-1]
135 assert new_filename.endswith('filename.txt')
136 assert len(new_filename) > len('filename.txt')
137 assert new_filename == secure_filename(new_filename)
138
139
140 def test_basic_storage_get_file():
141 tmpdir, this_storage = get_tmp_filestorage()
142
143 # Write a brand new file
144 filepath = ['dir1', 'dir2', 'ourfile.txt']
145
146 with this_storage.get_file(filepath, 'w') as our_file:
147 our_file.write('First file')
148 with this_storage.get_file(filepath, 'r') as our_file:
149 assert our_file.read() == 'First file'
150 assert os.path.exists(os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'))
151 with file(os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'), 'r') as our_file:
152 assert our_file.read() == 'First file'
153
154 # Write to the same path but try to get a unique file.
155 new_filepath = this_storage.get_unique_filepath(filepath)
156 assert not os.path.exists(os.path.join(tmpdir, *new_filepath))
157
158 with this_storage.get_file(new_filepath, 'w') as our_file:
159 our_file.write('Second file')
160 with this_storage.get_file(new_filepath, 'r') as our_file:
161 assert our_file.read() == 'Second file'
162 assert os.path.exists(os.path.join(tmpdir, *new_filepath))
163 with file(os.path.join(tmpdir, *new_filepath), 'r') as our_file:
164 assert our_file.read() == 'Second file'
165
166 # Read from an existing file
167 manually_written_file = os.makedirs(
168 os.path.join(tmpdir, 'testydir'))
169 with file(os.path.join(tmpdir, 'testydir/testyfile.txt'), 'w') as testyfile:
170 testyfile.write('testy file! so testy.')
171
172 with this_storage.get_file(['testydir', 'testyfile.txt']) as testyfile:
173 assert testyfile.read() == 'testy file! so testy.'
174
175
176 def test_basic_storage_delete_file():
177 tmpdir, this_storage = get_tmp_filestorage()
178
179 assert not os.path.exists(
180 os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'))
181
182 filepath = ['dir1', 'dir2', 'ourfile.txt']
183 with this_storage.get_file(filepath, 'w') as our_file:
184 our_file.write('Testing this file')
185
186 assert os.path.exists(
187 os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'))
188
189 this_storage.delete_file(filepath)
190
191 assert not os.path.exists(
192 os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'))
193
194
195 def test_basic_storage_url_for_file():
196 # Not supplying a base_url should actually just bork.
197 tmpdir, this_storage = get_tmp_filestorage()
198 assert_raises(
199 storage.NoWebServing,
200 this_storage.file_url,
201 ['dir1', 'dir2', 'filename.txt'])
202
203 # base_url without domain
204 tmpdir, this_storage = get_tmp_filestorage('/media/')
205 result = this_storage.file_url(
206 ['dir1', 'dir2', 'filename.txt'])
207 expected = '/media/dir1/dir2/filename.txt'
208 assert result == expected
209
210 # base_url with domain
211 tmpdir, this_storage = get_tmp_filestorage(
212 'http://media.example.org/ourmedia/')
213 result = this_storage.file_url(
214 ['dir1', 'dir2', 'filename.txt'])
215 expected = 'http://media.example.org/ourmedia/dir1/dir2/filename.txt'
216 assert result == expected
217
218
219 def test_basic_storage_get_local_path():
220 tmpdir, this_storage = get_tmp_filestorage()
221
222 result = this_storage.get_local_path(
223 ['dir1', 'dir2', 'filename.txt'])
224
225 expected = os.path.join(
226 tmpdir, 'dir1/dir2/filename.txt')
227
228 assert result == expected
229
230
231 def test_basic_storage_is_local():
232 tmpdir, this_storage = get_tmp_filestorage()
233 assert this_storage.local_storage is True
234
235
236 def test_basic_storage_copy_locally():
237 tmpdir, this_storage = get_tmp_filestorage()
238
239 dest_tmpdir = tempfile.mkdtemp()
240
241 filepath = ['dir1', 'dir2', 'ourfile.txt']
242 with this_storage.get_file(filepath, 'w') as our_file:
243 our_file.write('Testing this file')
244
245 new_file_dest = os.path.join(dest_tmpdir, 'file2.txt')
246
247 this_storage.copy_locally(filepath, new_file_dest)
248
249 assert file(new_file_dest).read() == 'Testing this file'