2011-10-13 18:51:33 +00:00
|
|
|
import os
|
2008-10-10 22:13:16 +00:00
|
|
|
import shutil
|
2012-09-05 15:05:28 +00:00
|
|
|
import sys
|
2008-10-10 22:13:16 +00:00
|
|
|
import tempfile
|
2015-01-28 14:55:52 +00:00
|
|
|
import threading
|
2009-05-08 10:56:51 +00:00
|
|
|
import time
|
2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2010-10-08 15:11:59 +00:00
|
|
|
from datetime import datetime, timedelta
|
2022-03-23 11:15:36 +00:00
|
|
|
from datetime import timezone as datetime_timezone
|
2017-01-07 11:11:46 +00:00
|
|
|
from io import StringIO
|
2019-10-31 07:51:12 +00:00
|
|
|
from pathlib import Path
|
2017-01-07 11:11:46 +00:00
|
|
|
from urllib.request import urlopen
|
2010-11-21 17:51:41 +00:00
|
|
|
|
2013-10-27 20:26:51 +00:00
|
|
|
from django.core.cache import cache
|
2017-05-25 12:19:01 +00:00
|
|
|
from django.core.exceptions import SuspiciousFileOperation
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.core.files.base import ContentFile, File
|
2020-03-31 10:12:39 +00:00
|
|
|
from django.core.files.storage import FileSystemStorage
|
2020-04-08 08:35:15 +00:00
|
|
|
from django.core.files.storage import Storage as BaseStorage
|
|
|
|
from django.core.files.storage import default_storage, get_storage_class
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.core.files.uploadedfile import (
|
|
|
|
InMemoryUploadedFile,
|
|
|
|
SimpleUploadedFile,
|
|
|
|
TemporaryUploadedFile,
|
|
|
|
)
|
2020-03-31 10:12:39 +00:00
|
|
|
from django.db.models import FileField
|
2015-10-01 17:00:44 +00:00
|
|
|
from django.db.models.fields.files import FileDescriptor
|
2016-12-31 16:11:04 +00:00
|
|
|
from django.test import LiveServerTestCase, SimpleTestCase, TestCase, override_settings
|
2016-02-09 15:00:14 +00:00
|
|
|
from django.test.utils import requires_tz_support
|
2016-07-28 11:37:07 +00:00
|
|
|
from django.urls import NoReverseMatch, reverse_lazy
|
2017-01-07 11:11:46 +00:00
|
|
|
from django.utils import timezone
|
2021-06-02 05:03:52 +00:00
|
|
|
from django.utils._os import symlinks_supported
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2020-08-24 19:27:22 +00:00
|
|
|
from .models import Storage, callable_storage, temp_storage, temp_storage_location
|
2009-05-08 10:56:51 +00:00
|
|
|
|
2014-08-08 14:20:08 +00:00
|
|
|
FILE_SUFFIX_REGEX = "[A-Za-z0-9]{7}"
|
|
|
|
|
|
|
|
|
2011-08-13 00:42:08 +00:00
|
|
|
class GetStorageClassTests(SimpleTestCase):
|
2010-09-13 23:15:06 +00: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.
|
|
|
|
"""
|
2017-01-20 02:10:33 +00:00
|
|
|
with self.assertRaisesMessage(ImportError, "No module named 'storage'"):
|
2017-02-03 01:43:21 +00:00
|
|
|
get_storage_class("storage.NonexistentStorage")
|
2010-09-13 23:15:06 +00:00
|
|
|
|
2017-02-03 01:43:21 +00:00
|
|
|
def test_get_nonexistent_storage_class(self):
|
2010-09-13 23:15:06 +00:00
|
|
|
"""
|
|
|
|
get_storage_class raises an error if the requested class don't exist.
|
|
|
|
"""
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ImportError):
|
2017-02-03 01:43:21 +00:00
|
|
|
get_storage_class("django.core.files.storage.NonexistentStorage")
|
2010-09-13 23:15:06 +00:00
|
|
|
|
2017-02-03 01:43:21 +00:00
|
|
|
def test_get_nonexistent_storage_module(self):
|
2010-09-13 23:15:06 +00:00
|
|
|
"""
|
|
|
|
get_storage_class raises an error if the requested module don't exist.
|
|
|
|
"""
|
2017-02-03 01:43:21 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ImportError, "No module named 'django.core.files.nonexistent_storage'"
|
|
|
|
):
|
|
|
|
get_storage_class(
|
|
|
|
"django.core.files.nonexistent_storage.NonexistentStorage"
|
|
|
|
)
|
2010-09-13 23:15:06 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2016-07-28 11:37:07 +00:00
|
|
|
class FileSystemStorageTests(unittest.TestCase):
|
2014-05-07 05:23:23 +00:00
|
|
|
def test_deconstruction(self):
|
|
|
|
path, args, kwargs = temp_storage.deconstruct()
|
|
|
|
self.assertEqual(path, "django.core.files.storage.FileSystemStorage")
|
2017-06-01 23:08:59 +00:00
|
|
|
self.assertEqual(args, ())
|
2014-05-07 05:23:23 +00:00
|
|
|
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)
|
|
|
|
|
2016-07-28 11:37:07 +00:00
|
|
|
def test_lazy_base_url_init(self):
|
|
|
|
"""
|
|
|
|
FileSystemStorage.__init__() shouldn't evaluate base_url.
|
|
|
|
"""
|
|
|
|
storage = FileSystemStorage(base_url=reverse_lazy("app:url"))
|
|
|
|
with self.assertRaises(NoReverseMatch):
|
|
|
|
storage.url(storage.base_url)
|
|
|
|
|
2014-05-07 05:23:23 +00:00
|
|
|
|
2016-06-06 03:47:17 +00:00
|
|
|
class FileStorageTests(SimpleTestCase):
|
2009-05-08 10:56:51 +00:00
|
|
|
storage_class = FileSystemStorage
|
2010-10-08 15:11:59 +00:00
|
|
|
|
2009-05-08 10:56:51 +00:00
|
|
|
def setUp(self):
|
2011-05-22 23:56:42 +00:00
|
|
|
self.temp_dir = tempfile.mkdtemp()
|
2016-04-08 02:04:45 +00:00
|
|
|
self.storage = self.storage_class(
|
|
|
|
location=self.temp_dir, base_url="/test_media_url/"
|
|
|
|
)
|
2011-05-22 23:56:42 +00: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 15:11:59 +00:00
|
|
|
|
2009-05-08 10:56:51 +00:00
|
|
|
def tearDown(self):
|
2010-09-13 23:15:06 +00:00
|
|
|
shutil.rmtree(self.temp_dir)
|
2011-05-22 23:56:42 +00:00
|
|
|
shutil.rmtree(self.temp_dir2)
|
2010-10-08 15:11:59 +00:00
|
|
|
|
2014-05-28 17:03:26 +00:00
|
|
|
def test_empty_location(self):
|
2011-09-21 15:58:32 +00:00
|
|
|
"""
|
|
|
|
Makes sure an exception is raised if the location is empty
|
|
|
|
"""
|
|
|
|
storage = self.storage_class(location="")
|
|
|
|
self.assertEqual(storage.base_location, "")
|
2017-01-20 13:01:02 +00:00
|
|
|
self.assertEqual(storage.location, os.getcwd())
|
2011-09-21 15:58:32 +00:00
|
|
|
|
2009-05-08 10:56:51 +00:00
|
|
|
def test_file_access_options(self):
|
|
|
|
"""
|
|
|
|
Standard file access options are available, and work as expected.
|
|
|
|
"""
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertFalse(self.storage.exists("storage_test"))
|
2009-05-08 10:56:51 +00:00
|
|
|
f = self.storage.open("storage_test", "w")
|
|
|
|
f.write("storage contents")
|
|
|
|
f.close()
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertTrue(self.storage.exists("storage_test"))
|
2009-05-08 10:56:51 +00:00
|
|
|
|
|
|
|
f = self.storage.open("storage_test", "r")
|
|
|
|
self.assertEqual(f.read(), "storage contents")
|
|
|
|
f.close()
|
2010-10-08 15:11:59 +00:00
|
|
|
|
2009-05-08 10:56:51 +00:00
|
|
|
self.storage.delete("storage_test")
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertFalse(self.storage.exists("storage_test"))
|
2009-05-08 10:56:51 +00:00
|
|
|
|
2016-02-09 15:00:14 +00:00
|
|
|
def _test_file_time_getter(self, getter):
|
|
|
|
# Check for correct behavior under both USE_TZ=True and USE_TZ=False.
|
|
|
|
# The tests are similar since they both set up a situation where the
|
|
|
|
# system time zone, Django's TIME_ZONE, and UTC are distinct.
|
|
|
|
self._test_file_time_getter_tz_handling_on(getter)
|
|
|
|
self._test_file_time_getter_tz_handling_off(getter)
|
|
|
|
|
|
|
|
@override_settings(USE_TZ=True, TIME_ZONE="Africa/Algiers")
|
|
|
|
def _test_file_time_getter_tz_handling_on(self, getter):
|
|
|
|
# Django's TZ (and hence the system TZ) is set to Africa/Algiers which
|
|
|
|
# is UTC+1 and has no DST change. We can set the Django TZ to something
|
|
|
|
# else so that UTC, Django's TIME_ZONE, and the system timezone are all
|
|
|
|
# different.
|
|
|
|
now_in_algiers = timezone.make_aware(datetime.now())
|
|
|
|
|
|
|
|
with timezone.override(timezone.get_fixed_timezone(-300)):
|
|
|
|
# At this point the system TZ is +1 and the Django TZ
|
|
|
|
# is -5. The following will be aware in UTC.
|
|
|
|
now = timezone.now()
|
|
|
|
self.assertFalse(self.storage.exists("test.file.tz.on"))
|
|
|
|
|
|
|
|
f = ContentFile("custom contents")
|
|
|
|
f_name = self.storage.save("test.file.tz.on", f)
|
|
|
|
self.addCleanup(self.storage.delete, f_name)
|
|
|
|
dt = getter(f_name)
|
|
|
|
# dt should be aware, in UTC
|
|
|
|
self.assertTrue(timezone.is_aware(dt))
|
|
|
|
self.assertEqual(now.tzname(), dt.tzname())
|
|
|
|
|
2016-10-27 07:53:39 +00:00
|
|
|
# The three timezones are indeed distinct.
|
2016-02-09 15:00:14 +00:00
|
|
|
naive_now = datetime.now()
|
|
|
|
algiers_offset = now_in_algiers.tzinfo.utcoffset(naive_now)
|
|
|
|
django_offset = timezone.get_current_timezone().utcoffset(naive_now)
|
2022-03-23 11:15:36 +00:00
|
|
|
utc_offset = datetime_timezone.utc.utcoffset(naive_now)
|
2016-02-09 15:00:14 +00:00
|
|
|
self.assertGreater(algiers_offset, utc_offset)
|
|
|
|
self.assertLess(django_offset, utc_offset)
|
|
|
|
|
|
|
|
# dt and now should be the same effective time.
|
|
|
|
self.assertLess(abs(dt - now), timedelta(seconds=2))
|
|
|
|
|
|
|
|
@override_settings(USE_TZ=False, TIME_ZONE="Africa/Algiers")
|
|
|
|
def _test_file_time_getter_tz_handling_off(self, getter):
|
|
|
|
# Django's TZ (and hence the system TZ) is set to Africa/Algiers which
|
|
|
|
# is UTC+1 and has no DST change. We can set the Django TZ to something
|
|
|
|
# else so that UTC, Django's TIME_ZONE, and the system timezone are all
|
|
|
|
# different.
|
|
|
|
now_in_algiers = timezone.make_aware(datetime.now())
|
|
|
|
|
|
|
|
with timezone.override(timezone.get_fixed_timezone(-300)):
|
|
|
|
# At this point the system TZ is +1 and the Django TZ
|
|
|
|
# is -5.
|
|
|
|
self.assertFalse(self.storage.exists("test.file.tz.off"))
|
|
|
|
|
|
|
|
f = ContentFile("custom contents")
|
|
|
|
f_name = self.storage.save("test.file.tz.off", f)
|
|
|
|
self.addCleanup(self.storage.delete, f_name)
|
|
|
|
dt = getter(f_name)
|
|
|
|
# dt should be naive, in system (+1) TZ
|
|
|
|
self.assertTrue(timezone.is_naive(dt))
|
|
|
|
|
2016-10-27 07:53:39 +00:00
|
|
|
# The three timezones are indeed distinct.
|
2016-02-09 15:00:14 +00:00
|
|
|
naive_now = datetime.now()
|
|
|
|
algiers_offset = now_in_algiers.tzinfo.utcoffset(naive_now)
|
|
|
|
django_offset = timezone.get_current_timezone().utcoffset(naive_now)
|
2022-03-23 11:15:36 +00:00
|
|
|
utc_offset = datetime_timezone.utc.utcoffset(naive_now)
|
2016-02-09 15:00:14 +00:00
|
|
|
self.assertGreater(algiers_offset, utc_offset)
|
|
|
|
self.assertLess(django_offset, utc_offset)
|
|
|
|
|
|
|
|
# dt and naive_now should be the same effective time.
|
|
|
|
self.assertLess(abs(dt - naive_now), timedelta(seconds=2))
|
|
|
|
# If we convert dt to an aware object using the Algiers
|
|
|
|
# timezone then it should be the same effective time to
|
|
|
|
# now_in_algiers.
|
|
|
|
_dt = timezone.make_aware(dt, now_in_algiers.tzinfo)
|
|
|
|
self.assertLess(abs(_dt - now_in_algiers), timedelta(seconds=2))
|
|
|
|
|
|
|
|
def test_file_get_accessed_time(self):
|
2010-10-08 15:11:59 +00:00
|
|
|
"""
|
|
|
|
File storage returns a Datetime object for the last accessed time of
|
|
|
|
a file.
|
|
|
|
"""
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertFalse(self.storage.exists("test.file"))
|
2010-10-08 15:11:59 +00:00
|
|
|
|
2012-08-29 08:00:29 +00:00
|
|
|
f = ContentFile("custom contents")
|
2010-10-08 15:11:59 +00:00
|
|
|
f_name = self.storage.save("test.file", f)
|
2016-02-09 15:00:14 +00:00
|
|
|
self.addCleanup(self.storage.delete, f_name)
|
|
|
|
atime = self.storage.get_accessed_time(f_name)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
atime, datetime.fromtimestamp(os.path.getatime(self.storage.path(f_name)))
|
|
|
|
)
|
|
|
|
self.assertLess(
|
|
|
|
timezone.now() - self.storage.get_accessed_time(f_name),
|
|
|
|
timedelta(seconds=2),
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2016-02-09 15:00:14 +00:00
|
|
|
|
|
|
|
@requires_tz_support
|
|
|
|
def test_file_get_accessed_time_timezone(self):
|
|
|
|
self._test_file_time_getter(self.storage.get_accessed_time)
|
|
|
|
|
|
|
|
def test_file_get_created_time(self):
|
|
|
|
"""
|
|
|
|
File storage returns a datetime for the creation time of a file.
|
|
|
|
"""
|
|
|
|
self.assertFalse(self.storage.exists("test.file"))
|
|
|
|
|
|
|
|
f = ContentFile("custom contents")
|
|
|
|
f_name = self.storage.save("test.file", f)
|
|
|
|
self.addCleanup(self.storage.delete, f_name)
|
|
|
|
ctime = self.storage.get_created_time(f_name)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
ctime, datetime.fromtimestamp(os.path.getctime(self.storage.path(f_name)))
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2016-02-09 15:00:14 +00:00
|
|
|
self.assertLess(
|
|
|
|
timezone.now() - self.storage.get_created_time(f_name), timedelta(seconds=2)
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2016-02-09 15:00:14 +00:00
|
|
|
|
|
|
|
@requires_tz_support
|
|
|
|
def test_file_get_created_time_timezone(self):
|
|
|
|
self._test_file_time_getter(self.storage.get_created_time)
|
|
|
|
|
|
|
|
def test_file_get_modified_time(self):
|
|
|
|
"""
|
|
|
|
File storage returns a datetime for the last modified time of a file.
|
|
|
|
"""
|
|
|
|
self.assertFalse(self.storage.exists("test.file"))
|
|
|
|
|
|
|
|
f = ContentFile("custom contents")
|
|
|
|
f_name = self.storage.save("test.file", f)
|
|
|
|
self.addCleanup(self.storage.delete, f_name)
|
|
|
|
mtime = self.storage.get_modified_time(f_name)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
mtime, datetime.fromtimestamp(os.path.getmtime(self.storage.path(f_name)))
|
|
|
|
)
|
|
|
|
self.assertLess(
|
|
|
|
timezone.now() - self.storage.get_modified_time(f_name),
|
|
|
|
timedelta(seconds=2),
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2010-10-08 15:11:59 +00:00
|
|
|
|
2016-02-09 15:00:14 +00:00
|
|
|
@requires_tz_support
|
|
|
|
def test_file_get_modified_time_timezone(self):
|
|
|
|
self._test_file_time_getter(self.storage.get_modified_time)
|
|
|
|
|
2010-09-13 23:15:06 +00: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 07:28:12 +00:00
|
|
|
self.assertFalse(self.storage.exists("test.file"))
|
2010-09-13 23:15:06 +00:00
|
|
|
|
2012-08-29 08:00:29 +00:00
|
|
|
f = ContentFile("custom contents")
|
2010-09-13 23:15:06 +00:00
|
|
|
f.name = "test.file"
|
|
|
|
|
|
|
|
storage_f_name = self.storage.save(None, f)
|
|
|
|
|
|
|
|
self.assertEqual(storage_f_name, f.name)
|
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertTrue(os.path.exists(os.path.join(self.temp_dir, f.name)))
|
2010-09-13 23:15:06 +00:00
|
|
|
|
|
|
|
self.storage.delete(storage_f_name)
|
|
|
|
|
2011-05-26 08:21:35 +00:00
|
|
|
def test_file_save_with_path(self):
|
|
|
|
"""
|
|
|
|
Saving a pathname should create intermediate directories as necessary.
|
|
|
|
"""
|
|
|
|
self.assertFalse(self.storage.exists("path/to"))
|
2016-04-08 02:04:45 +00:00
|
|
|
self.storage.save("path/to/test.file", ContentFile("file saved with path"))
|
2011-05-26 08:21:35 +00:00
|
|
|
|
|
|
|
self.assertTrue(self.storage.exists("path/to"))
|
2012-08-15 09:04:40 +00:00
|
|
|
with self.storage.open("path/to/test.file") as f:
|
|
|
|
self.assertEqual(f.read(), b"file saved with path")
|
2011-05-26 08:21:35 +00:00
|
|
|
|
|
|
|
self.assertTrue(
|
|
|
|
os.path.exists(os.path.join(self.temp_dir, "path", "to", "test.file"))
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2011-05-26 08:21:35 +00:00
|
|
|
|
|
|
|
self.storage.delete("path/to/test.file")
|
|
|
|
|
2021-12-17 20:07:50 +00:00
|
|
|
def test_file_save_abs_path(self):
|
|
|
|
test_name = "path/to/test.file"
|
|
|
|
f = ContentFile("file saved with path")
|
|
|
|
f_name = self.storage.save(os.path.join(self.temp_dir, test_name), f)
|
|
|
|
self.assertEqual(f_name, test_name)
|
|
|
|
|
2021-06-02 05:03:52 +00:00
|
|
|
@unittest.skipUnless(
|
|
|
|
symlinks_supported(), "Must be able to symlink to run this test."
|
|
|
|
)
|
|
|
|
def test_file_save_broken_symlink(self):
|
|
|
|
"""A new path is created on save when a broken symlink is supplied."""
|
|
|
|
nonexistent_file_path = os.path.join(self.temp_dir, "nonexistent.txt")
|
|
|
|
broken_symlink_path = os.path.join(self.temp_dir, "symlink.txt")
|
|
|
|
os.symlink(nonexistent_file_path, broken_symlink_path)
|
|
|
|
f = ContentFile("some content")
|
|
|
|
f_name = self.storage.save(broken_symlink_path, f)
|
|
|
|
self.assertIs(os.path.exists(os.path.join(self.temp_dir, f_name)), True)
|
|
|
|
|
2014-05-25 20:52:47 +00: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)
|
|
|
|
|
2017-01-07 11:11:46 +00:00
|
|
|
file = InMemoryUploadedFile(StringIO("1"), "", "test", "text/plain", 1, "utf8")
|
2014-05-25 20:52:47 +00:00
|
|
|
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-13 23:15:06 +00:00
|
|
|
def test_file_path(self):
|
|
|
|
"""
|
|
|
|
File storage returns the full path of a file
|
|
|
|
"""
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertFalse(self.storage.exists("test.file"))
|
2010-09-13 23:15:06 +00:00
|
|
|
|
2012-08-29 08:00:29 +00:00
|
|
|
f = ContentFile("custom contents")
|
2010-09-13 23:15:06 +00:00
|
|
|
f_name = self.storage.save("test.file", f)
|
|
|
|
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(self.storage.path(f_name), os.path.join(self.temp_dir, f_name))
|
2010-09-13 23:15:06 +00:00
|
|
|
|
|
|
|
self.storage.delete(f_name)
|
|
|
|
|
|
|
|
def test_file_url(self):
|
|
|
|
"""
|
2021-07-23 06:48:16 +00:00
|
|
|
File storage returns a url to access a given file from the web.
|
2010-09-13 23:15:06 +00:00
|
|
|
"""
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self.storage.url("test.file"), self.storage.base_url + "test.file"
|
|
|
|
)
|
2010-09-13 23:15:06 +00:00
|
|
|
|
2011-02-04 14:43:10 +00:00
|
|
|
# should encode special chars except ~!*()'
|
|
|
|
# like encodeURIComponent() JavaScript function do
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self.storage.url(r"~!*()'@#$%^&*abc`+ =.file"),
|
|
|
|
"/test_media_url/~!*()'%40%23%24%25%5E%26*abc%60%2B%20%3D.file",
|
|
|
|
)
|
|
|
|
self.assertEqual(self.storage.url("ab\0c"), "/test_media_url/ab%00c")
|
2011-02-04 14:43:10 +00:00
|
|
|
|
2015-05-20 17:05:41 +00:00
|
|
|
# should translate os path separator(s) to the url path separator
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self.storage.url("""a/b\\c.file"""), "/test_media_url/a/b/c.file"
|
|
|
|
)
|
2011-02-04 14:43:10 +00:00
|
|
|
|
2016-04-03 15:21:56 +00:00
|
|
|
# #25905: remove leading slashes from file names to prevent unsafe url output
|
|
|
|
self.assertEqual(self.storage.url("/evil.com"), "/test_media_url/evil.com")
|
|
|
|
self.assertEqual(self.storage.url(r"\evil.com"), "/test_media_url/evil.com")
|
|
|
|
self.assertEqual(self.storage.url("///evil.com"), "/test_media_url/evil.com")
|
|
|
|
self.assertEqual(self.storage.url(r"\\\evil.com"), "/test_media_url/evil.com")
|
|
|
|
|
|
|
|
self.assertEqual(self.storage.url(None), "/test_media_url/")
|
|
|
|
|
|
|
|
def test_base_url(self):
|
|
|
|
"""
|
|
|
|
File storage returns a url even when its base_url is unset or modified.
|
|
|
|
"""
|
2010-09-13 23:15:06 +00:00
|
|
|
self.storage.base_url = None
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
self.storage.url("test.file")
|
2010-09-13 23:15:06 +00:00
|
|
|
|
2014-05-28 17:03:26 +00:00
|
|
|
# #22717: missing ending slash in base_url should be auto-corrected
|
2016-04-08 02:04:45 +00:00
|
|
|
storage = self.storage_class(
|
|
|
|
location=self.temp_dir, base_url="/no_ending_slash"
|
|
|
|
)
|
2014-05-28 17:03:26 +00:00
|
|
|
self.assertEqual(
|
|
|
|
storage.url("test.file"), "%s%s" % (storage.base_url, "test.file")
|
|
|
|
)
|
|
|
|
|
2010-09-13 23:15:06 +00:00
|
|
|
def test_listdir(self):
|
|
|
|
"""
|
|
|
|
File storage returns a tuple containing directories and files.
|
|
|
|
"""
|
2010-12-04 07:28:12 +00: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-13 23:15:06 +00:00
|
|
|
|
2013-10-19 12:31:38 +00:00
|
|
|
self.storage.save("storage_test_1", ContentFile("custom content"))
|
|
|
|
self.storage.save("storage_test_2", ContentFile("custom content"))
|
2010-09-13 23:15:06 +00:00
|
|
|
os.mkdir(os.path.join(self.temp_dir, "storage_dir_1"))
|
|
|
|
|
2019-10-31 07:51:12 +00:00
|
|
|
self.addCleanup(self.storage.delete, "storage_test_1")
|
|
|
|
self.addCleanup(self.storage.delete, "storage_test_2")
|
2010-09-13 23:15:06 +00:00
|
|
|
|
2019-10-31 07:51:12 +00:00
|
|
|
for directory in ("", Path("")):
|
|
|
|
with self.subTest(directory=directory):
|
|
|
|
dirs, files = self.storage.listdir(directory)
|
|
|
|
self.assertEqual(set(dirs), {"storage_dir_1"})
|
|
|
|
self.assertEqual(set(files), {"storage_test_1", "storage_test_2"})
|
2010-09-13 23:15:06 +00:00
|
|
|
|
2009-05-08 10:56:51 +00: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).
|
|
|
|
"""
|
2017-05-25 12:19:01 +00:00
|
|
|
with self.assertRaises(SuspiciousFileOperation):
|
2016-01-17 11:26:39 +00:00
|
|
|
self.storage.exists("..")
|
2017-05-25 12:19:01 +00:00
|
|
|
with self.assertRaises(SuspiciousFileOperation):
|
2016-01-17 11:26:39 +00:00
|
|
|
self.storage.exists("/etc/passwd")
|
2009-05-08 10:56:51 +00:00
|
|
|
|
2011-05-22 23:56:42 +00: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-29 21:22:45 +00:00
|
|
|
other_temp_storage = self.storage_class(location=self.temp_dir2)
|
2011-05-22 23:56:42 +00:00
|
|
|
# Ask that storage backend to store a file with a mixed case filename.
|
|
|
|
mixed_case = "CaSe_SeNsItIvE"
|
2013-10-29 21:22:45 +00:00
|
|
|
file = other_temp_storage.open(mixed_case, "w")
|
2011-05-22 23:56:42 +00:00
|
|
|
file.write("storage contents")
|
|
|
|
file.close()
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(
|
|
|
|
os.path.join(self.temp_dir2, mixed_case),
|
|
|
|
other_temp_storage.path(mixed_case),
|
|
|
|
)
|
2013-10-29 21:22:45 +00:00
|
|
|
other_temp_storage.delete(mixed_case)
|
2011-05-22 23:56:42 +00:00
|
|
|
|
2011-05-26 08:21:35 +00:00
|
|
|
def test_makedirs_race_handling(self):
|
|
|
|
"""
|
|
|
|
File storage should be robust against directory creation race conditions.
|
|
|
|
"""
|
2011-05-28 13:06:08 +00:00
|
|
|
real_makedirs = os.makedirs
|
|
|
|
|
2011-05-26 08:21:35 +00:00
|
|
|
# Monkey-patch os.makedirs, to simulate a normal call, a raced call,
|
|
|
|
# and an error.
|
2019-01-31 15:12:55 +00:00
|
|
|
def fake_makedirs(path, mode=0o777, exist_ok=False):
|
2011-05-26 08:21:35 +00:00
|
|
|
if path == os.path.join(self.temp_dir, "normal"):
|
2019-01-31 15:12:55 +00:00
|
|
|
real_makedirs(path, mode, exist_ok)
|
2011-05-26 08:21:35 +00:00
|
|
|
elif path == os.path.join(self.temp_dir, "raced"):
|
2019-01-31 15:12:55 +00:00
|
|
|
real_makedirs(path, mode, exist_ok)
|
|
|
|
if not exist_ok:
|
|
|
|
raise FileExistsError()
|
2018-10-31 23:25:53 +00:00
|
|
|
elif path == os.path.join(self.temp_dir, "error"):
|
|
|
|
raise PermissionError()
|
2011-05-26 08:21:35 +00:00
|
|
|
else:
|
|
|
|
self.fail("unexpected argument %r" % path)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.makedirs = fake_makedirs
|
|
|
|
|
2016-04-08 02:04:45 +00:00
|
|
|
self.storage.save("normal/test.file", ContentFile("saved normally"))
|
2012-08-15 09:04:40 +00:00
|
|
|
with self.storage.open("normal/test.file") as f:
|
|
|
|
self.assertEqual(f.read(), b"saved normally")
|
2011-05-26 08:21:35 +00:00
|
|
|
|
2016-04-08 02:04:45 +00:00
|
|
|
self.storage.save("raced/test.file", ContentFile("saved with race"))
|
2012-08-15 09:04:40 +00:00
|
|
|
with self.storage.open("raced/test.file") as f:
|
|
|
|
self.assertEqual(f.read(), b"saved with race")
|
2011-05-26 08:21:35 +00:00
|
|
|
|
2018-10-31 23:25:53 +00:00
|
|
|
# Exceptions aside from FileExistsError are raised.
|
|
|
|
with self.assertRaises(PermissionError):
|
2016-01-17 11:26:39 +00:00
|
|
|
self.storage.save("error/test.file", ContentFile("not saved"))
|
2011-05-26 08:21:35 +00:00
|
|
|
finally:
|
|
|
|
os.makedirs = real_makedirs
|
|
|
|
|
2011-05-28 13:06:08 +00: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)
|
2017-01-25 15:13:08 +00:00
|
|
|
raise FileNotFoundError()
|
2011-05-28 13:06:08 +00:00
|
|
|
elif path == os.path.join(self.temp_dir, "error.file"):
|
2017-01-25 15:13:08 +00:00
|
|
|
raise PermissionError()
|
2011-05-28 13:06:08 +00:00
|
|
|
else:
|
|
|
|
self.fail("unexpected argument %r" % path)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove = fake_remove
|
|
|
|
|
2012-08-29 08:00:29 +00:00
|
|
|
self.storage.save("normal.file", ContentFile("delete normally"))
|
2011-05-28 13:06:08 +00:00
|
|
|
self.storage.delete("normal.file")
|
|
|
|
self.assertFalse(self.storage.exists("normal.file"))
|
|
|
|
|
2012-08-29 08:00:29 +00:00
|
|
|
self.storage.save("raced.file", ContentFile("delete with race"))
|
2011-05-28 13:06:08 +00:00
|
|
|
self.storage.delete("raced.file")
|
|
|
|
self.assertFalse(self.storage.exists("normal.file"))
|
|
|
|
|
2017-01-25 15:13:08 +00:00
|
|
|
# Exceptions aside from FileNotFoundError are raised.
|
2012-08-29 08:00:29 +00:00
|
|
|
self.storage.save("error.file", ContentFile("delete with error"))
|
2017-01-25 15:13:08 +00:00
|
|
|
with self.assertRaises(PermissionError):
|
2016-01-17 11:26:39 +00:00
|
|
|
self.storage.delete("error.file")
|
2011-05-28 13:06:08 +00:00
|
|
|
finally:
|
|
|
|
os.remove = real_remove
|
|
|
|
|
2012-08-29 13:13:20 +00:00
|
|
|
def test_file_chunks_error(self):
|
|
|
|
"""
|
2014-03-02 14:25:53 +00:00
|
|
|
Test behavior when file.chunks() is raising an error
|
2012-08-29 13:13:20 +00:00
|
|
|
"""
|
|
|
|
f1 = ContentFile("chunks fails")
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2012-08-29 13:13:20 +00:00
|
|
|
def failing_chunks():
|
2019-01-28 15:01:35 +00:00
|
|
|
raise OSError
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2012-08-29 13:13:20 +00:00
|
|
|
f1.chunks = failing_chunks
|
2019-01-28 15:01:35 +00:00
|
|
|
with self.assertRaises(OSError):
|
2012-08-29 13:13:20 +00:00
|
|
|
self.storage.save("error.file", f1)
|
|
|
|
|
2013-06-27 08:59:30 +00: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).
|
|
|
|
"""
|
2021-03-16 15:41:27 +00:00
|
|
|
msg = "The name must be given to delete()."
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
self.storage.delete(None)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2013-06-27 08:59:30 +00:00
|
|
|
self.storage.delete("")
|
|
|
|
|
2017-02-22 14:44:25 +00:00
|
|
|
def test_delete_deletes_directories(self):
|
2017-03-01 17:52:28 +00:00
|
|
|
tmp_dir = tempfile.mkdtemp(dir=self.storage.location)
|
|
|
|
self.storage.delete(tmp_dir)
|
|
|
|
self.assertFalse(os.path.exists(tmp_dir))
|
2017-02-22 14:44:25 +00:00
|
|
|
|
2016-01-07 04:34:55 +00:00
|
|
|
@override_settings(
|
|
|
|
MEDIA_ROOT="media_root",
|
|
|
|
MEDIA_URL="media_url/",
|
|
|
|
FILE_UPLOAD_PERMISSIONS=0o777,
|
|
|
|
FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o777,
|
|
|
|
)
|
|
|
|
def test_setting_changed(self):
|
|
|
|
"""
|
|
|
|
Properties using settings values as defaults should be updated on
|
|
|
|
referenced settings change while specified values should be unchanged.
|
|
|
|
"""
|
|
|
|
storage = self.storage_class(
|
|
|
|
location="explicit_location",
|
|
|
|
base_url="explicit_base_url/",
|
|
|
|
file_permissions_mode=0o666,
|
|
|
|
directory_permissions_mode=0o666,
|
|
|
|
)
|
|
|
|
defaults_storage = self.storage_class()
|
|
|
|
settings = {
|
2019-08-05 15:47:50 +00:00
|
|
|
"MEDIA_ROOT": "overridden_media_root",
|
2018-12-06 00:15:33 +00:00
|
|
|
"MEDIA_URL": "/overridden_media_url/",
|
2016-01-07 04:34:55 +00:00
|
|
|
"FILE_UPLOAD_PERMISSIONS": 0o333,
|
|
|
|
"FILE_UPLOAD_DIRECTORY_PERMISSIONS": 0o333,
|
|
|
|
}
|
|
|
|
with self.settings(**settings):
|
|
|
|
self.assertEqual(storage.base_location, "explicit_location")
|
|
|
|
self.assertIn("explicit_location", storage.location)
|
|
|
|
self.assertEqual(storage.base_url, "explicit_base_url/")
|
|
|
|
self.assertEqual(storage.file_permissions_mode, 0o666)
|
|
|
|
self.assertEqual(storage.directory_permissions_mode, 0o666)
|
|
|
|
self.assertEqual(defaults_storage.base_location, settings["MEDIA_ROOT"])
|
|
|
|
self.assertIn(settings["MEDIA_ROOT"], defaults_storage.location)
|
|
|
|
self.assertEqual(defaults_storage.base_url, settings["MEDIA_URL"])
|
|
|
|
self.assertEqual(
|
|
|
|
defaults_storage.file_permissions_mode,
|
|
|
|
settings["FILE_UPLOAD_PERMISSIONS"],
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2016-01-07 04:34:55 +00:00
|
|
|
self.assertEqual(
|
|
|
|
defaults_storage.directory_permissions_mode,
|
|
|
|
settings["FILE_UPLOAD_DIRECTORY_PERMISSIONS"],
|
|
|
|
)
|
|
|
|
|
2019-10-31 07:51:12 +00:00
|
|
|
def test_file_methods_pathlib_path(self):
|
|
|
|
p = Path("test.file")
|
|
|
|
self.assertFalse(self.storage.exists(p))
|
|
|
|
f = ContentFile("custom contents")
|
2019-10-21 16:03:48 +00:00
|
|
|
f_name = self.storage.save(p, f)
|
2019-10-31 07:51:12 +00:00
|
|
|
# Storage basic methods.
|
|
|
|
self.assertEqual(self.storage.path(p), os.path.join(self.temp_dir, p))
|
|
|
|
self.assertEqual(self.storage.size(p), 15)
|
|
|
|
self.assertEqual(self.storage.url(p), self.storage.base_url + f_name)
|
|
|
|
with self.storage.open(p) as f:
|
|
|
|
self.assertEqual(f.read(), b"custom contents")
|
|
|
|
self.addCleanup(self.storage.delete, p)
|
|
|
|
|
2011-05-28 13:06:08 +00:00
|
|
|
|
2009-05-08 10:56:51 +00:00
|
|
|
class CustomStorage(FileSystemStorage):
|
2014-10-15 07:42:06 +00:00
|
|
|
def get_available_name(self, name, max_length=None):
|
2009-05-08 10:56:51 +00:00
|
|
|
"""
|
|
|
|
Append numbers to duplicate files rather than underscores, like Trac.
|
|
|
|
"""
|
2019-10-21 16:03:48 +00:00
|
|
|
basename, *ext = os.path.splitext(name)
|
2009-05-08 10:56:51 +00:00
|
|
|
number = 2
|
|
|
|
while self.exists(name):
|
2019-10-21 16:03:48 +00:00
|
|
|
name = "".join([basename, ".", str(number)] + ext)
|
2009-05-08 10:56:51 +00:00
|
|
|
number += 1
|
|
|
|
|
|
|
|
return name
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2009-05-08 10:56:51 +00:00
|
|
|
class CustomStorageTests(FileStorageTests):
|
|
|
|
storage_class = CustomStorage
|
2010-10-08 15:11:59 +00:00
|
|
|
|
2009-05-08 10:56:51 +00:00
|
|
|
def test_custom_get_available_name(self):
|
2012-08-29 08:00:29 +00:00
|
|
|
first = self.storage.save("custom_storage", ContentFile("custom contents"))
|
2009-05-08 10:56:51 +00:00
|
|
|
self.assertEqual(first, "custom_storage")
|
2012-08-29 08:00:29 +00:00
|
|
|
second = self.storage.save("custom_storage", ContentFile("more contents"))
|
2009-05-08 10:56:51 +00:00
|
|
|
self.assertEqual(second, "custom_storage.2")
|
|
|
|
self.storage.delete(first)
|
|
|
|
self.storage.delete(second)
|
|
|
|
|
2013-10-27 20:26:51 +00:00
|
|
|
|
2017-04-26 15:43:56 +00:00
|
|
|
class OverwritingStorage(FileSystemStorage):
|
|
|
|
"""
|
|
|
|
Overwrite existing files instead of appending a suffix to generate an
|
|
|
|
unused name.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2017-04-26 15:43:56 +00:00
|
|
|
# Mask out O_EXCL so os.open() doesn't raise OSError if the file exists.
|
|
|
|
OS_OPEN_FLAGS = FileSystemStorage.OS_OPEN_FLAGS & ~os.O_EXCL
|
|
|
|
|
|
|
|
def get_available_name(self, name, max_length=None):
|
|
|
|
"""Override the effort to find an used name."""
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
|
|
|
class OverwritingStorageTests(FileStorageTests):
|
|
|
|
storage_class = OverwritingStorage
|
|
|
|
|
|
|
|
def test_save_overwrite_behavior(self):
|
|
|
|
"""Saving to same file name twice overwrites the first file."""
|
|
|
|
name = "test.file"
|
|
|
|
self.assertFalse(self.storage.exists(name))
|
|
|
|
content_1 = b"content one"
|
|
|
|
content_2 = b"second content"
|
|
|
|
f_1 = ContentFile(content_1)
|
|
|
|
f_2 = ContentFile(content_2)
|
|
|
|
stored_name_1 = self.storage.save(name, f_1)
|
|
|
|
try:
|
|
|
|
self.assertEqual(stored_name_1, name)
|
|
|
|
self.assertTrue(self.storage.exists(name))
|
|
|
|
self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name)))
|
2018-07-10 10:26:19 +00:00
|
|
|
with self.storage.open(name) as fp:
|
|
|
|
self.assertEqual(fp.read(), content_1)
|
2017-04-26 15:43:56 +00:00
|
|
|
stored_name_2 = self.storage.save(name, f_2)
|
|
|
|
self.assertEqual(stored_name_2, name)
|
|
|
|
self.assertTrue(self.storage.exists(name))
|
|
|
|
self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name)))
|
2018-07-10 10:26:19 +00:00
|
|
|
with self.storage.open(name) as fp:
|
|
|
|
self.assertEqual(fp.read(), content_2)
|
2017-04-26 15:43:56 +00:00
|
|
|
finally:
|
|
|
|
self.storage.delete(name)
|
|
|
|
|
|
|
|
|
2016-04-13 05:38:56 +00:00
|
|
|
class DiscardingFalseContentStorage(FileSystemStorage):
|
|
|
|
def _save(self, name, content):
|
|
|
|
if content:
|
2017-01-21 13:13:44 +00:00
|
|
|
return super()._save(name, content)
|
2016-04-13 05:38:56 +00:00
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
class DiscardingFalseContentStorageTests(FileStorageTests):
|
|
|
|
storage_class = DiscardingFalseContentStorage
|
|
|
|
|
|
|
|
def test_custom_storage_discarding_empty_content(self):
|
|
|
|
"""
|
|
|
|
When Storage.save() wraps a file-like object in File, it should include
|
|
|
|
the name argument so that bool(file) evaluates to True (#26495).
|
|
|
|
"""
|
2017-01-07 11:11:46 +00:00
|
|
|
output = StringIO("content")
|
2016-04-13 05:38:56 +00:00
|
|
|
self.storage.save("tests/stringio", output)
|
|
|
|
self.assertTrue(self.storage.exists("tests/stringio"))
|
|
|
|
|
|
|
|
with self.storage.open("tests/stringio") as f:
|
|
|
|
self.assertEqual(f.read(), b"content")
|
|
|
|
|
|
|
|
|
2015-04-13 15:56:58 +00:00
|
|
|
class FileFieldStorageTests(TestCase):
|
2013-10-27 20:26:51 +00:00
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(temp_storage_location)
|
|
|
|
|
2015-05-22 17:43:08 +00: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-27 20:26:51 +00:00
|
|
|
def test_files(self):
|
2015-10-01 17:00:44 +00:00
|
|
|
self.assertIsInstance(Storage.normal, FileDescriptor)
|
2013-10-27 20:26:51 +00:00
|
|
|
|
|
|
|
# An object without a file has limited functionality.
|
|
|
|
obj1 = Storage()
|
|
|
|
self.assertEqual(obj1.normal.name, "")
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
obj1.normal.size
|
2013-10-27 20:26:51 +00:00
|
|
|
|
|
|
|
# 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 10:02:56 +00:00
|
|
|
self.assertNotIn("assignment.txt", files)
|
2013-10-27 20:26:51 +00: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 14:20:08 +00:00
|
|
|
obj2_name = obj2.normal.name
|
2016-12-07 22:42:31 +00:00
|
|
|
self.assertRegex(obj2_name, "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX)
|
2013-10-27 20:26:51 +00: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 14:20:08 +00:00
|
|
|
self.assertNotEqual(obj2_name, obj2.normal.name)
|
2016-12-07 22:42:31 +00:00
|
|
|
self.assertRegex(
|
|
|
|
obj2.normal.name, "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX
|
|
|
|
)
|
2013-10-27 20:26:51 +00: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")
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2013-10-27 20:26:51 +00:00
|
|
|
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()
|
|
|
|
|
2016-07-19 20:17:39 +00:00
|
|
|
def test_filefield_write(self):
|
|
|
|
# Files can be written to.
|
|
|
|
obj = Storage.objects.create(
|
|
|
|
normal=SimpleUploadedFile("rewritten.txt", b"content")
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2016-07-19 20:17:39 +00:00
|
|
|
with obj.normal as normal:
|
|
|
|
normal.open("wb")
|
|
|
|
normal.write(b"updated")
|
|
|
|
obj.refresh_from_db()
|
|
|
|
self.assertEqual(obj.normal.read(), b"updated")
|
|
|
|
obj.normal.close()
|
|
|
|
|
2016-06-17 08:30:40 +00:00
|
|
|
def test_filefield_reopen(self):
|
|
|
|
obj = Storage.objects.create(
|
|
|
|
normal=SimpleUploadedFile("reopen.txt", b"content")
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2016-06-17 08:30:40 +00:00
|
|
|
with obj.normal as normal:
|
|
|
|
normal.open()
|
|
|
|
obj.normal.open()
|
|
|
|
obj.normal.file.seek(0)
|
|
|
|
obj.normal.close()
|
|
|
|
|
2014-08-08 14:20:08 +00: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-27 20:26:51 +00:00
|
|
|
for o in objs:
|
|
|
|
o.normal.save("multiple_files.txt", ContentFile("Same Content"))
|
2014-08-08 14:20:08 +00:00
|
|
|
try:
|
|
|
|
names = [o.normal.name for o in objs]
|
|
|
|
self.assertEqual(names[0], "tests/multiple_files.txt")
|
2016-12-07 22:42:31 +00:00
|
|
|
self.assertRegex(
|
|
|
|
names[1], "tests/multiple_files_%s.txt" % FILE_SUFFIX_REGEX
|
|
|
|
)
|
2014-08-08 14:20:08 +00:00
|
|
|
finally:
|
|
|
|
for o in objs:
|
|
|
|
o.delete()
|
2013-10-27 20:26:51 +00:00
|
|
|
|
2014-10-15 07:42:06 +00: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)
|
2016-12-07 22:42:31 +00:00
|
|
|
self.assertRegex(names[1], "tests/fi_%s.ext" % FILE_SUFFIX_REGEX)
|
2014-10-15 07:42:06 +00:00
|
|
|
|
|
|
|
# Testing exception is raised when filename is too short to truncate.
|
|
|
|
filename = "short.longext"
|
|
|
|
objs[0].limited_length.save(filename, ContentFile("Same Content"))
|
2016-01-04 08:50:08 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
SuspiciousFileOperation, "Storage can not find an available filename"
|
|
|
|
):
|
|
|
|
objs[1].limited_length.save(*(filename, ContentFile("Same Content")))
|
2014-10-15 07:42:06 +00:00
|
|
|
finally:
|
|
|
|
for o in objs:
|
|
|
|
o.delete()
|
|
|
|
|
2015-01-14 20:00:27 +00:00
|
|
|
@unittest.skipIf(
|
2019-11-06 14:14:30 +00:00
|
|
|
sys.platform == "win32",
|
2015-01-14 20:00:27 +00:00
|
|
|
"Windows supports at most 260 characters in a path.",
|
|
|
|
)
|
2014-10-15 07:42:06 +00: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-22 17:43:08 +00:00
|
|
|
filename = (
|
|
|
|
self._storage_max_filename_length(temp_storage) - 4
|
|
|
|
) * "a" # 4 chars for extension.
|
2014-10-15 07:42:06 +00: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-27 20:26:51 +00: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"))
|
2016-03-21 01:51:17 +00:00
|
|
|
self.assertEqual(obj.empty.name, "django_test.txt")
|
2013-10-27 20:26:51 +00:00
|
|
|
self.assertEqual(obj.empty.read(), b"more content")
|
|
|
|
obj.empty.close()
|
|
|
|
|
2019-08-18 09:10:49 +00:00
|
|
|
def test_pathlib_upload_to(self):
|
|
|
|
obj = Storage()
|
|
|
|
obj.pathlib_callable.save("some_file1.txt", ContentFile("some content"))
|
|
|
|
self.assertEqual(obj.pathlib_callable.name, "bar/some_file1.txt")
|
2019-08-18 09:40:11 +00:00
|
|
|
obj.pathlib_direct.save("some_file2.txt", ContentFile("some content"))
|
|
|
|
self.assertEqual(obj.pathlib_direct.name, "bar/some_file2.txt")
|
2019-08-18 09:10:49 +00:00
|
|
|
obj.random.close()
|
|
|
|
|
2013-10-27 20:26:51 +00:00
|
|
|
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 04:10:24 +00: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-27 20:26:51 +00: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"))
|
|
|
|
|
2018-10-09 13:26:07 +00:00
|
|
|
# Load it as Python file object
|
2013-10-27 20:26:51 +00:00
|
|
|
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
|
2017-01-07 11:11:46 +00:00
|
|
|
output = StringIO()
|
2013-10-27 20:26:51 +00:00
|
|
|
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 10:56:51 +00:00
|
|
|
|
2020-03-31 10:12:39 +00:00
|
|
|
class FieldCallableFileStorageTests(SimpleTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.temp_storage_location = tempfile.mkdtemp(
|
|
|
|
suffix="filefield_callable_storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.temp_storage_location)
|
|
|
|
|
|
|
|
def test_callable_base_class_error_raises(self):
|
|
|
|
class NotStorage:
|
|
|
|
pass
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2020-03-31 10:12:39 +00:00
|
|
|
msg = (
|
|
|
|
"FileField.storage must be a subclass/instance of "
|
2022-11-10 21:01:53 +00:00
|
|
|
"django.core.files.storage.base.Storage"
|
2022-02-04 07:08:27 +00:00
|
|
|
)
|
2020-03-31 10:12:39 +00:00
|
|
|
for invalid_type in (NotStorage, str, list, set, tuple):
|
|
|
|
with self.subTest(invalid_type=invalid_type):
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
FileField(storage=invalid_type)
|
|
|
|
|
2020-04-08 08:35:15 +00:00
|
|
|
def test_file_field_storage_none_uses_default_storage(self):
|
|
|
|
self.assertEqual(FileField().storage, default_storage)
|
|
|
|
|
2020-03-31 10:12:39 +00:00
|
|
|
def test_callable_function_storage_file_field(self):
|
|
|
|
storage = FileSystemStorage(location=self.temp_storage_location)
|
|
|
|
|
|
|
|
def get_storage():
|
|
|
|
return storage
|
|
|
|
|
|
|
|
obj = FileField(storage=get_storage)
|
|
|
|
self.assertEqual(obj.storage, storage)
|
|
|
|
self.assertEqual(obj.storage.location, storage.location)
|
|
|
|
|
|
|
|
def test_callable_class_storage_file_field(self):
|
|
|
|
class GetStorage(FileSystemStorage):
|
|
|
|
pass
|
|
|
|
|
|
|
|
obj = FileField(storage=GetStorage)
|
|
|
|
self.assertIsInstance(obj.storage, BaseStorage)
|
|
|
|
|
|
|
|
def test_callable_storage_file_field_in_model(self):
|
|
|
|
obj = Storage()
|
|
|
|
self.assertEqual(obj.storage_callable.storage, temp_storage)
|
|
|
|
self.assertEqual(obj.storage_callable.storage.location, temp_storage_location)
|
|
|
|
self.assertIsInstance(obj.storage_callable_class.storage, BaseStorage)
|
|
|
|
|
2020-08-24 19:27:22 +00:00
|
|
|
def test_deconstruction(self):
|
|
|
|
"""
|
|
|
|
Deconstructing gives the original callable, not the evaluated value.
|
|
|
|
"""
|
|
|
|
obj = Storage()
|
|
|
|
*_, kwargs = obj._meta.get_field("storage_callable").deconstruct()
|
|
|
|
storage = kwargs["storage"]
|
|
|
|
self.assertIs(storage, callable_storage)
|
|
|
|
|
2020-03-31 10:12:39 +00:00
|
|
|
|
2009-05-08 10:56:51 +00: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.
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2008-08-11 16:51:18 +00:00
|
|
|
class SlowFile(ContentFile):
|
|
|
|
def chunks(self):
|
|
|
|
time.sleep(1)
|
2017-01-21 13:13:44 +00:00
|
|
|
return super().chunks()
|
2008-08-11 16:51:18 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2016-12-07 22:42:31 +00:00
|
|
|
class FileSaveRaceConditionTest(SimpleTestCase):
|
2008-08-11 16:51:18 +00:00
|
|
|
def setUp(self):
|
2008-10-10 22:13:16 +00:00
|
|
|
self.storage_dir = tempfile.mkdtemp()
|
|
|
|
self.storage = FileSystemStorage(self.storage_dir)
|
2008-08-11 16:51:18 +00:00
|
|
|
self.thread = threading.Thread(target=self.save_file, args=["conflict"])
|
2009-05-08 05:50:31 +00:00
|
|
|
|
2008-10-10 22:13:16 +00:00
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.storage_dir)
|
2009-05-08 05:50:31 +00:00
|
|
|
|
2008-08-11 16:51:18 +00:00
|
|
|
def save_file(self, name):
|
2012-05-19 15:43:34 +00:00
|
|
|
name = self.storage.save(name, SlowFile(b"Data"))
|
2009-05-08 05:50:31 +00:00
|
|
|
|
2008-08-11 16:51:18 +00:00
|
|
|
def test_race_condition(self):
|
|
|
|
self.thread.start()
|
2013-10-19 12:31:38 +00:00
|
|
|
self.save_file("conflict")
|
2008-08-11 16:51:18 +00:00
|
|
|
self.thread.join()
|
2014-08-08 14:20:08 +00:00
|
|
|
files = sorted(os.listdir(self.storage_dir))
|
|
|
|
self.assertEqual(files[0], "conflict")
|
2016-12-07 22:42:31 +00:00
|
|
|
self.assertRegex(files[1], "conflict_%s" % FILE_SUFFIX_REGEX)
|
2008-08-11 16:51:18 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2019-11-06 14:14:30 +00:00
|
|
|
@unittest.skipIf(
|
|
|
|
sys.platform == "win32", "Windows only partially supports umasks and chmod."
|
|
|
|
)
|
2010-10-11 12:55:17 +00:00
|
|
|
class FileStoragePermissions(unittest.TestCase):
|
2008-08-27 22:21:14 +00:00
|
|
|
def setUp(self):
|
2012-09-05 15:05:28 +00:00
|
|
|
self.umask = 0o027
|
|
|
|
self.old_umask = os.umask(self.umask)
|
2008-10-10 22:13:16 +00:00
|
|
|
self.storage_dir = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.storage_dir)
|
2012-09-05 15:05:28 +00:00
|
|
|
os.umask(self.old_umask)
|
2008-10-10 22:13:16 +00:00
|
|
|
|
2012-09-05 15:05:28 +00:00
|
|
|
@override_settings(FILE_UPLOAD_PERMISSIONS=0o654)
|
2008-08-27 22:21:14 +00:00
|
|
|
def test_file_upload_permissions(self):
|
2013-10-19 12:40:12 +00:00
|
|
|
self.storage = FileSystemStorage(self.storage_dir)
|
2012-08-29 08:00:29 +00:00
|
|
|
name = self.storage.save("the_file", ContentFile("data"))
|
2012-07-20 11:28:36 +00:00
|
|
|
actual_mode = os.stat(self.storage.path(name))[0] & 0o777
|
2012-09-05 15:05:28 +00:00
|
|
|
self.assertEqual(actual_mode, 0o654)
|
2008-10-10 22:13:16 +00:00
|
|
|
|
2012-09-05 15:05:28 +00:00
|
|
|
@override_settings(FILE_UPLOAD_PERMISSIONS=None)
|
|
|
|
def test_file_upload_default_permissions(self):
|
2013-10-19 12:40:12 +00:00
|
|
|
self.storage = FileSystemStorage(self.storage_dir)
|
2012-09-05 15:05:28 +00: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 05:50:31 +00:00
|
|
|
|
2013-05-13 11:38:53 +00:00
|
|
|
@override_settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o765)
|
|
|
|
def test_file_upload_directory_permissions(self):
|
2013-10-19 12:40:12 +00:00
|
|
|
self.storage = FileSystemStorage(self.storage_dir)
|
2020-08-21 09:44:46 +00:00
|
|
|
name = self.storage.save("the_directory/subdir/the_file", ContentFile("data"))
|
|
|
|
file_path = Path(self.storage.path(name))
|
|
|
|
self.assertEqual(file_path.parent.stat().st_mode & 0o777, 0o765)
|
|
|
|
self.assertEqual(file_path.parent.parent.stat().st_mode & 0o777, 0o765)
|
2013-05-13 11:38:53 +00:00
|
|
|
|
|
|
|
@override_settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=None)
|
|
|
|
def test_file_upload_directory_default_permissions(self):
|
2013-10-19 12:40:12 +00:00
|
|
|
self.storage = FileSystemStorage(self.storage_dir)
|
2020-08-21 09:44:46 +00:00
|
|
|
name = self.storage.save("the_directory/subdir/the_file", ContentFile("data"))
|
|
|
|
file_path = Path(self.storage.path(name))
|
|
|
|
expected_mode = 0o777 & ~self.umask
|
|
|
|
self.assertEqual(file_path.parent.stat().st_mode & 0o777, expected_mode)
|
|
|
|
self.assertEqual(file_path.parent.parent.stat().st_mode & 0o777, expected_mode)
|
2013-05-13 11:38:53 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2016-12-07 22:42:31 +00:00
|
|
|
class FileStoragePathParsing(SimpleTestCase):
|
2009-05-08 05:50:31 +00: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 08:00:29 +00:00
|
|
|
self.storage.save("dotted.path/test", ContentFile("1"))
|
|
|
|
self.storage.save("dotted.path/test", ContentFile("2"))
|
2009-05-08 05:50:31 +00:00
|
|
|
|
2014-08-08 14:20:08 +00:00
|
|
|
files = sorted(os.listdir(os.path.join(self.storage_dir, "dotted.path")))
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertFalse(os.path.exists(os.path.join(self.storage_dir, "dotted_.path")))
|
2014-08-08 14:20:08 +00:00
|
|
|
self.assertEqual(files[0], "test")
|
2016-12-07 22:42:31 +00:00
|
|
|
self.assertRegex(files[1], "test_%s" % FILE_SUFFIX_REGEX)
|
2009-05-08 05:50:31 +00: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 08:00:29 +00:00
|
|
|
self.storage.save("dotted.path/.test", ContentFile("1"))
|
|
|
|
self.storage.save("dotted.path/.test", ContentFile("2"))
|
2009-05-08 05:50:31 +00:00
|
|
|
|
2014-08-08 14:20:08 +00: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")
|
2016-12-07 22:42:31 +00:00
|
|
|
self.assertRegex(files[1], ".test_%s" % FILE_SUFFIX_REGEX)
|
2009-05-08 10:56:51 +00:00
|
|
|
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2013-10-27 20:26:51 +00:00
|
|
|
class ContentFileStorageTestCase(unittest.TestCase):
|
2012-12-06 16:14:44 +00: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):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
ContentFile can be saved correctly with the filesystem storage,
|
|
|
|
if it was initialized with either bytes or unicode content.
|
|
|
|
"""
|
2012-12-06 16:14:44 +00:00
|
|
|
self.storage.save("bytes.txt", ContentFile(b"content"))
|
|
|
|
self.storage.save("unicode.txt", ContentFile("español"))
|
|
|
|
|
2012-08-29 07:45:02 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF="file_storage.urls")
|
2013-06-04 05:55:20 +00:00
|
|
|
class FileLikeObjectTestCase(LiveServerTestCase):
|
2012-04-05 15:44:04 +00:00
|
|
|
"""
|
|
|
|
Test file-like objects (#15644).
|
|
|
|
"""
|
2013-06-04 06:09:29 +00:00
|
|
|
|
|
|
|
available_apps = []
|
2013-06-04 05:55:20 +00:00
|
|
|
|
2012-04-05 15:44:04 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.temp_dir = tempfile.mkdtemp()
|
|
|
|
self.storage = FileSystemStorage(location=self.temp_dir)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.temp_dir)
|
|
|
|
|
2018-04-27 12:02:39 +00:00
|
|
|
def test_urllib_request_urlopen(self):
|
2012-04-05 15:44:04 +00:00
|
|
|
"""
|
2018-04-27 12:02:39 +00:00
|
|
|
Test the File storage API with a file-like object coming from
|
|
|
|
urllib.request.urlopen().
|
2012-04-05 15:44:04 +00:00
|
|
|
"""
|
2013-06-04 05:55:20 +00:00
|
|
|
file_like_object = urlopen(self.live_server_url + "/")
|
2012-04-05 15:44:04 +00:00
|
|
|
f = File(file_like_object)
|
|
|
|
stored_filename = self.storage.save("remote_file.html", f)
|
|
|
|
|
2013-06-04 05:55:20 +00:00
|
|
|
remote_file = urlopen(self.live_server_url + "/")
|
2013-02-24 12:10:42 +00:00
|
|
|
with self.storage.open(stored_filename) as stored_file:
|
|
|
|
self.assertEqual(stored_file.read(), remote_file.read())
|