Beispiel #1
0
    def test_download_scan_job(self):
        """Testing download 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'
        get_report_json_data = {'id': 1, 'report': [{'key': 'value'}]}
        test_dict = dict()
        test_dict['test.json'] = 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 = ReportDownloadCommand(SUBPARSER)
            args = Namespace(scan_job_id='1',
                             report_id=None,
                             path=self.test_tar_filename,
                             mask=False)
            with redirect_stdout(report_out):
                nac.main(args)
                self.assertEqual(report_out.getvalue().strip(),
                                 messages.DOWNLOAD_SUCCESSFULLY_WRITTEN %
                                 ('1', self.test_tar_filename))
Beispiel #2
0
 def test_file_fails_to_write(self, file):
     """Testing download failure while writing to file."""
     err = 'Mock Fail'
     file.side_effect = OSError(err)
     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,
                          mask=False)
         with redirect_stdout(report_out):
             with self.assertRaises(SystemExit):
                 nac.main(args)
             err_msg = (messages.WRITE_FILE_ERROR %
                        (self.test_tar_filename, err))
             self.assertEqual(report_out.getvalue().strip(), err_msg)
Beispiel #3
0
 def test_download_invalid_scan_job(self):
     """Testing download 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 = ReportDownloadCommand(SUBPARSER)
         args = Namespace(scan_job_id='1',
                          report_id=None,
                          path=self.test_tar_filename,
                          mask=False)
         with redirect_stdout(report_out):
             with self.assertRaises(SystemExit):
                 nac.main(args)
             self.assertEqual(report_out.getvalue().strip(),
                              messages.DOWNLOAD_NO_REPORT_FOR_SJ % '1')
Beispiel #4
0
 def test_download_scan_job_not_exist(self):
     """Testing download 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 = ReportDownloadCommand(SUBPARSER)
         args = Namespace(scan_job_id='1',
                          report_id=None,
                          path=self.test_tar_filename)
         with redirect_stdout(report_out):
             with self.assertRaises(SystemExit):
                 nac.main(args)
             self.assertEqual(report_out.getvalue().strip(),
                              messages.DOWNLOAD_SJ_DOES_NOT_EXIST % 1)
Beispiel #5
0
 def test_download_report_id_428(self):
     """Test download with nonexistent report id."""
     report_out = StringIO()
     get_report_url = get_server_location() + \
         REPORT_URI + '1'
     get_report_json_data = {'id': 1, 'report': [{'key': 'value'}]}
     with requests_mock.Mocker() as mocker:
         mocker.get(get_report_url, status_code=428,
                    headers={'X-Server-Version': VERSION},
                    json=get_report_json_data)
         nac = ReportDownloadCommand(SUBPARSER)
         args = Namespace(scan_job_id=None,
                          report_id='1',
                          path=self.test_tar_filename,
                          mask=False)
         with redirect_stdout(report_out):
             with self.assertRaises(SystemExit):
                 nac.main(args)
             self.assertEqual(report_out.getvalue().strip(),
                              messages.DOWNLOAD_NO_MASK_REPORT % 1)
Beispiel #6
0
 def test_output_is_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'
     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, content=buffer_content)
         nac = ReportDownloadCommand(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)))
Beispiel #7
0
 def test_download_from_server_with_old_version(self):
     """Test download with nonexistent report id."""
     report_out = StringIO()
     get_report_url = get_server_location() + \
         REPORT_URI + '1'
     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 = ReportDownloadCommand(SUBPARSER)
         args = Namespace(scan_job_id=None,
                          report_id=1,
                          path=self.test_tar_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'))
Beispiel #8
0
 def test_download_bad_file_extension(self):
     """Test download with bad file extension."""
     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='test.json')
         with redirect_stdout(report_out):
             with self.assertRaises(SystemExit):
                 nac.main(args)
             self.assertEqual(report_out.getvalue().strip(),
                              messages.DOWNLOAD_REQUIRE_TAR)