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

IM101 #4199

Merged
merged 10 commits into from
Apr 11, 2024
Merged

IM101 #4199

Show file tree
Hide file tree
Changes from all 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: 2 additions & 2 deletions demisto_sdk/commands/validate/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ select = [
"CL100",
"GF100", "GF101",
"IF100", "IF101", "IF102", "IF103", "IF104", "IF105", "IF106",
"IM100", "IM108", "IM109",
"IM100", "IM101", "IM108", "IM109",
"RP101"
]
warning = []
Expand All @@ -67,7 +67,7 @@ select = [
"CL100",
"GF100", "GF101",
"IF100", "IF101", "IF102", "IF103", "IF104", "IF105", "IF106",
"IM108",
"IM101", "IM108",
"RP101"
]
warning = []
Expand Down
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,52 @@
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 (
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.path),
content_object=content_item,
)
for content_item in content_items
if self.is_image_valid(content_item)
]

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

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

elif file_type == ".yml":
image_size = int(((len(content_item.data["image"]) - 22) / 4) * 3)
return image_size > IMAGE_MAX_SIZE

# image can't be saved in a different file type
return True
Loading