Skip to content

Commit

Permalink
fix header image extension check (#231)
Browse files Browse the repository at this point in the history
Co-authored-by: gounux <[email protected]>
  • Loading branch information
gounux and gounux authored Jul 7, 2024
1 parent 3ca5763 commit 92bb1e9
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 4 deletions.
11 changes: 7 additions & 4 deletions geotribu_cli/content/header_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def check_image_size(


def check_image_ratio(
image_url: str, images: dict, min_ratio: int, max_ratio: int
image_url: str, images: dict, min_ratio: float, max_ratio: float
) -> bool:
key = image_url.replace(f"{defaults_settings.cdn_base_url}img/", "")
if key not in images:
Expand All @@ -163,7 +163,7 @@ def check_image_extension(
allowed_extensions: tuple[str] = defaults_settings.images_header_extensions,
) -> bool:
ext = image_url.split(".")[-1]
return ext in allowed_extensions
return f".{ext}" in allowed_extensions


def get_existing_tags() -> list[str]:
Expand Down Expand Up @@ -226,6 +226,9 @@ def run(args: argparse.Namespace) -> None:
logger.debug(f"Running {args.command} with {args}")
content_paths: list[Path] = args.content_path

# fetch image sizes dict once before processing
image_sizes = download_image_sizes()

for content_path in content_paths:
logger.info(f"Checking header of {content_path}")
check_path(
Expand All @@ -249,7 +252,7 @@ def run(args: argparse.Namespace) -> None:
# check image max size
if not check_image_size(
yaml_meta["image"],
download_image_sizes(),
image_sizes,
args.max_image_width,
args.max_image_height,
):
Expand All @@ -267,7 +270,7 @@ def run(args: argparse.Namespace) -> None:
# check image max ratio
if not check_image_ratio(
yaml_meta["image"],
download_image_sizes(),
image_sizes,
args.min_image_ratio,
args.max_image_ratio,
):
Expand Down
102 changes: 102 additions & 0 deletions tests/test_yaml_header_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@
from geotribu_cli.content.header_check import (
check_author_md,
check_existing_tags,
check_image_extension,
check_image_ratio,
check_image_size,
check_license,
check_missing_mandatory_keys,
check_tags_order,
download_image_sizes,
)

# -- GLOBALS
TEAM_FOLDER = Path("tests/fixtures/team")
URL_TEST_VERTICAL_IMAGE = "https://cdn.geotribu.fr/img/articles-blog-rdp/articles/2024/mise_en_place_qfieldcloud_custom/screenshot_qfield_qfc_project.webp"
URL_TEST_HORIZONTAL_IMAGE = "https://cdn.geotribu.fr/img/articles-blog-rdp/capture-ecran/carte_trains_europe.png"


class TestYamlHeaderCheck(unittest.TestCase):
image_sizes = download_image_sizes()

def setUp(self):
with open("tests/fixtures/content/2012-12-21_article_passe.md") as past_file:
past_content = past_file.read()
Expand Down Expand Up @@ -84,3 +92,97 @@ def test_license_ok(self):

def test_license_nok(self):
self.assertFalse(check_license(self.future_yaml_meta["license"]))

def test_image_extension_ok(self):
self.assertTrue(check_image_extension("https://mon.image.png"))
self.assertTrue(check_image_extension("https://mon.image.jpg"))
self.assertTrue(check_image_extension("https://mon.image.jpeg"))

def test_image_extension_nok(self):
self.assertFalse(check_image_extension("https://mon.image.webp"))
self.assertFalse(check_image_extension("https://mon.image.gif"))
self.assertFalse(check_image_extension("https://mon.image.tiff"))

def test_image_size_ok(self):
self.assertTrue(
check_image_size(
URL_TEST_VERTICAL_IMAGE, self.image_sizes, max_width=800, max_height=800
)
)
self.assertTrue(
check_image_size(
URL_TEST_HORIZONTAL_IMAGE,
self.image_sizes,
max_width=800,
max_height=800,
)
)

def test_image_size_nok(self):
self.assertFalse(
check_image_size(
URL_TEST_VERTICAL_IMAGE, self.image_sizes, max_width=380, max_height=800
)
)
self.assertFalse(
check_image_size(
URL_TEST_VERTICAL_IMAGE, self.image_sizes, max_width=800, max_height=799
)
)
self.assertFalse(
check_image_size(
URL_TEST_HORIZONTAL_IMAGE,
self.image_sizes,
max_width=799,
max_height=800,
)
)
self.assertFalse(
check_image_size(
URL_TEST_HORIZONTAL_IMAGE,
self.image_sizes,
max_width=800,
max_height=532,
)
)

def test_image_ratio_ok(self):
self.assertTrue(
check_image_ratio(
URL_TEST_VERTICAL_IMAGE, self.image_sizes, min_ratio=0.45, max_ratio=0.5
)
)
self.assertTrue(
check_image_ratio(
URL_TEST_HORIZONTAL_IMAGE,
self.image_sizes,
min_ratio=1.45,
max_ratio=1.55,
)
)
self.assertTrue(
check_image_ratio(
URL_TEST_HORIZONTAL_IMAGE,
self.image_sizes,
min_ratio=1.49,
max_ratio=1.51,
)
)

def test_image_ratio_nok(self):
self.assertFalse(
check_image_ratio(
URL_TEST_VERTICAL_IMAGE,
self.image_sizes,
min_ratio=1.45,
max_ratio=1.55,
)
)
self.assertFalse(
check_image_ratio(
URL_TEST_HORIZONTAL_IMAGE,
self.image_sizes,
min_ratio=1.2,
max_ratio=1.3,
)
)

0 comments on commit 92bb1e9

Please sign in to comment.