def test_assert_production_bucket_version_matches_release_notes_version_negative( self, dummy_pack): """ Given: - Changelog from production bucket and the current branch's latest version of a given pack When: - current branch's latest version is lower than the production bucket version Then: - assertion should fail since this branch is not updated from master """ changelog = { '1.0.0': { 'releaseNotes': 'First release notes', 'displayName': '1.0.0', 'released': '2020-05-05T13:39:33Z' }, '2.0.0': { 'releaseNotes': 'Second release notes', 'displayName': '2.0.0', 'released': '2020-06-05T13:39:33Z' } } branch_latest_version = '1.9.9' with pytest.raises(AssertionError) as excinfo: Pack.assert_production_bucket_version_matches_release_notes_version( dummy_pack, changelog, branch_latest_version) assert 'Version mismatch detected between production bucket and current branch' in str( excinfo.value) assert 'Production bucket version: 2.0.0' in str(excinfo.value) assert f'current branch version: {branch_latest_version}' in str( excinfo.value)
def test_is_feed(self, yaml_context, yaml_type, is_actually_feed): """ Tests that is_feed for pack changes if it has a playbook that starts with "TIM " or an integration with script.feed==true """ dummy_pack = Pack(pack_name="TestPack", pack_path="dummy_path") dummy_pack.is_feed_pack(yaml_context, yaml_type) assert dummy_pack.is_feed == is_actually_feed
def test_assert_production_bucket_version_matches_release_notes_version_positive( self, dummy_pack): """ Given: - Changelog from production bucket and the current branch's latest version of a given pack When: - current branch's latest version is higher than the production bucket version Then: - assertion should pass, since this branch probably adds a new version to the pack """ changelog = { "1.0.0": { "releaseNotes": "First release notes", "displayName": "1.0.0", "released": "2020-05-05T13:39:33Z" }, "2.0.0": { "releaseNotes": "Second release notes", "displayName": "2.0.0", "released": "2020-06-05T13:39:33Z" } } branch_latest_version = '2.0.1' Pack.assert_production_bucket_version_matches_release_notes_version( dummy_pack, changelog, branch_latest_version)
def test_remove_unwanted_files(self): """ Given: - Pack name & path. When: - Preparing packs before uploading to marketplace. Then: - Assert `TestPlaybooks` directory was deleted from pack. - Assert `Integrations` directory was not deleted from pack. """ os.mkdir('Tests/Marketplace/Tests/test_data/pack_to_test') os.mkdir( 'Tests/Marketplace/Tests/test_data/pack_to_test/TestPlaybooks') os.mkdir('Tests/Marketplace/Tests/test_data/pack_to_test/Integrations') os.mkdir( 'Tests/Marketplace/Tests/test_data/pack_to_test/TestPlaybooks/NonCircleTests' ) test_pack = Pack( pack_name="pack_to_test", pack_path='Tests/Marketplace/Tests/test_data/pack_to_test') test_pack.remove_unwanted_files() assert not os.path.isdir( 'Tests/Marketplace/Tests/test_data/pack_to_test/TestPlaybooks') assert os.path.isdir( 'Tests/Marketplace/Tests/test_data/pack_to_test/Integrations') shutil.rmtree('Tests/Marketplace/Tests/test_data/pack_to_test')
def dummy_pack(self): """ dummy pack fixture """ dummy_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_data") sample_pack = Pack(pack_name="TestPack", pack_path=dummy_path) sample_pack.description = 'Sample description' sample_pack.current_version = '1.0.0' return sample_pack
def test_set_dependencies_new_dependencies(self): """ Given: - Pack with user dependencies - New generated dependencies When: - Formatting metadata Then: - The dependencies in the metadata file should be merged with the generated ones """ from Tests.Marketplace.marketplace_services import Pack metadata = self.get_pack_metadata() generated_dependencies = { 'ImpossibleTraveler': { 'dependencies': { 'HelloWorld': { 'mandatory': False, 'minVersion': '1.0.0', 'author': 'Cortex XSOAR', 'name': 'HelloWorld', 'certification': 'certified' }, 'ServiceNow': { 'mandatory': True, 'minVersion': '1.0.0', 'author': 'Cortex XSOAR', 'name': 'ServiceNow', 'certification': 'certified' }, 'Ipstack': { 'mandatory': False, 'minVersion': '1.0.0', 'author': 'Cortex XSOAR', 'name': 'Ipstack', 'certification': 'certified' }, 'Active_Directory_Query': { 'mandatory': True, 'minVersion': '1.0.0', 'author': 'Cortex XSOAR', 'name': 'Active Directory Query v2', 'certification': 'certified' } } } } p = Pack('ImpossibleTraveler', 'dummy_path') dependencies = json.dumps(metadata['dependencies']) dependencies = json.loads(dependencies) dependencies.update( generated_dependencies['ImpossibleTraveler']['dependencies']) p.set_pack_dependencies(metadata, generated_dependencies) assert metadata['dependencies'] == dependencies
def test_prepare_release_notes_upgrade_version_dup(self, mocker, dummy_pack): # TODO When we move to not overriding packs in the build, we will need to change result returned. """ Given: - Valid new version and valid current changelog found in index with existing version. When: - Not updating versions and adding to changelog.json Then: - return True """ dummy_pack.current_version = '2.0.0' mocker.patch("os.path.exists", return_value=True) mocker.patch("Tests.Marketplace.marketplace_services.print_warning") mocker.patch("Tests.Marketplace.marketplace_services.print_color") dir_list = ['1_0_1.md', '2_0_0.md'] mocker.patch("os.listdir", return_value=dir_list) original_changelog = '''{ "1.0.0": { "releaseNotes": "First release notes", "displayName": "1.0.0", "released": "2020-05-05T13:39:33Z" }, "2.0.0": { "releaseNotes": "Second release notes", "displayName": "2.0.0", "released": "2020-06-05T13:39:33Z" } }''' mocker.patch('builtins.open', mock_open(read_data=original_changelog)) dummy_path = 'Irrelevant/Test/Path' result = Pack.prepare_release_notes(self=dummy_pack, index_folder_path=dummy_path) assert result is True
def test_prepare_release_notes_upgrade_version_mismatch(self, mocker, dummy_pack): """ Given: - Invalid new version and valid current changelog found in index. Mismatching versions. When: - Upgrading versions and adding to changelog.json Then: - return False """ dummy_pack.current_version = '2.0.0' mocker.patch("Tests.Marketplace.marketplace_services.print_error") mocker.patch("Tests.Marketplace.marketplace_services.print_color") mocker.patch("os.path.exists", return_value=True) dir_list = ['1_0_1.md', '2_0_2.md', '2_0_0.md'] mocker.patch("os.listdir", return_value=dir_list) original_changelog = '''{ "1.0.0": { "releaseNotes": "First release notes", "displayName": "1.0.0", "released": "2020-05-05T13:39:33Z" }, "2.0.0": { "releaseNotes": "Second release notes", "displayName": "2.0.0", "released": "2020-06-05T13:39:33Z" } }''' mocker.patch('builtins.open', mock_open(read_data=original_changelog)) dummy_path = 'Irrelevant/Test/Path' result = Pack.prepare_release_notes(self=dummy_pack, index_folder_path=dummy_path) assert result is False
def test_prepare_release_notes_first_run(self, mocker, dummy_pack): """ In case changelog.json doesn't exists, expected result should be initial version 1.0.0 """ mocker.patch("os.path.exists", return_value=False) dummy_path = 'Irrelevant/Test/Path' result = Pack.prepare_release_notes(self=dummy_pack, index_folder_path=dummy_path) assert result is True
def test_validate_fields_of_parsed_metadata(self): parsed_metadata = Pack._parse_pack_metadata( user_metadata=USER_PACK_METADATA, pack_content_items={}, pack_id='test_pack_id', integration_images=[], author_image="", dependencies_data={}) assert parsed_metadata['name'] == 'Test Pack Name' assert parsed_metadata['id'] == 'test_pack_id' assert parsed_metadata['description'] == 'Description of test pack' assert 'created' in parsed_metadata assert 'updated' in parsed_metadata assert parsed_metadata['legacy'] assert parsed_metadata['support'] == 'demisto' assert parsed_metadata['supportDetails']['url'] == 'https://test.com' assert parsed_metadata['supportDetails']['email'] == '*****@*****.**' assert 'authorImage' in parsed_metadata assert not parsed_metadata['beta'] assert not parsed_metadata['deprecated'] assert 'certification' in parsed_metadata assert parsed_metadata['price'] == 120 assert parsed_metadata['serverMinVersion'] == '5.5.0' assert 'serverLicense' in parsed_metadata assert parsed_metadata['currentVersion'] == '2.3.0' assert 'general' in parsed_metadata assert parsed_metadata['tags'] == ["tag1", "tag2"] assert parsed_metadata['categories'] == ["Messaging"] assert parsed_metadata['contentItems'] == {} assert 'integrations' in parsed_metadata assert parsed_metadata['useCases'] == ["usecase1"] assert parsed_metadata['keywords'] == ["keyword1", "keyword2"] assert 'dependencies' in parsed_metadata
def test_prepare_release_notes_upgrade_version_dup(self, mocker, dummy_pack): """ Given: - Valid new version and valid current changelog found in index with existing version. When: - Not updating versions and adding to changelog.json Then: - return True """ dummy_pack.current_version = '2.0.0' mocker.patch("os.path.exists", return_value=True) mocker.patch("Tests.Marketplace.marketplace_services.print_warning") mocker.patch("Tests.Marketplace.marketplace_services.print_color") dir_list = ['1_0_1.md', '2_0_0.md'] mocker.patch("os.listdir", return_value=dir_list) original_changelog = '''{ "1.0.0": { "releaseNotes": "First release notes", "displayName": "1.0.0", "released": "2020-05-05T13:39:33Z" }, "2.0.0": { "releaseNotes": "Second release notes", "displayName": "2.0.0", "released": "2020-06-05T13:39:33Z" } }''' mocker.patch('builtins.open', mock_open(read_data=original_changelog)) dummy_path = 'Irrelevant/Test/Path' build_number = random.randint(0, 100000) task_status, not_updated_build = Pack.prepare_release_notes(self=dummy_pack, index_folder_path=dummy_path, build_number=build_number) assert task_status is True assert not_updated_build is False
def main(): install_logging('Prepare_Content_Packs_For_Testing.log') packs_dir = '/home/runner/work/content-private/content-private/content/artifacts/packs' temp_dir = '/home/runner/work/content-private/content-private/content/temp-dir' if not os.path.exists(packs_dir): os.mkdir(packs_dir) if not os.path.exists(temp_dir): os.mkdir(temp_dir) upload_config = option_handler() path_to_artifacts = upload_config.artifacts_path extract_destination_path = upload_config.extract_path service_account = upload_config.service_account pack_name = upload_config.pack_names storage_base_path = upload_config.storage_base_path storage_client = init_storage_client(service_account) private_testing_bucket_client = storage_client.bucket( GCPConfig.CI_PRIVATE_BUCKET) extract_packs_artifacts(path_to_artifacts, extract_destination_path) path_to_pack = os.path.join(extract_destination_path, pack_name) premium_pack = Pack(pack_name, path_to_pack) upload_premium_pack_to_private_testing_bucket( premium_pack, pack_name, private_testing_bucket_client, storage_base_path)
def test_get_certification_non_xsoar_support(self): """ Tests case when support is set to partner with certified value. Expected result should be certified certification. """ result_certification = Pack._get_certification(support_type="partner", certification="certified") assert result_certification == Metadata.CERTIFIED
def test_validate_all_fields_of_parsed_metadata(self, dummy_pack_metadata): """ Test function for existence of all fields in metadata. Important to maintain it according to #19786 issue. """ parsed_metadata = Pack._parse_pack_metadata(user_metadata=dummy_pack_metadata, pack_content_items={}, pack_id='test_pack_id', integration_images=[], author_image="", dependencies_data={}, server_min_version="5.5.0", build_number="dummy_build_number", commit_hash="dummy_commit", downloads_count=10) assert parsed_metadata['name'] == 'Test Pack Name' assert parsed_metadata['id'] == 'test_pack_id' assert parsed_metadata['description'] == 'Description of test pack' assert 'created' in parsed_metadata assert 'updated' in parsed_metadata assert parsed_metadata['legacy'] assert parsed_metadata['support'] == 'xsoar' assert parsed_metadata['supportDetails']['url'] == 'https://test.com' assert parsed_metadata['supportDetails']['email'] == '*****@*****.**' assert parsed_metadata['author'] == 'Cortex XSOAR' assert 'authorImage' in parsed_metadata assert 'certification' in parsed_metadata assert parsed_metadata['price'] == 0 assert parsed_metadata['serverMinVersion'] == '5.5.0' assert parsed_metadata['currentVersion'] == '2.3.0' assert parsed_metadata['versionInfo'] == "dummy_build_number" assert parsed_metadata['commit'] == "dummy_commit" assert parsed_metadata['tags'] == ["tag number one", "Tag number two"] assert parsed_metadata['categories'] == ["Messaging"] assert parsed_metadata['contentItems'] == {} assert 'integrations' in parsed_metadata assert parsed_metadata['useCases'] == ["Some Use Case"] assert parsed_metadata['keywords'] == ["dummy keyword", "Additional dummy keyword"] assert parsed_metadata['downloads'] == 10 assert 'dependencies' in parsed_metadata
def test_parsed_metadata_empty_input(self): """ Test for empty pack_metadata.json and validating that support, support details and author are set correctly to XSOAR defaults value of Metadata class. """ parsed_metadata = Pack._parse_pack_metadata( user_metadata={}, pack_content_items={}, pack_id='test_pack_id', integration_images=[], author_image="", dependencies_data={}, server_min_version="dummy_server_version", build_number="dummy_build_number") assert parsed_metadata['name'] == "test_pack_id" assert parsed_metadata['id'] == "test_pack_id" assert parsed_metadata['description'] == "test_pack_id" assert parsed_metadata['legacy'] assert parsed_metadata['support'] == Metadata.XSOAR_SUPPORT assert parsed_metadata['supportDetails'][ 'url'] == Metadata.XSOAR_SUPPORT_URL assert parsed_metadata['author'] == Metadata.XSOAR_AUTHOR assert parsed_metadata['certification'] == Metadata.CERTIFIED assert parsed_metadata['price'] == 0 assert parsed_metadata['serverMinVersion'] == "dummy_server_version"
def test_get_author_non_xsoar_support(self, author, expected): """ Test case when support is set to non xsoar, in that case partner. Expected behavior is not to override author str that was received as input. """ result_author = Pack._get_author(support_type="partner", author=author) assert result_author == expected
def test_get_certification_non_xsoar_support_empty(self, support_type, certification): """ Tests case when support is set to non xsoar. Expected result should empty certification string. """ result_certification = Pack._get_certification( support_type=support_type, certification=certification) assert result_certification == ""
def test_get_certification_xsoar_support(self, support_type, certification): """ Tests case when support is set to xsoar. Expected result should be certified certification. """ result_certification = Pack._get_certification( support_type=support_type, certification=certification) assert result_certification == Metadata.CERTIFIED
def test_set_dependencies_core_pack_mandatory_dependency_override(self): """ Given: - Core pack with new dependencies - Mandatory dependencies that are not core packs that were overridden in the user metadata When: - Formatting metadata Then: - Metadata should be formatted correctly """ from Tests.Marketplace.marketplace_services import Pack metadata = self.get_pack_metadata() generated_dependencies = { 'HelloWorld': { 'dependencies': { 'CommonPlaybooks': { 'mandatory': True, 'minVersion': '1.0.0', 'author': 'Cortex XSOAR', 'name': 'ServiceNow', 'certification': 'certified' }, 'Ipstack': { 'mandatory': True, 'minVersion': '1.0.0', 'author': 'Cortex XSOAR', 'name': 'Ipstack', 'certification': 'certified' } } } } p = Pack('HelloWorld', 'dummy_path') user_dependencies = metadata['dependencies'] dependencies = json.dumps( generated_dependencies['HelloWorld']['dependencies']) dependencies = json.loads(dependencies) dependencies.update(user_dependencies) p.set_pack_dependencies(metadata, generated_dependencies) assert metadata['dependencies'] == dependencies
def test_tim_tag_added_to_feed_pack(self, dummy_pack_metadata, is_feed_pack, tags): """ Test 'TIM' tag is added if is_feed_pack is True """ parsed_metadata = Pack._parse_pack_metadata(user_metadata=dummy_pack_metadata, pack_content_items={}, pack_id='test_pack_id', integration_images=[], author_image="", dependencies_data={}, server_min_version="5.5.0", build_number="dummy_build_number", commit_hash="dummy_commit", downloads_count=10, is_feed_pack=True) assert parsed_metadata['tags'] == ["tag number one", "Tag number two", 'TIM']
def test_set_dependencies_no_generated_dependencies(self): """ Given: - Pack with user dependencies - No generated dependencies When: - Formatting metadata Then: - The dependencies in the metadata file should be the user ones """ from Tests.Marketplace.marketplace_services import Pack metadata = self.get_pack_metadata() dependencies = metadata['dependencies'] p = Pack('ImpossibleTraveler', 'dummy_path') p.set_pack_dependencies(metadata, {}) assert metadata['dependencies'] == dependencies
def test_empty_create_support_section_with_other_support(self, support_type, support_url, support_email): """ Tests case when support is set to non xsoar, one of following: partner, developer or nonsupported. Expected not do override the url with XSOAR default support url and email if it be included eventually. """ support_details = Pack._create_support_section(support_type=support_type, support_url=support_url, support_email=support_email) assert support_details == {}
def test_empty_create_support_section_with_xsoar_support(self, support_url, support_email): """ Test the case when support type is set to xsoar and returns XSOAR support default details. Currently is only returned url field. May include XSOAR support email in the future. """ support_details = Pack._create_support_section(support_type="xsoar", support_url=support_url, support_email=support_email) expected_result = {'url': Metadata.XSOAR_SUPPORT_URL} assert support_details == expected_result
def test_set_dependencies_core_pack_new_mandatory_dependency(self): """ Given: - Core pack with new dependencies - Mandatory dependencies that are not core packs When: - Formatting metadata Then: - An exception should be raised """ from Tests.Marketplace.marketplace_services import Pack metadata = self.get_pack_metadata() generated_dependencies = { 'HelloWorld': { 'dependencies': { 'CommonPlaybooks': { 'mandatory': True, 'minVersion': '1.0.0', 'author': 'Cortex XSOAR', 'name': 'ServiceNow', 'certification': 'certified' }, 'SlackV2': { 'mandatory': True, 'minVersion': '1.0.0', 'author': 'Cortex XSOAR', 'name': 'Ipstack', 'certification': 'certified' } } } } metadata['dependencies'] = {} p = Pack('HelloWorld', 'dummy_path') with pytest.raises(Exception) as e: p.set_pack_dependencies(metadata, generated_dependencies) assert str( e.value ) == "New mandatory dependencies ['SlackV2'] were found in the core pack HelloWorld"
def test_parsed_metadata_with_price(self, pack_metadata_input, expected, mocker): """ Price field is not mandatory field and needs to be set to integer value. """ mocker.patch("Tests.Marketplace.marketplace_services.print_warning") parsed_metadata = Pack._parse_pack_metadata(user_metadata=pack_metadata_input, pack_content_items={}, pack_id="test_pack_id", integration_images=[], author_image="", dependencies_data={}, server_min_version="dummy_server_version") assert parsed_metadata['price'] == expected
def test_prepare_release_notes_first_run(self, mocker, dummy_pack): """ In case changelog.json doesn't exists, expected result should be initial version 1.0.0 """ mocker.patch("os.path.exists", return_value=False) dummy_path = 'Irrelevant/Test/Path' build_number = random.randint(0, 100000) task_status, not_updated_build = Pack.prepare_release_notes(self=dummy_pack, index_folder_path=dummy_path, build_number=build_number) assert task_status is True assert not_updated_build is False
def test_parsed_metadata_empty_input(self, empty_metadata): parsed_metadata = Pack._parse_pack_metadata( user_metadata=empty_metadata, pack_content_items={}, pack_id='test_pack_id', integration_images=[], author_image="", dependencies_data={}) assert parsed_metadata['name'] == "test_pack_id" assert parsed_metadata['id'] == "test_pack_id" assert parsed_metadata['description'] == "test_pack_id" assert parsed_metadata['supportDetails'] == {}
def test_set_dependencies_core_pack(self): """ Given: - Core pack with new dependencies - No mandatory dependencies that are not core packs When: - Formatting metadata Then: - The dependencies in the metadata file should be merged """ from Tests.Marketplace.marketplace_services import Pack metadata = self.get_pack_metadata() generated_dependencies = { 'HelloWorld': { 'dependencies': { 'CommonPlaybooks': { 'mandatory': True, 'minVersion': '1.0.0', 'author': 'Cortex XSOAR', 'name': 'ServiceNow', 'certification': 'certified' } } } } metadata['dependencies'] = {} metadata['name'] = 'HelloWorld' metadata['id'] = 'HelloWorld' p = Pack('HelloWorld', 'dummy_path') dependencies = json.dumps( generated_dependencies['HelloWorld']['dependencies']) dependencies = json.loads(dependencies) p.set_pack_dependencies(metadata, generated_dependencies) assert metadata['dependencies'] == dependencies
def test_clean_release_notes_lines(self): original_rn = ''' ### Integration - __SomeIntegration__ This is visible <!-- This is not --> ''' expected_rn = ''' ### Integration - __SomeIntegration__ This is visible ''' clean_rn = Pack._clean_release_notes(original_rn) assert expected_rn == clean_rn
def test_create_changelog_entry_existing(self): """ Given: - release notes, display version and build number When: - changelog entry already exists Then: - return changelog entry with release notes and R letter appended in display name """ release_notes = "dummy release notes" version_display_name = "1.2.3" build_number = "5555" version_changelog = Pack._create_changelog_entry(release_notes=release_notes, version_display_name=version_display_name, build_number=build_number, new_version=False) assert version_changelog['releaseNotes'] == "dummy release notes" assert version_changelog['displayName'] == f'{version_display_name} - R{build_number}'