def test_calls_get_file_extension_by_content_type(self, mock_func, mock_os): file = SimpleUploadedFile( 'img.png', open(self.get_valid_file(), 'rb').read()) file.content_type = 'image/png' with patch('questionnaire.upload.open') as mock_open: store_file(file) mock_func.assert_called_once_with('image/png')
def create_upload_image(): fixture_path = os.path.join(settings.BASE_DIR, 'emails', 'tests', 'fixtures', 'test.jpg') raw_file = open(fixture_path, 'rb') uploaded_image = SimpleUploadedFile('test.jpg', raw_file.read()) raw_file.close() uploaded_image.content_type = 'image/jpeg' return uploaded_image
def test_calls_get_file_extension_by_content_type(self, mock_func, mock_os): file = SimpleUploadedFile('img.png', open(self.get_valid_file(), 'rb').read()) file.content_type = 'image/png' with patch('questionnaire.upload.open') as mock_open: store_file(file) mock_func.assert_called_once_with('image/png')
def get_uploaded_file_from_path(self, path, quiet=True): try: with open(path, 'rb') as image: file = SimpleUploadedFile(image.name, image.read()) file.content_type = mimetypes.guess_type(path)[0] except IOError: if not quiet: raise file = None return file
def get_uploaded_file_from_path(self, path, quiet=True): try: with open(path, 'rb') as image: file = SimpleUploadedFile(image.name, image.read()) file.content_type = mimetypes.guess_type(path)[0] except IOError: if not quiet: raise file = None return file
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)
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)
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)
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)
def test_sop_submit_form_in_memory_file(self): inmemoryfile = SimpleUploadedFile( 'testfile.docx', b'this is test file') inmemoryfile.content_type = "application/msword" form = SOPSubmitForm( data={ 'name': "alamin", 'email': "*****@*****.**", 'msg': "hi", }, files={'file': inmemoryfile} ) # print(form.errors) print(form.errors) self.assertTrue(form.is_valid())
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')
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')
def test_raises_exception_if_invalid_file_extension(self, mock_os): file = SimpleUploadedFile('img.png', open(self.get_valid_file(), 'rb').read()) file.content_type = 'foo' with self.assertRaises(Exception): store_file(file)
def test_raises_exception_if_invalid_file_extension(self, mock_os): file = SimpleUploadedFile( 'img.png', open(self.get_valid_file(), 'rb').read()) file.content_type = 'foo' with self.assertRaises(Exception): store_file(file)