Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor validation IM111 #4255

Merged
merged 15 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changelog/4255.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Refactored validation IM111, Verify the integration image is within the allowed dimensions.
type: feature
pr_number: 4255
4 changes: 2 additions & 2 deletions demisto_sdk/commands/validate/sdk_validation_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ select = [
"CL100",
"GF100", "GF101", "GF102",
"IF100", "IF101", "IF102", "IF103", "IF104", "IF105", "IF106",
"IM100", "IM101", "IM108", "IM109", "IM106",
"IM100", "IM101", "IM108", "IM109", "IM106", "IM111",
"RP101", "BC106", "BC107",
"DA100", "DA101"
]
Expand All @@ -61,7 +61,7 @@ select = [
"CL100",
"GF100", "GF101",
"IF100", "IF101", "IF102", "IF103", "IF104", "IF105", "IF106",
"IM101", "IM108",
"IM101", "IM108", "IM111",
"RP101"
]
warning = ["IN101"]
Expand Down
42 changes: 42 additions & 0 deletions demisto_sdk/commands/validate/tests/IM_validators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
from demisto_sdk.commands.validate.validators.IM_validators.IM109_author_image_exists_validation import (
AuthorImageExistsValidator,
)
from demisto_sdk.commands.validate.validators.IM_validators.IM111_invalid_image_dimensions import (
InvalidImageDimensionsValidator,
)


def test_ImageExistsValidator_is_valid_image_path():
Expand Down Expand Up @@ -104,6 +107,45 @@ def test_ImageTooLargeValidator_is_valid(
)


@pytest.mark.parametrize(
"image_resolution, expected_message",
[
((120, 50), []),
(
(1, 5),
[
"The image dimensions do not match the requirements. A resolution of 120x50 pixels is required."
],
),
(
(1200, 500),
[
"The image dimensions do not match the requirements. A resolution of 120x50 pixels is required."
],
),
],
)
def test_InvalidImageDimensionsValidator_is_valid(
mocker, image_resolution, expected_message
):
mocker.patch(
"demisto_sdk.commands.validate.validators.IM_validators.IM111_invalid_image_dimensions.imagesize.get",
return_value=image_resolution,
)
content_items = [
create_integration_object(paths=["image"], values=["very nice image"])
]

results = InvalidImageDimensionsValidator().is_valid(content_items)

assert all(
[
result.message == expected_msg
for result, expected_msg in zip(results, expected_message)
]
)


def test_AuthorImageExistsValidator_is_valid_image_path():
"""
Given:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class ImageTooLargeValidator(BaseValidator[ContentTypes]):
error_code = "IM101"
description = "Checks that the image file dimensions are matching the requirements."
description = "Checks that the image file size are matching the requirements."
rationale = "Image needs to fit its place in the UI. For more information see: https://xsoar.pan.dev/docs/integrations/integration-logo"
error_message = "You've created/modified a yml or package with a large sized image. Please make sure to change the image dimensions at: {0}."
related_field = "image"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from __future__ import annotations

from typing import Iterable, List

import imagesize

from demisto_sdk.commands.content_graph.objects.integration import Integration
from demisto_sdk.commands.content_graph.parsers.related_files import RelatedFileType
from demisto_sdk.commands.validate.validators.base_validator import (
BaseValidator,
ValidationResult,
)

IMAGE_WIDTH = 120
IMAGE_HEIGHT = 50
ContentTypes = Integration


def is_image_dimensions_valid(content_item: ContentTypes) -> bool:
width, height = imagesize.get(content_item.image.file_path)
return (width, height) == (IMAGE_WIDTH, IMAGE_HEIGHT)


class InvalidImageDimensionsValidator(BaseValidator[ContentTypes]):
error_code = "IM111"
description = "Checks that the image file dimensions are matching the requirements."
rationale = "Image needs to fit its place in the UI. For more information see: https://xsoar.pan.dev/docs/integrations/integration-logo"
error_message = "The image dimensions do not match the requirements. A resolution of 120x50 pixels is required."
related_field = "image"
is_auto_fixable = False
related_file_type = [RelatedFileType.IMAGE]

def is_valid(self, content_items: Iterable[ContentTypes]) -> List[ValidationResult]:
return [
ValidationResult(
validator=self,
message=self.error_message,
content_object=content_item,
)
for content_item in content_items
if not is_image_dimensions_valid(content_item)
]
Loading