From 0bc572787a90a74a3d71d3131a6fb54aa0908ea5 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 16 Oct 2024 19:03:24 +0200 Subject: [PATCH] Add check for valid error code in translation checks in flows (#128445) --- .../components/config/test_config_entries.py | 12 +++++++++++ tests/components/conftest.py | 12 +++++++++++ tests/components/emoncms/test_config_flow.py | 6 ++++++ tests/components/flume/test_config_flow.py | 20 ++++++++++++++++++- tests/components/generic/test_config_flow.py | 4 ++++ tests/components/guardian/test_config_flow.py | 4 ++++ .../hvv_departures/test_config_flow.py | 9 +++++++++ .../components/hydrawise/test_config_flow.py | 4 ++++ .../components/lamarzocco/test_config_flow.py | 5 +++++ .../landisgyr_heat_meter/test_config_flow.py | 8 ++++++++ tests/components/nina/test_config_flow.py | 5 +++++ .../components/ovo_energy/test_config_flow.py | 15 +++++++++++++- tests/components/tradfri/test_config_flow.py | 4 ++++ .../utility_meter/test_config_flow.py | 4 ++++ tests/components/vilfo/test_config_flow.py | 4 ++++ 15 files changed, 114 insertions(+), 2 deletions(-) diff --git a/tests/components/config/test_config_entries.py b/tests/components/config/test_config_entries.py index c2a5e19c7d4..b96aa9ae006 100644 --- a/tests/components/config/test_config_entries.py +++ b/tests/components/config/test_config_entries.py @@ -393,6 +393,10 @@ async def test_available_flows( ############################ +@pytest.mark.parametrize( + "ignore_translations", + ["component.test.config.error.Should be unique."], +) async def test_initialize_flow(hass: HomeAssistant, client: TestClient) -> None: """Test we can initialize a flow.""" mock_platform(hass, "test.config_flow", None) @@ -772,6 +776,10 @@ async def test_get_progress_index_unauth( assert response["error"]["code"] == "unauthorized" +@pytest.mark.parametrize( + "ignore_translations", + ["component.test.config.error.Should be unique."], +) async def test_get_progress_flow(hass: HomeAssistant, client: TestClient) -> None: """Test we can query the API for same result as we get from init a flow.""" mock_platform(hass, "test.config_flow", None) @@ -804,6 +812,10 @@ async def test_get_progress_flow(hass: HomeAssistant, client: TestClient) -> Non assert data == data2 +@pytest.mark.parametrize( + "ignore_translations", + ["component.test.config.error.Should be unique."], +) async def test_get_progress_flow_unauth( hass: HomeAssistant, client: TestClient, hass_admin_user: MockUser ) -> None: diff --git a/tests/components/conftest.py b/tests/components/conftest.py index 763dbb1d002..ce2e67981da 100644 --- a/tests/components/conftest.py +++ b/tests/components/conftest.py @@ -540,6 +540,18 @@ def check_config_translations(ignore_translations: str | list[str]) -> Generator # Gets set to False on first run, and to True on subsequent runs setattr(flow, "__flow_seen_before", hasattr(flow, "__flow_seen_before")) + if result["type"] is FlowResultType.FORM: + if errors := result.get("errors"): + for error in errors.values(): + await _ensure_translation_exists( + flow.hass, + _ignore_translations, + category, + component, + f"error.{error}", + ) + return result + if result["type"] is FlowResultType.ABORT: # We don't need translations for a discovery flow which immediately # aborts, since such flows won't be seen by users diff --git a/tests/components/emoncms/test_config_flow.py b/tests/components/emoncms/test_config_flow.py index 17ec32a9008..b554466639e 100644 --- a/tests/components/emoncms/test_config_flow.py +++ b/tests/components/emoncms/test_config_flow.py @@ -2,6 +2,8 @@ from unittest.mock import AsyncMock +import pytest + from homeassistant.components.emoncms.const import CONF_ONLY_INCLUDE_FEEDID, DOMAIN from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER from homeassistant.const import CONF_API_KEY, CONF_URL @@ -127,6 +129,10 @@ async def test_options_flow( assert config_entry.options == CONFIG_ENTRY +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.emoncms.options.error.failure"], +) async def test_options_flow_failure( hass: HomeAssistant, mock_setup_entry: AsyncMock, diff --git a/tests/components/flume/test_config_flow.py b/tests/components/flume/test_config_flow.py index 490b496cbd7..c323defc791 100644 --- a/tests/components/flume/test_config_flow.py +++ b/tests/components/flume/test_config_flow.py @@ -61,6 +61,10 @@ async def test_form(hass: HomeAssistant) -> None: assert len(mock_setup_entry.mock_calls) == 1 +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.flume.config.error.invalid_auth"], +) @pytest.mark.usefixtures("access_token") async def test_form_invalid_auth(hass: HomeAssistant, requests_mock: Mocker) -> None: """Test we handle invalid auth.""" @@ -89,6 +93,10 @@ async def test_form_invalid_auth(hass: HomeAssistant, requests_mock: Mocker) -> assert result2["errors"] == {"password": "invalid_auth"} +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.flume.config.error.cannot_connect"], +) @pytest.mark.usefixtures("access_token", "device_list_timeout") async def test_form_cannot_connect(hass: HomeAssistant) -> None: """Test we handle cannot connect error.""" @@ -112,7 +120,13 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None: @pytest.mark.parametrize( # Remove when translations fixed "ignore_translations", - ["component.flume.config.abort.reauth_successful"], + [ + [ + "component.flume.config.abort.reauth_successful", + "component.flume.config.error.cannot_connect", + "component.flume.config.error.invalid_auth", + ] + ], ) @pytest.mark.usefixtures("access_token") async def test_reauth(hass: HomeAssistant, requests_mock: Mocker) -> None: @@ -194,6 +208,10 @@ async def test_reauth(hass: HomeAssistant, requests_mock: Mocker) -> None: assert result4["reason"] == "reauth_successful" +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.flume.config.error.cannot_connect"], +) @pytest.mark.usefixtures("access_token") async def test_form_no_devices(hass: HomeAssistant, requests_mock: Mocker) -> None: """Test a device list response that contains no values will raise an error.""" diff --git a/tests/components/generic/test_config_flow.py b/tests/components/generic/test_config_flow.py index cf4ab0bde57..7575a078675 100644 --- a/tests/components/generic/test_config_flow.py +++ b/tests/components/generic/test_config_flow.py @@ -637,6 +637,10 @@ async def test_form_stream_other_error(hass: HomeAssistant, user_flow) -> None: await hass.async_block_till_done() +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.generic.config.error.Some message"], +) @respx.mock @pytest.mark.usefixtures("fakeimg_png") async def test_form_stream_worker_error( diff --git a/tests/components/guardian/test_config_flow.py b/tests/components/guardian/test_config_flow.py index 6c06171a45f..876434e8333 100644 --- a/tests/components/guardian/test_config_flow.py +++ b/tests/components/guardian/test_config_flow.py @@ -33,6 +33,10 @@ async def test_duplicate_error(hass: HomeAssistant, config: dict[str, Any]) -> N assert result["reason"] == "already_configured" +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.guardian.config.error.cannot_connect"], +) async def test_connect_error(hass: HomeAssistant, config: dict[str, Any]) -> None: """Test that the config entry errors out if the device cannot connect.""" with patch( diff --git a/tests/components/hvv_departures/test_config_flow.py b/tests/components/hvv_departures/test_config_flow.py index c85bfb7f6ee..8d82382d9a2 100644 --- a/tests/components/hvv_departures/test_config_flow.py +++ b/tests/components/hvv_departures/test_config_flow.py @@ -4,6 +4,7 @@ import json from unittest.mock import patch from pygti.exceptions import CannotConnect, InvalidAuth +import pytest from homeassistant.components.hvv_departures.const import ( CONF_FILTER, @@ -312,6 +313,10 @@ async def test_options_flow(hass: HomeAssistant) -> None: } +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.hvv_departures.options.error.invalid_auth"], +) async def test_options_flow_invalid_auth(hass: HomeAssistant) -> None: """Test that options flow works.""" @@ -355,6 +360,10 @@ async def test_options_flow_invalid_auth(hass: HomeAssistant) -> None: assert result["errors"] == {"base": "invalid_auth"} +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.hvv_departures.options.error.cannot_connect"], +) async def test_options_flow_cannot_connect(hass: HomeAssistant) -> None: """Test that options flow works.""" diff --git a/tests/components/hydrawise/test_config_flow.py b/tests/components/hydrawise/test_config_flow.py index e85b1b9b249..e2eaaa51dc2 100644 --- a/tests/components/hydrawise/test_config_flow.py +++ b/tests/components/hydrawise/test_config_flow.py @@ -93,6 +93,10 @@ async def test_form_connect_timeout( assert result2["type"] is FlowResultType.CREATE_ENTRY +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.hydrawise.config.error.invalid_auth"], +) async def test_form_not_authorized_error( hass: HomeAssistant, mock_pydrawise: AsyncMock, user: User ) -> None: diff --git a/tests/components/lamarzocco/test_config_flow.py b/tests/components/lamarzocco/test_config_flow.py index e4e8d6ebafd..89e5c968724 100644 --- a/tests/components/lamarzocco/test_config_flow.py +++ b/tests/components/lamarzocco/test_config_flow.py @@ -4,6 +4,7 @@ from unittest.mock import MagicMock, patch from lmcloud.exceptions import AuthFail, RequestNotSuccessful from lmcloud.models import LaMarzoccoDeviceInfo +import pytest from homeassistant.components.lamarzocco.config_flow import CONF_MACHINE from homeassistant.components.lamarzocco.const import CONF_USE_BLUETOOTH, DOMAIN @@ -365,6 +366,10 @@ async def test_bluetooth_discovery( } +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.lamarzocco.config.error.machine_not_found"], +) async def test_bluetooth_discovery_errors( hass: HomeAssistant, mock_lamarzocco: MagicMock, diff --git a/tests/components/landisgyr_heat_meter/test_config_flow.py b/tests/components/landisgyr_heat_meter/test_config_flow.py index fe62d530719..79088508e61 100644 --- a/tests/components/landisgyr_heat_meter/test_config_flow.py +++ b/tests/components/landisgyr_heat_meter/test_config_flow.py @@ -101,6 +101,10 @@ async def test_list_entry(mock_port, mock_heat_meter, hass: HomeAssistant) -> No } +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.landisgyr_heat_meter.config.error.cannot_connect"], +) @patch(API_HEAT_METER_SERVICE) async def test_manual_entry_fail(mock_heat_meter, hass: HomeAssistant) -> None: """Test manual entry fails.""" @@ -131,6 +135,10 @@ async def test_manual_entry_fail(mock_heat_meter, hass: HomeAssistant) -> None: assert result["errors"] == {"base": "cannot_connect"} +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.landisgyr_heat_meter.config.error.cannot_connect"], +) @patch(API_HEAT_METER_SERVICE) @patch("serial.tools.list_ports.comports", return_value=[mock_serial_port()]) async def test_list_entry_fail(mock_port, mock_heat_meter, hass: HomeAssistant) -> None: diff --git a/tests/components/nina/test_config_flow.py b/tests/components/nina/test_config_flow.py index 309c8860c20..cd0904b181d 100644 --- a/tests/components/nina/test_config_flow.py +++ b/tests/components/nina/test_config_flow.py @@ -8,6 +8,7 @@ from typing import Any from unittest.mock import patch from pynina import ApiError +import pytest from homeassistant.components.nina.const import ( CONF_AREA_FILTER, @@ -278,6 +279,10 @@ async def test_options_flow_connection_error(hass: HomeAssistant) -> None: assert result["errors"] == {"base": "cannot_connect"} +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.nina.options.error.unknown"], +) async def test_options_flow_unexpected_exception(hass: HomeAssistant) -> None: """Test config flow options but with an unexpected exception.""" config_entry = MockConfigEntry( diff --git a/tests/components/ovo_energy/test_config_flow.py b/tests/components/ovo_energy/test_config_flow.py index c49af5ce826..f21672679bd 100644 --- a/tests/components/ovo_energy/test_config_flow.py +++ b/tests/components/ovo_energy/test_config_flow.py @@ -121,6 +121,10 @@ async def test_full_flow_implementation(hass: HomeAssistant) -> None: assert result2["data"][CONF_ACCOUNT] == FIXTURE_USER_INPUT[CONF_ACCOUNT] +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.ovo_energy.config.error.authorization_error"], +) async def test_reauth_authorization_error(hass: HomeAssistant) -> None: """Test we show user form on authorization error.""" mock_config = MockConfigEntry( @@ -147,6 +151,10 @@ async def test_reauth_authorization_error(hass: HomeAssistant) -> None: assert result2["errors"] == {"base": "authorization_error"} +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.ovo_energy.config.error.connection_error"], +) async def test_reauth_connection_error(hass: HomeAssistant) -> None: """Test we show user form on connection error.""" mock_config = MockConfigEntry( @@ -175,7 +183,12 @@ async def test_reauth_connection_error(hass: HomeAssistant) -> None: @pytest.mark.parametrize( # Remove when translations fixed "ignore_translations", - ["component.ovo_energy.config.abort.reauth_successful"], + [ + [ + "component.ovo_energy.config.abort.reauth_successful", + "component.ovo_energy.config.error.authorization_error", + ] + ], ) async def test_reauth_flow(hass: HomeAssistant) -> None: """Test reauth works.""" diff --git a/tests/components/tradfri/test_config_flow.py b/tests/components/tradfri/test_config_flow.py index af2fdc22d2a..5c06851782c 100644 --- a/tests/components/tradfri/test_config_flow.py +++ b/tests/components/tradfri/test_config_flow.py @@ -86,6 +86,10 @@ async def test_user_connection_timeout( assert result["errors"] == {"base": "timeout"} +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.tradfri.config.error.invalid_security_code"], +) async def test_user_connection_bad_key( hass: HomeAssistant, mock_auth, mock_entry_setup ) -> None: diff --git a/tests/components/utility_meter/test_config_flow.py b/tests/components/utility_meter/test_config_flow.py index 560566d7c49..612bfaa88d7 100644 --- a/tests/components/utility_meter/test_config_flow.py +++ b/tests/components/utility_meter/test_config_flow.py @@ -72,6 +72,10 @@ async def test_config_flow(hass: HomeAssistant, platform) -> None: assert config_entry.title == "Electricity meter" +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.utility_meter.config.error.tariffs_not_unique"], +) async def test_tariffs(hass: HomeAssistant) -> None: """Test tariffs.""" input_sensor_entity_id = "sensor.input" diff --git a/tests/components/vilfo/test_config_flow.py b/tests/components/vilfo/test_config_flow.py index c4fdb2fe22c..24739f509e4 100644 --- a/tests/components/vilfo/test_config_flow.py +++ b/tests/components/vilfo/test_config_flow.py @@ -150,6 +150,10 @@ async def test_form_exceptions( assert result["type"] is FlowResultType.CREATE_ENTRY +@pytest.mark.parametrize( # Remove when translations fixed + "ignore_translations", + ["component.vilfo.config.error.wrong_host"], +) async def test_form_wrong_host( hass: HomeAssistant, mock_is_valid_host: AsyncMock,