1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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.
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.
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/>.
21 from nose
.tools
import assert_raises
22 from werkzeug
.utils
import secure_filename
24 from mediagoblin
import storage
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
36 expected
= [u
'dir1', u
'foo_.._nasty', u
'linooks.jpg']
37 assert storage
.clean_listy_filepath(
38 ['/dir1/', 'foo/../nasty', 'linooks.jpg']) == expected
40 expected
= [u
'etc', u
'passwd']
41 assert storage
.clean_listy_filepath(
42 ['../../../etc/', 'passwd']) == expected
45 storage
.InvalidFilepath
,
46 storage
.clean_listy_filepath
,
47 ['../../', 'linooks.jpg'])
50 class FakeStorageSystem():
51 def __init__(self
, foobie
, blech
, **kwargs
):
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'},
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
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'},
75 assert this_storage
.foobie
== 'eiboof'
76 assert this_storage
.blech
== 'hcelb'
77 assert this_storage
.__class
__ is FakeStorageSystem
80 ##########################
81 # Basic file storage tests
82 ##########################
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
90 def test_basic_storage__resolve_filepath():
91 tmpdir
, this_storage
= get_tmp_filestorage()
93 result
= this_storage
._resolve
_filepath
(['dir1', 'dir2', 'filename.jpg'])
94 assert result
== os
.path
.join(
95 tmpdir
, 'dir1/dir2/filename.jpg')
97 result
= this_storage
._resolve
_filepath
(['../../etc/', 'passwd'])
98 assert result
== os
.path
.join(
102 storage
.InvalidFilepath
,
103 this_storage
._resolve
_filepath
,
104 ['../../', 'etc', 'passwd'])
107 def test_basic_storage_file_exists():
108 tmpdir
, this_storage
= get_tmp_filestorage()
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!")
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'])
120 def test_basic_storage_get_unique_filepath():
121 tmpdir
, this_storage
= get_tmp_filestorage()
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!")
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']
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
)
140 def test_basic_storage_get_file():
141 tmpdir
, this_storage
= get_tmp_filestorage()
143 # Write a brand new file
144 filepath
= ['dir1', 'dir2', 'ourfile.txt']
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'
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
))
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'
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.')
172 with this_storage
.get_file(['testydir', 'testyfile.txt']) as testyfile
:
173 assert testyfile
.read() == 'testy file! so testy.'
176 def test_basic_storage_delete_file():
177 tmpdir
, this_storage
= get_tmp_filestorage()
179 assert not os
.path
.exists(
180 os
.path
.join(tmpdir
, 'dir1/dir2/ourfile.txt'))
182 filepath
= ['dir1', 'dir2', 'ourfile.txt']
183 with this_storage
.get_file(filepath
, 'w') as our_file
:
184 our_file
.write('Testing this file')
186 assert os
.path
.exists(
187 os
.path
.join(tmpdir
, 'dir1/dir2/ourfile.txt'))
189 this_storage
.delete_file(filepath
)
191 assert not os
.path
.exists(
192 os
.path
.join(tmpdir
, 'dir1/dir2/ourfile.txt'))
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()
199 storage
.NoWebServing
,
200 this_storage
.file_url
,
201 ['dir1', 'dir2', 'filename.txt'])
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
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