def test_insights_tar_path(self): """Testing error for nonjson output path.""" non_tar_file = '/Users/insights.json' 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_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=non_tar_file) with redirect_stdout(report_out): with self.assertRaises(SystemExit): nac.main(args) self.assertEqual(report_out.getvalue().strip(), (messages.OUTPUT_FILE_TYPE % 'tar.gz'))
def test_insights_nonexistent_directory(self): """Testing error for nonexistent directory in output.""" fake_dir = '/kevan/is/awesome/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_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, 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_id_not_exist(self): """Test insights with nonexistent report id.""" 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=400, 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 = (messages.REPORT_NO_INSIGHTS_REPORT_FOR_REPORT_ID % 1) self.assertEqual(report_out.getvalue().strip(), err)
def test_deployments_noncsv_directory(self): """Testing error for noncsv output path.""" non_csv_dir = '/cody/is/awesome/deployments.tar.gz' report_out = StringIO() get_report_url = get_server_location() + \ REPORT_URI + '1/deployments/' 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 = ReportDeploymentsCommand(SUBPARSER) args = Namespace(scan_job_id=None, report_id='1', output_json=False, output_csv=True, path=non_csv_dir) 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_insights_report_as_json(self): """Testing retreiving insights report as json.""" 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_id': 1, 'hosts': {'00968d16-78b7-4bda-ab7d-668f3c0ef1ee': { '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=200, headers={'X-Server-Version': VERSION}, content=buffer_content) nac = ReportInsightsCommand(SUBPARSER) args = Namespace(scan_job_id='1', report_id=None, path=self.test_json_filename) 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_insights_upload_valid_report(self, subprocess): """Testing response with a valid report id.""" subprocess.return_value.communicate.side_effect = self.success_effect subprocess.return_value.returncode = 0 report_out = StringIO() get_report_url = get_server_location() + \ REPORT_URI + '1/insights/' buffer_content = create_tar_buffer( {'insights.json': self.success_json}) with requests_mock.Mocker() as mocker: mocker.get(get_report_url, status_code=200, headers={'X-Server-Version': VERSION}, content=buffer_content) nac = InsightsUploadCommand(SUBPARSER) args = Namespace(report_id='1', scan_job=None, input_file=None, no_gpg=True) with redirect_stdout(report_out): nac.main(args) self.assertIn( messages.REPORT_INSIGHTS_REPORT_SUCCESSFULLY_UPLOADED, report_out.getvalue().strip())
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) 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) err_msg = (messages.WRITE_FILE_ERROR % (self.test_json_filename, '')) self.assertEqual(report_out.getvalue().strip(), err_msg)
def test_download_report_id(self): """Testing download with report id.""" report_out = StringIO() get_report_url = get_server_location() + \ REPORT_URI + '1' get_report_json_data = {'id': 1, 'report': [{'key': 'value'}]} test_dict = dict() test_dict[self.test_tar_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 = ReportDownloadCommand(SUBPARSER) args = Namespace(scan_job_id=None, report_id='1', path=self.test_tar_filename) with redirect_stdout(report_out): nac.main(args) self.assertEqual( report_out.getvalue().strip(), messages.DOWNLOAD_SUCCESSFULLY_WRITTEN % ('1', self.test_tar_filename))