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.BadRequestError( 'err_invalid_refresh_token') mock_create_session.side_effect = get_session config = { 'platform': 'automatic', 'client_id': 'client_id', 'secret': 'client_secret', 'devices': None, } hass.loop.run_until_complete( async_setup_scanner(hass, config, None)) assert mock_create_session.called assert len(mock_create_session.mock_calls) == 1 assert mock_create_session.mock_calls[0][1][0] == 'bad_token'
def test_setup(self): """ Test setup method. """ try: hass = get_test_home_assistant() mock_http_component(hass) self.assertTrue(logbook.setup(hass, {})) finally: hass.stop()
def setUp(self): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() mock_http_component(self.hass) self.hass.config.components += ['frontend', 'recorder', 'api'] with patch('homeassistant.components.logbook.' 'register_built_in_panel'): assert setup_component(self.hass, logbook.DOMAIN, self.EMPTY_CONFIG)
def test_setup_check_env_prevents_load(hass, loop): """Test it does not set up hassbian if environment var not present.""" mock_http_component(hass) with patch.dict(os.environ, clear=True), \ patch.object(config, 'SECTIONS', ['hassbian']): loop.run_until_complete(async_setup_component(hass, 'config', {})) assert 'config' in hass.config.components assert HassbianSuitesView.name not in hass.http.views assert HassbianSuiteInstallView.name not in hass.http.views
def setUp(self): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() init_recorder_component(self.hass) # Force an in memory DB mock_http_component(self.hass) self.hass.config.components |= set(['frontend', 'recorder', 'api']) assert setup_component(self.hass, logbook.DOMAIN, self.EMPTY_CONFIG) self.hass.start()
def test_setup_check_env_works(hass, loop): """Test it sets up hassbian if environment var present.""" mock_http_component(hass) with patch.dict(os.environ, {'FORCE_HASSBIAN': '1'}), \ patch.object(config, 'SECTIONS', ['hassbian']): loop.run_until_complete(async_setup_component(hass, 'config', {})) assert 'config' in hass.config.components assert HassbianSuitesView.name in hass.http.views assert HassbianSuiteInstallView.name in hass.http.views
def test_frontend_panel_register(hass, mock_openzwave): """Test network auto-heal disabled.""" mock_http_component(hass) hass.config.components |= set(['frontend']) with patch('homeassistant.components.zwave.' 'register_built_in_panel') as mock_register: assert (yield from async_setup_component(hass, 'zwave', { 'zwave': { 'autoheal': False, }})) assert mock_register.called assert len(mock_register.mock_calls) == 1
def test_setup(self): """Test setup method of history.""" mock_http_component(self.hass) config = history.CONFIG_SCHEMA({ ha.DOMAIN: {}, history.DOMAIN: {history.CONF_INCLUDE: { history.CONF_DOMAINS: ['media_player'], history.CONF_ENTITIES: ['thermostat.test']}, history.CONF_EXCLUDE: { history.CONF_DOMAINS: ['thermostat'], history.CONF_ENTITIES: ['media_player.test']}}}) self.assertTrue(setup_component(self.hass, history.DOMAIN, config))
def test_file_not_readable(hass): """Test local file will not setup when file is not readable.""" mock_http_component(hass) def run_test(): with mock.patch('os.path.isfile', mock.Mock(return_value=True)), \ mock.patch('os.access', return_value=False), \ assert_setup_component(0, 'camera'): assert setup_component(hass, 'camera', { 'camera': { 'name': 'config_test', 'platform': 'local_file', 'file_path': 'mock.file', }}) yield from hass.loop.run_in_executor(None, run_test)
def test_file_not_readable(hass, caplog): """Test a warning is shown setup when file is not readable.""" mock_http_component(hass) @mock.patch('os.path.isfile', mock.Mock(return_value=True)) @mock.patch('os.access', mock.Mock(return_value=False)) def run_test(): caplog.set_level( logging.WARNING, logger='requests.packages.urllib3.connectionpool') assert setup_component(hass, 'camera', { 'camera': { 'name': 'config_test', 'platform': 'local_file', 'file_path': 'mock.file', }}) assert 'Could not read' in caplog.text assert 'config_test' in caplog.text assert 'mock.file' in caplog.text yield from hass.loop.run_in_executor(None, run_test)
def setUp(self): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() mock_http_component(self.hass)
def setUp(self): # pylint: disable=invalid-name self.hass = ha.HomeAssistant() mock_http_component(self.hass)
def setUp(self): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() mock_http_component(self.hass) self.assertTrue(logbook.setup(self.hass, {}))
def stub_http(hass): """Stub the HTTP component.""" mock_http_component(hass)
def setUp(self): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() mock_http_component(self.hass) assert setup_component(self.hass, logbook.DOMAIN, self.EMPTY_CONFIG)
def setup_method(self, method): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() mock_http_component(self.hass, 'super_secret')
def setUp(self): """ Test setup method. """ self.hass = get_test_home_assistant() mock_http_component(self.hass) self.assertTrue(logbook.setup(self.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() mock_see = MagicMock() vehicle.id = 'mock_id' vehicle.display_name = 'mock_display_name' vehicle.fuel_level_percent = 45.6 vehicle.latest_location = None vehicle.updated_at = datetime(2017, 8, 13, 1, 2, 3) trip.end_location.lat = 45.567 trip.end_location.lon = 34.345 trip.end_location.accuracy_m = 5.6 trip.ended_at = datetime(2017, 8, 13, 1, 2, 4) @asyncio.coroutine def get_session(*args, **kwargs): """Return the test session.""" return session @asyncio.coroutine def get_vehicles(*args, **kwargs): """Return list of test vehicles.""" return [vehicle] @asyncio.coroutine def get_trips(*args, **kwargs): """Return list of test trips.""" return [trip] mock_create_session.side_effect = get_session session.ws_connect = MagicMock() session.get_vehicles.side_effect = get_vehicles session.get_trips.side_effect = get_trips session.refresh_token = 'mock_refresh_token' @asyncio.coroutine def ws_connect(): return asyncio.Future(loop=hass.loop) mock_ws_connect.side_effect = ws_connect config = { 'platform': 'automatic', 'username': '******', 'password': '******', 'client_id': 'client_id', 'secret': 'client_secret', 'devices': None, } result = hass.loop.run_until_complete( async_setup_scanner(hass, config, mock_see)) 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' assert mock_see.mock_calls[0][2]['mac'] == 'mock_id' assert mock_see.mock_calls[0][2]['host_name'] == 'mock_display_name' assert mock_see.mock_calls[0][2]['attributes'] == {'fuel_level': 45.6} assert mock_see.mock_calls[0][2]['gps'] == (45.567, 34.345) assert mock_see.mock_calls[0][2]['gps_accuracy'] == 5.6 assert mock_json_dump.called assert len(mock_json_dump.mock_calls) == 1 assert mock_json_dump.mock_calls[0][1][0] == { 'refresh_token': 'mock_refresh_token' }
def setUp(self): # pylint: disable=invalid-name self.hass = get_test_home_assistant() mock_http_component(self.hass)
def test_setup(self): """ Test setup method of history. """ mock_http_component(self.hass) self.assertTrue(history.setup(self.hass, {}))
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() mock_http_component(self.hass)