async def _patched_refresh_access_token( hass, new_token, new_token_expire_time, refresh_access_token_mock, should_refresh_mock, authenticate_mock, async_get_operable_locks_mock, ): authenticate_mock.side_effect = MagicMock( return_value=_mock_august_authentication( "original_token", 1234, AuthenticationState.AUTHENTICATED)) august_gateway = AugustGateway(hass) mocked_config = _mock_get_config() await august_gateway.async_setup(mocked_config[DOMAIN]) await august_gateway.async_authenticate() should_refresh_mock.return_value = False await august_gateway.async_refresh_access_token_if_needed() refresh_access_token_mock.assert_not_called() should_refresh_mock.return_value = True refresh_access_token_mock.return_value = _mock_august_authentication( new_token, new_token_expire_time, AuthenticationState.AUTHENTICATED) await august_gateway.async_refresh_access_token_if_needed() refresh_access_token_mock.assert_called() assert august_gateway.access_token == new_token assert august_gateway.authentication.access_token_expires == new_token_expire_time
async def test_requires_validation_state(opp): """Config entry state is SETUP_ERROR when august requires validation.""" config_entry = MockConfigEntry( domain=DOMAIN, data=_mock_get_config()[DOMAIN], title="August august", ) config_entry.add_to_opp(opp) assert opp.config_entries.flow.async_progress() == [] await setup.async_setup_component(opp, "persistent_notification", {}) with patch( "yalexs.authenticator_async.AuthenticatorAsync.async_authenticate", return_value=_mock_august_authentication( "original_token", 1234, AuthenticationState.REQUIRES_VALIDATION), ): await opp.config_entries.async_setup(config_entry.entry_id) await opp.async_block_till_done() assert config_entry.state is ConfigEntryState.SETUP_ERROR assert len(opp.config_entries.flow.async_progress()) == 1 assert opp.config_entries.flow.async_progress( )[0]["context"]["source"] == "reauth"
async def test_unknown_auth_http_401(opp): """Config entry state is SETUP_ERROR when august gets an http.""" config_entry = MockConfigEntry( domain=DOMAIN, data=_mock_get_config()[DOMAIN], title="August august", ) config_entry.add_to_opp(opp) assert opp.config_entries.flow.async_progress() == [] await setup.async_setup_component(opp, "persistent_notification", {}) with patch( "yalexs.authenticator_async.AuthenticatorAsync.async_authenticate", return_value=_mock_august_authentication("original_token", 1234, None), ): await opp.config_entries.async_setup(config_entry.entry_id) await opp.async_block_till_done() assert config_entry.state is ConfigEntryState.SETUP_ERROR flows = opp.config_entries.flow.async_progress() assert flows[0]["step_id"] == "reauth_validate"
async def test_unknown_auth_state(hass): """Config entry state is ENTRY_STATE_SETUP_ERROR when august is in an unknown auth state.""" config_entry = MockConfigEntry( domain=DOMAIN, data=_mock_get_config()[DOMAIN], title="August august", ) config_entry.add_to_hass(hass) assert hass.config_entries.flow.async_progress() == [] await setup.async_setup_component(hass, "persistent_notification", {}) with patch( "august.authenticator_async.AuthenticatorAsync.async_authenticate", return_value=_mock_august_authentication("original_token", 1234, None), ): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert config_entry.state == ENTRY_STATE_SETUP_ERROR flows = hass.config_entries.flow.async_progress() assert flows[0]["step_id"] == "user"
async def test_set_up_from_yaml(hass): """Test to make sure config is imported from yaml.""" await setup.async_setup_component(hass, "persistent_notification", {}) with patch( "homeassistant.components.august.async_setup_august", return_value=True, ) as mock_setup_august, patch( "homeassistant.components.august.config_flow.AugustGateway.async_authenticate", return_value=True, ): assert await async_setup_component(hass, DOMAIN, _mock_get_config()) await hass.async_block_till_done() assert len(mock_setup_august.mock_calls) == 1 call = mock_setup_august.call_args args, _ = call imported_config_entry = args[1] # The import must use DEFAULT_AUGUST_CONFIG_FILE so they # do not loose their token when config is migrated assert imported_config_entry.data == { CONF_ACCESS_TOKEN_CACHE_FILE: DEFAULT_AUGUST_CONFIG_FILE, CONF_INSTALL_ID: None, CONF_LOGIN_METHOD: "email", CONF_PASSWORD: "******", CONF_TIMEOUT: None, CONF_USERNAME: "******", }
async def test_bad_password(hass): """Config entry state is ENTRY_STATE_SETUP_ERROR when the password has been changed.""" config_entry = MockConfigEntry( domain=DOMAIN, data=_mock_get_config()[DOMAIN], title="August august", ) config_entry.add_to_hass(hass) assert hass.config_entries.flow.async_progress() == [] await setup.async_setup_component(hass, "persistent_notification", {}) with patch( "yalexs.authenticator_async.AuthenticatorAsync.async_authenticate", return_value=_mock_august_authentication( "original_token", 1234, AuthenticationState.BAD_PASSWORD), ): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert config_entry.state == ENTRY_STATE_SETUP_ERROR flows = hass.config_entries.flow.async_progress() assert flows[0]["step_id"] == "reauth_validate"
async def test_august_is_offline(hass): """Config entry state is ENTRY_STATE_SETUP_RETRY when august is offline.""" config_entry = MockConfigEntry( domain=DOMAIN, data=_mock_get_config()[DOMAIN], title="August august", ) config_entry.add_to_hass(hass) await setup.async_setup_component(hass, "persistent_notification", {}) with patch( "august.authenticator_async.AuthenticatorAsync.async_authenticate", side_effect=asyncio.TimeoutError, ): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert config_entry.state == ENTRY_STATE_SETUP_RETRY
async def test_august_is_offline(hass): """Config entry state is SETUP_RETRY when august is offline.""" config_entry = MockConfigEntry( domain=DOMAIN, data=_mock_get_config()[DOMAIN], title="August august", ) config_entry.add_to_hass(hass) with patch( "yalexs.authenticator_async.AuthenticatorAsync.async_authenticate", side_effect=asyncio.TimeoutError, ): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert config_entry.state is ConfigEntryState.SETUP_RETRY
async def test_august_api_is_failing(hass): """Config entry state is SETUP_RETRY when august api is failing.""" config_entry = MockConfigEntry( domain=DOMAIN, data=_mock_get_config()[DOMAIN], title="August august", ) config_entry.add_to_hass(hass) with patch( "yalexs.authenticator_async.AuthenticatorAsync.async_authenticate", side_effect=ClientResponseError(None, None, status=500), ): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert config_entry.state is ConfigEntryState.SETUP_RETRY
async def test_http_failure(opp): """Config entry state is SETUP_RETRY when august is offline.""" config_entry = MockConfigEntry( domain=DOMAIN, data=_mock_get_config()[DOMAIN], title="August august", ) config_entry.add_to_opp(opp) assert opp.config_entries.flow.async_progress() == [] await setup.async_setup_component(opp, "persistent_notification", {}) with patch( "yalexs.authenticator_async.AuthenticatorAsync.async_authenticate", side_effect=ClientResponseError(None, None, status=500), ): await opp.config_entries.async_setup(config_entry.entry_id) await opp.async_block_till_done() assert config_entry.state is ConfigEntryState.SETUP_RETRY assert opp.config_entries.flow.async_progress() == []
async def test_auth_fails(hass): """Config entry state is SETUP_ERROR when auth fails.""" config_entry = MockConfigEntry( domain=DOMAIN, data=_mock_get_config()[DOMAIN], title="August august", ) config_entry.add_to_hass(hass) assert hass.config_entries.flow.async_progress() == [] with patch( "yalexs.authenticator_async.AuthenticatorAsync.async_authenticate", side_effect=ClientResponseError(None, None, status=401), ): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert config_entry.state is ConfigEntryState.SETUP_ERROR flows = hass.config_entries.flow.async_progress() assert flows[0]["step_id"] == "reauth_validate"
async def test_unknown_auth_state(hass): """Config entry state is SETUP_ERROR when august is in an unknown auth state.""" config_entry = MockConfigEntry( domain=DOMAIN, data=_mock_get_config()[DOMAIN], title="August august", ) config_entry.add_to_hass(hass) assert hass.config_entries.flow.async_progress() == [] with patch( "yalexs.authenticator_async.AuthenticatorAsync.async_authenticate", return_value=_mock_august_authentication("original_token", 1234, None), ): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert config_entry.state is ConfigEntryState.SETUP_ERROR flows = hass.config_entries.flow.async_progress() assert flows[0]["step_id"] == "reauth_validate"
async def test_requires_validation_state(hass): """Config entry state is ENTRY_STATE_SETUP_ERROR when august requires validation.""" config_entry = MockConfigEntry( domain=DOMAIN, data=_mock_get_config()[DOMAIN], title="August august", ) config_entry.add_to_hass(hass) assert hass.config_entries.flow.async_progress() == [] await setup.async_setup_component(hass, "persistent_notification", {}) with patch( "august.authenticator_async.AuthenticatorAsync.async_authenticate", return_value=_mock_august_authentication( "original_token", 1234, AuthenticationState.REQUIRES_VALIDATION), ): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert config_entry.state == ENTRY_STATE_SETUP_ERROR assert hass.config_entries.flow.async_progress() == []
async def test_auth_fails(hass): """Config entry state is ENTRY_STATE_SETUP_ERROR when auth fails.""" config_entry = MockConfigEntry( domain=DOMAIN, data=_mock_get_config()[DOMAIN], title="August august", ) config_entry.add_to_hass(hass) assert hass.config_entries.flow.async_progress() == [] await setup.async_setup_component(hass, "persistent_notification", {}) with patch( "august.authenticator_async.AuthenticatorAsync.async_authenticate", side_effect=ClientResponseError(None, None, status=401), ): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert config_entry.state == ENTRY_STATE_SETUP_ERROR flows = hass.config_entries.flow.async_progress() assert flows[0]["step_id"] == "user"