diff --git a/tests/components/tile/conftest.py b/tests/components/tile/conftest.py index 474c784ec3c..187d8f3a2c1 100644 --- a/tests/components/tile/conftest.py +++ b/tests/components/tile/conftest.py @@ -7,10 +7,12 @@ from pytile.tile import Tile from homeassistant.components.tile.const 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_PASSWORD = "123abc" +TEST_USERNAME = "user@host.com" + @pytest.fixture(name="api") def api_fixture(hass, data_tile_details): @@ -34,11 +36,11 @@ def config_entry_fixture(hass, config): @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: "123abc", + CONF_USERNAME: TEST_USERNAME, + CONF_PASSWORD: TEST_PASSWORD, } @@ -48,14 +50,18 @@ def data_tile_details_fixture(): return json.loads(load_fixture("tile_details_data.json", "tile")) -@pytest.fixture(name="setup_tile") -async def setup_tile_fixture(hass, api, config): - """Define a fixture to set up Tile.""" +@pytest.fixture(name="mock_pytile") +async def mock_pytile_fixture(api): + """Define a fixture to patch pytile.""" with patch( "homeassistant.components.tile.config_flow.async_login", return_value=api - ), patch("homeassistant.components.tile.async_login", return_value=api), patch( - "homeassistant.components.tile.PLATFORMS", [] - ): - assert await async_setup_component(hass, DOMAIN, config) - await hass.async_block_till_done() + ), patch("homeassistant.components.tile.async_login", return_value=api): 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 diff --git a/tests/components/tile/test_config_flow.py b/tests/components/tile/test_config_flow.py index 9200ed3f382..ff17f9b26e1 100644 --- a/tests/components/tile/test_config_flow.py +++ b/tests/components/tile/test_config_flow.py @@ -1,5 +1,5 @@ """Define tests for the Tile config flow.""" -from unittest.mock import patch +from unittest.mock import AsyncMock, patch import pytest 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.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.""" result = await hass.config_entries.flow.async_init( 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" -@pytest.mark.parametrize( - "err,err_string", - [ - (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.""" +async def test_import_entry(hass, config, mock_pytile): + """Test importing an entry.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_IMPORT}, data=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: "123abc", + CONF_USERNAME: TEST_USERNAME, + 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.""" result = await hass.config_entries.flow.async_init( 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["reason"] == "reauth_successful" 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", - } diff --git a/tests/components/tile/test_diagnostics.py b/tests/components/tile/test_diagnostics.py index e9cf0d34d77..d2193519975 100644 --- a/tests/components/tile/test_diagnostics.py +++ b/tests/components/tile/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_tile): +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) == { "tiles": [