2009-05-08 12:56:51 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-07 18:08:47 +02:00
|
|
|
from __future__ import absolute_import, unicode_literals
|
2012-04-05 17:44:04 +02:00
|
|
|
|
2011-05-26 10:21:35 +02:00
|
|
|
import errno
|
2011-10-13 20:51:33 +02:00
|
|
|
import os
|
2008-10-11 00:13:16 +02:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
2009-05-08 12:56:51 +02:00
|
|
|
import time
|
2010-10-08 17:11:59 +02:00
|
|
|
from datetime import datetime, timedelta
|
2012-05-05 19:47:03 +02:00
|
|
|
from io import BytesIO
|
2010-11-21 18:51:41 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
import threading
|
|
|
|
except ImportError:
|
|
|
|
import dummy_threading as threading
|
|
|
|
|
2008-08-28 00:21:14 +02:00
|
|
|
from django.conf import settings
|
2010-12-04 08:28:12 +01:00
|
|
|
from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
|
2012-04-05 17:44:04 +02:00
|
|
|
from django.core.files.base import File, ContentFile
|
2009-05-08 12:56:51 +02:00
|
|
|
from django.core.files.images import get_image_dimensions
|
2010-09-14 01:15:06 +02:00
|
|
|
from django.core.files.storage import FileSystemStorage, get_storage_class
|
2009-05-08 12:56:51 +02:00
|
|
|
from django.core.files.uploadedfile import UploadedFile
|
2011-08-13 02:42:08 +02:00
|
|
|
from django.test import SimpleTestCase
|
2010-10-11 14:55:17 +02:00
|
|
|
from django.utils import unittest
|
2012-04-05 17:44:04 +02:00
|
|
|
from ..servers.tests import LiveServerBase
|
2010-10-11 14:55:17 +02:00
|
|
|
|
2010-02-14 19:28:28 +01: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 12:56:51 +02:00
|
|
|
try:
|
|
|
|
from PIL import Image, _imaging
|
|
|
|
except ImportError:
|
2010-02-14 19:28:28 +01:00
|
|
|
try:
|
|
|
|
import Image, _imaging
|
|
|
|
except ImportError:
|
|
|
|
Image = None
|
2009-05-08 12:56:51 +02:00
|
|
|
|
2010-11-21 18:51:41 +01:00
|
|
|
|
2011-08-13 02:42:08 +02:00
|
|
|
class GetStorageClassTests(SimpleTestCase):
|
2010-09-14 01:15:06 +02:00
|
|
|
|
|
|
|
def test_get_filesystem_storage(self):
|
|
|
|
"""
|
|
|
|
get_storage_class returns the class for a storage backend name/path.
|
|
|
|
"""
|
|
|
|
self.assertEqual(
|
|
|
|
get_storage_class('django.core.files.storage.FileSystemStorage'),
|
|
|
|
FileSystemStorage)
|
|
|
|
|
|
|
|
def test_get_invalid_storage_module(self):
|
|
|
|
"""
|
|
|
|
get_storage_class raises an error if the requested import don't exist.
|
|
|
|
"""
|
2011-08-13 02:42:08 +02:00
|
|
|
self.assertRaisesMessage(
|
2010-09-14 01:15:06 +02:00
|
|
|
ImproperlyConfigured,
|
|
|
|
"NonExistingStorage isn't a storage module.",
|
|
|
|
get_storage_class,
|
|
|
|
'NonExistingStorage')
|
|
|
|
|
|
|
|
def test_get_nonexisting_storage_class(self):
|
|
|
|
"""
|
|
|
|
get_storage_class raises an error if the requested class don't exist.
|
|
|
|
"""
|
2011-08-13 02:42:08 +02:00
|
|
|
self.assertRaisesMessage(
|
2010-09-14 01:15:06 +02:00
|
|
|
ImproperlyConfigured,
|
|
|
|
'Storage module "django.core.files.storage" does not define a '\
|
|
|
|
'"NonExistingStorage" class.',
|
|
|
|
get_storage_class,
|
|
|
|
'django.core.files.storage.NonExistingStorage')
|
|
|
|
|
|
|
|
def test_get_nonexisting_storage_module(self):
|
|
|
|
"""
|
|
|
|
get_storage_class raises an error if the requested module don't exist.
|
|
|
|
"""
|
2010-11-12 00:03:53 +01:00
|
|
|
# Error message may or may not be the fully qualified path.
|
|
|
|
self.assertRaisesRegexp(
|
2010-09-14 01:15:06 +02:00
|
|
|
ImproperlyConfigured,
|
2010-11-12 00:03:53 +01:00
|
|
|
('Error importing storage module django.core.files.non_existing_'
|
|
|
|
'storage: "No module named .*non_existing_storage"'),
|
2010-09-14 01:15:06 +02:00
|
|
|
get_storage_class,
|
2010-11-12 00:03:53 +01:00
|
|
|
'django.core.files.non_existing_storage.NonExistingStorage'
|
|
|
|
)
|
2010-09-14 01:15:06 +02:00
|
|
|
|
2009-05-08 12:56:51 +02:00
|
|
|
class FileStorageTests(unittest.TestCase):
|
|
|
|
storage_class = FileSystemStorage
|
2010-10-08 17:11:59 +02:00
|
|
|
|
2009-05-08 12:56:51 +02:00
|
|
|
def setUp(self):
|
2011-05-23 01:56:42 +02:00
|
|
|
self.temp_dir = tempfile.mkdtemp()
|
2010-09-14 01:15:06 +02:00
|
|
|
self.storage = self.storage_class(location=self.temp_dir,
|
|
|
|
base_url='/test_media_url/')
|
2011-05-23 01:56:42 +02:00
|
|
|
# Set up a second temporary directory which is ensured to have a mixed
|
|
|
|
# case name.
|
|
|
|
self.temp_dir2 = tempfile.mkdtemp(suffix='aBc')
|
2010-10-08 17:11:59 +02:00
|
|
|
|
2009-05-08 12:56:51 +02:00
|
|
|
def tearDown(self):
|
2010-09-14 01:15:06 +02:00
|
|
|
shutil.rmtree(self.temp_dir)
|
2011-05-23 01:56:42 +02:00
|
|
|
shutil.rmtree(self.temp_dir2)
|
2010-10-08 17:11:59 +02:00
|
|
|
|
2011-09-21 17:58:32 +02:00
|
|
|
def test_emtpy_location(self):
|
|
|
|
"""
|
|
|
|
Makes sure an exception is raised if the location is empty
|
|
|
|
"""
|
|
|
|
storage = self.storage_class(location='')
|
|
|
|
self.assertEqual(storage.base_location, '')
|
|
|
|
self.assertEqual(storage.location, os.getcwd())
|
|
|
|
|
2009-05-08 12:56:51 +02:00
|
|
|
def test_file_access_options(self):
|
|
|
|
"""
|
|
|
|
Standard file access options are available, and work as expected.
|
|
|
|
"""
|
2010-12-04 08:28:12 +01:00
|
|
|
self.assertFalse(self.storage.exists('storage_test'))
|
2009-05-08 12:56:51 +02:00
|
|
|
f = self.storage.open('storage_test', 'w')
|
|
|
|
f.write('storage contents')
|
|
|
|
f.close()
|
2011-03-03 16:04:39 +01:00
|
|
|
self.assertTrue(self.storage.exists('storage_test'))
|
2009-05-08 12:56:51 +02:00
|
|
|
|
|
|
|
f = self.storage.open('storage_test', 'r')
|
|
|
|
self.assertEqual(f.read(), 'storage contents')
|
|
|
|
f.close()
|
2010-10-08 17:11:59 +02:00
|
|
|
|
2009-05-08 12:56:51 +02:00
|
|
|
self.storage.delete('storage_test')
|
2010-12-04 08:28:12 +01:00
|
|
|
self.assertFalse(self.storage.exists('storage_test'))
|
2009-05-08 12:56:51 +02:00
|
|
|
|
2010-10-08 17:11:59 +02:00
|
|
|
def test_file_accessed_time(self):
|
|
|
|
"""
|
|
|
|
File storage returns a Datetime object for the last accessed time of
|
|
|
|
a file.
|
|
|
|
"""
|
2010-12-04 08:28:12 +01:00
|
|
|
self.assertFalse(self.storage.exists('test.file'))
|
2010-10-08 17:11:59 +02:00
|
|
|
|
2012-05-19 17:43:34 +02:00
|
|
|
f = ContentFile(b'custom contents')
|
2010-10-08 17:11:59 +02:00
|
|
|
f_name = self.storage.save('test.file', f)
|
|
|
|
atime = self.storage.accessed_time(f_name)
|
|
|
|
|
|
|
|
self.assertEqual(atime, datetime.fromtimestamp(
|
|
|
|
os.path.getatime(self.storage.path(f_name))))
|
|
|
|
self.assertTrue(datetime.now() - self.storage.accessed_time(f_name) < timedelta(seconds=2))
|
|
|
|
self.storage.delete(f_name)
|
|
|
|
|
|
|
|
def test_file_created_time(self):
|
|
|
|
"""
|
|
|
|
File storage returns a Datetime object for the creation time of
|
|
|
|
a file.
|
|
|
|
"""
|
2010-12-04 08:28:12 +01:00
|
|
|
self.assertFalse(self.storage.exists('test.file'))
|
2010-10-08 17:11:59 +02:00
|
|
|
|
2012-05-19 17:43:34 +02:00
|
|
|
f = ContentFile(b'custom contents')
|
2010-10-08 17:11:59 +02:00
|
|
|
f_name = self.storage.save('test.file', f)
|
|
|
|
ctime = self.storage.created_time(f_name)
|
|
|
|
|
|
|
|
self.assertEqual(ctime, datetime.fromtimestamp(
|
|
|
|
os.path.getctime(self.storage.path(f_name))))
|
|
|
|
self.assertTrue(datetime.now() - self.storage.created_time(f_name) < timedelta(seconds=2))
|
|
|
|
|
|
|
|
self.storage.delete(f_name)
|
|
|
|
|
|
|
|
def test_file_modified_time(self):
|
|
|
|
"""
|
|
|
|
File storage returns a Datetime object for the last modified time of
|
|
|
|
a file.
|
|
|
|
"""
|
2010-12-04 08:28:12 +01:00
|
|
|
self.assertFalse(self.storage.exists('test.file'))
|
2010-10-08 17:11:59 +02:00
|
|
|
|
2012-05-19 17:43:34 +02:00
|
|
|
f = ContentFile(b'custom contents')
|
2010-10-08 17:11:59 +02:00
|
|
|
f_name = self.storage.save('test.file', f)
|
|
|
|
mtime = self.storage.modified_time(f_name)
|
|
|
|
|
|
|
|
self.assertEqual(mtime, datetime.fromtimestamp(
|
|
|
|
os.path.getmtime(self.storage.path(f_name))))
|
|
|
|
self.assertTrue(datetime.now() - self.storage.modified_time(f_name) < timedelta(seconds=2))
|
|
|
|
|
|
|
|
self.storage.delete(f_name)
|
|
|
|
|
2010-09-14 01:15:06 +02:00
|
|
|
def test_file_save_without_name(self):
|
|
|
|
"""
|
|
|
|
File storage extracts the filename from the content object if no
|
|
|
|
name is given explicitly.
|
|
|
|
"""
|
2010-12-04 08:28:12 +01:00
|
|
|
self.assertFalse(self.storage.exists('test.file'))
|
2010-09-14 01:15:06 +02:00
|
|
|
|
2012-05-19 17:43:34 +02:00
|
|
|
f = ContentFile(b'custom contents')
|
2010-09-14 01:15:06 +02:00
|
|
|
f.name = 'test.file'
|
|
|
|
|
|
|
|
storage_f_name = self.storage.save(None, f)
|
|
|
|
|
|
|
|
self.assertEqual(storage_f_name, f.name)
|
|
|
|
|
2011-03-03 16:04:39 +01:00
|
|
|
self.assertTrue(os.path.exists(os.path.join(self.temp_dir, f.name)))
|
2010-09-14 01:15:06 +02:00
|
|
|
|
|
|
|
self.storage.delete(storage_f_name)
|
|
|
|
|
2011-05-26 10:21:35 +02:00
|
|
|
def test_file_save_with_path(self):
|
|
|
|
"""
|
|
|
|
Saving a pathname should create intermediate directories as necessary.
|
|
|
|
"""
|
|
|
|
self.assertFalse(self.storage.exists('path/to'))
|
|
|
|
self.storage.save('path/to/test.file',
|
2012-05-19 17:43:34 +02:00
|
|
|
ContentFile(b'file saved with path'))
|
2011-05-26 10:21:35 +02:00
|
|
|
|
|
|
|
self.assertTrue(self.storage.exists('path/to'))
|
|
|
|
self.assertEqual(self.storage.open('path/to/test.file').read(),
|
2012-05-19 17:43:34 +02:00
|
|
|
b'file saved with path')
|
2011-05-26 10:21:35 +02:00
|
|
|
|
|
|
|
self.assertTrue(os.path.exists(
|
|
|
|
os.path.join(self.temp_dir, 'path', 'to', 'test.file')))
|
|
|
|
|
|
|
|
self.storage.delete('path/to/test.file')
|
|
|
|
|
2010-09-14 01:15:06 +02:00
|
|
|
def test_file_path(self):
|
|
|
|
"""
|
|
|
|
File storage returns the full path of a file
|
|
|
|
"""
|
2010-12-04 08:28:12 +01:00
|
|
|
self.assertFalse(self.storage.exists('test.file'))
|
2010-09-14 01:15:06 +02:00
|
|
|
|
2012-05-19 17:43:34 +02:00
|
|
|
f = ContentFile(b'custom contents')
|
2010-09-14 01:15:06 +02:00
|
|
|
f_name = self.storage.save('test.file', f)
|
|
|
|
|
|
|
|
self.assertEqual(self.storage.path(f_name),
|
|
|
|
os.path.join(self.temp_dir, f_name))
|
|
|
|
|
|
|
|
self.storage.delete(f_name)
|
|
|
|
|
|
|
|
def test_file_url(self):
|
|
|
|
"""
|
2010-10-09 10:12:50 +02:00
|
|
|
File storage returns a url to access a given file from the Web.
|
2010-09-14 01:15:06 +02:00
|
|
|
"""
|
|
|
|
self.assertEqual(self.storage.url('test.file'),
|
|
|
|
'%s%s' % (self.storage.base_url, 'test.file'))
|
|
|
|
|
2011-02-04 15:43:10 +01:00
|
|
|
# should encode special chars except ~!*()'
|
|
|
|
# like encodeURIComponent() JavaScript function do
|
2012-01-08 20:23:57 +01:00
|
|
|
self.assertEqual(self.storage.url(r"""~!*()'@#$%^&*abc`+ =.file"""),
|
|
|
|
"""/test_media_url/~!*()'%40%23%24%25%5E%26*abc%60%2B%20%3D.file""")
|
2011-02-04 15:43:10 +01:00
|
|
|
|
|
|
|
# should stanslate os path separator(s) to the url path separator
|
|
|
|
self.assertEqual(self.storage.url("""a/b\\c.file"""),
|
|
|
|
"""/test_media_url/a/b/c.file""")
|
|
|
|
|
2010-09-14 01:15:06 +02:00
|
|
|
self.storage.base_url = None
|
|
|
|
self.assertRaises(ValueError, self.storage.url, 'test.file')
|
|
|
|
|
|
|
|
def test_listdir(self):
|
|
|
|
"""
|
|
|
|
File storage returns a tuple containing directories and files.
|
|
|
|
"""
|
2010-12-04 08:28:12 +01:00
|
|
|
self.assertFalse(self.storage.exists('storage_test_1'))
|
|
|
|
self.assertFalse(self.storage.exists('storage_test_2'))
|
|
|
|
self.assertFalse(self.storage.exists('storage_dir_1'))
|
2010-09-14 01:15:06 +02:00
|
|
|
|
2012-05-19 17:43:34 +02:00
|
|
|
f = self.storage.save('storage_test_1', ContentFile(b'custom content'))
|
|
|
|
f = self.storage.save('storage_test_2', ContentFile(b'custom content'))
|
2010-09-14 01:15:06 +02:00
|
|
|
os.mkdir(os.path.join(self.temp_dir, 'storage_dir_1'))
|
|
|
|
|
|
|
|
dirs, files = self.storage.listdir('')
|
2012-06-07 18:08:47 +02:00
|
|
|
self.assertEqual(set(dirs), set(['storage_dir_1']))
|
2010-09-14 01:15:06 +02:00
|
|
|
self.assertEqual(set(files),
|
2012-06-07 18:08:47 +02:00
|
|
|
set(['storage_test_1', 'storage_test_2']))
|
2010-09-14 01:15:06 +02:00
|
|
|
|
|
|
|
self.storage.delete('storage_test_1')
|
|
|
|
self.storage.delete('storage_test_2')
|
|
|
|
os.rmdir(os.path.join(self.temp_dir, 'storage_dir_1'))
|
|
|
|
|
2009-05-08 12:56:51 +02:00
|
|
|
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')
|
|
|
|
|
2011-05-23 01:56:42 +02:00
|
|
|
def test_file_storage_preserves_filename_case(self):
|
|
|
|
"""The storage backend should preserve case of filenames."""
|
|
|
|
# Create a storage backend associated with the mixed case name
|
|
|
|
# directory.
|
|
|
|
temp_storage = self.storage_class(location=self.temp_dir2)
|
|
|
|
# Ask that storage backend to store a file with a mixed case filename.
|
|
|
|
mixed_case = 'CaSe_SeNsItIvE'
|
|
|
|
file = temp_storage.open(mixed_case, 'w')
|
|
|
|
file.write('storage contents')
|
|
|
|
file.close()
|
|
|
|
self.assertEqual(os.path.join(self.temp_dir2, mixed_case),
|
|
|
|
temp_storage.path(mixed_case))
|
|
|
|
temp_storage.delete(mixed_case)
|
|
|
|
|
2011-05-26 10:21:35 +02:00
|
|
|
def test_makedirs_race_handling(self):
|
|
|
|
"""
|
|
|
|
File storage should be robust against directory creation race conditions.
|
|
|
|
"""
|
2011-05-28 15:06:08 +02:00
|
|
|
real_makedirs = os.makedirs
|
|
|
|
|
2011-05-26 10:21:35 +02:00
|
|
|
# Monkey-patch os.makedirs, to simulate a normal call, a raced call,
|
|
|
|
# and an error.
|
|
|
|
def fake_makedirs(path):
|
|
|
|
if path == os.path.join(self.temp_dir, 'normal'):
|
2011-05-28 15:06:08 +02:00
|
|
|
real_makedirs(path)
|
2011-05-26 10:21:35 +02:00
|
|
|
elif path == os.path.join(self.temp_dir, 'raced'):
|
2011-05-28 15:06:08 +02:00
|
|
|
real_makedirs(path)
|
2011-05-26 10:21:35 +02:00
|
|
|
raise OSError(errno.EEXIST, 'simulated EEXIST')
|
|
|
|
elif path == os.path.join(self.temp_dir, 'error'):
|
|
|
|
raise OSError(errno.EACCES, 'simulated EACCES')
|
|
|
|
else:
|
|
|
|
self.fail('unexpected argument %r' % path)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.makedirs = fake_makedirs
|
|
|
|
|
|
|
|
self.storage.save('normal/test.file',
|
2012-05-19 17:43:34 +02:00
|
|
|
ContentFile(b'saved normally'))
|
2011-05-26 10:21:35 +02:00
|
|
|
self.assertEqual(self.storage.open('normal/test.file').read(),
|
2012-05-19 17:43:34 +02:00
|
|
|
b'saved normally')
|
2011-05-26 10:21:35 +02:00
|
|
|
|
|
|
|
self.storage.save('raced/test.file',
|
2012-05-19 17:43:34 +02:00
|
|
|
ContentFile(b'saved with race'))
|
2011-05-26 10:21:35 +02:00
|
|
|
self.assertEqual(self.storage.open('raced/test.file').read(),
|
2012-05-19 17:43:34 +02:00
|
|
|
b'saved with race')
|
2011-05-26 10:21:35 +02:00
|
|
|
|
|
|
|
# Check that OSErrors aside from EEXIST are still raised.
|
|
|
|
self.assertRaises(OSError,
|
2012-05-19 17:43:34 +02:00
|
|
|
self.storage.save, 'error/test.file', ContentFile(b'not saved'))
|
2011-05-26 10:21:35 +02:00
|
|
|
finally:
|
|
|
|
os.makedirs = real_makedirs
|
|
|
|
|
2011-05-28 15:06:08 +02:00
|
|
|
def test_remove_race_handling(self):
|
|
|
|
"""
|
|
|
|
File storage should be robust against file removal race conditions.
|
|
|
|
"""
|
|
|
|
real_remove = os.remove
|
|
|
|
|
|
|
|
# Monkey-patch os.remove, to simulate a normal call, a raced call,
|
|
|
|
# and an error.
|
|
|
|
def fake_remove(path):
|
|
|
|
if path == os.path.join(self.temp_dir, 'normal.file'):
|
|
|
|
real_remove(path)
|
|
|
|
elif path == os.path.join(self.temp_dir, 'raced.file'):
|
|
|
|
real_remove(path)
|
|
|
|
raise OSError(errno.ENOENT, 'simulated ENOENT')
|
|
|
|
elif path == os.path.join(self.temp_dir, 'error.file'):
|
|
|
|
raise OSError(errno.EACCES, 'simulated EACCES')
|
|
|
|
else:
|
|
|
|
self.fail('unexpected argument %r' % path)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove = fake_remove
|
|
|
|
|
2012-05-19 17:43:34 +02:00
|
|
|
self.storage.save('normal.file', ContentFile(b'delete normally'))
|
2011-05-28 15:06:08 +02:00
|
|
|
self.storage.delete('normal.file')
|
|
|
|
self.assertFalse(self.storage.exists('normal.file'))
|
|
|
|
|
2012-05-19 17:43:34 +02:00
|
|
|
self.storage.save('raced.file', ContentFile(b'delete with race'))
|
2011-05-28 15:06:08 +02:00
|
|
|
self.storage.delete('raced.file')
|
|
|
|
self.assertFalse(self.storage.exists('normal.file'))
|
|
|
|
|
|
|
|
# Check that OSErrors aside from ENOENT are still raised.
|
2012-05-19 17:43:34 +02:00
|
|
|
self.storage.save('error.file', ContentFile(b'delete with error'))
|
2011-05-28 15:06:08 +02:00
|
|
|
self.assertRaises(OSError, self.storage.delete, 'error.file')
|
|
|
|
finally:
|
|
|
|
os.remove = real_remove
|
|
|
|
|
|
|
|
|
2009-05-08 12:56:51 +02:00
|
|
|
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
|
2010-10-08 17:11:59 +02:00
|
|
|
|
2009-05-08 12:56:51 +02:00
|
|
|
def test_custom_get_available_name(self):
|
2012-05-19 17:43:34 +02:00
|
|
|
first = self.storage.save('custom_storage', ContentFile(b'custom contents'))
|
2009-05-08 12:56:51 +02:00
|
|
|
self.assertEqual(first, 'custom_storage')
|
2012-05-19 17:43:34 +02:00
|
|
|
second = self.storage.save('custom_storage', ContentFile(b'more contents'))
|
2009-05-08 12:56:51 +02:00
|
|
|
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.
|
|
|
|
"""
|
2012-06-07 18:08:47 +02:00
|
|
|
uf = UploadedFile(name='¿Cómo?',content_type='text')
|
2009-05-08 12:56:51 +02:00
|
|
|
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-11 18:51:18 +02:00
|
|
|
class SlowFile(ContentFile):
|
|
|
|
def chunks(self):
|
|
|
|
time.sleep(1)
|
|
|
|
return super(ContentFile, self).chunks()
|
|
|
|
|
2010-10-11 14:55:17 +02:00
|
|
|
class FileSaveRaceConditionTest(unittest.TestCase):
|
2008-08-11 18:51:18 +02:00
|
|
|
def setUp(self):
|
2008-10-11 00:13:16 +02:00
|
|
|
self.storage_dir = tempfile.mkdtemp()
|
|
|
|
self.storage = FileSystemStorage(self.storage_dir)
|
2008-08-11 18:51:18 +02:00
|
|
|
self.thread = threading.Thread(target=self.save_file, args=['conflict'])
|
2009-05-08 07:50:31 +02:00
|
|
|
|
2008-10-11 00:13:16 +02:00
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.storage_dir)
|
2009-05-08 07:50:31 +02:00
|
|
|
|
2008-08-11 18:51:18 +02:00
|
|
|
def save_file(self, name):
|
2012-05-19 17:43:34 +02:00
|
|
|
name = self.storage.save(name, SlowFile(b"Data"))
|
2009-05-08 07:50:31 +02:00
|
|
|
|
2008-08-11 18:51:18 +02:00
|
|
|
def test_race_condition(self):
|
|
|
|
self.thread.start()
|
|
|
|
name = self.save_file('conflict')
|
|
|
|
self.thread.join()
|
2011-03-03 16:04:39 +01:00
|
|
|
self.assertTrue(self.storage.exists('conflict'))
|
|
|
|
self.assertTrue(self.storage.exists('conflict_1'))
|
2008-10-11 00:13:16 +02:00
|
|
|
self.storage.delete('conflict')
|
2010-02-23 23:39:22 +01:00
|
|
|
self.storage.delete('conflict_1')
|
2008-08-11 18:51:18 +02:00
|
|
|
|
2010-10-11 14:55:17 +02:00
|
|
|
class FileStoragePermissions(unittest.TestCase):
|
2008-08-28 00:21:14 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.old_perms = settings.FILE_UPLOAD_PERMISSIONS
|
2012-07-20 13:28:36 +02:00
|
|
|
settings.FILE_UPLOAD_PERMISSIONS = 0o666
|
2008-10-11 00:13:16 +02: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 00:21:14 +02:00
|
|
|
def test_file_upload_permissions(self):
|
2012-05-19 17:43:34 +02:00
|
|
|
name = self.storage.save("the_file", ContentFile(b"data"))
|
2012-07-20 13:28:36 +02:00
|
|
|
actual_mode = os.stat(self.storage.path(name))[0] & 0o777
|
|
|
|
self.assertEqual(actual_mode, 0o666)
|
2008-10-11 00:13:16 +02:00
|
|
|
|
2009-05-08 07:50:31 +02:00
|
|
|
|
2010-10-11 14:55:17 +02:00
|
|
|
class FileStoragePathParsing(unittest.TestCase):
|
2009-05-08 07:50:31 +02:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2012-05-19 17:43:34 +02:00
|
|
|
self.storage.save('dotted.path/test', ContentFile(b"1"))
|
|
|
|
self.storage.save('dotted.path/test', ContentFile(b"2"))
|
2009-05-08 07:50:31 +02:00
|
|
|
|
2010-12-04 08:28:12 +01:00
|
|
|
self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
|
2011-03-03 16:04:39 +01:00
|
|
|
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')))
|
2009-05-08 07:50:31 +02: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.
|
|
|
|
"""
|
2012-05-19 17:43:34 +02:00
|
|
|
self.storage.save('dotted.path/.test', ContentFile(b"1"))
|
|
|
|
self.storage.save('dotted.path/.test', ContentFile(b"2"))
|
2009-05-08 07:50:31 +02:00
|
|
|
|
2011-03-03 16:04:39 +01:00
|
|
|
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test')))
|
2012-03-30 11:20:04 +02:00
|
|
|
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test_1')))
|
2009-05-08 12:56:51 +02:00
|
|
|
|
2010-10-11 14:55:17 +02:00
|
|
|
class DimensionClosingBug(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Test that get_image_dimensions() properly closes files (#8817)
|
|
|
|
"""
|
|
|
|
@unittest.skipUnless(Image, "PIL not installed")
|
|
|
|
def test_not_closing_of_files(self):
|
|
|
|
"""
|
|
|
|
Open files passed into get_image_dimensions() should stay opened.
|
|
|
|
"""
|
2012-05-05 19:47:03 +02:00
|
|
|
empty_io = BytesIO()
|
2010-10-11 14:55:17 +02:00
|
|
|
try:
|
|
|
|
get_image_dimensions(empty_io)
|
|
|
|
finally:
|
2011-03-03 16:04:39 +01:00
|
|
|
self.assertTrue(not empty_io.closed)
|
2010-10-11 14:55:17 +02:00
|
|
|
|
|
|
|
@unittest.skipUnless(Image, "PIL not installed")
|
|
|
|
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
|
2011-03-03 16:04:39 +01:00
|
|
|
self.assertTrue(FileWrapper._closed)
|
2010-10-11 14:55:17 +02:00
|
|
|
|
|
|
|
class InconsistentGetImageDimensionsBug(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Test that get_image_dimensions() works properly after various calls
|
|
|
|
using a file handler (#11158)
|
|
|
|
"""
|
|
|
|
@unittest.skipUnless(Image, "PIL not installed")
|
|
|
|
def test_multiple_calls(self):
|
|
|
|
"""
|
|
|
|
Multiple calls of get_image_dimensions() should return the same size.
|
|
|
|
"""
|
|
|
|
from django.core.files.images import ImageFile
|
2010-11-21 18:51:41 +01:00
|
|
|
|
2010-10-11 14:55:17 +02:00
|
|
|
img_path = os.path.join(os.path.dirname(__file__), "test.png")
|
|
|
|
image = ImageFile(open(img_path, 'rb'))
|
|
|
|
image_pil = Image.open(img_path)
|
|
|
|
size_1, size_2 = get_image_dimensions(image), get_image_dimensions(image)
|
|
|
|
self.assertEqual(image_pil.size, size_1)
|
|
|
|
self.assertEqual(size_1, size_2)
|
2011-12-30 15:51:05 +01:00
|
|
|
|
|
|
|
class ContentFileTestCase(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Test that the constructor of ContentFile accepts 'name' (#16590).
|
|
|
|
"""
|
|
|
|
def test_content_file_default_name(self):
|
2012-05-19 17:43:34 +02:00
|
|
|
self.assertEqual(ContentFile(b"content").name, None)
|
2011-12-30 15:51:05 +01:00
|
|
|
|
2012-04-05 17:44:04 +02:00
|
|
|
def test_content_file_custom_name(self):
|
2011-12-30 15:51:05 +01:00
|
|
|
name = "I can have a name too!"
|
2012-05-19 17:43:34 +02:00
|
|
|
self.assertEqual(ContentFile(b"content", name=name).name, name)
|
2012-04-05 17:44:04 +02:00
|
|
|
|
|
|
|
class NoNameFileTestCase(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Other examples of unnamed files may be tempfile.SpooledTemporaryFile or
|
|
|
|
urllib.urlopen()
|
|
|
|
"""
|
|
|
|
def test_noname_file_default_name(self):
|
2012-05-05 19:47:03 +02:00
|
|
|
self.assertEqual(File(BytesIO(b'A file with no name')).name, None)
|
2012-04-05 17:44:04 +02:00
|
|
|
|
|
|
|
def test_noname_file_get_size(self):
|
2012-05-05 19:47:03 +02:00
|
|
|
self.assertEqual(File(BytesIO(b'A file with no name')).size, 19)
|
2012-04-05 17:44:04 +02:00
|
|
|
|
|
|
|
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())
|