2009-05-08 18:56:51 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-08-28 06:21:14 +08:00
|
|
|
import os
|
2008-10-11 06:13:16 +08:00
|
|
|
import shutil
|
2009-05-08 13:50:31 +08:00
|
|
|
import sys
|
2008-10-11 06:13:16 +08:00
|
|
|
import tempfile
|
2009-05-08 18:56:51 +08:00
|
|
|
import time
|
|
|
|
import unittest
|
|
|
|
from cStringIO import StringIO
|
2008-08-28 06:21:14 +08:00
|
|
|
from django.conf import settings
|
2009-05-08 18:56:51 +08:00
|
|
|
from django.core.exceptions import SuspiciousOperation
|
2008-08-12 00:51:18 +08:00
|
|
|
from django.core.files.base import ContentFile
|
2009-05-08 18:56:51 +08:00
|
|
|
from django.core.files.images import get_image_dimensions
|
2008-10-11 06:13:16 +08:00
|
|
|
from django.core.files.storage import FileSystemStorage
|
2009-05-08 18:56:51 +08:00
|
|
|
from django.core.files.uploadedfile import UploadedFile
|
|
|
|
from unittest import TestCase
|
2008-08-12 00:51:18 +08:00
|
|
|
try:
|
|
|
|
import threading
|
|
|
|
except ImportError:
|
|
|
|
import dummy_threading as threading
|
|
|
|
|
2010-02-15 02:28:28 +08:00
|
|
|
# Try to import PIL in either of the two ways it can end up installed.
|
|
|
|
# Checking for the existence of Image is enough for CPython, but
|
|
|
|
# for PyPy, you need to check for the underlying modules
|
2009-05-08 18:56:51 +08:00
|
|
|
try:
|
|
|
|
from PIL import Image, _imaging
|
|
|
|
except ImportError:
|
2010-02-15 02:28:28 +08:00
|
|
|
try:
|
|
|
|
import Image, _imaging
|
|
|
|
except ImportError:
|
|
|
|
Image = None
|
2009-05-08 18:56:51 +08:00
|
|
|
|
|
|
|
class FileStorageTests(unittest.TestCase):
|
|
|
|
storage_class = FileSystemStorage
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.temp_dir = tempfile.mktemp()
|
|
|
|
os.makedirs(self.temp_dir)
|
|
|
|
self.storage = self.storage_class(location=self.temp_dir)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.rmdir(self.temp_dir)
|
|
|
|
|
|
|
|
def test_file_access_options(self):
|
|
|
|
"""
|
|
|
|
Standard file access options are available, and work as expected.
|
|
|
|
"""
|
|
|
|
self.failIf(self.storage.exists('storage_test'))
|
|
|
|
f = self.storage.open('storage_test', 'w')
|
|
|
|
f.write('storage contents')
|
|
|
|
f.close()
|
|
|
|
self.assert_(self.storage.exists('storage_test'))
|
|
|
|
|
|
|
|
f = self.storage.open('storage_test', 'r')
|
|
|
|
self.assertEqual(f.read(), 'storage contents')
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
self.storage.delete('storage_test')
|
|
|
|
self.failIf(self.storage.exists('storage_test'))
|
|
|
|
|
|
|
|
def test_file_storage_prevents_directory_traversal(self):
|
|
|
|
"""
|
|
|
|
File storage prevents directory traversal (files can only be accessed if
|
|
|
|
they're below the storage location).
|
|
|
|
"""
|
|
|
|
self.assertRaises(SuspiciousOperation, self.storage.exists, '..')
|
|
|
|
self.assertRaises(SuspiciousOperation, self.storage.exists, '/etc/passwd')
|
|
|
|
|
|
|
|
class CustomStorage(FileSystemStorage):
|
|
|
|
def get_available_name(self, name):
|
|
|
|
"""
|
|
|
|
Append numbers to duplicate files rather than underscores, like Trac.
|
|
|
|
"""
|
|
|
|
parts = name.split('.')
|
|
|
|
basename, ext = parts[0], parts[1:]
|
|
|
|
number = 2
|
|
|
|
while self.exists(name):
|
|
|
|
name = '.'.join([basename, str(number)] + ext)
|
|
|
|
number += 1
|
|
|
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
class CustomStorageTests(FileStorageTests):
|
|
|
|
storage_class = CustomStorage
|
|
|
|
|
|
|
|
def test_custom_get_available_name(self):
|
|
|
|
first = self.storage.save('custom_storage', ContentFile('custom contents'))
|
|
|
|
self.assertEqual(first, 'custom_storage')
|
|
|
|
second = self.storage.save('custom_storage', ContentFile('more contents'))
|
|
|
|
self.assertEqual(second, 'custom_storage.2')
|
|
|
|
self.storage.delete(first)
|
|
|
|
self.storage.delete(second)
|
|
|
|
|
|
|
|
class UnicodeFileNameTests(unittest.TestCase):
|
|
|
|
def test_unicode_file_names(self):
|
|
|
|
"""
|
|
|
|
Regression test for #8156: files with unicode names I can't quite figure
|
|
|
|
out the encoding situation between doctest and this file, but the actual
|
|
|
|
repr doesn't matter; it just shouldn't return a unicode object.
|
|
|
|
"""
|
|
|
|
uf = UploadedFile(name=u'¿Cómo?',content_type='text')
|
|
|
|
self.assertEqual(type(uf.__repr__()), str)
|
|
|
|
|
|
|
|
# Tests for a race condition on file saving (#4948).
|
|
|
|
# This is written in such a way that it'll always pass on platforms
|
|
|
|
# without threading.
|
|
|
|
|
2008-08-12 00:51:18 +08:00
|
|
|
class SlowFile(ContentFile):
|
|
|
|
def chunks(self):
|
|
|
|
time.sleep(1)
|
|
|
|
return super(ContentFile, self).chunks()
|
|
|
|
|
|
|
|
class FileSaveRaceConditionTest(TestCase):
|
|
|
|
def setUp(self):
|
2008-10-11 06:13:16 +08:00
|
|
|
self.storage_dir = tempfile.mkdtemp()
|
|
|
|
self.storage = FileSystemStorage(self.storage_dir)
|
2008-08-12 00:51:18 +08:00
|
|
|
self.thread = threading.Thread(target=self.save_file, args=['conflict'])
|
2009-05-08 13:50:31 +08:00
|
|
|
|
2008-10-11 06:13:16 +08:00
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.storage_dir)
|
2009-05-08 13:50:31 +08:00
|
|
|
|
2008-08-12 00:51:18 +08:00
|
|
|
def save_file(self, name):
|
2008-10-11 06:13:16 +08:00
|
|
|
name = self.storage.save(name, SlowFile("Data"))
|
2009-05-08 13:50:31 +08:00
|
|
|
|
2008-08-12 00:51:18 +08:00
|
|
|
def test_race_condition(self):
|
|
|
|
self.thread.start()
|
|
|
|
name = self.save_file('conflict')
|
|
|
|
self.thread.join()
|
2008-10-11 06:13:16 +08:00
|
|
|
self.assert_(self.storage.exists('conflict'))
|
2010-02-24 06:39:22 +08:00
|
|
|
self.assert_(self.storage.exists('conflict_1'))
|
2008-10-11 06:13:16 +08:00
|
|
|
self.storage.delete('conflict')
|
2010-02-24 06:39:22 +08:00
|
|
|
self.storage.delete('conflict_1')
|
2008-08-12 00:51:18 +08:00
|
|
|
|
2008-08-28 06:21:14 +08:00
|
|
|
class FileStoragePermissions(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.old_perms = settings.FILE_UPLOAD_PERMISSIONS
|
|
|
|
settings.FILE_UPLOAD_PERMISSIONS = 0666
|
2008-10-11 06:13:16 +08:00
|
|
|
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)
|
|
|
|
|
2008-08-28 06:21:14 +08:00
|
|
|
def test_file_upload_permissions(self):
|
2008-10-11 06:13:16 +08:00
|
|
|
name = self.storage.save("the_file", ContentFile("data"))
|
|
|
|
actual_mode = os.stat(self.storage.path(name))[0] & 0777
|
2008-08-28 06:21:14 +08:00
|
|
|
self.assertEqual(actual_mode, 0666)
|
2008-10-11 06:13:16 +08:00
|
|
|
|
2009-05-08 13:50:31 +08:00
|
|
|
|
|
|
|
class FileStoragePathParsing(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"))
|
|
|
|
|
2009-05-29 13:23:50 +08:00
|
|
|
self.failIf(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
|
|
|
|
self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test')))
|
2010-02-24 06:39:22 +08:00
|
|
|
self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test_1')))
|
2009-05-08 13:50:31 +08:00
|
|
|
|
|
|
|
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"))
|
|
|
|
|
2009-05-29 13:23:50 +08:00
|
|
|
self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test')))
|
2009-05-08 13:50:31 +08:00
|
|
|
# Before 2.6, a leading dot was treated as an extension, and so
|
|
|
|
# underscore gets added to beginning instead of end.
|
|
|
|
if sys.version_info < (2, 6):
|
2009-05-29 13:23:50 +08:00
|
|
|
self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/_.test')))
|
2009-05-08 13:50:31 +08:00
|
|
|
else:
|
2010-02-24 06:39:22 +08:00
|
|
|
self.assert_(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test_1')))
|
2009-05-08 18:56:51 +08:00
|
|
|
|
|
|
|
if Image is not None:
|
|
|
|
class DimensionClosingBug(TestCase):
|
|
|
|
"""
|
|
|
|
Test that get_image_dimensions() properly closes files (#8817)
|
|
|
|
"""
|
|
|
|
def test_not_closing_of_files(self):
|
|
|
|
"""
|
|
|
|
Open files passed into get_image_dimensions() should stay opened.
|
|
|
|
"""
|
|
|
|
empty_io = StringIO()
|
|
|
|
try:
|
|
|
|
get_image_dimensions(empty_io)
|
|
|
|
finally:
|
|
|
|
self.assert_(not empty_io.closed)
|
|
|
|
|
|
|
|
def test_closing_of_filenames(self):
|
|
|
|
"""
|
|
|
|
get_image_dimensions() called with a filename should closed the file.
|
|
|
|
"""
|
|
|
|
# We need to inject a modified open() builtin into the images module
|
|
|
|
# that checks if the file was closed properly if the function is
|
|
|
|
# called with a filename instead of an file object.
|
|
|
|
# get_image_dimensions will call our catching_open instead of the
|
|
|
|
# regular builtin one.
|
|
|
|
|
|
|
|
class FileWrapper(object):
|
|
|
|
_closed = []
|
|
|
|
def __init__(self, f):
|
|
|
|
self.f = f
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return getattr(self.f, name)
|
|
|
|
def close(self):
|
|
|
|
self._closed.append(True)
|
|
|
|
self.f.close()
|
|
|
|
|
|
|
|
def catching_open(*args):
|
|
|
|
return FileWrapper(open(*args))
|
|
|
|
|
|
|
|
from django.core.files import images
|
|
|
|
images.open = catching_open
|
|
|
|
try:
|
|
|
|
get_image_dimensions(os.path.join(os.path.dirname(__file__), "test1.png"))
|
|
|
|
finally:
|
|
|
|
del images.open
|
|
|
|
self.assert_(FileWrapper._closed)
|