Skip to content

Commit

Permalink
add test for unsupported image
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtonpaul committed Sep 2, 2022
1 parent ab32aac commit 59cf89c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion constrainedfilefield/tests/models.py
Expand Up @@ -28,7 +28,11 @@ class TestDocModel(models.Model):


class TestImageModel(models.Model):
the_image = ConstrainedImageField(null=True, blank=True, upload_to="testfile",)
the_image = ConstrainedImageField(
null=True,
blank=True,
upload_to="testfile",
content_types=['image/png'],)
the_image_small = ConstrainedImageField(
null=True,
blank=True,
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions constrainedfilefield/tests/tests/backend.py
Expand Up @@ -224,6 +224,24 @@ def test_form_invalid_filetype(self):
u"Allowed types are ['image/png'].",
)

def test_form_invalid_image_filetype(self):
files = {
"the_image": self._create_simple_uploaded_file(
orig_filename="unsupported_image.jpg",
dest_filename="the_file.jpg",
content_type="image/jpeg",
),
}
form = TestImageModelForm(data={}, files=files)
self.assertFalse(form.is_valid())
self.assertEqual(len(form.errors), 1)
self.assertEqual(len(form.errors["the_image"]), 1)
self.assertEqual(
form.errors["the_image"][0],
u"Unsupported file type: image/jpeg. "
u"Allowed types are ['image/png']."
)

def test_form_invalid_filetype_and_size(self):
form = self._create_bound_test_model_form(
form_class=TestModelForm,
Expand Down

0 comments on commit 59cf89c

Please sign in to comment.