mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Prevent changing email address in inexogy reauth (#131632)
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
parent
bf9e7e4a0c
commit
883c6121cf
@ -11,12 +11,7 @@ from pydiscovergy.authentication import BasicAuth
|
|||||||
import pydiscovergy.error as discovergyError
|
import pydiscovergy.error as discovergyError
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import (
|
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
|
||||||
SOURCE_REAUTH,
|
|
||||||
ConfigEntry,
|
|
||||||
ConfigFlow,
|
|
||||||
ConfigFlowResult,
|
|
||||||
)
|
|
||||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
||||||
from homeassistant.helpers.httpx_client import get_async_client
|
from homeassistant.helpers.httpx_client import get_async_client
|
||||||
from homeassistant.helpers.selector import (
|
from homeassistant.helpers.selector import (
|
||||||
@ -57,35 +52,14 @@ class DiscovergyConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
VERSION = 1
|
VERSION = 1
|
||||||
|
|
||||||
_existing_entry: ConfigEntry
|
|
||||||
|
|
||||||
async def async_step_user(
|
|
||||||
self, user_input: dict[str, Any] | None = None
|
|
||||||
) -> ConfigFlowResult:
|
|
||||||
"""Handle the initial step."""
|
|
||||||
if user_input is None:
|
|
||||||
return self.async_show_form(
|
|
||||||
step_id="user",
|
|
||||||
data_schema=CONFIG_SCHEMA,
|
|
||||||
)
|
|
||||||
|
|
||||||
return await self._validate_and_save(user_input)
|
|
||||||
|
|
||||||
async def async_step_reauth(
|
async def async_step_reauth(
|
||||||
self, entry_data: Mapping[str, Any]
|
self, entry_data: Mapping[str, Any]
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle the initial step."""
|
"""Handle the initial step."""
|
||||||
self._existing_entry = self._get_reauth_entry()
|
return await self.async_step_user()
|
||||||
return await self.async_step_reauth_confirm()
|
|
||||||
|
|
||||||
async def async_step_reauth_confirm(
|
async def async_step_user(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: Mapping[str, Any] | None = None
|
||||||
) -> ConfigFlowResult:
|
|
||||||
"""Handle the reauth step."""
|
|
||||||
return await self._validate_and_save(user_input, step_id="reauth_confirm")
|
|
||||||
|
|
||||||
async def _validate_and_save(
|
|
||||||
self, user_input: Mapping[str, Any] | None = None, step_id: str = "user"
|
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Validate user input and create config entry."""
|
"""Validate user input and create config entry."""
|
||||||
errors = {}
|
errors = {}
|
||||||
@ -106,17 +80,17 @@ class DiscovergyConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
_LOGGER.exception("Unexpected error occurred while getting meters")
|
_LOGGER.exception("Unexpected error occurred while getting meters")
|
||||||
errors["base"] = "unknown"
|
errors["base"] = "unknown"
|
||||||
else:
|
else:
|
||||||
|
await self.async_set_unique_id(user_input[CONF_EMAIL].lower())
|
||||||
|
|
||||||
if self.source == SOURCE_REAUTH:
|
if self.source == SOURCE_REAUTH:
|
||||||
|
self._abort_if_unique_id_mismatch(reason="account_mismatch")
|
||||||
return self.async_update_reload_and_abort(
|
return self.async_update_reload_and_abort(
|
||||||
entry=self._existing_entry,
|
entry=self._get_reauth_entry(),
|
||||||
data={
|
data_updates={
|
||||||
CONF_EMAIL: user_input[CONF_EMAIL],
|
|
||||||
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
# set unique id to title which is the account email
|
|
||||||
await self.async_set_unique_id(user_input[CONF_EMAIL].lower())
|
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
@ -124,10 +98,10 @@ class DiscovergyConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
)
|
)
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id=step_id,
|
step_id="user",
|
||||||
data_schema=self.add_suggested_values_to_schema(
|
data_schema=self.add_suggested_values_to_schema(
|
||||||
CONFIG_SCHEMA,
|
CONFIG_SCHEMA,
|
||||||
self._existing_entry.data
|
self._get_reauth_entry().data
|
||||||
if self.source == SOURCE_REAUTH
|
if self.source == SOURCE_REAUTH
|
||||||
else user_input,
|
else user_input,
|
||||||
),
|
),
|
||||||
|
@ -6,12 +6,6 @@
|
|||||||
"email": "[%key:common::config_flow::data::email%]",
|
"email": "[%key:common::config_flow::data::email%]",
|
||||||
"password": "[%key:common::config_flow::data::password%]"
|
"password": "[%key:common::config_flow::data::password%]"
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"reauth_confirm": {
|
|
||||||
"data": {
|
|
||||||
"email": "[%key:common::config_flow::data::email%]",
|
|
||||||
"password": "[%key:common::config_flow::data::password%]"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
@ -21,6 +15,7 @@
|
|||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
|
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
|
||||||
|
"account_mismatch": "The inexogy account authenticated with, does not match the account needed re-authentication.",
|
||||||
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -20,7 +20,7 @@ async def test_form(hass: HomeAssistant, discovergy: AsyncMock) -> None:
|
|||||||
DOMAIN, context={"source": SOURCE_USER}
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["errors"] is None
|
assert result["errors"] == {}
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.discovergy.async_setup_entry",
|
"homeassistant.components.discovergy.async_setup_entry",
|
||||||
@ -51,7 +51,7 @@ async def test_reauth(
|
|||||||
config_entry.add_to_hass(hass)
|
config_entry.add_to_hass(hass)
|
||||||
init_result = await config_entry.start_reauth_flow(hass)
|
init_result = await config_entry.start_reauth_flow(hass)
|
||||||
assert init_result["type"] is FlowResultType.FORM
|
assert init_result["type"] is FlowResultType.FORM
|
||||||
assert init_result["step_id"] == "reauth_confirm"
|
assert init_result["step_id"] == "user"
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.discovergy.async_setup_entry",
|
"homeassistant.components.discovergy.async_setup_entry",
|
||||||
@ -60,7 +60,7 @@ async def test_reauth(
|
|||||||
configure_result = await hass.config_entries.flow.async_configure(
|
configure_result = await hass.config_entries.flow.async_configure(
|
||||||
init_result["flow_id"],
|
init_result["flow_id"],
|
||||||
{
|
{
|
||||||
CONF_EMAIL: "test@example.com",
|
CONF_EMAIL: "user@example.org",
|
||||||
CONF_PASSWORD: "test-password",
|
CONF_PASSWORD: "test-password",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -111,3 +111,30 @@ async def test_form_fail(
|
|||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == "test@example.com"
|
assert result["title"] == "test@example.com"
|
||||||
assert "errors" not in result
|
assert "errors" not in result
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reauth_unique_id_mismatch(
|
||||||
|
hass: HomeAssistant, config_entry: MockConfigEntry, discovergy: AsyncMock
|
||||||
|
) -> None:
|
||||||
|
"""Test reauth flow with unique id mismatch."""
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
result = await config_entry.start_reauth_flow(hass)
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "user"
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.discovergy.async_setup_entry",
|
||||||
|
return_value=True,
|
||||||
|
):
|
||||||
|
configure_result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{
|
||||||
|
CONF_EMAIL: "user2@example.org",
|
||||||
|
CONF_PASSWORD: "test-password",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert configure_result["type"] is FlowResultType.ABORT
|
||||||
|
assert configure_result["reason"] == "account_mismatch"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user