def test_get_layouts_by_layout_container_type(self, tmpdir):
        """
        Given:
        - Layout container FileType.

        When:
        - Wanting to retrieve all layout-containers from the current pack.

        Then:
        - Ensure only layout-containers in the pack are returned.
        """
        fake_pack_name = 'FakeTestPack'
        repo = Repo(tmpdir)
        repo_path = Path(repo.path)
        fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
        fake_pack.create_layoutcontainer('ExtraHop Detection',
                                         util_load_json(self.LAYOUT_CONTAINER))
        fake_pack_path = fake_pack.path
        layouts = BaseConverter.get_entities_by_entity_type(
            Pack(fake_pack_path).layouts, FileType.LAYOUTS_CONTAINER)
        assert all(layout.type() == FileType.LAYOUTS_CONTAINER
                   for layout in layouts)
        assert [layout.layout_id()
                for layout in layouts] == ['ExtraHop Detection']
Exemple #2
0
def test_get_ignore_pack_tests__ignore_missing_test(tmpdir, mocker):
    """
    Given
    - Pack have .pack-ignore file
    - There are skipped tests in .pack-ignore
    - The tests are missing from the content pack
    When
    - Collecting packs' ignored tests - running `get_ignore_pack_tests()`
    Then:
    - returns a list with the skipped tests
    """
    fake_pack_name = 'FakeTestPack'
    fake_test_name = 'FakeTestPlaybook.yml'

    # prepare repo
    repo = Repo(tmpdir)
    packs_path = Path(repo.path) / PACKS_DIR
    pack = Pack(packs_path, fake_pack_name, repo)
    test_playbook_path = packs_path / fake_pack_name / TEST_PLAYBOOKS_DIR
    pack_ignore_path = os.path.join(pack.path, PACKS_PACK_IGNORE_FILE_NAME)

    # prepare .pack-ignore
    with open(pack_ignore_path, 'a') as pack_ignore_f:
        pack_ignore_f.write("[file:TestIntegration.yml]\nignore=IN126\n\n"
                            f"[file:{fake_test_name}]\nignore=auto-test")

    # prepare mocks
    mocker.patch.object(tools,
                        "get_pack_ignore_file_path",
                        return_value=pack_ignore_path)
    mocker.patch.object(os.path,
                        "join",
                        return_value=str(test_playbook_path / fake_test_name))

    ignore_test_set = get_ignore_pack_skipped_tests(fake_pack_name)
    assert len(ignore_test_set) == 0
    def test_layout_to_indicators_dict(self, tmpdir):
        """
        Given:
        - Indicator types of the pack.

        When:
        - Creating a dict of key as layout ID and value as list of indicator type IDs whom layout field equals to layout
          ID.

        Then:
        - Ensure expected dict is returned.

        """
        fake_pack_name = 'FakeTestPack'
        repo = Repo(tmpdir)
        repo_path = Path(repo.path)
        fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
        fake_pack.create_indicator_type(
            'Cryptocurrency Address', util_load_json(self.INDICATOR_TYPE_ONE))
        fake_pack_path = fake_pack.path
        layout_converter = LayoutBelowSixConverter(Pack(fake_pack_path))
        res = layout_converter.layout_to_indicators_or_incidents_dict(
            layout_converter.pack.indicator_types)
        assert res == {'ExtraHop Detection': ['Cryptocurrency Address']}
