Exemplo n.º 1
0
    def test_input_as_dict(self):
        signed_request = get_fixture("upload_signed_request.json", as_binary=True)
        signed_request = json.loads(signed_request)

        s3 = S3ImageUploader(signed_request)

        self.assertIsInstance(s3.signed_data, dict)
Exemplo n.º 2
0
    def test_get_image_from_src_url(self):
        uri = 'http://httpbin.org/image/png'
        mock_request(uri, method='GET', status=200, fixture='image1.png', include_location=False)

        web_data = image.get_image_from_src(uri)

        local_data = get_fixture('image1.png', as_binary=True)

        self.assertEqual(web_data, local_data)
Exemplo n.º 3
0
    def test_get_image_data_and_filename(self):
        uri = 'http://httpbin.org/image/png'
        mock_request(uri, method='GET', status=200, fixture='image1.png', include_location=False)

        img_data, filename = image.get_image_data_and_filename(uri, 'png.png')
        local_data = get_fixture('image1.png', as_binary=True)

        self.assertEqual(local_data, img_data)
        self.assertEqual(filename, 'png')
Exemplo n.º 4
0
    def test_export_docx_to_html_unique_filename(self):
        mock_request()

        docx_file_path = get_fixture('png_basic_resize_linked_photo.docx')

        signed_request = get_fixture('upload_signed_request.json', as_binary=True)

        kwargs = {
            's3_upload': signed_request,
            'unique_filename': True
        }

        html = self.exporter(docx_file_path, **kwargs).export()

        # we can't check here for the exact html content as the file name is
        # generated dynamically so, we use regexp

        self.assertRegexpMatches(html,
                                 'http://pydocx.s3.amazonaws.com/uploads/pydocx/\d+-image\d+.png')
Exemplo n.º 5
0
    def test_upload_image_invalid_location_header(self):
        mock_request(include_location=False)

        signed_request = get_signed_request()

        s3 = S3ImageUploader(signed_request)

        img_data = get_fixture("image1.png", as_binary=True)

        with self.assertRaisesRegexp(ImageUploadException, "S3 Invalid location header"):
            s3.upload(img_data, "image4.png")
Exemplo n.º 6
0
    def test_export_docx_to_html_rotate_image(self):
        mock_request()

        docx_file_path = get_fixture('rotate_image.docx')

        signed_request = get_fixture('upload_signed_request.json', as_binary=True)

        html_file_content = get_fixture(
            'rotate_image.html',
            as_binary=True
        )

        kwargs = {
            's3_upload': signed_request,
            'unique_filename': False
        }

        html = self.exporter(docx_file_path, **kwargs).export()

        assert_html_equal(html, html_file_content)
Exemplo n.º 7
0
    def test_export_docx_to_html_with_image_upload_to_s3(self):
        mock_request()

        docx_file_path = get_fixture('png_basic_resize_linked_photo.docx')

        signed_request = get_fixture('upload_signed_request.json', as_binary=True)

        html_file_content = get_fixture(
            'png_basic_s3_upload.html',
            as_binary=True
        )

        kwargs = {
            's3_upload': signed_request,
            'unique_filename': False
        }

        html = self.exporter(docx_file_path, **kwargs).export()

        assert_html_equal(html, html_file_content)
Exemplo n.º 8
0
    def test_upload_image(self):
        mock_request()

        signed_request = get_signed_request()

        s3 = S3ImageUploader(signed_request)

        img_data = get_fixture("image1.png", as_binary=True)

        result = s3.upload(img_data, "image2.png", "png")

        self.assertEqual("http://pydocx.s3.amazonaws.com/uploads/pydocx/image2.png", result)
Exemplo n.º 9
0
    def test_upload_image_invalid_response(self):
        mock_request(status=404)

        signed_request = get_signed_request()

        signed_request["url"] = "http://pydocx.s3.amazonaws.com/"

        s3 = S3ImageUploader(signed_request)

        img_data = get_fixture("image1.png", as_binary=True)

        with self.assertRaisesRegexp(ImageUploadException, "S3 Upload Error"):
            s3.upload(img_data, "image1.png")
Exemplo n.º 10
0
    def test_upload_image_invalid_bucket_url(self):
        mock_request(url="http://invalid_bucket.s3.amazonaws.com/", status=403, fixture="s3_invalid_response.xml")

        signed_request = get_signed_request()

        signed_request["url"] = "http://invalid_bucket.s3.amazonaws.com/"

        s3 = S3ImageUploader(signed_request)

        img_data = get_fixture("image1.png", as_binary=True)

        with self.assertRaisesRegexp(ImageUploadException, "S3 NoSuchBucket - The specified bucket does not exist"):
            s3.upload(img_data, "image2.png", "png")
Exemplo n.º 11
0
    def test_upload_image_invalid_signed_request(self):
        mock_request(status=403, fixture="s3_invalid_access_key.xml")

        signed_request = get_signed_request()

        signed_request["AWSAccessKeyId"] += "test"

        s3 = S3ImageUploader(signed_request)

        img_data = get_fixture("image1.png", as_binary=True)

        with self.assertRaisesRegexp(ImageUploadException, "S3 InvalidAccessKeyId "):
            s3.upload(img_data, "image3.png", "png")
Exemplo n.º 12
0
    def test_export_external_image(self):
        mock_request()

        responses.add(responses.GET, 'https://www.google.com/images/srpr/logo11w.png',
                      body=get_fixture('logo11w.png'), status=200, content_type='image/png')

        docx_file_path = get_fixture('external_image.docx')

        html_file_content = get_fixture(
            'external_image.html',
            as_binary=True
        )

        signed_request = get_fixture('upload_signed_request.json', as_binary=True)

        kwargs = {
            's3_upload': signed_request,
            'unique_filename': False
        }

        html = self.exporter(docx_file_path, **kwargs).export()

        assert_html_equal(html_file_content, html)
Exemplo n.º 13
0
    def test_upload_image_missing_url(self):
        mock_request(url="https://pydocx.s3.amazonaws.com/")

        signed_request = get_signed_request()

        # it will use the default url instead
        del signed_request["url"]

        s3 = S3ImageUploader(signed_request)

        img_data = get_fixture("image1.png", as_binary=True)

        result = s3.upload(img_data, "image1.png")

        self.assertEqual("https://pydocx.s3.amazonaws.com/uploads/pydocx/image1.png", result)
Exemplo n.º 14
0
def get_signed_request():
    signed_request = get_fixture("upload_signed_request.json", as_binary=True)
    signed_request = json.loads(signed_request.decode())

    return signed_request
Exemplo n.º 15
0
    def test_get_image_from_src_data(self):
        img_data = get_fixture('image1.data', as_binary=True)
        result = image.get_image_from_src(img_data)

        self.assertEqual(img_data, result)