2019-07-09 08:29:06 +00:00
|
|
|
"""Define tests for the Notion config flow."""
|
2022-12-19 22:03:58 +00:00
|
|
|
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
|
2019-07-09 08:29:06 +00:00
|
|
|
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
|
2019-07-09 08:29:06 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
2023-02-15 09:50:02 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2022-12-31 14:34:59 +00:00
|
|
|
from .conftest import TEST_PASSWORD, TEST_USERNAME
|
|
|
|
|
2023-03-01 08:41:55 +00:00
|
|
|
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
|
|
|
|
2019-07-09 08:29:06 +00:00
|
|
|
|
2022-12-19 22:03:58 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
("get_client_with_exception", "errors"),
|
2022-12-19 22:03:58 +00:00
|
|
|
[
|
|
|
|
(AsyncMock(side_effect=Exception), {"base": "unknown"}),
|
|
|
|
(AsyncMock(side_effect=InvalidCredentialsError), {"base": "invalid_auth"}),
|
|
|
|
(AsyncMock(side_effect=NotionError), {"base": "unknown"}),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_create_entry(
|
2023-02-15 09:50:02 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
client,
|
|
|
|
config,
|
|
|
|
errors,
|
|
|
|
get_client_with_exception,
|
|
|
|
mock_aionotion,
|
|
|
|
) -> None:
|
2022-12-19 22:03:58 +00:00
|
|
|
"""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
|
2022-12-31 14:34:59 +00:00
|
|
|
assert result["title"] == TEST_USERNAME
|
2022-12-19 22:03:58 +00:00
|
|
|
assert result["data"] == {
|
2022-12-31 14:34:59 +00:00
|
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
2022-12-19 22:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-01 08:41:55 +00:00
|
|
|
async def test_duplicate_error(hass: HomeAssistant, config, config_entry) -> None:
|
2019-07-09 08:29:06 +00:00
|
|
|
"""Test that errors are shown when duplicates are added."""
|
2020-02-25 05:36:58 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2022-01-23 09:18:54 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}, data=config
|
2020-02-25 05:36:58 +00:00
|
|
|
)
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
2020-02-25 05:36:58 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
2019-07-09 08:29:06 +00:00
|
|
|
|
|
|
|
|
2022-01-23 09:18:54 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
("get_client_with_exception", "errors"),
|
2022-01-23 09:18:54 +00:00
|
|
|
[
|
2022-12-19 22:03:58 +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
|
|
|
],
|
|
|
|
)
|
2022-12-19 22:03:58 +00:00
|
|
|
async def test_reauth(
|
2023-02-15 09:50:02 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config,
|
|
|
|
config_entry,
|
|
|
|
errors,
|
|
|
|
get_client_with_exception,
|
2023-03-01 08:41:55 +00:00
|
|
|
mock_aionotion,
|
2023-02-15 09:50:02 +00:00
|
|
|
) -> None:
|
2022-12-19 22:03:58 +00:00
|
|
|
"""Test that re-auth works."""
|
2021-09-24 18:23:19 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2022-12-19 22:03:58 +00:00
|
|
|
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"
|
|
|
|
|
2022-12-19 22:03:58 +00:00
|
|
|
# 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"}
|
|
|
|
)
|
2022-12-19 22:03:58 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
|
|
assert result["errors"] == errors
|
2021-09-24 18:23:19 +00:00
|
|
|
|
2022-12-19 22:03:58 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_PASSWORD: "password"}
|
|
|
|
)
|
2023-03-01 08:07:46 +00:00
|
|
|
# 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()
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
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
|