|
13 | 13 | from homeassistant import config_entries |
14 | 14 | from homeassistant.components.volvo.const import CONF_VIN, DOMAIN |
15 | 15 | 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 |
17 | 17 | from homeassistant.core import HomeAssistant |
18 | 18 | from homeassistant.data_entry_flow import FlowResultType |
19 | 19 | from homeassistant.helpers import config_entry_oauth2_flow |
@@ -117,6 +117,53 @@ async def test_reauth_flow( |
117 | 117 | assert result["reason"] == "reauth_successful" |
118 | 118 |
|
119 | 119 |
|
| 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 | + |
120 | 167 | async def test_reconfigure_flow( |
121 | 168 | hass: HomeAssistant, |
122 | 169 | mock_config_entry: MockConfigEntry, |
|
0 commit comments