From 95c57412ff3ce6245942ab733d741fcf81a19754 Mon Sep 17 00:00:00 2001 From: Adam Mills Date: Tue, 15 Aug 2017 21:04:44 -0400 Subject: [PATCH] Automatic device tracker remove password (#9002) * Remove now disabled password auth from automatic * Fallback to configurator more permissively * Fix test for changes * Bump lib --- .../components/device_tracker/automatic.py | 26 +----------- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- .../device_tracker/test_automatic.py | 40 ++++++++++++++----- 4 files changed, 34 insertions(+), 36 deletions(-) diff --git a/homeassistant/components/device_tracker/automatic.py b/homeassistant/components/device_tracker/automatic.py index 1cd0e84cd8f..071edf42642 100644 --- a/homeassistant/components/device_tracker/automatic.py +++ b/homeassistant/components/device_tracker/automatic.py @@ -17,14 +17,13 @@ from homeassistant.components.device_tracker import ( PLATFORM_SCHEMA, ATTR_ATTRIBUTES, ATTR_DEV_ID, ATTR_HOST_NAME, ATTR_MAC, ATTR_GPS, ATTR_GPS_ACCURACY) from homeassistant.components.http import HomeAssistantView -from homeassistant.const import ( - CONF_USERNAME, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP) +from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.core import callback from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv from homeassistant.helpers.event import async_track_time_interval -REQUIREMENTS = ['aioautomatic==0.5.0'] +REQUIREMENTS = ['aioautomatic==0.6.0'] DEPENDENCIES = ['http'] _LOGGER = logging.getLogger(__name__) @@ -51,8 +50,6 @@ DATA_REFRESH_TOKEN = 'refresh_token' PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_CLIENT_ID): cv.string, vol.Required(CONF_SECRET): cv.string, - vol.Inclusive(CONF_USERNAME, 'auth'): cv.string, - vol.Inclusive(CONF_PASSWORD, 'auth'): cv.string, vol.Optional(CONF_CURRENT_LOCATION, default=False): cv.boolean, vol.Optional(CONF_DEVICES, default=None): vol.All( cv.ensure_list, [cv.string]) @@ -131,27 +128,8 @@ def async_setup_scanner(hass, config, async_see, discovery_info=None): refresh_token) yield from initialize_data(session) return True - except aioautomatic.exceptions.BadRequestError as err: - if str(err) == 'err_invalid_refresh_token': - _LOGGER.error("Stored refresh token was invalid.") - yield from hass.async_add_job( - _write_refresh_token_to_file, hass, filename, None) - else: - _LOGGER.error(str(err)) - return False except aioautomatic.exceptions.AutomaticError as err: _LOGGER.error(str(err)) - return False - - if CONF_USERNAME in config: - try: - session = yield from client.create_session_from_password( - scope, config[CONF_USERNAME], config[CONF_PASSWORD]) - yield from initialize_data(session) - return True - except aioautomatic.exceptions.AutomaticError as err: - _LOGGER.error(str(err)) - return False configurator = hass.components.configurator request_id = configurator.async_request_config( diff --git a/requirements_all.txt b/requirements_all.txt index c35d8f3232e..0d8d5f55843 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -39,7 +39,7 @@ SoCo==0.12 TwitterAPI==2.4.6 # homeassistant.components.device_tracker.automatic -aioautomatic==0.5.0 +aioautomatic==0.6.0 # homeassistant.components.sensor.dnsip aiodns==1.1.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 7127ef9bb5f..68ce17c04f2 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -27,7 +27,7 @@ PyJWT==1.5.2 SoCo==0.12 # homeassistant.components.device_tracker.automatic -aioautomatic==0.5.0 +aioautomatic==0.6.0 # homeassistant.components.emulated_hue # homeassistant.components.http diff --git a/tests/components/device_tracker/test_automatic.py b/tests/components/device_tracker/test_automatic.py index 3355f0d1f1b..d572791168c 100644 --- a/tests/components/device_tracker/test_automatic.py +++ b/tests/components/device_tracker/test_automatic.py @@ -12,41 +12,56 @@ from tests.common import mock_http_component _LOGGER = logging.getLogger(__name__) -@patch('aioautomatic.Client.create_session_from_password') -def test_invalid_credentials(mock_create_session, hass): +@patch('aioautomatic.Client.create_session_from_refresh_token') +@patch('json.load') +@patch('json.dump') +@patch('os.makedirs') +@patch('os.path.isfile', return_value=True) +@patch('homeassistant.components.device_tracker.automatic.open', create=True) +def test_invalid_credentials( + mock_open, mock_isfile, mock_makedirs, mock_json_dump, mock_json_load, + mock_create_session, hass): """Test with invalid credentials.""" mock_http_component(hass) + mock_json_load.return_value = {'refresh_token': 'bad_token'} + @asyncio.coroutine def get_session(*args, **kwargs): """Return the test session.""" - raise aioautomatic.exceptions.ForbiddenError() + raise aioautomatic.exceptions.BadRequestError( + 'err_invalid_refresh_token') mock_create_session.side_effect = get_session config = { 'platform': 'automatic', - 'username': 'bad_username', - 'password': 'bad_password', 'client_id': 'client_id', 'secret': 'client_secret', 'devices': None, } - result = hass.loop.run_until_complete( + hass.loop.run_until_complete( async_setup_scanner(hass, config, None)) - assert not result + assert mock_create_session.called + assert len(mock_create_session.mock_calls) == 1 + assert mock_create_session.mock_calls[0][1][0] == 'bad_token' -@patch('aioautomatic.Client.create_session_from_password') +@patch('aioautomatic.Client.create_session_from_refresh_token') @patch('aioautomatic.Client.ws_connect') +@patch('json.load') @patch('json.dump') @patch('os.makedirs') +@patch('os.path.isfile', return_value=True) @patch('homeassistant.components.device_tracker.automatic.open', create=True) -def test_valid_credentials(mock_open, mock_os_makedirs, mock_json_dump, - mock_ws_connect, mock_create_session, hass): +def test_valid_credentials( + mock_open, mock_isfile, mock_makedirs, mock_json_dump, mock_json_load, + mock_ws_connect, mock_create_session, hass): """Test with valid credentials.""" mock_http_component(hass) + mock_json_load.return_value = {'refresh_token': 'good_token'} + session = MagicMock() vehicle = MagicMock() trip = MagicMock() @@ -102,6 +117,11 @@ def test_valid_credentials(mock_open, mock_os_makedirs, mock_json_dump, hass.async_block_till_done() assert result + + assert mock_create_session.called + assert len(mock_create_session.mock_calls) == 1 + assert mock_create_session.mock_calls[0][1][0] == 'good_token' + assert mock_see.called assert len(mock_see.mock_calls) == 2 assert mock_see.mock_calls[0][2]['dev_id'] == 'mock_id'