core/tests/components/notion/test_config_flow.py

122 lines
4.1 KiB
Python
Raw Normal View History

"""Define tests for the Notion config flow."""
from unittest.mock import AsyncMock, patch
2021-01-01 21:31:56 +00:00
2021-09-24 18:23:19 +00:00
from aionotion.errors import InvalidCredentialsError, NotionError
import pytest
from homeassistant import data_entry_flow
2021-09-24 18:23:19 +00:00
from homeassistant.components.notion import DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from .conftest import TEST_PASSWORD, TEST_USERNAME
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
@pytest.mark.parametrize(
("get_client_with_exception", "errors"),
[
(AsyncMock(side_effect=Exception), {"base": "unknown"}),
(AsyncMock(side_effect=InvalidCredentialsError), {"base": "invalid_auth"}),
(AsyncMock(side_effect=NotionError), {"base": "unknown"}),
],
)
async def test_create_entry(
hass: HomeAssistant,
client,
config,
errors,
get_client_with_exception,
mock_aionotion,
) -> None:
"""Test creating an etry (including recovery from errors)."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "user"
# Test errors that can arise when getting a Notion API client:
with patch(
"homeassistant.components.notion.config_flow.async_get_client",
get_client_with_exception,
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] == errors
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=config
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["title"] == TEST_USERNAME
assert result["data"] == {
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
}
async def test_duplicate_error(hass: HomeAssistant, config, config_entry) -> None:
"""Test that errors are shown when duplicates are added."""
result = await hass.config_entries.flow.async_init(
2022-01-23 09:18:54 +00:00
DOMAIN, context={"source": SOURCE_USER}, data=config
)
assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "already_configured"
2022-01-23 09:18:54 +00:00
@pytest.mark.parametrize(
("get_client_with_exception", "errors"),
2022-01-23 09:18:54 +00:00
[
(AsyncMock(side_effect=Exception), {"base": "unknown"}),
(AsyncMock(side_effect=InvalidCredentialsError), {"base": "invalid_auth"}),
(AsyncMock(side_effect=NotionError), {"base": "unknown"}),
2022-01-23 09:18:54 +00:00
],
)
async def test_reauth(
hass: HomeAssistant,
config,
config_entry,
errors,
get_client_with_exception,
mock_aionotion,
) -> None:
"""Test that re-auth works."""
2021-09-24 18:23:19 +00:00
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": SOURCE_REAUTH,
"entry_id": config_entry.entry_id,
"unique_id": config_entry.unique_id,
},
data=config,
2021-09-24 18:23:19 +00:00
)
assert result["step_id"] == "reauth_confirm"
# Test errors that can arise when getting a Notion API client:
with patch(
"homeassistant.components.notion.config_flow.async_get_client",
get_client_with_exception,
):
2021-09-24 18:23:19 +00:00
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_PASSWORD: "password"}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] == errors
2021-09-24 18:23:19 +00:00
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_PASSWORD: "password"}
)
# Block to ensure the setup_config_entry fixture does not
# get undone before hass is shutdown so we do not try
# to setup the config entry via reload.
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.FlowResultType.ABORT
2021-09-24 18:23:19 +00:00
assert result["reason"] == "reauth_successful"
assert len(hass.config_entries.async_entries()) == 1