Refactor abode config flow tests (#128334)

* Refactor abode config flow tests

* Cleanup
This commit is contained in:
epenet 2024-10-14 12:13:47 +02:00 committed by GitHub
parent 6d72391ee1
commit d2bbfe1282
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,7 +10,6 @@ from jaraco.abode.helpers.errors import MFA_CODE_REQUIRED
import pytest import pytest
from requests.exceptions import ConnectTimeout from requests.exceptions import ConnectTimeout
from homeassistant.components.abode import config_flow
from homeassistant.components.abode.const import CONF_POLLING, DOMAIN from homeassistant.components.abode.const import CONF_POLLING, DOMAIN
from homeassistant.config_entries import SOURCE_USER from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
@ -22,114 +21,110 @@ from tests.common import MockConfigEntry
pytestmark = pytest.mark.usefixtures("mock_setup_entry") pytestmark = pytest.mark.usefixtures("mock_setup_entry")
async def test_show_form(hass: HomeAssistant) -> None:
"""Test that the form is served with no input."""
flow = config_flow.AbodeFlowHandler()
flow.hass = hass
result = await flow.async_step_user(user_input=None)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
async def test_one_config_allowed(hass: HomeAssistant) -> None: async def test_one_config_allowed(hass: HomeAssistant) -> None:
"""Test that only one Abode configuration is allowed.""" """Test that only one Abode configuration is allowed."""
flow = config_flow.AbodeFlowHandler()
flow.hass = hass
MockConfigEntry( MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
data={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}, data={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"},
).add_to_hass(hass) ).add_to_hass(hass)
step_user_result = await flow.async_step_user() result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert step_user_result["type"] is FlowResultType.ABORT assert result["type"] is FlowResultType.ABORT
assert step_user_result["reason"] == "single_instance_allowed" assert result["reason"] == "single_instance_allowed"
async def test_invalid_credentials(hass: HomeAssistant) -> None: async def test_user_flow(hass: HomeAssistant) -> None:
"""Test that invalid credentials throws an error.""" """Test user flow, with various errors."""
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"} result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
flow = config_flow.AbodeFlowHandler() )
flow.hass = hass assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
# Test that invalid credentials throws an error.
with patch( with patch(
"homeassistant.components.abode.config_flow.Abode", "homeassistant.components.abode.config_flow.Abode",
side_effect=AbodeAuthenticationException( side_effect=AbodeAuthenticationException(
(HTTPStatus.BAD_REQUEST, "auth error") (HTTPStatus.BAD_REQUEST, "auth error")
), ),
): ):
result = await flow.async_step_user(user_input=conf) result = await hass.config_entries.flow.async_configure(
assert result["errors"] == {"base": "invalid_auth"} result["flow_id"],
user_input={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"},
)
async def test_connection_auth_error(hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.FORM
"""Test other than invalid credentials throws an error.""" assert result["step_id"] == "user"
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"} assert result["errors"] == {"base": "invalid_auth"}
flow = config_flow.AbodeFlowHandler()
flow.hass = hass
# Test other than invalid credentials throws an error.
with patch( with patch(
"homeassistant.components.abode.config_flow.Abode", "homeassistant.components.abode.config_flow.Abode",
side_effect=AbodeAuthenticationException( side_effect=AbodeAuthenticationException(
(HTTPStatus.INTERNAL_SERVER_ERROR, "connection error") (HTTPStatus.INTERNAL_SERVER_ERROR, "connection error")
), ),
): ):
result = await flow.async_step_user(user_input=conf) result = await hass.config_entries.flow.async_configure(
assert result["errors"] == {"base": "cannot_connect"} result["flow_id"],
user_input={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"},
)
async def test_connection_error(hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.FORM
"""Test login throws an error if connection times out.""" assert result["step_id"] == "user"
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"} assert result["errors"] == {"base": "cannot_connect"}
flow = config_flow.AbodeFlowHandler()
flow.hass = hass
# Test login throws an error if connection times out.
with patch( with patch(
"homeassistant.components.abode.config_flow.Abode", "homeassistant.components.abode.config_flow.Abode",
side_effect=ConnectTimeout, side_effect=ConnectTimeout,
): ):
result = await flow.async_step_user(user_input=conf) result = await hass.config_entries.flow.async_configure(
assert result["errors"] == {"base": "cannot_connect"} result["flow_id"],
user_input={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "cannot_connect"}
# Test success
async def test_step_user(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_init(
"""Test that the user step works.""" DOMAIN, context={"source": SOURCE_USER}
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"} )
with patch("homeassistant.components.abode.config_flow.Abode"): with patch("homeassistant.components.abode.config_flow.Abode"):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_configure(
DOMAIN, context={"source": SOURCE_USER}, data=conf result["flow_id"],
user_input={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"},
) )
assert result["type"] is FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "user@email.com" assert result["title"] == "user@email.com"
assert result["data"] == { assert result["data"] == {
CONF_USERNAME: "user@email.com", CONF_USERNAME: "user@email.com",
CONF_PASSWORD: "password", CONF_PASSWORD: "password",
CONF_POLLING: False, CONF_POLLING: False,
} }
async def test_step_mfa(hass: HomeAssistant) -> None: async def test_step_mfa(hass: HomeAssistant) -> None:
"""Test that the MFA step works.""" """Test that the MFA step works."""
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
with patch( with patch(
"homeassistant.components.abode.config_flow.Abode", "homeassistant.components.abode.config_flow.Abode",
side_effect=AbodeAuthenticationException(MFA_CODE_REQUIRED), side_effect=AbodeAuthenticationException(MFA_CODE_REQUIRED),
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_configure(
DOMAIN, context={"source": SOURCE_USER}, data=conf result["flow_id"],
user_input={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"},
) )
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "mfa" assert result["step_id"] == "mfa"
with patch( with patch(
"homeassistant.components.abode.config_flow.Abode", "homeassistant.components.abode.config_flow.Abode",
@ -141,46 +136,51 @@ async def test_step_mfa(hass: HomeAssistant) -> None:
result["flow_id"], user_input={"mfa_code": "123456"} result["flow_id"], user_input={"mfa_code": "123456"}
) )
assert result["errors"] == {"base": "invalid_mfa_code"} assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "mfa"
assert result["errors"] == {"base": "invalid_mfa_code"}
with patch("homeassistant.components.abode.config_flow.Abode"): with patch("homeassistant.components.abode.config_flow.Abode"):
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={"mfa_code": "123456"} result["flow_id"], user_input={"mfa_code": "123456"}
) )
assert result["type"] is FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "user@email.com" assert result["title"] == "user@email.com"
assert result["data"] == { assert result["data"] == {
CONF_USERNAME: "user@email.com", CONF_USERNAME: "user@email.com",
CONF_PASSWORD: "password", CONF_PASSWORD: "password",
CONF_POLLING: False, CONF_POLLING: False,
} }
async def test_step_reauth(hass: HomeAssistant) -> None: async def test_step_reauth(hass: HomeAssistant) -> None:
"""Test the reauth flow.""" """Test the reauth flow."""
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
unique_id="user@email.com", unique_id="user@email.com",
data=conf, data={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"},
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
with patch("homeassistant.components.abode.config_flow.Abode"): result = await entry.start_reauth_flow(hass)
result = await entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm" assert result["step_id"] == "reauth_confirm"
with patch("homeassistant.config_entries.ConfigEntries.async_reload"): with (
result = await hass.config_entries.flow.async_configure( patch("homeassistant.components.abode.config_flow.Abode"),
result["flow_id"], ):
user_input=conf, result = await hass.config_entries.flow.async_configure(
) result["flow_id"],
user_input={
CONF_USERNAME: "user@email.com",
CONF_PASSWORD: "new_password",
},
)
assert result["type"] is FlowResultType.ABORT assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful" assert result["reason"] == "reauth_successful"
assert len(hass.config_entries.async_entries()) == 1 assert len(hass.config_entries.async_entries()) == 1
assert entry.data[CONF_PASSWORD] == "new_password"