Add start_reauth helper method to MockConfigEntry (#124767)

* Add start_reauth helper method to MockConfigEntry

* Two more
This commit is contained in:
epenet 2024-08-28 13:08:26 +02:00 committed by GitHub
parent 274d98f4d7
commit 14eec2e57a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 41 additions and 70 deletions

View File

@ -47,7 +47,7 @@ from homeassistant.components.device_automation import ( # noqa: F401
_async_get_device_automation_capabilities as async_get_device_automation_capabilities,
)
from homeassistant.config import IntegrationConfigInfo, async_process_component_config
from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
from homeassistant.const import (
DEVICE_DEFAULT_NAME,
EVENT_HOMEASSISTANT_CLOSE,
@ -1054,6 +1054,19 @@ class MockConfigEntry(config_entries.ConfigEntry):
"""
self._async_set_state(hass, state, reason)
async def start_reauth_flow(self, hass: HomeAssistant) -> ConfigFlowResult:
"""Start a reauthentication flow."""
return await hass.config_entries.flow.async_init(
self.domain,
context={
"source": config_entries.SOURCE_REAUTH,
"entry_id": self.entry_id,
"title_placeholders": {"name": self.title},
"unique_id": self.unique_id,
},
data=self.data,
)
def patch_yaml_files(files_dict, endswith=True):
"""Patch load_yaml with a dictionary of yaml files."""

View File

@ -12,7 +12,7 @@ from requests.exceptions import ConnectTimeout
from homeassistant.components.abode import config_flow
from homeassistant.components.abode.const import CONF_POLLING, DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@ -161,18 +161,15 @@ async def test_step_reauth(hass: HomeAssistant) -> None:
"""Test the reauth flow."""
conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}
MockConfigEntry(
entry = MockConfigEntry(
domain=DOMAIN,
unique_id="user@email.com",
data=conf,
).add_to_hass(hass)
)
entry.add_to_hass(hass)
with patch("homeassistant.components.abode.config_flow.Abode"):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_REAUTH},
data=conf,
)
result = await entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"

View File

@ -18,7 +18,7 @@ from homeassistant.components.airvisual import (
INTEGRATION_TYPE_GEOGRAPHY_COORDS,
INTEGRATION_TYPE_GEOGRAPHY_NAME,
)
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_API_KEY, CONF_SHOW_ON_MAP
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@ -33,6 +33,8 @@ from .conftest import (
TEST_STATE,
)
from tests.common import MockConfigEntry
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
@ -146,12 +148,10 @@ async def test_options_flow(
async def test_step_reauth(
hass: HomeAssistant, config_entry, setup_config_entry
hass: HomeAssistant, config_entry: MockConfigEntry, setup_config_entry
) -> None:
"""Test that the reauth step works."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_REAUTH}, data=config_entry.data
)
result = await config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"

View File

@ -10,11 +10,13 @@ from pyairvisual.node import (
import pytest
from homeassistant.components.airvisual_pro.const import DOMAIN
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_REAUTH, SOURCE_USER
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
@ -98,22 +100,14 @@ async def test_step_import(hass: HomeAssistant, config, setup_airvisual_pro) ->
async def test_reauth(
hass: HomeAssistant,
config,
config_entry,
config_entry: MockConfigEntry,
connect_errors,
connect_mock,
pro,
setup_airvisual_pro,
) -> None:
"""Test re-auth (including errors)."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": SOURCE_REAUTH,
"entry_id": config_entry.entry_id,
"unique_id": config_entry.unique_id,
},
data=config,
)
result = await config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"

View File

@ -248,9 +248,7 @@ async def test_form_reauth(hass: HomeAssistant) -> None:
)
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=entry.data
)
result = await entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
@ -294,9 +292,7 @@ async def test_form_reauth_with_2fa(hass: HomeAssistant) -> None:
)
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=entry.data
)
result = await entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}

View File

@ -7,7 +7,7 @@ from aiohttp.client_exceptions import ClientConnectorError
from python_awair.exceptions import AuthError, AwairError
from homeassistant.components.awair.const import DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER, SOURCE_ZEROCONF
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@ -136,11 +136,7 @@ async def test_reauth(hass: HomeAssistant, user, cloud_devices) -> None:
)
mock_config.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_REAUTH, "unique_id": CLOUD_UNIQUE_ID},
data={**CLOUD_CONFIG, CONF_ACCESS_TOKEN: "blah"},
)
result = await mock_config.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {}
@ -180,11 +176,7 @@ async def test_reauth_error(hass: HomeAssistant) -> None:
)
mock_config.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_REAUTH, "unique_id": CLOUD_UNIQUE_ID},
data={**CLOUD_CONFIG, CONF_ACCESS_TOKEN: "blah"},
)
result = await mock_config.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {}

View File

@ -17,7 +17,6 @@ from homeassistant.components.axis.const import (
)
from homeassistant.config_entries import (
SOURCE_DHCP,
SOURCE_REAUTH,
SOURCE_RECONFIGURE,
SOURCE_SSDP,
SOURCE_USER,
@ -205,12 +204,7 @@ async def test_reauth_flow_update_configuration(
assert config_entry_setup.data[CONF_USERNAME] == "root"
assert config_entry_setup.data[CONF_PASSWORD] == "pass"
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN,
context={"source": SOURCE_REAUTH},
data=config_entry_setup.data,
)
result = await config_entry_setup.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"

View File

@ -53,18 +53,14 @@ async def test_authorization_error(
async def test_reauth_authorization_error(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_devops_client: AsyncMock,
) -> None:
"""Test we show user form on Azure DevOps authorization error."""
mock_devops_client.authorize.return_value = False
mock_devops_client.authorized = False
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_REAUTH},
data=FIXTURE_USER_INPUT,
)
result = await mock_config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth"
@ -108,17 +104,14 @@ async def test_connection_error(
async def test_reauth_connection_error(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_devops_client: AsyncMock,
) -> None:
"""Test we show user form on Azure DevOps connection error."""
mock_devops_client.authorize.side_effect = aiohttp.ClientError
mock_devops_client.authorized = False
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_REAUTH},
data=FIXTURE_USER_INPUT,
)
result = await mock_config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth"
@ -174,11 +167,7 @@ async def test_reauth_project_error(
mock_config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_REAUTH},
data=FIXTURE_USER_INPUT,
)
result = await mock_config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth"
@ -205,11 +194,7 @@ async def test_reauth_flow(
mock_config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_REAUTH},
data=FIXTURE_USER_INPUT,
)
result = await mock_config_entry.start_reauth_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth"