def test_clear_all_with_error(self): """Testing the clear source command successfully. With stubbed data list of sources with delete error """ source_out = StringIO() get_url = get_server_location() + SOURCE_URI delete_url = get_server_location() + SOURCE_URI + '1/' source_entry = { 'id': 1, 'name': 'source1', 'hosts': ['1.2.3.4'], 'credential': ['credential1', 'cred2'], 'port': 22 } results = [source_entry] data = {'count': 1, 'results': results} err_data = {'error': ['Server Error']} with requests_mock.Mocker() as mocker: mocker.get(get_url, status_code=200, json=data) mocker.delete(delete_url, status_code=500, json=err_data) ncc = SourceClearCommand(SUBPARSER) args = Namespace(name=None) with self.assertRaises(SystemExit): with redirect_stdout(source_out): ncc.main(args) expected = 'Some sources were removed, however and' \ ' error occurred removing the following' \ ' credentials:' self.assertTrue(expected in source_out.getvalue())
def test_clear_by_name_err(self): """Test the clear source command successfully. With stubbed data when specifying a name with an error response """ source_out = StringIO() get_url = get_server_location() + SOURCE_URI + '?name=source1' delete_url = get_server_location() + SOURCE_URI + '1/' source_entry = { 'id': 1, 'name': 'source1', 'hosts': ['1.2.3.4'], 'credential': ['credential1', 'cred2'], 'port': 22 } results = [source_entry] data = {'count': 1, 'results': results} err_data = {'error': ['Server Error']} with requests_mock.Mocker() as mocker: mocker.get(get_url, status_code=200, json=data) mocker.delete(delete_url, status_code=500, json=err_data) ncc = SourceClearCommand(SUBPARSER) args = Namespace(name='source1') with self.assertRaises(SystemExit): with redirect_stdout(source_out): ncc.main(args) expected = 'Failed to remove source "source1"' self.assertTrue(expected in source_out.getvalue())
def test_clear_by_name(self): """Testing the clear source command. Successfully with stubbed data when specifying a name """ source_out = StringIO() get_url = get_server_location() + SOURCE_URI + '?name=source1' delete_url = get_server_location() + SOURCE_URI + '1/' source_entry = { 'id': 1, 'name': 'source1', 'hosts': ['1.2.3.4'], 'credential': ['credential1', 'cred2'], 'port': 22 } results = [source_entry] data = {'count': 1, 'results': results} with requests_mock.Mocker() as mocker: mocker.get(get_url, status_code=200, json=data) mocker.delete(delete_url, status_code=204) ncc = SourceClearCommand(SUBPARSER) args = Namespace(name='source1') with redirect_stdout(source_out): ncc.main(args) expected = messages.SOURCE_REMOVED % 'source1' + '\n' self.assertEqual(source_out.getvalue(), expected)
def test_clear_source_internal_err(self): """Testing the clear source command with an internal error.""" source_out = StringIO() url = get_server_location() + SOURCE_URI + '?name=source1' with requests_mock.Mocker() as mocker: mocker.get(url, status_code=500, json={'error': ['Server Error']}) ncc = SourceClearCommand(SUBPARSER) args = Namespace(name='source1') with self.assertRaises(SystemExit): with redirect_stdout(source_out): ncc.main(args) self.assertEqual(source_out.getvalue(), 'Server Error')
def test_clear_source_ssl_err(self): """Testing the clear source command with a connection error.""" source_out = StringIO() url = get_server_location() + SOURCE_URI + '?name=source1' with requests_mock.Mocker() as mocker: mocker.get(url, exc=requests.exceptions.SSLError) ncc = SourceClearCommand(SUBPARSER) args = Namespace(name='source1') with self.assertRaises(SystemExit): with redirect_stdout(source_out): ncc.main(args) self.assertEqual(source_out.getvalue(), SSL_ERROR_MSG)
def test_clear_source_empty(self): """Testing the clear source command successfully with empty data.""" source_out = StringIO() url = get_server_location() + SOURCE_URI + '?name=source1' with requests_mock.Mocker() as mocker: mocker.get(url, status_code=200, json={'count': 0}) ncc = SourceClearCommand(SUBPARSER) args = Namespace(name='source1') with self.assertRaises(SystemExit): with redirect_stdout(source_out): ncc.main(args) self.assertEqual(source_out.getvalue(), 'Source "source1" was not found\n')
def test_clear_all_empty(self): """Test the clear source command successfully. With stubbed data empty list of sources """ source_out = StringIO() get_url = get_server_location() + SOURCE_URI with requests_mock.Mocker() as mocker: mocker.get(get_url, status_code=200, json={'count': 0}) ncc = SourceClearCommand(SUBPARSER) args = Namespace(name=None) with self.assertRaises(SystemExit): with redirect_stdout(source_out): ncc.main(args) expected = 'No sources exist to be removed\n' self.assertEqual(source_out.getvalue(), expected)
def test_clear_all(self): """Testing the clear source command successfully with stubbed data.""" source_out = StringIO() get_url = BASE_URL + SOURCE_URI delete_url = BASE_URL + SOURCE_URI + '1/' source_entry = { 'id': 1, 'name': 'source1', 'hosts': ['1.2.3.4'], 'credential': ['credential1', 'cred2'], 'port': 22 } data = [source_entry] with requests_mock.Mocker() as mocker: mocker.get(get_url, status_code=200, json=data) mocker.delete(delete_url, status_code=204) ncc = SourceClearCommand(SUBPARSER) args = Namespace(name=None) with redirect_stdout(source_out): ncc.main(args) expected = 'All sources were removed\n' self.assertEqual(source_out.getvalue(), expected)