Add config flow tests for OwnTracks (#28644)

* Add config flow tests for OwnTracks

* Fix pylint

* Woops, uncomment test

* Woops again, logs removed

* Review from @MartinHjelmare + fix pylint
This commit is contained in:
Quentame authored and Martin Hjelmare committed 2019-11-11 21:30:00 +01:00
1 parent cfcacc2827
commit decab3e15b
5 files changed
+134 -26

No files matched your search

+119 -9
View File
@@ -1,25 +1,135 @@
"""Tests for OwnTracks config flow."""
from unittest.mock import patch
from unittest.mock import Mock, patch
import pytest
from homeassistant import data_entry_flow
from homeassistant.const import CONF_WEBHOOK_ID
from homeassistant.components.owntracks import config_flow
from homeassistant.components.owntracks.config_flow import CONF_CLOUDHOOK, CONF_SECRET
from homeassistant.components.owntracks.const import DOMAIN
from homeassistant.setup import async_setup_component
from tests.common import mock_coro
from tests.common import mock_coro, MockConfigEntry
async def test_config_flow_import(hass):
CONF_WEBHOOK_URL = "webhook_url"
BASE_URL = "http://example.com"
CLOUDHOOK = False
SECRET = "secret"
WEBHOOK_ID = "webhook_id"
WEBHOOK_URL = f"{BASE_URL}/api/webhook/webhook_id"
@pytest.fixture(name="webhook_id")
def mock_webhook_id():
"""Mock webhook_id."""
with patch(
"homeassistant.components.webhook.async_generate_id", return_value=WEBHOOK_ID
):
yield
@pytest.fixture(name="secret")
def mock_secret():
"""Mock secret."""
with patch("binascii.hexlify", return_value=str.encode(SECRET)):
yield
@pytest.fixture(name="not_supports_encryption")
def mock_not_supports_encryption():
"""Mock non successful nacl import."""
with patch(
"homeassistant.components.owntracks.config_flow.supports_encryption",
return_value=False,
):
yield
def init_config_flow(hass):
"""Init a configuration flow."""
hass.config.api = Mock(base_url=BASE_URL)
flow = config_flow.OwnTracksFlow()
flow.hass = hass
return flow
async def test_user(hass, webhook_id, secret):
"""Test user step."""
flow = init_config_flow(hass)
result = await flow.async_step_user()
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
result = await flow.async_step_user({})
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "OwnTracks"
assert result["data"][CONF_WEBHOOK_ID] == WEBHOOK_ID
assert result["data"][CONF_SECRET] == SECRET
assert result["data"][CONF_CLOUDHOOK] == CLOUDHOOK
assert result["description_placeholders"][CONF_WEBHOOK_URL] == WEBHOOK_URL
async def test_import(hass, webhook_id, secret):
"""Test import step."""
flow = init_config_flow(hass)
result = await flow.async_step_import({})
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "OwnTracks"
assert result["data"][CONF_WEBHOOK_ID] == WEBHOOK_ID
assert result["data"][CONF_SECRET] == SECRET
assert result["data"][CONF_CLOUDHOOK] == CLOUDHOOK
assert result["description_placeholders"] is None
async def test_import_setup(hass):
"""Test that we automatically create a config flow."""
assert not hass.config_entries.async_entries("owntracks")
assert await async_setup_component(hass, "owntracks", {"owntracks": {}})
assert not hass.config_entries.async_entries(DOMAIN)
assert await async_setup_component(hass, DOMAIN, {"owntracks": {}})
await hass.async_block_till_done()
assert hass.config_entries.async_entries("owntracks")
assert hass.config_entries.async_entries(DOMAIN)
async def test_config_flow_unload(hass):
async def test_abort_if_already_setup(hass):
"""Test that we can't add more than one instance."""
flow = init_config_flow(hass)
MockConfigEntry(domain=DOMAIN, data={}).add_to_hass(hass)
assert hass.config_entries.async_entries(DOMAIN)
# Should fail, already setup (import)
result = await flow.async_step_import({})
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "one_instance_allowed"
# Should fail, already setup (flow)
result = await flow.async_step_user({})
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "one_instance_allowed"
async def test_user_not_supports_encryption(hass, not_supports_encryption):
"""Test user step."""
flow = init_config_flow(hass)
result = await flow.async_step_user({})
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert (
result["description_placeholders"]["secret"]
== "Encryption is not supported because nacl is not installed."
)
async def test_unload(hass):
"""Test unloading a config flow."""
with patch(
"homeassistant.config_entries.ConfigEntries" ".async_forward_entry_setup"
) as mock_forward:
result = await hass.config_entries.flow.async_init(
"owntracks", context={"source": "import"}, data={}
DOMAIN, context={"source": "import"}, data={}
)
assert len(mock_forward.mock_calls) == 1
@@ -51,7 +161,7 @@ async def test_with_cloud_sub(hass):
return_value=mock_coro("https://hooks.nabu.casa/ABCD"),
):
result = await hass.config_entries.flow.async_init(
"owntracks", context={"source": "user"}, data={}
DOMAIN, context={"source": "user"}, data={}
)
entry = result["result"]