From aa3d0e10472e0d8c9dfcf6fb7f807afd62091007 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 22 Jun 2016 20:01:39 -0400 Subject: [PATCH] Fix incorrect check on presence of password and pub_key (#2355) This commit fixes an issue with the use of None in default values for the config get() calls in __init__() of AsusWrtDeviceScanner. These values are cast as strings and when a NoneType is cast it returns the string "None" this broke the check for the existence of these fields. This commit fixes the issue by changing the default value to be an empty string '' which will conform with the behavior expected by the ssh login code. Closes #2343 --- .../components/device_tracker/asuswrt.py | 4 +- .../components/device_tracker/test_asuswrt.py | 65 +++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/device_tracker/asuswrt.py b/homeassistant/components/device_tracker/asuswrt.py index 282ae46ba85..725a49308be 100644 --- a/homeassistant/components/device_tracker/asuswrt.py +++ b/homeassistant/components/device_tracker/asuswrt.py @@ -83,8 +83,8 @@ class AsusWrtDeviceScanner(object): """Initialize the scanner.""" self.host = config[CONF_HOST] self.username = str(config[CONF_USERNAME]) - self.password = str(config.get(CONF_PASSWORD)) - self.pub_key = str(config.get('pub_key')) + self.password = str(config.get(CONF_PASSWORD, "")) + self.pub_key = str(config.get('pub_key', "")) self.protocol = config.get('protocol') self.mode = config.get('mode') diff --git a/tests/components/device_tracker/test_asuswrt.py b/tests/components/device_tracker/test_asuswrt.py index 210ce2c58fa..241e4a65a0f 100644 --- a/tests/components/device_tracker/test_asuswrt.py +++ b/tests/components/device_tracker/test_asuswrt.py @@ -67,3 +67,68 @@ class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase): self.assertIsNotNone(device_tracker.asuswrt.get_scanner( self.hass, conf_dict)) asuswrt_mock.assert_called_once_with(conf_dict[device_tracker.DOMAIN]) + + def test_ssh_login_with_pub_key(self): + """Test that login is done with pub_key when configured to.""" + ssh = mock.MagicMock() + ssh_mock = mock.patch('pexpect.pxssh.pxssh', return_value=ssh) + ssh_mock.start() + self.addCleanup(ssh_mock.stop) + conf_dict = { + CONF_PLATFORM: 'asuswrt', + CONF_HOST: 'fake_host', + CONF_USERNAME: 'fake_user', + 'pub_key': '/fake_path' + } + update_mock = mock.patch( + 'homeassistant.components.device_tracker.asuswrt.' + 'AsusWrtDeviceScanner.get_asuswrt_data') + update_mock.start() + self.addCleanup(update_mock.stop) + asuswrt = device_tracker.asuswrt.AsusWrtDeviceScanner(conf_dict) + asuswrt.ssh_connection() + ssh.login.assert_called_once_with('fake_host', 'fake_user', + ssh_key='/fake_path') + + def test_ssh_login_with_password(self): + """Test that login is done with password when configured to.""" + ssh = mock.MagicMock() + ssh_mock = mock.patch('pexpect.pxssh.pxssh', return_value=ssh) + ssh_mock.start() + self.addCleanup(ssh_mock.stop) + conf_dict = { + CONF_PLATFORM: 'asuswrt', + CONF_HOST: 'fake_host', + CONF_USERNAME: 'fake_user', + CONF_PASSWORD: 'fake_pass' + } + update_mock = mock.patch( + 'homeassistant.components.device_tracker.asuswrt.' + 'AsusWrtDeviceScanner.get_asuswrt_data') + update_mock.start() + self.addCleanup(update_mock.stop) + asuswrt = device_tracker.asuswrt.AsusWrtDeviceScanner(conf_dict) + asuswrt.ssh_connection() + ssh.login.assert_called_once_with('fake_host', 'fake_user', + 'fake_pass') + + def test_ssh_login_without_password_or_pubkey(self): + """Test that login is not called without password or pub_key.""" + ssh = mock.MagicMock() + ssh_mock = mock.patch('pexpect.pxssh.pxssh', return_value=ssh) + ssh_mock.start() + self.addCleanup(ssh_mock.stop) + conf_dict = { + CONF_PLATFORM: 'asuswrt', + CONF_HOST: 'fake_host', + CONF_USERNAME: 'fake_user', + } + update_mock = mock.patch( + 'homeassistant.components.device_tracker.asuswrt.' + 'AsusWrtDeviceScanner.get_asuswrt_data') + update_mock.start() + self.addCleanup(update_mock.stop) + asuswrt = device_tracker.asuswrt.AsusWrtDeviceScanner(conf_dict) + result = asuswrt.ssh_connection() + ssh.login.assert_not_called() + self.assertIsNone(result)