diff options
| author | Manatsawin Hanmongkolchai <manatsawin+git@gmail.com> | 2017-05-28 14:05:21 +0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-06-01 10:44:47 -0400 |
| commit | 110bd820380e06fc8572f94b36bba6fc9d057a6b (patch) | |
| tree | f76f09265ce2a5883826ad7e4c1719849c74451c /tests | |
| parent | af9a81aa7f14ae1a9fd1f25676f526a43f0c65f3 (diff) | |
[1.11.x] Fixed #28242 -- Moved ImageField file extension validation to the form field.
Backport of a0c07d77fc313388c72a17cd59411265069f037f from master
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/forms_tests/field_tests/test_imagefield.py | 11 | ||||
| -rw-r--r-- | tests/model_fields/test_imagefield.py | 8 | ||||
| -rw-r--r-- | tests/model_forms/models.py | 11 | ||||
| -rw-r--r-- | tests/model_forms/tests.py | 20 |
4 files changed, 41 insertions, 9 deletions
diff --git a/tests/forms_tests/field_tests/test_imagefield.py b/tests/forms_tests/field_tests/test_imagefield.py index ee0e1e3b73..c73d9b70d3 100644 --- a/tests/forms_tests/field_tests/test_imagefield.py +++ b/tests/forms_tests/field_tests/test_imagefield.py @@ -4,7 +4,7 @@ import os import unittest from django.core.files.uploadedfile import SimpleUploadedFile -from django.forms import ImageField +from django.forms import ImageField, ValidationError from django.test import SimpleTestCase from django.utils._os import upath @@ -58,3 +58,12 @@ class ImageFieldTest(SimpleTestCase): self.assertIsNone(uploaded_file.content_type) finally: Image.register_mime(BmpImageFile.format, 'image/bmp') + + def test_file_extension_validation(self): + f = ImageField() + img_path = get_img_path('filepath_test_files/1x1.png') + with open(img_path, 'rb') as img_file: + img_data = img_file.read() + img_file = SimpleUploadedFile('1x1.txt', img_data) + with self.assertRaisesMessage(ValidationError, "File extension 'txt' is not allowed."): + f.clean(img_file) diff --git a/tests/model_fields/test_imagefield.py b/tests/model_fields/test_imagefield.py index e178760d2f..2d636f9610 100644 --- a/tests/model_fields/test_imagefield.py +++ b/tests/model_fields/test_imagefield.py @@ -4,7 +4,7 @@ import os import shutil from unittest import skipIf -from django.core.exceptions import ImproperlyConfigured, ValidationError +from django.core.exceptions import ImproperlyConfigured from django.core.files import File from django.core.files.images import ImageFile from django.test import TestCase @@ -133,12 +133,6 @@ class ImageFieldTests(ImageFieldTestMixin, TestCase): self.assertEqual(hash(p1_db.mugshot), hash(p1.mugshot)) self.assertIs(p1_db.mugshot != p1.mugshot, False) - def test_validation(self): - p = self.PersonModel(name="Joan") - p.mugshot.save("shot.txt", self.file1) - with self.assertRaisesMessage(ValidationError, "File extension 'txt' is not allowed."): - p.full_clean() - def test_instantiate_missing(self): """ If the underlying file is unavailable, still create instantiate the diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py index f873cfea97..42224ac63e 100644 --- a/tests/model_forms/models.py +++ b/tests/model_forms/models.py @@ -214,6 +214,17 @@ try: def __str__(self): return self.description + + class NoExtensionImageFile(models.Model): + def upload_to(self, filename): + return 'tests/no_extension' + + description = models.CharField(max_length=20) + image = models.ImageField(storage=temp_storage, upload_to=upload_to) + + def __str__(self): + return self.description + except ImportError: test_images = False diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 159d0634ea..fe9a153a90 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -36,7 +36,7 @@ from .models import ( ) if test_images: - from .models import ImageFile, OptionalImageFile + from .models import ImageFile, OptionalImageFile, NoExtensionImageFile class ImageFileForm(forms.ModelForm): class Meta: @@ -48,6 +48,11 @@ if test_images: model = OptionalImageFile fields = '__all__' + class NoExtensionImageFileForm(forms.ModelForm): + class Meta: + model = NoExtensionImageFile + fields = '__all__' + class ProductForm(forms.ModelForm): class Meta: @@ -2469,6 +2474,19 @@ class FileAndImageFieldTests(TestCase): self.assertEqual(instance.image.name, 'foo/test4.png') instance.delete() + # Editing an instance that has an image without an extension shouldn't + # fail validation. First create: + f = NoExtensionImageFileForm( + data={'description': 'An image'}, + files={'image': SimpleUploadedFile('test.png', image_data)}, + ) + self.assertTrue(f.is_valid()) + instance = f.save() + self.assertEqual(instance.image.name, 'tests/no_extension') + # Then edit: + f = NoExtensionImageFileForm(data={'description': 'Edited image'}, instance=instance) + self.assertTrue(f.is_valid()) + class ModelOtherFieldTests(SimpleTestCase): def test_big_integer_field(self): |
