def test_list_filter_type(self): """Testing the list scan with filter by type.""" scan_out = StringIO() url = get_server_location() + SCAN_URI scan_entry = { 'id': 1, 'scan_type': 'inspect', 'source': { 'id': 1, 'name': 'scan1' } } results = [scan_entry] data = {'count': 1, 'next': None, 'results': results} with requests_mock.Mocker() as mocker: mocker.get(url, status_code=200, json=data) slc = ScanListCommand(SUBPARSER) args = Namespace(type='inspect') with redirect_stdout(scan_out): slc.main(args) expected = '[{"id":1,"scan_type":"inspect"' \ ',"source":{"id":1,"name":"scan1"}'\ '}]' self.assertEqual( scan_out.getvalue().replace('\n', '').replace(' ', '').strip(), expected)
def test_list_scan_data(self, b_input): """Testing the list scan command successfully with stubbed data.""" scan_out = StringIO() url = get_server_location() + SCAN_URI scan_entry = { 'id': 1, 'scan_type': 'inspect', 'source': { 'id': 1, 'name': 'scan1' } } results = [scan_entry] next_link = get_server_location() + SCAN_URI + '?page=2' data = {'count': 1, 'next': next_link, 'results': results} data2 = {'count': 1, 'next': None, 'results': results} with requests_mock.Mocker() as mocker: mocker.get(url, status_code=200, json=data) mocker.get(next_link, status_code=200, json=data2) slc = ScanListCommand(SUBPARSER) args = Namespace() with redirect_stdout(scan_out): slc.main(args) expected = '[{"id":1,"scan_type":"inspect"' \ ',"source":{"id":1,"name":"scan1"}'\ '}]' self.assertEqual( scan_out.getvalue().replace('\n', '').replace(' ', '').strip(), expected + expected) b_input.assert_called_with(ANY)
def test_list_scan_data(self): """Testing the list scan command successfully with stubbed data.""" scan_out = StringIO() url = BASE_URL + SCAN_URI scan_entry = { 'id': 1, 'scan_type': 'inspect', 'source': { 'id': 1, 'name': 'scan1' }, 'status': 'completed' } data = [scan_entry] with requests_mock.Mocker() as mocker: mocker.get(url, status_code=200, json=data) slc = ScanListCommand(SUBPARSER) args = Namespace() with redirect_stdout(scan_out): slc.main(args) expected = '[{"id":1,"scan_type":"inspect"' \ ',"source":{"id":1,"name":"scan1"},'\ '"status":"completed"}]' self.assertEqual( scan_out.getvalue().replace('\n', '').replace(' ', '').strip(), expected)
def test_list_scan_empty(self): """Testing the list scan command successfully with empty data.""" scan_out = StringIO() url = get_server_location() + SCAN_URI with requests_mock.Mocker() as mocker: mocker.get(url, status_code=200, json={'count': 0}) slc = ScanListCommand(SUBPARSER) args = Namespace() with redirect_stdout(scan_out): slc.main(args) self.assertEqual(scan_out.getvalue(), messages.SCAN_LIST_NO_SCANS + '\n')
def test_list_scan_internal_err(self): """Testing the list scan command with an internal error.""" scan_out = StringIO() url = get_server_location() + SCAN_URI with requests_mock.Mocker() as mocker: mocker.get(url, status_code=500, json={'error': ['Server Error']}) slc = ScanListCommand(SUBPARSER) args = Namespace() with self.assertRaises(SystemExit): with redirect_stdout(scan_out): slc.main(args) self.assertEqual(scan_out.getvalue(), 'Server Error')
def test_list_scan_conn_err(self): """Testing the list scan command with a connection error.""" scan_out = StringIO() url = get_server_location() + SCAN_URI with requests_mock.Mocker() as mocker: mocker.get(url, exc=requests.exceptions.ConnectTimeout) slc = ScanListCommand(SUBPARSER) args = Namespace() with self.assertRaises(SystemExit): with redirect_stdout(scan_out): slc.main(args) self.assertEqual(scan_out.getvalue(), CONNECTION_ERROR_MSG)
def test_list_scan_ssl_err(self): """Testing the list scan command with a connection error.""" scan_out = StringIO() url = BASE_URL + SCAN_URI with requests_mock.Mocker() as mocker: mocker.get(url, exc=requests.exceptions.SSLError) slc = ScanListCommand(SUBPARSER) args = Namespace() with self.assertRaises(SystemExit): with redirect_stdout(scan_out): slc.main(args) self.assertEqual(scan_out.getvalue(), SSL_ERROR_MSG)