Increased test coverage for forms.ImageField.to_python().

This commit is contained in:
David Smith 2020-04-07 21:38:14 +01:00 committed by Mariusz Felisiak
parent 4bbe8261c4
commit 911545da1d
1 changed files with 16 additions and 1 deletions

View File

@ -1,7 +1,9 @@
import os import os
import unittest import unittest
from django.core.files.uploadedfile import SimpleUploadedFile from django.core.files.uploadedfile import (
SimpleUploadedFile, TemporaryUploadedFile,
)
from django.forms import ( from django.forms import (
ClearableFileInput, FileInput, ImageField, ValidationError, Widget, ClearableFileInput, FileInput, ImageField, ValidationError, Widget,
) )
@ -69,6 +71,19 @@ class ImageFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
with self.assertRaisesMessage(ValidationError, 'File extension “txt” is not allowed.'): with self.assertRaisesMessage(ValidationError, 'File extension “txt” is not allowed.'):
f.clean(img_file) f.clean(img_file)
def test_corrupted_image(self):
f = ImageField()
img_file = SimpleUploadedFile('not_an_image.jpg', b'not an image')
msg = (
'Upload a valid image. The file you uploaded was either not an '
'image or a corrupted image.'
)
with self.assertRaisesMessage(ValidationError, msg):
f.clean(img_file)
with TemporaryUploadedFile('not_an_image_tmp.png', 'text/plain', 1, 'utf-8') as tmp_file:
with self.assertRaisesMessage(ValidationError, msg):
f.clean(tmp_file)
def test_widget_attrs_default_accept(self): def test_widget_attrs_default_accept(self):
f = ImageField() f = ImageField()
# Nothing added for non-FileInput widgets. # Nothing added for non-FileInput widgets.