Commit | Line | Data |
---|---|---|
2616d709 | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
12a100e4 | 2 | # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS. |
2616d709 CAW |
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() | |
52426ae0 E |
33 | assert os.path.isdir(workbench.dir) |
34 | assert workbench.dir.startswith(self.workbench_manager.base_workbench_dir) | |
35 | ||
36 | def test_joinpath(self): | |
37 | this_workbench = self.workbench_manager.create_workbench() | |
38 | tmpname = this_workbench.joinpath('temp.txt') | |
39 | assert tmpname == os.path.join(this_workbench.dir, 'temp.txt') | |
b67a983a | 40 | this_workbench.destroy_self() |
2616d709 CAW |
41 | |
42 | def test_destroy_workbench(self): | |
43 | # kill a workbench | |
2ecee34f | 44 | this_workbench = self.workbench_manager.create_workbench() |
52426ae0 E |
45 | tmpfile_name = this_workbench.joinpath('temp.txt') |
46 | tmpfile = file(tmpfile_name, 'w') | |
2616d709 CAW |
47 | with tmpfile: |
48 | tmpfile.write('lollerskates') | |
49 | ||
52426ae0 | 50 | assert os.path.exists(tmpfile_name) |
2ecee34f | 51 | |
b67a983a E |
52 | wb_dir = this_workbench.dir |
53 | this_workbench.destroy_self() | |
52426ae0 | 54 | assert not os.path.exists(tmpfile_name) |
b67a983a | 55 | assert not os.path.exists(wb_dir) |
f43ecb0f | 56 | |
68ffb136 | 57 | def test_localized_file(self): |
f43ecb0f CAW |
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 | |
52426ae0 | 68 | filename = this_workbench.localized_file(this_storage, filepath) |
f43ecb0f CAW |
69 | assert filename == os.path.join( |
70 | tmpdir, 'dir1/dir2/ourfile.txt') | |
71 | ||
72 | # with a fake remote file storage | |
73 | tmpdir, this_storage = get_tmp_filestorage(fake_remote=True) | |
74 | ||
75 | # ... write a brand new file, again ;) | |
76 | with this_storage.get_file(filepath, 'w') as our_file: | |
77 | our_file.write('Our file') | |
78 | ||
8bfa533f | 79 | filename = this_workbench.localized_file(this_storage, filepath) |
f43ecb0f | 80 | assert filename == os.path.join( |
52426ae0 | 81 | this_workbench.dir, 'ourfile.txt') |
f43ecb0f CAW |
82 | |
83 | # fake remote file storage, filename_if_copying set | |
8bfa533f E |
84 | filename = this_workbench.localized_file( |
85 | this_storage, filepath, 'thisfile') | |
f43ecb0f | 86 | assert filename == os.path.join( |
52426ae0 | 87 | this_workbench.dir, 'thisfile.txt') |
f43ecb0f CAW |
88 | |
89 | # fake remote file storage, filename_if_copying set, | |
90 | # keep_extension_if_copying set to false | |
8bfa533f E |
91 | filename = this_workbench.localized_file( |
92 | this_storage, filepath, 'thisfile.text', False) | |
f43ecb0f | 93 | assert filename == os.path.join( |
52426ae0 | 94 | this_workbench.dir, 'thisfile.text') |