def test_edit_ssl_protocol(self): """Testing that you can edit the ssl_protocol arg successfully.""" source_out = StringIO() url_get_cred = get_server_location() + CREDENTIAL_URI + \ '?name=credential1' url_get_source = get_server_location() + SOURCE_URI + '?name=source1' url_patch = get_server_location() + SOURCE_URI + '1/' cred_results = [{'id': 1, 'name': 'credential1', 'username': '******', 'password': '******'}] cred_data = {'count': 1, 'results': cred_results} results = [{'id': 1, 'name': 'source1', 'hosts': ['1.2.3.4'], 'credentials': [{'id': 2, 'name': 'cred2'}], 'ssl_protocol': 'SSLv23'}] source_data = {'count': 1, 'results': results} with requests_mock.Mocker() as mocker: mocker.get(url_get_source, status_code=200, json=source_data) mocker.get(url_get_cred, status_code=200, json=cred_data) mocker.patch(url_patch, status_code=200) aec = SourceEditCommand(SUBPARSER) args = Namespace(name='source1', hosts=['1.2.3.5'], cred=['credential1'], ssl_protocol='SSLv23') with redirect_stdout(source_out): aec.main(args) self.assertEqual(source_out.getvalue(), messages.SOURCE_UPDATED % 'source1' + '\n')
def test_edit_source_cred_err(self): """Testing the edit source command where cred request hits error.""" source_out = StringIO() url_get_cred = BASE_URL + CREDENTIAL_URI + '?name=credential1%2Ccred2' url_get_source = BASE_URL + SOURCE_URI + '?name=source1' source_data = [{ 'id': 1, 'name': 'source1', 'hosts': ['1.2.3.4'], 'credentials': [{ 'id': 2, 'name': 'cred2' }] }] with requests_mock.Mocker() as mocker: mocker.get(url_get_source, status_code=200, json=source_data) mocker.get(url_get_cred, status_code=500) aec = SourceEditCommand(SUBPARSER) args = Namespace(name='source1', hosts=['1.2.3.4'], cred=['credential1', 'cred2'], port=22) with self.assertRaises(SystemExit): with redirect_stdout(source_out): aec.main(args) self.assertTrue( 'An error occurred while processing ' 'the "--cred" input' in source_out.getvalue())
def test_edit_vc_source(self): """Testing the edit vcenter source command successfully.""" source_out = StringIO() url_get_cred = BASE_URL + CREDENTIAL_URI + '?name=credential1' url_get_source = BASE_URL + SOURCE_URI + '?name=source1' url_patch = BASE_URL + SOURCE_URI + '1/' cred_data = [{ 'id': 1, 'name': 'credential1', 'username': '******', 'password': '******' }] source_data = [{ 'id': 1, 'name': 'source1', 'hosts': ['1.2.3.4'], 'credentials': [{ 'id': 2, 'name': 'cred2' }] }] with requests_mock.Mocker() as mocker: mocker.get(url_get_source, status_code=200, json=source_data) mocker.get(url_get_cred, status_code=200, json=cred_data) mocker.patch(url_patch, status_code=200) aec = SourceEditCommand(SUBPARSER) args = Namespace(name='source1', hosts=['1.2.3.5'], cred=['credential1']) with redirect_stdout(source_out): aec.main(args) self.assertEqual(source_out.getvalue(), 'Source "source1" was updated\n')
def test_edit_source_exclude_host(self): """Testing edit network source command by adding an excluded host.""" source_out = StringIO() url_get_cred = get_server_location() + CREDENTIAL_URI + \ '?name=credential1' url_get_source = get_server_location() + SOURCE_URI + '?name=source1' url_patch = get_server_location() + SOURCE_URI + '1/' cred_results = [{'id': 1, 'name': 'credential1', 'username': '******', 'password': '******'}] cred_data = {'count': 1, 'results': cred_results} results = [{'id': 1, 'name': 'source1', 'hosts': ['1.2.3.4'], 'credentials':[{'id': 2, 'name': 'cred2'}]}] source_data = {'count': 1, 'results': results} with requests_mock.Mocker() as mocker: mocker.get(url_get_source, status_code=200, json=source_data) mocker.get(url_get_cred, status_code=200, json=cred_data) mocker.patch(url_patch, status_code=200) aec = SourceEditCommand(SUBPARSER) args = Namespace(name='source1', hosts=['1.2.3.[0:255]'], exclude_hosts=['1.2.3.4'], cred=['credential1'], port=22) with redirect_stdout(source_out): aec.main(args) self.assertEqual(source_out.getvalue(), messages.SOURCE_UPDATED % 'source1' + '\n')
def test_edit_source_ssl_err(self): """Testing the edit 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) aec = SourceEditCommand(SUBPARSER) args = Namespace(name='source1', hosts=['1.2.3.4'], cred=['credential1'], port=22) with self.assertRaises(SystemExit): with redirect_stdout(source_out): aec.main(args) self.assertEqual(source_out.getvalue(), SSL_ERROR_MSG)
def test_edit_source_no_val(self): """Testing the edit source command with a server error.""" source_out = StringIO() url_get_source = get_server_location() + SOURCE_URI + '?name=source1' with requests_mock.Mocker() as mocker: mocker.get(url_get_source, status_code=500, json=None) aec = SourceEditCommand(SUBPARSER) args = Namespace(name='source1', hosts=['1.2.3.4'], cred=['credential1'], port=22) with self.assertRaises(SystemExit): with redirect_stdout(source_out): aec.main(args) self.assertEqual(source_out.getvalue(), messages.SERVER_INTERNAL_ERROR)
def test_edit_source_none(self): """Testing the edit cred command for none existing cred.""" source_out = StringIO() url = get_server_location() + SOURCE_URI + '?name=source_none' with requests_mock.Mocker() as mocker: mocker.get(url, status_code=200, json={'count': 0}) aec = SourceEditCommand(SUBPARSER) args = Namespace(name='source_none', hosts=['1.2.3.4'], cred=['credential1'], port=22) with self.assertRaises(SystemExit): with redirect_stdout(source_out): aec.main(args) aec.main(args) self.assertTrue('Source "source_none" does not exist' in source_out.getvalue())
def test_edit_source_no_val(self): """Testing the edit source command with source doesn't exist.""" source_out = StringIO() url_get_source = BASE_URL + SOURCE_URI + '?name=source1' with requests_mock.Mocker() as mocker: mocker.get(url_get_source, status_code=500, json=[]) aec = SourceEditCommand(SUBPARSER) args = Namespace(name='source1', hosts=['1.2.3.4'], cred=['credential1'], port=22) with self.assertRaises(SystemExit): with redirect_stdout(source_out): aec.main(args) self.assertEqual(source_out.getvalue(), 'Source "source1" does not exist\n')
def test_edit_source_cred_nf(self): """Testing the edit source command where cred is not found.""" source_out = StringIO() url_get_cred = get_server_location() + CREDENTIAL_URI + \ '?name=credential1%2Ccred2' url_get_source = get_server_location() + SOURCE_URI + '?name=source1' cred_results = [{ 'id': 1, 'name': 'credential1', 'username': '******', 'password': '******' }] cred_data = {'count': 1, 'results': cred_results} results = [{ 'id': 1, 'name': 'source1', 'hosts': ['1.2.3.4'], 'credentials': [{ 'id': 2, 'name': 'cred2' }] }] source_data = {'count': 1, 'results': results} with requests_mock.Mocker() as mocker: mocker.get(url_get_source, status_code=200, json=source_data) mocker.get(url_get_cred, status_code=200, json=cred_data) aec = SourceEditCommand(SUBPARSER) args = Namespace(name='source1', hosts=['1.2.3.4'], cred=['credential1', 'cred2'], port=22) with self.assertRaises(SystemExit): with redirect_stdout(source_out): aec.main(args) self.assertTrue( 'An error occurred while processing ' 'the "--cred" input' in source_out.getvalue())