示例#1
0
    def test_deployments_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/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_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 = ReportDeploymentsCommand(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_DEPLOYMENTS_REPORT_FOR_SJ %
                                 1)
示例#2
0
    def test_deployments_report_as_json_report_id(self):
        """Testing retreiving deployments report as json with report id."""
        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,
                       headers={'X-Server-Version': VERSION})
            nac = ReportDeploymentsCommand(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)
示例#3
0
 def test_deployments_masked_report_428(self):
     """Deployments report retrieved from report returns 428."""
     report_out = StringIO()
     get_report_url = get_server_location() + \
         REPORT_URI + '1/deployments/' + '?mask=True'
     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=428,
                    content=buffer_content,
                    headers={'X-Server-Version': VERSION})
         nac = ReportDeploymentsCommand(SUBPARSER)
         args = Namespace(scan_job_id=None,
                          report_id='1',
                          output_json=True,
                          output_csv=False,
                          path=self.test_json_filename,
                          mask=True)
         with redirect_stdout(report_out):
             with self.assertRaises(SystemExit):
                 nac.main(args)
             err = (messages.REPORT_COULD_NOT_BE_MASKED_REPORT_ID %
                    1)
             self.assertEqual(report_out.getvalue().strip(), err)
示例#4
0
 def test_deployments_file_fails_to_write(self, file):
     """Testing deployments failure while writing to file."""
     file.side_effect = EnvironmentError()
     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,
                    headers={'X-Server-Version': VERSION})
         nac = ReportDeploymentsCommand(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)
示例#5
0
 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,
                          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'))
示例#6
0
    def test_deployments_report_as_csv(self):
        """Testing retreiving deployments report as csv."""
        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/deployments/'
        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)
            nac = ReportDeploymentsCommand(SUBPARSER)
            args = Namespace(scan_job_id='1',
                             report_id=None,
                             output_json=False,
                             output_csv=True,
                             path=self.test_csv_filename)
            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)
                self.assertDictEqual(get_report_csv_data, file_content_dict)
示例#7
0
    def test_deployments_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 = ReportDeploymentsCommand(SUBPARSER)
            args = Namespace(scan_job_id='1',
                             output_json=True,
                             report_id=None,
                             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_SJ_DOES_NOT_EXIST)
示例#8
0
 def test_deployments_report_id_not_exist(self):
     """Test deployments with nonexistent report id."""
     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=400, content=buffer_content)
         nac = ReportDeploymentsCommand(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 = (messages.REPORT_NO_DEPLOYMENTS_REPORT_FOR_REPORT_ID % 1)
             self.assertEqual(report_out.getvalue().strip(), err)
示例#9
0
 def test_deployments_old_version(self):
     """Test too old server version."""
     report_out = StringIO()
     get_report_url = get_server_location() + \
         REPORT_URI + '1/deployments/'
     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 = ReportDeploymentsCommand(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)
             self.assertEqual(report_out.getvalue().strip(),
                              messages.SERVER_TOO_OLD_FOR_CLI %
                              ('0.9.2', '0.9.2', '0.0.45'))
示例#10
0
    def test_deployments_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 = ReportDeploymentsCommand(SUBPARSER)
            args = Namespace(scan_job_id='1',
                             report_id=None,
                             output_json=True,
                             output_csv=False,
                             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)
示例#11
0
 def test_deployments_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/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=True,
                          output_csv=False,
                          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)))