예제 #1
0
    def test_content_too_large(self, requests_mock):
        remote_image_url = "https://example.com/image.jpg"
        requests_mock.get(remote_image_url, content=b"1234567890")

        with pytest.raises(exceptions.FileSizeExceeded):
            validation.get_distant_image(
                url=remote_image_url,
                accepted_types=("jpeg", "jpg", "png"),
                max_size=5,
            )
예제 #2
0
    def test_timeout(self, requests_mock):
        remote_image_url = "https://example.com/image.jpg"
        requests_mock.get(remote_image_url, exc=requests.exceptions.ConnectTimeout)

        with pytest.raises(exceptions.FailureToRetrieve):
            validation.get_distant_image(
                url=remote_image_url,
                accepted_types=("jpeg", "jpg"),
                max_size=100000,
            )
예제 #3
0
    def test_content_type_header_not_accepted(self, requests_mock):
        remote_image_url = "https://example.com/image.gif"
        requests_mock.get(
            remote_image_url,
            headers={"content-type": "image/gif", "content-length": "27661"},
        )

        with pytest.raises(exceptions.UnacceptedFileType):
            validation.get_distant_image(
                url=remote_image_url,
                accepted_types=("jpeg", "jpg", "png"),
                max_size=100000,
            )
예제 #4
0
    def test_unaccessible_file(self, requests_mock):
        remote_image_url = "https://example.com/this-goes-nowhere"
        requests_mock.get(
            remote_image_url,
            status_code=404,
        )

        with pytest.raises(exceptions.FailureToRetrieve):
            validation.get_distant_image(
                url=remote_image_url,
                accepted_types=("jpeg", "jpg"),
                max_size=100000,
            )
예제 #5
0
    def test_ok_without_headers(self, requests_mock):
        remote_image_url = "https://example.com/image.jpg"
        requests_mock.get(
            remote_image_url,
            headers={},
            content=b"\xff\xd8\xff\xd9",
        )

        validation.get_distant_image(
            url=remote_image_url,
            accepted_types=("jpeg", "jpg"),
            max_size=100000,
        )
예제 #6
0
    def test_content_length_header_too_large(self, requests_mock):
        remote_image_url = "https://example.com/image.jpg"
        requests_mock.get(
            remote_image_url,
            headers={"content-type": "image/jpeg", "content-length": "2000"},
            content=b"\xff\xd8\xff\xd9",
        )

        with pytest.raises(exceptions.FileSizeExceeded):
            validation.get_distant_image(
                url=remote_image_url,
                accepted_types=("jpeg", "jpg", "png"),
                max_size=1000,
            )
    def get_image_as_bytes(self, request) -> bytes:
        """
        Get the image from the POSTed data (request) or from the form field
        (in which case it's supposed to be an URL that we are going to request.
        Only the max size is checked at this stage, and possibly the content type header
        """
        if "thumb" in request.files:
            blob = request.files["thumb"]
            image_as_bytes = blob.read()
            return validation.get_uploaded_image(image_as_bytes)

        if self.thumb_url:
            return validation.get_distant_image(self.thumb_url)

        raise validation.exceptions.MissingImage
예제 #8
0
def validate_distant_image(body: offers_serialize.ImageBodyModel) -> offers_serialize.ImageResponseModel:
    errors = []

    try:
        image = get_distant_image(body.url)
        check_image(image)
        image_as_base64 = base64.b64encode(image)
        return offers_serialize.ImageResponseModel(
            image=f'data:image/png;base64,{str(image_as_base64, encoding="utf-8")}', errors=errors
        )
    except (
        exceptions.FileSizeExceeded,
        exceptions.ImageTooSmall,
        exceptions.UnacceptedFileType,
        exceptions.FailureToRetrieve,
    ) as exc:
        logger.info("When validating image at: %s, this error was encountered: %s", body.url, exc.__class__.__name__)
        errors.append(exc.args[0])

    return offers_serialize.ImageResponseModel(errors=errors)