mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Clean up Notion config flow (and tests) (#84007)
* Clean up Notion config flow (and tests) * Code review
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
"""Define tests for the Notion config flow."""
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from aionotion.errors import InvalidCredentialsError, NotionError
|
||||
import pytest
|
||||
@@ -10,6 +10,46 @@ from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
|
||||
|
||||
@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, client, config, errors, get_client_with_exception, setup_notion
|
||||
):
|
||||
"""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"] == "user@host.com"
|
||||
assert result["data"] == {
|
||||
CONF_USERNAME: "user@host.com",
|
||||
CONF_PASSWORD: "password123",
|
||||
}
|
||||
|
||||
|
||||
async def test_duplicate_error(hass, config, config_entry):
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -20,62 +60,42 @@ async def test_duplicate_error(hass, config, config_entry):
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"exc,error",
|
||||
"get_client_with_exception,errors",
|
||||
[
|
||||
(NotionError, "unknown"),
|
||||
(InvalidCredentialsError, "invalid_auth"),
|
||||
(AsyncMock(side_effect=Exception), {"base": "unknown"}),
|
||||
(AsyncMock(side_effect=InvalidCredentialsError), {"base": "invalid_auth"}),
|
||||
(AsyncMock(side_effect=NotionError), {"base": "unknown"}),
|
||||
],
|
||||
)
|
||||
async def test_erros(hass, config, error, exc):
|
||||
"""Test that exceptions show the correct error."""
|
||||
with patch(
|
||||
"homeassistant.components.notion.config_flow.async_get_client", side_effect=exc
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
)
|
||||
assert result["errors"] == {"base": error}
|
||||
|
||||
|
||||
async def test_step_reauth(hass, config, config_entry, setup_notion):
|
||||
"""Test that the reauth step works."""
|
||||
async def test_reauth(
|
||||
hass, config, config_entry, errors, get_client_with_exception, setup_notion
|
||||
):
|
||||
"""Test that re-auth works."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_REAUTH}, data=config
|
||||
DOMAIN,
|
||||
context={
|
||||
"source": SOURCE_REAUTH,
|
||||
"entry_id": config_entry.entry_id,
|
||||
"unique_id": config_entry.unique_id,
|
||||
},
|
||||
data=config,
|
||||
)
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
with patch("homeassistant.components.notion.async_setup_entry", return_value=True):
|
||||
# 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_configure(
|
||||
result["flow_id"], user_input={CONF_PASSWORD: "password"}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
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={CONF_PASSWORD: "password"}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
|
||||
|
||||
async def test_show_form(hass):
|
||||
"""Test that the form is served with no input."""
|
||||
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"
|
||||
|
||||
|
||||
async def test_step_user(hass, config, setup_notion):
|
||||
"""Test that the user step works."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "user@host.com"
|
||||
assert result["data"] == {
|
||||
CONF_USERNAME: "user@host.com",
|
||||
CONF_PASSWORD: "password123",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user