Skip to content

Commit b0ab3cd

Browse files
thomasddnfrenck
authored andcommitted
Fix re-auth flow for Volvo integration (home-assistant#150478)
1 parent 3d4d57f commit b0ab3cd

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

homeassistant/components/volvo/config_flow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ def logger(self) -> logging.Logger:
6969

7070
async def async_oauth_create_entry(self, data: dict) -> ConfigFlowResult:
7171
"""Create an entry for the flow."""
72-
self._config_data |= data
72+
self._config_data |= (self.init_data or {}) | data
7373
return await self.async_step_api_key()
7474

7575
async def async_step_reauth(self, _: Mapping[str, Any]) -> ConfigFlowResult:
7676
"""Perform reauth upon an API authentication error."""
7777
return await self.async_step_reauth_confirm()
7878

7979
async def async_step_reconfigure(
80-
self, _: dict[str, Any] | None = None
80+
self, data: dict[str, Any] | None = None
8181
) -> ConfigFlowResult:
8282
"""Reconfigure the entry."""
8383
return await self.async_step_api_key()
@@ -121,7 +121,7 @@ async def async_step_api_key(
121121

122122
if user_input is None:
123123
if self.source == SOURCE_REAUTH:
124-
user_input = self._config_data = dict(self._get_reauth_entry().data)
124+
user_input = self._config_data
125125
api = _create_volvo_cars_api(
126126
self.hass,
127127
self._config_data[CONF_TOKEN][CONF_ACCESS_TOKEN],

tests/components/volvo/test_config_flow.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from homeassistant import config_entries
1414
from homeassistant.components.volvo.const import CONF_VIN, DOMAIN
1515
from homeassistant.config_entries import ConfigFlowResult
16-
from homeassistant.const import CONF_API_KEY
16+
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_API_KEY, CONF_TOKEN
1717
from homeassistant.core import HomeAssistant
1818
from homeassistant.data_entry_flow import FlowResultType
1919
from homeassistant.helpers import config_entry_oauth2_flow
@@ -117,6 +117,53 @@ async def test_reauth_flow(
117117
assert result["reason"] == "reauth_successful"
118118

119119

120+
@pytest.mark.usefixtures("current_request_with_host")
121+
async def test_reauth_no_stale_data(
122+
hass: HomeAssistant,
123+
mock_config_entry: MockConfigEntry,
124+
hass_client_no_auth: ClientSessionGenerator,
125+
mock_config_flow_api: VolvoCarsApi,
126+
) -> None:
127+
"""Test if reauthentication flow does not use stale data."""
128+
old_access_token = mock_config_entry.data[CONF_TOKEN][CONF_ACCESS_TOKEN]
129+
130+
with patch(
131+
"homeassistant.components.volvo.config_flow._create_volvo_cars_api",
132+
return_value=mock_config_flow_api,
133+
) as mock_create_volvo_cars_api:
134+
result = await mock_config_entry.start_reauth_flow(hass)
135+
assert result["type"] is FlowResultType.FORM
136+
assert result["step_id"] == "reauth_confirm"
137+
138+
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
139+
140+
state = config_entry_oauth2_flow._encode_jwt(
141+
hass,
142+
{
143+
"flow_id": result["flow_id"],
144+
"redirect_uri": REDIRECT_URI,
145+
},
146+
)
147+
148+
client = await hass_client_no_auth()
149+
resp = await client.get(f"/auth/external/callback?code=abcd&state={state}")
150+
assert resp.status == 200
151+
assert resp.headers["content-type"] == "text/html; charset=utf-8"
152+
153+
result = await _async_run_flow_to_completion(
154+
hass,
155+
result,
156+
mock_config_flow_api,
157+
has_vin_step=False,
158+
is_reauth=True,
159+
)
160+
161+
assert mock_create_volvo_cars_api.called
162+
call = mock_create_volvo_cars_api.call_args_list[0]
163+
access_token_arg = call.args[1]
164+
assert old_access_token != access_token_arg
165+
166+
120167
async def test_reconfigure_flow(
121168
hass: HomeAssistant,
122169
mock_config_entry: MockConfigEntry,

0 commit comments

Comments
 (0)