def test_restart_scan_data(self): """Testing the restart scan command successfully with stubbed data.""" scan_out = StringIO() url = get_server_location() + SCAN_JOB_URI + '1/restart/' with requests_mock.Mocker() as mocker: mocker.put(url, status_code=200, json=None) nsc = ScanRestartCommand(SUBPARSER) args = Namespace(id='1') with redirect_stdout(scan_out): nsc.main(args) expected = messages.SCAN_RESTARTED % '1' + '\n' self.assertEqual(scan_out.getvalue(), expected)
def test_restart_scan_internal_err(self): """Testing the restart scan command with an internal error.""" scan_out = StringIO() url = get_server_location() + SCAN_JOB_URI + '1/restart/' with requests_mock.Mocker() as mocker: mocker.put(url, status_code=500, json={'error': ['Server Error']}) nsc = ScanRestartCommand(SUBPARSER) args = Namespace(id='1') with self.assertRaises(SystemExit): with redirect_stdout(scan_out): nsc.main(args) self.assertEqual(scan_out.getvalue(), 'Server Error')
def test_restart_scan_ssl_err(self): """Testing the restart scan command with a connection error.""" scan_out = StringIO() url = get_server_location() + SCAN_JOB_URI + '1/restart/' with requests_mock.Mocker() as mocker: mocker.put(url, exc=requests.exceptions.SSLError) nsc = ScanRestartCommand(SUBPARSER) args = Namespace(id='1') with self.assertRaises(SystemExit): with redirect_stdout(scan_out): nsc.main(args) self.assertEqual(scan_out.getvalue(), SSL_ERROR_MSG)
def test_restart_scan_conn_err(self): """Testing the restart scan command with a connection error.""" scan_out = StringIO() url = BASE_URL + SCAN_URI + '1/restart/' with requests_mock.Mocker() as mocker: mocker.put(url, exc=requests.exceptions.ConnectTimeout) nsc = ScanRestartCommand(SUBPARSER) args = Namespace(id='1') with self.assertRaises(SystemExit): with redirect_stdout(scan_out): nsc.main(args) self.assertEqual(scan_out.getvalue(), CONNECTION_ERROR_MSG)
def test_restart_scan_data(self): """Testing the restart scan command successfully with stubbed data.""" scan_out = StringIO() url = BASE_URL + SCAN_URI + '1/restart/' scan_entry = {'id': 1, 'source': { 'id': 1, 'name': 'scan1'}, 'scan_type': 'host', 'status': 'completed'} with requests_mock.Mocker() as mocker: mocker.put(url, status_code=200, json=scan_entry) nsc = ScanRestartCommand(SUBPARSER) args = Namespace(id='1') with redirect_stdout(scan_out): nsc.main(args) expected = 'Scan "1" restarted\n' self.assertEqual(scan_out.getvalue(), expected)