Add check for valid error code in translation checks in flows (#128445)

pull/128532/head
epenet 2024-10-16 19:03:24 +02:00 committed by GitHub
parent 15fc4a8ae4
commit 0bc572787a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 114 additions and 2 deletions

View File

@ -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:

View File

@ -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

View File

@ -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,

View File

@ -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."""

View File

@ -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(

View File

@ -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(

View File

@ -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."""

View File

@ -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:

View File

@ -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,

View File

@ -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:

View File

@ -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(

View File

@ -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."""

View File

@ -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:

View File

@ -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"

View File

@ -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,