Renovate Notion config flow tests (#84906)

This commit is contained in:
Aaron Bach 2022-12-31 07:34:59 -07:00 committed by GitHub
parent 9c348b6330
commit e7cb3f1979
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 26 deletions

View file

@ -1,40 +1,42 @@
"""Define fixtures for Notion tests.""" """Define fixtures for Notion tests."""
import json import json
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, Mock, patch
import pytest import pytest
from homeassistant.components.notion import DOMAIN from homeassistant.components.notion import DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, load_fixture from tests.common import MockConfigEntry, load_fixture
TEST_USERNAME = "user@host.com"
TEST_PASSWORD = "password123"
@pytest.fixture(name="client") @pytest.fixture(name="client")
def client_fixture(data_bridge, data_sensor, data_task): def client_fixture(data_bridge, data_sensor, data_task):
"""Define a fixture for an aionotion client.""" """Define a fixture for an aionotion client."""
client = AsyncMock() return Mock(
client.bridge.async_all.return_value = data_bridge bridge=Mock(async_all=AsyncMock(return_value=data_bridge)),
client.sensor.async_all.return_value = data_sensor sensor=Mock(async_all=AsyncMock(return_value=data_sensor)),
client.task.async_all.return_value = data_task task=Mock(async_all=AsyncMock(return_value=data_task)),
return client )
@pytest.fixture(name="config_entry") @pytest.fixture(name="config_entry")
def config_entry_fixture(hass, config): def config_entry_fixture(hass, config):
"""Define a config entry fixture.""" """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) entry.add_to_hass(hass)
return entry return entry
@pytest.fixture(name="config") @pytest.fixture(name="config")
def config_fixture(hass): def config_fixture():
"""Define a config entry data fixture.""" """Define a config entry data fixture."""
return { return {
CONF_USERNAME: "user@host.com", CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: "password123", CONF_PASSWORD: TEST_PASSWORD,
} }
@ -62,14 +64,22 @@ def get_client_fixture(client):
return AsyncMock(return_value=client) return AsyncMock(return_value=client)
@pytest.fixture(name="setup_notion") @pytest.fixture(name="mock_aionotion")
async def setup_notion_fixture(hass, config, get_client): async def mock_aionotion_fixture(client):
"""Define a fixture to set up Notion.""" """Define a fixture to patch aionotion."""
with patch( with patch(
"homeassistant.components.notion.config_flow.async_get_client", get_client "homeassistant.components.notion.async_get_client",
), patch("homeassistant.components.notion.async_get_client", get_client), patch( AsyncMock(return_value=client),
"homeassistant.components.notion.PLATFORMS", [] ), 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 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

View file

@ -9,6 +9,8 @@ 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 .conftest import TEST_PASSWORD, TEST_USERNAME
@pytest.mark.parametrize( @pytest.mark.parametrize(
"get_client_with_exception,errors", "get_client_with_exception,errors",
@ -19,7 +21,7 @@ from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
], ],
) )
async def test_create_entry( 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).""" """Test creating an etry (including recovery from errors)."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
@ -43,14 +45,14 @@ async def test_create_entry(
result["flow_id"], user_input=config result["flow_id"], user_input=config
) )
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["title"] == "user@host.com" assert result["title"] == TEST_USERNAME
assert result["data"] == { assert result["data"] == {
CONF_USERNAME: "user@host.com", CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: "password123", 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.""" """Test that errors are shown when duplicates are added."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config DOMAIN, context={"source": SOURCE_USER}, data=config
@ -68,7 +70,7 @@ async def test_duplicate_error(hass, config, config_entry):
], ],
) )
async def test_reauth( 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.""" """Test that re-auth works."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(

View file

@ -4,7 +4,7 @@ from homeassistant.components.diagnostics import REDACTED
from tests.components.diagnostics import get_diagnostics_for_config_entry 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.""" """Test config entry diagnostics."""
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == { assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
"entry": { "entry": {