2020-06-04 16:07:27 +00:00
|
|
|
"""Define tests for the Tile config flow."""
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2020-06-04 16:07:27 +00:00
|
|
|
from pytile.errors import TileError
|
|
|
|
|
|
|
|
from homeassistant import data_entry_flow
|
|
|
|
from homeassistant.components.tile import DOMAIN
|
2020-11-13 23:04:34 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
|
2020-06-04 16:07:27 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
|
|
|
|
|
|
async def test_duplicate_error(hass):
|
|
|
|
"""Test that errors are shown when duplicates are added."""
|
2020-11-13 23:04:34 +00:00
|
|
|
conf = {
|
|
|
|
CONF_USERNAME: "user@host.com",
|
|
|
|
CONF_PASSWORD: "123abc",
|
|
|
|
}
|
2020-06-04 16:07:27 +00:00
|
|
|
|
|
|
|
MockConfigEntry(domain=DOMAIN, unique_id="user@host.com", data=conf).add_to_hass(
|
|
|
|
hass
|
|
|
|
)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_invalid_credentials(hass):
|
|
|
|
"""Test that invalid credentials key throws an error."""
|
2020-11-13 23:04:34 +00:00
|
|
|
conf = {
|
|
|
|
CONF_USERNAME: "user@host.com",
|
|
|
|
CONF_PASSWORD: "123abc",
|
|
|
|
}
|
2020-06-04 16:07:27 +00:00
|
|
|
|
|
|
|
with patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"homeassistant.components.tile.config_flow.async_login",
|
|
|
|
side_effect=TileError,
|
2020-06-04 16:07:27 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2020-10-06 12:44:14 +00:00
|
|
|
assert result["errors"] == {"base": "invalid_auth"}
|
2020-06-04 16:07:27 +00:00
|
|
|
|
|
|
|
|
2020-11-13 23:04:34 +00:00
|
|
|
async def test_step_import(hass):
|
|
|
|
"""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(
|
|
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["title"] == "user@host.com"
|
|
|
|
assert result["data"] == {
|
|
|
|
CONF_USERNAME: "user@host.com",
|
|
|
|
CONF_PASSWORD: "123abc",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-04 16:07:27 +00:00
|
|
|
async def test_step_user(hass):
|
|
|
|
"""Test that the user step works."""
|
2020-11-13 23:04:34 +00:00
|
|
|
conf = {
|
|
|
|
CONF_USERNAME: "user@host.com",
|
|
|
|
CONF_PASSWORD: "123abc",
|
|
|
|
}
|
2020-06-04 16:07:27 +00:00
|
|
|
|
|
|
|
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(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["title"] == "user@host.com"
|
|
|
|
assert result["data"] == {
|
|
|
|
CONF_USERNAME: "user@host.com",
|
|
|
|
CONF_PASSWORD: "123abc",
|
|
|
|
}
|