Ejemplo n.º 1
0
 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')
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
 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')
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
0
 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
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 10
0
    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())
Ejemplo n.º 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')
Ejemplo n.º 12
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')
Ejemplo n.º 13
0
 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)
Ejemplo n.º 14
0
 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)