Exemple #4
0
def test_convert_contribution_zip_updated_pack(get_content_path_mock, get_python_version_mock, tmp_path, mocker):
    """
    Create a fake contribution zip file and test that it is converted to a Pack correctly.
    The pack already exists, checking the update flow.

    Args:
        get_content_path_mock (MagicMock): Patch of the 'get_content_path' function to return the fake repo directory
            used in the test
        get_python_version_mock (MagicMock): Patch of the 'get_python_version' function to return the "3.7"
        tmp_path (fixture): Temporary Path used for the unit test and cleaned up afterwards

    Scenario: Simulate converting a contribution zip file.

    Given
    - A contribution zip file
    - The zipfile contains a unified integration file
    When
    - Converting the zipfile to a valid Pack structure
    - The contribution is an update to an existing pack
    Then
    - Ensure integration are componentized and in valid directory structure
    - Ensure that readme file has not been changed.

    """
    mocker.patch.object(GitUtil, '__init__', return_value=None)
    mocker.patch.object(GitUtil, 'added_files', return_value=set())
    mocker.patch.object(GitUtil, 'modified_files', return_value=set())
    # Create all Necessary Temporary directories
    # create temp directory for the repo
    repo_dir = tmp_path / 'content_repo'
    repo_dir.mkdir()
    get_content_path_mock.return_value = repo_dir
    get_python_version_mock.return_value = 3.7
    # create temp target dir in which we will create all the TestSuite content items to use in the contribution zip and
    # that will be deleted after
    target_dir = repo_dir / 'target_dir'
    target_dir.mkdir()
    # create temp directory in which the contribution zip will reside
    contribution_zip_dir = tmp_path / 'contrib_zip'
    contribution_zip_dir.mkdir()
    # Create fake content repo and contribution zip
    repo = Repo(repo_dir)
    pack = repo.create_pack('TestPack')
    integration = pack.create_integration('integration0')
    integration.create_default_integration()
    contrib_zip = Contribution(target_dir, 'ContribTestPack', repo)
    contrib_zip.create_zip(contribution_zip_dir)
    # target_dir should have been deleted after creation of the zip file
    assert not target_dir.exists()
    name = 'Test Pack'
    contribution_path = contrib_zip.created_zip_filepath
    description = 'test pack description here'
    author = 'Octocat Smith'
    contrib_converter_inst = ContributionConverter(
        name=name, contribution=contribution_path, description=description, author=author, create_new=False,
        no_pipenv=True)
    contrib_converter_inst.convert_contribution_to_pack()
    converted_pack_path = repo_dir / 'Packs' / 'TestPack'
    assert converted_pack_path.exists()
    integrations_path = converted_pack_path / 'Integrations'
    sample_integration_path = integrations_path / 'integration0'
    integration_yml = sample_integration_path / 'integration0.yml'
    integration_py = sample_integration_path / 'integration0.py'
    integration_description = sample_integration_path / 'integration0_description.md'
    integration_image = sample_integration_path / 'integration0_image.png'
    integration_readme_md = sample_integration_path / 'README.md'
    unified_yml = integrations_path / 'integration-integration0.yml'
    unified_yml_in_sample = sample_integration_path / 'integration-integration0.yml'
    integration_files = [integration_yml, integration_py, integration_description, integration_image,
                         integration_readme_md]
    for integration_file in integration_files:
        assert integration_file.exists()
    # In a new pack that part will exist.

    assert not unified_yml.exists()
    assert not unified_yml_in_sample.exists()
