Skip to content

Commit

Permalink
Merge 880490d into 26c32c4
Browse files Browse the repository at this point in the history
  • Loading branch information
BEAdi committed Apr 1, 2024
2 parents 26c32c4 + 880490d commit 3a342b4
Show file tree
Hide file tree
Showing 4 changed files with 81 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
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 @@ -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,45 @@ def test_ImageExistsValidator_is_valid_image_path():
)


@pytest.mark.parametrize(
"content_items, expected_number_of_failures, expected_msgs",
[
([create_integration_object(image="short image")], 0, []),
(
[create_integration_object(image="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 with 2 integrations:
- One integration with an image that is not in a valid size.
- One 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
3 changes: 3 additions & 0 deletions demisto_sdk/commands/validate/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def create_integration_object(
values: Optional[List[Any]] = None,
pack_info: Optional[Dict[str, Any]] = None,
readme_content: Optional[str] = None,
image: Optional[str] = None,
code: Optional[str] = None,
) -> Integration:
"""Creating an integration object with altered fields from a default integration yml structure.
Expand All @@ -57,6 +58,8 @@ def create_integration_object(
integration = pack.create_integration(yml=yml_content, **additional_params)
code = code or "from MicrosoftApiModule import *"
integration.code.write(code)
if image is not None:
integration.image.write(image)
return BaseContent.from_path(Path(integration.path)) # type:ignore


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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.name + "_image.png"),
content_object=content_item,
)
for content_item in content_items
if content_item.image.get_file_size().st_size > IMAGE_MAX_SIZE
]

0 comments on commit 3a342b4

Please sign in to comment.