def __create_incident_with_playbook(client: DefaultApi, name, playbook_id, integrations, logging_manager, ) -> Tuple[Optional[Incident], int]: # create incident create_incident_request = demisto_client.demisto_api.CreateIncidentRequest() create_incident_request.create_investigation = True create_incident_request.playbook_id = playbook_id create_incident_request.name = name try: response = client.create_incident(create_incident_request=create_incident_request) except ApiException: logging_manager.exception(f'Failed to create incident with name {name} for playbook {playbook_id}') try: inc_id = response.id except: # noqa: E722 inc_id = 'incCreateErr' # inc_id = response_json.get('id', 'incCreateErr') if inc_id == 'incCreateErr': integration_names = [integration['name'] for integration in integrations if 'name' in integration] error_message = f'Failed to create incident for integration names: {integration_names} ' \ f'and playbookID: {playbook_id}.' \ 'Possible reasons are:\nMismatch between playbookID in conf.json and ' \ 'the id of the real playbook you were trying to use,' \ 'or schema problems in the TestPlaybook.' logging_manager.error(error_message) return None, -1 # get incident search_filter = demisto_client.demisto_api.SearchIncidentsData() inc_filter = demisto_client.demisto_api.IncidentFilter() inc_filter.query = 'id:' + str(inc_id) # inc_filter.query search_filter.filter = inc_filter incident_search_responses = [] found_incidents = 0 # poll the incidents queue for a max time of 300 seconds timeout = time.time() + 300 while found_incidents < 1: try: incidents = client.search_incidents(filter=search_filter) found_incidents = incidents.total incident_search_responses.append(incidents) except ApiException: logging_manager.exception(f'Searching incident with id {inc_id} failed') if time.time() > timeout: logging_manager.error(f'Got timeout for searching incident with id {inc_id}') logging_manager.error(f'Incident search responses: {incident_search_responses}') return None, -1 time.sleep(10) return incidents.data[0], inc_id
def test_run_repo_test_playbooks(self, mocker, tpb_result, res, massage, capsys): """ Given: - run all repo test playbook with result as True or False When: - run the run_test_playbook command Then: - validate the results is aas expected - validate the num of tpb is as expected (7 tpb in CONTENT_REPO_EXAMPLE_ROOT) """ with ChangeCWD(CONTENT_REPO_EXAMPLE_ROOT): mocker.patch.object(demisto_client, 'configure', return_value=DefaultApi()) mocker.patch.object(TestPlaybookRunner, 'print_tpb_error_details') mocker.patch.object(TestPlaybookRunner, 'create_incident_with_test_playbook', return_value='1234') mocker.patch.object(TestPlaybookRunner, 'get_test_playbook_results_dict', return_value={"state": tpb_result}) result = click.Context(command=run_test_playbook).invoke( run_test_playbook, all=True, test_playbook_path='') assert result == res stdout, _ = capsys.readouterr() assert stdout.count(massage) == 7
def test_zip_with_upload(self, mocker): """ Given: - the upload flag is turn on When: - run the zip_packs command Then: - validate the pack.zipped_pack_uploader was called with correct path """ mocker.patch.object(demisto_client, 'configure', return_value=DefaultApi()) mocker.patch.object(uploader, 'get_demisto_version', return_value=parse('6.0.0')) mocker.patch.object(Uploader, 'zipped_pack_uploader') with temp_dir() as tmp_output_dir: click.Context(command=zip_packs).invoke(zip_packs, input=TEST_PACK_PATH, output=tmp_output_dir, content_version='0.0.0', zip_all=True, upload=True) assert Uploader.zipped_pack_uploader.call_args[1][ 'path'] == f'{tmp_output_dir}/uploadable_packs.zip'
def test_run_pack_test_playbooks(self, mocker, tpb_result, res, massage, capsys): """ Given: - run all pack test playbooks with result as True or False When: - run the run_test_playbook command Then: - validate the results is aas expected - validate the num of tpb is as expected (4 tpb in Azure Pack) """ mocker.patch.object(demisto_client, 'configure', return_value=DefaultApi()) mocker.patch.object(TestPlaybookRunner, 'print_tpb_error_details') mocker.patch.object(TestPlaybookRunner, 'create_incident_with_test_playbook', return_value='1234') mocker.patch.object(TestPlaybookRunner, 'get_test_playbook_results_dict', return_value={"state": tpb_result}) result = click.Context(command=run_test_playbook).invoke( run_test_playbook, test_playbook_path=VALID_PACK) assert result == res stdout, _ = capsys.readouterr() assert stdout.count(massage) == 4
def test_failed_run_test_playbook_by_id(self, mocker, playbook_id, tpb_results, exit_code, capsys): """ Given: - arguments to the xsoar-configuration-file When: - check that the run_test_playbook_by_id works as expected Then: - validate the error code is as expected. - validate the all the massages is as expected. """ mocker.patch.object(demisto_client, 'configure', return_value=DefaultApi()) mocker.patch.object(TestPlaybookRunner, 'print_tpb_error_details') mocker.patch.object(TestPlaybookRunner, 'create_incident_with_test_playbook', return_value='1234') mocker.patch.object(TestPlaybookRunner, 'get_test_playbook_results_dict', return_value={'state': tpb_results}) self.test_playbook_input = TEST_PLAYBOOK test_playbook_runner = TestPlaybookRunner( test_playbook_path=self.test_playbook_input) res = test_playbook_runner.run_test_playbook_by_id(playbook_id) assert res == exit_code stdout, _ = capsys.readouterr() assert WAITING_MASSAGE in stdout assert LINK_MASSAGE in stdout assert FAILED_MASSAGE in stdout
def test_failed_run_test_playbook_manager(self, mocker, input_tpb, exit_code, err, capsys): """ Given: - arguments to the run-test-playbook When: - check that the run-test-playbook works as expected Then: - validate the error code is as expected. - validate the Error massage when the argument is missing """ mocker.patch.object(demisto_client, 'configure', return_value=DefaultApi()) mocker.patch.object(TestPlaybookRunner, 'create_incident_with_test_playbook', return_value='1234') mocker.patch.object(TestPlaybookRunner, 'get_test_playbook_results_dict', return_value={'state': 'success'}) self.test_playbook_input = input_tpb test_playbook = TestPlaybookRunner( test_playbook_path=self.test_playbook_input) error_code = test_playbook.manage_and_run_test_playbooks() assert error_code == exit_code stdout, _ = capsys.readouterr() if err: assert err in stdout
def upload(self, client: DefaultApi): """ Upload the job item to demisto_client Args: client: The demisto_client object of the desired XSOAR machine to upload to. Returns: The result of the upload command from demisto_client """ return client.generic_request(method='POST', path='jobs/import', files={'file': str(self.path)}, content_type='multipart/form-data')
def upload(self, client: DefaultApi): """ Upload the lists item to demisto_client Args: client: The demisto_client object of the desired XSOAR machine to upload to. Returns: The result of the upload command from demisto_client """ response = client.generic_request(method='POST', path='lists/save', body=self.to_dict(), response_type='object')[0] return pprint.pformat(response)
def test_run_specific_test_playbook(self, mocker, tpb_result, res): """ Given: - run specific test playbook with result as True or False When: - run the run_test_playbook command Then: - validate the results is aas expected """ mocker.patch.object(demisto_client, 'configure', return_value=DefaultApi()) mocker.patch.object(TestPlaybookRunner, 'print_tpb_error_details') mocker.patch.object(TestPlaybookRunner, 'create_incident_with_test_playbook', return_value='1234') mocker.patch.object(TestPlaybookRunner, 'get_test_playbook_results_dict', return_value={"state": tpb_result}) result = click.Context(command=run_test_playbook).invoke( run_test_playbook, test_playbook_path=TEST_PLAYBOOK) assert result == res
""" # verify exactly 3 calls to print_color assert secho.call_count == 3 assert secho.call_args_list[0][0][0] == expected_upload_summary_title assert secho.call_args_list[1][0][ 0] == expected_failed_uploaded_files_title assert secho.call_args_list[2][0][0] == expected_failed_uploaded_files TEST_DATA = src_root() / 'commands' / 'upload' / 'tests' / 'data' CONTENT_PACKS_ZIP = str(TEST_DATA / 'content_packs.zip') TEST_PACK_ZIP = str(TEST_DATA / 'TestPack.zip') TEST_PACK = 'Packs/TestPack' INVALID_ZIP = 'invalid_zip' INVALID_ZIP_ERROR = 'Error: Given input path: {path} does not exist' API_CLIENT = DefaultApi() def mock_api_client(mocker): mocker.patch.object(demisto_client, 'configure', return_value=API_CLIENT) mocker.patch.object(uploader, 'get_demisto_version', return_value=parse('6.0.0')) class TestZippedPackUpload: """ Happy path tests: 1. Upload one zipped pack 2. Upload content_artifacts.zip with multiple packs 3. Upload with compile flag