def test_auth_exception(self, mock_auth_password): mock_auth_password.side_effect = Exception device_handler = JunosDeviceHandler({'name': 'junos'}) obj = SSHSession(device_handler) obj._transport = paramiko.Transport(MagicMock()) self.assertRaises(AuthenticationError, obj._auth, 'user', 'password', [], False, True)
def test_auth_password(self, mock_auth_password): device_handler = JunosDeviceHandler({'name': 'junos'}) obj = SSHSession(device_handler) obj._transport = paramiko.Transport(None) obj._auth('user', 'password', [], False, True) self.assertEqual(mock_auth_password.call_args_list[0][0], ('user', 'password'))
def test_auth_keyfiles_exception(self, mock_get_key, mock_auth_public_key): key = paramiko.PKey() mock_get_key.side_effect = paramiko.ssh_exception.PasswordRequiredException device_handler = JunosDeviceHandler({'name': 'junos'}) obj = SSHSession(device_handler) obj._transport = paramiko.Transport(MagicMock()) self.assertRaises(AuthenticationError, obj._auth,'user', None, ["key_file_name"], False, True)
def test_auth_password(self, mock_auth_password): device_handler = JunosDeviceHandler({'name': 'junos'}) obj = SSHSession(device_handler) obj._transport = paramiko.Transport(MagicMock()) obj._auth('user', 'password', [], False, True) self.assertEqual( mock_auth_password.call_args_list[0][0], ('user', 'password'))
def test_auth_agent_exception(self, mock_get_key, mock_auth_public_key): key = paramiko.PKey() mock_get_key.return_value = [key] mock_auth_public_key.side_effect = paramiko.ssh_exception.AuthenticationException device_handler = JunosDeviceHandler({'name': 'junos'}) obj = SSHSession(device_handler) obj._transport = paramiko.Transport(MagicMock()) self.assertRaises(AuthenticationError, obj._auth,'user', None, [], True, False)
def test_close(self, mock_close): device_handler = JunosDeviceHandler({'name': 'junos'}) obj = SSHSession(device_handler) obj._transport = paramiko.Transport(MagicMock()) obj._transport.active = True obj._connected = True obj.close() mock_close.assert_called_once_with() self.assertFalse(obj._connected)
def test_auth_default_keyfiles_exception(self, mock_get_key, mock_auth_public_key, mock_is_file): key = paramiko.PKey() mock_is_file.return_value = True mock_get_key.side_effect = paramiko.ssh_exception.PasswordRequiredException device_handler = JunosDeviceHandler({'name': 'junos'}) obj = SSHSession(device_handler) obj._transport = paramiko.Transport(None) self.assertRaises(AuthenticationError, obj._auth,'user', None, [], False, True)
def test_auth_agent(self, mock_get_key, mock_auth_public_key): key = paramiko.PKey(msg="hello") mock_get_key.return_value = [key] device_handler = JunosDeviceHandler({'name': 'junos'}) obj = SSHSession(device_handler) obj._transport = paramiko.Transport(MagicMock()) obj._auth('user', 'password', [], True, True) self.assertEqual( (mock_auth_public_key.call_args_list[0][0][1]).__repr__(), key.__repr__())
def test_auth_keyfiles(self, mock_get_key, mock_auth_public_key): key = paramiko.PKey() mock_get_key.return_value = key device_handler = JunosDeviceHandler({'name': 'junos'}) obj = SSHSession(device_handler) obj._transport = paramiko.Transport(MagicMock()) obj._auth('user', 'password', ["key_file_name"], False, True) self.assertEqual( (mock_auth_public_key.call_args_list[0][0][1]).__repr__(), key.__repr__())
def test_auth_no_methods_exception(self): device_handler = JunosDeviceHandler({'name': 'junos'}) obj = SSHSession(device_handler) obj._transport = paramiko.Transport(MagicMock()) self.assertRaises(AuthenticationError, obj._auth,'user', None, [], False, False)