Skip to content

Commit

Permalink
(PC-31247)[API] feat: make min_height and min_width nullable in `…
Browse files Browse the repository at this point in the history
…check_image`
  • Loading branch information
tcoudray-pass committed Aug 9, 2024
1 parent 6ac22d5 commit f6f9da0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
16 changes: 11 additions & 5 deletions api/src/pcapi/core/offers/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,21 @@ def _natural_size(value: float | str) -> str:


class ImageTooSmall(ImageValidationError):
def __init__(self, min_width: int, min_height: int) -> None:
super().__init__(f"Utilisez une image plus grande (supérieure à {min_width}px par {min_height}px)")
def __init__(self, min_width: int | None, min_height: int | None) -> None:
if min_width and min_height:
super().__init__(f"Utilisez une image plus grande (supérieure à {min_width}px par {min_height}px)")
elif min_width:
super().__init__(f"Utilisez une image plus grande (supérieure à {min_width}px de large)")
elif min_height:
super().__init__(f"Utilisez une image plus grande (supérieure à {min_height}px de haut)")


class ImageTooLarge(ImageValidationError):
def __init__(self, max_width: int | None, max_height: int | None):
if max_width and not max_height:
super().__init__(f"Utilisez une image plus petite (inférieure à {max_width} px de large)")
super().__init__(f"Utilisez une image plus petite (inférieure à {max_width}px de large)")
elif max_height and not max_width:
super().__init__(f"Utilisez une image plus petite (inférieure à {max_height}px de haut)")
super().__init__(f"Utilisez une image plus petite (inférieure à {max_height}px de haut)")
else:
super().__init__(f"Utilisez une image plus petite (inférieure à {max_width}px par {max_height}px)")

Expand Down Expand Up @@ -239,7 +244,8 @@ class CollectiveOfferNotFound(Exception):
pass


class UnapplicableModel(Exception): ...
class UnapplicableModel(Exception):
pass


class UnexpectedCinemaProvider(Exception):
Expand Down
6 changes: 3 additions & 3 deletions api/src/pcapi/core/offers/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ def check_image_size(image_as_bytes: bytes, max_size: int = MAX_THUMBNAIL_SIZE)
def check_image(
image_as_bytes: bytes,
accepted_types: tuple = ACCEPTED_THUMBNAIL_FORMATS,
min_width: int = MIN_THUMBNAIL_WIDTH,
min_height: int = MIN_THUMBNAIL_HEIGHT,
min_width: int | None = MIN_THUMBNAIL_WIDTH,
min_height: int | None = MIN_THUMBNAIL_HEIGHT,
max_width: int | None = None,
max_height: int | None = None,
max_size: int = MAX_THUMBNAIL_SIZE,
Expand All @@ -314,7 +314,7 @@ def check_image(
if image.format.lower() not in accepted_types:
raise exceptions.UnacceptedFileType(accepted_types, image.format)

if image.width < min_width or image.height < min_height:
if min_width is not None and image.width < min_width or min_height is not None and image.height < min_height:
raise exceptions.ImageTooSmall(min_width, min_height)

if max_width is not None and image.width > max_width or max_height is not None and image.height > max_height:
Expand Down
38 changes: 35 additions & 3 deletions api/tests/core/offers/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,19 +356,51 @@ def test_ok_converted_webp(self):
min_height=400,
)

def test_image_too_small(self):
def test_image_too_small_with_width_and_height_constraint(self):
image_as_bytes = (IMAGES_DIR / "mouette_portrait.jpg").read_bytes()
with pytest.raises(exceptions.ImageTooSmall):
with pytest.raises(exceptions.ImageTooSmall) as error:
validation.check_image(
image_as_bytes,
accepted_types=("jpeg", "jpg"),
min_width=400,
min_height=400,
)
assert str(error.value) == "Utilisez une image plus grande (supérieure à 400px par 400px)"

def test_image_too_small_with_width_constraint(self):
image_as_bytes = (IMAGES_DIR / "mouette_portrait.jpg").read_bytes()
with pytest.raises(exceptions.ImageTooSmall) as error:
validation.check_image(
image_as_bytes,
accepted_types=("jpeg", "jpg"),
min_width=400,
min_height=None,
)
assert str(error.value) == "Utilisez une image plus grande (supérieure à 400px de large)"

def test_image_too_small_with_height_constraint(self):
image_as_bytes = (IMAGES_DIR / "mouette_portrait.jpg").read_bytes()
with pytest.raises(exceptions.ImageTooSmall) as error:
validation.check_image(
image_as_bytes,
accepted_types=("jpeg", "jpg"),
min_width=None,
min_height=1000,
)
assert str(error.value) == "Utilisez une image plus grande (supérieure à 1000px de haut)"

def test_image_too_small_with_no_min_constraint_should_not_raise(self):
image_as_bytes = (IMAGES_DIR / "mouette_portrait.jpg").read_bytes()
validation.check_image(
image_as_bytes,
accepted_types=("jpeg", "jpg"),
min_width=None,
min_height=None,
)

def test_fake_jpeg(self):
image_as_bytes = (IMAGES_DIR / "mouette_fake_jpg.jpg").read_bytes()
with pytest.raises(exceptions.ImageValidationError):
with pytest.raises(exceptions.UnidentifiedImage):
validation.check_image(
image_as_bytes,
accepted_types=("jpeg", "jpg"),
Expand Down

0 comments on commit f6f9da0

Please sign in to comment.