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)
예제 #2
0
 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)
예제 #3
0
    def validate(self, attrs):

        default_error_messages = {
            'Image Provided is Invalid':
            'Please Upload a valid image. The file you uploaded was either not an image'
        }

        for i in self.initial_data.getlist('image'):

            django_field = DjangoImageField()
            django_field.error_messages = default_error_messages
            django_field.clean(i)
        return attrs
예제 #4
0
    def validate(self, attrs):
    
        default_error_messages = {
            'invalid_image':
            'Upload a valid image. The file you uploaded was either not an image'
        }

        for i in self.initial_data.getlist('image'):
            print('this is imgData', i)
            django_field = DjangoImageField()
            django_field.error_messages = default_error_messages
            django_field.clean(i)
        return attrs
예제 #5
0
 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)
예제 #6
0
    def test_imagefield_annotate_with_image_after_clean(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.png', img_data)
        img_file.content_type = 'text/plain'

        uploaded_file = f.clean(img_file)

        self.assertEqual('PNG', uploaded_file.image.format)
        self.assertEqual('image/png', uploaded_file.content_type)
예제 #7
0
    def test_imagefield_annotate_with_image_after_clean(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.png', img_data)
        img_file.content_type = 'text/plain'

        uploaded_file = f.clean(img_file)

        self.assertEqual('PNG', uploaded_file.image.format)
        self.assertEqual('image/png', uploaded_file.content_type)
예제 #8
0
    def test_imagefield_annotate_with_image_after_clean(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.png", img_data)
        img_file.content_type = "text/plain"

        uploaded_file = f.clean(img_file)

        self.assertEqual("PNG", uploaded_file.image.format)
        self.assertEqual("image/png", uploaded_file.content_type)
예제 #9
0
    def test_imagefield_annotate_with_image_after_clean(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.png", img_data)
        img_file.content_type = "text/plain"

        uploaded_file = f.clean(img_file)

        self.assertEqual("PNG", uploaded_file.image.format)
        self.assertEqual("image/png", uploaded_file.content_type)
예제 #10
0
    def test_imagefield_annotate_with_bitmap_image_after_clean(self):
        """
        This also tests the situation when Pillow doesn't detect the MIME type
        of the image (#24948).
        """
        from PIL.BmpImagePlugin import BmpImageFile
        try:
            Image.register_mime(BmpImageFile.format, None)
            f = ImageField()
            img_path = get_img_path('filepath_test_files/1x1.bmp')
            with open(img_path, 'rb') as img_file:
                img_data = img_file.read()

            img_file = SimpleUploadedFile('1x1.bmp', img_data)
            img_file.content_type = 'text/plain'

            uploaded_file = f.clean(img_file)

            self.assertEqual('BMP', uploaded_file.image.format)
            self.assertIsNone(uploaded_file.content_type)
        finally:
            Image.register_mime(BmpImageFile.format, 'image/bmp')
예제 #11
0
    def test_imagefield_annotate_with_bitmap_image_after_clean(self):
        """
        This also tests the situation when Pillow doesn't detect the MIME type
        of the image (#24948).
        """
        from PIL.BmpImagePlugin import BmpImageFile
        try:
            Image.register_mime(BmpImageFile.format, None)
            f = ImageField()
            img_path = get_img_path('filepath_test_files/1x1.bmp')
            with open(img_path, 'rb') as img_file:
                img_data = img_file.read()

            img_file = SimpleUploadedFile('1x1.bmp', img_data)
            img_file.content_type = 'text/plain'

            uploaded_file = f.clean(img_file)

            self.assertEqual('BMP', uploaded_file.image.format)
            self.assertIsNone(uploaded_file.content_type)
        finally:
            Image.register_mime(BmpImageFile.format, 'image/bmp')