diff --git a/homeassistant/components/tessie/config_flow.py b/homeassistant/components/tessie/config_flow.py index 4379a810309..3e3207b264b 100644 --- a/homeassistant/components/tessie/config_flow.py +++ b/homeassistant/components/tessie/config_flow.py @@ -81,7 +81,7 @@ class TessieConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): ) except ClientResponseError as e: if e.status == HTTPStatus.UNAUTHORIZED: - errors["base"] = "invalid_access_token" + errors[CONF_ACCESS_TOKEN] = "invalid_access_token" else: errors["base"] = "unknown" except ClientConnectionError: diff --git a/tests/components/tessie/conftest.py b/tests/components/tessie/conftest.py new file mode 100644 index 00000000000..c7a344d54c5 --- /dev/null +++ b/tests/components/tessie/conftest.py @@ -0,0 +1,28 @@ +"""Fixtures for Tessie.""" +from __future__ import annotations + +from unittest.mock import patch + +import pytest + +from .common import TEST_STATE_OF_ALL_VEHICLES, TEST_VEHICLE_STATE_ONLINE + + +@pytest.fixture +def mock_get_state(): + """Mock get_state function.""" + with patch( + "homeassistant.components.tessie.coordinator.get_state", + return_value=TEST_VEHICLE_STATE_ONLINE, + ) as mock_get_state: + yield mock_get_state + + +@pytest.fixture +def mock_get_state_of_all_vehicles(): + """Mock get_state_of_all_vehicles function.""" + with patch( + "homeassistant.components.tessie.config_flow.get_state_of_all_vehicles", + return_value=TEST_STATE_OF_ALL_VEHICLES, + ) as mock_get_state_of_all_vehicles: + yield mock_get_state_of_all_vehicles diff --git a/tests/components/tessie/test_config_flow.py b/tests/components/tessie/test_config_flow.py index d1977a13193..182468e200c 100644 --- a/tests/components/tessie/test_config_flow.py +++ b/tests/components/tessie/test_config_flow.py @@ -15,24 +15,13 @@ from .common import ( ERROR_CONNECTION, ERROR_UNKNOWN, TEST_CONFIG, - TEST_STATE_OF_ALL_VEHICLES, setup_platform, ) from tests.common import MockConfigEntry -@pytest.fixture -def mock_get_state_of_all_vehicles(): - """Mock get_state_of_all_vehicles function.""" - with patch( - "homeassistant.components.tessie.config_flow.get_state_of_all_vehicles", - return_value=TEST_STATE_OF_ALL_VEHICLES, - ) as mock_get_state_of_all_vehicles: - yield mock_get_state_of_all_vehicles - - -async def test_form(hass: HomeAssistant) -> None: +async def test_form(hass: HomeAssistant, mock_get_state_of_all_vehicles) -> None: """Test we get the form.""" result1 = await hass.config_entries.flow.async_init( @@ -42,9 +31,6 @@ async def test_form(hass: HomeAssistant) -> None: assert not result1["errors"] with patch( - "homeassistant.components.tessie.config_flow.get_state_of_all_vehicles", - return_value=TEST_STATE_OF_ALL_VEHICLES, - ) as mock_get_state_of_all_vehicles, patch( "homeassistant.components.tessie.async_setup_entry", return_value=True, ) as mock_setup_entry: @@ -61,96 +47,38 @@ async def test_form(hass: HomeAssistant) -> None: assert result2["data"] == TEST_CONFIG -async def test_form_invalid_access_token(hass: HomeAssistant) -> None: +@pytest.mark.parametrize( + ("side_effect", "error"), + [ + (ERROR_AUTH, {CONF_ACCESS_TOKEN: "invalid_access_token"}), + (ERROR_UNKNOWN, {"base": "unknown"}), + (ERROR_CONNECTION, {"base": "cannot_connect"}), + ], +) +async def test_form_errors( + hass: HomeAssistant, side_effect, error, mock_get_state_of_all_vehicles +) -> None: """Test invalid auth is handled.""" result1 = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with patch( - "homeassistant.components.tessie.config_flow.get_state_of_all_vehicles", - side_effect=ERROR_AUTH, - ): - result2 = await hass.config_entries.flow.async_configure( - result1["flow_id"], - TEST_CONFIG, - ) - - assert result2["type"] == FlowResultType.FORM - assert result2["errors"] == {CONF_ACCESS_TOKEN: "invalid_access_token"} - - # Complete the flow - with patch( - "homeassistant.components.tessie.config_flow.get_state_of_all_vehicles", - return_value=TEST_STATE_OF_ALL_VEHICLES, - ): - result3 = await hass.config_entries.flow.async_configure( - result2["flow_id"], - TEST_CONFIG, - ) - assert result3["type"] == FlowResultType.CREATE_ENTRY - - -async def test_form_invalid_response(hass: HomeAssistant) -> None: - """Test invalid auth is handled.""" - - result1 = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} + mock_get_state_of_all_vehicles.side_effect = side_effect + result2 = await hass.config_entries.flow.async_configure( + result1["flow_id"], + TEST_CONFIG, ) - with patch( - "homeassistant.components.tessie.config_flow.get_state_of_all_vehicles", - side_effect=ERROR_UNKNOWN, - ): - result2 = await hass.config_entries.flow.async_configure( - result1["flow_id"], - TEST_CONFIG, - ) - assert result2["type"] == FlowResultType.FORM - assert result2["errors"] == {"base": "unknown"} + assert result2["errors"] == error # Complete the flow - with patch( - "homeassistant.components.tessie.config_flow.get_state_of_all_vehicles", - return_value=TEST_STATE_OF_ALL_VEHICLES, - ): - result3 = await hass.config_entries.flow.async_configure( - result2["flow_id"], - TEST_CONFIG, - ) - assert result3["type"] == FlowResultType.CREATE_ENTRY - - -async def test_form_network_issue(hass: HomeAssistant) -> None: - """Test network issues are handled.""" - - result1 = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} + mock_get_state_of_all_vehicles.side_effect = None + result3 = await hass.config_entries.flow.async_configure( + result2["flow_id"], + TEST_CONFIG, ) - - with patch( - "homeassistant.components.tessie.config_flow.get_state_of_all_vehicles", - side_effect=ERROR_CONNECTION, - ): - result2 = await hass.config_entries.flow.async_configure( - result1["flow_id"], - TEST_CONFIG, - ) - - assert result2["type"] == FlowResultType.FORM - assert result2["errors"] == {"base": "cannot_connect"} - - # Complete the flow - with patch( - "homeassistant.components.tessie.config_flow.get_state_of_all_vehicles", - return_value=TEST_STATE_OF_ALL_VEHICLES, - ): - result3 = await hass.config_entries.flow.async_configure( - result2["flow_id"], - TEST_CONFIG, - ) assert result3["type"] == FlowResultType.CREATE_ENTRY @@ -196,7 +124,7 @@ async def test_reauth(hass: HomeAssistant, mock_get_state_of_all_vehicles) -> No @pytest.mark.parametrize( ("side_effect", "error"), [ - (ERROR_AUTH, {"base": "invalid_access_token"}), + (ERROR_AUTH, {CONF_ACCESS_TOKEN: "invalid_access_token"}), (ERROR_UNKNOWN, {"base": "unknown"}), (ERROR_CONNECTION, {"base": "cannot_connect"}), ], diff --git a/tests/components/tessie/test_coordinator.py b/tests/components/tessie/test_coordinator.py index 8fe92454c36..50a9f2f7733 100644 --- a/tests/components/tessie/test_coordinator.py +++ b/tests/components/tessie/test_coordinator.py @@ -1,8 +1,5 @@ """Test the Tessie sensor platform.""" from datetime import timedelta -from unittest.mock import patch - -import pytest from homeassistant.components.tessie.coordinator import TESSIE_SYNC_INTERVAL from homeassistant.components.tessie.sensor import TessieStatus @@ -25,15 +22,6 @@ from tests.common import async_fire_time_changed WAIT = timedelta(seconds=TESSIE_SYNC_INTERVAL) -@pytest.fixture -def mock_get_state(): - """Mock get_state function.""" - with patch( - "homeassistant.components.tessie.coordinator.get_state", - ) as mock_get_state: - yield mock_get_state - - async def test_coordinator_online(hass: HomeAssistant, mock_get_state) -> None: """Tests that the coordinator handles online vehciles."""