Add reauth support to NUT (#114131)
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
from ipaddress import ip_address
|
||||
from unittest.mock import patch
|
||||
|
||||
from aionut import NUTError
|
||||
from aionut import NUTError, NUTLoginError
|
||||
|
||||
from homeassistant import config_entries, data_entry_flow, setup
|
||||
from homeassistant.components import zeroconf
|
||||
@@ -232,8 +232,8 @@ async def test_form_user_one_ups_with_ignored_entry(hass: HomeAssistant) -> None
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
||||
"""Test we handle cannot connect error."""
|
||||
async def test_form_no_upses_found(hass: HomeAssistant) -> None:
|
||||
"""Test we abort when the NUT server has not UPSes."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
@@ -254,15 +254,22 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
assert result2["type"] is data_entry_flow.FlowResultType.ABORT
|
||||
assert result2["reason"] == "no_ups_found"
|
||||
|
||||
|
||||
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
||||
"""Test we handle cannot connect error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient.list_ups",
|
||||
side_effect=NUTError,
|
||||
side_effect=NUTError("no route to host"),
|
||||
), patch(
|
||||
"homeassistant.components.nut.AIONUTClient.list_vars",
|
||||
side_effect=NUTError,
|
||||
side_effect=NUTError("no route to host"),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
@@ -276,6 +283,7 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
||||
|
||||
assert result2["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
assert result2["description_placeholders"] == {"error": "no route to host"}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient.list_ups",
|
||||
@@ -297,6 +305,154 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
||||
assert result2["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_vars={"battery.voltage": "voltage", "ups.status": "OL"}, list_ups=["ups1"]
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
), patch(
|
||||
"homeassistant.components.nut.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_PASSWORD: "test-password",
|
||||
CONF_PORT: 2222,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] is data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "1.1.1.1:2222"
|
||||
assert result2["data"] == {
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_PASSWORD: "test-password",
|
||||
CONF_PORT: 2222,
|
||||
CONF_USERNAME: "test-username",
|
||||
}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_auth_failures(hass: HomeAssistant) -> None:
|
||||
"""Test authentication failures."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient.list_ups",
|
||||
side_effect=NUTLoginError,
|
||||
), patch(
|
||||
"homeassistant.components.nut.AIONUTClient.list_vars",
|
||||
side_effect=NUTLoginError,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_PASSWORD: "test-password",
|
||||
CONF_PORT: 2222,
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is data_entry_flow.FlowResultType.FORM
|
||||
assert result2["errors"] == {"password": "invalid_auth"}
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_vars={"battery.voltage": "voltage", "ups.status": "OL"}, list_ups=["ups1"]
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
), patch(
|
||||
"homeassistant.components.nut.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_PASSWORD: "test-password",
|
||||
CONF_PORT: 2222,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] is data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "1.1.1.1:2222"
|
||||
assert result2["data"] == {
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_PASSWORD: "test-password",
|
||||
CONF_PORT: 2222,
|
||||
CONF_USERNAME: "test-username",
|
||||
}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_reauth(hass: HomeAssistant) -> None:
|
||||
"""Test reauth flow."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_PORT: 123,
|
||||
CONF_RESOURCES: ["battery.voltage"],
|
||||
},
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
config_entry.async_start_reauth(hass)
|
||||
await hass.async_block_till_done()
|
||||
flows = hass.config_entries.flow.async_progress_by_handler(DOMAIN)
|
||||
assert len(flows) == 1
|
||||
flow = flows[0]
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient.list_ups",
|
||||
side_effect=NUTLoginError,
|
||||
), patch(
|
||||
"homeassistant.components.nut.AIONUTClient.list_vars",
|
||||
side_effect=NUTLoginError,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
flow["flow_id"],
|
||||
{
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is data_entry_flow.FlowResultType.FORM
|
||||
assert result2["errors"] == {"password": "invalid_auth"}
|
||||
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_vars={"battery.voltage": "voltage", "ups.status": "OL"}, list_ups=["ups1"]
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
), patch(
|
||||
"homeassistant.components.nut.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
flow["flow_id"],
|
||||
{
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] is data_entry_flow.FlowResultType.ABORT
|
||||
assert result2["reason"] == "reauth_successful"
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_abort_if_already_setup(hass: HomeAssistant) -> None:
|
||||
"""Test we abort if component is already setup."""
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from aionut import NUTError
|
||||
from aionut import NUTError, NUTLoginError
|
||||
|
||||
from homeassistant.components.nut.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
@@ -66,3 +66,27 @@ async def test_config_not_ready(hass: HomeAssistant) -> None:
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_auth_fails(hass: HomeAssistant) -> None:
|
||||
"""Test for setup failure if auth has changed."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={CONF_HOST: "mock", CONF_PORT: "mock"},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.AIONUTClient.list_ups",
|
||||
return_value={"ups1"},
|
||||
), patch(
|
||||
"homeassistant.components.nut.AIONUTClient.list_vars",
|
||||
side_effect=NUTLoginError,
|
||||
):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert entry.state is ConfigEntryState.SETUP_ERROR
|
||||
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
assert len(flows) == 1
|
||||
assert flows[0]["context"]["source"] == "reauth"
|
||||
|
||||
Reference in New Issue
Block a user