def test_process_layoutscontainer__sanity(): """ Given - A layoutscontainer file called layoutscontainer-to-test.json When - parsing layoutscontainer files Then - parsing all the data from file successfully """ test_dir = os.path.join(git_path(), 'demisto_sdk', 'commands', 'create_id_set', 'tests', 'test_data', 'layoutscontainer-to-test.json') res = process_layoutscontainer(test_dir, True) assert len(res) == 1 result = res[0] result = result.get('layouts_container_test') assert 'detailsV2' in result.keys() assert 'name' in result.keys() assert 'group' in result.keys() assert 'fromversion' in result.keys() assert 'toversion' in result.keys() assert 'file_path' in result.keys() assert 'incident_and_indicator_types' in result.keys() assert 'incident_and_indicator_fields' in result.keys()
def test_process_layouts__no_incident_types_and_fields(): """ Given - A layout file called layout-to-test.json that doesnt have related incident fields and indicator fields When - parsing layout files Then - parsing all the data from file successfully """ test_file = os.path.join(git_path(), 'demisto_sdk', 'commands', 'create_id_set', 'tests', 'test_data', 'layout-to-test-no-types-fields.json') res = process_general_items(test_file, False, (FileType.LAYOUT,), get_layout_data) assert len(res) == 1 result = res[0] result = result.get('urlRep') assert 'kind' in result.keys() assert 'name' in result.keys() assert 'fromversion' in result.keys() assert 'toversion' in result.keys() assert 'file_path' in result.keys() assert 'typeID' in result.keys() assert 'incident_and_indicator_types' in result.keys() assert 'incident_and_indicator_fields' not in result.keys()
def test_process_mappers__sanity(): """ Given - A mapper file called classifier-mapper-to-test.json When - parsing mapper files Then - parsing all the data from file successfully """ test_file = os.path.join(git_path(), 'demisto_sdk', 'commands', 'create_id_set', 'tests', 'test_data', 'classifier-mapper-to-test.json') res = get_mapper_data(test_file) result = res.get('dummy mapper') assert 'name' in result.keys() assert 'file_path' in result.keys() assert 'fromversion' in result.keys() assert 'incident_types' in result.keys() assert 'incident_fields' in result.keys() assert 'dummy incident type' in result['incident_types'] assert 'dummy incident type 1' in result['incident_types'] assert 'dummy incident type 2' in result['incident_types'] assert 'dummy incident field' in result['incident_fields'] assert 'dummy incident field 1' in result['incident_fields'] assert 'dummy incident field 2' in result['incident_fields'] assert 'dummy incident field 3' in result['incident_fields'] assert 'occurred' not in result['incident_fields']
def test_process_general_items__sanity(): """ Given - A classifier file called classifier-to-test.json When - parsing classifier files Then - parsing all the data from file successfully """ test_file = os.path.join(git_path(), 'demisto_sdk', 'commands', 'create_id_set', 'tests', 'test_data', 'classifier-to-test.json') res = process_general_items(test_file, True, (FileType.CLASSIFIER,), get_classifier_data) assert len(res) == 1 result = res[0] result = result.get('dummy classifier') assert 'name' in result.keys() assert 'file_path' in result.keys() assert 'fromversion' in result.keys() assert 'incident_types' in result.keys() assert 'dummy incident type' in result['incident_types'] assert 'dummy incident type 2' in result['incident_types'] assert 'dummy incident type 3' in result['incident_types']
def id_set(): id_set_path = os.path.normpath( os.path.join(__file__, git_path(), 'demisto_sdk', 'tests', 'test_files', 'id_set', 'id_set.json')) with open(id_set_path, 'r') as id_set_file: id_set = json.load(id_set_file) yield id_set
def test_extract_to_package_format_py(pack, mocker, tmp_path): mocker.patch.object(Extractor, 'extract_image', return_value='12312321') mocker.patch('demisto_sdk.commands.split_yml.extractor.get_python_version', return_value='2.7') mocker.patch('demisto_sdk.commands.split_yml.extractor.get_pipenv_dir', return_value=os.path.join( git_path(), 'demisto_sdk/tests/test_files/default_python2')) integration = pack.create_integration('Sample') integration.create_default_integration() out = tmp_path / 'TestIntegration' non_sorted_imports = 'from CommonServerPython import *\nimport datetime\nimport json' integration.yml.update({ 'image': '', 'script': { 'type': 'python', 'script': non_sorted_imports } }) extractor = Extractor(input=integration.yml.path, output=str(out), file_type='integration') extractor.extract_to_package_format() with open(out / 'TestIntegration.py', encoding='utf-8') as f: file_data = f.read() # check imports are sorted assert non_sorted_imports not in file_data
def test_validate_invalid_pack_dependencies__validate_manager(self, ): """ Given: - A file path with invalid pack dependencies When: - checking validity of pack dependencies for added or modified files Then: - return a False validation response """ validate_manager = ValidateManager(skip_conf_json=True) id_set_path = os.path.normpath( os.path.join(__file__, git_path(), 'demisto_sdk', 'tests', 'test_files', 'id_set', 'id_set.json')) result = validate_manager.validate_pack_unique_files('QRadar', pack_error_ignore_list={}, id_set_path=id_set_path) assert not result
def test_update_tests_on_integration_with_test_playbook(self): """ Given - An integration file. When - Run format on the integration Then - Ensure run_format return value is 0 - Ensure `tests` field gets the Test Playbook ID """ test_files_path = os.path.join(git_path(), 'demisto_sdk', 'tests') vmware_integration_yml_path = os.path.join(test_files_path, 'test_files', 'content_repo_example', 'Packs', 'VMware', 'Integrations', 'integration-VMware.yml') formatter = IntegrationYMLFormat(input=vmware_integration_yml_path, output='') res = formatter.update_tests() assert res is None assert formatter.data.get('tests') == ['VMWare Test']
def test_extract_to_package_format_py(tmpdir, mocker): mocker.patch('demisto_sdk.commands.split_yml.extractor.get_python_version', return_value='2.7') mocker.patch('demisto_sdk.commands.split_yml.extractor.get_pipenv_dir', return_value=os.path.join( git_path(), 'demisto_sdk/tests/test_files/default_python2')) out = tmpdir.join('Integrations') extractor = Extractor( input=f'{git_path()}/demisto_sdk/tests/test_files/integration-Zoom.yml', output=str(out), file_type='integration') extractor.extract_to_package_format() with open(out.join('Zoom').join('Zoom.py'), 'r', encoding='utf-8') as f: file_data = f.read() # check imports are sorted assert 'import datetime\nimport json\nimport shutil\nfrom zipfile import ZipFile\n\nimport requests\n\n' \ 'import demistomock as demisto\nimport jwt\nfrom CommonServerPython import *\n' in file_data
def test_process_widget__no_script(): """ Given - A widget file called widget-no-scripts.json When - parsing widget files Then - parsing all the data from file successfully """ test_file = os.path.join(git_path(), 'demisto_sdk', 'commands', 'create_id_set', 'tests', 'test_data', 'widget-no-scripts.json') res = get_widget_data(test_file) result = res.get('dummy_widget') assert 'name' in result.keys() assert 'file_path' in result.keys() assert 'fromversion' in result.keys() assert 'scripts' not in result.keys()
def test_process_mappers__no_types_fields(): """ Given - An mapper file called classifier-mapper-to-test-no-types-fields.json with incident type related to it When - parsing mapper files Then - parsing all the data from file successfully """ test_file = os.path.join(git_path(), 'demisto_sdk', 'commands', 'create_id_set', 'tests', 'test_data', 'classifier-mapper-to-test-no-types-fields.json') res = get_mapper_data(test_file) result = res.get('dummy mapper') assert 'name' in result.keys() assert 'file_path' in result.keys() assert 'fromversion' in result.keys() assert 'incident_types' not in result.keys() assert 'incident_fields' not in result.keys()
def test_get_incident_type_data__no_playbooks_scripts(): """ Given - An incident type file called incidenttype-to-test-no-playbook-script.json with no script or playbook related to it When - parsing incident type files Then - parsing all the data from file successfully """ test_file = os.path.join(git_path(), 'demisto_sdk', 'commands', 'create_id_set', 'tests', 'test_data', 'incidenttype-to-test-no-playbook-script.json') res = get_incident_type_data(test_file) result = res.get('dummy incident type') assert 'name' in result.keys() assert 'file_path' in result.keys() assert 'fromversion' in result.keys() assert 'playbooks' not in result.keys() assert 'scripts' not in result.keys()
def test_process_incident_fields__sanity(): """ Given - An incident field file called incidentfield-to-test.json When - parsing incident field files Then - parsing all the data from file successfully """ test_dir = os.path.join(git_path(), 'demisto_sdk', 'commands', 'create_id_set', 'tests', 'test_data', 'incidentfield-to-test.json') res = process_incident_fields(test_dir, True, []) assert len(res) == 1 result = res[0] result = result.get('incidentfield-test') assert 'name' in result.keys() assert 'file_path' in result.keys() assert 'fromversion' in result.keys() assert 'toversion' in result.keys() assert 'incident_types' in result.keys() assert 'scripts' in result.keys()
def test_process_classifiers__no_types_scripts(): """ Given - An classifier file called classifier-to-test-no-incidenttypes.json with incident type related to it When - parsing classifier files Then - parsing all the data from file successfully """ test_dir = os.path.join(git_path(), 'demisto_sdk', 'commands', 'create_id_set', 'tests', 'test_data', 'classifier-to-test-no-incidenttypes.json') res = process_classifier(test_dir, False) assert len(res) == 1 result = res[0] result = result.get('dummy classifier') assert 'name' in result.keys() assert 'file_path' in result.keys() assert 'fromversion' in result.keys() assert 'incident_types' not in result.keys() assert 'incident_fields' not in result.keys()
def test_check_deprecated_playbook(repo): """ Given - An non-deprecated playbook yml. When - Running check_deprecated method. Then - Ensure the resulting ignored errors list included the deprecated default error list only. """ pack = repo.create_pack('pack') playbook = pack.create_integration('playbook-somePlaybook') test_file_path = join(git_path(), 'demisto_sdk', 'tests', 'test_files') valid_deprecated_playbook_file_path = join( test_file_path, 'Packs', 'CortexXDR', 'Playbooks', 'Valid_Deprecated_Playbook.yml') playbook.yml.write_dict(get_yaml(valid_deprecated_playbook_file_path)) files_path = playbook.yml.path with ChangeCWD(repo.path): base_validator = BaseValidator(ignored_errors={}) base_validator.check_deprecated(files_path) assert base_validator.ignored_errors[ 'playbook-somePlaybook.yml'] == DEPRECATED_IGNORE_ERRORS_DEFAULT_LIST
class TestOpenAPICodeGen: test_files_path = os.path.join(git_path(), 'demisto_sdk', 'tests', 'test_files') swagger_path = os.path.join(test_files_path, 'swagger_pets.json') def init_integration(self): base_name = 'TestSwagger' integration = OpenAPIIntegration(self.swagger_path, base_name, '-'.join(base_name.split(' ')).lower(), base_name.replace(' ', ''), unique_keys='id', root_objects='Pet') integration.load_file() return integration def test_config_file(self, mocker): """ Scenario: Generating an integration from a swagger file Given - A swagger file When - Generating the integration configuration file for the swagger file Then - Ensure the configuration file is generated correctly """ from demisto_sdk.commands.common.hook_validations.docker import \ DockerImageValidator mocker.patch.object(DockerImageValidator, 'get_docker_image_latest_tag_request', return_value='3.8.3.9324') integration = self.init_integration() integration.generate_configuration() with open(os.path.join(self.test_files_path, 'swagger_config.json'), 'rb') as config_path: config = json.load(config_path) assert json.dumps(integration.configuration) == json.dumps(config) def test_yaml_file(self, mocker): """ Scenario: Generating an integration from a swagger file Given - A swagger file - A generated integration configuration file When - Generating the integration yaml Then - Ensure the yaml file is generated correctly """ import yaml from demisto_sdk.commands.common.hook_validations.docker import \ DockerImageValidator mocker.patch.object(DockerImageValidator, 'get_docker_image_latest_tag_request', return_value='3.8.3.9324') integration = self.init_integration() with open(os.path.join(self.test_files_path, 'swagger_yaml.yml'), 'rb') as yaml_file: expected_yaml = yaml.safe_load(yaml_file) yaml_obj = integration.generate_yaml().to_yaml() assert yaml.dump(yaml_obj) == yaml.dump(expected_yaml) def test_python_file(self): """ Scenario: Generating an integration from a swagger file Given - A swagger file - A generated integration configuration file When - Generating the integration python code Then - Ensure the python file is generated correctly """ integration = self.init_integration() with open(os.path.join(self.test_files_path, 'swagger_python.txt'), 'r') as py_file: expected_py = py_file.read() py = integration.generate_python_code() assert py == expected_py def test_get_command_function(self): """ Scenario: Generating an integration from a swagger file Given - A swagger file - A generated integration configuration file - Generated commands from the configuration file When - Generating a command function and a request function for a command Then - Ensure the commands are generated correctly """ integration = self.init_integration() command = [c for c in integration.configuration['commands'] if c['name'] == 'get-pet-by-id'][0] command_function, req_function = integration.get_python_command_and_request_functions(command) assert command_function == expected_command_function assert req_function == expected_request_function def test_command_body_args(self): """ Scenario: Generating an integration from a swagger file Given - A swagger file - A generated integration configuration file - Generated commands from the configuration file When - Generating arguments for the command request body Then - Ensure the arguments are generated correctly """ from demisto_sdk.commands.openapi_codegen.openapi_codegen import \ base_data integration = self.init_integration() command = [c for c in integration.configuration['commands'] if c['name'] == 'create-user'][0] expected_args = 'id=user_id, username=user_username, firstName=user_firstname, lastName=user_lastname,' \ ' email=user_email, password=user_password, phone=user_phone, userStatus=user_userstatus' arguments = integration.process_command_arguments(command) body_args = integration.format_params(arguments[3], base_data, base_data) assert expected_args == body_args def test_command_headers(self): """ Scenario: Generating an integration from a swagger file Given - A swagger file - A generated integration configuration file - Generated commands from the configuration file When - Generating headers for the command request Then - Ensure the headers are generated correctly """ integration = self.init_integration() command = [c for c in integration.configuration['commands'] if c['name'] == 'upload-file'][0] expected_headers = [{'Content-Type': 'multipart/form-data'}] assert expected_headers == command['headers']
from os.path import join from click.testing import CliRunner from demisto_sdk.__main__ import main from demisto_sdk.commands.common.git_tools import git_path from demisto_sdk.commands.common.hook_validations.base_validator import \ BaseValidator VALIDATE_CMD = "validate" TEST_FILES_PATH = join(git_path(), "demisto_sdk/tests/test_files") AZURE_FEED_PACK_PATH = join(TEST_FILES_PATH, "content_repo_example/Packs/FeedAzure") AZURE_FEED_INVALID_PACK_PATH = join(TEST_FILES_PATH, "content_repo_example/Packs/FeedAzureab") VALID_PACK_PATH = join(TEST_FILES_PATH, "content_repo_example/Packs/FeedAzureValid") CONF_JSON_MOCK = { "tests": [{ "integrations": "AzureFeed", "playbookID": "AzureFeed - Test" }] } def assert_positive(file_path, result): """ Series of asserts every positive test should do :param file_path: path to the file :param result: result object as returned from runner.invoke """ assert result.exit_code == 0
from os.path import join import pytest from click.testing import CliRunner from demisto_sdk.__main__ import main from demisto_sdk.commands.common.git_tools import git_path from demisto_sdk.commands.download.tests.downloader_test import Environment DOWNLOAD_COMMAND = "download" DEMISTO_SDK_PATH = join(git_path(), "demisto_sdk") @pytest.fixture def demisto_client(mocker): mocker.patch("demisto_sdk.commands.download.downloader.demisto_client", return_valure="object") with open( 'demisto_sdk/tests/test_files/download_command/demisto_api_response', 'r') as f: api_response = f.read() mocker.patch( "demisto_sdk.commands.download.downloader.demisto_client.generic_request_func", return_value=(api_response, None, None)) def test_integration_download_no_force(demisto_client, tmp_path): """ Given - playbook & script exist in the output pack path. When
import os import pytest from demisto_sdk.commands.common.git_tools import git_path from demisto_sdk.commands.common.tools import get_json, get_yaml from demisto_sdk.commands.generate_docs.generate_integration_doc import ( append_or_replace_command_in_docs, generate_commands_section, generate_integration_doc) FILES_PATH = os.path.normpath( os.path.join(__file__, git_path(), 'demisto_sdk', 'tests', 'test_files')) FAKE_ID_SET = get_json(os.path.join(FILES_PATH, 'fake_id_set.json')) TEST_PLAYBOOK_PATH = os.path.join(FILES_PATH, 'playbook-Test_playbook.yml') TEST_SCRIPT_PATH = os.path.join(FILES_PATH, 'script-test_script.yml') TEST_INTEGRATION_PATH = os.path.join(FILES_PATH, 'fake_integration/fake_integration.yml') # common tests def test_format_md(): """ Given - A string representing a mrakdown returned from server with <br> tag When - generating docs Then - Ensure all <br> <BR> tags in markdown replaced with <br/> tags """
import json import os import pytest from demisto_sdk.commands.common.git_tools import git_path from demisto_sdk.commands.common.tools import get_json, get_yaml from demisto_sdk.commands.create_id_set.create_id_set import IDSetCreator from demisto_sdk.commands.generate_docs.generate_integration_doc import ( append_or_replace_command_in_docs, generate_commands_section, generate_integration_doc, generate_setup_section, generate_single_command_section) from demisto_sdk.commands.generate_docs.generate_script_doc import \ generate_script_doc FILES_PATH = os.path.normpath(os.path.join(__file__, git_path(), 'demisto_sdk', 'tests', 'test_files')) FAKE_ID_SET = get_json(os.path.join(FILES_PATH, 'fake_id_set.json')) TEST_PLAYBOOK_PATH = os.path.join(FILES_PATH, 'playbook-Test_playbook.yml') TEST_SCRIPT_PATH = os.path.join(FILES_PATH, 'script-test_script.yml') TEST_INTEGRATION_PATH = os.path.join(FILES_PATH, 'fake_integration/fake_integration.yml') # common tests def test_format_md(): """ Given - A string representing a markdown returned from server with <br> tag When - generating docs
import demisto_sdk.commands.common.constants as constants from demisto_sdk.commands.common.git_tools import git_path GIT_ROOT = "{}".format(git_path()) INVALID_PLAYBOOK_PATH = f"{GIT_ROOT}/demisto_sdk/tests/test_files/Playbooks.playbook-invalid.yml" VALID_TEST_PLAYBOOK_PATH = f"{GIT_ROOT}/demisto_sdk/tests/test_files/Playbooks.playbook-test.yml" VALID_BETA_PLAYBOOK_PATH = f"{GIT_ROOT}/demisto_sdk/tests/test_files/beta-playbook-valid.yml" VALID_PLAYBOOK_ARCSIGHT_ADD_DOMAIN_PATH = f"{GIT_ROOT}/demisto_sdk/tests/test_files/Playbooks." \ f"playbook-ArcSight_Add_Domain_Indicators.yml" INVALID_INTEGRATION_NO_TESTS = f'{GIT_ROOT}/demisto_sdk/tests/test_files/non-valid-integration-no-test-playbooks.yml' INVALID_INTEGRATION_NON_CONFIGURED_TESTS = f'{GIT_ROOT}/demisto_sdk/tests/test_files/' \ f'non-valid-integration-test-not-configured.yml' TEST_PLAYBOOK = f'{GIT_ROOT}/demisto_sdk/tests/test_files/playbook-TestPlaybooks.yml' VALID_PYTHON_INTEGRATION_TEST_PATH = f"{GIT_ROOT}/demisto_sdk/tests/test_files/integration_test.py" VALID_PYTHON_INTEGRATION_PATH = f"{GIT_ROOT}/demisto_sdk/tests/test_files/integration-test.py" VALID_METADATA1_PATH = f'{GIT_ROOT}/demisto_sdk/tests/test_files/1.pack_metadata.json' VALID_METADATA2_PATH = f'{GIT_ROOT}/demisto_sdk/tests/test_files/2.pack_metadata.json' VALID_DESCRIPTION_PATH = f'{GIT_ROOT}/demisto_sdk/tests/test_files/integration-test_description.md' VALID_README_PATH = f'{GIT_ROOT}/demisto_sdk/tests/test_files/integration-test_README.md' VALID_IMAGE_PATH = f'{GIT_ROOT}/demisto_sdk/tests/test_files/integration-test_image.png' NOT_VALID_IMAGE_PATH = f'{GIT_ROOT}/demisto_sdk/tests/test_files/default.png' VALID_PIPEFILE_PATH = f'{GIT_ROOT}/demisto_sdk/tests/test_files/Pipfile' VALID_PIPEFILE_LOCK_PATH = f'{GIT_ROOT}/demisto_sdk/tests/test_files/Pipfile.lock' VALID_PACK_IGNORE_PATH = f'{GIT_ROOT}/demisto_sdk/tests/test_files/.pack-ignore' VALID_SECRETS_IGNORE_PATH = f'{GIT_ROOT}/demisto_sdk/tests/test_files/.secrets-ignore' VALID_CLASSIFIER_PATH = f'{GIT_ROOT}/demisto_sdk/tests/test_files/classifier.json' VALID_JSON_FILE_FOR_UNIT_TESTING = f'{GIT_ROOT}/demisto_sdk/tests/test_files/fake_pack/Integrations/' \ f'test_data/results.json' VALID_INTEGRATION_TEST_PATH = f"{GIT_ROOT}/demisto_sdk/tests/test_files/integration-test.yml"
import os from os.path import join import conftest # noqa: F401 import pytest from click.testing import CliRunner from demisto_sdk.__main__ import main from demisto_sdk.commands.common.git_tools import git_path from demisto_sdk.commands.update_release_notes.update_rn import UpdateRN from demisto_sdk.commands.validate.validate_manager import ValidateManager UPDATE_RN_COMMAND = "update-release-notes" DEMISTO_SDK_PATH = join(git_path(), "demisto_sdk") TEST_FILES_PATH = join(git_path(), 'demisto_sdk', 'tests') AZURE_FEED_PACK_PATH = join(TEST_FILES_PATH, 'test_files', 'content_repo_example', 'Packs', 'FeedAzureValid') RN_FOLDER = join(git_path(), 'Packs', 'FeedAzureValid', 'ReleaseNotes') VMWARE_PACK_PATH = join(TEST_FILES_PATH, 'test_files', 'content_repo_example', 'Packs', 'VMware') VMWARE_RN_PACK_PATH = join(git_path(), 'Packs', 'VMware', 'ReleaseNotes') @pytest.fixture def demisto_client(mocker): mocker.patch("demisto_sdk.commands.download.downloader.demisto_client", return_valure="object") def test_update_release_notes_new_integration(demisto_client, mocker): """ Given