Exemple #5
0
def test_convert_contribution_zip_with_args(get_content_path_mock, get_python_version_mock, tmp_path, mocker):
    """Convert a contribution zip to a pack and test that the converted pack's 'pack_metadata.json' is correct

    Args:
        get_content_path_mock (MagicMock): Patch of the 'get_content_path' function to return the fake repo directory
            used in the test
        get_python_version_mock (MagicMock): Patch of the 'get_python_version' function to return the "3.7"
        tmp_path (fixture): Temporary Path used for the unit test and cleaned up afterwards

    Scenario: Simulate converting a contribution zip file

    Given
    - A contribution zip file
    When
    - The contrib_converter class instance is instantiated with the 'name' argument of 'Test Pack'
    - The contrib_converter class instance is instantiated with the 'description' argument
      of 'test pack description here'
    - The contrib_converter class instance is instantiated with the 'author' argument of 'Octocat Smith'
    - The contrib_converter class instance is instantiated with the 'gh_user' argument of 'octocat'
    Then
    - Ensure pack with directory name of 'TestPack' is created
    - Ensure that the pack's 'pack_metadata.json' file's 'name' field is 'Test Pack'
    - Ensure that the pack's 'pack_metadata.json' file's 'description' field is 'test pack description here'
    - Ensure that the pack's 'pack_metadata.json' file's 'author' field is 'Octocat Smith'
    - Ensure that the pack's 'pack_metadata.json' file's 'githubUser' field a list containing only 'octocat'
    - Ensure that the pack's 'pack_metadata.json' file's 'email' field is the empty string
    """
    mocker.patch.object(GitUtil, '__init__', return_value=None)
    mocker.patch.object(GitUtil, 'added_files', return_value=set())
    mocker.patch.object(GitUtil, 'modified_files', return_value=set())

    # Create all Necessary Temporary directories
    # create temp directory for the repo
    repo_dir = tmp_path / 'content_repo'
    repo_dir.mkdir()
    get_content_path_mock.return_value = repo_dir
    get_python_version_mock.return_value = 3.7
    # create temp target dir in which we will create all the TestSuite content items to use in the contribution zip and
    # that will be deleted after
    target_dir = repo_dir / 'target_dir'
    target_dir.mkdir()
    # create temp directory in which the contribution zip will reside
    contribution_zip_dir = tmp_path / 'contrib_zip'
    contribution_zip_dir.mkdir()
    # Create fake content repo and contribution zip
    repo = Repo(repo_dir)
    contrib_zip = Contribution(target_dir, 'ContribTestPack', repo)
    # contrib_zip.create_zip(contribution_zip_dir)
    contrib_zip.create_zip(contribution_zip_dir)

    # target_dir should have been deleted after creation of the zip file
    assert not target_dir.exists()

    name = 'Test Pack'
    contribution_path = contrib_zip.created_zip_filepath
    description = 'test pack description here'
    author = 'Octocat Smith'
    gh_user = '******'
    contrib_converter_inst = ContributionConverter(
        name=name, contribution=contribution_path, description=description, author=author, gh_user=gh_user,
        no_pipenv=True)
    contrib_converter_inst.convert_contribution_to_pack()

    converted_pack_path = repo_dir / 'Packs' / 'TestPack'
    assert converted_pack_path.exists()

    pack_metadata_path = converted_pack_path / 'pack_metadata.json'
    assert pack_metadata_path.exists()
    with open(pack_metadata_path, 'r') as pack_metadata:
        metadata = json.load(pack_metadata)
        assert metadata.get('name', '') == name
        assert metadata.get('description', '') == description
        assert metadata.get('author', '') == author
        assert metadata.get('githubUser', []) == [gh_user]
        assert metadata.get('marketplaces', []) == ['xsoar', 'marketplacev2']
        assert not metadata.get('email')
