| 1 |
import os |
| 2 |
import shutil |
| 3 |
import sys |
| 4 |
import tempfile |
| 5 |
import threading |
| 6 |
import time |
| 7 |
import unittest |
| 8 |
from datetime import datetime, timedelta |
| 9 |
from datetime import timezone as datetime_timezone |
| 10 |
from io import StringIO |
| 11 |
from pathlib import Path |
| 12 |
from urllib.request import urlopen |
| 13 |
|
| 14 |
from django.conf import DEFAULT_STORAGE_ALIAS, STATICFILES_STORAGE_ALIAS |
| 15 |
from django.core.cache import cache |
| 16 |
from django.core.exceptions import SuspiciousFileOperation |
| 17 |
from django.core.files.base import ContentFile, File |
| 18 |
from django.core.files.storage import FileSystemStorage, InvalidStorageError |
| 19 |
from django.core.files.storage import Storage as BaseStorage |
| 20 |
from django.core.files.storage import StorageHandler, default_storage, storages |
| 21 |
from django.core.files.uploadedfile import ( |
| 22 |
InMemoryUploadedFile, |
| 23 |
SimpleUploadedFile, |
| 24 |
TemporaryUploadedFile, |
| 25 |
) |
| 26 |
from django.db.models import FileField |
| 27 |
from django.db.models.fields.files import FileDescriptor |
| 28 |
from django.test import ( |
| 29 |
LiveServerTestCase, |
| 30 |
SimpleTestCase, |
| 31 |
TestCase, |
| 32 |
ignore_warnings, |
| 33 |
override_settings, |
| 34 |
) |
| 35 |
from django.test.utils import requires_tz_support |
| 36 |
from django.urls import NoReverseMatch, reverse_lazy |
| 37 |
from django.utils import timezone |
| 38 |
from django.utils._os import symlinks_supported |
| 39 |
from django.utils.deprecation import RemovedInDjango60Warning |
| 40 |
|
| 41 |
from .models import ( |
| 42 |
Storage, |
| 43 |
callable_default_storage, |
| 44 |
callable_storage, |
| 45 |
temp_storage, |
| 46 |
temp_storage_location, |
| 47 |
) |
| 48 |
|
| 49 |
FILE_SUFFIX_REGEX = "[A-Za-z0-9]{7}" |
| 50 |
|
| 51 |
|
| 52 |
class FileSystemStorageTests(unittest.TestCase): |
| 53 |
def test_deconstruction(self): |
| 54 |
path, args, kwargs = temp_storage.deconstruct() |
| 55 |
self.assertEqual(path, "django.core.files.storage.FileSystemStorage") |
| 56 |
self.assertEqual(args, ()) |
| 57 |
self.assertEqual(kwargs, {"location": temp_storage_location}) |
| 58 |
|
| 59 |
kwargs_orig = { |
| 60 |
"location": temp_storage_location, |
| 61 |
"base_url": "http://myfiles.example.com/", |
| 62 |
} |
| 63 |
storage = FileSystemStorage(**kwargs_orig) |
| 64 |
path, args, kwargs = storage.deconstruct() |
| 65 |
self.assertEqual(kwargs, kwargs_orig) |
| 66 |
|
| 67 |
def test_lazy_base_url_init(self): |
| 68 |
""" |
| 69 |
FileSystemStorage.__init__() shouldn't evaluate base_url. |
| 70 |
""" |
| 71 |
storage = FileSystemStorage(base_url=reverse_lazy("app:url")) |
| 72 |
with self.assertRaises(NoReverseMatch): |
| 73 |
storage.url(storage.base_url) |
| 74 |
|
| 75 |
|
| 76 |
class FileStorageTests(SimpleTestCase): |
| 77 |
storage_class = FileSystemStorage |
| 78 |
|
| 79 |
def setUp(self): |
| 80 |
self.temp_dir = tempfile.mkdtemp() |
| 81 |
self.addCleanup(shutil.rmtree, self.temp_dir) |
| 82 |
self.storage = self.storage_class( |
| 83 |
location=self.temp_dir, base_url="/test_media_url/" |
| 84 |
) |
| 85 |
|
| 86 |
def test_empty_location(self): |
| 87 |
""" |
| 88 |
Makes sure an exception is raised if the location is empty |
| 89 |
""" |
| 90 |
storage = self.storage_class(location="") |
| 91 |
self.assertEqual(storage.base_location, "") |
| 92 |
self.assertEqual(storage.location, os.getcwd()) |
| 93 |
|
| 94 |
def test_file_access_options(self): |
| 95 |
""" |
| 96 |
Standard file access options are available, and work as expected. |
| 97 |
""" |
| 98 |
self.assertFalse(self.storage.exists("storage_test")) |
| 99 |
f = self.storage.open("storage_test", "w") |
| 100 |
f.write("storage contents") |
| 101 |
f.close() |
| 102 |
self.assertTrue(self.storage.exists("storage_test")) |
| 103 |
|
| 104 |
f = self.storage.open("storage_test", "r") |
| 105 |
self.assertEqual(f.read(), "storage contents") |
| 106 |
f.close() |
| 107 |
|
| 108 |
self.storage.delete("storage_test") |
| 109 |
self.assertFalse(self.storage.exists("storage_test")) |
| 110 |
|
| 111 |
def _test_file_time_getter(self, getter): |
| 112 |
# Check for correct behavior under both USE_TZ=True and USE_TZ=False. |
| 113 |
# The tests are similar since they both set up a situation where the |
| 114 |
# system time zone, Django's TIME_ZONE, and UTC are distinct. |
| 115 |
self._test_file_time_getter_tz_handling_on(getter) |
| 116 |
self._test_file_time_getter_tz_handling_off(getter) |
| 117 |
|
| 118 |
@override_settings(USE_TZ=True, TIME_ZONE="Africa/Algiers") |
| 119 |
def _test_file_time_getter_tz_handling_on(self, getter): |
| 120 |
# Django's TZ (and hence the system TZ) is set to Africa/Algiers which |
| 121 |
# is UTC+1 and has no DST change. We can set the Django TZ to something |
| 122 |
# else so that UTC, Django's TIME_ZONE, and the system timezone are all |
| 123 |
# different. |
| 124 |
now_in_algiers = timezone.make_aware(datetime.now()) |
| 125 |
|
| 126 |
with timezone.override(timezone.get_fixed_timezone(-300)): |
| 127 |
# At this point the system TZ is +1 and the Django TZ |
| 128 |
# is -5. The following will be aware in UTC. |
| 129 |
now = timezone.now() |
| 130 |
self.assertFalse(self.storage.exists("test.file.tz.on")) |
| 131 |
|
| 132 |
f = ContentFile("custom contents") |
| 133 |
f_name = self.storage.save("test.file.tz.on", f) |
| 134 |
self.addCleanup(self.storage.delete, f_name) |
| 135 |
dt = getter(f_name) |
| 136 |
# dt should be aware, in UTC |
| 137 |
self.assertTrue(timezone.is_aware(dt)) |
| 138 |
self.assertEqual(now.tzname(), dt.tzname()) |
| 139 |
|
| 140 |
# The three timezones are indeed distinct. |
| 141 |
naive_now = datetime.now() |
| 142 |
algiers_offset = now_in_algiers.tzinfo.utcoffset(naive_now) |
| 143 |
django_offset = timezone.get_current_timezone().utcoffset(naive_now) |
| 144 |
utc_offset = datetime_timezone.utc.utcoffset(naive_now) |
| 145 |
self.assertGreater(algiers_offset, utc_offset) |
| 146 |
self.assertLess(django_offset, utc_offset) |
| 147 |
|
| 148 |
# dt and now should be the same effective time. |
| 149 |
self.assertLess(abs(dt - now), timedelta(seconds=2)) |
| 150 |
|
| 151 |
@override_settings(USE_TZ=False, TIME_ZONE="Africa/Algiers") |
| 152 |
def _test_file_time_getter_tz_handling_off(self, getter): |
| 153 |
# Django's TZ (and hence the system TZ) is set to Africa/Algiers which |
| 154 |
# is UTC+1 and has no DST change. We can set the Django TZ to something |
| 155 |
# else so that UTC, Django's TIME_ZONE, and the system timezone are all |
| 156 |
# different. |
| 157 |
now_in_algiers = timezone.make_aware(datetime.now()) |
| 158 |
|
| 159 |
with timezone.override(timezone.get_fixed_timezone(-300)): |
| 160 |
# At this point the system TZ is +1 and the Django TZ |
| 161 |
# is -5. |
| 162 |
self.assertFalse(self.storage.exists("test.file.tz.off")) |
| 163 |
|
| 164 |
f = ContentFile("custom contents") |
| 165 |
f_name = self.storage.save("test.file.tz.off", f) |
| 166 |
self.addCleanup(self.storage.delete, f_name) |
| 167 |
dt = getter(f_name) |
| 168 |
# dt should be naive, in system (+1) TZ |
| 169 |
self.assertTrue(timezone.is_naive(dt)) |
| 170 |
|
| 171 |
# The three timezones are indeed distinct. |
| 172 |
naive_now = datetime.now() |
| 173 |
algiers_offset = now_in_algiers.tzinfo.utcoffset(naive_now) |
| 174 |
django_offset = timezone.get_current_timezone().utcoffset(naive_now) |
| 175 |
utc_offset = datetime_timezone.utc.utcoffset(naive_now) |
| 176 |
self.assertGreater(algiers_offset, utc_offset) |
| 177 |
self.assertLess(django_offset, utc_offset) |
| 178 |
|
| 179 |
# dt and naive_now should be the same effective time. |
| 180 |
self.assertLess(abs(dt - naive_now), timedelta(seconds=2)) |
| 181 |
# If we convert dt to an aware object using the Algiers |
| 182 |
# timezone then it should be the same effective time to |
| 183 |
# now_in_algiers. |
| 184 |
_dt = timezone.make_aware(dt, now_in_algiers.tzinfo) |
| 185 |
self.assertLess(abs(_dt - now_in_algiers), timedelta(seconds=2)) |
| 186 |
|
| 187 |
def test_file_get_accessed_time(self): |
| 188 |
""" |
| 189 |
File storage returns a Datetime object for the last accessed time of |
| 190 |
a file. |
| 191 |
""" |
| 192 |
self.assertFalse(self.storage.exists("test.file")) |
| 193 |
|
| 194 |
f = ContentFile("custom contents") |
| 195 |
f_name = self.storage.save("test.file", f) |
| 196 |
self.addCleanup(self.storage.delete, f_name) |
| 197 |
atime = self.storage.get_accessed_time(f_name) |
| 198 |
|
| 199 |
self.assertEqual( |
| 200 |
atime, datetime.fromtimestamp(os.path.getatime(self.storage.path(f_name))) |
| 201 |
) |
| 202 |
self.assertLess( |
| 203 |
timezone.now() - self.storage.get_accessed_time(f_name), |
| 204 |
timedelta(seconds=2), |
| 205 |
) |
| 206 |
|
| 207 |
@requires_tz_support |
| 208 |
def test_file_get_accessed_time_timezone(self): |
| 209 |
self._test_file_time_getter(self.storage.get_accessed_time) |
| 210 |
|
| 211 |
def test_file_get_created_time(self): |
| 212 |
""" |
| 213 |
File storage returns a datetime for the creation time of a file. |
| 214 |
""" |
| 215 |
self.assertFalse(self.storage.exists("test.file")) |
| 216 |
|
| 217 |
f = ContentFile("custom contents") |
| 218 |
f_name = self.storage.save("test.file", f) |
| 219 |
self.addCleanup(self.storage.delete, f_name) |
| 220 |
ctime = self.storage.get_created_time(f_name) |
| 221 |
|
| 222 |
self.assertEqual( |
| 223 |
ctime, datetime.fromtimestamp(os.path.getctime(self.storage.path(f_name))) |
| 224 |
) |
| 225 |
self.assertLess( |
| 226 |
timezone.now() - self.storage.get_created_time(f_name), timedelta(seconds=2) |
| 227 |
) |
| 228 |
|
| 229 |
@requires_tz_support |
| 230 |
def test_file_get_created_time_timezone(self): |
| 231 |
self._test_file_time_getter(self.storage.get_created_time) |
| 232 |
|
| 233 |
def test_file_get_modified_time(self): |
| 234 |
""" |
| 235 |
File storage returns a datetime for the last modified time of a file. |
| 236 |
""" |
| 237 |
self.assertFalse(self.storage.exists("test.file")) |
| 238 |
|
| 239 |
f = ContentFile("custom contents") |
| 240 |
f_name = self.storage.save("test.file", f) |
| 241 |
self.addCleanup(self.storage.delete, f_name) |
| 242 |
mtime = self.storage.get_modified_time(f_name) |
| 243 |
|
| 244 |
self.assertEqual( |
| 245 |
mtime, datetime.fromtimestamp(os.path.getmtime(self.storage.path(f_name))) |
| 246 |
) |
| 247 |
self.assertLess( |
| 248 |
timezone.now() - self.storage.get_modified_time(f_name), |
| 249 |
timedelta(seconds=2), |
| 250 |
) |
| 251 |
|
| 252 |
@requires_tz_support |
| 253 |
def test_file_get_modified_time_timezone(self): |
| 254 |
self._test_file_time_getter(self.storage.get_modified_time) |
| 255 |
|
| 256 |
def test_file_save_without_name(self): |
| 257 |
""" |
| 258 |
File storage extracts the filename from the content object if no |
| 259 |
name is given explicitly. |
| 260 |
""" |
| 261 |
self.assertFalse(self.storage.exists("test.file")) |
| 262 |
|
| 263 |
f = ContentFile("custom contents") |
| 264 |
f.name = "test.file" |
| 265 |
|
| 266 |
storage_f_name = self.storage.save(None, f) |
| 267 |
|
| 268 |
self.assertEqual(storage_f_name, f.name) |
| 269 |
|
| 270 |
self.assertTrue(os.path.exists(os.path.join(self.temp_dir, f.name))) |
| 271 |
|
| 272 |
self.storage.delete(storage_f_name) |
| 273 |
|
| 274 |
def test_file_save_with_path(self): |
| 275 |
""" |
| 276 |
Saving a pathname should create intermediate directories as necessary. |
| 277 |
""" |
| 278 |
self.assertFalse(self.storage.exists("path/to")) |
| 279 |
self.storage.save("path/to/test.file", ContentFile("file saved with path")) |
| 280 |
|
| 281 |
self.assertTrue(self.storage.exists("path/to")) |
| 282 |
with self.storage.open("path/to/test.file") as f: |
| 283 |
self.assertEqual(f.read(), b"file saved with path") |
| 284 |
|
| 285 |
self.assertTrue( |
| 286 |
os.path.exists(os.path.join(self.temp_dir, "path", "to", "test.file")) |
| 287 |
) |
| 288 |
|
| 289 |
self.storage.delete("path/to/test.file") |
| 290 |
|
| 291 |
@unittest.skipUnless( |
| 292 |
symlinks_supported(), "Must be able to symlink to run this test." |
| 293 |
) |
| 294 |
def test_file_save_broken_symlink(self): |
| 295 |
"""A new path is created on save when a broken symlink is supplied.""" |
| 296 |
nonexistent_file_path = os.path.join(self.temp_dir, "nonexistent.txt") |
| 297 |
broken_symlink_file_name = "symlink.txt" |
| 298 |
broken_symlink_path = os.path.join(self.temp_dir, broken_symlink_file_name) |
| 299 |
os.symlink(nonexistent_file_path, broken_symlink_path) |
| 300 |
f = ContentFile("some content") |
| 301 |
f_name = self.storage.save(broken_symlink_file_name, f) |
| 302 |
self.assertIs(os.path.exists(os.path.join(self.temp_dir, f_name)), True) |
| 303 |
|
| 304 |
def test_save_doesnt_close(self): |
| 305 |
with TemporaryUploadedFile("test", "text/plain", 1, "utf8") as file: |
| 306 |
file.write(b"1") |
| 307 |
file.seek(0) |
| 308 |
self.assertFalse(file.closed) |
| 309 |
self.storage.save("path/to/test.file", file) |
| 310 |
self.assertFalse(file.closed) |
| 311 |
self.assertFalse(file.file.closed) |
| 312 |
|
| 313 |
file = InMemoryUploadedFile(StringIO("1"), "", "test", "text/plain", 1, "utf8") |
| 314 |
with file: |
| 315 |
self.assertFalse(file.closed) |
| 316 |
self.storage.save("path/to/test.file", file) |
| 317 |
self.assertFalse(file.closed) |
| 318 |
self.assertFalse(file.file.closed) |
| 319 |
|
| 320 |
def test_file_path(self): |
| 321 |
""" |
| 322 |
File storage returns the full path of a file |
| 323 |
""" |
| 324 |
self.assertFalse(self.storage.exists("test.file")) |
| 325 |
|
| 326 |
f = ContentFile("custom contents") |
| 327 |
f_name = self.storage.save("test.file", f) |
| 328 |
|
| 329 |
self.assertEqual(self.storage.path(f_name), os.path.join(self.temp_dir, f_name)) |
| 330 |
|
| 331 |
self.storage.delete(f_name) |
| 332 |
|
| 333 |
def test_file_url(self): |
| 334 |
""" |
| 335 |
File storage returns a url to access a given file from the web. |
| 336 |
""" |
| 337 |
self.assertEqual( |
| 338 |
self.storage.url("test.file"), self.storage.base_url + "test.file" |
| 339 |
) |
| 340 |
|
| 341 |
# should encode special chars except ~!*()' |
| 342 |
# like encodeURIComponent() JavaScript function do |
| 343 |
self.assertEqual( |
| 344 |
self.storage.url(r"~!*()'@#$%^&*abc`+ =.file"), |
| 345 |
"/test_media_url/~!*()'%40%23%24%25%5E%26*abc%60%2B%20%3D.file", |
| 346 |
) |
| 347 |
self.assertEqual(self.storage.url("ab\0c"), "/test_media_url/ab%00c") |
| 348 |
|
| 349 |
# should translate os path separator(s) to the url path separator |
| 350 |
self.assertEqual( |
| 351 |
self.storage.url("""a/b\\c.file"""), "/test_media_url/a/b/c.file" |
| 352 |
) |
| 353 |
|
| 354 |
# #25905: remove leading slashes from file names to prevent unsafe url output |
| 355 |
self.assertEqual(self.storage.url("/evil.com"), "/test_media_url/evil.com") |
| 356 |
self.assertEqual(self.storage.url(r"\evil.com"), "/test_media_url/evil.com") |
| 357 |
self.assertEqual(self.storage.url("///evil.com"), "/test_media_url/evil.com") |
| 358 |
self.assertEqual(self.storage.url(r"\\\evil.com"), "/test_media_url/evil.com") |
| 359 |
|
| 360 |
self.assertEqual(self.storage.url(None), "/test_media_url/") |
| 361 |
|
| 362 |
def test_base_url(self): |
| 363 |
""" |
| 364 |
File storage returns a url even when its base_url is unset or modified. |
| 365 |
""" |
| 366 |
self.storage.base_url = None |
| 367 |
with self.assertRaises(ValueError): |
| 368 |
self.storage.url("test.file") |
| 369 |
|
| 370 |
# #22717: missing ending slash in base_url should be auto-corrected |
| 371 |
storage = self.storage_class( |
| 372 |
location=self.temp_dir, base_url="/no_ending_slash" |
| 373 |
) |
| 374 |
self.assertEqual( |
| 375 |
storage.url("test.file"), "%s%s" % (storage.base_url, "test.file") |
| 376 |
) |
| 377 |
|
| 378 |
def test_listdir(self): |
| 379 |
""" |
| 380 |
File storage returns a tuple containing directories and files. |
| 381 |
""" |
| 382 |
self.assertFalse(self.storage.exists("storage_test_1")) |
| 383 |
self.assertFalse(self.storage.exists("storage_test_2")) |
| 384 |
self.assertFalse(self.storage.exists("storage_dir_1")) |
| 385 |
|
| 386 |
self.storage.save("storage_test_1", ContentFile("custom content")) |
| 387 |
self.storage.save("storage_test_2", ContentFile("custom content")) |
| 388 |
os.mkdir(os.path.join(self.temp_dir, "storage_dir_1")) |
| 389 |
|
| 390 |
self.addCleanup(self.storage.delete, "storage_test_1") |
| 391 |
self.addCleanup(self.storage.delete, "storage_test_2") |
| 392 |
|
| 393 |
for directory in ("", Path("")): |
| 394 |
with self.subTest(directory=directory): |
| 395 |
dirs, files = self.storage.listdir(directory) |
| 396 |
self.assertEqual(set(dirs), {"storage_dir_1"}) |
| 397 |
self.assertEqual(set(files), {"storage_test_1", "storage_test_2"}) |
| 398 |
|
| 399 |
def test_file_storage_prevents_directory_traversal(self): |
| 400 |
""" |
| 401 |
File storage prevents directory traversal (files can only be accessed if |
| 402 |
they're below the storage location). |
| 403 |
""" |
| 404 |
with self.assertRaises(SuspiciousFileOperation): |
| 405 |
self.storage.exists("..") |
| 406 |
with self.assertRaises(SuspiciousFileOperation): |
| 407 |
self.storage.exists("/etc/passwd") |
| 408 |
|
| 409 |
def test_file_storage_preserves_filename_case(self): |
| 410 |
"""The storage backend should preserve case of filenames.""" |
| 411 |
# Create a storage backend associated with the mixed case name |
| 412 |
# directory. |
| 413 |
temp_dir2 = tempfile.mkdtemp(suffix="aBc") |
| 414 |
self.addCleanup(shutil.rmtree, temp_dir2) |
| 415 |
other_temp_storage = self.storage_class(location=temp_dir2) |
| 416 |
# Ask that storage backend to store a file with a mixed case filename. |
| 417 |
mixed_case = "CaSe_SeNsItIvE" |
| 418 |
file = other_temp_storage.open(mixed_case, "w") |
| 419 |
file.write("storage contents") |
| 420 |
file.close() |
| 421 |
self.assertEqual( |
| 422 |
os.path.join(temp_dir2, mixed_case), |
| 423 |
other_temp_storage.path(mixed_case), |
| 424 |
) |
| 425 |
other_temp_storage.delete(mixed_case) |
| 426 |
|
| 427 |
def test_makedirs_race_handling(self): |
| 428 |
""" |
| 429 |
File storage should be robust against directory creation race conditions. |
| 430 |
""" |
| 431 |
real_makedirs = os.makedirs |
| 432 |
|
| 433 |
# Monkey-patch os.makedirs, to simulate a normal call, a raced call, |
| 434 |
# and an error. |
| 435 |
def fake_makedirs(path, mode=0o777, exist_ok=False): |
| 436 |
if path == os.path.join(self.temp_dir, "normal"): |
| 437 |
real_makedirs(path, mode, exist_ok) |
| 438 |
elif path == os.path.join(self.temp_dir, "raced"): |
| 439 |
real_makedirs(path, mode, exist_ok) |
| 440 |
if not exist_ok: |
| 441 |
raise FileExistsError() |
| 442 |
elif path == os.path.join(self.temp_dir, "error"): |
| 443 |
raise PermissionError() |
| 444 |
else: |
| 445 |
self.fail("unexpected argument %r" % path) |
| 446 |
|
| 447 |
try: |
| 448 |
os.makedirs = fake_makedirs |
| 449 |
|
| 450 |
self.storage.save("normal/test.file", ContentFile("saved normally")) |
| 451 |
with self.storage.open("normal/test.file") as f: |
| 452 |
self.assertEqual(f.read(), b"saved normally") |
| 453 |
|
| 454 |
self.storage.save("raced/test.file", ContentFile("saved with race")) |
| 455 |
with self.storage.open("raced/test.file") as f: |
| 456 |
self.assertEqual(f.read(), b"saved with race") |
| 457 |
|
| 458 |
# Exceptions aside from FileExistsError are raised. |
| 459 |
with self.assertRaises(PermissionError): |
| 460 |
self.storage.save("error/test.file", ContentFile("not saved")) |
| 461 |
finally: |
| 462 |
os.makedirs = real_makedirs |
| 463 |
|
| 464 |
def test_remove_race_handling(self): |
| 465 |
""" |
| 466 |
File storage should be robust against file removal race conditions. |
| 467 |
""" |
| 468 |
real_remove = os.remove |
| 469 |
|
| 470 |
# Monkey-patch os.remove, to simulate a normal call, a raced call, |
| 471 |
# and an error. |
| 472 |
def fake_remove(path): |
| 473 |
if path == os.path.join(self.temp_dir, "normal.file"): |
| 474 |
real_remove(path) |
| 475 |
elif path == os.path.join(self.temp_dir, "raced.file"): |
| 476 |
real_remove(path) |
| 477 |
raise FileNotFoundError() |
| 478 |
elif path == os.path.join(self.temp_dir, "error.file"): |
| 479 |
raise PermissionError() |
| 480 |
else: |
| 481 |
self.fail("unexpected argument %r" % path) |
| 482 |
|
| 483 |
try: |
| 484 |
os.remove = fake_remove |
| 485 |
|
| 486 |
self.storage.save("normal.file", ContentFile("delete normally")) |
| 487 |
self.storage.delete("normal.file") |
| 488 |
self.assertFalse(self.storage.exists("normal.file")) |
| 489 |
|
| 490 |
self.storage.save("raced.file", ContentFile("delete with race")) |
| 491 |
self.storage.delete("raced.file") |
| 492 |
self.assertFalse(self.storage.exists("normal.file")) |
| 493 |
|
| 494 |
# Exceptions aside from FileNotFoundError are raised. |
| 495 |
self.storage.save("error.file", ContentFile("delete with error")) |
| 496 |
with self.assertRaises(PermissionError): |
| 497 |
self.storage.delete("error.file") |
| 498 |
finally: |
| 499 |
os.remove = real_remove |
| 500 |
|
| 501 |
def test_file_chunks_error(self): |
| 502 |
""" |
| 503 |
Test behavior when file.chunks() is raising an error |
| 504 |
""" |
| 505 |
f1 = ContentFile("chunks fails") |
| 506 |
|
| 507 |
def failing_chunks(): |
| 508 |
raise OSError |
| 509 |
|
| 510 |
f1.chunks = failing_chunks |
| 511 |
with self.assertRaises(OSError): |
| 512 |
self.storage.save("error.file", f1) |
| 513 |
|
| 514 |
def test_delete_no_name(self): |
| 515 |
""" |
| 516 |
Calling delete with an empty name should not try to remove the base |
| 517 |
storage directory, but fail loudly (#20660). |
| 518 |
""" |
| 519 |
msg = "The name must be given to delete()." |
| 520 |
with self.assertRaisesMessage(ValueError, msg): |
| 521 |
self.storage.delete(None) |
| 522 |
with self.assertRaisesMessage(ValueError, msg): |
| 523 |
self.storage.delete("") |
| 524 |
|
| 525 |
def test_delete_deletes_directories(self): |
| 526 |
tmp_dir = tempfile.mkdtemp(dir=self.storage.location) |
| 527 |
self.storage.delete(tmp_dir) |
| 528 |
self.assertFalse(os.path.exists(tmp_dir)) |
| 529 |
|
| 530 |
@override_settings( |
| 531 |
MEDIA_ROOT="media_root", |
| 532 |
MEDIA_URL="media_url/", |
| 533 |
FILE_UPLOAD_PERMISSIONS=0o777, |
| 534 |
FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o777, |
| 535 |
) |
| 536 |
def test_setting_changed(self): |
| 537 |
""" |
| 538 |
Properties using settings values as defaults should be updated on |
| 539 |
referenced settings change while specified values should be unchanged. |
| 540 |
""" |
| 541 |
storage = self.storage_class( |
| 542 |
location="explicit_location", |
| 543 |
base_url="explicit_base_url/", |
| 544 |
file_permissions_mode=0o666, |
| 545 |
directory_permissions_mode=0o666, |
| 546 |
) |
| 547 |
defaults_storage = self.storage_class() |
| 548 |
settings = { |
| 549 |
"MEDIA_ROOT": "overridden_media_root", |
| 550 |
"MEDIA_URL": "/overridden_media_url/", |
| 551 |
"FILE_UPLOAD_PERMISSIONS": 0o333, |
| 552 |
"FILE_UPLOAD_DIRECTORY_PERMISSIONS": 0o333, |
| 553 |
} |
| 554 |
with self.settings(**settings): |
| 555 |
self.assertEqual(storage.base_location, "explicit_location") |
| 556 |
self.assertIn("explicit_location", storage.location) |
| 557 |
self.assertEqual(storage.base_url, "explicit_base_url/") |
| 558 |
self.assertEqual(storage.file_permissions_mode, 0o666) |
| 559 |
self.assertEqual(storage.directory_permissions_mode, 0o666) |
| 560 |
self.assertEqual(defaults_storage.base_location, settings["MEDIA_ROOT"]) |
| 561 |
self.assertIn(settings["MEDIA_ROOT"], defaults_storage.location) |
| 562 |
self.assertEqual(defaults_storage.base_url, settings["MEDIA_URL"]) |
| 563 |
self.assertEqual( |
| 564 |
defaults_storage.file_permissions_mode, |
| 565 |
settings["FILE_UPLOAD_PERMISSIONS"], |
| 566 |
) |
| 567 |
self.assertEqual( |
| 568 |
defaults_storage.directory_permissions_mode, |
| 569 |
settings["FILE_UPLOAD_DIRECTORY_PERMISSIONS"], |
| 570 |
) |
| 571 |
|
| 572 |
def test_file_methods_pathlib_path(self): |
| 573 |
p = Path("test.file") |
| 574 |
self.assertFalse(self.storage.exists(p)) |
| 575 |
f = ContentFile("custom contents") |
| 576 |
f_name = self.storage.save(p, f) |
| 577 |
# Storage basic methods. |
| 578 |
self.assertEqual(self.storage.path(p), os.path.join(self.temp_dir, p)) |
| 579 |
self.assertEqual(self.storage.size(p), 15) |
| 580 |
self.assertEqual(self.storage.url(p), self.storage.base_url + f_name) |
| 581 |
with self.storage.open(p) as f: |
| 582 |
self.assertEqual(f.read(), b"custom contents") |
| 583 |
self.addCleanup(self.storage.delete, p) |
| 584 |
|
| 585 |
|
| 586 |
class CustomStorage(FileSystemStorage): |
| 587 |
def get_available_name(self, name, max_length=None): |
| 588 |
""" |
| 589 |
Append numbers to duplicate files rather than underscores, like Trac. |
| 590 |
""" |
| 591 |
basename, *ext = os.path.splitext(name) |
| 592 |
number = 2 |
| 593 |
while self.exists(name): |
| 594 |
name = "".join([basename, ".", str(number)] + ext) |
| 595 |
number += 1 |
| 596 |
|
| 597 |
return name |
| 598 |
|
| 599 |
|
| 600 |
class CustomStorageTests(FileStorageTests): |
| 601 |
storage_class = CustomStorage |
| 602 |
|
| 603 |
def test_custom_get_available_name(self): |
| 604 |
first = self.storage.save("custom_storage", ContentFile("custom contents")) |
| 605 |
self.assertEqual(first, "custom_storage") |
| 606 |
second = self.storage.save("custom_storage", ContentFile("more contents")) |
| 607 |
self.assertEqual(second, "custom_storage.2") |
| 608 |
self.storage.delete(first) |
| 609 |
self.storage.delete(second) |
| 610 |
|
| 611 |
|
| 612 |
# RemovedInDjango60Warning: Remove this class. |
| 613 |
class OverwritingStorage(FileSystemStorage): |
| 614 |
""" |
| 615 |
Overwrite existing files instead of appending a suffix to generate an |
| 616 |
unused name. |
| 617 |
""" |
| 618 |
|
| 619 |
# Mask out O_EXCL so os.open() doesn't raise OSError if the file exists. |
| 620 |
OS_OPEN_FLAGS = FileSystemStorage.OS_OPEN_FLAGS & ~os.O_EXCL |
| 621 |
|
| 622 |
def get_available_name(self, name, max_length=None): |
| 623 |
"""Override the effort to find an used name.""" |
| 624 |
return name |
| 625 |
|
| 626 |
|
| 627 |
# RemovedInDjango60Warning: Remove this test class. |
| 628 |
class OverwritingStorageOSOpenFlagsWarningTests(SimpleTestCase): |
| 629 |
storage_class = OverwritingStorage |
| 630 |
|
| 631 |
def setUp(self): |
| 632 |
self.temp_dir = tempfile.mkdtemp() |
| 633 |
self.addCleanup(shutil.rmtree, self.temp_dir) |
| 634 |
|
| 635 |
def test_os_open_flags_deprecation_warning(self): |
| 636 |
msg = "Overriding OS_OPEN_FLAGS is deprecated. Use the allow_overwrite " |
| 637 |
msg += "parameter instead." |
| 638 |
with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx: |
| 639 |
self.storage = self.storage_class( |
| 640 |
location=self.temp_dir, base_url="/test_media_url/" |
| 641 |
) |
| 642 |
self.assertEqual(ctx.filename, __file__) |
| 643 |
|
| 644 |
|
| 645 |
# RemovedInDjango60Warning: Remove this test class. |
| 646 |
@ignore_warnings(category=RemovedInDjango60Warning) |
| 647 |
class OverwritingStorageOSOpenFlagsTests(FileStorageTests): |
| 648 |
storage_class = OverwritingStorage |
| 649 |
|
| 650 |
def test_save_overwrite_behavior(self): |
| 651 |
"""Saving to same file name twice overwrites the first file.""" |
| 652 |
name = "test.file" |
| 653 |
self.assertFalse(self.storage.exists(name)) |
| 654 |
content_1 = b"content one" |
| 655 |
content_2 = b"second content" |
| 656 |
f_1 = ContentFile(content_1) |
| 657 |
f_2 = ContentFile(content_2) |
| 658 |
stored_name_1 = self.storage.save(name, f_1) |
| 659 |
try: |
| 660 |
self.assertEqual(stored_name_1, name) |
| 661 |
self.assertTrue(self.storage.exists(name)) |
| 662 |
self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name))) |
| 663 |
with self.storage.open(name) as fp: |
| 664 |
self.assertEqual(fp.read(), content_1) |
| 665 |
stored_name_2 = self.storage.save(name, f_2) |
| 666 |
self.assertEqual(stored_name_2, name) |
| 667 |
self.assertTrue(self.storage.exists(name)) |
| 668 |
self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name))) |
| 669 |
with self.storage.open(name) as fp: |
| 670 |
self.assertEqual(fp.read(), content_2) |
| 671 |
finally: |
| 672 |
self.storage.delete(name) |
| 673 |
|
| 674 |
|
| 675 |
class OverwritingStorageTests(FileStorageTests): |
| 676 |
storage_class = FileSystemStorage |
| 677 |
|
| 678 |
def setUp(self): |
| 679 |
self.temp_dir = tempfile.mkdtemp() |
| 680 |
self.addCleanup(shutil.rmtree, self.temp_dir) |
| 681 |
self.storage = self.storage_class( |
| 682 |
location=self.temp_dir, base_url="/test_media_url/", allow_overwrite=True |
| 683 |
) |
| 684 |
|
| 685 |
def test_save_overwrite_behavior(self): |
| 686 |
"""Saving to same file name twice overwrites the first file.""" |
| 687 |
name = "test.file" |
| 688 |
self.assertFalse(self.storage.exists(name)) |
| 689 |
content_1 = b"content one" |
| 690 |
content_2 = b"second content" |
| 691 |
f_1 = ContentFile(content_1) |
| 692 |
f_2 = ContentFile(content_2) |
| 693 |
stored_name_1 = self.storage.save(name, f_1) |
| 694 |
try: |
| 695 |
self.assertEqual(stored_name_1, name) |
| 696 |
self.assertTrue(self.storage.exists(name)) |
| 697 |
with self.storage.open(name) as fp: |
| 698 |
self.assertEqual(fp.read(), content_1) |
| 699 |
stored_name_2 = self.storage.save(name, f_2) |
| 700 |
self.assertEqual(stored_name_2, name) |
| 701 |
self.assertTrue(self.storage.exists(name)) |
| 702 |
with self.storage.open(name) as fp: |
| 703 |
self.assertEqual(fp.read(), content_2) |
| 704 |
finally: |
| 705 |
self.storage.delete(name) |
| 706 |
|
| 707 |
def test_save_overwrite_behavior_truncate(self): |
| 708 |
name = "test.file" |
| 709 |
original_content = b"content extra extra extra" |
| 710 |
new_smaller_content = b"content" |
| 711 |
self.storage.save(name, ContentFile(original_content)) |
| 712 |
try: |
| 713 |
self.storage.save(name, ContentFile(new_smaller_content)) |
| 714 |
with self.storage.open(name) as fp: |
| 715 |
self.assertEqual(fp.read(), new_smaller_content) |
| 716 |
finally: |
| 717 |
self.storage.delete(name) |
| 718 |
|
| 719 |
def test_save_overwrite_behavior_temp_file(self): |
| 720 |
"""Saving to same file name twice overwrites the first file.""" |
| 721 |
name = "test.file" |
| 722 |
self.assertFalse(self.storage.exists(name)) |
| 723 |
content_1 = b"content one" |
| 724 |
content_2 = b"second content" |
| 725 |
f_1 = TemporaryUploadedFile("tmp1", "text/plain", 11, "utf8") |
| 726 |
self.addCleanup(f_1.close) |
| 727 |
f_1.write(content_1) |
| 728 |
f_1.seek(0) |
| 729 |
f_2 = TemporaryUploadedFile("tmp2", "text/plain", 14, "utf8") |
| 730 |
self.addCleanup(f_2.close) |
| 731 |
f_2.write(content_2) |
| 732 |
f_2.seek(0) |
| 733 |
stored_name_1 = self.storage.save(name, f_1) |
| 734 |
try: |
| 735 |
self.assertEqual(stored_name_1, name) |
| 736 |
self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name))) |
| 737 |
with self.storage.open(name) as fp: |
| 738 |
self.assertEqual(fp.read(), content_1) |
| 739 |
stored_name_2 = self.storage.save(name, f_2) |
| 740 |
self.assertEqual(stored_name_2, name) |
| 741 |
self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name))) |
| 742 |
with self.storage.open(name) as fp: |
| 743 |
self.assertEqual(fp.read(), content_2) |
| 744 |
finally: |
| 745 |
self.storage.delete(name) |
| 746 |
|
| 747 |
def test_file_name_truncation(self): |
| 748 |
name = "test_long_file_name.txt" |
| 749 |
file = ContentFile(b"content") |
| 750 |
stored_name = self.storage.save(name, file, max_length=10) |
| 751 |
self.addCleanup(self.storage.delete, stored_name) |
| 752 |
self.assertEqual(stored_name, "test_l.txt") |
| 753 |
self.assertEqual(len(stored_name), 10) |
| 754 |
|
| 755 |
def test_file_name_truncation_extension_too_long(self): |
| 756 |
name = "file_name.longext" |
| 757 |
file = ContentFile(b"content") |
| 758 |
with self.assertRaisesMessage( |
| 759 |
SuspiciousFileOperation, "Storage can not find an available filename" |
| 760 |
): |
| 761 |
self.storage.save(name, file, max_length=5) |
| 762 |
|
| 763 |
|
| 764 |
class DiscardingFalseContentStorage(FileSystemStorage): |
| 765 |
def _save(self, name, content): |
| 766 |
if content: |
| 767 |
return super()._save(name, content) |
| 768 |
return "" |
| 769 |
|
| 770 |
|
| 771 |
class DiscardingFalseContentStorageTests(FileStorageTests): |
| 772 |
storage_class = DiscardingFalseContentStorage |
| 773 |
|
| 774 |
def test_custom_storage_discarding_empty_content(self): |
| 775 |
""" |
| 776 |
When Storage.save() wraps a file-like object in File, it should include |
| 777 |
the name argument so that bool(file) evaluates to True (#26495). |
| 778 |
""" |
| 779 |
output = StringIO("content") |
| 780 |
self.storage.save("tests/stringio", output) |
| 781 |
self.assertTrue(self.storage.exists("tests/stringio")) |
| 782 |
|
| 783 |
with self.storage.open("tests/stringio") as f: |
| 784 |
self.assertEqual(f.read(), b"content") |
| 785 |
|
| 786 |
|
| 787 |
class FileFieldStorageTests(TestCase): |
| 788 |
def tearDown(self): |
| 789 |
if os.path.exists(temp_storage_location): |
| 790 |
shutil.rmtree(temp_storage_location) |
| 791 |
|
| 792 |
def _storage_max_filename_length(self, storage): |
| 793 |
""" |
| 794 |
Query filesystem for maximum filename length (e.g. AUFS has 242). |
| 795 |
""" |
| 796 |
dir_to_test = storage.location |
| 797 |
while not os.path.exists(dir_to_test): |
| 798 |
dir_to_test = os.path.dirname(dir_to_test) |
| 799 |
try: |
| 800 |
return os.pathconf(dir_to_test, "PC_NAME_MAX") |
| 801 |
except Exception: |
| 802 |
return 255 # Should be safe on most backends |
| 803 |
|
| 804 |
def test_files(self): |
| 805 |
self.assertIsInstance(Storage.normal, FileDescriptor) |
| 806 |
|
| 807 |
# An object without a file has limited functionality. |
| 808 |
obj1 = Storage() |
| 809 |
self.assertEqual(obj1.normal.name, "") |
| 810 |
with self.assertRaises(ValueError): |
| 811 |
obj1.normal.size |
| 812 |
|
| 813 |
# Saving a file enables full functionality. |
| 814 |
obj1.normal.save("django_test.txt", ContentFile("content")) |
| 815 |
self.assertEqual(obj1.normal.name, "tests/django_test.txt") |
| 816 |
self.assertEqual(obj1.normal.size, 7) |
| 817 |
self.assertEqual(obj1.normal.read(), b"content") |
| 818 |
obj1.normal.close() |
| 819 |
|
| 820 |
# File objects can be assigned to FileField attributes, but shouldn't |
| 821 |
# get committed until the model it's attached to is saved. |
| 822 |
obj1.normal = SimpleUploadedFile("assignment.txt", b"content") |
| 823 |
dirs, files = temp_storage.listdir("tests") |
| 824 |
self.assertEqual(dirs, []) |
| 825 |
self.assertNotIn("assignment.txt", files) |
| 826 |
|
| 827 |
obj1.save() |
| 828 |
dirs, files = temp_storage.listdir("tests") |
| 829 |
self.assertEqual(sorted(files), ["assignment.txt", "django_test.txt"]) |
| 830 |
|
| 831 |
# Save another file with the same name. |
| 832 |
obj2 = Storage() |
| 833 |
obj2.normal.save("django_test.txt", ContentFile("more content")) |
| 834 |
obj2_name = obj2.normal.name |
| 835 |
self.assertRegex(obj2_name, "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX) |
| 836 |
self.assertEqual(obj2.normal.size, 12) |
| 837 |
obj2.normal.close() |
| 838 |
|
| 839 |
# Deleting an object does not delete the file it uses. |
| 840 |
obj2.delete() |
| 841 |
obj2.normal.save("django_test.txt", ContentFile("more content")) |
| 842 |
self.assertNotEqual(obj2_name, obj2.normal.name) |
| 843 |
self.assertRegex( |
| 844 |
obj2.normal.name, "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX |
| 845 |
) |
| 846 |
obj2.normal.close() |
| 847 |
|
| 848 |
def test_filefield_read(self): |
| 849 |
# Files can be read in a little at a time, if necessary. |
| 850 |
obj = Storage.objects.create( |
| 851 |
normal=SimpleUploadedFile("assignment.txt", b"content") |
| 852 |
) |
| 853 |
obj.normal.open() |
| 854 |
self.assertEqual(obj.normal.read(3), b"con") |
| 855 |
self.assertEqual(obj.normal.read(), b"tent") |
| 856 |
self.assertEqual( |
| 857 |
list(obj.normal.chunks(chunk_size=2)), [b"co", b"nt", b"en", b"t"] |
| 858 |
) |
| 859 |
obj.normal.close() |
| 860 |
|
| 861 |
def test_filefield_write(self): |
| 862 |
# Files can be written to. |
| 863 |
obj = Storage.objects.create( |
| 864 |
normal=SimpleUploadedFile("rewritten.txt", b"content") |
| 865 |
) |
| 866 |
with obj.normal as normal: |
| 867 |
normal.open("wb") |
| 868 |
normal.write(b"updated") |
| 869 |
obj.refresh_from_db() |
| 870 |
self.assertEqual(obj.normal.read(), b"updated") |
| 871 |
obj.normal.close() |
| 872 |
|
| 873 |
def test_filefield_reopen(self): |
| 874 |
obj = Storage.objects.create( |
| 875 |
normal=SimpleUploadedFile("reopen.txt", b"content") |
| 876 |
) |
| 877 |
with obj.normal as normal: |
| 878 |
normal.open() |
| 879 |
obj.normal.open() |
| 880 |
obj.normal.file.seek(0) |
| 881 |
obj.normal.close() |
| 882 |
|
| 883 |
def test_duplicate_filename(self): |
| 884 |
# Multiple files with the same name get _(7 random chars) appended to them. |
| 885 |
tests = [ |
| 886 |
("multiple_files", "txt"), |
| 887 |
("multiple_files_many_extensions", "tar.gz"), |
| 888 |
] |
| 889 |
for filename, extension in tests: |
| 890 |
with self.subTest(filename=filename): |
| 891 |
objs = [Storage() for i in range(2)] |
| 892 |
for o in objs: |
| 893 |
o.normal.save(f"{filename}.{extension}", ContentFile("Content")) |
| 894 |
try: |
| 895 |
names = [o.normal.name for o in objs] |
| 896 |
self.assertEqual(names[0], f"tests/{filename}.{extension}") |
| 897 |
self.assertRegex( |
| 898 |
names[1], f"tests/{filename}_{FILE_SUFFIX_REGEX}.{extension}" |
| 899 |
) |
| 900 |
finally: |
| 901 |
for o in objs: |
| 902 |
o.delete() |
| 903 |
|
| 904 |
def test_file_truncation(self): |
| 905 |
# Given the max_length is limited, when multiple files get uploaded |
| 906 |
# under the same name, then the filename get truncated in order to fit |
| 907 |
# in _(7 random chars). When most of the max_length is taken by |
| 908 |
# dirname + extension and there are not enough characters in the |
| 909 |
# filename to truncate, an exception should be raised. |
| 910 |
objs = [Storage() for i in range(2)] |
| 911 |
filename = "filename.ext" |
| 912 |
|
| 913 |
for o in objs: |
| 914 |
o.limited_length.save(filename, ContentFile("Same Content")) |
| 915 |
try: |
| 916 |
# Testing truncation. |
| 917 |
names = [o.limited_length.name for o in objs] |
| 918 |
self.assertEqual(names[0], "tests/%s" % filename) |
| 919 |
self.assertRegex(names[1], "tests/fi_%s.ext" % FILE_SUFFIX_REGEX) |
| 920 |
|
| 921 |
# Testing exception is raised when filename is too short to truncate. |
| 922 |
filename = "short.longext" |
| 923 |
objs[0].limited_length.save(filename, ContentFile("Same Content")) |
| 924 |
with self.assertRaisesMessage( |
| 925 |
SuspiciousFileOperation, "Storage can not find an available filename" |
| 926 |
): |
| 927 |
objs[1].limited_length.save(*(filename, ContentFile("Same Content"))) |
| 928 |
finally: |
| 929 |
for o in objs: |
| 930 |
o.delete() |
| 931 |
|
| 932 |
@unittest.skipIf( |
| 933 |
sys.platform == "win32", |
| 934 |
"Windows supports at most 260 characters in a path.", |
| 935 |
) |
| 936 |
def test_extended_length_storage(self): |
| 937 |
# Testing FileField with max_length > 255. Most systems have filename |
| 938 |
# length limitation of 255. Path takes extra chars. |
| 939 |
filename = ( |
| 940 |
self._storage_max_filename_length(temp_storage) - 4 |
| 941 |
) * "a" # 4 chars for extension. |
| 942 |
obj = Storage() |
| 943 |
obj.extended_length.save("%s.txt" % filename, ContentFile("Same Content")) |
| 944 |
self.assertEqual(obj.extended_length.name, "tests/%s.txt" % filename) |
| 945 |
self.assertEqual(obj.extended_length.read(), b"Same Content") |
| 946 |
obj.extended_length.close() |
| 947 |
|
| 948 |
def test_filefield_default(self): |
| 949 |
# Default values allow an object to access a single file. |
| 950 |
temp_storage.save("tests/default.txt", ContentFile("default content")) |
| 951 |
obj = Storage.objects.create() |
| 952 |
self.assertEqual(obj.default.name, "tests/default.txt") |
| 953 |
self.assertEqual(obj.default.read(), b"default content") |
| 954 |
obj.default.close() |
| 955 |
|
| 956 |
# But it shouldn't be deleted, even if there are no more objects using |
| 957 |
# it. |
| 958 |
obj.delete() |
| 959 |
obj = Storage() |
| 960 |
self.assertEqual(obj.default.read(), b"default content") |
| 961 |
obj.default.close() |
| 962 |
|
| 963 |
def test_filefield_db_default(self): |
| 964 |
temp_storage.save("tests/db_default.txt", ContentFile("default content")) |
| 965 |
obj = Storage.objects.create() |
| 966 |
self.assertEqual(obj.db_default.name, "tests/db_default.txt") |
| 967 |
self.assertEqual(obj.db_default.read(), b"default content") |
| 968 |
obj.db_default.close() |
| 969 |
|
| 970 |
# File is not deleted, even if there are no more objects using it. |
| 971 |
obj.delete() |
| 972 |
s = Storage() |
| 973 |
self.assertEqual(s.db_default.name, "tests/db_default.txt") |
| 974 |
self.assertEqual(s.db_default.read(), b"default content") |
| 975 |
s.db_default.close() |
| 976 |
|
| 977 |
def test_empty_upload_to(self): |
| 978 |
# upload_to can be empty, meaning it does not use subdirectory. |
| 979 |
obj = Storage() |
| 980 |
obj.empty.save("django_test.txt", ContentFile("more content")) |
| 981 |
self.assertEqual(obj.empty.name, "django_test.txt") |
| 982 |
self.assertEqual(obj.empty.read(), b"more content") |
| 983 |
obj.empty.close() |
| 984 |
|
| 985 |
def test_pathlib_upload_to(self): |
| 986 |
obj = Storage() |
| 987 |
obj.pathlib_callable.save("some_file1.txt", ContentFile("some content")) |
| 988 |
self.assertEqual(obj.pathlib_callable.name, "bar/some_file1.txt") |
| 989 |
obj.pathlib_direct.save("some_file2.txt", ContentFile("some content")) |
| 990 |
self.assertEqual(obj.pathlib_direct.name, "bar/some_file2.txt") |
| 991 |
obj.random.close() |
| 992 |
|
| 993 |
def test_random_upload_to(self): |
| 994 |
# Verify the fix for #5655, making sure the directory is only |
| 995 |
# determined once. |
| 996 |
obj = Storage() |
| 997 |
obj.random.save("random_file", ContentFile("random content")) |
| 998 |
self.assertTrue(obj.random.name.endswith("/random_file")) |
| 999 |
obj.random.close() |
| 1000 |
|
| 1001 |
def test_custom_valid_name_callable_upload_to(self): |
| 1002 |
""" |
| 1003 |
Storage.get_valid_name() should be called when upload_to is a callable. |
| 1004 |
""" |
| 1005 |
obj = Storage() |
| 1006 |
obj.custom_valid_name.save("random_file", ContentFile("random content")) |
| 1007 |
# CustomValidNameStorage.get_valid_name() appends '_valid' to the name |
| 1008 |
self.assertTrue(obj.custom_valid_name.name.endswith("/random_file_valid")) |
| 1009 |
obj.custom_valid_name.close() |
| 1010 |
|
| 1011 |
def test_filefield_pickling(self): |
| 1012 |
# Push an object into the cache to make sure it pickles properly |
| 1013 |
obj = Storage() |
| 1014 |
obj.normal.save("django_test.txt", ContentFile("more content")) |
| 1015 |
obj.normal.close() |
| 1016 |
cache.set("obj", obj) |
| 1017 |
self.assertEqual(cache.get("obj").normal.name, "tests/django_test.txt") |
| 1018 |
|
| 1019 |
def test_file_object(self): |
| 1020 |
# Create sample file |
| 1021 |
temp_storage.save("tests/example.txt", ContentFile("some content")) |
| 1022 |
|
| 1023 |
# Load it as Python file object |
| 1024 |
with open(temp_storage.path("tests/example.txt")) as file_obj: |
| 1025 |
# Save it using storage and read its content |
| 1026 |
temp_storage.save("tests/file_obj", file_obj) |
| 1027 |
self.assertTrue(temp_storage.exists("tests/file_obj")) |
| 1028 |
with temp_storage.open("tests/file_obj") as f: |
| 1029 |
self.assertEqual(f.read(), b"some content") |
| 1030 |
|
| 1031 |
def test_stringio(self): |
| 1032 |
# Test passing StringIO instance as content argument to save |
| 1033 |
output = StringIO() |
| 1034 |
output.write("content") |
| 1035 |
output.seek(0) |
| 1036 |
|
| 1037 |
# Save it and read written file |
| 1038 |
temp_storage.save("tests/stringio", output) |
| 1039 |
self.assertTrue(temp_storage.exists("tests/stringio")) |
| 1040 |
with temp_storage.open("tests/stringio") as f: |
| 1041 |
self.assertEqual(f.read(), b"content") |
| 1042 |
|
| 1043 |
@override_settings( |
| 1044 |
STORAGES={ |
| 1045 |
DEFAULT_STORAGE_ALIAS: { |
| 1046 |
"BACKEND": "django.core.files.storage.InMemoryStorage" |
| 1047 |
} |
| 1048 |
} |
| 1049 |
) |
| 1050 |
def test_create_file_field_from_another_file_field_in_memory_storage(self): |
| 1051 |
f = ContentFile("content", "file.txt") |
| 1052 |
obj = Storage.objects.create(storage_callable_default=f) |
| 1053 |
new_obj = Storage.objects.create( |
| 1054 |
storage_callable_default=obj.storage_callable_default.file |
| 1055 |
) |
| 1056 |
storage = callable_default_storage() |
| 1057 |
with storage.open(new_obj.storage_callable_default.name) as f: |
| 1058 |
self.assertEqual(f.read(), b"content") |
| 1059 |
|
| 1060 |
|
| 1061 |
class FieldCallableFileStorageTests(SimpleTestCase): |
| 1062 |
def setUp(self): |
| 1063 |
self.temp_storage_location = tempfile.mkdtemp( |
| 1064 |
suffix="filefield_callable_storage" |
| 1065 |
) |
| 1066 |
self.addCleanup(shutil.rmtree, self.temp_storage_location) |
| 1067 |
|
| 1068 |
def test_callable_base_class_error_raises(self): |
| 1069 |
class NotStorage: |
| 1070 |
pass |
| 1071 |
|
| 1072 |
msg = ( |
| 1073 |
"FileField.storage must be a subclass/instance of " |
| 1074 |
"django.core.files.storage.base.Storage" |
| 1075 |
) |
| 1076 |
for invalid_type in (NotStorage, str, list, set, tuple): |
| 1077 |
with self.subTest(invalid_type=invalid_type): |
| 1078 |
with self.assertRaisesMessage(TypeError, msg): |
| 1079 |
FileField(storage=invalid_type) |
| 1080 |
|
| 1081 |
def test_file_field_storage_none_uses_default_storage(self): |
| 1082 |
self.assertEqual(FileField().storage, default_storage) |
| 1083 |
|
| 1084 |
def test_callable_function_storage_file_field(self): |
| 1085 |
storage = FileSystemStorage(location=self.temp_storage_location) |
| 1086 |
|
| 1087 |
def get_storage(): |
| 1088 |
return storage |
| 1089 |
|
| 1090 |
obj = FileField(storage=get_storage) |
| 1091 |
self.assertEqual(obj.storage, storage) |
| 1092 |
self.assertEqual(obj.storage.location, storage.location) |
| 1093 |
|
| 1094 |
def test_callable_class_storage_file_field(self): |
| 1095 |
class GetStorage(FileSystemStorage): |
| 1096 |
pass |
| 1097 |
|
| 1098 |
obj = FileField(storage=GetStorage) |
| 1099 |
self.assertIsInstance(obj.storage, BaseStorage) |
| 1100 |
|
| 1101 |
def test_callable_storage_file_field_in_model(self): |
| 1102 |
obj = Storage() |
| 1103 |
self.assertEqual(obj.storage_callable.storage, temp_storage) |
| 1104 |
self.assertEqual(obj.storage_callable.storage.location, temp_storage_location) |
| 1105 |
self.assertIsInstance(obj.storage_callable_class.storage, BaseStorage) |
| 1106 |
|
| 1107 |
def test_deconstruction(self): |
| 1108 |
""" |
| 1109 |
Deconstructing gives the original callable, not the evaluated value. |
| 1110 |
""" |
| 1111 |
obj = Storage() |
| 1112 |
*_, kwargs = obj._meta.get_field("storage_callable").deconstruct() |
| 1113 |
storage = kwargs["storage"] |
| 1114 |
self.assertIs(storage, callable_storage) |
| 1115 |
|
| 1116 |
def test_deconstruction_storage_callable_default(self): |
| 1117 |
""" |
| 1118 |
A callable that returns default_storage is not omitted when |
| 1119 |
deconstructing. |
| 1120 |
""" |
| 1121 |
obj = Storage() |
| 1122 |
*_, kwargs = obj._meta.get_field("storage_callable_default").deconstruct() |
| 1123 |
self.assertIs(kwargs["storage"], callable_default_storage) |
| 1124 |
|
| 1125 |
|
| 1126 |
# Tests for a race condition on file saving (#4948). |
| 1127 |
# This is written in such a way that it'll always pass on platforms |
| 1128 |
# without threading. |
| 1129 |
|
| 1130 |
|
| 1131 |
class SlowFile(ContentFile): |
| 1132 |
def chunks(self): |
| 1133 |
time.sleep(1) |
| 1134 |
return super().chunks() |
| 1135 |
|
| 1136 |
|
| 1137 |
class FileSaveRaceConditionTest(SimpleTestCase): |
| 1138 |
def setUp(self): |
| 1139 |
self.storage_dir = tempfile.mkdtemp() |
| 1140 |
self.addCleanup(shutil.rmtree, self.storage_dir) |
| 1141 |
self.storage = FileSystemStorage(self.storage_dir) |
| 1142 |
self.thread = threading.Thread(target=self.save_file, args=["conflict"]) |
| 1143 |
|
| 1144 |
def save_file(self, name): |
| 1145 |
name = self.storage.save(name, SlowFile(b"Data")) |
| 1146 |
|
| 1147 |
def test_race_condition(self): |
| 1148 |
self.thread.start() |
| 1149 |
self.save_file("conflict") |
| 1150 |
self.thread.join() |
| 1151 |
files = sorted(os.listdir(self.storage_dir)) |
| 1152 |
self.assertEqual(files[0], "conflict") |
| 1153 |
self.assertRegex(files[1], "conflict_%s" % FILE_SUFFIX_REGEX) |
| 1154 |
|
| 1155 |
|
| 1156 |
@unittest.skipIf( |
| 1157 |
sys.platform == "win32", "Windows only partially supports umasks and chmod." |
| 1158 |
) |
| 1159 |
class FileStoragePermissions(unittest.TestCase): |
| 1160 |
def setUp(self): |
| 1161 |
self.umask = 0o027 |
| 1162 |
old_umask = os.umask(self.umask) |
| 1163 |
self.addCleanup(os.umask, old_umask) |
| 1164 |
self.storage_dir = tempfile.mkdtemp() |
| 1165 |
self.addCleanup(shutil.rmtree, self.storage_dir) |
| 1166 |
|
| 1167 |
@override_settings(FILE_UPLOAD_PERMISSIONS=0o654) |
| 1168 |
def test_file_upload_permissions(self): |
| 1169 |
self.storage = FileSystemStorage(self.storage_dir) |
| 1170 |
name = self.storage.save("the_file", ContentFile("data")) |
| 1171 |
actual_mode = os.stat(self.storage.path(name))[0] & 0o777 |
| 1172 |
self.assertEqual(actual_mode, 0o654) |
| 1173 |
|
| 1174 |
@override_settings(FILE_UPLOAD_PERMISSIONS=None) |
| 1175 |
def test_file_upload_default_permissions(self): |
| 1176 |
self.storage = FileSystemStorage(self.storage_dir) |
| 1177 |
fname = self.storage.save("some_file", ContentFile("data")) |
| 1178 |
mode = os.stat(self.storage.path(fname))[0] & 0o777 |
| 1179 |
self.assertEqual(mode, 0o666 & ~self.umask) |
| 1180 |
|
| 1181 |
@override_settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o765) |
| 1182 |
def test_file_upload_directory_permissions(self): |
| 1183 |
self.storage = FileSystemStorage(self.storage_dir) |
| 1184 |
name = self.storage.save("the_directory/subdir/the_file", ContentFile("data")) |
| 1185 |
file_path = Path(self.storage.path(name)) |
| 1186 |
self.assertEqual(file_path.parent.stat().st_mode & 0o777, 0o765) |
| 1187 |
self.assertEqual(file_path.parent.parent.stat().st_mode & 0o777, 0o765) |
| 1188 |
|
| 1189 |
@override_settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=None) |
| 1190 |
def test_file_upload_directory_default_permissions(self): |
| 1191 |
self.storage = FileSystemStorage(self.storage_dir) |
| 1192 |
name = self.storage.save("the_directory/subdir/the_file", ContentFile("data")) |
| 1193 |
file_path = Path(self.storage.path(name)) |
| 1194 |
expected_mode = 0o777 & ~self.umask |
| 1195 |
self.assertEqual(file_path.parent.stat().st_mode & 0o777, expected_mode) |
| 1196 |
self.assertEqual(file_path.parent.parent.stat().st_mode & 0o777, expected_mode) |
| 1197 |
|
| 1198 |
|
| 1199 |
class FileStoragePathParsing(SimpleTestCase): |
| 1200 |
def setUp(self): |
| 1201 |
self.storage_dir = tempfile.mkdtemp() |
| 1202 |
self.addCleanup(shutil.rmtree, self.storage_dir) |
| 1203 |
self.storage = FileSystemStorage(self.storage_dir) |
| 1204 |
|
| 1205 |
def test_directory_with_dot(self): |
| 1206 |
"""Regression test for #9610. |
| 1207 |
|
| 1208 |
If the directory name contains a dot and the file name doesn't, make |
| 1209 |
sure we still mangle the file name instead of the directory name. |
| 1210 |
""" |
| 1211 |
|
| 1212 |
self.storage.save("dotted.path/test", ContentFile("1")) |
| 1213 |
self.storage.save("dotted.path/test", ContentFile("2")) |
| 1214 |
|
| 1215 |
files = sorted(os.listdir(os.path.join(self.storage_dir, "dotted.path"))) |
| 1216 |
self.assertFalse(os.path.exists(os.path.join(self.storage_dir, "dotted_.path"))) |
| 1217 |
self.assertEqual(files[0], "test") |
| 1218 |
self.assertRegex(files[1], "test_%s" % FILE_SUFFIX_REGEX) |
| 1219 |
|
| 1220 |
def test_first_character_dot(self): |
| 1221 |
""" |
| 1222 |
File names with a dot as their first character don't have an extension, |
| 1223 |
and the underscore should get added to the end. |
| 1224 |
""" |
| 1225 |
self.storage.save("dotted.path/.test", ContentFile("1")) |
| 1226 |
self.storage.save("dotted.path/.test", ContentFile("2")) |
| 1227 |
|
| 1228 |
files = sorted(os.listdir(os.path.join(self.storage_dir, "dotted.path"))) |
| 1229 |
self.assertFalse(os.path.exists(os.path.join(self.storage_dir, "dotted_.path"))) |
| 1230 |
self.assertEqual(files[0], ".test") |
| 1231 |
self.assertRegex(files[1], ".test_%s" % FILE_SUFFIX_REGEX) |
| 1232 |
|
| 1233 |
|
| 1234 |
class ContentFileStorageTestCase(unittest.TestCase): |
| 1235 |
def setUp(self): |
| 1236 |
storage_dir = tempfile.mkdtemp() |
| 1237 |
self.addCleanup(shutil.rmtree, storage_dir) |
| 1238 |
self.storage = FileSystemStorage(storage_dir) |
| 1239 |
|
| 1240 |
def test_content_saving(self): |
| 1241 |
""" |
| 1242 |
ContentFile can be saved correctly with the filesystem storage, |
| 1243 |
if it was initialized with either bytes or unicode content. |
| 1244 |
""" |
| 1245 |
self.storage.save("bytes.txt", ContentFile(b"content")) |
| 1246 |
self.storage.save("unicode.txt", ContentFile("espaƱol")) |
| 1247 |
|
| 1248 |
|
| 1249 |
@override_settings(ROOT_URLCONF="file_storage.urls") |
| 1250 |
class FileLikeObjectTestCase(LiveServerTestCase): |
| 1251 |
""" |
| 1252 |
Test file-like objects (#15644). |
| 1253 |
""" |
| 1254 |
|
| 1255 |
available_apps = [] |
| 1256 |
|
| 1257 |
def setUp(self): |
| 1258 |
self.temp_dir = tempfile.mkdtemp() |
| 1259 |
self.addCleanup(shutil.rmtree, self.temp_dir) |
| 1260 |
self.storage = FileSystemStorage(location=self.temp_dir) |
| 1261 |
|
| 1262 |
def test_urllib_request_urlopen(self): |
| 1263 |
""" |
| 1264 |
Test the File storage API with a file-like object coming from |
| 1265 |
urllib.request.urlopen(). |
| 1266 |
""" |
| 1267 |
file_like_object = urlopen(self.live_server_url + "/") |
| 1268 |
f = File(file_like_object) |
| 1269 |
stored_filename = self.storage.save("remote_file.html", f) |
| 1270 |
|
| 1271 |
remote_file = urlopen(self.live_server_url + "/") |
| 1272 |
with self.storage.open(stored_filename) as stored_file: |
| 1273 |
self.assertEqual(stored_file.read(), remote_file.read()) |
| 1274 |
|
| 1275 |
|
| 1276 |
class StorageHandlerTests(SimpleTestCase): |
| 1277 |
@override_settings( |
| 1278 |
STORAGES={ |
| 1279 |
"custom_storage": { |
| 1280 |
"BACKEND": "django.core.files.storage.FileSystemStorage", |
| 1281 |
}, |
| 1282 |
} |
| 1283 |
) |
| 1284 |
def test_same_instance(self): |
| 1285 |
cache1 = storages["custom_storage"] |
| 1286 |
cache2 = storages["custom_storage"] |
| 1287 |
self.assertIs(cache1, cache2) |
| 1288 |
|
| 1289 |
def test_defaults(self): |
| 1290 |
storages = StorageHandler() |
| 1291 |
self.assertEqual( |
| 1292 |
storages.backends, |
| 1293 |
{ |
| 1294 |
DEFAULT_STORAGE_ALIAS: { |
| 1295 |
"BACKEND": "django.core.files.storage.FileSystemStorage", |
| 1296 |
}, |
| 1297 |
STATICFILES_STORAGE_ALIAS: { |
| 1298 |
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage", |
| 1299 |
}, |
| 1300 |
}, |
| 1301 |
) |
| 1302 |
|
| 1303 |
def test_nonexistent_alias(self): |
| 1304 |
msg = "Could not find config for 'nonexistent' in settings.STORAGES." |
| 1305 |
storages = StorageHandler() |
| 1306 |
with self.assertRaisesMessage(InvalidStorageError, msg): |
| 1307 |
storages["nonexistent"] |
| 1308 |
|
| 1309 |
def test_nonexistent_backend(self): |
| 1310 |
test_storages = StorageHandler( |
| 1311 |
{ |
| 1312 |
"invalid_backend": { |
| 1313 |
"BACKEND": "django.nonexistent.NonexistentBackend", |
| 1314 |
}, |
| 1315 |
} |
| 1316 |
) |
| 1317 |
msg = ( |
| 1318 |
"Could not find backend 'django.nonexistent.NonexistentBackend': " |
| 1319 |
"No module named 'django.nonexistent'" |
| 1320 |
) |
| 1321 |
with self.assertRaisesMessage(InvalidStorageError, msg): |
| 1322 |
test_storages["invalid_backend"] |