Skip to content

Commit

Permalink
Merge pull request #342 from demisto/price-tag-fix
Browse files Browse the repository at this point in the history
Removed validation for price in pack metadata
  • Loading branch information
Shellyber committed Apr 13, 2020
2 parents ccf04e0 + 79cce26 commit 7997e8c
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 42 deletions.
31 changes: 13 additions & 18 deletions demisto_sdk/commands/common/hook_validations/pack_unique_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
import os
import re

from demisto_sdk.commands.common.constants import (API_MODULES_PACK,
PACK_METADATA_CATEGORIES,
PACK_METADATA_DEPENDENCIES,
PACK_METADATA_FIELDS,
PACK_METADATA_KEYWORDS,
PACK_METADATA_PRICE,
PACK_METADATA_TAGS,
PACK_METADATA_USE_CASES,
PACKS_PACK_IGNORE_FILE_NAME,
PACKS_PACK_META_FILE_NAME,
PACKS_README_FILE_NAME,
PACKS_WHITELIST_FILE_NAME)
from demisto_sdk.commands.common.constants import ( # PACK_METADATA_PRICE,
API_MODULES_PACK, PACK_METADATA_CATEGORIES, PACK_METADATA_DEPENDENCIES,
PACK_METADATA_FIELDS, PACK_METADATA_KEYWORDS, PACK_METADATA_TAGS,
PACK_METADATA_USE_CASES, PACKS_PACK_IGNORE_FILE_NAME,
PACKS_PACK_META_FILE_NAME, PACKS_README_FILE_NAME,
PACKS_WHITELIST_FILE_NAME)
from demisto_sdk.commands.common.tools import pack_name_to_path


Expand Down Expand Up @@ -151,12 +145,13 @@ def _is_pack_meta_file_structure_valid(self):
if not isinstance(dependencies_field, dict):
self._add_error('The dependencies field in the pack must be a dictionary.')
return False
price_field = metadata[PACK_METADATA_PRICE]
try:
int(price_field)
except Exception:
self._add_error('The price field in the pack must be a number.')
return False
# TODO: add it back after #23546 is ready.
# price_field = metadata.get(PACK_METADATA_PRICE)
# try:
# int(price_field)
# except Exception:
# self._add_error('The price field in the pack must be a number.')
# return False
for list_field in (PACK_METADATA_KEYWORDS, PACK_METADATA_TAGS, PACK_METADATA_CATEGORIES,
PACK_METADATA_USE_CASES):
field = metadata[list_field]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ def test_metadata_validator_valid(self, mocker):
validator = PackUniqueFilesValidator('fake')
assert validator.validate_pack_meta_file()

# TODO: add the validation for price after #23546 is ready.
@pytest.mark.parametrize('metadata', [os.path.join(FILES_PATH, 'pack_metadata_missing_fields.json'),
os.path.join(FILES_PATH, 'pack_metadata_invalid_price.json'),
# os.path.join(FILES_PATH, 'pack_metadata_invalid_price.json'),
os.path.join(FILES_PATH, 'pack_metadata_invalid_dependencies.json'),
os.path.join(FILES_PATH, 'pack_metadata_list_dependencies.json'),
os.path.join(FILES_PATH, 'pack_metadata_empty_category.json'),
os.path.join(FILES_PATH, 'pack_metadata_invalid_keywords.json'),
os.path.join(FILES_PATH, 'pack_metadata_invalid_tags.json'),
os.path.join(FILES_PATH, 'pack_metadata_list.json')])
def test_metadata_validator_invalid(self, mocker, metadata):
Expand Down
24 changes: 6 additions & 18 deletions demisto_sdk/commands/init/initiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def create_metadata(fill_manually: bool) -> Dict:
'certification': 'certified',
'useCases': [],
'keywords': [],
'price': '0',
# 'price': '0',
'dependencies': {},
}

Expand All @@ -232,9 +232,9 @@ def create_metadata(fill_manually: bool) -> Dict:
tags = input("\nTags of the pack, comma separated values: ")
tags_list = [t.strip() for t in tags.split(',')]
metadata['tags'] = tags_list