Exemple #6
0
def test_convert_contribution_zip(get_content_path_mock, get_python_version_mock, tmp_path, mocker):
    """Create a fake contribution zip file and test that it is converted to a Pack correctly

    Args:
        get_content_path_mock (MagicMock): Patch of the 'get_content_path' function to return the fake repo directory
            used in the test
        get_python_version_mock (MagicMock): Patch of the 'get_python_version' function to return the "3.7"
        tmp_path (fixture): Temporary Path used for the unit test and cleaned up afterwards

    Scenario: Simulate converting a contribution zip file

    Given
    - A contribution zip file
    - The zipfile contains a unified script file
    - The zipfile contains a unified integration file
    When
    - Converting the zipfile to a valid Pack structure
    Then
    - Ensure script and integration are componentized and in valid directory structure
    - Ensure readme_files is not empty and the generated docs exists.
    """
    mocker.patch.object(GitUtil, '__init__', return_value=None)
    mocker.patch.object(GitUtil, 'added_files', return_value=set())
    mocker.patch.object(GitUtil, 'modified_files', return_value=set())
    # Create all Necessary Temporary directories
    # create temp directory for the repo
    repo_dir = tmp_path / 'content_repo'
    repo_dir.mkdir()
    get_content_path_mock.return_value = repo_dir
    get_python_version_mock.return_value = 3.7
    # create temp target dir in which we will create all the TestSuite content items to use in the contribution zip and
    # that will be deleted after
    target_dir = repo_dir / 'target_dir'
    target_dir.mkdir()
    # create temp directory in which the contribution zip will reside
    contribution_zip_dir = tmp_path / 'contrib_zip'
    contribution_zip_dir.mkdir()
    # Create fake content repo and contribution zip
    repo = Repo(repo_dir)
    contrib_zip = Contribution(target_dir, 'ContribTestPack', repo)
    contrib_zip.create_zip(contribution_zip_dir)
    # target_dir should have been deleted after creation of the zip file
    assert not target_dir.exists()

    # rename script-script0.yml unified to automation-script0.yml
    # this naming is aligned to how the server exports scripts in contribution zips
    rename_file_in_zip(
        contrib_zip.created_zip_filepath, 'automation/script-script0.yml', 'automation/automation-script0.yml'
    )

    name = 'Contrib Test Pack'
    contribution_path = contrib_zip.created_zip_filepath
    description = 'test pack description here'
    author = 'Octocat Smith'
    contrib_converter_inst = ContributionConverter(
        name=name, contribution=contribution_path, description=description, author=author, no_pipenv=True)
    contrib_converter_inst.convert_contribution_to_pack()

    converted_pack_path = repo_dir / 'Packs' / 'ContribTestPack'
    assert converted_pack_path.exists()

    scripts_path = converted_pack_path / 'Scripts'
    sample_script_path = scripts_path / 'SampleScript'
    script_yml = sample_script_path / 'SampleScript.yml'
    script_py = sample_script_path / 'SampleScript.py'
    script_readme_md = sample_script_path / 'README.md'
    unified_script_in_sample = sample_script_path / 'automation-script0.yml'
    unified_script = scripts_path / 'automation-script0.yml'

    assert scripts_path.exists()
    assert sample_script_path.exists()
    assert script_yml.exists()
    assert script_py.exists()
    assert script_readme_md.exists()
    assert not unified_script_in_sample.exists()
    assert not unified_script.exists()

    integrations_path = converted_pack_path / 'Integrations'
    sample_integration_path = integrations_path / 'Sample'
    integration_yml = sample_integration_path / 'Sample.yml'
    integration_py = sample_integration_path / 'Sample.py'
    integration_description = sample_integration_path / 'Sample_description.md'
    integration_image = sample_integration_path / 'Sample_image.png'
    integration_readme_md = sample_integration_path / 'README.md'
    unified_yml = integrations_path / 'integration-integration0.yml'
    unified_yml_in_sample = sample_integration_path / 'integration-integration0.yml'
    integration_files = [integration_yml, integration_py, integration_description, integration_image,
                         integration_readme_md]
    for integration_file in integration_files:
        assert integration_file.exists()
    assert not unified_yml.exists()
    assert not unified_yml_in_sample.exists()

    playbooks_path = converted_pack_path / 'Playbooks'
    playbook_yml = playbooks_path / 'playbook-SamplePlaybook.yml'
    playbook_readme_md = playbooks_path / 'playbook-SamplePlaybook_README.md'

    assert playbooks_path.exists()
    assert playbook_yml.exists()
    assert playbook_readme_md.exists()

    layouts_path = converted_pack_path / 'Layouts'
    sample_layoutscontainer = layouts_path / f'{LAYOUTS_CONTAINER}-fakelayoutscontainer.json'
    sample_layout = layouts_path / f'{LAYOUT}-fakelayout.json'

    assert layouts_path.exists()
    assert sample_layoutscontainer.exists()
    assert sample_layout.exists()

    assert set(contrib_converter_inst.readme_files) == {str(playbook_readme_md), str(integration_readme_md),
                                                        str(script_readme_md)}
