mirror of
https://github.com/home-assistant/core.git
synced 2026-09-25 17:04:04 -04:00
Normalize Hive auth username before login (#168827)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Josef Zweck <josef@zweck.dev> Co-authored-by: Joostlek <joostlek@outlook.com>
This commit is contained in:
co-authored by
Copilot Autofix powered by AI
Josef Zweck
Joostlek
parent
76ca483aec
commit
c20b1a5d4d
@@ -50,12 +50,14 @@ class HiveFlowHandler(ConfigFlow, domain=DOMAIN):
|
||||
# Login to Hive with user data.
|
||||
if user_input is not None:
|
||||
self.data.update(user_input)
|
||||
username = self.data[CONF_USERNAME].lower()
|
||||
self.hive_auth = Auth(
|
||||
username=self.data[CONF_USERNAME], password=self.data[CONF_PASSWORD]
|
||||
username=username,
|
||||
password=self.data[CONF_PASSWORD],
|
||||
)
|
||||
|
||||
# Get user from existing entry and abort if already setup
|
||||
await self.async_set_unique_id(self.data[CONF_USERNAME])
|
||||
await self.async_set_unique_id(username)
|
||||
if self.context["source"] != SOURCE_REAUTH:
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
"""Test the Hive config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from apyhiveapi.helper import hive_exceptions
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.hive.const import CONF_CODE, CONF_DEVICE_NAME, DOMAIN
|
||||
@@ -25,8 +26,13 @@ MFA_RESEND_CODE = "0000"
|
||||
MFA_INVALID_CODE = "HIVE"
|
||||
|
||||
|
||||
async def test_user_flow(hass: HomeAssistant) -> None:
|
||||
"""Test the user flow."""
|
||||
@pytest.mark.parametrize(
|
||||
"username",
|
||||
[USERNAME, USERNAME.upper()],
|
||||
ids=["lowercase", "uppercase"],
|
||||
)
|
||||
async def test_user_flow(hass: HomeAssistant, username: str) -> None:
|
||||
"""Test the user flow normalizes the username for Hive auth."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
@@ -35,31 +41,31 @@ async def test_user_flow(hass: HomeAssistant) -> None:
|
||||
assert result["errors"] == {}
|
||||
|
||||
with (
|
||||
patch("homeassistant.components.hive.config_flow.Auth") as mock_auth,
|
||||
patch(
|
||||
"homeassistant.components.hive.config_flow.Auth.login",
|
||||
"homeassistant.components.hive.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry,
|
||||
):
|
||||
mock_auth.return_value.login = AsyncMock(
|
||||
return_value={
|
||||
"ChallengeName": "SUCCESS",
|
||||
"AuthenticationResult": {
|
||||
"RefreshToken": "mock-refresh-token",
|
||||
"AccessToken": "mock-access-token",
|
||||
},
|
||||
},
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.hive.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry,
|
||||
):
|
||||
}
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD},
|
||||
{CONF_USERNAME: username, CONF_PASSWORD: PASSWORD},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == USERNAME
|
||||
assert result["title"] == username
|
||||
assert result["data"] == {
|
||||
CONF_USERNAME: USERNAME,
|
||||
CONF_USERNAME: username,
|
||||
CONF_PASSWORD: PASSWORD,
|
||||
"tokens": {
|
||||
"AuthenticationResult": {
|
||||
@@ -69,9 +75,10 @@ async def test_user_flow(hass: HomeAssistant) -> None:
|
||||
"ChallengeName": "SUCCESS",
|
||||
},
|
||||
}
|
||||
|
||||
assert mock_auth.call_args.kwargs["username"] == username.lower()
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert hass.config_entries.async_entries(DOMAIN)[0].unique_id == username.lower()
|
||||
|
||||
|
||||
async def test_user_flow_with_no_2fa(hass: HomeAssistant) -> None:
|
||||
@@ -734,7 +741,7 @@ async def test_abort_if_existing_entry(hass: HomeAssistant) -> None:
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
data={
|
||||
CONF_USERNAME: USERNAME,
|
||||
CONF_USERNAME: USERNAME.upper(),
|
||||
CONF_PASSWORD: PASSWORD,
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user