price = input("\nThe price of the pack: ")
metadata['price'] = price
# TODO: add it back after #23546 is ready.
# price = input("\nThe price of the pack: ")
# metadata['price'] = price

return metadata

Expand Down Expand Up @@ -269,19 +269,6 @@ def get_valid_user_input(options_list: List[str], option_message: str) -> str:

return options_list[user_choice - 1]

@staticmethod
def get_yml_data_as_dict(file_path: str) -> Dict:
"""Converts YML file data to Dict.
Args:
file_path (str): The path to the .yml file
Returns:
Dict. Data from YML.
"""
with open(file_path) as f:
return yaml.load(f, Loader=yamlordereddictloader.SafeLoader)

def integration_init(self) -> bool:
"""Creates a new integration according to a template.
Expand Down Expand Up @@ -365,7 +352,8 @@ def yml_reformatting(self, current_suffix: str, integration: bool = False):
current_suffix (str): The yml file name (HelloWorld or HelloWorldScript)
integration (bool): Indicates if integration yml is being reformatted.
"""
yml_dict = self.get_yml_data_as_dict(file_path=os.path.join(self.full_output_path, f"{current_suffix}.yml"))
with open(os.path.join(self.full_output_path, f"{current_suffix}.yml")) as f:
yml_dict = yaml.load(f, Loader=yamlordereddictloader.SafeLoader)
yml_dict["commonfields"]["id"] = self.id
yml_dict['name'] = self.id
if integration:
Expand Down
49 changes: 44 additions & 5 deletions demisto_sdk/commands/init/tests/initiator_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
from collections import deque
from collections import OrderedDict, deque
from datetime import datetime
from pathlib import Path
from typing import Callable

import pytest
import yaml
import yamlordereddictloader
from demisto_sdk.commands.common.constants import (INTEGRATION_CATEGORIES,
PACK_INITIAL_VERSION,
PACK_SUPPORT_OPTIONS)
Expand All @@ -17,7 +20,7 @@
PACK_URL = 'https://www.github.com/pack'
PACK_EMAIL = 'author@mail.com'
PACK_TAGS = 'Tag1,Tag2'
PACK_PRICE = '0'
# PACK_PRICE = '0'


@pytest.fixture
Expand Down Expand Up @@ -61,6 +64,7 @@ def test_get_object_id(monkeypatch, initiator):
assert initiator.id == 'SomeIntegrationID'


# TODO: add the validation for price after #23546 is ready.
def test_create_metadata(monkeypatch, initiator):
# test create_metadata without user filling manually
pack_metadata = initiator.create_metadata(False)
Expand All @@ -82,7 +86,7 @@ def test_create_metadata(monkeypatch, initiator):
'certification': 'certified',
'useCases': [],
'keywords': [],
'price': '0',
# 'price': '0',
'dependencies': {},
}

Expand All @@ -92,7 +96,8 @@ def test_create_metadata(monkeypatch, initiator):
generate_multiple_inputs(
deque([
PACK_NAME, PACK_DESC, '1', PACK_SERVER_MIN_VERSION, PACK_AUTHOR,
PACK_URL, PACK_EMAIL, '1', PACK_TAGS, PACK_PRICE
PACK_URL, PACK_EMAIL, '1', PACK_TAGS
# PACK_PRICE
])
)
)
Expand All @@ -109,7 +114,7 @@ def test_create_metadata(monkeypatch, initiator):
'email': PACK_EMAIL,
'keywords': [],
'name': PACK_NAME,
'price': PACK_PRICE,
# 'price': PACK_PRICE,
'serverMinVersion': PACK_SERVER_MIN_VERSION,
'support': PACK_SUPPORT_OPTIONS[0],
'tags': ['Tag1', 'Tag2'],
Expand Down Expand Up @@ -143,3 +148,37 @@ def test_create_new_directory(mocker, monkeypatch, initiator):
# fail to create pack cause of existing dir without overriding it
monkeypatch.setattr('builtins.input', lambda _: 'N')
assert initiator.create_new_directory() is False


def test_yml_reformatting(tmp_path, initiator):
integration_id = 'HelloWorld'
initiator.id = integration_id
d = tmp_path / integration_id
d.mkdir()
full_output_path = Path(d)
initiator.full_output_path = full_output_path

p = d / f'{integration_id}.yml'
with p.open(mode='w') as f:
yaml.dump(
{
'commonfields': {
'id': ''
},
'name': '',
'display': ''
},
f
)
dir_name = 'HelloWorldTest'
initiator.dir_name = dir_name
initiator.yml_reformatting(current_suffix=initiator.HELLO_WORLD_INTEGRATION, integration=True)
with open(full_output_path / f'{dir_name}.yml', 'r') as f:
yml_dict = yaml.load(f, Loader=yamlordereddictloader.SafeLoader)
assert yml_dict == OrderedDict({
'commonfields': OrderedDict({
'id': 'HelloWorld'
}),
'display': 'HelloWorld',
'name': 'HelloWorld'
})
28 changes: 28 additions & 0 deletions demisto_sdk/tests/test_files/pack_metadata_empty_category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "AWS Feed",
"description": "Indicators feed from AWS",
"support": "Cortex XSOAR",
"serverMinVersion": "5.5.0",
"currentVersion": "1.0.0",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
"categories": [""],
"tags": [],
"created": "2020-03-09T16:04:45Z",
"beta": false,
"deprecated": false,
"certification": "certified",
"useCases": [],
"keywords": ["AWS", "Feed"],
"price": 0,
"dependencies": {
"Base": {
"mandatory": true,
"minVersion": "1.0.0",
"name": "Base",
"certification": "certified",
"author": "Cortex XSOAR"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "AWS Feed",
"description": "Indicators feed from AWS",
"support": "Cortex XSOAR",
"serverMinVersion": "5.5.0",
"currentVersion": "1.0.0",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
"categories": [
"Data Enrichment & Threat Intelligence"
],
"tags": [""],
"created": "2020-03-09T16:04:45Z",
"beta": false,
"deprecated": false,
"certification": "certified",
"useCases": [],
"keywords": ["AWS", "Feed", "test"],
"price": 0,
"dependencies": {
"Base": {
"mandatory": true,
"minVersion": "1.0.0",
"name": "Base",
"certification": "certified"
}
}
}
30 changes: 30 additions & 0 deletions demisto_sdk/tests/test_files/pack_metadata_invalid_keywords.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "AWS Feed",
"description": "Indicators feed from AWS",
"support": "Cortex XSOAR",
"serverMinVersion": "5.5.0",
"currentVersion": "1.0.0",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
"categories": [
"Data Enrichment & Threat Intelligence"
],
"tags": [],
"created": "2020-03-09T16:04:45Z",
"beta": false,
"deprecated": false,
"certification": "certified",
"useCases": [],
"keywords": [""],
"price": 0,
"dependencies": {
"Base": {
"mandatory": true,
"minVersion": "1.0.0",
"name": "Base",
"certification": "certified",
"author": "Cortex XSOAR"
}
}
}
30 changes: 30 additions & 0 deletions demisto_sdk/tests/test_files/pack_metadata_list_dependencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "AWS Feed",
"description": "Indicators feed from AWS",
"support": "Cortex XSOAR",
"serverMinVersion": "5.5.0",
"currentVersion": "1.0.0",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
"categories": [
"Data Enrichment & Threat Intelligence"
],
"tags": [],
"created": "2020-03-09T16:04:45Z",
"beta": false,
"deprecated": false,
"certification": "certified",
"useCases": [],
"keywords": ["AWS", "Feed"],
"price": 0,
"dependencies": [{
"Base": {
"mandatory": true,
"minVersion": "1.0.0",
"name": "Base",
"certification": "certified",
"author": "Cortex XSOAR"
}
}]
}

0 comments on commit 7997e8c

Please sign in to comment.