Пример #1
0
    def test_image_url(self):
        """Test image url generation."""
        impr_instance = ImageProcess('simple.jpg')
        img_name = impr_instance.filename
        ref_url = f'http://127.0.0.1:8000/media/uploads/{img_name}'

        tested_url = impr_instance.generate_img_url()

        self.assertEqual(tested_url, ref_url)
Пример #2
0
    def get_image_data(self) -> Sequence[Tuple[Dict, str]]:
        """Parse image data from request."""
        image = self.request.FILES['upload']
        filename = self.request.FILES['upload'].name

        self.impr_instance = ImageProcess(filename)
        image.name = self.impr_instance.filename

        return {'image': image}
Пример #3
0
    def case_check_image(self, img_name: str, expected_result: str):
        path = UnitTests.path_to_test_img(img_name)

        with patch(
            'editor.editor.tests.ImageProcess.generate_path',
            return_value=path
        ):
            instance = ImageProcess(img_name)
            actual_result = instance.check_image()
            instance.generate_path.assert_called()

        self.assertEqual(expected_result, actual_result)
Пример #4
0
    def file_cleanup(self, response: dict):
        """Parse filename from the received url and remove the file.

        Needed in order to cleanup uploads folder from the test images.
        Must be called after every tests, that are saving correct images.
        """
        if 'url' in response:
            filename = URLUtils.get_filename_from_url(response['url'])
            path = ImageProcess.generate_path(filename)
            os.remove(path)
Пример #5
0
 def test_image_is_deleted_after_failed_upload(self):
     with patch(
         'editor.editor.tests.ImageProcess.get_unique_filename',
         return_value='wrong_file.ext'
     ):
         self.open_image_and_post_it('icon.svg')
     
     file_exists = os.path.isfile(
         ImageProcess.generate_path('wrong_file.ext')
     )
     self.assertFalse(file_exists)
Пример #6
0
    def test_filepath_check(self):
        img_name = 'image.jpg'

        # We are testing only path to the file
        # So we don't need the filename
        # Which is set by random with py-nanoid lib
        ref_path = os.path.join(settings.UPLOAD_ROOT, img_name)
        ref_path = os.path.split(ref_path)[0]

        tested_path = ImageProcess.generate_path(img_name)
        tested_path = os.path.split(tested_path)[0]

        self.assertEqual(tested_path, ref_path)
Пример #7
0
class ImageUpload():
    def __init__(self, request):
        self.request = request
        self.impr_instance = None

    def get_image_data(self) -> Sequence[Tuple[Dict, str]]:
        """Parse image data from request."""
        image = self.request.FILES['upload']
        filename = self.request.FILES['upload'].name

        self.impr_instance = ImageProcess(filename)
        image.name = self.impr_instance.filename

        return {'image': image}

    def get_response(self) -> JsonResponse:
        """Check image and get corresponding response."""
        img_status = self.impr_instance.check_image()
        if img_status == StatusMessages.OK:
            result_url = self.impr_instance.generate_img_url()
            return JsonResponse({'url': result_url})
        else:
            return JsonResponse({'error': {'message': img_status}}, status=415)

    def process_images(self) -> JsonResponse:
        image_data = self.get_image_data()

        form = ImageForm(self.request.POST, image_data)
        if form.is_valid():
            form.save()
            return self.get_response()

        # Add function to cleanup form data from server
        # self.impr_instance.remove_image()

        return JsonResponse(
            {'error': {
                'message': 'Failed to load image file'
            }}, status=400)
Пример #8
0
    def case_filename_check(self, filename: str):
        """Raise AssertException if the filename is not correct.
        
        Example of the filename param:
        'simple.jpg'

        Correct filename would be:
        '2ZxeY-Fqr_TZBx0.jpg'
        """
        img_name = ImageProcess(filename).filename

        ext = UnitTests.get_extension(img_name)
        match = UnitTests.fullmatch_filename(img_name, ext)
        self.assertNotEqual(match, None, f'Result is: {img_name}')
Пример #9
0
    def test_image_exists_after_successful_upload(self):
        # Patch is used because image name is generated by random.
        # But we must be able to test, whether new image file exists or not.
        with patch(
            'editor.editor.tests.ImageProcess.get_unique_filename',
            return_value='correct_file.ext'
        ):
            response = self.open_image_and_post_it('image.gif')
        
        file_exists = os.path.isfile(
            ImageProcess.generate_path('correct_file.ext')
        )
        self.assertTrue(file_exists)

        self.file_cleanup(response)