def test_detail_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/details/' 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_scanjob_url, status_code=200, json=get_scanjob_json_data) mocker.get(get_report_url, status_code=400, content=buffer_content, headers={'X-Server-Version': VERSION}) nac = ReportDetailsCommand(SUBPARSER) args = Namespace(scan_job_id='1', report_id=None, output_json=True, output_csv=False, path=self.test_json_filename, mask=False) with redirect_stdout(report_out): with self.assertRaises(SystemExit): nac.main(args) self.assertEqual(report_out.getvalue().strip(), messages.REPORT_NO_DETAIL_REPORT_FOR_SJ % 1)
def test_detail_report_as_json_report_id(self): """Testing retreiving detail report as json with report id.""" report_out = StringIO() get_report_url = get_server_location() + \ REPORT_URI + '1/details/' 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, headers={'X-Server-Version': VERSION}) nac = ReportDetailsCommand(SUBPARSER) args = Namespace(scan_job_id=None, report_id='1', output_json=True, output_csv=False, path=self.test_json_filename, mask=False) with redirect_stdout(report_out): nac.main(args) self.assertEqual(report_out.getvalue().strip(), messages.REPORT_SUCCESSFULLY_WRITTEN) with open(self.test_json_filename, 'r') as json_file: data = json_file.read() file_content_dict = json.loads(data) self.assertDictEqual(get_report_json_data, file_content_dict)
def test_details_noncsv_path(self): """Testing error for noncsv file path.""" non_csv_dir = '/Users/details.tar.gz' report_out = StringIO() get_report_url = get_server_location() + \ REPORT_URI + '1/details/' 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 = ReportDetailsCommand(SUBPARSER) args = Namespace(scan_job_id=None, report_id='1', output_json=False, output_csv=True, path=non_csv_dir, mask=False) with redirect_stdout(report_out): with self.assertRaises(SystemExit): nac.main(args) self.assertEqual(report_out.getvalue().strip(), (messages.OUTPUT_FILE_TYPE % '.csv'))
def test_details_nonexistent_directory(self): """Testing error for nonexistent directory in output.""" fake_dir = '/cody/is/awesome/details.json' report_out = StringIO() get_report_url = get_server_location() + \ REPORT_URI + '1/details/' 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 = ReportDetailsCommand(SUBPARSER) args = Namespace(scan_job_id=None, report_id='1', output_json=True, output_csv=False, path=fake_dir, mask=False) 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_details_file_fails_to_write(self, file): """Testing details failure while writing to file.""" file.side_effect = EnvironmentError() report_out = StringIO() get_report_url = get_server_location() + \ REPORT_URI + '1/details/' 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, headers={'X-Server-Version': VERSION}) nac = ReportDetailsCommand(SUBPARSER) args = Namespace(scan_job_id=None, report_id='1', output_json=True, output_csv=False, path=self.test_json_filename, mask=False) 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_detail_report_invalid_scan_job(self): """Details 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 = ReportDetailsCommand(SUBPARSER) args = Namespace(scan_job_id='1', report_id=None, output_json=True, output_csv=False, path=self.test_json_filename, mask=False) with self.assertRaises(SystemExit): with redirect_stdout(report_out): nac.main(args) self.assertEqual(report_out.getvalue(), messages.REPORT_NO_DETAIL_REPORT_FOR_SJ)
def test_details_report_id_not_exist(self): """Test details with nonexistent report id.""" report_out = StringIO() get_report_url = get_server_location() + \ REPORT_URI + '1/details/' get_report_json_data = {'id': 1, 'report': [{'key': 'value'}]} with requests_mock.Mocker() as mocker: mocker.get(get_report_url, status_code=400, json=get_report_json_data) nac = ReportDetailsCommand(SUBPARSER) args = Namespace(scan_job_id=None, report_id='1', output_json=True, output_csv=False, path=self.test_json_filename) with redirect_stdout(report_out): with self.assertRaises(SystemExit): nac.main(args) self.assertEqual( report_out.getvalue().strip(), messages.REPORT_NO_DETAIL_REPORT_FOR_REPORT_ID % 1)
def test_details_old_version(self): """Test too old server version.""" report_out = StringIO() get_report_url = get_server_location() + \ REPORT_URI + '1/details/' get_report_json_data = {'id': 1, 'report': [{'key': 'value'}]} with requests_mock.Mocker() as mocker: mocker.get(get_report_url, status_code=400, headers={'X-Server-Version': '0.0.45'}, json=get_report_json_data) nac = ReportDetailsCommand(SUBPARSER) args = Namespace(scan_job_id=None, report_id='1', output_json=False, output_csv=True, path=self.test_csv_filename, mask=False) with redirect_stdout(report_out): with self.assertRaises(SystemExit): nac.main(args) self.assertEqual(report_out.getvalue().strip(), messages.SERVER_TOO_OLD_FOR_CLI % ('0.9.2', '0.9.2', '0.0.45'))
def test_detail_report_as_csv_masked(self): """Testing retreiving csv details report with masked query param.""" 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/details/' + '?mask=True' get_report_csv_data = 'Report\n' get_report_csv_data += '1\n\n\n' get_report_csv_data += 'key\n' get_report_csv_data += 'value\n' get_report_csv_data = {'id': 1, 'report': [{'key': 'value'}]} 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=200, json=get_report_csv_data, headers={'X-Server-Version': VERSION}) nac = ReportDetailsCommand(SUBPARSER) args = Namespace(scan_job_id='1', report_id=None, output_json=False, output_csv=True, path=self.test_csv_filename, mask=True) with redirect_stdout(report_out): nac.main(args) self.assertEqual(report_out.getvalue().strip(), messages.REPORT_SUCCESSFULLY_WRITTEN) with open(self.test_csv_filename, 'r') as json_file: data = json_file.read() file_content_dict = json.loads(data) print(file_content_dict) self.assertDictEqual(get_report_csv_data, file_content_dict)