def test_show_source_data(self): """Testing the show source command successfully with stubbed data.""" source_out = StringIO() url = BASE_URL + SOURCE_URI + '?name=source1' source_entry = { 'id': 1, 'name': 'source1', 'hosts': ['1.2.3.4'], 'credentials': [{ 'id': 1, 'name': 'cred1' }] } results = [source_entry] data = {'count': 1, 'results': results} with requests_mock.Mocker() as mocker: mocker.get(url, status_code=200, json=data) nsc = SourceShowCommand(SUBPARSER) args = Namespace(name='source1') with redirect_stdout(source_out): nsc.main(args) expected = '{"credentials":[{"id":1,"name":"cred1"}],' \ '"hosts":["1.2.3.4"],"id":1,"name":"source1"}' self.assertEqual( source_out.getvalue().replace('\n', '').replace(' ', '').strip(), expected)
def test_show_source_internal_err(self): """Testing the show source command with an internal error.""" source_out = StringIO() url = BASE_URL + SOURCE_URI + '?name=source1' with requests_mock.Mocker() as mocker: mocker.get(url, status_code=500, json={'error': ['Server Error']}) nsc = SourceShowCommand(SUBPARSER) args = Namespace(name='source1') with self.assertRaises(SystemExit): with redirect_stdout(source_out): nsc.main(args) self.assertEqual(source_out.getvalue(), 'Server Error')
def test_show_source_ssl_err(self): """Testing the show source command with a connection error.""" source_out = StringIO() url = BASE_URL + SOURCE_URI + '?name=source1' with requests_mock.Mocker() as mocker: mocker.get(url, exc=requests.exceptions.SSLError) nsc = SourceShowCommand(SUBPARSER) args = Namespace(name='source1') with self.assertRaises(SystemExit): with redirect_stdout(source_out): nsc.main(args) self.assertEqual(source_out.getvalue(), SSL_ERROR_MSG)
def test_show_source_empty(self): """Testing the show source command successfully with empty data.""" source_out = StringIO() url = BASE_URL + SOURCE_URI + '?name=source1' with requests_mock.Mocker() as mocker: mocker.get(url, status_code=200, json={'count': 0}) nsc = SourceShowCommand(SUBPARSER) args = Namespace(name='source1') with self.assertRaises(SystemExit): with redirect_stdout(source_out): nsc.main(args) self.assertEqual(source_out.getvalue(), 'Source "source1" does not exist\n')