Fix re-auth flow for Volvo integration (#150478)

This commit is contained in:
Thomas D
2025-08-15 13:58:03 +02:00
committed by Franck Nijhof
parent 3d4d57fa32
commit b0ab3cddb8
2 changed files with 51 additions and 4 deletions

View File

@@ -69,7 +69,7 @@ class VolvoOAuth2FlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
async def async_oauth_create_entry(self, data: dict) -> ConfigFlowResult:
"""Create an entry for the flow."""
self._config_data |= data
self._config_data |= (self.init_data or {}) | data
return await self.async_step_api_key()
async def async_step_reauth(self, _: Mapping[str, Any]) -> ConfigFlowResult:
@@ -77,7 +77,7 @@ class VolvoOAuth2FlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
return await self.async_step_reauth_confirm()
async def async_step_reconfigure(
self, _: dict[str, Any] | None = None
self, data: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Reconfigure the entry."""
return await self.async_step_api_key()
@@ -121,7 +121,7 @@ class VolvoOAuth2FlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
if user_input is None:
if self.source == SOURCE_REAUTH:
user_input = self._config_data = dict(self._get_reauth_entry().data)
user_input = self._config_data
api = _create_volvo_cars_api(
self.hass,
self._config_data[CONF_TOKEN][CONF_ACCESS_TOKEN],

View File

@@ -13,7 +13,7 @@ from yarl import URL
from homeassistant import config_entries
from homeassistant.components.volvo.const import CONF_VIN, DOMAIN
from homeassistant.config_entries import ConfigFlowResult
from homeassistant.const import CONF_API_KEY
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_API_KEY, CONF_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import config_entry_oauth2_flow
@@ -117,6 +117,53 @@ async def test_reauth_flow(
assert result["reason"] == "reauth_successful"
@pytest.mark.usefixtures("current_request_with_host")
async def test_reauth_no_stale_data(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
hass_client_no_auth: ClientSessionGenerator,
mock_config_flow_api: VolvoCarsApi,
) -> None:
"""Test if reauthentication flow does not use stale data."""
old_access_token = mock_config_entry.data[CONF_TOKEN][CONF_ACCESS_TOKEN]
with patch(
"homeassistant.components.volvo.config_flow._create_volvo_cars_api",
return_value=mock_config_flow_api,
) as mock_create_volvo_cars_api:
result = await mock_config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
state = config_entry_oauth2_flow._encode_jwt(
hass,
{
"flow_id": result["flow_id"],
"redirect_uri": REDIRECT_URI,
},
)
client = await hass_client_no_auth()
resp = await client.get(f"/auth/external/callback?code=abcd&state={state}")
assert resp.status == 200
assert resp.headers["content-type"] == "text/html; charset=utf-8"
result = await _async_run_flow_to_completion(
hass,
result,
mock_config_flow_api,
has_vin_step=False,
is_reauth=True,
)
assert mock_create_volvo_cars_api.called
call = mock_create_volvo_cars_api.call_args_list[0]
access_token_arg = call.args[1]
assert old_access_token != access_token_arg
async def test_reconfigure_flow(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,