test_filefield.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import os
  2. import pickle
  3. import sys
  4. import tempfile
  5. import unittest
  6. from pathlib import Path
  7. from django.core.exceptions import FieldError, SuspiciousFileOperation
  8. from django.core.files import File, temp
  9. from django.core.files.base import ContentFile
  10. from django.core.files.uploadedfile import TemporaryUploadedFile
  11. from django.db import IntegrityError, models
  12. from django.test import TestCase, override_settings
  13. from django.test.utils import isolate_apps
  14. from .models import Document
  15. class FileFieldTests(TestCase):
  16. def test_clearable(self):
  17. """
  18. FileField.save_form_data() will clear its instance attribute value if
  19. passed False.
  20. """
  21. d = Document(myfile="something.txt")
  22. self.assertEqual(d.myfile, "something.txt")
  23. field = d._meta.get_field("myfile")
  24. field.save_form_data(d, False)
  25. self.assertEqual(d.myfile, "")
  26. def test_unchanged(self):
  27. """
  28. FileField.save_form_data() considers None to mean "no change" rather
  29. than "clear".
  30. """
  31. d = Document(myfile="something.txt")
  32. self.assertEqual(d.myfile, "something.txt")
  33. field = d._meta.get_field("myfile")
  34. field.save_form_data(d, None)
  35. self.assertEqual(d.myfile, "something.txt")
  36. def test_changed(self):
  37. """
  38. FileField.save_form_data(), if passed a truthy value, updates its
  39. instance attribute.
  40. """
  41. d = Document(myfile="something.txt")
  42. self.assertEqual(d.myfile, "something.txt")
  43. field = d._meta.get_field("myfile")
  44. field.save_form_data(d, "else.txt")
  45. self.assertEqual(d.myfile, "else.txt")
  46. def test_delete_when_file_unset(self):
  47. """
  48. Calling delete on an unset FileField should not call the file deletion
  49. process, but fail silently (#20660).
  50. """
  51. d = Document()
  52. d.myfile.delete()
  53. def test_refresh_from_db(self):
  54. d = Document.objects.create(myfile="something.txt")
  55. d.refresh_from_db()
  56. self.assertIs(d.myfile.instance, d)
  57. @unittest.skipIf(sys.platform == "win32", "Crashes with OSError on Windows.")
  58. def test_save_without_name(self):
  59. with tempfile.NamedTemporaryFile(suffix=".txt") as tmp:
  60. document = Document.objects.create(myfile="something.txt")
  61. document.myfile = File(tmp)
  62. msg = f"Detected path traversal attempt in '{tmp.name}'"
  63. with self.assertRaisesMessage(SuspiciousFileOperation, msg):
  64. document.save()
  65. def test_save_content_file_without_name(self):
  66. d = Document()
  67. d.myfile = ContentFile(b"")
  68. msg = "File for myfile must have the name attribute specified to be saved."
  69. with self.assertRaisesMessage(FieldError, msg) as cm:
  70. d.save()
  71. self.assertEqual(
  72. cm.exception.__notes__, ["Pass a 'name' argument to ContentFile."]
  73. )
  74. def test_delete_content_file(self):
  75. file = ContentFile(b"", name="foo")
  76. d = Document.objects.create(myfile=file)
  77. d.myfile.delete()
  78. self.assertIsNone(d.myfile.name)
  79. msg = "The 'myfile' attribute has no file associated with it."
  80. with self.assertRaisesMessage(ValueError, msg):
  81. getattr(d.myfile, "file")
  82. def test_defer(self):
  83. Document.objects.create(myfile="something.txt")
  84. self.assertEqual(Document.objects.defer("myfile")[0].myfile, "something.txt")
  85. def test_unique_when_same_filename(self):
  86. """
  87. A FileField with unique=True shouldn't allow two instances with the
  88. same name to be saved.
  89. """
  90. Document.objects.create(myfile="something.txt")
  91. with self.assertRaises(IntegrityError):
  92. Document.objects.create(myfile="something.txt")
  93. @unittest.skipIf(
  94. sys.platform == "win32", "Windows doesn't support moving open files."
  95. )
  96. # The file's source and destination must be on the same filesystem.
  97. @override_settings(MEDIA_ROOT=temp.gettempdir())
  98. def test_move_temporary_file(self):
  99. """
  100. The temporary uploaded file is moved rather than copied to the
  101. destination.
  102. """
  103. with TemporaryUploadedFile(
  104. "something.txt", "text/plain", 0, "UTF-8"
  105. ) as tmp_file:
  106. tmp_file_path = tmp_file.temporary_file_path()
  107. Document.objects.create(myfile=tmp_file)
  108. self.assertFalse(
  109. os.path.exists(tmp_file_path), "Temporary file still exists"
  110. )
  111. def test_open_returns_self(self):
  112. """
  113. FieldField.open() returns self so it can be used as a context manager.
  114. """
  115. d = Document.objects.create(myfile="something.txt")
  116. # Replace the FileField's file with an in-memory ContentFile, so that
  117. # open() doesn't write to disk.
  118. d.myfile.file = ContentFile(b"", name="bla")
  119. self.assertEqual(d.myfile, d.myfile.open())
  120. def test_media_root_pathlib(self):
  121. with tempfile.TemporaryDirectory() as tmp_dir:
  122. with override_settings(MEDIA_ROOT=Path(tmp_dir)):
  123. with TemporaryUploadedFile(
  124. "foo.txt", "text/plain", 1, "utf-8"
  125. ) as tmp_file:
  126. document = Document.objects.create(myfile=tmp_file)
  127. self.assertIs(
  128. document.myfile.storage.exists(
  129. os.path.join("unused", "foo.txt")
  130. ),
  131. True,
  132. )
  133. def test_pickle(self):
  134. with tempfile.TemporaryDirectory() as tmp_dir:
  135. with override_settings(MEDIA_ROOT=Path(tmp_dir)):
  136. with open(__file__, "rb") as fp:
  137. file1 = File(fp, name="test_file.py")
  138. document = Document(myfile="test_file.py")
  139. document.myfile.save("test_file.py", file1)
  140. try:
  141. dump = pickle.dumps(document)
  142. loaded_document = pickle.loads(dump)
  143. self.assertEqual(document.myfile, loaded_document.myfile)
  144. self.assertEqual(
  145. document.myfile.url,
  146. loaded_document.myfile.url,
  147. )
  148. self.assertEqual(
  149. document.myfile.storage,
  150. loaded_document.myfile.storage,
  151. )
  152. self.assertEqual(
  153. document.myfile.instance,
  154. loaded_document.myfile.instance,
  155. )
  156. self.assertEqual(
  157. document.myfile.field,
  158. loaded_document.myfile.field,
  159. )
  160. myfile_dump = pickle.dumps(document.myfile)
  161. loaded_myfile = pickle.loads(myfile_dump)
  162. self.assertEqual(document.myfile, loaded_myfile)
  163. self.assertEqual(document.myfile.url, loaded_myfile.url)
  164. self.assertEqual(
  165. document.myfile.storage,
  166. loaded_myfile.storage,
  167. )
  168. self.assertEqual(
  169. document.myfile.instance,
  170. loaded_myfile.instance,
  171. )
  172. self.assertEqual(document.myfile.field, loaded_myfile.field)
  173. finally:
  174. document.myfile.delete()
  175. @isolate_apps("model_fields")
  176. def test_abstract_filefield_model(self):
  177. """
  178. FileField.model returns the concrete model for fields defined in an
  179. abstract model.
  180. """
  181. class AbstractMyDocument(models.Model):
  182. myfile = models.FileField(upload_to="unused")
  183. class Meta:
  184. abstract = True
  185. class MyDocument(AbstractMyDocument):
  186. pass
  187. document = MyDocument(myfile="test_file.py")
  188. self.assertEqual(document.myfile.field.model, MyDocument)