435 lines
12 KiB
Python
435 lines
12 KiB
Python
"""Test the pyLoad config flow."""
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
from pyloadapi.exceptions import CannotConnect, InvalidAuth, ParserError
|
|
import pytest
|
|
|
|
from homeassistant.components.pyload.const import DEFAULT_NAME, DOMAIN
|
|
from homeassistant.config_entries import SOURCE_HASSIO, SOURCE_IGNORE, SOURCE_USER
|
|
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME, CONF_VERIFY_SSL
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
from .conftest import (
|
|
ADDON_DISCOVERY_INFO,
|
|
ADDON_SERVICE_INFO,
|
|
NEW_INPUT,
|
|
REAUTH_INPUT,
|
|
USER_INPUT,
|
|
)
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
async def test_form(
|
|
hass: HomeAssistant,
|
|
mock_setup_entry: AsyncMock,
|
|
mock_pyloadapi: AsyncMock,
|
|
) -> None:
|
|
"""Test we get the form."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {}
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
USER_INPUT,
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == DEFAULT_NAME
|
|
assert result["data"] == USER_INPUT
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("exception", "expected_error"),
|
|
[
|
|
(InvalidAuth, "invalid_auth"),
|
|
(CannotConnect, "cannot_connect"),
|
|
(ParserError, "cannot_connect"),
|
|
(ValueError, "unknown"),
|
|
],
|
|
)
|
|
async def test_form_errors(
|
|
hass: HomeAssistant,
|
|
mock_setup_entry: AsyncMock,
|
|
mock_pyloadapi: AsyncMock,
|
|
exception: Exception,
|
|
expected_error: str,
|
|
) -> None:
|
|
"""Test we handle invalid auth."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
mock_pyloadapi.login.side_effect = exception
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
USER_INPUT,
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {"base": expected_error}
|
|
|
|
mock_pyloadapi.login.side_effect = None
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
USER_INPUT,
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == DEFAULT_NAME
|
|
assert result["data"] == USER_INPUT
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
async def test_flow_user_already_configured(
|
|
hass: HomeAssistant, config_entry: MockConfigEntry, mock_pyloadapi: AsyncMock
|
|
) -> None:
|
|
"""Test we abort user data set when entry is already configured."""
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {}
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input=USER_INPUT,
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
async def test_reauth(
|
|
hass: HomeAssistant,
|
|
config_entry: MockConfigEntry,
|
|
mock_pyloadapi: AsyncMock,
|
|
) -> None:
|
|
"""Test reauth flow."""
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
result = await config_entry.start_reauth_flow(hass)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
REAUTH_INPUT,
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "reauth_successful"
|
|
assert config_entry.data == NEW_INPUT
|
|
assert len(hass.config_entries.async_entries()) == 1
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("side_effect", "error_text"),
|
|
[
|
|
(InvalidAuth, "invalid_auth"),
|
|
(CannotConnect, "cannot_connect"),
|
|
(IndexError, "unknown"),
|
|
],
|
|
)
|
|
async def test_reauth_errors(
|
|
hass: HomeAssistant,
|
|
config_entry: MockConfigEntry,
|
|
mock_pyloadapi: AsyncMock,
|
|
side_effect: Exception,
|
|
error_text: str,
|
|
) -> None:
|
|
"""Test reauth flow."""
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
result = await config_entry.start_reauth_flow(hass)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
mock_pyloadapi.login.side_effect = side_effect
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
REAUTH_INPUT,
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {"base": error_text}
|
|
|
|
mock_pyloadapi.login.side_effect = None
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
REAUTH_INPUT,
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "reauth_successful"
|
|
assert config_entry.data == NEW_INPUT
|
|
assert len(hass.config_entries.async_entries()) == 1
|
|
|
|
|
|
async def test_reconfiguration(
|
|
hass: HomeAssistant,
|
|
config_entry: MockConfigEntry,
|
|
mock_pyloadapi: AsyncMock,
|
|
) -> None:
|
|
"""Test reconfiguration flow."""
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
result = await config_entry.start_reconfigure_flow(hass)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "reconfigure"
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
USER_INPUT,
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "reconfigure_successful"
|
|
assert config_entry.data == USER_INPUT
|
|
assert len(hass.config_entries.async_entries()) == 1
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("side_effect", "error_text"),
|
|
[
|
|
(InvalidAuth, "invalid_auth"),
|
|
(CannotConnect, "cannot_connect"),
|
|
(IndexError, "unknown"),
|
|
],
|
|
)
|
|
async def test_reconfigure_errors(
|
|
hass: HomeAssistant,
|
|
config_entry: MockConfigEntry,
|
|
mock_pyloadapi: AsyncMock,
|
|
side_effect: Exception,
|
|
error_text: str,
|
|
) -> None:
|
|
"""Test reconfiguration flow."""
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
result = await config_entry.start_reconfigure_flow(hass)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "reconfigure"
|
|
|
|
mock_pyloadapi.login.side_effect = side_effect
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
USER_INPUT,
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {"base": error_text}
|
|
|
|
mock_pyloadapi.login.side_effect = None
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
USER_INPUT,
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "reconfigure_successful"
|
|
assert config_entry.data == USER_INPUT
|
|
assert len(hass.config_entries.async_entries()) == 1
|
|
|
|
|
|
async def test_hassio_discovery(
|
|
hass: HomeAssistant,
|
|
mock_setup_entry: AsyncMock,
|
|
mock_pyloadapi: AsyncMock,
|
|
) -> None:
|
|
"""Test flow started from Supervisor discovery."""
|
|
|
|
mock_pyloadapi.login.side_effect = InvalidAuth
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
data=ADDON_SERVICE_INFO,
|
|
context={"source": SOURCE_HASSIO},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "hassio_confirm"
|
|
assert result["errors"] is None
|
|
|
|
mock_pyloadapi.login.side_effect = None
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], {CONF_USERNAME: "pyload", CONF_PASSWORD: "pyload"}
|
|
)
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "p539df76c_pyload-ng"
|
|
assert result["data"] == {**ADDON_DISCOVERY_INFO, CONF_VERIFY_SSL: False}
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_pyloadapi")
|
|
async def test_hassio_discovery_confirm_only(
|
|
hass: HomeAssistant,
|
|
mock_setup_entry: AsyncMock,
|
|
) -> None:
|
|
"""Test flow started from Supervisor discovery. Abort with confirm only."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
data=ADDON_SERVICE_INFO,
|
|
context={"source": SOURCE_HASSIO},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "hassio_confirm"
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "p539df76c_pyload-ng"
|
|
assert result["data"] == {**ADDON_DISCOVERY_INFO, CONF_VERIFY_SSL: False}
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("side_effect", "error_text"),
|
|
[
|
|
(InvalidAuth, "invalid_auth"),
|
|
(CannotConnect, "cannot_connect"),
|
|
(IndexError, "unknown"),
|
|
],
|
|
)
|
|
async def test_hassio_discovery_errors(
|
|
hass: HomeAssistant,
|
|
mock_setup_entry: AsyncMock,
|
|
mock_pyloadapi: AsyncMock,
|
|
side_effect: Exception,
|
|
error_text: str,
|
|
) -> None:
|
|
"""Test flow started from Supervisor discovery."""
|
|
|
|
mock_pyloadapi.login.side_effect = side_effect
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
data=ADDON_SERVICE_INFO,
|
|
context={"source": SOURCE_HASSIO},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "hassio_confirm"
|
|
assert result["errors"] is None
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], {CONF_USERNAME: "pyload", CONF_PASSWORD: "pyload"}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {"base": error_text}
|
|
|
|
mock_pyloadapi.login.side_effect = None
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], {CONF_USERNAME: "pyload", CONF_PASSWORD: "pyload"}
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "p539df76c_pyload-ng"
|
|
assert result["data"] == {**ADDON_DISCOVERY_INFO, CONF_VERIFY_SSL: False}
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_pyloadapi")
|
|
async def test_hassio_discovery_already_configured(
|
|
hass: HomeAssistant,
|
|
) -> None:
|
|
"""Test we abort discovery flow if already configured."""
|
|
|
|
MockConfigEntry(
|
|
domain=DOMAIN,
|
|
data={
|
|
CONF_URL: "http://539df76c-pyload-ng:8000/",
|
|
CONF_USERNAME: "pyload",
|
|
CONF_PASSWORD: "pyload",
|
|
},
|
|
).add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
data=ADDON_SERVICE_INFO,
|
|
context={"source": SOURCE_HASSIO},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_pyloadapi")
|
|
async def test_hassio_discovery_data_update(
|
|
hass: HomeAssistant,
|
|
) -> None:
|
|
"""Test we abort discovery flow if already configured and we update entry from discovery data."""
|
|
|
|
entry = MockConfigEntry(
|
|
domain=DOMAIN,
|
|
data={
|
|
CONF_URL: "http://localhost:8000/",
|
|
CONF_USERNAME: "pyload",
|
|
CONF_PASSWORD: "pyload",
|
|
},
|
|
unique_id="1234",
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
data=ADDON_SERVICE_INFO,
|
|
context={"source": SOURCE_HASSIO},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert entry.data[CONF_URL] == "http://539df76c-pyload-ng:8000/"
|
|
|
|
|
|
@pytest.mark.usefixtures("mock_pyloadapi")
|
|
async def test_hassio_discovery_ignored(
|
|
hass: HomeAssistant,
|
|
) -> None:
|
|
"""Test we abort discovery flow if discovery was ignored."""
|
|
|
|
MockConfigEntry(
|
|
domain=DOMAIN,
|
|
source=SOURCE_IGNORE,
|
|
data={},
|
|
unique_id="1234",
|
|
).add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
data=ADDON_SERVICE_INFO,
|
|
context={"source": SOURCE_HASSIO},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "already_configured"
|