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_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_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_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__refresh_access_token(hass): """Set up things to be run when tests are started.""" authentication = _mock_august_authentication("original_token", 1234) authenticator = _mock_august_authenticator() token_refresh_lock = asyncio.Lock() data = august.AugustData(hass, MagicMock(name="api"), authentication, authenticator, token_refresh_lock) await data._async_refresh_access_token_if_needed() authenticator.refresh_access_token.assert_not_called() authenticator.should_refresh.return_value = 1 authenticator.refresh_access_token.return_value = _mock_august_authentication( "new_token", 5678) await data._async_refresh_access_token_if_needed() authenticator.refresh_access_token.assert_called() assert data._access_token == "new_token" assert data._access_token_expires == 5678
def _create_august_data_with_lock_details(lock_details): locks = [] for lock in lock_details: if isinstance(lock, LockDetail): locks.append(_mock_august_lock(lock.device_id)) authentication = _mock_august_authentication("original_token", 1234) authenticator = _mock_august_authenticator() token_refresh_lock = MagicMock() api = MagicMock() api.get_lock_status = MagicMock(return_value=(MagicMock(), MagicMock())) api.get_lock_detail = MagicMock(side_effect=lock_details) api.get_operable_locks = MagicMock(return_value=locks) api.get_doorbells = MagicMock(return_value=[]) return august.AugustData( MagicMock(), api, authentication, authenticator, token_refresh_lock )
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() == []