a30ca149fbb9f515af25c84dffa6e704145649e0
[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
23 from mediagoblin import storage
24
25
26 def test_clean_listy_filepath():
27 expected = [u'dir1', u'dir2', u'linooks.jpg']
28 assert storage.clean_listy_filepath(
29 ['dir1', 'dir2', 'linooks.jpg']) == expected
30
31 expected = [u'dir1', u'foo_.._nasty', u'linooks.jpg']
32 assert storage.clean_listy_filepath(
33 ['/dir1/', 'foo/../nasty', 'linooks.jpg']) == expected
34
35 expected = [u'etc', u'passwd']
36 assert storage.clean_listy_filepath(
37 ['../../../etc/', 'passwd']) == expected
38
39 assert_raises(
40 storage.InvalidFilepath,
41 storage.clean_listy_filepath,
42 ['../../', 'linooks.jpg'])
43
44
45 ##########################
46 # Basic file storage tests
47 ##########################
48
49 def get_tmp_filestorage(mount_url=None):
50 tmpdir = tempfile.mkdtemp()
51 this_storage = storage.BasicFileStorage(tmpdir, mount_url)
52 return tmpdir, this_storage
53
54
55 def test_basic_storage__resolve_filepath():
56 tmpdir, this_storage = get_tmp_filestorage()
57
58 result = this_storage._resolve_filepath(['dir1', 'dir2', 'filename.jpg'])
59 assert result == os.path.join(
60 tmpdir, 'dir1/dir2/filename.jpg')
61
62 result = this_storage._resolve_filepath(['../../etc/', 'passwd'])
63 assert result == os.path.join(
64 tmpdir, 'etc/passwd')
65
66 assert_raises(
67 storage.InvalidFilepath,
68 this_storage._resolve_filepath,
69 ['../../', 'etc', 'passwd'])
70
71
72 def test_basic_storage_file_exists():
73 pass
74
75
76 def test_basic_storage_get_file():
77 pass
78
79
80 def test_basic_storage_delete_file():
81 pass
82
83
84 def test_basic_storage_url_for_file():
85 pass