| 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
| 2 | # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS. |
| 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 | |
| 20 | from nose.tools import assert_raises |
| 21 | |
| 22 | from mediagoblin import workbench |
| 23 | from mediagoblin.tests.test_storage import get_tmp_filestorage |
| 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.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') |
| 40 | this_workbench.destroy_self() |
| 41 | |
| 42 | def test_destroy_workbench(self): |
| 43 | # kill a workbench |
| 44 | this_workbench = self.workbench_manager.create_workbench() |
| 45 | tmpfile_name = this_workbench.joinpath('temp.txt') |
| 46 | tmpfile = file(tmpfile_name, 'w') |
| 47 | with tmpfile: |
| 48 | tmpfile.write('lollerskates') |
| 49 | |
| 50 | assert os.path.exists(tmpfile_name) |
| 51 | |
| 52 | wb_dir = this_workbench.dir |
| 53 | this_workbench.destroy_self() |
| 54 | assert not os.path.exists(tmpfile_name) |
| 55 | assert not os.path.exists(wb_dir) |
| 56 | |
| 57 | def test_localized_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 |
| 68 | filename = this_workbench.localized_file(this_storage, filepath) |
| 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 | |
| 79 | filename = this_workbench.localized_file(this_storage, filepath) |
| 80 | assert filename == os.path.join( |
| 81 | this_workbench.dir, 'ourfile.txt') |
| 82 | |
| 83 | # fake remote file storage, filename_if_copying set |
| 84 | filename = this_workbench.localized_file( |
| 85 | this_storage, filepath, 'thisfile') |
| 86 | assert filename == os.path.join( |
| 87 | this_workbench.dir, 'thisfile.txt') |
| 88 | |
| 89 | # fake remote file storage, filename_if_copying set, |
| 90 | # keep_extension_if_copying set to false |
| 91 | filename = this_workbench.localized_file( |
| 92 | this_storage, filepath, 'thisfile.text', False) |
| 93 | assert filename == os.path.join( |
| 94 | this_workbench.dir, 'thisfile.text') |