Example #1
0
    def test_summary_report_as_json(self):
        """Testing retreiving summary 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/deployments/'
        get_report_json_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_json_data)
            nac = ReportSummaryCommand(SUBPARSER)
            args = Namespace(scan_job_id='1',
                             report_id=None,
                             output_json=True,
                             output_csv=False,
                             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)
Example #2
0
    def test_summary_report_invalid_scan_job(self):
        """Summary 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 = ReportSummaryCommand(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_SUMMARY_REPORT_FOR_SJ)
Example #3
0
    def test_summary_report_scan_job_not_exist(self):
        """Summary report with nonexistent scanjob."""
        report_out = StringIO()

        get_scanjob_url = get_server_location() + \
            SCAN_URI + '1'
        get_scanjob_json_data = {'id': 1, 'fact_collection_id': 1}
        with requests_mock.Mocker() as mocker:
            mocker.get(get_scanjob_url,
                       status_code=400,
                       json=get_scanjob_json_data)
            nac = ReportSummaryCommand(SUBPARSER)
            args = Namespace(scan_id='1',
                             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_SJ_DOES_NOT_EXIST)