Fix totalconnect config flow (#59461)
* update total_connect_client to 2021.10 * update for total_connect_client changes * remove unused return value * bump total_connect_client to 2021.11.1 * bump total_connect_client to 2021.11.2 * Move to public ResultCode * load locations to prevent 'unknown error occurred' * add test for zero locations * put error message in strings * test for abort and message from strings * handle AuthenticationError in step_user * update tests with exceptions * update reauth with exceptions * use try except else per suggestion * only create schema if necessary * catch auth error in async_setup_entry * one more fix in test_init
This commit is contained in:
parent
5fc51130ea
commit
49c4886f40
5 changed files with 67 additions and 30 deletions
|
@ -1,6 +1,8 @@
|
|||
"""Tests for the TotalConnect config flow."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from total_connect_client.exceptions import AuthenticationError
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.components.totalconnect.const import CONF_USERCODES, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
||||
|
@ -92,10 +94,7 @@ async def test_abort_if_already_setup(hass):
|
|||
).add_to_hass(hass)
|
||||
|
||||
# Should fail, same USERNAME (flow)
|
||||
with patch(
|
||||
"homeassistant.components.totalconnect.config_flow.TotalConnectClient"
|
||||
) as client_mock:
|
||||
client_mock.return_value.is_logged_in.return_value = True
|
||||
with patch("homeassistant.components.totalconnect.config_flow.TotalConnectClient"):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
|
@ -111,7 +110,7 @@ async def test_login_failed(hass):
|
|||
with patch(
|
||||
"homeassistant.components.totalconnect.config_flow.TotalConnectClient"
|
||||
) as client_mock:
|
||||
client_mock.return_value.is_logged_in.return_value = False
|
||||
client_mock.side_effect = AuthenticationError()
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
|
@ -143,7 +142,7 @@ async def test_reauth(hass):
|
|||
"homeassistant.components.totalconnect.async_setup_entry", return_value=True
|
||||
):
|
||||
# first test with an invalid password
|
||||
client_mock.return_value.is_logged_in.return_value = False
|
||||
client_mock.side_effect = AuthenticationError()
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_PASSWORD: "password"}
|
||||
|
@ -153,7 +152,7 @@ async def test_reauth(hass):
|
|||
assert result["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
# now test with the password valid
|
||||
client_mock.return_value.is_logged_in.return_value = True
|
||||
client_mock.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_PASSWORD: "password"}
|
||||
|
@ -163,3 +162,31 @@ async def test_reauth(hass):
|
|||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
|
||||
|
||||
async def test_no_locations(hass):
|
||||
"""Test with no user locations."""
|
||||
responses = [
|
||||
RESPONSE_AUTHENTICATE,
|
||||
RESPONSE_PARTITION_DETAILS,
|
||||
RESPONSE_GET_ZONE_DETAILS_SUCCESS,
|
||||
RESPONSE_DISARMED,
|
||||
]
|
||||
|
||||
with patch(TOTALCONNECT_REQUEST, side_effect=responses,) as mock_request, patch(
|
||||
"homeassistant.components.totalconnect.async_setup_entry", return_value=True
|
||||
), patch(
|
||||
"homeassistant.components.totalconnect.TotalConnectClient.get_number_locations",
|
||||
return_value=0,
|
||||
):
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=CONFIG_DATA_NO_USERCODES,
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "no_locations"
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_request.call_count == 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue