Cookidoo reauth config flow for silver (#133110)

* reauth

* add check for duplicate email in reauth

* fix reauth double email check

* parametrize tests

* check reauth double entry data as well
This commit is contained in:
Cyrill Raccaud 2024-12-13 10:40:23 +01:00 committed by GitHub
parent 7f3373d233
commit 91f7afc2c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 174 additions and 4 deletions

View File

@ -102,6 +102,40 @@ class CookidooConfigFlow(ConfigFlow, domain=DOMAIN):
errors=errors,
)
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Perform reauth upon an API authentication error."""
return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Dialog that informs the user that reauth is required."""
errors: dict[str, str] = {}
reauth_entry = self._get_reauth_entry()
if user_input is not None:
if not (
errors := await self.validate_input({**reauth_entry.data, **user_input})
):
if user_input[CONF_EMAIL] != reauth_entry.data[CONF_EMAIL]:
self._async_abort_entries_match(
{CONF_EMAIL: user_input[CONF_EMAIL]}
)
return self.async_update_reload_and_abort(
reauth_entry, data_updates=user_input
)
return self.async_show_form(
step_id="reauth_confirm",
data_schema=self.add_suggested_values_to_schema(
data_schema=vol.Schema(AUTH_DATA_SCHEMA),
suggested_values={CONF_EMAIL: reauth_entry.data[CONF_EMAIL]},
),
errors=errors,
)
async def generate_country_schema(self) -> None:
"""Generate country schema."""
self.COUNTRY_DATA_SCHEMA = {

View File

@ -63,7 +63,7 @@ class CookidooDataUpdateCoordinator(DataUpdateCoordinator[CookidooData]):
translation_key="setup_request_exception",
) from e
except CookidooAuthException as e:
raise UpdateFailed(
raise ConfigEntryAuthFailed(
translation_domain=DOMAIN,
translation_key="setup_authentication_exception",
translation_placeholders={

View File

@ -6,6 +6,6 @@
"documentation": "https://www.home-assistant.io/integrations/cookidoo",
"integration_type": "service",
"iot_class": "cloud_polling",
"quality_scale": "bronze",
"quality_scale": "silver",
"requirements": ["cookidoo-api==0.10.0"]
}

View File

@ -38,7 +38,7 @@ rules:
action-exceptions:
status: done
comment: Only providing todo actions
reauthentication-flow: todo
reauthentication-flow: done
parallel-updates: done
test-coverage: done
integration-owner: done

View File

@ -22,6 +22,18 @@
"data_description": {
"language": "Pick your language for the Cookidoo content."
}
},
"reauth_confirm": {
"title": "Login again to Cookidoo",
"description": "Please log in to Cookidoo again to continue using this integration.",
"data": {
"email": "[%key:common::config_flow::data::email%]",
"password": "[%key:common::config_flow::data::password%]"
},
"data_description": {
"email": "[%key:component::cookidoo::config::step::user::data_description::email%]",
"password": "[%key:component::cookidoo::config::step::user::data_description::password%]"
}
}
},
"error": {

View File

@ -180,3 +180,127 @@ async def test_flow_user_init_data_already_configured(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
async def test_flow_reauth(
hass: HomeAssistant,
mock_cookidoo_client: AsyncMock,
cookidoo_config_entry: MockConfigEntry,
) -> None:
"""Test reauth flow."""
cookidoo_config_entry.add_to_hass(hass)
result = await cookidoo_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"],
{CONF_EMAIL: "new-email", CONF_PASSWORD: "new-password"},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
assert cookidoo_config_entry.data == {
CONF_EMAIL: "new-email",
CONF_PASSWORD: "new-password",
CONF_COUNTRY: COUNTRY,
CONF_LANGUAGE: LANGUAGE,
}
assert len(hass.config_entries.async_entries()) == 1
@pytest.mark.parametrize(
("raise_error", "text_error"),
[
(CookidooRequestException(), "cannot_connect"),
(CookidooAuthException(), "invalid_auth"),
(CookidooException(), "unknown"),
(IndexError(), "unknown"),
],
)
async def test_flow_reauth_error_and_recover(
hass: HomeAssistant,
mock_cookidoo_client: AsyncMock,
cookidoo_config_entry: MockConfigEntry,
raise_error,
text_error,
) -> None:
"""Test reauth flow."""
cookidoo_config_entry.add_to_hass(hass)
result = await cookidoo_config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
mock_cookidoo_client.login.side_effect = raise_error
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_EMAIL: "new-email", CONF_PASSWORD: "new-password"},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": text_error}
mock_cookidoo_client.login.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_EMAIL: "new-email", CONF_PASSWORD: "new-password"},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
assert cookidoo_config_entry.data == {
CONF_EMAIL: "new-email",
CONF_PASSWORD: "new-password",
CONF_COUNTRY: COUNTRY,
CONF_LANGUAGE: LANGUAGE,
}
assert len(hass.config_entries.async_entries()) == 1
@pytest.mark.parametrize(
("new_email", "saved_email", "result_reason"),
[
(EMAIL, EMAIL, "reauth_successful"),
("another-email", EMAIL, "already_configured"),
],
)
async def test_flow_reauth_init_data_already_configured(
hass: HomeAssistant,
mock_cookidoo_client: AsyncMock,
cookidoo_config_entry: MockConfigEntry,
new_email: str,
saved_email: str,
result_reason: str,
) -> None:
"""Test we abort user data set when entry is already configured."""
cookidoo_config_entry.add_to_hass(hass)
another_cookidoo_config_entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_EMAIL: "another-email",
CONF_PASSWORD: PASSWORD,
CONF_COUNTRY: COUNTRY,
CONF_LANGUAGE: LANGUAGE,
},
)
another_cookidoo_config_entry.add_to_hass(hass)
result = await cookidoo_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"],
{CONF_EMAIL: new_email, CONF_PASSWORD: PASSWORD},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == result_reason
assert cookidoo_config_entry.data[CONF_EMAIL] == saved_email

View File

@ -35,7 +35,7 @@ async def test_load_unload(
("exception", "status"),
[
(CookidooRequestException, ConfigEntryState.SETUP_RETRY),
(CookidooAuthException, ConfigEntryState.SETUP_RETRY),
(CookidooAuthException, ConfigEntryState.SETUP_ERROR),
],
)
async def test_init_failure(