Exemple #7
0
def test_convert_contribution_zip_outputs_structure(get_content_path_mock, get_python_version_mock, tmp_path, mocker):
    """Create a fake contribution zip file and test that it is converted to a Pack correctly

    Args:
        get_content_path_mock (MagicMock): Patch of the 'get_content_path' function to return the fake repo directory
            used in the test
        get_python_version_mock (MagicMock): Patch of the 'get_python_version' function to return the "3.7"
        tmp_path (fixture): Temporary Path used for the unit test and cleaned up afterwards

    Scenario: Simulate converting a contribution zip file

    Given
    - A contribution zip file
    - The zipfile contains a unified script file
    - The zipfile contains a unified integration file
    When
    - Converting the zipfile to a valid Pack structure
    Then
    - Ensure the unified yaml files of the integration and script have been removed from the output created by
      converting the contribution zip file
    """
    mocker.patch.object(GitUtil, '__init__', return_value=None)
    mocker.patch.object(GitUtil, 'added_files', return_value=set())
    mocker.patch.object(GitUtil, 'modified_files', return_value=set())
    # ### SETUP ### #
    # Create all Necessary Temporary directories
    # create temp directory for the repo
    repo_dir = tmp_path / 'content_repo'
    repo_dir.mkdir()
    get_content_path_mock.return_value = repo_dir
    get_python_version_mock.return_value = 3.7
    # create temp target dir in which we will create all the TestSuite content items to use in the contribution zip and
    # that will be deleted after
    target_dir = repo_dir / 'target_dir'
    target_dir.mkdir()
    # create temp directory in which the contribution zip will reside
    contribution_zip_dir = tmp_path / 'contrib_zip'
    contribution_zip_dir.mkdir()
    # Create fake content repo and contribution zip
    repo = Repo(repo_dir)
    contrib_zip = Contribution(target_dir, 'ContribTestPack', repo)
    contrib_zip.create_zip(contribution_zip_dir)
    # rename script-script0.yml unified to automation-script0.yml
    # this naming is aligned to how the server exports scripts in contribution zips
    rename_file_in_zip(
        contrib_zip.created_zip_filepath, 'automation/script-script0.yml', 'automation/automation-script0.yml'
    )

    # Convert Zip
    name = 'Contrib Test Pack'
    contribution_path = contrib_zip.created_zip_filepath
    description = 'test pack description here'
    author = 'Octocat Smith'
    contrib_converter_inst = ContributionConverter(
        name=name, contribution=contribution_path, description=description, author=author, no_pipenv=True)
    contrib_converter_inst.convert_contribution_to_pack()

    # Ensure directory/file structure output by conversion meets expectations

    # target_dir should have been deleted after creation of the zip file
    assert not target_dir.exists()

    converted_pack_path = repo_dir / 'Packs' / 'ContribTestPack'
    assert converted_pack_path.exists()

    scripts_path = converted_pack_path / 'Scripts'
    sample_script_path = scripts_path / 'SampleScript'
    script_yml = sample_script_path / 'SampleScript.yml'
    script_py = sample_script_path / 'SampleScript.py'
    script_readme_md = sample_script_path / 'README.md'
    unified_script_in_sample = sample_script_path / 'automation-script0.yml'
    unified_script = scripts_path / 'automation-script0.yml'

    assert scripts_path.exists()
    assert sample_script_path.exists()
    assert script_yml.exists()
    assert script_py.exists()
    assert script_readme_md.exists()

    # generated script readme should not be empty
    script_statinfo = os.stat(script_readme_md)
    assert script_statinfo and script_statinfo.st_size > 0
    # unified yaml of the script should have been deleted
    assert not unified_script_in_sample.exists()
    assert not unified_script.exists()

    integrations_path = converted_pack_path / 'Integrations'
    sample_integration_path = integrations_path / 'Sample'
    integration_yml = sample_integration_path / 'Sample.yml'
    integration_py = sample_integration_path / 'Sample.py'
    integration_description = sample_integration_path / 'Sample_description.md'
    integration_image = sample_integration_path / 'Sample_image.png'
    integration_readme_md = sample_integration_path / 'README.md'
    unified_yml = integrations_path / 'integration-integration0.yml'
    unified_yml_in_sample = sample_integration_path / 'integration-integration0.yml'
    integration_files = [integration_yml, integration_py, integration_description, integration_image,
                         integration_readme_md]
    for integration_file in integration_files:
        assert integration_file.exists()
    # generated integration readme should not be empty
    statinfo = os.stat(integration_readme_md)
    assert statinfo and statinfo.st_size > 0

    # unified yaml of the integration should have been deleted
    assert not unified_yml.exists()
    assert not unified_yml_in_sample.exists()
    def test_excluded_items_contain_aliased_field(mocker, repo: Repo):
        """
        Given
            - An xsoar-only incident field.
            - An incident field with alias of the first field.
            - A mapper using the xsoar-only field.

        When
            creating an ID set for marketplacev2

        Then
            the ID set should not filter the mapper.

        """
        host = {
            'id': 'incident_host',
            'name': 'Host',
            'cliName': 'host',
            'marketplaces': ['xsoar'],
        }
        common_types_pack = repo.create_pack('CommonTypes')
        common_types_pack.pack_metadata.write_json({
            'name':
            'CommonTypes',
            'currentVersion':
            '1.0.0',
            'marketplaces': ['xsoar', 'marketplacev2'],
        })
        common_types_pack.create_incident_field('Host', content=host)
        common_types_pack.create_incident_type('IncidentType')
        host_name = {
            'id': 'incident_hostname',
            'name': 'Host Name',
            'cliName': 'hostname',
            'marketplaces': ['xsoar', 'marketplacev2'],
            'Aliases': [{
                'cliName': 'host',
                'type': 'shortText',
                'name': 'Host',
            }]
        }

        core_alert_fields_pack = repo.create_pack('CoreAlertFields')
        core_alert_fields_pack.pack_metadata.write_json({
            'name':
            'CoreAlertFields',
            'currentVersion':
            '1.0.0',
            'marketplaces': ['marketplacev2'],
        })
        core_alert_fields_pack.create_incident_field('HostName',
                                                     content=host_name)
        mapper = {
            'id': 'Mapper',
            'type': 'mapping-incoming',
            'mapping': {
                'IncidentType': {
                    'dontMapEventToLabels': False,
                    'internalMapping': {
                        'Host': {
                            'simple': 'blabla'
                        },
                    }
                }
            }
        }
        pack = repo.create_pack('MapperPack')
        pack.pack_metadata.write_json({
            'name':
            'MapperPack',
            'currentVersion':
            '1.0.0',
            'marketplaces': ['xsoar', 'marketplacev2'],
        })
        pack.create_mapper('Mapper', content=mapper)

        with ChangeCWD(repo.path):
            # Circle froze on 3.7 dut to high usage of processing power.
            # pool = Pool(processes=cpu_count() * 2) is the line that in charge of the multiprocessing initiation,
            # so changing `cpu_count` return value to 1 still gives you multiprocessing but with only 2 processors,
            # and not the maximum amount.
            import demisto_sdk.commands.common.update_id_set as uis
            mocker.patch.object(uis, 'cpu_count', return_value=1)
            runner = CliRunner(mix_stderr=False)
            result = runner.invoke(main, [
                CREATE_ID_SET_CMD, '-o', 'id_set_result.json', '--marketplace',
                'marketplacev2'
            ],
                                   catch_exceptions=False)
            assert result.exit_code == 0

        with open(os.path.join(repo.path, 'id_set_result.json')) as file_:
            id_set = json.load(file_)
        assert len(id_set['Mappers']) == 1
