From e7cb3f1979fc09d29ed693b98186418cf465d7aa Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Sat, 31 Dec 2022 07:34:59 -0700 Subject: [PATCH] Renovate Notion config flow tests (#84906) --- tests/components/notion/conftest.py | 48 +++++++++++++-------- tests/components/notion/test_config_flow.py | 14 +++--- tests/components/notion/test_diagnostics.py | 2 +- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/tests/components/notion/conftest.py b/tests/components/notion/conftest.py index e29ea83ef2a..3250beffcf5 100644 --- a/tests/components/notion/conftest.py +++ b/tests/components/notion/conftest.py @@ -1,40 +1,42 @@ """Define fixtures for Notion tests.""" import json -from unittest.mock import AsyncMock, patch +from unittest.mock import AsyncMock, Mock, 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, load_fixture +TEST_USERNAME = "user@host.com" +TEST_PASSWORD = "password123" + @pytest.fixture(name="client") def client_fixture(data_bridge, data_sensor, data_task): """Define a fixture for an aionotion client.""" - client = AsyncMock() - client.bridge.async_all.return_value = data_bridge - client.sensor.async_all.return_value = data_sensor - client.task.async_all.return_value = data_task - return client + return Mock( + bridge=Mock(async_all=AsyncMock(return_value=data_bridge)), + sensor=Mock(async_all=AsyncMock(return_value=data_sensor)), + task=Mock(async_all=AsyncMock(return_value=data_task)), + ) @pytest.fixture(name="config_entry") def config_entry_fixture(hass, config): """Define a config entry fixture.""" - entry = MockConfigEntry(domain=DOMAIN, unique_id=config[CONF_USERNAME], data=config) + entry = MockConfigEntry(domain=DOMAIN, unique_id=TEST_USERNAME, data=config) entry.add_to_hass(hass) return entry @pytest.fixture(name="config") -def config_fixture(hass): +def config_fixture(): """Define a config entry data fixture.""" return { - CONF_USERNAME: "user@host.com", - CONF_PASSWORD: "password123", + CONF_USERNAME: TEST_USERNAME, + CONF_PASSWORD: TEST_PASSWORD, } @@ -62,14 +64,22 @@ def get_client_fixture(client): return AsyncMock(return_value=client) -@pytest.fixture(name="setup_notion") -async def setup_notion_fixture(hass, config, get_client): - """Define a fixture to set up Notion.""" +@pytest.fixture(name="mock_aionotion") +async def mock_aionotion_fixture(client): + """Define a fixture to patch aionotion.""" with patch( - "homeassistant.components.notion.config_flow.async_get_client", get_client - ), patch("homeassistant.components.notion.async_get_client", get_client), patch( - "homeassistant.components.notion.PLATFORMS", [] + "homeassistant.components.notion.async_get_client", + AsyncMock(return_value=client), + ), patch( + "homeassistant.components.notion.config_flow.async_get_client", + AsyncMock(return_value=client), ): - assert await async_setup_component(hass, DOMAIN, config) - await hass.async_block_till_done() yield + + +@pytest.fixture(name="setup_config_entry") +async def setup_config_entry_fixture(hass, config_entry, mock_aionotion): + """Define a fixture to set up notion.""" + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + yield diff --git a/tests/components/notion/test_config_flow.py b/tests/components/notion/test_config_flow.py index 0eff3890274..437a88ffda9 100644 --- a/tests/components/notion/test_config_flow.py +++ b/tests/components/notion/test_config_flow.py @@ -9,6 +9,8 @@ from homeassistant.components.notion import DOMAIN from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER from homeassistant.const import CONF_PASSWORD, CONF_USERNAME +from .conftest import TEST_PASSWORD, TEST_USERNAME + @pytest.mark.parametrize( "get_client_with_exception,errors", @@ -19,7 +21,7 @@ from homeassistant.const import CONF_PASSWORD, CONF_USERNAME ], ) async def test_create_entry( - hass, client, config, errors, get_client_with_exception, setup_notion + hass, client, config, errors, get_client_with_exception, mock_aionotion ): """Test creating an etry (including recovery from errors).""" result = await hass.config_entries.flow.async_init( @@ -43,14 +45,14 @@ async def test_create_entry( result["flow_id"], user_input=config ) assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY - assert result["title"] == "user@host.com" + assert result["title"] == TEST_USERNAME assert result["data"] == { - CONF_USERNAME: "user@host.com", - CONF_PASSWORD: "password123", + CONF_USERNAME: TEST_USERNAME, + CONF_PASSWORD: TEST_PASSWORD, } -async def test_duplicate_error(hass, config, config_entry): +async def test_duplicate_error(hass, config, setup_config_entry): """Test that errors are shown when duplicates are added.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, data=config @@ -68,7 +70,7 @@ async def test_duplicate_error(hass, config, config_entry): ], ) async def test_reauth( - hass, config, config_entry, errors, get_client_with_exception, setup_notion + hass, config, config_entry, errors, get_client_with_exception, setup_config_entry ): """Test that re-auth works.""" result = await hass.config_entries.flow.async_init( diff --git a/tests/components/notion/test_diagnostics.py b/tests/components/notion/test_diagnostics.py index d8b5abcc781..3b45ed535a1 100644 --- a/tests/components/notion/test_diagnostics.py +++ b/tests/components/notion/test_diagnostics.py @@ -4,7 +4,7 @@ from homeassistant.components.diagnostics import REDACTED from tests.components.diagnostics import get_diagnostics_for_config_entry -async def test_entry_diagnostics(hass, config_entry, hass_client, setup_notion): +async def test_entry_diagnostics(hass, config_entry, hass_client, setup_config_entry): """Test config entry diagnostics.""" assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == { "entry": {