Debian Codegraph @

tests/files/tests.py

raw
1 import errno
2 import gzip
3 import os
4 import struct
5 import tempfile
6 import unittest
7 from io import BytesIO, StringIO, TextIOWrapper
8 from pathlib import Path
9 from unittest import mock
10
11 from django.core.files import File, locks
12 from django.core.files.base import ContentFile
13 from django.core.files.move import file_move_safe
14 from django.core.files.temp import NamedTemporaryFile
15 from django.core.files.uploadedfile import (
16 InMemoryUploadedFile,
17 SimpleUploadedFile,
18 TemporaryUploadedFile,
19 UploadedFile,
20 )
21 from django.test import override_settings
22
23 try:
24 from PIL import Image, features
25
26 HAS_WEBP = features.check("webp")
27 except ImportError:
28 Image = None
29 HAS_WEBP = False
30 else:
31 from django.core.files import images
32
33
34 class FileTests(unittest.TestCase):
35 def test_unicode_uploadedfile_name(self):
36 uf = UploadedFile(name="¿Cómo?", content_type="text")
37 self.assertIs(type(repr(uf)), str)
38
39 def test_unicode_file_name(self):
40 f = File(None, "djángö")
41 self.assertIs(type(repr(f)), str)
42
43 def test_context_manager(self):
44 orig_file = tempfile.TemporaryFile()
45 base_file = File(orig_file)
46 with base_file as f:
47 self.assertIs(base_file, f)
48 self.assertFalse(f.closed)
49 self.assertTrue(f.closed)
50 self.assertTrue(orig_file.closed)
51
52 def test_open_resets_opened_file_to_start_and_returns_context_manager(self):
53 file = File(BytesIO(b"content"))
54 file.read()
55 with file.open() as f:
56 self.assertEqual(f.read(), b"content")
57
58 def test_open_reopens_closed_file_and_returns_context_manager(self):
59 temporary_file = tempfile.NamedTemporaryFile(delete=False)
60 file = File(temporary_file)
61 try:
62 file.close()
63 with file.open() as f:
64 self.assertFalse(f.closed)
65 finally:
66 # remove temporary file
67 os.unlink(file.name)
68
69 def test_namedtemporaryfile_closes(self):
70 """
71 The symbol django.core.files.NamedTemporaryFile is assigned as
72 a different class on different operating systems. In
73 any case, the result should minimally mock some of the API of
74 tempfile.NamedTemporaryFile from the Python standard library.
75 """
76 tempfile = NamedTemporaryFile()
77 self.assertTrue(hasattr(tempfile, "closed"))
78 self.assertFalse(tempfile.closed)
79
80 tempfile.close()
81 self.assertTrue(tempfile.closed)
82
83 def test_file_mode(self):
84 # Should not set mode to None if it is not present.
85 # See #14681, stdlib gzip module crashes if mode is set to None
86 file = SimpleUploadedFile("mode_test.txt", b"content")
87 self.assertFalse(hasattr(file, "mode"))
88 gzip.GzipFile(fileobj=file)
89
90 def test_file_iteration(self):
91 """
92 File objects should yield lines when iterated over.
93 Refs #22107.
94 """
95 file = File(BytesIO(b"one\ntwo\nthree"))
96 self.assertEqual(list(file), [b"one\n", b"two\n", b"three"])
97
98 def test_file_iteration_windows_newlines(self):
99 """
100 #8149 - File objects with \r\n line endings should yield lines
101 when iterated over.
102 """
103 f = File(BytesIO(b"one\r\ntwo\r\nthree"))
104 self.assertEqual(list(f), [b"one\r\n", b"two\r\n", b"three"])
105
106 def test_file_iteration_mac_newlines(self):
107 """
108 #8149 - File objects with \r line endings should yield lines
109 when iterated over.
110 """
111 f = File(BytesIO(b"one\rtwo\rthree"))
112 self.assertEqual(list(f), [b"one\r", b"two\r", b"three"])
113
114 def test_file_iteration_mixed_newlines(self):
115 f = File(BytesIO(b"one\rtwo\nthree\r\nfour"))
116 self.assertEqual(list(f), [b"one\r", b"two\n", b"three\r\n", b"four"])
117
118 def test_file_iteration_with_unix_newline_at_chunk_boundary(self):
119 f = File(BytesIO(b"one\ntwo\nthree"))
120 # Set chunk size to create a boundary after \n:
121 # b'one\n...
122 # ^
123 f.DEFAULT_CHUNK_SIZE = 4
124 self.assertEqual(list(f), [b"one\n", b"two\n", b"three"])
125
126 def test_file_iteration_with_windows_newline_at_chunk_boundary(self):
127 f = File(BytesIO(b"one\r\ntwo\r\nthree"))
128 # Set chunk size to create a boundary between \r and \n:
129 # b'one\r\n...
130 # ^
131 f.DEFAULT_CHUNK_SIZE = 4
132 self.assertEqual(list(f), [b"one\r\n", b"two\r\n", b"three"])
133
134 def test_file_iteration_with_mac_newline_at_chunk_boundary(self):
135 f = File(BytesIO(b"one\rtwo\rthree"))
136 # Set chunk size to create a boundary after \r:
137 # b'one\r...
138 # ^
139 f.DEFAULT_CHUNK_SIZE = 4
140 self.assertEqual(list(f), [b"one\r", b"two\r", b"three"])
141
142 def test_file_iteration_with_text(self):
143 f = File(StringIO("one\ntwo\nthree"))
144 self.assertEqual(list(f), ["one\n", "two\n", "three"])
145
146 def test_readable(self):
147 with (
148 tempfile.TemporaryFile() as temp,
149 File(temp, name="something.txt") as test_file,
150 ):
151 self.assertTrue(test_file.readable())
152 self.assertFalse(test_file.readable())
153
154 def test_writable(self):
155 with (
156 tempfile.TemporaryFile() as temp,
157 File(temp, name="something.txt") as test_file,
158 ):
159 self.assertTrue(test_file.writable())
160 self.assertFalse(test_file.writable())
161 with (
162 tempfile.TemporaryFile("rb") as temp,
163 File(temp, name="something.txt") as test_file,
164 ):
165 self.assertFalse(test_file.writable())
166
167 def test_seekable(self):
168 with (
169 tempfile.TemporaryFile() as temp,
170 File(temp, name="something.txt") as test_file,
171 ):
172 self.assertTrue(test_file.seekable())
173 self.assertFalse(test_file.seekable())
174
175 def test_io_wrapper(self):
176 content = "vive l'été\n"
177 with (
178 tempfile.TemporaryFile() as temp,
179 File(temp, name="something.txt") as test_file,
180 ):
181 test_file.write(content.encode())
182 test_file.seek(0)
183 wrapper = TextIOWrapper(test_file, "utf-8", newline="\n")
184 self.assertEqual(wrapper.read(), content)
185 wrapper.write(content)
186 wrapper.seek(0)
187 self.assertEqual(wrapper.read(), content * 2)
188 test_file = wrapper.detach()
189 test_file.seek(0)
190 self.assertEqual(test_file.read(), (content * 2).encode())
191
192 def test_exclusive_lock(self):
193 file_path = Path(__file__).parent / "test.png"
194 with open(file_path) as f1, open(file_path) as f2:
195 self.assertIs(locks.lock(f1, locks.LOCK_EX), True)
196 self.assertIs(locks.lock(f2, locks.LOCK_EX | locks.LOCK_NB), False)
197 self.assertIs(locks.lock(f2, locks.LOCK_SH | locks.LOCK_NB), False)
198 self.assertIs(locks.unlock(f1), True)
199
200 def test_shared_lock(self):
201 file_path = Path(__file__).parent / "test.png"
202 with open(file_path) as f1, open(file_path) as f2:
203 self.assertIs(locks.lock(f1, locks.LOCK_SH), True)
204 self.assertIs(locks.lock(f2, locks.LOCK_SH | locks.LOCK_NB), True)
205 self.assertIs(locks.unlock(f1), True)
206 self.assertIs(locks.unlock(f2), True)
207
208 def test_open_supports_full_signature(self):
209 called = False
210
211 def opener(path, flags):
212 nonlocal called
213 called = True
214 return os.open(path, flags)
215
216 file_path = Path(__file__).parent / "test.png"
217 with open(file_path) as f:
218 test_file = File(f)
219
220 with test_file.open(opener=opener):
221 self.assertIs(called, True)
222
223
224 class NoNameFileTestCase(unittest.TestCase):
225 """
226 Other examples of unnamed files may be tempfile.SpooledTemporaryFile or
227 urllib.urlopen()
228 """
229
230 def test_noname_file_default_name(self):
231 self.assertIsNone(File(BytesIO(b"A file with no name")).name)
232
233 def test_noname_file_get_size(self):
234 self.assertEqual(File(BytesIO(b"A file with no name")).size, 19)
235
236
237 class ContentFileTestCase(unittest.TestCase):
238 def test_content_file_default_name(self):
239 self.assertIsNone(ContentFile(b"content").name)
240
241 def test_content_file_custom_name(self):
242 """
243 The constructor of ContentFile accepts 'name' (#16590).
244 """
245 name = "I can have a name too!"
246 self.assertEqual(ContentFile(b"content", name=name).name, name)
247
248 def test_content_file_input_type(self):
249 """
250 ContentFile can accept both bytes and strings and the retrieved content
251 is of the same type.
252 """
253 self.assertIsInstance(ContentFile(b"content").read(), bytes)
254 self.assertIsInstance(ContentFile("español").read(), str)
255
256 def test_open_resets_file_to_start_and_returns_context_manager(self):
257 file = ContentFile(b"content")
258 with file.open() as f:
259 self.assertEqual(f.read(), b"content")
260 with file.open() as f:
261 self.assertEqual(f.read(), b"content")
262
263 def test_size_changing_after_writing(self):
264 """ContentFile.size changes after a write()."""
265 f = ContentFile("")
266 self.assertEqual(f.size, 0)
267 f.write("Test ")
268 f.write("string")
269 self.assertEqual(f.size, 11)
270 with f.open() as fh:
271 self.assertEqual(fh.read(), "Test string")
272
273
274 class InMemoryUploadedFileTests(unittest.TestCase):
275 def test_open_resets_file_to_start_and_returns_context_manager(self):
276 uf = InMemoryUploadedFile(StringIO("1"), "", "test", "text/plain", 1, "utf8")
277 uf.read()
278 with uf.open() as f:
279 self.assertEqual(f.read(), "1")
280
281
282 class TemporaryUploadedFileTests(unittest.TestCase):
283 def test_extension_kept(self):
284 """The temporary file name has the same suffix as the original file."""
285 with TemporaryUploadedFile("test.txt", "text/plain", 1, "utf8") as temp_file:
286 self.assertTrue(temp_file.file.name.endswith(".upload.txt"))
287
288 def test_file_upload_temp_dir_pathlib(self):
289 with tempfile.TemporaryDirectory() as tmp_dir:
290 with override_settings(FILE_UPLOAD_TEMP_DIR=Path(tmp_dir)):
291 with TemporaryUploadedFile(
292 "test.txt", "text/plain", 1, "utf-8"
293 ) as temp_file:
294 self.assertTrue(os.path.exists(temp_file.file.name))
295
296
297 class DimensionClosingBug(unittest.TestCase):
298 """
299 get_image_dimensions() properly closes files (#8817)
300 """
301
302 @unittest.skipUnless(Image, "Pillow not installed")
303 def test_not_closing_of_files(self):
304 """
305 Open files passed into get_image_dimensions() should stay opened.
306 """
307 empty_io = BytesIO()
308 try:
309 images.get_image_dimensions(empty_io)
310 finally:
311 self.assertTrue(not empty_io.closed)
312
313 @unittest.skipUnless(Image, "Pillow not installed")
314 def test_closing_of_filenames(self):
315 """
316 get_image_dimensions() called with a filename should closed the file.
317 """
318 # We need to inject a modified open() builtin into the images module
319 # that checks if the file was closed properly if the function is
320 # called with a filename instead of a file object.
321 # get_image_dimensions will call our catching_open instead of the
322 # regular builtin one.
323
324 class FileWrapper:
325 _closed = []
326
327 def __init__(self, f):
328 self.f = f
329
330 def __getattr__(self, name):
331 return getattr(self.f, name)
332
333 def close(self):
334 self._closed.append(True)
335 self.f.close()
336
337 def catching_open(*args):
338 return FileWrapper(open(*args))
339
340 images.open = catching_open
341 try:
342 images.get_image_dimensions(
343 os.path.join(os.path.dirname(__file__), "test1.png")
344 )
345 finally:
346 del images.open
347 self.assertTrue(FileWrapper._closed)
348
349
350 class InconsistentGetImageDimensionsBug(unittest.TestCase):
351 """
352 get_image_dimensions() works properly after various calls
353 using a file handler (#11158)
354 """
355
356 @unittest.skipUnless(Image, "Pillow not installed")
357 def test_multiple_calls(self):
358 """
359 Multiple calls of get_image_dimensions() should return the same size.
360 """
361 img_path = os.path.join(os.path.dirname(__file__), "test.png")
362 with open(img_path, "rb") as fh:
363 image = images.ImageFile(fh)
364 image_pil = Image.open(fh)
365 size_1 = images.get_image_dimensions(image)
366 size_2 = images.get_image_dimensions(image)
367 self.assertEqual(image_pil.size, size_1)
368 self.assertEqual(size_1, size_2)
369
370 @unittest.skipUnless(Image, "Pillow not installed")
371 def test_bug_19457(self):
372 """
373 Regression test for #19457
374 get_image_dimensions() fails on some PNGs, while Image.size is working
375 good on them.
376 """
377 img_path = os.path.join(os.path.dirname(__file__), "magic.png")
378 size = images.get_image_dimensions(img_path)
379 with open(img_path, "rb") as fh:
380 self.assertEqual(size, Image.open(fh).size)
381
382
383 @unittest.skipUnless(Image, "Pillow not installed")
384 class GetImageDimensionsTests(unittest.TestCase):
385 def test_invalid_image(self):
386 """
387 get_image_dimensions() should return (None, None) for the dimensions of
388 invalid images (#24441).
389
390 brokenimg.png is not a valid image and it has been generated by:
391 $ echo "123" > brokenimg.png
392 """
393 img_path = os.path.join(os.path.dirname(__file__), "brokenimg.png")
394 with open(img_path, "rb") as fh:
395 size = images.get_image_dimensions(fh)
396 self.assertEqual(size, (None, None))
397
398 def test_valid_image(self):
399 """
400 get_image_dimensions() should catch struct.error while feeding the PIL
401 Image parser (#24544).
402
403 Emulates the Parser feed error. Since the error is raised on every feed
404 attempt, the resulting image size should be invalid: (None, None).
405 """
406 img_path = os.path.join(os.path.dirname(__file__), "test.png")
407 with mock.patch("PIL.ImageFile.Parser.feed", side_effect=struct.error):
408 with open(img_path, "rb") as fh:
409 size = images.get_image_dimensions(fh)
410 self.assertEqual(size, (None, None))
411
412 def test_missing_file(self):
413 size = images.get_image_dimensions("missing.png")
414 self.assertEqual(size, (None, None))
415
416 @unittest.skipUnless(HAS_WEBP, "WEBP not installed")
417 def test_webp(self):
418 img_path = os.path.join(os.path.dirname(__file__), "test.webp")
419 with open(img_path, "rb") as fh:
420 size = images.get_image_dimensions(fh)
421 self.assertEqual(size, (540, 405))
422
423
424 class FileMoveSafeTests(unittest.TestCase):
425 def test_file_move_overwrite(self):
426 handle_a, self.file_a = tempfile.mkstemp()
427 handle_b, self.file_b = tempfile.mkstemp()
428
429 # file_move_safe() raises FileExistsError if the destination file
430 # exists and allow_overwrite is False.
431 msg = r"Destination file .* exists and allow_overwrite is False\."
432 with self.assertRaisesRegex(FileExistsError, msg):
433 file_move_safe(self.file_a, self.file_b, allow_overwrite=False)
434
435 # should allow it and continue on if allow_overwrite is True
436 self.assertIsNone(
437 file_move_safe(self.file_a, self.file_b, allow_overwrite=True)
438 )
439
440 os.close(handle_a)
441 os.close(handle_b)
442
443 def test_file_move_permissionerror(self):
444 """
445 file_move_safe() ignores PermissionError thrown by copystat() and
446 copymode().
447 For example, PermissionError can be raised when the destination
448 filesystem is CIFS, or when relabel is disabled by SELinux across
449 filesystems.
450 """
451 permission_error = PermissionError(errno.EPERM, "msg")
452 os_error = OSError("msg")
453 handle_a, self.file_a = tempfile.mkstemp()
454 handle_b, self.file_b = tempfile.mkstemp()
455 handle_c, self.file_c = tempfile.mkstemp()
456 try:
457 # This exception is required to reach the copystat() call in
458 # file_safe_move().
459 with mock.patch("django.core.files.move.os.rename", side_effect=OSError()):
460 # An error besides PermissionError isn't ignored.
461 with mock.patch(
462 "django.core.files.move.copystat", side_effect=os_error
463 ):
464 with self.assertRaises(OSError):
465 file_move_safe(self.file_a, self.file_b, allow_overwrite=True)
466 # When copystat() throws PermissionError, copymode() error besides
467 # PermissionError isn't ignored.
468 with mock.patch(
469 "django.core.files.move.copystat", side_effect=permission_error
470 ):
471 with mock.patch(
472 "django.core.files.move.copymode", side_effect=os_error
473 ):
474 with self.assertRaises(OSError):
475 file_move_safe(
476 self.file_a, self.file_b, allow_overwrite=True
477 )
478 # PermissionError raised by copystat() is ignored.
479 with mock.patch(
480 "django.core.files.move.copystat", side_effect=permission_error
481 ):
482 self.assertIsNone(
483 file_move_safe(self.file_a, self.file_b, allow_overwrite=True)
484 )
485 # PermissionError raised by copymode() is ignored too.
486 with mock.patch(
487 "django.core.files.move.copymode", side_effect=permission_error
488 ):
489 self.assertIsNone(
490 file_move_safe(
491 self.file_c, self.file_b, allow_overwrite=True
492 )
493 )
494 finally:
495 os.close(handle_a)
496 os.close(handle_b)
497 os.close(handle_c)
498
499 def test_file_move_ensure_truncation(self):
500 with tempfile.NamedTemporaryFile(delete=False) as src:
501 src.write(b"content")
502 src_name = src.name
503 self.addCleanup(
504 lambda: os.remove(src_name) if os.path.exists(src_name) else None
505 )
506
507 with tempfile.NamedTemporaryFile(delete=False) as dest:
508 dest.write(b"This is a longer content.")
509 dest_name = dest.name
510 self.addCleanup(os.remove, dest_name)
511
512 with mock.patch("django.core.files.move.os.rename", side_effect=OSError()):
513 file_move_safe(src_name, dest_name, allow_overwrite=True)
514
515 with open(dest_name, "rb") as f:
516 content = f.read()
517
518 self.assertEqual(content, b"content")
519
520
521 class SpooledTempTests(unittest.TestCase):
522 def test_in_memory_spooled_temp(self):
523 with tempfile.SpooledTemporaryFile() as temp:
524 temp.write(b"foo bar baz quux\n")
525 django_file = File(temp, name="something.txt")
526 self.assertEqual(django_file.size, 17)
527
528 def test_written_spooled_temp(self):
529 with tempfile.SpooledTemporaryFile(max_size=4) as temp:
530 temp.write(b"foo bar baz quux\n")
531 django_file = File(temp, name="something.txt")
532 self.assertEqual(django_file.size, 17)