Use unique_id_mismatch in aseko_pool_live reauth (#128339)

pull/127646/head
epenet 2024-10-16 13:47:33 +02:00 committed by GitHub
parent c5046f7809
commit 9f2bdca9ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 6 deletions

View File

@ -29,7 +29,7 @@ class AsekoConfigFlow(ConfigFlow, domain=DOMAIN):
}
)
async def get_account_info(self, email: str, password: str) -> dict:
async def get_account_info(self, email: str, password: str) -> dict[str, Any]:
"""Get account info from the mobile API and the web API."""
aseko = Aseko(email, password)
user = await aseko.login()
@ -70,7 +70,9 @@ class AsekoConfigFlow(ConfigFlow, domain=DOMAIN):
async def async_store_credentials(self, info: dict[str, Any]) -> ConfigFlowResult:
"""Store validated credentials."""
await self.async_set_unique_id(info[CONF_UNIQUE_ID])
if self.source == SOURCE_REAUTH:
self._abort_if_unique_id_mismatch()
return self.async_update_reload_and_abort(
self._get_reauth_entry(),
title=info[CONF_EMAIL],
@ -80,7 +82,6 @@ class AsekoConfigFlow(ConfigFlow, domain=DOMAIN):
},
)
await self.async_set_unique_id(info[CONF_UNIQUE_ID])
self._abort_if_unique_id_configured()
return self.async_create_entry(

View File

@ -21,7 +21,8 @@
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"unique_id_mismatch": "The user identifier does not match the previous identifier"
}
},
"entity": {

View File

@ -128,8 +128,9 @@ async def test_async_step_reauth_success(hass: HomeAssistant, user: User) -> Non
mock_entry = MockConfigEntry(
domain=DOMAIN,
unique_id="UID",
data={CONF_EMAIL: "aseko@example.com"},
unique_id="a_user_id",
data={CONF_EMAIL: "aseko@example.com", CONF_PASSWORD: "passw0rd"},
version=2,
)
mock_entry.add_to_hass(hass)
@ -151,13 +152,61 @@ async def test_async_step_reauth_success(hass: HomeAssistant, user: User) -> Non
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_EMAIL: "aseko@example.com", CONF_PASSWORD: "passw0rd"},
{CONF_EMAIL: "aseko@example.com", CONF_PASSWORD: "new_password"},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
assert len(mock_setup_entry.mock_calls) == 1
assert mock_entry.unique_id == "a_user_id"
assert dict(mock_entry.data) == {
CONF_EMAIL: "aseko@example.com",
CONF_PASSWORD: "new_password",
}
async def test_async_step_reauth_mismatch(hass: HomeAssistant, user: User) -> None:
"""Test mismatch reauthentication."""
mock_entry = MockConfigEntry(
domain=DOMAIN,
unique_id="UID",
data={CONF_EMAIL: "aseko@example.com", CONF_PASSWORD: "passw0rd"},
version=2,
)
mock_entry.add_to_hass(hass)
result = await mock_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {}
with (
patch(
"homeassistant.components.aseko_pool_live.config_flow.Aseko.login",
return_value=user,
),
patch(
"homeassistant.components.aseko_pool_live.async_setup_entry",
return_value=True,
) as mock_setup_entry,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_EMAIL: "aseko@example.com", CONF_PASSWORD: "new_password"},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unique_id_mismatch"
assert len(mock_setup_entry.mock_calls) == 0
assert mock_entry.unique_id == "UID"
assert dict(mock_entry.data) == {
CONF_EMAIL: "aseko@example.com",
CONF_PASSWORD: "passw0rd",
}
@pytest.mark.parametrize(