"""Test the Lichess config flow.""" from unittest.mock import AsyncMock from aiolichess.exceptions import AioLichessError, AuthError import pytest from homeassistant.components.lichess.const import DOMAIN from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_API_TOKEN from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from tests.common import MockConfigEntry @pytest.mark.usefixtures("mock_lichess_client") async def test_full_flow(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: """Test the full flow.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} ) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} result = await hass.config_entries.flow.async_configure( result["flow_id"], {CONF_API_TOKEN: "my_secret_token"} ) assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == "DrNykterstein" assert result["data"] == {CONF_API_TOKEN: "my_secret_token"} assert result["result"].unique_id == "drnykterstein" assert len(mock_setup_entry.mock_calls) == 1 @pytest.mark.parametrize( ("exception", "error"), [ (AuthError, "invalid_auth"), (AioLichessError, "cannot_connect"), (Exception, "unknown"), ], ) async def test_form_errors( hass: HomeAssistant, mock_lichess_client: AsyncMock, mock_setup_entry: AsyncMock, exception: Exception, error: str, ) -> None: """Test we handle form errors.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} ) mock_lichess_client.get_all.side_effect = exception result = await hass.config_entries.flow.async_configure( result["flow_id"], {CONF_API_TOKEN: "my_secret_token"} ) assert result["type"] is FlowResultType.FORM assert result["errors"] == {"base": error} mock_lichess_client.get_all.side_effect = None result = await hass.config_entries.flow.async_configure( result["flow_id"], {CONF_API_TOKEN: "my_secret_token"} ) assert result["type"] is FlowResultType.CREATE_ENTRY assert len(mock_setup_entry.mock_calls) == 1 @pytest.mark.usefixtures("mock_lichess_client") async def test_duplicate_entry( hass: HomeAssistant, mock_config_entry: MockConfigEntry ) -> None: """Test we handle duplicate entries.""" mock_config_entry.add_to_hass(hass) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} ) result = await hass.config_entries.flow.async_configure( result["flow_id"], {CONF_API_TOKEN: "my_secret_token"} ) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_configured"