2020-03-11 21:15:59 +00:00
|
|
|
"""Tests for the Freebox config flow."""
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
|
2021-02-02 21:57:06 +00:00
|
|
|
from freebox_api.exceptions import (
|
2020-03-11 21:15:59 +00:00
|
|
|
AuthorizationError,
|
|
|
|
HttpRequestError,
|
|
|
|
InvalidTokenError,
|
|
|
|
)
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant import data_entry_flow
|
|
|
|
from homeassistant.components.freebox.const import DOMAIN
|
2021-02-25 15:50:58 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER, SOURCE_ZEROCONF
|
2020-03-11 21:15:59 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
|
|
HOST = "myrouter.freeboxos.fr"
|
|
|
|
PORT = 1234
|
|
|
|
|
2021-02-25 15:50:58 +00:00
|
|
|
MOCK_ZEROCONF_DATA = {
|
|
|
|
"host": "192.168.0.254",
|
|
|
|
"port": 80,
|
|
|
|
"hostname": "Freebox-Server.local.",
|
|
|
|
"type": "_fbx-api._tcp.local.",
|
|
|
|
"name": "Freebox Server._fbx-api._tcp.local.",
|
|
|
|
"properties": {
|
|
|
|
"api_version": "8.0",
|
|
|
|
"device_type": "FreeboxServer1,2",
|
|
|
|
"api_base_url": "/api/",
|
|
|
|
"uid": "b15ab20debb399f95001a9ca207d2777",
|
|
|
|
"https_available": "1",
|
|
|
|
"https_port": f"{PORT}",
|
|
|
|
"box_model": "fbxgw-r2/full",
|
|
|
|
"box_model_name": "Freebox Server (r2)",
|
|
|
|
"api_domain": HOST,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-03-11 21:15:59 +00:00
|
|
|
|
|
|
|
@pytest.fixture(name="connect")
|
|
|
|
def mock_controller_connect():
|
|
|
|
"""Mock a successful connection."""
|
|
|
|
with patch("homeassistant.components.freebox.router.Freepybox") as service_mock:
|
2020-04-30 20:29:50 +00:00
|
|
|
service_mock.return_value.open = AsyncMock()
|
|
|
|
service_mock.return_value.system.get_config = AsyncMock(
|
2020-04-25 21:32:55 +00:00
|
|
|
return_value={
|
|
|
|
"mac": "abcd",
|
|
|
|
"model_info": {"pretty_name": "Pretty Model"},
|
|
|
|
"firmware_version": "123",
|
|
|
|
}
|
|
|
|
)
|
2020-04-30 20:29:50 +00:00
|
|
|
service_mock.return_value.lan.get_hosts_list = AsyncMock()
|
|
|
|
service_mock.return_value.connection.get_status = AsyncMock()
|
|
|
|
service_mock.return_value.close = AsyncMock()
|
2020-03-11 21:15:59 +00:00
|
|
|
yield service_mock
|
|
|
|
|
|
|
|
|
|
|
|
async def test_user(hass):
|
|
|
|
"""Test user config."""
|
|
|
|
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"
|
|
|
|
|
|
|
|
# test with all provided
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_USER},
|
|
|
|
data={CONF_HOST: HOST, CONF_PORT: PORT},
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "link"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_import(hass):
|
|
|
|
"""Test import step."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data={CONF_HOST: HOST, CONF_PORT: PORT},
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "link"
|
|
|
|
|
|
|
|
|
2021-02-25 15:50:58 +00:00
|
|
|
async def test_zeroconf(hass):
|
|
|
|
"""Test zeroconf step."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_ZEROCONF},
|
|
|
|
data=MOCK_ZEROCONF_DATA,
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "link"
|
|
|
|
|
|
|
|
|
2020-03-11 21:15:59 +00:00
|
|
|
async def test_link(hass, connect):
|
|
|
|
"""Test linking."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_USER},
|
|
|
|
data={CONF_HOST: HOST, CONF_PORT: PORT},
|
|
|
|
)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["result"].unique_id == HOST
|
|
|
|
assert result["title"] == HOST
|
|
|
|
assert result["data"][CONF_HOST] == HOST
|
|
|
|
assert result["data"][CONF_PORT] == PORT
|
|
|
|
|
|
|
|
|
|
|
|
async def test_abort_if_already_setup(hass):
|
|
|
|
"""Test we abort if component is already setup."""
|
|
|
|
MockConfigEntry(
|
|
|
|
domain=DOMAIN, data={CONF_HOST: HOST, CONF_PORT: PORT}, unique_id=HOST
|
|
|
|
).add_to_hass(hass)
|
|
|
|
|
|
|
|
# Should fail, same HOST (import)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data={CONF_HOST: HOST, CONF_PORT: PORT},
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
# Should fail, same HOST (flow)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_USER},
|
|
|
|
data={CONF_HOST: HOST, CONF_PORT: PORT},
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_on_link_failed(hass):
|
|
|
|
"""Test when we have errors during linking the router."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_USER},
|
|
|
|
data={CONF_HOST: HOST, CONF_PORT: PORT},
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.freebox.router.Freepybox.open",
|
|
|
|
side_effect=AuthorizationError(),
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["errors"] == {"base": "register_failed"}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.freebox.router.Freepybox.open",
|
|
|
|
side_effect=HttpRequestError(),
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2020-10-01 09:31:43 +00:00
|
|
|
assert result["errors"] == {"base": "cannot_connect"}
|
2020-03-11 21:15:59 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.freebox.router.Freepybox.open",
|
|
|
|
side_effect=InvalidTokenError(),
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["errors"] == {"base": "unknown"}
|