Skip to content

Commit

Permalink
Cookidoo reauth config flow for silver (home-assistant#133110)
Browse files Browse the repository at this point in the history
* reauth

* add check for duplicate email in reauth

* fix reauth double email check

* parametrize tests

* check reauth double entry data as well
  • Loading branch information
miaucl authored Dec 13, 2024
1 parent 7f3373d commit 91f7afc
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 4 deletions.
34 changes: 34 additions & 0 deletions homeassistant/components/cookidoo/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,40 @@ async def async_step_language(
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 = {
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/cookidoo/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async def _async_setup(self) -> None:
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={
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/cookidoo/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
2 changes: 1 addition & 1 deletion homeassistant/components/cookidoo/quality_scale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions homeassistant/components/cookidoo/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
124 changes: 124 additions & 0 deletions tests/components/cookidoo/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tests/components/cookidoo/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 91f7afc

Please sign in to comment.