Clean up Notion config tests (#64669)
This commit is contained in:
parent
a56bfe012f
commit
c3ccc76eb1
2 changed files with 71 additions and 77 deletions
44
tests/components/notion/conftest.py
Normal file
44
tests/components/notion/conftest.py
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
"""Define fixtures for Notion tests."""
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant.components.notion import DOMAIN
|
||||||
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="config_entry")
|
||||||
|
def config_entry_fixture(hass, config, unique_id):
|
||||||
|
"""Define a config entry fixture."""
|
||||||
|
entry = MockConfigEntry(domain=DOMAIN, unique_id=unique_id, data=config)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
return entry
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="config")
|
||||||
|
def config_fixture(hass):
|
||||||
|
"""Define a config entry data fixture."""
|
||||||
|
return {
|
||||||
|
CONF_USERNAME: "user@host.com",
|
||||||
|
CONF_PASSWORD: "password123",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="setup_notion")
|
||||||
|
async def setup_notion_fixture(hass, config):
|
||||||
|
"""Define a fixture to set up Notion."""
|
||||||
|
with patch("homeassistant.components.notion.async_get_client"), patch(
|
||||||
|
"homeassistant.components.notion.config_flow.async_get_client"
|
||||||
|
), patch("homeassistant.components.notion.PLATFORMS", []):
|
||||||
|
assert await async_setup_component(hass, DOMAIN, config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="unique_id")
|
||||||
|
def unique_id_fixture(hass):
|
||||||
|
"""Define a config entry unique ID fixture."""
|
||||||
|
return "user@host.com"
|
|
@ -1,5 +1,5 @@
|
||||||
"""Define tests for the Notion config flow."""
|
"""Define tests for the Notion config flow."""
|
||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from aionotion.errors import InvalidCredentialsError, NotionError
|
from aionotion.errors import InvalidCredentialsError, NotionError
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -9,79 +9,38 @@ from homeassistant.components.notion import DOMAIN
|
||||||
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
|
||||||
|
|
||||||
|
async def test_duplicate_error(hass, config, config_entry):
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def client_fixture():
|
|
||||||
"""Define a fixture for an aionotion client."""
|
|
||||||
return AsyncMock(return_value=None)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client_login")
|
|
||||||
def client_login_fixture(client):
|
|
||||||
"""Define a fixture for patching the aiowatttime coroutine to get a client."""
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.notion.config_flow.async_get_client"
|
|
||||||
) as mock_client:
|
|
||||||
mock_client.side_effect = client
|
|
||||||
yield mock_client
|
|
||||||
|
|
||||||
|
|
||||||
async def test_duplicate_error(hass):
|
|
||||||
"""Test that errors are shown when duplicates are added."""
|
"""Test that errors are shown when duplicates are added."""
|
||||||
conf = {CONF_USERNAME: "user@host.com", CONF_PASSWORD: "password123"}
|
|
||||||
|
|
||||||
MockConfigEntry(domain=DOMAIN, unique_id="user@host.com", data=conf).add_to_hass(
|
|
||||||
hass
|
|
||||||
)
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("client", [AsyncMock(side_effect=NotionError)])
|
@pytest.mark.parametrize(
|
||||||
async def test_generic_notion_error(client_login, hass):
|
"exc,error",
|
||||||
"""Test that a generic aionotion error is handled correctly."""
|
[
|
||||||
conf = {CONF_USERNAME: "user@host.com", CONF_PASSWORD: "password123"}
|
(NotionError, "unknown"),
|
||||||
|
(InvalidCredentialsError, "invalid_auth"),
|
||||||
result = await hass.config_entries.flow.async_init(
|
],
|
||||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
)
|
||||||
)
|
async def test_erros(hass, config, error, exc):
|
||||||
|
"""Test that exceptions show the correct error."""
|
||||||
assert result["errors"] == {"base": "unknown"}
|
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}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("client", [AsyncMock(side_effect=InvalidCredentialsError)])
|
async def test_step_reauth(hass, config, config_entry, setup_notion):
|
||||||
async def test_invalid_credentials(client_login, hass):
|
|
||||||
"""Test that invalid credentials throw an error."""
|
|
||||||
conf = {CONF_USERNAME: "user@host.com", CONF_PASSWORD: "password123"}
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["errors"] == {"base": "invalid_auth"}
|
|
||||||
|
|
||||||
|
|
||||||
async def test_step_reauth(client_login, hass):
|
|
||||||
"""Test that the reauth step works."""
|
"""Test that the reauth step works."""
|
||||||
MockConfigEntry(
|
|
||||||
domain=DOMAIN,
|
|
||||||
unique_id="user@email.com",
|
|
||||||
data={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"},
|
|
||||||
).add_to_hass(hass)
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN, context={"source": SOURCE_REAUTH}, data=config
|
||||||
context={"source": SOURCE_REAUTH},
|
|
||||||
data={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"},
|
|
||||||
)
|
)
|
||||||
assert result["step_id"] == "reauth_confirm"
|
assert result["step_id"] == "reauth_confirm"
|
||||||
|
|
||||||
|
@ -89,9 +48,7 @@ async def test_step_reauth(client_login, hass):
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["step_id"] == "reauth_confirm"
|
assert result["step_id"] == "reauth_confirm"
|
||||||
|
|
||||||
with patch(
|
with patch("homeassistant.components.notion.async_setup_entry", return_value=True):
|
||||||
"homeassistant.components.notion.async_setup_entry", return_value=True
|
|
||||||
), patch("homeassistant.config_entries.ConfigEntries.async_reload"):
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], user_input={CONF_PASSWORD: "password"}
|
result["flow_id"], user_input={CONF_PASSWORD: "password"}
|
||||||
)
|
)
|
||||||
|
@ -102,27 +59,20 @@ async def test_step_reauth(client_login, hass):
|
||||||
assert len(hass.config_entries.async_entries()) == 1
|
assert len(hass.config_entries.async_entries()) == 1
|
||||||
|
|
||||||
|
|
||||||
async def test_show_form(client_login, hass):
|
async def test_show_form(hass):
|
||||||
"""Test that the form is served with no input."""
|
"""Test that the form is served with no input."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_USER}
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
|
|
||||||
|
|
||||||
async def test_step_user(client_login, hass):
|
async def test_step_user(hass, config, setup_notion):
|
||||||
"""Test that the user step works."""
|
"""Test that the user step works."""
|
||||||
conf = {CONF_USERNAME: "user@host.com", CONF_PASSWORD: "password123"}
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||||
with patch("homeassistant.components.notion.async_setup_entry", return_value=True):
|
)
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
assert result["title"] == "user@host.com"
|
assert result["title"] == "user@host.com"
|
||||||
assert result["data"] == {
|
assert result["data"] == {
|
||||||
|
|
Loading…
Add table
Reference in a new issue