コード例 #1
0
 def test_ok(self):
     image_as_bytes = (IMAGES_DIR / "mouette_full_size.jpg").read_bytes()
     validation.check_image(
         image_as_bytes,
         accepted_types=("jpeg", "jpg"),
         min_width=400,
         min_height=400,
     )
コード例 #2
0
 def test_wrong_format(self):
     image_as_bytes = (IMAGES_DIR / "mouette_full_size.jpg").read_bytes()
     with pytest.raises(exceptions.UnacceptedFileType):
         validation.check_image(
             image_as_bytes,
             accepted_types=("png",),
             min_width=1,
             min_height=1,
         )
コード例 #3
0
 def test_fake_jpeg(self):
     image_as_bytes = (IMAGES_DIR / "mouette_fake_jpg.jpg").read_bytes()
     with pytest.raises(exceptions.UnacceptedFileType):
         validation.check_image(
             image_as_bytes,
             accepted_types=("jpeg", "jpg"),
             min_width=1,
             min_height=1,
         )
コード例 #4
0
 def test_image_too_small(self):
     image_as_bytes = (IMAGES_DIR / "mouette_portrait.jpg").read_bytes()
     with pytest.raises(exceptions.ImageTooSmall):
         validation.check_image(
             image_as_bytes,
             accepted_types=("jpeg", "jpg"),
             min_width=400,
             min_height=400,
         )
コード例 #5
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)