def test_image_optimization(self):
     instance = IconFactory.build()
     instance.image.file = self._build_in_memory_uploaded_file()
     with patch('snippets.base.models.subprocess.run',
                wraps=subprocess.run) as run_mock:
         instance.clean()
     self.assertTrue(run_mock.called)
 def test_invalid_size(self):
     instance = IconFactory.build()
     instance.image.file = self._build_in_memory_uploaded_file()
     try:
         instance.clean()
     except ValidationError as e:
         self.assertEqual(
             e.message_dict['image'],
             ['Upload an image less than 0 KiB. This image is 0 KiB.'])
 def test_invalid_dimensions(self):
     instance = IconFactory.build()
     instance.image.file = self._build_in_memory_uploaded_file()
     try:
         instance.clean()
     except ValidationError as e:
         self.assertEqual(
             e.message_dict['image'],
             ['Upload an image at most 20x20. This image is 30x30.'])
 def test_invalid_image(self):
     instance = IconFactory.build()
     img = Image.new('RGB', (30, 30), color='red')
     fle = io.BytesIO()
     img.save(fle, 'JPEG')
     fle.seek(0)
     size = len(fle.read())
     fle.seek(0)
     instance.image.file = InMemoryUploadedFile(fle, 'ImageField',
                                                'foo.jpeg', 'image/jpeg',
                                                size, None)
     try:
         instance.clean()
     except ValidationError as e:
         self.assertEqual(e.message_dict['image'],
                          ['Upload only PNG images.'])
 def test_valid_image_png(self):
     instance = IconFactory.build()
     instance.image.file = self._build_in_memory_uploaded_file()
     self.assertIsNone(instance.clean())
 def test_size_test_disabled(self):
     instance = IconFactory.build()
     instance.image.file = self._build_in_memory_uploaded_file()
     self.assertIsNone(instance.clean())
 def test_dont_process_existing_files(self):
     instance = IconFactory.build()
     with patch('snippets.base.models.subprocess.run') as run_mock:
         instance.clean()
     self.assertFalse(run_mock.called)