Skip to content

Commit

Permalink
Merge d30142d into 26c32c4
Browse files Browse the repository at this point in the history
  • Loading branch information
BEAdi committed Apr 1, 2024
2 parents 26c32c4 + d30142d commit ec37a1f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
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 @@ -36,7 +36,7 @@ select = [
"CL100",
"GF100", "GF101", "GF102",
"IF100", "IF101", "IF102", "IF103", "IF104", "IF105", "IF106",
"IM100", "IM108", "IM109", "IM106",
"IM100", "IM101", "IM108", "IM109", "IM106",
"RP101", "BC106", "BC107"
]
warning = []
Expand All @@ -57,7 +57,7 @@ select = [
"CL100",
"GF100", "GF101",
"IF100", "IF101", "IF102", "IF103", "IF104", "IF105", "IF106",
"IM108",
"IM101", "IM108",
"RP101"
]
warning = ["IN101"]
Expand Down
55 changes: 55 additions & 0 deletions demisto_sdk/commands/validate/tests/IM_validators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from demisto_sdk.commands.validate.validators.IM_validators.IM100_image_exists_validation import (
ImageExistsValidator,
)
from demisto_sdk.commands.validate.validators.IM_validators.IM101_image_too_large import (
ImageTooLargeValidator,
)
from demisto_sdk.commands.validate.validators.IM_validators.IM106_default_image_validator import (
DefaultImageValidator,
)
Expand Down Expand Up @@ -49,6 +52,58 @@ def test_ImageExistsValidator_is_valid_image_path():
)


@pytest.mark.parametrize(
"content_items, expected_number_of_failures, expected_msgs",
[
(
[
create_integration_object(
paths=["image"], values=["data:image/png;base64,short image"]
)
],
0,
[],
),
(
[
create_integration_object(
paths=["image"],
values=["data:image/png;base64," + ("A very big image" * 1000)],
)
],
1,
[
"You've created/modified a yml or package with a large sized image. Please make sure to change the image dimensions at: TestIntegration_image.png."
],
),
],
)
def test_ImageTooLargeValidator_is_valid(
content_items, expected_number_of_failures, expected_msgs
):
"""
Given:
content_items:
- Case 1: Integration with an image that is not in a valid size.
- Case 2: Integration with an image that is in a valid size.
When:
- Calling the ImageTooLargeValidator is_valid function.
Then:
- Make sure the right amount of integration image path failed, and that the right error message is returned.
- Case 1: Should fail.
- Case 2: Shouldn't fail.
"""
results = ImageTooLargeValidator().is_valid(content_items)
assert all(
[
result.message == expected_msg
for result, expected_msg in zip(results, expected_msgs)
]
)


def test_AuthorImageExistsValidator_is_valid_image_path():
"""
Given:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import annotations

from typing import Iterable, List

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

IMAGE_MAX_SIZE = 10 * 1024 # 10kB

ContentTypes = Integration


class ImageTooLargeValidator(BaseValidator[ContentTypes]):
error_code = "IM101"
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 = "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"
related_file_type = [RelatedFileType.IMAGE]

def is_valid(self, content_items: Iterable[ContentTypes]) -> List[ValidationResult]:
return [
ValidationResult(
validator=self,
message=self.error_message.format(content_item.name + "_image.png"),
content_object=content_item,
)
for content_item in content_items
if self.is_image_valid(content_item.image)
]

def is_image_valid(self, image: ImageRelatedFile):
file_type = image.file_path.suffix
if file_type == ".png":
return image.get_file_size().st_size > IMAGE_MAX_SIZE

elif file_type == ".svg":
# No size validation done for SVG images
return False

image_size = int(((image.get_file_size().st_size - 22) / 4) * 3)
return image_size > IMAGE_MAX_SIZE

0 comments on commit ec37a1f

Please sign in to comment.