mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Clean up Tile config flow tests (#64866)
This commit is contained in:
parent
0ca38c0928
commit
c3ecf426d0
70
tests/components/tile/conftest.py
Normal file
70
tests/components/tile/conftest.py
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
"""Define test fixtures for Tile."""
|
||||||
|
from datetime import datetime
|
||||||
|
from unittest.mock import AsyncMock, Mock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant.components.tile.const import DOMAIN
|
||||||
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
TILE_UUID = "tile_123"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="api")
|
||||||
|
def api_fixture(hass):
|
||||||
|
"""Define a pytile API object."""
|
||||||
|
tile = Mock(
|
||||||
|
accuracy=20,
|
||||||
|
altitude=1000,
|
||||||
|
dead=False,
|
||||||
|
latitude=51.528308,
|
||||||
|
longitude=-0.3817765,
|
||||||
|
lost=False,
|
||||||
|
lost_timestamp=datetime(2022, 1, 1, 0, 0, 0),
|
||||||
|
ring_state="STOPPED",
|
||||||
|
uuid=TILE_UUID,
|
||||||
|
voip_state="OFFLINE",
|
||||||
|
async_update=AsyncMock(),
|
||||||
|
)
|
||||||
|
tile.name = "Tile 123"
|
||||||
|
|
||||||
|
return Mock(async_get_tiles=AsyncMock(return_value={TILE_UUID: tile}))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="config_entry")
|
||||||
|
def config_entry_fixture(hass, config, unique_id):
|
||||||
|
"""Define a config entry fixture."""
|
||||||
|
entry = MockConfigEntry(domain=DOMAIN, unique_id=unique_id, data=config)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
return entry
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="config")
|
||||||
|
def config_fixture(hass):
|
||||||
|
"""Define a config entry data fixture."""
|
||||||
|
return {
|
||||||
|
CONF_USERNAME: "user@host.com",
|
||||||
|
CONF_PASSWORD: "123abc",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="setup_tile")
|
||||||
|
async def setup_tile_fixture(hass, api, config):
|
||||||
|
"""Define a fixture to set up Tile."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.tile.config_flow.async_login", return_value=api
|
||||||
|
), patch("homeassistant.components.tile.async_login", return_value=api), patch(
|
||||||
|
"homeassistant.components.tile.PLATFORMS", []
|
||||||
|
):
|
||||||
|
assert await async_setup_component(hass, DOMAIN, config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="unique_id")
|
||||||
|
def unique_id_fixture(hass):
|
||||||
|
"""Define a config entry unique ID fixture."""
|
||||||
|
return "user@host.com"
|
@ -9,24 +9,12 @@ from homeassistant.components.tile import DOMAIN
|
|||||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_REAUTH, SOURCE_USER
|
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_REAUTH, SOURCE_USER
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
|
||||||
|
|
||||||
|
async def test_duplicate_error(hass, config, config_entry):
|
||||||
async def test_duplicate_error(hass):
|
|
||||||
"""Test that errors are shown when duplicates are added."""
|
"""Test that errors are shown when duplicates are added."""
|
||||||
conf = {
|
|
||||||
CONF_USERNAME: "user@host.com",
|
|
||||||
CONF_PASSWORD: "123abc",
|
|
||||||
}
|
|
||||||
|
|
||||||
MockConfigEntry(domain=DOMAIN, unique_id="user@host.com", data=conf).add_to_hass(
|
|
||||||
hass
|
|
||||||
)
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
@ -38,36 +26,23 @@ async def test_duplicate_error(hass):
|
|||||||
(TileError, "unknown"),
|
(TileError, "unknown"),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_errors(hass, err, err_string):
|
async def test_errors(hass, config, err, err_string):
|
||||||
"""Test that errors are handled correctly."""
|
"""Test that errors are handled correctly."""
|
||||||
conf = {
|
|
||||||
CONF_USERNAME: "user@host.com",
|
|
||||||
CONF_PASSWORD: "123abc",
|
|
||||||
}
|
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.tile.config_flow.async_login",
|
"homeassistant.components.tile.config_flow.async_login",
|
||||||
side_effect=err,
|
side_effect=err,
|
||||||
):
|
):
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["errors"] == {"base": err_string}
|
assert result["errors"] == {"base": err_string}
|
||||||
|
|
||||||
|
|
||||||
async def test_step_import(hass):
|
async def test_step_import(hass, config, setup_tile):
|
||||||
"""Test that the import step works."""
|
"""Test that the import step works."""
|
||||||
conf = {
|
|
||||||
CONF_USERNAME: "user@host.com",
|
|
||||||
CONF_PASSWORD: "123abc",
|
|
||||||
}
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.tile.async_setup_entry", return_value=True
|
|
||||||
), patch("homeassistant.components.tile.config_flow.async_login"):
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
assert result["title"] == "user@host.com"
|
assert result["title"] == "user@host.com"
|
||||||
@ -77,21 +52,10 @@ async def test_step_import(hass):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_step_reauth(hass):
|
async def test_step_reauth(hass, config, config_entry, setup_tile):
|
||||||
"""Test that the reauth step works."""
|
"""Test that the reauth step works."""
|
||||||
conf = {
|
|
||||||
CONF_USERNAME: "user@host.com",
|
|
||||||
CONF_PASSWORD: "123abc",
|
|
||||||
}
|
|
||||||
|
|
||||||
MockConfigEntry(domain=DOMAIN, unique_id="user@host.com", data=conf).add_to_hass(
|
|
||||||
hass
|
|
||||||
)
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN, context={"source": SOURCE_REAUTH}, data=config
|
||||||
context={"source": SOURCE_REAUTH},
|
|
||||||
data={CONF_USERNAME: "user@host.com", CONF_PASSWORD: "password"},
|
|
||||||
)
|
)
|
||||||
assert result["step_id"] == "reauth_confirm"
|
assert result["step_id"] == "reauth_confirm"
|
||||||
|
|
||||||
@ -99,29 +63,16 @@ async def test_step_reauth(hass):
|
|||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["step_id"] == "reauth_confirm"
|
assert result["step_id"] == "reauth_confirm"
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.tile.async_setup_entry", return_value=True
|
|
||||||
), patch("homeassistant.components.tile.config_flow.async_login"):
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], user_input={CONF_PASSWORD: "password"}
|
result["flow_id"], user_input={CONF_PASSWORD: "password"}
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
assert result["type"] == data_entry_flow.RESULT_TYPE_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
|
||||||
|
|
||||||
|
|
||||||
async def test_step_user(hass):
|
async def test_step_user(hass, config, setup_tile):
|
||||||
"""Test that the user step works."""
|
"""Test that the user step works."""
|
||||||
conf = {
|
|
||||||
CONF_USERNAME: "user@host.com",
|
|
||||||
CONF_PASSWORD: "123abc",
|
|
||||||
}
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.tile.async_setup_entry", return_value=True
|
|
||||||
), patch("homeassistant.components.tile.config_flow.async_login"):
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_USER}
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
)
|
)
|
||||||
@ -129,7 +80,7 @@ async def test_step_user(hass):
|
|||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
assert result["title"] == "user@host.com"
|
assert result["title"] == "user@host.com"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user