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)
def test_list_cred_data(self, b_input): """Testing the list credential command with stubbed data.""" cred_out = StringIO() url = get_server_location() + CREDENTIAL_URI credential_entry = { 'id': 1, 'name': 'cred1', 'username': '******', 'password': '******' } results = [credential_entry] next_link = 'http://127.0.0.1:8000/api/v1/credentials/?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) 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 + expected) b_input.assert_called_with(ANY)
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']}) clc = CredListCommand(SUBPARSER) args = Namespace() with self.assertRaises(SystemExit): with redirect_stdout(cred_out): clc.main(args)
def test_list_cred_conn_err(self): """Testing the list 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) clc = CredListCommand(SUBPARSER) args = Namespace() with self.assertRaises(SystemExit): with redirect_stdout(cred_out): clc.main(args)
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_conn_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.ConnectTimeout) alc = CredListCommand(SUBPARSER) args = Namespace() with self.assertRaises(SystemExit): with redirect_stdout(cred_out): alc.main(args) self.assertEqual(cred_out.getvalue(), CONNECTION_ERROR_MSG)
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)