2017-05-10 23:29:12 +08:00
|
|
|
import errno
|
2012-05-31 15:59:58 +08:00
|
|
|
import gzip
|
2015-01-28 20:35:27 +08:00
|
|
|
import os
|
2015-03-28 03:54:14 +08:00
|
|
|
import struct
|
2011-03-28 13:58:43 +08:00
|
|
|
import tempfile
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2016-05-23 00:43:56 +08:00
|
|
|
from io import BytesIO, StringIO, TextIOWrapper
|
2019-11-06 15:20:23 +08:00
|
|
|
from pathlib import Path
|
2017-01-20 01:16:04 +08:00
|
|
|
from unittest import mock
|
2010-09-13 04:03:55 +08:00
|
|
|
|
2020-09-11 17:48:40 +08:00
|
|
|
from django.core.files import File, locks
|
2010-09-13 04:03:55 +08:00
|
|
|
from django.core.files.base import ContentFile
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.files.move import file_move_safe
|
2013-09-07 02:23:50 +08:00
|
|
|
from django.core.files.temp import NamedTemporaryFile
|
2017-04-07 20:21:06 +08:00
|
|
|
from django.core.files.uploadedfile import (
|
2017-06-05 06:05:50 +08:00
|
|
|
InMemoryUploadedFile, SimpleUploadedFile, TemporaryUploadedFile,
|
|
|
|
UploadedFile,
|
2017-04-07 20:21:06 +08:00
|
|
|
)
|
2019-11-06 15:20:23 +08:00
|
|
|
from django.test import override_settings
|
2013-10-28 04:26:51 +08:00
|
|
|
|
|
|
|
try:
|
2020-10-06 17:25:04 +08:00
|
|
|
from PIL import Image, features
|
|
|
|
HAS_WEBP = features.check('webp')
|
2014-03-21 22:54:53 +08:00
|
|
|
except ImportError:
|
2013-10-28 04:26:51 +08:00
|
|
|
Image = None
|
2020-10-06 17:25:04 +08:00
|
|
|
HAS_WEBP = False
|
2014-03-21 22:54:53 +08:00
|
|
|
else:
|
|
|
|
from django.core.files import images
|
2013-02-23 20:42:04 +08:00
|
|
|
|
|
|
|
|
2012-05-31 15:59:58 +08:00
|
|
|
class FileTests(unittest.TestCase):
|
2013-10-28 04:26:51 +08:00
|
|
|
def test_unicode_uploadedfile_name(self):
|
|
|
|
uf = UploadedFile(name='¿Cómo?', content_type='text')
|
2014-11-21 22:26:46 +08:00
|
|
|
self.assertIs(type(repr(uf)), str)
|
|
|
|
|
|
|
|
def test_unicode_file_name(self):
|
|
|
|
f = File(None, 'djángö')
|
|
|
|
self.assertIs(type(repr(f)), str)
|
2013-10-28 04:26:51 +08:00
|
|
|
|
2011-03-28 13:58:43 +08:00
|
|
|
def test_context_manager(self):
|
|
|
|
orig_file = tempfile.TemporaryFile()
|
|
|
|
base_file = File(orig_file)
|
|
|
|
with base_file as f:
|
|
|
|
self.assertIs(base_file, f)
|
|
|
|
self.assertFalse(f.closed)
|
|
|
|
self.assertTrue(f.closed)
|
|
|
|
self.assertTrue(orig_file.closed)
|
2012-05-31 15:59:58 +08:00
|
|
|
|
2017-04-07 20:21:06 +08:00
|
|
|
def test_open_resets_opened_file_to_start_and_returns_context_manager(self):
|
|
|
|
file = File(BytesIO(b'content'))
|
|
|
|
file.read()
|
|
|
|
with file.open() as f:
|
|
|
|
self.assertEqual(f.read(), b'content')
|
|
|
|
|
|
|
|
def test_open_reopens_closed_file_and_returns_context_manager(self):
|
|
|
|
temporary_file = tempfile.NamedTemporaryFile(delete=False)
|
|
|
|
file = File(temporary_file)
|
|
|
|
try:
|
|
|
|
file.close()
|
|
|
|
with file.open() as f:
|
|
|
|
self.assertFalse(f.closed)
|
|
|
|
finally:
|
|
|
|
# remove temporary file
|
|
|
|
os.unlink(file.name)
|
|
|
|
|
2013-09-07 02:23:50 +08:00
|
|
|
def test_namedtemporaryfile_closes(self):
|
|
|
|
"""
|
|
|
|
The symbol django.core.files.NamedTemporaryFile is assigned as
|
|
|
|
a different class on different operating systems. In
|
|
|
|
any case, the result should minimally mock some of the API of
|
|
|
|
tempfile.NamedTemporaryFile from the Python standard library.
|
|
|
|
"""
|
|
|
|
tempfile = NamedTemporaryFile()
|
|
|
|
self.assertTrue(hasattr(tempfile, "closed"))
|
|
|
|
self.assertFalse(tempfile.closed)
|
|
|
|
|
|
|
|
tempfile.close()
|
|
|
|
self.assertTrue(tempfile.closed)
|
|
|
|
|
2012-05-31 15:59:58 +08:00
|
|
|
def test_file_mode(self):
|
|
|
|
# Should not set mode to None if it is not present.
|
|
|
|
# See #14681, stdlib gzip module crashes if mode is set to None
|
2012-08-12 04:29:18 +08:00
|
|
|
file = SimpleUploadedFile("mode_test.txt", b"content")
|
2012-05-31 15:59:58 +08:00
|
|
|
self.assertFalse(hasattr(file, 'mode'))
|
2013-10-19 20:31:38 +08:00
|
|
|
gzip.GzipFile(fileobj=file)
|
2013-06-20 18:55:27 +08:00
|
|
|
|
2014-02-21 02:13:25 +08:00
|
|
|
def test_file_iteration(self):
|
|
|
|
"""
|
|
|
|
File objects should yield lines when iterated over.
|
|
|
|
Refs #22107.
|
|
|
|
"""
|
|
|
|
file = File(BytesIO(b'one\ntwo\nthree'))
|
|
|
|
self.assertEqual(list(file), [b'one\n', b'two\n', b'three'])
|
|
|
|
|
2014-09-30 09:24:33 +08:00
|
|
|
def test_file_iteration_windows_newlines(self):
|
|
|
|
"""
|
|
|
|
#8149 - File objects with \r\n line endings should yield lines
|
|
|
|
when iterated over.
|
|
|
|
"""
|
|
|
|
f = File(BytesIO(b'one\r\ntwo\r\nthree'))
|
|
|
|
self.assertEqual(list(f), [b'one\r\n', b'two\r\n', b'three'])
|
|
|
|
|
|
|
|
def test_file_iteration_mac_newlines(self):
|
|
|
|
"""
|
|
|
|
#8149 - File objects with \r line endings should yield lines
|
|
|
|
when iterated over.
|
|
|
|
"""
|
|
|
|
f = File(BytesIO(b'one\rtwo\rthree'))
|
|
|
|
self.assertEqual(list(f), [b'one\r', b'two\r', b'three'])
|
|
|
|
|
|
|
|
def test_file_iteration_mixed_newlines(self):
|
|
|
|
f = File(BytesIO(b'one\rtwo\nthree\r\nfour'))
|
|
|
|
self.assertEqual(list(f), [b'one\r', b'two\n', b'three\r\n', b'four'])
|
|
|
|
|
|
|
|
def test_file_iteration_with_unix_newline_at_chunk_boundary(self):
|
|
|
|
f = File(BytesIO(b'one\ntwo\nthree'))
|
|
|
|
# Set chunk size to create a boundary after \n:
|
|
|
|
# b'one\n...
|
|
|
|
# ^
|
|
|
|
f.DEFAULT_CHUNK_SIZE = 4
|
|
|
|
self.assertEqual(list(f), [b'one\n', b'two\n', b'three'])
|
|
|
|
|
|
|
|
def test_file_iteration_with_windows_newline_at_chunk_boundary(self):
|
|
|
|
f = File(BytesIO(b'one\r\ntwo\r\nthree'))
|
|
|
|
# Set chunk size to create a boundary between \r and \n:
|
|
|
|
# b'one\r\n...
|
|
|
|
# ^
|
|
|
|
f.DEFAULT_CHUNK_SIZE = 4
|
|
|
|
self.assertEqual(list(f), [b'one\r\n', b'two\r\n', b'three'])
|
|
|
|
|
|
|
|
def test_file_iteration_with_mac_newline_at_chunk_boundary(self):
|
|
|
|
f = File(BytesIO(b'one\rtwo\rthree'))
|
|
|
|
# Set chunk size to create a boundary after \r:
|
|
|
|
# b'one\r...
|
|
|
|
# ^
|
|
|
|
f.DEFAULT_CHUNK_SIZE = 4
|
|
|
|
self.assertEqual(list(f), [b'one\r', b'two\r', b'three'])
|
|
|
|
|
|
|
|
def test_file_iteration_with_text(self):
|
|
|
|
f = File(StringIO('one\ntwo\nthree'))
|
|
|
|
self.assertEqual(list(f), ['one\n', 'two\n', 'three'])
|
|
|
|
|
2016-05-23 00:43:56 +08:00
|
|
|
def test_readable(self):
|
|
|
|
with tempfile.TemporaryFile() as temp, File(temp, name='something.txt') as test_file:
|
|
|
|
self.assertTrue(test_file.readable())
|
|
|
|
self.assertFalse(test_file.readable())
|
|
|
|
|
|
|
|
def test_writable(self):
|
|
|
|
with tempfile.TemporaryFile() as temp, File(temp, name='something.txt') as test_file:
|
|
|
|
self.assertTrue(test_file.writable())
|
|
|
|
self.assertFalse(test_file.writable())
|
|
|
|
with tempfile.TemporaryFile('rb') as temp, File(temp, name='something.txt') as test_file:
|
|
|
|
self.assertFalse(test_file.writable())
|
|
|
|
|
2015-06-10 22:43:49 +08:00
|
|
|
def test_seekable(self):
|
2016-05-23 00:43:56 +08:00
|
|
|
with tempfile.TemporaryFile() as temp, File(temp, name='something.txt') as test_file:
|
|
|
|
self.assertTrue(test_file.seekable())
|
|
|
|
self.assertFalse(test_file.seekable())
|
|
|
|
|
|
|
|
def test_io_wrapper(self):
|
|
|
|
content = "vive l'été\n"
|
|
|
|
with tempfile.TemporaryFile() as temp, File(temp, name='something.txt') as test_file:
|
2017-02-08 01:05:47 +08:00
|
|
|
test_file.write(content.encode())
|
2016-05-23 00:43:56 +08:00
|
|
|
test_file.seek(0)
|
|
|
|
wrapper = TextIOWrapper(test_file, 'utf-8', newline='\n')
|
|
|
|
self.assertEqual(wrapper.read(), content)
|
|
|
|
wrapper.write(content)
|
|
|
|
wrapper.seek(0)
|
|
|
|
self.assertEqual(wrapper.read(), content * 2)
|
|
|
|
test_file = wrapper.detach()
|
|
|
|
test_file.seek(0)
|
2017-02-08 01:05:47 +08:00
|
|
|
self.assertEqual(test_file.read(), (content * 2).encode())
|
2015-06-10 22:43:49 +08:00
|
|
|
|
2020-09-11 17:48:40 +08:00
|
|
|
def test_exclusive_lock(self):
|
|
|
|
file_path = Path(__file__).parent / 'test.png'
|
|
|
|
with open(file_path) as f1, open(file_path) as f2:
|
|
|
|
self.assertIs(locks.lock(f1, locks.LOCK_EX), True)
|
|
|
|
self.assertIs(locks.lock(f2, locks.LOCK_EX | locks.LOCK_NB), False)
|
|
|
|
self.assertIs(locks.lock(f2, locks.LOCK_SH | locks.LOCK_NB), False)
|
|
|
|
self.assertIs(locks.unlock(f1), True)
|
|
|
|
|
|
|
|
def test_shared_lock(self):
|
|
|
|
file_path = Path(__file__).parent / 'test.png'
|
|
|
|
with open(file_path) as f1, open(file_path) as f2:
|
|
|
|
self.assertIs(locks.lock(f1, locks.LOCK_SH), True)
|
|
|
|
self.assertIs(locks.lock(f2, locks.LOCK_SH | locks.LOCK_NB), True)
|
|
|
|
self.assertIs(locks.unlock(f1), True)
|
|
|
|
self.assertIs(locks.unlock(f2), True)
|
|
|
|
|
2013-06-20 18:55:27 +08:00
|
|
|
|
2013-10-28 04:26:51 +08:00
|
|
|
class NoNameFileTestCase(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Other examples of unnamed files may be tempfile.SpooledTemporaryFile or
|
|
|
|
urllib.urlopen()
|
|
|
|
"""
|
|
|
|
def test_noname_file_default_name(self):
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(File(BytesIO(b'A file with no name')).name)
|
2013-10-28 04:26:51 +08:00
|
|
|
|
|
|
|
def test_noname_file_get_size(self):
|
|
|
|
self.assertEqual(File(BytesIO(b'A file with no name')).size, 19)
|
|
|
|
|
|
|
|
|
|
|
|
class ContentFileTestCase(unittest.TestCase):
|
|
|
|
def test_content_file_default_name(self):
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(ContentFile(b"content").name)
|
2013-10-28 04:26:51 +08:00
|
|
|
|
|
|
|
def test_content_file_custom_name(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The constructor of ContentFile accepts 'name' (#16590).
|
2013-10-28 04:26:51 +08:00
|
|
|
"""
|
|
|
|
name = "I can have a name too!"
|
|
|
|
self.assertEqual(ContentFile(b"content", name=name).name, name)
|
|
|
|
|
|
|
|
def test_content_file_input_type(self):
|
|
|
|
"""
|
2016-12-29 23:27:49 +08:00
|
|
|
ContentFile can accept both bytes and strings and the retrieved content
|
|
|
|
is of the same type.
|
2013-10-28 04:26:51 +08:00
|
|
|
"""
|
|
|
|
self.assertIsInstance(ContentFile(b"content").read(), bytes)
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(ContentFile("español").read(), str)
|
2013-10-28 04:26:51 +08:00
|
|
|
|
2017-04-07 20:21:06 +08:00
|
|
|
def test_open_resets_file_to_start_and_returns_context_manager(self):
|
|
|
|
file = ContentFile(b'content')
|
|
|
|
with file.open() as f:
|
|
|
|
self.assertEqual(f.read(), b'content')
|
|
|
|
with file.open() as f:
|
|
|
|
self.assertEqual(f.read(), b'content')
|
|
|
|
|
2018-03-08 05:20:25 +08:00
|
|
|
def test_size_changing_after_writing(self):
|
|
|
|
"""ContentFile.size changes after a write()."""
|
|
|
|
f = ContentFile('')
|
|
|
|
self.assertEqual(f.size, 0)
|
|
|
|
f.write('Test ')
|
|
|
|
f.write('string')
|
|
|
|
self.assertEqual(f.size, 11)
|
|
|
|
with f.open() as fh:
|
|
|
|
self.assertEqual(fh.read(), 'Test string')
|
|
|
|
|
2017-04-07 20:21:06 +08:00
|
|
|
|
|
|
|
class InMemoryUploadedFileTests(unittest.TestCase):
|
|
|
|
def test_open_resets_file_to_start_and_returns_context_manager(self):
|
|
|
|
uf = InMemoryUploadedFile(StringIO('1'), '', 'test', 'text/plain', 1, 'utf8')
|
|
|
|
uf.read()
|
|
|
|
with uf.open() as f:
|
|
|
|
self.assertEqual(f.read(), '1')
|
|
|
|
|
2013-10-28 04:26:51 +08:00
|
|
|
|
2017-06-05 06:05:50 +08:00
|
|
|
class TemporaryUploadedFileTests(unittest.TestCase):
|
|
|
|
def test_extension_kept(self):
|
|
|
|
"""The temporary file name has the same suffix as the original file."""
|
|
|
|
with TemporaryUploadedFile('test.txt', 'text/plain', 1, 'utf8') as temp_file:
|
|
|
|
self.assertTrue(temp_file.file.name.endswith('.upload.txt'))
|
|
|
|
|
2019-11-06 15:20:23 +08:00
|
|
|
def test_file_upload_temp_dir_pathlib(self):
|
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
|
|
|
with override_settings(FILE_UPLOAD_TEMP_DIR=Path(tmp_dir)):
|
|
|
|
with TemporaryUploadedFile('test.txt', 'text/plain', 1, 'utf-8') as temp_file:
|
|
|
|
self.assertTrue(os.path.exists(temp_file.file.name))
|
|
|
|
|
2017-06-05 06:05:50 +08:00
|
|
|
|
2013-10-28 04:26:51 +08:00
|
|
|
class DimensionClosingBug(unittest.TestCase):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
get_image_dimensions() properly closes files (#8817)
|
2013-10-28 04:26:51 +08:00
|
|
|
"""
|
2014-03-21 22:54:53 +08:00
|
|
|
@unittest.skipUnless(Image, "Pillow not installed")
|
2013-10-28 04:26:51 +08:00
|
|
|
def test_not_closing_of_files(self):
|
|
|
|
"""
|
|
|
|
Open files passed into get_image_dimensions() should stay opened.
|
|
|
|
"""
|
|
|
|
empty_io = BytesIO()
|
|
|
|
try:
|
|
|
|
images.get_image_dimensions(empty_io)
|
|
|
|
finally:
|
|
|
|
self.assertTrue(not empty_io.closed)
|
|
|
|
|
2014-03-21 22:54:53 +08:00
|
|
|
@unittest.skipUnless(Image, "Pillow not installed")
|
2013-10-28 04:26:51 +08:00
|
|
|
def test_closing_of_filenames(self):
|
|
|
|
"""
|
|
|
|
get_image_dimensions() called with a filename should closed the file.
|
|
|
|
"""
|
|
|
|
# We need to inject a modified open() builtin into the images module
|
|
|
|
# that checks if the file was closed properly if the function is
|
2017-11-07 11:11:39 +08:00
|
|
|
# called with a filename instead of a file object.
|
2013-10-28 04:26:51 +08:00
|
|
|
# get_image_dimensions will call our catching_open instead of the
|
|
|
|
# regular builtin one.
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class FileWrapper:
|
2013-10-28 04:26:51 +08:00
|
|
|
_closed = []
|
|
|
|
|
|
|
|
def __init__(self, f):
|
|
|
|
self.f = f
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return getattr(self.f, name)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self._closed.append(True)
|
|
|
|
self.f.close()
|
|
|
|
|
|
|
|
def catching_open(*args):
|
|
|
|
return FileWrapper(open(*args))
|
|
|
|
|
|
|
|
images.open = catching_open
|
|
|
|
try:
|
2017-01-20 21:01:02 +08:00
|
|
|
images.get_image_dimensions(os.path.join(os.path.dirname(__file__), "test1.png"))
|
2013-10-28 04:26:51 +08:00
|
|
|
finally:
|
|
|
|
del images.open
|
|
|
|
self.assertTrue(FileWrapper._closed)
|
|
|
|
|
|
|
|
|
|
|
|
class InconsistentGetImageDimensionsBug(unittest.TestCase):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
get_image_dimensions() works properly after various calls
|
2013-10-28 04:26:51 +08:00
|
|
|
using a file handler (#11158)
|
|
|
|
"""
|
2014-03-21 22:54:53 +08:00
|
|
|
@unittest.skipUnless(Image, "Pillow not installed")
|
2013-10-28 04:26:51 +08:00
|
|
|
def test_multiple_calls(self):
|
|
|
|
"""
|
|
|
|
Multiple calls of get_image_dimensions() should return the same size.
|
|
|
|
"""
|
2017-01-20 21:01:02 +08:00
|
|
|
img_path = os.path.join(os.path.dirname(__file__), "test.png")
|
2014-01-03 04:36:15 +08:00
|
|
|
with open(img_path, 'rb') as fh:
|
|
|
|
image = images.ImageFile(fh)
|
|
|
|
image_pil = Image.open(fh)
|
2013-10-28 04:26:51 +08:00
|
|
|
size_1 = images.get_image_dimensions(image)
|
|
|
|
size_2 = images.get_image_dimensions(image)
|
|
|
|
self.assertEqual(image_pil.size, size_1)
|
|
|
|
self.assertEqual(size_1, size_2)
|
|
|
|
|
2014-03-21 22:54:53 +08:00
|
|
|
@unittest.skipUnless(Image, "Pillow not installed")
|
2013-10-28 04:26:51 +08:00
|
|
|
def test_bug_19457(self):
|
|
|
|
"""
|
|
|
|
Regression test for #19457
|
|
|
|
get_image_dimensions fails on some pngs, while Image.size is working good on them
|
|
|
|
"""
|
2017-01-20 21:01:02 +08:00
|
|
|
img_path = os.path.join(os.path.dirname(__file__), "magic.png")
|
2015-06-05 15:32:29 +08:00
|
|
|
size = images.get_image_dimensions(img_path)
|
2014-01-03 04:36:15 +08:00
|
|
|
with open(img_path, 'rb') as fh:
|
|
|
|
self.assertEqual(size, Image.open(fh).size)
|
2013-10-28 04:26:51 +08:00
|
|
|
|
|
|
|
|
2015-03-28 03:54:14 +08:00
|
|
|
@unittest.skipUnless(Image, "Pillow not installed")
|
|
|
|
class GetImageDimensionsTests(unittest.TestCase):
|
|
|
|
|
2015-03-22 21:46:38 +08:00
|
|
|
def test_invalid_image(self):
|
|
|
|
"""
|
|
|
|
get_image_dimensions() should return (None, None) for the dimensions of
|
|
|
|
invalid images (#24441).
|
|
|
|
|
|
|
|
brokenimg.png is not a valid image and it has been generated by:
|
|
|
|
$ echo "123" > brokenimg.png
|
|
|
|
"""
|
2017-01-20 21:01:02 +08:00
|
|
|
img_path = os.path.join(os.path.dirname(__file__), "brokenimg.png")
|
2015-03-22 21:46:38 +08:00
|
|
|
with open(img_path, 'rb') as fh:
|
|
|
|
size = images.get_image_dimensions(fh)
|
|
|
|
self.assertEqual(size, (None, None))
|
|
|
|
|
2015-03-28 03:54:14 +08:00
|
|
|
def test_valid_image(self):
|
|
|
|
"""
|
|
|
|
get_image_dimensions() should catch struct.error while feeding the PIL
|
|
|
|
Image parser (#24544).
|
|
|
|
|
|
|
|
Emulates the Parser feed error. Since the error is raised on every feed
|
|
|
|
attempt, the resulting image size should be invalid: (None, None).
|
|
|
|
"""
|
2017-01-20 21:01:02 +08:00
|
|
|
img_path = os.path.join(os.path.dirname(__file__), "test.png")
|
2015-03-28 03:54:14 +08:00
|
|
|
with mock.patch('PIL.ImageFile.Parser.feed', side_effect=struct.error):
|
|
|
|
with open(img_path, 'rb') as fh:
|
|
|
|
size = images.get_image_dimensions(fh)
|
|
|
|
self.assertEqual(size, (None, None))
|
|
|
|
|
2021-05-24 23:10:00 +08:00
|
|
|
def test_missing_file(self):
|
|
|
|
size = images.get_image_dimensions('missing.png')
|
|
|
|
self.assertEqual(size, (None, None))
|
|
|
|
|
2020-10-06 17:25:04 +08:00
|
|
|
@unittest.skipUnless(HAS_WEBP, 'WEBP not installed')
|
2018-08-22 19:34:51 +08:00
|
|
|
def test_webp(self):
|
|
|
|
img_path = os.path.join(os.path.dirname(__file__), 'test.webp')
|
|
|
|
with open(img_path, 'rb') as fh:
|
|
|
|
size = images.get_image_dimensions(fh)
|
|
|
|
self.assertEqual(size, (540, 405))
|
|
|
|
|
2015-03-22 21:46:38 +08:00
|
|
|
|
2013-06-20 18:55:27 +08:00
|
|
|
class FileMoveSafeTests(unittest.TestCase):
|
|
|
|
def test_file_move_overwrite(self):
|
2015-02-22 01:56:36 +08:00
|
|
|
handle_a, self.file_a = tempfile.mkstemp()
|
|
|
|
handle_b, self.file_b = tempfile.mkstemp()
|
2013-06-20 18:55:27 +08:00
|
|
|
|
2019-01-28 23:01:35 +08:00
|
|
|
# file_move_safe() raises OSError if the destination file exists and
|
|
|
|
# allow_overwrite is False.
|
|
|
|
with self.assertRaises(FileExistsError):
|
2016-01-17 19:26:39 +08:00
|
|
|
file_move_safe(self.file_a, self.file_b, allow_overwrite=False)
|
2013-06-20 18:55:27 +08:00
|
|
|
|
|
|
|
# should allow it and continue on if allow_overwrite is True
|
|
|
|
self.assertIsNone(file_move_safe(self.file_a, self.file_b, allow_overwrite=True))
|
2013-09-20 05:02:49 +08:00
|
|
|
|
|
|
|
os.close(handle_a)
|
|
|
|
os.close(handle_b)
|
2014-03-21 22:57:18 +08:00
|
|
|
|
2017-05-10 23:29:12 +08:00
|
|
|
def test_file_move_copystat_cifs(self):
|
|
|
|
"""
|
|
|
|
file_move_safe() ignores a copystat() EPERM PermissionError. This
|
|
|
|
happens when the destination filesystem is CIFS, for example.
|
|
|
|
"""
|
|
|
|
copystat_EACCES_error = PermissionError(errno.EACCES, 'msg')
|
|
|
|
copystat_EPERM_error = PermissionError(errno.EPERM, 'msg')
|
|
|
|
handle_a, self.file_a = tempfile.mkstemp()
|
|
|
|
handle_b, self.file_b = tempfile.mkstemp()
|
|
|
|
try:
|
|
|
|
# This exception is required to reach the copystat() call in
|
|
|
|
# file_safe_move().
|
|
|
|
with mock.patch('django.core.files.move.os.rename', side_effect=OSError()):
|
|
|
|
# An error besides EPERM isn't ignored.
|
|
|
|
with mock.patch('django.core.files.move.copystat', side_effect=copystat_EACCES_error):
|
|
|
|
with self.assertRaises(PermissionError):
|
|
|
|
file_move_safe(self.file_a, self.file_b, allow_overwrite=True)
|
|
|
|
# EPERM is ignored.
|
|
|
|
with mock.patch('django.core.files.move.copystat', side_effect=copystat_EPERM_error):
|
|
|
|
self.assertIsNone(file_move_safe(self.file_a, self.file_b, allow_overwrite=True))
|
|
|
|
finally:
|
|
|
|
os.close(handle_a)
|
|
|
|
os.close(handle_b)
|
|
|
|
|
2014-03-21 22:57:18 +08:00
|
|
|
|
|
|
|
class SpooledTempTests(unittest.TestCase):
|
|
|
|
def test_in_memory_spooled_temp(self):
|
|
|
|
with tempfile.SpooledTemporaryFile() as temp:
|
|
|
|
temp.write(b"foo bar baz quux\n")
|
|
|
|
django_file = File(temp, name="something.txt")
|
|
|
|
self.assertEqual(django_file.size, 17)
|
|
|
|
|
|
|
|
def test_written_spooled_temp(self):
|
|
|
|
with tempfile.SpooledTemporaryFile(max_size=4) as temp:
|
|
|
|
temp.write(b"foo bar baz quux\n")
|
|
|
|
django_file = File(temp, name="something.txt")
|
|
|
|
self.assertEqual(django_file.size, 17)
|