mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Prefill the account in A Better Routeplanner reauthentication (#191818)
Co-authored-by: Martin Andersson <178440+mtandersson@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Martin Andersson
Claude Opus 5.5
parent
8228b18d24
commit
783ba1eebf
@@ -1,6 +1,7 @@
|
||||
"""Config flow for the A Better Routeplanner integration."""
|
||||
|
||||
from collections.abc import Mapping
|
||||
from contextlib import suppress
|
||||
import logging
|
||||
from typing import Any, override
|
||||
|
||||
@@ -37,7 +38,14 @@ class AbrpFlowHandler(
|
||||
@override
|
||||
def extra_authorize_data(self) -> dict[str, Any]:
|
||||
"""Extra data that needs to be appended to the authorize url."""
|
||||
return {"scope": " ".join(OAUTH2_SCOPES)}
|
||||
data = {"scope": " ".join(OAUTH2_SCOPES)}
|
||||
if self.source == SOURCE_REAUTH:
|
||||
id_token = self._get_reauth_entry().data["token"].get("id_token")
|
||||
with suppress(AbrpAuthError):
|
||||
# Only prefills the login page; the account is still checked on return.
|
||||
if email := parse_unverified_identity(id_token).email:
|
||||
data["login_hint"] = email
|
||||
return data
|
||||
|
||||
@override
|
||||
async def async_step_user(
|
||||
|
||||
@@ -121,15 +121,21 @@ def mock_expires_at() -> float:
|
||||
return time.time() + 86400
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def id_token() -> str:
|
||||
"""Fixture for the stored OIDC id_token."""
|
||||
return build_id_token()
|
||||
|
||||
|
||||
@pytest.fixture(name="token_entry")
|
||||
def mock_token_entry(expires_at: float) -> dict[str, Any]:
|
||||
def mock_token_entry(expires_at: float, id_token: str) -> dict[str, Any]:
|
||||
"""Fixture for OAuth ``token`` data for a ConfigEntry."""
|
||||
return {
|
||||
"access_token": "mock-access-token",
|
||||
"refresh_token": "mock-refresh-token",
|
||||
"token_type": "Bearer",
|
||||
"expires_at": expires_at,
|
||||
"id_token": build_id_token(),
|
||||
"id_token": id_token,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ from pytest_homeassistant.common import MockConfigEntry
|
||||
from pytest_homeassistant.test_util.aiohttp import AiohttpClientMocker
|
||||
from pytest_homeassistant.typing import ClientSessionGenerator
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
from yarl import URL
|
||||
|
||||
from homeassistant.components.abrp.const import DOMAIN, OAUTH2_AUTHORIZE, OAUTH2_TOKEN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
@@ -365,3 +366,30 @@ async def test_reauth(
|
||||
mock_abrp_client.assert_not_called()
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup_entry.mock_calls) == expected_setups
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("id_token", "expected_login_hint"),
|
||||
[
|
||||
pytest.param(
|
||||
build_id_token(email="sofie@example.com", name="Sofie"),
|
||||
"sofie@example.com",
|
||||
id="email",
|
||||
),
|
||||
pytest.param(build_id_token(name="Sofie"), None, id="no_email"),
|
||||
pytest.param("header.not-base64!.sig", None, id="unparsable_id_token"),
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("current_request_with_host", "use_cloud")
|
||||
async def test_reauth_login_hint(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
expected_login_hint: str | None,
|
||||
) -> None:
|
||||
"""Reauth prefills the stored account's email on the login page when known."""
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
result = await config_entry.start_reauth_flow(hass)
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
|
||||
assert URL(result["url"]).query.get("login_hint") == expected_login_hint
|
||||
|
||||
Reference in New Issue
Block a user