Exemple #9
0
def get_repo(request: FixtureRequest,
             tmp_path_factory: TempPathFactory) -> Repo:
    tmp_dir = _mk_tmp(request, tmp_path_factory)
    return Repo(tmp_dir)
Exemple #10
0
def test_convert_contribution_zip(get_content_path_mock, get_python_version_mock, tmp_path, initiator):
    '''Create a fake contribution zip file and test that it is converted to a Pack correctly

    Args:
        get_content_path_mock (MagicMock): Patch of the 'get_content_path' function to return the fake repo directory
            used in the test
        get_python_version_mock (MagicMock): Patch of the 'get_python_version' function to return the "3.7"
        tmp_path (fixture): Temporary Path used for the unit test and cleaned up afterwards
        initiator (fixture): Initializes an instance of the 'Initiator' class

    Scenario: Simulate executing the 'init' command with the 'contribution' option passed

    Given
    - A contribution zip file
    - The zipfile contains a unified script file
    - The zipfile contains a unified integration file
    When
    - Converting the zipfile to a valid Pack structure
    Then
    - Ensure script and integration are componentized and in valid directory structure
    '''
    # Create all Necessary Temporary directories
    # create temp directory for the repo
    repo_dir = tmp_path / 'content_repo'
    repo_dir.mkdir()
    get_content_path_mock.return_value = repo_dir
    get_python_version_mock.return_value = 3.7
    # create temp target dir in which we will create all the TestSuite content items to use in the contribution zip and
    # that will be deleted after
    target_dir = repo_dir / 'target_dir'
    target_dir.mkdir()
    # create temp directory in which the contribution zip will reside
    contribution_zip_dir = tmp_path / 'contrib_zip'
    contribution_zip_dir.mkdir()
    # Create fake content repo and contribution zip
    repo = Repo(repo_dir)
    contrib_zip = Contribution(target_dir, 'ContribTestPack', repo)
    # contrib_zip.create_zip(contribution_zip_dir)
    contrib_zip.create_zip(contribution_zip_dir)

    # target_dir should have been deleted after creation of the zip file
    assert not target_dir.exists()

    initiator.contribution = contrib_zip.created_zip_filepath
    initiator.init()

    converted_pack_path = repo_dir / 'Packs' / 'ContribTestPack'
    assert converted_pack_path.exists()

    scripts_path = converted_pack_path / 'Scripts'
    sample_script_path = scripts_path / 'SampleScript'
    script_yml = sample_script_path / 'SampleScript.yml'
    script_py = sample_script_path / 'SampleScript.py'

    assert scripts_path.exists()
    assert sample_script_path.exists()
    assert script_yml.exists()
    assert script_py.exists()

    integrations_path = converted_pack_path / 'Integrations'
    sample_integration_path = integrations_path / 'Sample'
    integration_yml = sample_integration_path / 'Sample.yml'
    integration_py = sample_integration_path / 'Sample.py'
    integration_description = sample_integration_path / 'Sample_description.md'
    integration_image = sample_integration_path / 'Sample_image.png'
    integration_files = [integration_yml, integration_py, integration_description, integration_image]
    for integration_file in integration_files:
        assert integration_file.exists()
