def test_insights_report_error_scan_job(self): """Testing error with scan job id.""" report_out = StringIO() get_scanjob_url = get_server_location() + \ SCAN_JOB_URI + '1' get_scanjob_json_data = {'id': 1, 'report_id': 1} get_report_url = get_server_location() + \ REPORT_URI + '1/insights/' get_report_json_data = {'id': 1, 'report': [{'key': 'value'}]} test_dict = dict() test_dict[self.test_tar_gz_filename] = get_report_json_data buffer_content = create_tar_buffer(test_dict) with requests_mock.Mocker() as mocker: mocker.get(get_scanjob_url, status_code=200, json=get_scanjob_json_data) mocker.get(get_report_url, status_code=400, headers={'X-Server-Version': VERSION}, content=buffer_content) nac = ReportInsightsCommand(SUBPARSER) args = Namespace(scan_job_id='1', report_id=None, path=self.test_tar_gz_filename) with redirect_stdout(report_out): with self.assertRaises(SystemExit): nac.main(args) self.assertEqual(report_out.getvalue().strip(), messages.REPORT_NO_INSIGHTS_REPORT_FOR_SJ % 1)
def test_insights_report_as_json_report_id(self): """Testing retreiving insights report as json with report id.""" report_out = StringIO() get_report_url = get_server_location() + \ REPORT_URI + '1/insights/' get_report_json_data = \ {'id': 1, 'report_id': 1, 'hosts': {'00968d16-78b7-4bda-ab7d-668f3c0ef1ee': { 'key': 'value'}}} test_dict = dict() test_dict[self.test_tar_gz_filename] = get_report_json_data buffer_content = create_tar_buffer(test_dict) with requests_mock.Mocker() as mocker: mocker.get(get_report_url, status_code=200, headers={'X-Server-Version': VERSION}, content=buffer_content) nac = ReportInsightsCommand(SUBPARSER) args = Namespace(scan_job_id=None, report_id='1', path=self.test_tar_gz_filename) with redirect_stdout(report_out): nac.main(args) self.assertEqual(report_out.getvalue().strip(), messages.REPORT_SUCCESSFULLY_WRITTEN)
def test_insights_file_fails_to_write(self, file): """Testing insights failure while writing to file.""" file.side_effect = EnvironmentError() report_out = StringIO() get_report_url = get_server_location() + \ REPORT_URI + '1/insights/' get_report_json_data = {'id': 1, 'report': [{'key': 'value'}]} test_dict = dict() test_dict[self.test_json_filename] = get_report_json_data buffer_content = create_tar_buffer(test_dict) with requests_mock.Mocker() as mocker: mocker.get(get_report_url, status_code=200, headers={'X-Server-Version': VERSION}, content=buffer_content) nac = ReportInsightsCommand(SUBPARSER) args = Namespace(scan_job_id=None, report_id='1', path=self.test_json_filename) with redirect_stdout(report_out): with self.assertRaises(SystemExit): nac.main(args) err_msg = (messages.WRITE_FILE_ERROR % (self.test_json_filename, '')) self.assertEqual(report_out.getvalue().strip(), err_msg)
def test_insights_report_scan_job_not_exist(self): """Deployments report with nonexistent scanjob.""" report_out = StringIO() get_scanjob_url = get_server_location() + \ SCAN_JOB_URI + '1' get_scanjob_json_data = {'id': 1, 'report_id': 1} with requests_mock.Mocker() as mocker: mocker.get(get_scanjob_url, status_code=400, json=get_scanjob_json_data) nac = ReportInsightsCommand(SUBPARSER) args = Namespace(scan_job_id='1', report_id=None, path=self.test_tar_gz_filename) with self.assertRaises(SystemExit): with redirect_stdout(report_out): nac.main(args) self.assertEqual(report_out.getvalue(), messages.REPORT_SJ_DOES_NOT_EXIST)
def test_insights_nonexistent_directory(self): """Testing error for nonexistent directory in output.""" fake_dir = '/cody/is/awesome/' report_out = StringIO() get_report_url = get_server_location() + \ REPORT_URI + '1/insights/' get_report_json_data = {'id': 1, 'report': [{'key': 'value'}]} test_dict = dict() test_dict[self.test_json_filename] = get_report_json_data buffer_content = create_tar_buffer(test_dict) with requests_mock.Mocker() as mocker: mocker.get(get_report_url, status_code=200, content=buffer_content) nac = ReportInsightsCommand(SUBPARSER) args = Namespace(scan_job_id=None, report_id='1', path=fake_dir) with redirect_stdout(report_out): with self.assertRaises(SystemExit): nac.main(args) self.assertEqual(report_out.getvalue().strip(), (messages.REPORT_DIRECTORY_DOES_NOT_EXIST % os.path.dirname(fake_dir)))
def test_insights_report_invalid_scan_job(self): """Deployments report with scanjob but no report_id.""" report_out = StringIO() get_scanjob_url = get_server_location() + \ SCAN_JOB_URI + '1' get_scanjob_json_data = {'id': 1} with requests_mock.Mocker() as mocker: mocker.get(get_scanjob_url, status_code=200, json=get_scanjob_json_data) nac = ReportInsightsCommand(SUBPARSER) args = Namespace(scan_job_id='1', report_id=None, path=self.test_json_filename) with self.assertRaises(SystemExit): with redirect_stdout(report_out): nac.main(args) self.assertEqual( report_out.getvalue(), messages.REPORT_NO_DEPLOYMENTS_REPORT_FOR_SJ)
def test_insights_nonjson_path(self): """Testing error for nonjson output path.""" non_json_dir = '/Users/insights.tar.gz' report_out = StringIO() get_report_url = get_server_location() + \ REPORT_URI + '1/insights/' get_report_json_data = {'id': 1, 'report': [{'key': 'value'}]} test_dict = dict() test_dict[self.test_json_filename] = get_report_json_data buffer_content = create_tar_buffer(test_dict) with requests_mock.Mocker() as mocker: mocker.get(get_report_url, status_code=200, content=buffer_content) nac = ReportInsightsCommand(SUBPARSER) args = Namespace(scan_job_id=None, report_id='1', path=non_json_dir) with redirect_stdout(report_out): with self.assertRaises(SystemExit): nac.main(args) self.assertEqual(report_out.getvalue().strip(), (messages.OUTPUT_FILE_TYPE % '.json'))