diff options
| author | John Parton <john.parton.iv@gmail.com> | 2024-01-24 16:22:07 -0600 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-05-22 00:25:56 +0200 |
| commit | 9c5fe93349bd4339c41d057b87046e5d28be6f77 (patch) | |
| tree | df348b1d2dff195ff9f133039ba511d82c802f6f /tests/model_fields | |
| parent | 4971a9afe5642569f3dcfcd3972ebb39e88dd457 (diff) | |
Fixed #35139 -- Prevented file read after ImageField is saved to storage.
Diffstat (limited to 'tests/model_fields')
| -rw-r--r-- | tests/model_fields/models.py | 17 | ||||
| -rw-r--r-- | tests/model_fields/storage.py | 6 | ||||
| -rw-r--r-- | tests/model_fields/test_imagefield.py | 28 |
3 files changed, 50 insertions, 1 deletions
diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py index e34f3c8947..18a6fdbc1c 100644 --- a/tests/model_fields/models.py +++ b/tests/model_fields/models.py @@ -13,6 +13,8 @@ from django.db.models.functions import Lower from django.utils.functional import SimpleLazyObject from django.utils.translation import gettext_lazy as _ +from .storage import NoReadFileSystemStorage + try: from PIL import Image except ImportError: @@ -373,6 +375,21 @@ if Image: width_field="headshot_width", ) + class PersonNoReadImage(models.Model): + """ + Model that defines an ImageField with a storage backend that does not + support reading. + """ + + mugshot = models.ImageField( + upload_to="tests", + storage=NoReadFileSystemStorage(), + width_field="mugshot_width", + height_field="mugshot_height", + ) + mugshot_width = models.IntegerField() + mugshot_height = models.IntegerField() + class CustomJSONDecoder(json.JSONDecoder): def __init__(self, object_hook=None, *args, **kwargs): diff --git a/tests/model_fields/storage.py b/tests/model_fields/storage.py new file mode 100644 index 0000000000..9002c12683 --- /dev/null +++ b/tests/model_fields/storage.py @@ -0,0 +1,6 @@ +from django.core.files.storage.filesystem import FileSystemStorage + + +class NoReadFileSystemStorage(FileSystemStorage): + def open(self, *args, **kwargs): + raise AssertionError("This storage class does not support reading.") diff --git a/tests/model_fields/test_imagefield.py b/tests/model_fields/test_imagefield.py index 8c93ed1bdb..7265da598b 100644 --- a/tests/model_fields/test_imagefield.py +++ b/tests/model_fields/test_imagefield.py @@ -18,6 +18,7 @@ if Image: from .models import ( Person, PersonDimensionsFirst, + PersonNoReadImage, PersonTwoImages, PersonWithHeight, PersonWithHeightAndWidth, @@ -30,7 +31,7 @@ else: pass PersonWithHeight = PersonWithHeightAndWidth = PersonDimensionsFirst = Person - PersonTwoImages = Person + PersonTwoImages = PersonNoReadImage = Person class ImageFieldTestMixin(SerializeMixin): @@ -469,3 +470,28 @@ class TwoImageFieldTests(ImageFieldTestMixin, TestCase): # Dimensions were recalculated, and hence file should have opened. self.assertIs(p.mugshot.was_opened, True) self.assertIs(p.headshot.was_opened, True) + + +@skipIf(Image is None, "Pillow is required to test ImageField") +class NoReadTests(ImageFieldTestMixin, TestCase): + def test_width_height_correct_name_mangling_correct(self): + instance1 = PersonNoReadImage() + + instance1.mugshot.save("mug", self.file1) + + self.assertEqual(instance1.mugshot_width, 4) + self.assertEqual(instance1.mugshot_height, 8) + + instance1.save() + + self.assertEqual(instance1.mugshot_width, 4) + self.assertEqual(instance1.mugshot_height, 8) + + instance2 = PersonNoReadImage() + instance2.mugshot.save("mug", self.file1) + instance2.save() + + self.assertNotEqual(instance1.mugshot.name, instance2.mugshot.name) + + self.assertEqual(instance1.mugshot_width, instance2.mugshot_width) + self.assertEqual(instance1.mugshot_height, instance2.mugshot_height) |
