Add reauth flow to fibaro (#74300)
This commit is contained in:
parent
420733a064
commit
601fb5ebb5
4 changed files with 212 additions and 15 deletions
|
@ -9,6 +9,8 @@ from homeassistant.components.fibaro import DOMAIN
|
|||
from homeassistant.components.fibaro.const import CONF_IMPORT_PLUGINS
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
TEST_SERIALNUMBER = "HC2-111111"
|
||||
TEST_NAME = "my_fibaro_home_center"
|
||||
TEST_URL = "http://192.168.1.1/api/"
|
||||
|
@ -31,20 +33,27 @@ def fibaro_client_fixture():
|
|||
client_mock = Mock()
|
||||
client_mock.base_url.return_value = TEST_URL
|
||||
|
||||
with patch("fiblary3.client.v4.client.Client.__init__", return_value=None,), patch(
|
||||
"fiblary3.client.v4.client.Client.info",
|
||||
with patch(
|
||||
"homeassistant.components.fibaro.FibaroClientV4.__init__",
|
||||
return_value=None,
|
||||
), patch(
|
||||
"homeassistant.components.fibaro.FibaroClientV4.info",
|
||||
info_mock,
|
||||
create=True,
|
||||
), patch("fiblary3.client.v4.client.Client.rooms", array_mock, create=True,), patch(
|
||||
"fiblary3.client.v4.client.Client.devices",
|
||||
), patch(
|
||||
"homeassistant.components.fibaro.FibaroClientV4.rooms",
|
||||
array_mock,
|
||||
create=True,
|
||||
), patch(
|
||||
"fiblary3.client.v4.client.Client.scenes",
|
||||
"homeassistant.components.fibaro.FibaroClientV4.devices",
|
||||
array_mock,
|
||||
create=True,
|
||||
), patch(
|
||||
"fiblary3.client.v4.client.Client.client",
|
||||
"homeassistant.components.fibaro.FibaroClientV4.scenes",
|
||||
array_mock,
|
||||
create=True,
|
||||
), patch(
|
||||
"homeassistant.components.fibaro.FibaroClientV4.client",
|
||||
client_mock,
|
||||
create=True,
|
||||
):
|
||||
|
@ -64,7 +73,7 @@ async def test_config_flow_user_initiated_success(hass):
|
|||
login_mock = Mock()
|
||||
login_mock.get.return_value = Mock(status=True)
|
||||
with patch(
|
||||
"fiblary3.client.v4.client.Client.login", login_mock, create=True
|
||||
"homeassistant.components.fibaro.FibaroClientV4.login", login_mock, create=True
|
||||
), patch(
|
||||
"homeassistant.components.fibaro.async_setup_entry",
|
||||
return_value=True,
|
||||
|
@ -100,7 +109,9 @@ async def test_config_flow_user_initiated_connect_failure(hass):
|
|||
|
||||
login_mock = Mock()
|
||||
login_mock.get.return_value = Mock(status=False)
|
||||
with patch("fiblary3.client.v4.client.Client.login", login_mock, create=True):
|
||||
with patch(
|
||||
"homeassistant.components.fibaro.FibaroClientV4.login", login_mock, create=True
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
|
@ -127,7 +138,9 @@ async def test_config_flow_user_initiated_auth_failure(hass):
|
|||
|
||||
login_mock = Mock()
|
||||
login_mock.get.side_effect = HTTPException(details="Forbidden")
|
||||
with patch("fiblary3.client.v4.client.Client.login", login_mock, create=True):
|
||||
with patch(
|
||||
"homeassistant.components.fibaro.FibaroClientV4.login", login_mock, create=True
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
|
@ -154,7 +167,9 @@ async def test_config_flow_user_initiated_unknown_failure_1(hass):
|
|||
|
||||
login_mock = Mock()
|
||||
login_mock.get.side_effect = HTTPException(details="Any")
|
||||
with patch("fiblary3.client.v4.client.Client.login", login_mock, create=True):
|
||||
with patch(
|
||||
"homeassistant.components.fibaro.FibaroClientV4.login", login_mock, create=True
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
|
@ -198,7 +213,7 @@ async def test_config_flow_import(hass):
|
|||
login_mock = Mock()
|
||||
login_mock.get.return_value = Mock(status=True)
|
||||
with patch(
|
||||
"fiblary3.client.v4.client.Client.login", login_mock, create=True
|
||||
"homeassistant.components.fibaro.FibaroClientV4.login", login_mock, create=True
|
||||
), patch(
|
||||
"homeassistant.components.fibaro.async_setup_entry",
|
||||
return_value=True,
|
||||
|
@ -222,3 +237,128 @@ async def test_config_flow_import(hass):
|
|||
CONF_PASSWORD: TEST_PASSWORD,
|
||||
CONF_IMPORT_PLUGINS: False,
|
||||
}
|
||||
|
||||
|
||||
async def test_reauth_success(hass):
|
||||
"""Successful reauth flow initialized by the user."""
|
||||
mock_config = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
entry_id=TEST_SERIALNUMBER,
|
||||
data={
|
||||
CONF_URL: TEST_URL,
|
||||
CONF_USERNAME: TEST_USERNAME,
|
||||
CONF_PASSWORD: TEST_PASSWORD,
|
||||
CONF_IMPORT_PLUGINS: False,
|
||||
},
|
||||
)
|
||||
mock_config.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={
|
||||
"source": config_entries.SOURCE_REAUTH,
|
||||
"entry_id": mock_config.entry_id,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
assert result["errors"] == {}
|
||||
|
||||
login_mock = Mock()
|
||||
login_mock.get.return_value = Mock(status=True)
|
||||
with patch(
|
||||
"homeassistant.components.fibaro.FibaroClientV4.login", login_mock, create=True
|
||||
), patch(
|
||||
"homeassistant.components.fibaro.async_setup_entry",
|
||||
return_value=True,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_PASSWORD: "other_fake_password"},
|
||||
)
|
||||
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "reauth_successful"
|
||||
|
||||
|
||||
async def test_reauth_connect_failure(hass):
|
||||
"""Successful reauth flow initialized by the user."""
|
||||
mock_config = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
entry_id=TEST_SERIALNUMBER,
|
||||
data={
|
||||
CONF_URL: TEST_URL,
|
||||
CONF_USERNAME: TEST_USERNAME,
|
||||
CONF_PASSWORD: TEST_PASSWORD,
|
||||
CONF_IMPORT_PLUGINS: False,
|
||||
},
|
||||
)
|
||||
mock_config.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={
|
||||
"source": config_entries.SOURCE_REAUTH,
|
||||
"entry_id": mock_config.entry_id,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
assert result["errors"] == {}
|
||||
|
||||
login_mock = Mock()
|
||||
login_mock.get.return_value = Mock(status=False)
|
||||
with patch(
|
||||
"homeassistant.components.fibaro.FibaroClientV4.login", login_mock, create=True
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_PASSWORD: "other_fake_password"},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_reauth_auth_failure(hass):
|
||||
"""Successful reauth flow initialized by the user."""
|
||||
mock_config = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
entry_id=TEST_SERIALNUMBER,
|
||||
data={
|
||||
CONF_URL: TEST_URL,
|
||||
CONF_USERNAME: TEST_USERNAME,
|
||||
CONF_PASSWORD: TEST_PASSWORD,
|
||||
CONF_IMPORT_PLUGINS: False,
|
||||
},
|
||||
)
|
||||
mock_config.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={
|
||||
"source": config_entries.SOURCE_REAUTH,
|
||||
"entry_id": mock_config.entry_id,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
assert result["errors"] == {}
|
||||
|
||||
login_mock = Mock()
|
||||
login_mock.get.side_effect = HTTPException(details="Forbidden")
|
||||
with patch(
|
||||
"homeassistant.components.fibaro.FibaroClientV4.login", login_mock, create=True
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_PASSWORD: "other_fake_password"},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
assert result["errors"] == {"base": "invalid_auth"}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue