Commit | Line | Data |
---|---|---|
2616d709 CAW |
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 | import os | |
18 | import tempfile | |
19 | ||
2ecee34f CAW |
20 | from nose.tools import assert_raises |
21 | ||
a32acafa | 22 | from mediagoblin import workbench |
f43ecb0f | 23 | from mediagoblin.tests.test_storage import get_tmp_filestorage |
2616d709 CAW |
24 | |
25 | ||
26 | class TestWorkbench(object): | |
27 | def setUp(self): | |
28 | self.workbench_manager = workbench.WorkbenchManager( | |
29 | os.path.join(tempfile.gettempdir(), u'mgoblin_workbench_testing')) | |
30 | ||
31 | def test_create_workbench(self): | |
32 | workbench = self.workbench_manager.create_workbench() | |
33 | assert os.path.isdir(workbench) | |
34 | assert workbench.startswith(self.workbench_manager.base_workbench_dir) | |
35 | ||
36 | def test_destroy_workbench(self): | |
37 | # kill a workbench | |
2ecee34f CAW |
38 | this_workbench = self.workbench_manager.create_workbench() |
39 | tmpfile = file(os.path.join(this_workbench, 'temp.txt'), 'w') | |
2616d709 CAW |
40 | with tmpfile: |
41 | tmpfile.write('lollerskates') | |
42 | ||
2ecee34f CAW |
43 | assert os.path.exists(os.path.join(this_workbench, 'temp.txt')) |
44 | ||
45 | self.workbench_manager.destroy_workbench(this_workbench) | |
46 | assert not os.path.exists(os.path.join(this_workbench, 'temp.txt')) | |
47 | assert not os.path.exists(this_workbench) | |
48 | ||
49 | # make sure we can't kill other stuff though | |
50 | dont_kill_this = tempfile.mkdtemp() | |
2616d709 | 51 | |
2ecee34f CAW |
52 | assert_raises( |
53 | workbench.WorkbenchOutsideScope, | |
54 | self.workbench_manager.destroy_workbench, | |
55 | dont_kill_this) | |
f43ecb0f CAW |
56 | |
57 | def test_possibly_localize_file(self): | |
58 | tmpdir, this_storage = get_tmp_filestorage() | |
59 | this_workbench = self.workbench_manager.create_workbench() | |
60 | ||
61 | # Write a brand new file | |
62 | filepath = ['dir1', 'dir2', 'ourfile.txt'] | |
63 | ||
64 | with this_storage.get_file(filepath, 'w') as our_file: | |
65 | our_file.write('Our file') | |
66 | ||
67 | # with a local file storage | |
fdc50039 | 68 | filename = self.workbench_manager.possibly_localize_file( |
f43ecb0f | 69 | this_workbench, this_storage, filepath) |
f43ecb0f CAW |
70 | assert filename == os.path.join( |
71 | tmpdir, 'dir1/dir2/ourfile.txt') | |
72 | ||
73 | # with a fake remote file storage | |
74 | tmpdir, this_storage = get_tmp_filestorage(fake_remote=True) | |
75 | ||
76 | # ... write a brand new file, again ;) | |
77 | with this_storage.get_file(filepath, 'w') as our_file: | |
78 | our_file.write('Our file') | |
79 | ||
fdc50039 | 80 | filename = self.workbench_manager.possibly_localize_file( |
f43ecb0f CAW |
81 | this_workbench, this_storage, filepath) |
82 | assert filename == os.path.join( | |
83 | this_workbench, 'ourfile.txt') | |
84 | ||
85 | # fake remote file storage, filename_if_copying set | |
fdc50039 | 86 | filename = self.workbench_manager.possibly_localize_file( |
f43ecb0f CAW |
87 | this_workbench, this_storage, filepath, 'thisfile') |
88 | assert filename == os.path.join( | |
89 | this_workbench, 'thisfile.txt') | |
90 | ||
91 | # fake remote file storage, filename_if_copying set, | |
92 | # keep_extension_if_copying set to false | |
fdc50039 | 93 | filename = self.workbench_manager.possibly_localize_file( |
f43ecb0f CAW |
94 | this_workbench, this_storage, filepath, 'thisfile.text', False) |
95 | assert filename == os.path.join( | |
96 | this_workbench, 'thisfile.text') |