示例#1
0
class FileStoragePathParsing(unittest.TestCase):
    def setUp(self):
        self.storage_dir = tempfile.mkdtemp()
        self.storage = FileSystemStorage(self.storage_dir)

    def tearDown(self):
        shutil.rmtree(self.storage_dir)

    def test_directory_with_dot(self):
        """Regression test for #9610.

        If the directory name contains a dot and the file name doesn't, make
        sure we still mangle the file name instead of the directory name.
        """

        self.storage.save('dotted.path/test', ContentFile("1"))
        self.storage.save('dotted.path/test', ContentFile("2"))

        self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
        self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test')))
        self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test_1')))

    def test_first_character_dot(self):
        """
        File names with a dot as their first character don't have an extension,
        and the underscore should get added to the end.
        """
        self.storage.save('dotted.path/.test', ContentFile("1"))
        self.storage.save('dotted.path/.test', ContentFile("2"))

        self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test')))
        self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test_1')))
示例#2
0
 def __init__(self, apps=None, *args, **kwargs):
     # List of locations with static files
     self.locations = []
     # Maps dir paths to an appropriate storage instance
     self.storages = SortedDict()
     if not isinstance(settings.STATICFILES_DIRS, (list, tuple)):
         raise ImproperlyConfigured(
             "Your STATICFILES_DIRS setting is not a tuple or list; "
             "perhaps you forgot a trailing comma?")
     for root in settings.STATICFILES_DIRS:
         if isinstance(root, (list, tuple)):
             prefix, root = root
         else:
             prefix = ''
         if os.path.abspath(settings.STATIC_ROOT) == os.path.abspath(root):
             raise ImproperlyConfigured(
                 "The STATICFILES_DIRS setting should "
                 "not contain the STATIC_ROOT setting")
         if (prefix, root) not in self.locations:
             self.locations.append((prefix, root))
     for prefix, root in self.locations:
         filesystem_storage = FileSystemStorage(location=root)
         filesystem_storage.prefix = prefix
         self.storages[root] = filesystem_storage
     super(FileSystemFinder, self).__init__(*args, **kwargs)
示例#3
0
class FileStoragePermissions(unittest.TestCase):
    def setUp(self):
        self.old_perms = settings.FILE_UPLOAD_PERMISSIONS
        settings.FILE_UPLOAD_PERMISSIONS = 0o666
        self.storage_dir = tempfile.mkdtemp()
        self.storage = FileSystemStorage(self.storage_dir)

    def tearDown(self):
        settings.FILE_UPLOAD_PERMISSIONS = self.old_perms
        shutil.rmtree(self.storage_dir)

    def test_file_upload_permissions(self):
        name = self.storage.save("the_file", ContentFile("data"))
        actual_mode = os.stat(self.storage.path(name))[0] & 0o777
        self.assertEqual(actual_mode, 0o666)
示例#4
0
class FileLikeObjectTestCase(LiveServerBase):
    """
    Test file-like objects (#15644).
    """
    def setUp(self):
        self.temp_dir = tempfile.mkdtemp()
        self.storage = FileSystemStorage(location=self.temp_dir)

    def tearDown(self):
        shutil.rmtree(self.temp_dir)

    def test_urllib2_urlopen(self):
        """
        Test the File storage API with a file like object coming from urllib2.urlopen()
        """

        file_like_object = self.urlopen('/example_view/')
        f = File(file_like_object)
        stored_filename = self.storage.save("remote_file.html", f)

        stored_file = self.storage.open(stored_filename)
        remote_file = self.urlopen('/example_view/')

        self.assertEqual(stored_file.read(), remote_file.read())
示例#5
0
class FileSaveRaceConditionTest(unittest.TestCase):
    def setUp(self):
        self.storage_dir = tempfile.mkdtemp()
        self.storage = FileSystemStorage(self.storage_dir)
        self.thread = threading.Thread(target=self.save_file, args=['conflict'])

    def tearDown(self):
        shutil.rmtree(self.storage_dir)

    def save_file(self, name):
        name = self.storage.save(name, SlowFile(b"Data"))

    def test_race_condition(self):
        self.thread.start()
        name = self.save_file('conflict')
        self.thread.join()
        self.assertTrue(self.storage.exists('conflict'))
        self.assertTrue(self.storage.exists('conflict_1'))
        self.storage.delete('conflict')
        self.storage.delete('conflict_1')
示例#6
0
 def setUp(self):
     self.temp_dir = tempfile.mkdtemp()
     self.storage = FileSystemStorage(location=self.temp_dir)
示例#7
0
 def setUp(self):
     self.storage_dir = tempfile.mkdtemp()
     self.storage = FileSystemStorage(self.storage_dir)
示例#8
0
 def setUp(self):
     self.old_perms = settings.FILE_UPLOAD_PERMISSIONS
     settings.FILE_UPLOAD_PERMISSIONS = 0o666
     self.storage_dir = tempfile.mkdtemp()
     self.storage = FileSystemStorage(self.storage_dir)
示例#9
0
 def setUp(self):
     self.storage_dir = tempfile.mkdtemp()
     self.storage = FileSystemStorage(self.storage_dir)
     self.thread = threading.Thread(target=self.save_file, args=['conflict'])