def test_list_cred_ssl_err(self): """Testing the list 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) alc = CredListCommand(SUBPARSER) args = Namespace() with self.assertRaises(SystemExit): with redirect_stdout(cred_out): alc.main(args) self.assertEqual(cred_out.getvalue(), SSL_ERROR_MSG)
def test_list_cred_internal_err(self): """Testing the list credential command with an internal error.""" cred_out = StringIO() url = get_server_location() + CREDENTIAL_URI with requests_mock.Mocker() as mocker: mocker.get(url, status_code=500, json={'error': ['Server Error']}) alc = CredListCommand(SUBPARSER) args = Namespace() with self.assertRaises(SystemExit): with redirect_stdout(cred_out): alc.main(args) self.assertEqual(cred_out.getvalue(), 'Server Error')
def test_list_cred_empty(self): """Testing the list credential command successfully with empty data.""" cred_out = StringIO() url = get_server_location() + CREDENTIAL_URI with requests_mock.Mocker() as mocker: mocker.get(url, status_code=200, json={'count': 0}) alc = CredListCommand(SUBPARSER) args = Namespace() with redirect_stdout(cred_out): alc.main(args) self.assertEqual(cred_out.getvalue(), 'No credentials exist yet.\n')
def test_list_cred_data(self): """Testing the list credential command with stubbed data.""" cred_out = StringIO() url = BASE_URL + CREDENTIAL_URI credential_entry = { 'id': 1, 'name': 'cred1', 'username': '******', 'password': '******' } data = [credential_entry] with requests_mock.Mocker() as mocker: mocker.get(url, status_code=200, json=data) alc = CredListCommand(SUBPARSER) args = Namespace() with redirect_stdout(cred_out): alc.main(args) expected = '[{"id":1,"name":"cred1","password":"******",' \ '"username":"******"}]' self.assertEqual( cred_out.getvalue().replace('\n', '').replace(' ', '').strip(), expected)
def test_list_filtered_cred_data(self): """Testing the list credential with filter by cred type.""" cred_out = StringIO() url = get_server_location() + CREDENTIAL_URI credential_entry = {'id': 1, 'name': 'cred1', 'cred_type': 'network', 'username': '******', 'password': '******'} results = [credential_entry] data = { 'count': 1, 'next': None, 'results': results } with requests_mock.Mocker() as mocker: mocker.get(url, status_code=200, json=data) alc = CredListCommand(SUBPARSER) args = Namespace(type='network') with redirect_stdout(cred_out): alc.main(args) expected = '[{"cred_type":"network","id":1,'\ '"name":"cred1","password":"******",' \ '"username":"******"}]' self.assertEqual(cred_out.getvalue().replace('\n', '') .replace(' ', '').strip(), expected)