2009-05-08 18:56:51 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2012-04-05 23:44:04 +08:00
|
|
|
|
2011-05-26 16:21:35 +08:00
|
|
|
import errno
|
2011-10-14 02:51:33 +08:00
|
|
|
import os
|
2008-10-11 06:13:16 +08:00
|
|
|
import shutil
|
2012-09-05 23:05:28 +08:00
|
|
|
import sys
|
2008-10-11 06:13:16 +08:00
|
|
|
import tempfile
|
2015-01-28 22:55:52 +08:00
|
|
|
import threading
|
2009-05-08 18:56:51 +08:00
|
|
|
import time
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2010-10-08 23:11:59 +08:00
|
|
|
from datetime import datetime, timedelta
|
2010-11-22 01:51:41 +08:00
|
|
|
|
2013-10-28 04:26:51 +08:00
|
|
|
from django.core.cache import cache
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.exceptions import SuspiciousFileOperation, SuspiciousOperation
|
|
|
|
from django.core.files.base import ContentFile, File
|
2010-09-14 07:15:06 +08:00
|
|
|
from django.core.files.storage import FileSystemStorage, get_storage_class
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.files.uploadedfile import (
|
|
|
|
InMemoryUploadedFile, SimpleUploadedFile, TemporaryUploadedFile,
|
|
|
|
)
|
2015-04-13 23:56:58 +08:00
|
|
|
from django.test import (
|
|
|
|
LiveServerTestCase, SimpleTestCase, TestCase, override_settings,
|
|
|
|
)
|
2012-08-29 15:45:02 +08:00
|
|
|
from django.utils import six
|
2012-12-08 18:13:52 +08:00
|
|
|
from django.utils._os import upath
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.six.moves.urllib.request import urlopen
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2013-10-28 04:26:51 +08:00
|
|
|
from .models import Storage, temp_storage, temp_storage_location
|
2009-05-08 18:56:51 +08:00
|
|
|
|
2014-08-08 22:20:08 +08:00
|
|
|
FILE_SUFFIX_REGEX = '[A-Za-z0-9]{7}'
|
|
|
|
|
|
|
|
|
2011-08-13 08:42:08 +08:00
|
|
|
class GetStorageClassTests(SimpleTestCase):
|
2010-09-14 07:15:06 +08: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.
|
|
|
|
"""
|
2014-01-21 04:15:14 +08:00
|
|
|
with six.assertRaisesRegex(self, ImportError, "No module named '?storage'?"):
|
2013-02-05 01:00:19 +08:00
|
|
|
get_storage_class('storage.NonExistingStorage')
|
2010-09-14 07:15:06 +08:00
|
|
|
|
|
|
|
def test_get_nonexisting_storage_class(self):
|
|
|
|
"""
|
|
|
|
get_storage_class raises an error if the requested class don't exist.
|
|
|
|
"""
|
2014-01-21 04:15:14 +08:00
|
|
|
self.assertRaises(ImportError, get_storage_class,
|
|
|
|
'django.core.files.storage.NonExistingStorage')
|
2010-09-14 07:15:06 +08:00
|
|
|
|
|
|
|
def test_get_nonexisting_storage_module(self):
|
|
|
|
"""
|
|
|
|
get_storage_class raises an error if the requested module don't exist.
|
|
|
|
"""
|
2010-11-12 07:03:53 +08:00
|
|
|
# Error message may or may not be the fully qualified path.
|
2014-01-21 04:15:14 +08:00
|
|
|
with six.assertRaisesRegex(self, ImportError,
|
|
|
|
"No module named '?(django.core.files.)?non_existing_storage'?"):
|
2013-02-05 01:00:19 +08:00
|
|
|
get_storage_class(
|
|
|
|
'django.core.files.non_existing_storage.NonExistingStorage')
|
2010-09-14 07:15:06 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2014-05-07 13:23:23 +08:00
|
|
|
class FileStorageDeconstructionTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_deconstruction(self):
|
|
|
|
path, args, kwargs = temp_storage.deconstruct()
|
|
|
|
self.assertEqual(path, "django.core.files.storage.FileSystemStorage")
|
|
|
|
self.assertEqual(args, tuple())
|
|
|
|
self.assertEqual(kwargs, {'location': temp_storage_location})
|
|
|
|
|
|
|
|
kwargs_orig = {
|
|
|
|
'location': temp_storage_location,
|
|
|
|
'base_url': 'http://myfiles.example.com/'
|
|
|
|
}
|
|
|
|
storage = FileSystemStorage(**kwargs_orig)
|
|
|
|
path, args, kwargs = storage.deconstruct()
|
|
|
|
self.assertEqual(kwargs, kwargs_orig)
|
|
|
|
|
|
|
|
|
2009-05-08 18:56:51 +08:00
|
|
|
class FileStorageTests(unittest.TestCase):
|
|
|
|
storage_class = FileSystemStorage
|
2010-10-08 23:11:59 +08:00
|
|
|
|
2009-05-08 18:56:51 +08:00
|
|
|
def setUp(self):
|
2011-05-23 07:56:42 +08:00
|
|
|
self.temp_dir = tempfile.mkdtemp()
|
2010-09-14 07:15:06 +08:00
|
|
|
self.storage = self.storage_class(location=self.temp_dir,
|
|
|
|
base_url='/test_media_url/')
|
2011-05-23 07:56:42 +08: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 23:11:59 +08:00
|
|
|
|
2009-05-08 18:56:51 +08:00
|
|
|
def tearDown(self):
|
2010-09-14 07:15:06 +08:00
|
|
|
shutil.rmtree(self.temp_dir)
|
2011-05-23 07:56:42 +08:00
|
|
|
shutil.rmtree(self.temp_dir2)
|
2010-10-08 23:11:59 +08:00
|
|
|
|
2014-05-29 01:03:26 +08:00
|
|
|
def test_empty_location(self):
|
2011-09-21 23:58:32 +08:00
|
|
|
"""
|
|
|
|
Makes sure an exception is raised if the location is empty
|
|
|
|
"""
|
|
|
|
storage = self.storage_class(location='')
|
|
|
|
self.assertEqual(storage.base_location, '')
|
2012-12-08 18:13:52 +08:00
|
|
|
self.assertEqual(storage.location, upath(os.getcwd()))
|
2011-09-21 23:58:32 +08:00
|
|
|
|
2009-05-08 18:56:51 +08:00
|
|
|
def test_file_access_options(self):
|
|
|
|
"""
|
|
|
|
Standard file access options are available, and work as expected.
|
|
|
|
"""
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertFalse(self.storage.exists('storage_test'))
|
2009-05-08 18:56:51 +08:00
|
|
|
f = self.storage.open('storage_test', 'w')
|
|
|
|
f.write('storage contents')
|
|
|
|
f.close()
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertTrue(self.storage.exists('storage_test'))
|
2009-05-08 18:56:51 +08:00
|
|
|
|
|
|
|
f = self.storage.open('storage_test', 'r')
|
|
|
|
self.assertEqual(f.read(), 'storage contents')
|
|
|
|
f.close()
|
2010-10-08 23:11:59 +08:00
|
|
|
|
2009-05-08 18:56:51 +08:00
|
|
|
self.storage.delete('storage_test')
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertFalse(self.storage.exists('storage_test'))
|
2009-05-08 18:56:51 +08:00
|
|
|
|
2010-10-08 23:11:59 +08:00
|
|
|
def test_file_accessed_time(self):
|
|
|
|
"""
|
|
|
|
File storage returns a Datetime object for the last accessed time of
|
|
|
|
a file.
|
|
|
|
"""
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertFalse(self.storage.exists('test.file'))
|
2010-10-08 23:11:59 +08:00
|
|
|
|
2012-08-29 16:00:29 +08:00
|
|
|
f = ContentFile('custom contents')
|
2010-10-08 23:11:59 +08: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))))
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertLess(datetime.now() - self.storage.accessed_time(f_name), timedelta(seconds=2))
|
2010-10-08 23:11:59 +08:00
|
|
|
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 15:28:12 +08:00
|
|
|
self.assertFalse(self.storage.exists('test.file'))
|
2010-10-08 23:11:59 +08:00
|
|
|
|
2012-08-29 16:00:29 +08:00
|
|
|
f = ContentFile('custom contents')
|
2010-10-08 23:11:59 +08: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))))
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertLess(datetime.now() - self.storage.created_time(f_name), timedelta(seconds=2))
|
2010-10-08 23:11:59 +08:00
|
|
|
|
|
|
|
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 15:28:12 +08:00
|
|
|
self.assertFalse(self.storage.exists('test.file'))
|
2010-10-08 23:11:59 +08:00
|
|
|
|
2012-08-29 16:00:29 +08:00
|
|
|
f = ContentFile('custom contents')
|
2010-10-08 23:11:59 +08: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))))
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertLess(datetime.now() - self.storage.modified_time(f_name), timedelta(seconds=2))
|
2010-10-08 23:11:59 +08:00
|
|
|
|
|
|
|
self.storage.delete(f_name)
|
|
|
|
|
2010-09-14 07:15:06 +08: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 15:28:12 +08:00
|
|
|
self.assertFalse(self.storage.exists('test.file'))
|
2010-09-14 07:15:06 +08:00
|
|
|
|
2012-08-29 16:00:29 +08:00
|
|
|
f = ContentFile('custom contents')
|
2010-09-14 07:15:06 +08:00
|
|
|
f.name = 'test.file'
|
|
|
|
|
|
|
|
storage_f_name = self.storage.save(None, f)
|
|
|
|
|
|
|
|
self.assertEqual(storage_f_name, f.name)
|
|
|
|
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertTrue(os.path.exists(os.path.join(self.temp_dir, f.name)))
|
2010-09-14 07:15:06 +08:00
|
|
|
|
|
|
|
self.storage.delete(storage_f_name)
|
|
|
|
|
2011-05-26 16:21:35 +08: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-08-29 16:00:29 +08:00
|
|
|
ContentFile('file saved with path'))
|
2011-05-26 16:21:35 +08:00
|
|
|
|
|
|
|
self.assertTrue(self.storage.exists('path/to'))
|
2012-08-15 17:04:40 +08:00
|
|
|
with self.storage.open('path/to/test.file') as f:
|
|
|
|
self.assertEqual(f.read(), b'file saved with path')
|
2011-05-26 16:21:35 +08:00
|
|
|
|
|
|
|
self.assertTrue(os.path.exists(
|
|
|
|
os.path.join(self.temp_dir, 'path', 'to', 'test.file')))
|
|
|
|
|
|
|
|
self.storage.delete('path/to/test.file')
|
|
|
|
|
2014-05-26 04:52:47 +08:00
|
|
|
def test_save_doesnt_close(self):
|
|
|
|
with TemporaryUploadedFile('test', 'text/plain', 1, 'utf8') as file:
|
|
|
|
file.write(b'1')
|
|
|
|
file.seek(0)
|
|
|
|
self.assertFalse(file.closed)
|
|
|
|
self.storage.save('path/to/test.file', file)
|
|
|
|
self.assertFalse(file.closed)
|
|
|
|
self.assertFalse(file.file.closed)
|
|
|
|
|
|
|
|
file = InMemoryUploadedFile(six.StringIO('1'), '', 'test',
|
|
|
|
'text/plain', 1, 'utf8')
|
|
|
|
with file:
|
|
|
|
self.assertFalse(file.closed)
|
|
|
|
self.storage.save('path/to/test.file', file)
|
|
|
|
self.assertFalse(file.closed)
|
|
|
|
self.assertFalse(file.file.closed)
|
|
|
|
|
2010-09-14 07:15:06 +08:00
|
|
|
def test_file_path(self):
|
|
|
|
"""
|
|
|
|
File storage returns the full path of a file
|
|
|
|
"""
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertFalse(self.storage.exists('test.file'))
|
2010-09-14 07:15:06 +08:00
|
|
|
|
2012-08-29 16:00:29 +08:00
|
|
|
f = ContentFile('custom contents')
|
2010-09-14 07:15:06 +08: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 16:12:50 +08:00
|
|
|
File storage returns a url to access a given file from the Web.
|
2010-09-14 07:15:06 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(self.storage.url('test.file'),
|
|
|
|
'%s%s' % (self.storage.base_url, 'test.file'))
|
|
|
|
|
2011-02-04 22:43:10 +08:00
|
|
|
# should encode special chars except ~!*()'
|
|
|
|
# like encodeURIComponent() JavaScript function do
|
2012-01-09 03:23:57 +08: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 22:43:10 +08:00
|
|
|
|
2015-05-21 01:05:41 +08:00
|
|
|
# should translate os path separator(s) to the url path separator
|
2011-02-04 22:43:10 +08:00
|
|
|
self.assertEqual(self.storage.url("""a/b\\c.file"""),
|
|
|
|
"""/test_media_url/a/b/c.file""")
|
|
|
|
|
2010-09-14 07:15:06 +08:00
|
|
|
self.storage.base_url = None
|
|
|
|
self.assertRaises(ValueError, self.storage.url, 'test.file')
|
|
|
|
|
2014-05-29 01:03:26 +08:00
|
|
|
# #22717: missing ending slash in base_url should be auto-corrected
|
|
|
|
storage = self.storage_class(location=self.temp_dir,
|
|
|
|
base_url='/no_ending_slash')
|
|
|
|
self.assertEqual(
|
|
|
|
storage.url('test.file'),
|
|
|
|
'%s%s' % (storage.base_url, 'test.file')
|
|
|
|
)
|
|
|
|
|
2010-09-14 07:15:06 +08:00
|
|
|
def test_listdir(self):
|
|
|
|
"""
|
|
|
|
File storage returns a tuple containing directories and files.
|
|
|
|
"""
|
2010-12-04 15:28:12 +08: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 07:15:06 +08:00
|
|
|
|
2013-10-19 20:31:38 +08:00
|
|
|
self.storage.save('storage_test_1', ContentFile('custom content'))
|
|
|
|
self.storage.save('storage_test_2', ContentFile('custom content'))
|
2010-09-14 07:15:06 +08:00
|
|
|
os.mkdir(os.path.join(self.temp_dir, 'storage_dir_1'))
|
|
|
|
|
|
|
|
dirs, files = self.storage.listdir('')
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual(set(dirs), {'storage_dir_1'})
|
2010-09-14 07:15:06 +08:00
|
|
|
self.assertEqual(set(files),
|
2014-09-26 20:31:50 +08:00
|
|
|
{'storage_test_1', 'storage_test_2'})
|
2010-09-14 07:15:06 +08: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 18:56:51 +08: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 07:56:42 +08: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.
|
2013-10-30 05:22:45 +08:00
|
|
|
other_temp_storage = self.storage_class(location=self.temp_dir2)
|
2011-05-23 07:56:42 +08:00
|
|
|
# Ask that storage backend to store a file with a mixed case filename.
|
|
|
|
mixed_case = 'CaSe_SeNsItIvE'
|
2013-10-30 05:22:45 +08:00
|
|
|
file = other_temp_storage.open(mixed_case, 'w')
|
2011-05-23 07:56:42 +08:00
|
|
|
file.write('storage contents')
|
|
|
|
file.close()
|
|
|
|
self.assertEqual(os.path.join(self.temp_dir2, mixed_case),
|
2013-10-30 05:22:45 +08:00
|
|
|
other_temp_storage.path(mixed_case))
|
|
|
|
other_temp_storage.delete(mixed_case)
|
2011-05-23 07:56:42 +08:00
|
|
|
|
2011-05-26 16:21:35 +08:00
|
|
|
def test_makedirs_race_handling(self):
|
|
|
|
"""
|
|
|
|
File storage should be robust against directory creation race conditions.
|
|
|
|
"""
|
2011-05-28 21:06:08 +08:00
|
|
|
real_makedirs = os.makedirs
|
|
|
|
|
2011-05-26 16:21:35 +08: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 21:06:08 +08:00
|
|
|
real_makedirs(path)
|
2011-05-26 16:21:35 +08:00
|
|
|
elif path == os.path.join(self.temp_dir, 'raced'):
|
2011-05-28 21:06:08 +08:00
|
|
|
real_makedirs(path)
|
2011-05-26 16:21:35 +08: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-08-29 16:00:29 +08:00
|
|
|
ContentFile('saved normally'))
|
2012-08-15 17:04:40 +08:00
|
|
|
with self.storage.open('normal/test.file') as f:
|
|
|
|
self.assertEqual(f.read(), b'saved normally')
|
2011-05-26 16:21:35 +08:00
|
|
|
|
|
|
|
self.storage.save('raced/test.file',
|
2012-08-29 16:00:29 +08:00
|
|
|
ContentFile('saved with race'))
|
2012-08-15 17:04:40 +08:00
|
|
|
with self.storage.open('raced/test.file') as f:
|
|
|
|
self.assertEqual(f.read(), b'saved with race')
|
2011-05-26 16:21:35 +08:00
|
|
|
|
|
|
|
# Check that OSErrors aside from EEXIST are still raised.
|
|
|
|
self.assertRaises(OSError,
|
2012-08-29 16:00:29 +08:00
|
|
|
self.storage.save, 'error/test.file', ContentFile('not saved'))
|
2011-05-26 16:21:35 +08:00
|
|
|
finally:
|
|
|
|
os.makedirs = real_makedirs
|
|
|
|
|
2011-05-28 21:06:08 +08: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-08-29 16:00:29 +08:00
|
|
|
self.storage.save('normal.file', ContentFile('delete normally'))
|
2011-05-28 21:06:08 +08:00
|
|
|
self.storage.delete('normal.file')
|
|
|
|
self.assertFalse(self.storage.exists('normal.file'))
|
|
|
|
|
2012-08-29 16:00:29 +08:00
|
|
|
self.storage.save('raced.file', ContentFile('delete with race'))
|
2011-05-28 21:06:08 +08:00
|
|
|
self.storage.delete('raced.file')
|
|
|
|
self.assertFalse(self.storage.exists('normal.file'))
|
|
|
|
|
|
|
|
# Check that OSErrors aside from ENOENT are still raised.
|
2012-08-29 16:00:29 +08:00
|
|
|
self.storage.save('error.file', ContentFile('delete with error'))
|
2011-05-28 21:06:08 +08:00
|
|
|
self.assertRaises(OSError, self.storage.delete, 'error.file')
|
|
|
|
finally:
|
|
|
|
os.remove = real_remove
|
|
|
|
|
2012-08-29 21:13:20 +08:00
|
|
|
def test_file_chunks_error(self):
|
|
|
|
"""
|
2014-03-02 22:25:53 +08:00
|
|
|
Test behavior when file.chunks() is raising an error
|
2012-08-29 21:13:20 +08:00
|
|
|
"""
|
|
|
|
f1 = ContentFile('chunks fails')
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2012-08-29 21:13:20 +08:00
|
|
|
def failing_chunks():
|
|
|
|
raise IOError
|
|
|
|
f1.chunks = failing_chunks
|
|
|
|
with self.assertRaises(IOError):
|
|
|
|
self.storage.save('error.file', f1)
|
|
|
|
|
2013-06-27 16:59:30 +08:00
|
|
|
def test_delete_no_name(self):
|
|
|
|
"""
|
|
|
|
Calling delete with an empty name should not try to remove the base
|
|
|
|
storage directory, but fail loudly (#20660).
|
|
|
|
"""
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
self.storage.delete('')
|
|
|
|
|
2011-05-28 21:06:08 +08:00
|
|
|
|
2009-05-08 18:56:51 +08:00
|
|
|
class CustomStorage(FileSystemStorage):
|
2014-10-15 15:42:06 +08:00
|
|
|
def get_available_name(self, name, max_length=None):
|
2009-05-08 18:56:51 +08:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2009-05-08 18:56:51 +08:00
|
|
|
class CustomStorageTests(FileStorageTests):
|
|
|
|
storage_class = CustomStorage
|
2010-10-08 23:11:59 +08:00
|
|
|
|
2009-05-08 18:56:51 +08:00
|
|
|
def test_custom_get_available_name(self):
|
2012-08-29 16:00:29 +08:00
|
|
|
first = self.storage.save('custom_storage', ContentFile('custom contents'))
|
2009-05-08 18:56:51 +08:00
|
|
|
self.assertEqual(first, 'custom_storage')
|
2012-08-29 16:00:29 +08:00
|
|
|
second = self.storage.save('custom_storage', ContentFile('more contents'))
|
2009-05-08 18:56:51 +08:00
|
|
|
self.assertEqual(second, 'custom_storage.2')
|
|
|
|
self.storage.delete(first)
|
|
|
|
self.storage.delete(second)
|
|
|
|
|
2013-10-28 04:26:51 +08:00
|
|
|
|
2015-04-13 23:56:58 +08:00
|
|
|
class FileFieldStorageTests(TestCase):
|
2013-10-28 04:26:51 +08:00
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(temp_storage_location)
|
|
|
|
|
2015-05-23 01:43:08 +08:00
|
|
|
def _storage_max_filename_length(self, storage):
|
|
|
|
"""
|
|
|
|
Query filesystem for maximum filename length (e.g. AUFS has 242).
|
|
|
|
"""
|
|
|
|
dir_to_test = storage.location
|
|
|
|
while not os.path.exists(dir_to_test):
|
|
|
|
dir_to_test = os.path.dirname(dir_to_test)
|
|
|
|
try:
|
|
|
|
return os.pathconf(dir_to_test, 'PC_NAME_MAX')
|
|
|
|
except Exception:
|
|
|
|
return 255 # Should be safe on most backends
|
|
|
|
|
2013-10-28 04:26:51 +08:00
|
|
|
def test_files(self):
|
|
|
|
# Attempting to access a FileField from the class raises a descriptive
|
|
|
|
# error
|
|
|
|
self.assertRaises(AttributeError, lambda: Storage.normal)
|
|
|
|
|
|
|
|
# An object without a file has limited functionality.
|
|
|
|
obj1 = Storage()
|
|
|
|
self.assertEqual(obj1.normal.name, "")
|
|
|
|
self.assertRaises(ValueError, lambda: obj1.normal.size)
|
|
|
|
|
|
|
|
# Saving a file enables full functionality.
|
|
|
|
obj1.normal.save("django_test.txt", ContentFile("content"))
|
|
|
|
self.assertEqual(obj1.normal.name, "tests/django_test.txt")
|
|
|
|
self.assertEqual(obj1.normal.size, 7)
|
|
|
|
self.assertEqual(obj1.normal.read(), b"content")
|
|
|
|
obj1.normal.close()
|
|
|
|
|
|
|
|
# File objects can be assigned to FileField attributes, but shouldn't
|
|
|
|
# get committed until the model it's attached to is saved.
|
|
|
|
obj1.normal = SimpleUploadedFile("assignment.txt", b"content")
|
|
|
|
dirs, files = temp_storage.listdir("tests")
|
|
|
|
self.assertEqual(dirs, [])
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertNotIn("assignment.txt", files)
|
2013-10-28 04:26:51 +08:00
|
|
|
|
|
|
|
obj1.save()
|
|
|
|
dirs, files = temp_storage.listdir("tests")
|
|
|
|
self.assertEqual(sorted(files), ["assignment.txt", "django_test.txt"])
|
|
|
|
|
|
|
|
# Save another file with the same name.
|
|
|
|
obj2 = Storage()
|
|
|
|
obj2.normal.save("django_test.txt", ContentFile("more content"))
|
2014-08-08 22:20:08 +08:00
|
|
|
obj2_name = obj2.normal.name
|
|
|
|
six.assertRegex(self, obj2_name, "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX)
|
2013-10-28 04:26:51 +08:00
|
|
|
self.assertEqual(obj2.normal.size, 12)
|
|
|
|
obj2.normal.close()
|
|
|
|
|
|
|
|
# Deleting an object does not delete the file it uses.
|
|
|
|
obj2.delete()
|
|
|
|
obj2.normal.save("django_test.txt", ContentFile("more content"))
|
2014-08-08 22:20:08 +08:00
|
|
|
self.assertNotEqual(obj2_name, obj2.normal.name)
|
|
|
|
six.assertRegex(self, obj2.normal.name, "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX)
|
2013-10-28 04:26:51 +08:00
|
|
|
obj2.normal.close()
|
|
|
|
|
|
|
|
def test_filefield_read(self):
|
|
|
|
# Files can be read in a little at a time, if necessary.
|
|
|
|
obj = Storage.objects.create(
|
|
|
|
normal=SimpleUploadedFile("assignment.txt", b"content"))
|
|
|
|
obj.normal.open()
|
|
|
|
self.assertEqual(obj.normal.read(3), b"con")
|
|
|
|
self.assertEqual(obj.normal.read(), b"tent")
|
|
|
|
self.assertEqual(list(obj.normal.chunks(chunk_size=2)), [b"co", b"nt", b"en", b"t"])
|
|
|
|
obj.normal.close()
|
|
|
|
|
2014-08-08 22:20:08 +08:00
|
|
|
def test_duplicate_filename(self):
|
|
|
|
# Multiple files with the same name get _(7 random chars) appended to them.
|
|
|
|
objs = [Storage() for i in range(2)]
|
2013-10-28 04:26:51 +08:00
|
|
|
for o in objs:
|
|
|
|
o.normal.save("multiple_files.txt", ContentFile("Same Content"))
|
2014-08-08 22:20:08 +08:00
|
|
|
try:
|
|
|
|
names = [o.normal.name for o in objs]
|
|
|
|
self.assertEqual(names[0], "tests/multiple_files.txt")
|
|
|
|
six.assertRegex(self, names[1], "tests/multiple_files_%s.txt" % FILE_SUFFIX_REGEX)
|
|
|
|
finally:
|
|
|
|
for o in objs:
|
|
|
|
o.delete()
|
2013-10-28 04:26:51 +08:00
|
|
|
|
2014-10-15 15:42:06 +08:00
|
|
|
def test_file_truncation(self):
|
|
|
|
# Given the max_length is limited, when multiple files get uploaded
|
|
|
|
# under the same name, then the filename get truncated in order to fit
|
|
|
|
# in _(7 random chars). When most of the max_length is taken by
|
|
|
|
# dirname + extension and there are not enough characters in the
|
|
|
|
# filename to truncate, an exception should be raised.
|
|
|
|
objs = [Storage() for i in range(2)]
|
|
|
|
filename = 'filename.ext'
|
|
|
|
|
|
|
|
for o in objs:
|
|
|
|
o.limited_length.save(filename, ContentFile('Same Content'))
|
|
|
|
try:
|
|
|
|
# Testing truncation.
|
|
|
|
names = [o.limited_length.name for o in objs]
|
|
|
|
self.assertEqual(names[0], 'tests/%s' % filename)
|
|
|
|
six.assertRegex(self, names[1], 'tests/fi_%s.ext' % FILE_SUFFIX_REGEX)
|
|
|
|
|
|
|
|
# Testing exception is raised when filename is too short to truncate.
|
|
|
|
filename = 'short.longext'
|
|
|
|
objs[0].limited_length.save(filename, ContentFile('Same Content'))
|
|
|
|
self.assertRaisesMessage(
|
|
|
|
SuspiciousFileOperation, 'Storage can not find an available filename',
|
|
|
|
objs[1].limited_length.save, *(filename, ContentFile('Same Content'))
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
for o in objs:
|
|
|
|
o.delete()
|
|
|
|
|
2015-01-15 04:00:27 +08:00
|
|
|
@unittest.skipIf(
|
|
|
|
sys.platform.startswith('win'),
|
|
|
|
"Windows supports at most 260 characters in a path.",
|
|
|
|
)
|
2014-10-15 15:42:06 +08:00
|
|
|
def test_extended_length_storage(self):
|
|
|
|
# Testing FileField with max_length > 255. Most systems have filename
|
|
|
|
# length limitation of 255. Path takes extra chars.
|
2015-05-23 01:43:08 +08:00
|
|
|
filename = (self._storage_max_filename_length(temp_storage) - 4) * 'a' # 4 chars for extension.
|
2014-10-15 15:42:06 +08:00
|
|
|
obj = Storage()
|
|
|
|
obj.extended_length.save('%s.txt' % filename, ContentFile('Same Content'))
|
|
|
|
self.assertEqual(obj.extended_length.name, 'tests/%s.txt' % filename)
|
|
|
|
self.assertEqual(obj.extended_length.read(), b'Same Content')
|
|
|
|
obj.extended_length.close()
|
|
|
|
|
2013-10-28 04:26:51 +08:00
|
|
|
def test_filefield_default(self):
|
|
|
|
# Default values allow an object to access a single file.
|
|
|
|
temp_storage.save('tests/default.txt', ContentFile('default content'))
|
|
|
|
obj = Storage.objects.create()
|
|
|
|
self.assertEqual(obj.default.name, "tests/default.txt")
|
|
|
|
self.assertEqual(obj.default.read(), b"default content")
|
|
|
|
obj.default.close()
|
|
|
|
|
|
|
|
# But it shouldn't be deleted, even if there are no more objects using
|
|
|
|
# it.
|
|
|
|
obj.delete()
|
|
|
|
obj = Storage()
|
|
|
|
self.assertEqual(obj.default.read(), b"default content")
|
|
|
|
obj.default.close()
|
|
|
|
|
|
|
|
def test_empty_upload_to(self):
|
|
|
|
# upload_to can be empty, meaning it does not use subdirectory.
|
|
|
|
obj = Storage()
|
|
|
|
obj.empty.save('django_test.txt', ContentFile('more content'))
|
|
|
|
self.assertEqual(obj.empty.name, "./django_test.txt")
|
|
|
|
self.assertEqual(obj.empty.read(), b"more content")
|
|
|
|
obj.empty.close()
|
|
|
|
|
|
|
|
def test_random_upload_to(self):
|
|
|
|
# Verify the fix for #5655, making sure the directory is only
|
|
|
|
# determined once.
|
|
|
|
obj = Storage()
|
|
|
|
obj.random.save("random_file", ContentFile("random content"))
|
|
|
|
self.assertTrue(obj.random.name.endswith("/random_file"))
|
|
|
|
obj.random.close()
|
|
|
|
|
2015-05-03 12:10:24 +08:00
|
|
|
def test_custom_valid_name_callable_upload_to(self):
|
|
|
|
"""
|
|
|
|
Storage.get_valid_name() should be called when upload_to is a callable.
|
|
|
|
"""
|
|
|
|
obj = Storage()
|
|
|
|
obj.custom_valid_name.save("random_file", ContentFile("random content"))
|
|
|
|
# CustomValidNameStorage.get_valid_name() appends '_valid' to the name
|
|
|
|
self.assertTrue(obj.custom_valid_name.name.endswith("/random_file_valid"))
|
|
|
|
obj.custom_valid_name.close()
|
|
|
|
|
2013-10-28 04:26:51 +08:00
|
|
|
def test_filefield_pickling(self):
|
|
|
|
# Push an object into the cache to make sure it pickles properly
|
|
|
|
obj = Storage()
|
|
|
|
obj.normal.save("django_test.txt", ContentFile("more content"))
|
|
|
|
obj.normal.close()
|
|
|
|
cache.set("obj", obj)
|
|
|
|
self.assertEqual(cache.get("obj").normal.name, "tests/django_test.txt")
|
|
|
|
|
|
|
|
def test_file_object(self):
|
|
|
|
# Create sample file
|
|
|
|
temp_storage.save('tests/example.txt', ContentFile('some content'))
|
|
|
|
|
|
|
|
# Load it as python file object
|
|
|
|
with open(temp_storage.path('tests/example.txt')) as file_obj:
|
|
|
|
# Save it using storage and read its content
|
|
|
|
temp_storage.save('tests/file_obj', file_obj)
|
|
|
|
self.assertTrue(temp_storage.exists('tests/file_obj'))
|
|
|
|
with temp_storage.open('tests/file_obj') as f:
|
|
|
|
self.assertEqual(f.read(), b'some content')
|
|
|
|
|
|
|
|
def test_stringio(self):
|
|
|
|
# Test passing StringIO instance as content argument to save
|
|
|
|
output = six.StringIO()
|
|
|
|
output.write('content')
|
|
|
|
output.seek(0)
|
|
|
|
|
|
|
|
# Save it and read written file
|
|
|
|
temp_storage.save('tests/stringio', output)
|
|
|
|
self.assertTrue(temp_storage.exists('tests/stringio'))
|
|
|
|
with temp_storage.open('tests/stringio') as f:
|
|
|
|
self.assertEqual(f.read(), b'content')
|
|
|
|
|
2009-05-08 18:56:51 +08:00
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
class FileSaveRaceConditionTest(unittest.TestCase):
|
2008-08-12 00:51:18 +08:00
|
|
|
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):
|
2012-05-19 23:43:34 +08:00
|
|
|
name = self.storage.save(name, SlowFile(b"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()
|
2013-10-19 20:31:38 +08:00
|
|
|
self.save_file('conflict')
|
2008-08-12 00:51:18 +08:00
|
|
|
self.thread.join()
|
2014-08-08 22:20:08 +08:00
|
|
|
files = sorted(os.listdir(self.storage_dir))
|
|
|
|
self.assertEqual(files[0], 'conflict')
|
|
|
|
six.assertRegex(self, files[1], 'conflict_%s' % FILE_SUFFIX_REGEX)
|
2008-08-12 00:51:18 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-09-05 23:05:28 +08:00
|
|
|
@unittest.skipIf(sys.platform.startswith('win'), "Windows only partially supports umasks and chmod.")
|
2010-10-11 20:55:17 +08:00
|
|
|
class FileStoragePermissions(unittest.TestCase):
|
2008-08-28 06:21:14 +08:00
|
|
|
def setUp(self):
|
2012-09-05 23:05:28 +08:00
|
|
|
self.umask = 0o027
|
|
|
|
self.old_umask = os.umask(self.umask)
|
2008-10-11 06:13:16 +08:00
|
|
|
self.storage_dir = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.storage_dir)
|
2012-09-05 23:05:28 +08:00
|
|
|
os.umask(self.old_umask)
|
2008-10-11 06:13:16 +08:00
|
|
|
|
2012-09-05 23:05:28 +08:00
|
|
|
@override_settings(FILE_UPLOAD_PERMISSIONS=0o654)
|
2008-08-28 06:21:14 +08:00
|
|
|
def test_file_upload_permissions(self):
|
2013-10-19 20:40:12 +08:00
|
|
|
self.storage = FileSystemStorage(self.storage_dir)
|
2012-08-29 16:00:29 +08:00
|
|
|
name = self.storage.save("the_file", ContentFile("data"))
|
2012-07-20 19:28:36 +08:00
|
|
|
actual_mode = os.stat(self.storage.path(name))[0] & 0o777
|
2012-09-05 23:05:28 +08:00
|
|
|
self.assertEqual(actual_mode, 0o654)
|
2008-10-11 06:13:16 +08:00
|
|
|
|
2012-09-05 23:05:28 +08:00
|
|
|
@override_settings(FILE_UPLOAD_PERMISSIONS=None)
|
|
|
|
def test_file_upload_default_permissions(self):
|
2013-10-19 20:40:12 +08:00
|
|
|
self.storage = FileSystemStorage(self.storage_dir)
|
2012-09-05 23:05:28 +08:00
|
|
|
fname = self.storage.save("some_file", ContentFile("data"))
|
|
|
|
mode = os.stat(self.storage.path(fname))[0] & 0o777
|
|
|
|
self.assertEqual(mode, 0o666 & ~self.umask)
|
2009-05-08 13:50:31 +08:00
|
|
|
|
2013-05-13 19:38:53 +08:00
|
|
|
@override_settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o765)
|
|
|
|
def test_file_upload_directory_permissions(self):
|
2013-10-19 20:40:12 +08:00
|
|
|
self.storage = FileSystemStorage(self.storage_dir)
|
2013-05-13 19:38:53 +08:00
|
|
|
name = self.storage.save("the_directory/the_file", ContentFile("data"))
|
|
|
|
dir_mode = os.stat(os.path.dirname(self.storage.path(name)))[0] & 0o777
|
|
|
|
self.assertEqual(dir_mode, 0o765)
|
|
|
|
|
|
|
|
@override_settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=None)
|
|
|
|
def test_file_upload_directory_default_permissions(self):
|
2013-10-19 20:40:12 +08:00
|
|
|
self.storage = FileSystemStorage(self.storage_dir)
|
2013-05-13 19:38:53 +08:00
|
|
|
name = self.storage.save("the_directory/the_file", ContentFile("data"))
|
|
|
|
dir_mode = os.stat(os.path.dirname(self.storage.path(name)))[0] & 0o777
|
|
|
|
self.assertEqual(dir_mode, 0o777 & ~self.umask)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
class FileStoragePathParsing(unittest.TestCase):
|
2009-05-08 13:50:31 +08: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-08-29 16:00:29 +08:00
|
|
|
self.storage.save('dotted.path/test', ContentFile("1"))
|
|
|
|
self.storage.save('dotted.path/test', ContentFile("2"))
|
2009-05-08 13:50:31 +08:00
|
|
|
|
2014-08-08 22:20:08 +08:00
|
|
|
files = sorted(os.listdir(os.path.join(self.storage_dir, 'dotted.path')))
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
|
2014-08-08 22:20:08 +08:00
|
|
|
self.assertEqual(files[0], 'test')
|
|
|
|
six.assertRegex(self, files[1], 'test_%s' % FILE_SUFFIX_REGEX)
|
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.
|
|
|
|
"""
|
2012-08-29 16:00:29 +08:00
|
|
|
self.storage.save('dotted.path/.test', ContentFile("1"))
|
|
|
|
self.storage.save('dotted.path/.test', ContentFile("2"))
|
2009-05-08 13:50:31 +08:00
|
|
|
|
2014-08-08 22:20:08 +08:00
|
|
|
files = sorted(os.listdir(os.path.join(self.storage_dir, 'dotted.path')))
|
|
|
|
self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
|
|
|
|
self.assertEqual(files[0], '.test')
|
|
|
|
six.assertRegex(self, files[1], '.test_%s' % FILE_SUFFIX_REGEX)
|
2009-05-08 18:56:51 +08:00
|
|
|
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-10-28 04:26:51 +08:00
|
|
|
class ContentFileStorageTestCase(unittest.TestCase):
|
2012-08-29 15:45:02 +08:00
|
|
|
|
2012-12-07 00:14:44 +08: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_content_saving(self):
|
|
|
|
"""
|
|
|
|
Test that ContentFile can be saved correctly with the filesystem storage,
|
|
|
|
both if it was initialized with string or unicode content"""
|
|
|
|
self.storage.save('bytes.txt', ContentFile(b"content"))
|
|
|
|
self.storage.save('unicode.txt', ContentFile("español"))
|
|
|
|
|
2012-08-29 15:45:02 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='file_storage.urls')
|
2013-06-04 13:55:20 +08:00
|
|
|
class FileLikeObjectTestCase(LiveServerTestCase):
|
2012-04-05 23:44:04 +08:00
|
|
|
"""
|
|
|
|
Test file-like objects (#15644).
|
|
|
|
"""
|
2013-06-04 14:09:29 +08:00
|
|
|
|
|
|
|
available_apps = []
|
2013-06-04 13:55:20 +08:00
|
|
|
|
2012-04-05 23:44:04 +08:00
|
|
|
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()
|
|
|
|
"""
|
2013-06-04 13:55:20 +08:00
|
|
|
file_like_object = urlopen(self.live_server_url + '/')
|
2012-04-05 23:44:04 +08:00
|
|
|
f = File(file_like_object)
|
|
|
|
stored_filename = self.storage.save("remote_file.html", f)
|
|
|
|
|
2013-06-04 13:55:20 +08:00
|
|
|
remote_file = urlopen(self.live_server_url + '/')
|
2013-02-24 20:10:42 +08:00
|
|
|
with self.storage.open(stored_filename) as stored_file:
|
|
|
|
self.assertEqual(stored_file.read(), remote_file.read())
|