| 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | Tests for directory creation via _save_FIELD_file (ticket #6450) |
| 4 | """ |
| 5 | import shutil |
| 6 | import unittest |
| 7 | import os |
| 8 | import errno |
| 9 | |
| 10 | from regressiontests.directory_creation.models import Foo, UPLOAD_ROOT, \ |
| 11 | UPLOAD_TO |
| 12 | |
| 13 | |
| 14 | class TestDirectoryCreation(unittest.TestCase): |
| 15 | |
| 16 | def setUp(self): |
| 17 | self.obj = Foo() |
| 18 | if not os.path.isdir(UPLOAD_ROOT): |
| 19 | os.makedirs(UPLOAD_ROOT) |
| 20 | |
| 21 | def tearDown(self): |
| 22 | os.chmod(UPLOAD_ROOT, 0700) |
| 23 | shutil.rmtree(UPLOAD_ROOT) |
| 24 | |
| 25 | def test_readonly_root(self): |
| 26 | """Test that permission errors are not swallowed""" |
| 27 | os.chmod(UPLOAD_ROOT, 0500) |
| 28 | try: |
| 29 | self.obj.save_f_file('foo.txt', 'x') |
| 30 | except OSError, err: |
| 31 | self.assertEquals(err.errno, errno.EACCES) |
| 32 | except: |
| 33 | self.fail("OSError [Errno %s] not raised" % errno.EACCES) |
| 34 | |
| 35 | def test_not_a_directory(self): |
| 36 | """Test that the expected IOError is raised""" |
| 37 | # create a file with the upload directory name: |
| 38 | fd = open(UPLOAD_TO, 'w') |
| 39 | fd.close() |
| 40 | try: |
| 41 | self.obj.save_f_file('foo.txt', 'x') |
| 42 | except IOError, err: |
| 43 | # The test needs to be done on a specific string as IOError |
| 44 | # is raised even without the patch (just not early enough) |
| 45 | self.assertEquals(err.args[0], |
| 46 | "%s exists and is not a directory" % UPLOAD_TO) |
| 47 | except: |
| 48 | self.fail("IOError not raised") |