Django

Code

Ticket #6450: directory_creation_tests.diff

File directory_creation_tests.diff, 2.5 kB (added by vung, 6 months ago)
  • tests/regressiontests/directory_creation/__init__.py

    old new  
  • tests/regressiontests/directory_creation/tests.py

    old new  
     1# -*- coding: utf-8 -*- 
     2""" 
     3Tests for directory creation via _save_FIELD_file (ticket #6450) 
     4""" 
     5import shutil 
     6import unittest 
     7import os 
     8import errno 
     9 
     10from regressiontests.directory_creation.models import Foo, UPLOAD_ROOT, \ 
     11    UPLOAD_TO 
     12 
     13 
     14class 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") 
  • tests/regressiontests/directory_creation/models.py

    old new  
     1import tempfile 
     2import os 
     3from django.db import models 
     4 
     5 
     6UPLOAD_ROOT = tempfile.mkdtemp() 
     7UPLOAD_TO = os.path.join(UPLOAD_ROOT, 'foo_upload') 
     8 
     9 
     10class Foo(models.Model): 
     11    f = models.FileField(upload_to=UPLOAD_TO)