def test_insert_script_to_yml_exceptions(package_path, dir_name, file_path):
    with patch.object(IntegrationScriptUnifier, "__init__", lambda a, b, c, d, e: None):
        unifier = IntegrationScriptUnifier("", None, None, None)
        unifier.package_path = package_path
        unifier.dir_name = dir_name
        unifier.is_script_package = dir_name == 'Scripts'
        with open(file_path + ".yml", "r") as yml:
            test_yml_data = yaml.load(yml)
        if dir_name == "Scripts":
            test_yml_data['script'] = 'blah'
        else:
            test_yml_data['script']['script'] = 'blah'

        unifier.insert_script_to_yml(".py", {'script': {}}, test_yml_data)
def test_get_data():
    with patch.object(IntegrationScriptUnifier, "__init__", lambda a, b, c, d, e: None):
        unifier = IntegrationScriptUnifier('', None, None, None)
        unifier.package_path = f"{git_path()}/demisto_sdk/tests/test_files/VulnDB/"
        unifier.is_script_package = False
        with open(f"{git_path()}/demisto_sdk/tests/test_files/VulnDB/VulnDB_image.png", "rb") as image_file:
            image = image_file.read()
        data, found_data_path = unifier.get_data(unifier.package_path, "*png")
        assert data == image
        assert found_data_path == f"{git_path()}/demisto_sdk/tests/test_files/VulnDB/VulnDB_image.png"
        unifier.is_script_package = True
        data, found_data_path = unifier.get_data(unifier.package_path, "*png")
        assert data is None
        assert found_data_path is None
def test_insert_description_to_yml():
    with patch.object(IntegrationScriptUnifier, "__init__", lambda a, b, c, d, e: None):
        unifier = IntegrationScriptUnifier('', None, None, None)
        unifier.package_path = f"{git_path()}/demisto_sdk/tests/test_files/VulnDB/"
        unifier.dir_name = "Integrations"
        unifier.is_script_package = False
        with open(f"{git_path()}/demisto_sdk/tests/test_files/VulnDB/VulnDB_description.md", "rb") as desc_file:
            desc_data = desc_file.read().decode("utf-8")
        integration_doc_link = '\n\n---\n[View Integration Documentation]' \
                               '(https://xsoar.pan.dev/docs/reference/integrations/vuln-db)'
        yml_unified, found_data_path = unifier.insert_description_to_yml(
            {'commonfields': {'id': 'VulnDB'}}, {}
        )

        assert found_data_path == f"{git_path()}/demisto_sdk/tests/test_files/VulnDB/VulnDB_description.md"
        assert (desc_data + integration_doc_link) == yml_unified['detaileddescription']
def test_insert_image_to_yml():
    with patch.object(IntegrationScriptUnifier, "__init__", lambda a, b, c, d, e: None):
        unifier = IntegrationScriptUnifier('', None, None, None)
        unifier.package_path = f"{git_path()}/demisto_sdk/tests/test_files/VulnDB/"
        unifier.dir_name = "Integrations"
        unifier.is_script_package = False
        unifier.image_prefix = "data:image/png;base64,"
        with open(f"{git_path()}/demisto_sdk/tests/test_files/VulnDB/VulnDB_image.png", "rb") as image_file:
            image_data = image_file.read()
            image_data = unifier.image_prefix + base64.b64encode(image_data).decode('utf-8')
        with open(f"{git_path()}/demisto_sdk/tests/test_files/VulnDB/VulnDB.yml", mode="r", encoding="utf-8") \
                as yml_file:
            yml_unified_test = yaml.load(yml_file)
        with open(f"{git_path()}/demisto_sdk/tests/test_files/VulnDB/VulnDB.yml", "r") as yml:
            yml_data = yaml.load(yml)
        yml_unified, found_img_path = unifier.insert_image_to_yml(yml_data, yml_unified_test)
        yml_unified_test['image'] = image_data
        assert found_img_path == f"{git_path()}/demisto_sdk/tests/test_files/VulnDB/VulnDB_image.png"
        assert yml_unified == yml_unified_test
def test_insert_script_to_yml(package_path, dir_name, file_path):
    with patch.object(IntegrationScriptUnifier, "__init__", lambda a, b, c, d, e: None):
        unifier = IntegrationScriptUnifier("", None, None, None)
        unifier.package_path = package_path
        unifier.dir_name = dir_name
        unifier.is_script_package = dir_name == 'Scripts'
        with open(file_path + ".yml", "r") as yml:
            test_yml_data = yaml.load(yml)

        test_yml_unified = copy.deepcopy(test_yml_data)

        yml_unified, script_path = unifier.insert_script_to_yml(".py", test_yml_unified, test_yml_data)

        with open(file_path + ".py", mode="r", encoding="utf-8") as script_file:
            script_code = script_file.read()
        clean_code = unifier.clean_python_code(script_code)

        if isinstance(test_yml_unified.get('script', {}), str):
            test_yml_unified['script'] = clean_code
        else:
            test_yml_unified['script']['script'] = clean_code

        assert yml_unified == test_yml_unified
        assert script_path == file_path + ".py"