diff --git a/tests/components/notion/conftest.py b/tests/components/notion/conftest.py new file mode 100644 index 00000000000..18ff4d0264e --- /dev/null +++ b/tests/components/notion/conftest.py @@ -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" diff --git a/tests/components/notion/test_config_flow.py b/tests/components/notion/test_config_flow.py index bda70bd6af9..45bcedde155 100644 --- a/tests/components/notion/test_config_flow.py +++ b/tests/components/notion/test_config_flow.py @@ -1,5 +1,5 @@ """Define tests for the Notion config flow.""" -from unittest.mock import AsyncMock, patch +from unittest.mock import patch from aionotion.errors import InvalidCredentialsError, NotionError import pytest @@ -9,79 +9,38 @@ from homeassistant.components.notion import DOMAIN from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER from homeassistant.const import CONF_PASSWORD, CONF_USERNAME -from tests.common import MockConfigEntry - -@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): +async def test_duplicate_error(hass, config, config_entry): """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( - 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["reason"] == "already_configured" -@pytest.mark.parametrize("client", [AsyncMock(side_effect=NotionError)]) -async def test_generic_notion_error(client_login, hass): - """Test that a generic aionotion error is handled correctly.""" - conf = {CONF_USERNAME: "user@host.com", CONF_PASSWORD: "password123"} - - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER}, data=conf - ) - - assert result["errors"] == {"base": "unknown"} +@pytest.mark.parametrize( + "exc,error", + [ + (NotionError, "unknown"), + (InvalidCredentialsError, "invalid_auth"), + ], +) +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} -@pytest.mark.parametrize("client", [AsyncMock(side_effect=InvalidCredentialsError)]) -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): +async def test_step_reauth(hass, config, config_entry, setup_notion): """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( - DOMAIN, - context={"source": SOURCE_REAUTH}, - data={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"}, + DOMAIN, context={"source": SOURCE_REAUTH}, data=config ) 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["step_id"] == "reauth_confirm" - with patch( - "homeassistant.components.notion.async_setup_entry", return_value=True - ), patch("homeassistant.config_entries.ConfigEntries.async_reload"): + with patch("homeassistant.components.notion.async_setup_entry", return_value=True): result = await hass.config_entries.flow.async_configure( 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 -async def test_show_form(client_login, hass): +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} ) - await hass.async_block_till_done() - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM 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.""" - conf = {CONF_USERNAME: "user@host.com", CONF_PASSWORD: "password123"} - - 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() - + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER}, data=config + ) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["title"] == "user@host.com" assert result["data"] == {