Renovate Tile config flow tests (#85154)

This commit is contained in:
Aaron Bach 2023-01-06 05:08:52 -07:00 committed by GitHub
parent 4b178e88a4
commit 31bf0a0105
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 60 deletions

View File

@ -7,10 +7,12 @@ from pytile.tile import Tile
from homeassistant.components.tile.const import DOMAIN from homeassistant.components.tile.const 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_PASSWORD = "123abc"
TEST_USERNAME = "user@host.com"
@pytest.fixture(name="api") @pytest.fixture(name="api")
def api_fixture(hass, data_tile_details): def api_fixture(hass, data_tile_details):
@ -34,11 +36,11 @@ def config_entry_fixture(hass, config):
@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: "123abc", CONF_PASSWORD: TEST_PASSWORD,
} }
@ -48,14 +50,18 @@ def data_tile_details_fixture():
return json.loads(load_fixture("tile_details_data.json", "tile")) return json.loads(load_fixture("tile_details_data.json", "tile"))
@pytest.fixture(name="setup_tile") @pytest.fixture(name="mock_pytile")
async def setup_tile_fixture(hass, api, config): async def mock_pytile_fixture(api):
"""Define a fixture to set up Tile.""" """Define a fixture to patch pytile."""
with patch( with patch(
"homeassistant.components.tile.config_flow.async_login", return_value=api "homeassistant.components.tile.config_flow.async_login", return_value=api
), patch("homeassistant.components.tile.async_login", return_value=api), patch( ), patch("homeassistant.components.tile.async_login", return_value=api):
"homeassistant.components.tile.PLATFORMS", []
):
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_pytile):
"""Define a fixture to set up tile."""
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
yield

View File

@ -1,5 +1,5 @@
"""Define tests for the Tile config flow.""" """Define tests for the Tile config flow."""
from unittest.mock import patch from unittest.mock import AsyncMock, patch
import pytest import pytest
from pytile.errors import InvalidAuthError, TileError from pytile.errors import InvalidAuthError, TileError
@ -9,8 +9,50 @@ from homeassistant.components.tile import DOMAIN
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_REAUTH, SOURCE_USER from homeassistant.config_entries import SOURCE_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
async def test_duplicate_error(hass, config, config_entry):
@pytest.mark.parametrize(
"mock_login_response,errors",
[
(AsyncMock(side_effect=InvalidAuthError), {"base": "invalid_auth"}),
(AsyncMock(side_effect=TileError), {"base": "unknown"}),
],
)
async def test_create_entry(
hass, api, config, errors, mock_login_response, mock_pytile
):
"""Test creating an entry."""
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:
with patch(
"homeassistant.components.tile.config_flow.async_login", mock_login_response
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=config
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == errors
# Test that we can recover from login 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"] == TEST_USERNAME
assert result["data"] == {
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
}
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
@ -19,40 +61,20 @@ async def test_duplicate_error(hass, config, config_entry):
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
@pytest.mark.parametrize( async def test_import_entry(hass, config, mock_pytile):
"err,err_string", """Test importing an entry."""
[
(InvalidAuthError, "invalid_auth"),
(TileError, "unknown"),
],
)
async def test_errors(hass, config, err, err_string):
"""Test that errors are handled correctly."""
with patch(
"homeassistant.components.tile.config_flow.async_login",
side_effect=err,
):
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"] == {"base": err_string}
async def test_step_import(hass, config, setup_tile):
"""Test that the import step works."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=config DOMAIN, context={"source": SOURCE_IMPORT}, data=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: "123abc", CONF_PASSWORD: TEST_PASSWORD,
} }
async def test_step_reauth(hass, config, config_entry, setup_tile): async def test_step_reauth(hass, config, config_entry, setup_config_entry):
"""Test that the reauth step works.""" """Test that the reauth step works."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_REAUTH}, data=config DOMAIN, context={"source": SOURCE_REAUTH}, data=config
@ -69,22 +91,3 @@ async def test_step_reauth(hass, config, config_entry, setup_tile):
assert result["type"] == data_entry_flow.FlowResultType.ABORT assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "reauth_successful" assert result["reason"] == "reauth_successful"
assert len(hass.config_entries.async_entries()) == 1 assert len(hass.config_entries.async_entries()) == 1
async def test_step_user(hass, config, setup_tile):
"""Test that the user step works."""
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"
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: "123abc",
}

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_tile): 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) == {
"tiles": [ "tiles": [