Exemple #11
0
def test_convert_contribution_zip_with_args(get_content_path_mock, get_python_version_mock, tmp_path):
    '''Convert a contribution zip to a pack and test that the converted pack's 'pack_metadata.json' is correct

    Args:
        get_content_path_mock (MagicMock): Patch of the 'get_content_path' function to return the fake repo directory
            used in the test
        get_python_version_mock (MagicMock): Patch of the 'get_python_version' function to return the "3.7"
        tmp_path (fixture): Temporary Path used for the unit test and cleaned up afterwards

    Scenario: Simulate executing the 'init' command with the 'contribution' option passed

    Given
    - A contribution zip file
    When
    - The initiator class instance is instantiated with the 'name' argument of 'Test Pack'
    - The initiator class instance is instantiated with the 'description' argument of 'test pack description here'
    - The initiator class instance is instantiated with the 'author' argument of 'Octocat Smith'
    Then
    - Ensure pack with directory name of 'TestPack' is created
    - Ensure that the pack's 'pack_metadata.json' file's 'name' field is 'Test Pack'
    - Ensure that the pack's 'pack_metadata.json' file's 'description' field is 'test pack description here'
    - Ensure that the pack's 'pack_metadata.json' file's 'author' field is 'Octocat Smith'
    - Ensure that the pack's 'pack_metadata.json' file's 'email' field is the empty string
    '''
    # Create all Necessary Temporary directories
    # create temp directory for the repo
    repo_dir = tmp_path / 'content_repo'
    repo_dir.mkdir()
    get_content_path_mock.return_value = repo_dir
    get_python_version_mock.return_value = 3.7
    # create temp target dir in which we will create all the TestSuite content items to use in the contribution zip and
    # that will be deleted after
    target_dir = repo_dir / 'target_dir'
    target_dir.mkdir()
    # create temp directory in which the contribution zip will reside
    contribution_zip_dir = tmp_path / 'contrib_zip'
    contribution_zip_dir.mkdir()
    # Create fake content repo and contribution zip
    repo = Repo(repo_dir)
    contrib_zip = Contribution(target_dir, 'ContribTestPack', repo)
    # contrib_zip.create_zip(contribution_zip_dir)
    contrib_zip.create_zip(contribution_zip_dir)

    # target_dir should have been deleted after creation of the zip file
    assert not target_dir.exists()

    name = 'Test Pack'
    contribution_path = contrib_zip.created_zip_filepath
    description = 'test pack description here'
    author = 'Octocat Smith'
    initiator_inst = Initiator('', name=name, contribution=contribution_path, description=description, author=author)
    initiator_inst.init()

    converted_pack_path = repo_dir / 'Packs' / 'TestPack'
    assert converted_pack_path.exists()

    pack_metadata_path = converted_pack_path / 'pack_metadata.json'
    assert pack_metadata_path.exists()
    with open(pack_metadata_path, 'r') as pack_metadata:
        metadata = json.load(pack_metadata)
        assert metadata.get('name', '') == name
        assert metadata.get('description', '') == description
        assert metadata.get('author', '') == author
        assert not metadata.get('email')