def test_edit_host_cred(self): """Testing the edit credential command successfully.""" cred_out = StringIO() url_get = BASE_URL + CREDENTIAL_URI url_patch = BASE_URL + CREDENTIAL_URI + '1/' data = [{ 'id': 1, 'name': 'cred1', 'cred_type': NETWORK_CRED_TYPE, 'username': '******', 'password': '******' }] with requests_mock.Mocker() as mocker: mocker.get(url_get, status_code=200, json=data) mocker.patch(url_patch, status_code=200) aec = CredEditCommand(SUBPARSER) args = Namespace(name='cred1', username='******', filename=TMP_KEY, password=None, sudo_password=None, ssh_passphrase=None) with redirect_stdout(cred_out): aec.main(args) self.assertEqual(cred_out.getvalue(), 'Credential "cred1" was updated\n')
def test_partial_edit_host_cred(self): """Testing the edit credential command successfully.""" cred_out = StringIO() url_get = get_server_location() + CREDENTIAL_URI url_patch = get_server_location() + CREDENTIAL_URI + '1/' results = [{ 'id': 1, 'name': 'cred1', 'cred_type': NETWORK_CRED_TYPE, 'username': '******' }] data = {'count': 1, 'results': results} with requests_mock.Mocker() as mocker: mocker.get(url_get, status_code=200, json=data) mocker.patch(url_patch, status_code=200) cec = CredEditCommand(SUBPARSER) args = Namespace(name='cred1', username='******', filename=TMP_KEY, password=None, become_password=None, ssh_passphrase=None) with redirect_stdout(cred_out): cec.main(args) self.assertEqual(cred_out.getvalue(), messages.CRED_UPDATED % 'cred1' + '\n')
def test_edit_cred_conn_err(self): """Testing the edit credential command with a connection error.""" cred_out = StringIO() url = get_server_location() + CREDENTIAL_URI with requests_mock.Mocker() as mocker: mocker.get(url, exc=requests.exceptions.ConnectTimeout) aec = CredEditCommand(SUBPARSER) args = Namespace(name='credential1', username='******', filename=TMP_KEY, password=None, become_password=None) with self.assertRaises(SystemExit): with redirect_stdout(cred_out): aec.main(args) self.assertEqual(cred_out.getvalue(), CONNECTION_ERROR_MSG)
def test_edit_cred_get_error(self): """Testing the edit credential command server error occurs.""" cred_out = StringIO() url_get = get_server_location() + CREDENTIAL_URI with requests_mock.Mocker() as mocker: mocker.get(url_get, status_code=500, json=None) aec = CredEditCommand(SUBPARSER) args = Namespace(name='cred1', username='******', filename=TMP_KEY, password=None, become_password=None) with self.assertRaises(SystemExit): with redirect_stdout(cred_out): aec.main(args) self.assertEqual(cred_out.getvalue(), messages.SERVER_INTERNAL_ERROR)
def test_edit_cred_none(self): """Testing the edit credential command for non-existing credential.""" cred_out = StringIO() url = get_server_location() + CREDENTIAL_URI + '?name=cred_none' with requests_mock.Mocker() as mocker: mocker.get(url, status_code=200, json={'count': 0}) cec = CredEditCommand(SUBPARSER) args = Namespace(name='cred_none', username='******', filename=TMP_KEY, password=None, become_password=None) with self.assertRaises(SystemExit): with redirect_stdout(cred_out): cec.main(args)
def test_edit_cred_ssl_err(self): """Testing the edit credential command with a connection error.""" cred_out = StringIO() url = BASE_URL + CREDENTIAL_URI with requests_mock.Mocker() as mocker: mocker.get(url, exc=requests.exceptions.SSLError) aec = CredEditCommand(SUBPARSER) args = Namespace(name='credential1', username='******', filename=TMP_KEY, password=None, sudo_password=None) with self.assertRaises(SystemExit): with redirect_stdout(cred_out): aec.main(args) self.assertEqual(cred_out.getvalue(), SSL_ERROR_MSG)
def test_edit_cred_get_error(self): """Testing the edit credential command server error occurs.""" cred_out = StringIO() url_get = get_server_location() + CREDENTIAL_URI results = [{'id': 1, 'name': 'cred1', 'username': '******', 'password': '******'}] data = {'count': 1, 'results': results} with requests_mock.Mocker() as mocker: mocker.get(url_get, status_code=500, json=data) aec = CredEditCommand(SUBPARSER) args = Namespace(name='cred1', username='******', filename=TMP_KEY, password=None, become_password=None) with self.assertRaises(SystemExit): with redirect_stdout(cred_out): aec.main(args) self.assertEqual(cred_out.getvalue(), 'Credential "cred1" does not exist\n')
def test_edit_cred_none(self): """Testing the edit credential command for none existing credential.""" cred_out = StringIO() url = BASE_URL + CREDENTIAL_URI + '?name=cred_none' with requests_mock.Mocker() as mocker: mocker.get(url, status_code=200, json=[]) aec = CredEditCommand(SUBPARSER) args = Namespace(name='cred_none', username='******', filename=TMP_KEY, password=None, sudo_password=None) with self.assertRaises(SystemExit): with redirect_stdout(cred_out): aec.main(args) aec.main(args) self.assertTrue('credential "cred_none" does not exist' in cred_out.getvalue())