From ff1ec7a028f747de1f96521eb3df6f98d7426434 Mon Sep 17 00:00:00 2001 From: rappenze Date: Wed, 16 Nov 2022 10:44:34 +0100 Subject: [PATCH] Normalize url entered in fibaro integration setup dialog (#81996) * Normalize url entered in fibaro integration setup dialog * Improvements as suggested in code review * Fix spelling in comments --- .../components/fibaro/config_flow.py | 13 ++++++++++++ tests/components/fibaro/test_config_flow.py | 21 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/homeassistant/components/fibaro/config_flow.py b/homeassistant/components/fibaro/config_flow.py index 7a6d7422520..e5cb75890be 100644 --- a/homeassistant/components/fibaro/config_flow.py +++ b/homeassistant/components/fibaro/config_flow.py @@ -54,6 +54,18 @@ async def _validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str } +def _normalize_url(url: str) -> str: + """Try to fix errors in the entered url. + + We know that the url should be in the format http:///api/ + """ + if url.endswith("/api"): + return f"{url}/" + if not url.endswith("/api/"): + return f"{url}api/" if url.endswith("/") else f"{url}/api/" + return url + + class FibaroConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow for Fibaro.""" @@ -71,6 +83,7 @@ class FibaroConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): if user_input is not None: try: + user_input[CONF_URL] = _normalize_url(user_input[CONF_URL]) info = await _validate_input(self.hass, user_input) except FibaroConnectFailed: errors["base"] = "cannot_connect" diff --git a/tests/components/fibaro/test_config_flow.py b/tests/components/fibaro/test_config_flow.py index f68bb5fe4ca..2a0936588cd 100644 --- a/tests/components/fibaro/test_config_flow.py +++ b/tests/components/fibaro/test_config_flow.py @@ -6,6 +6,7 @@ import pytest from homeassistant import config_entries from homeassistant.components.fibaro import DOMAIN +from homeassistant.components.fibaro.config_flow import _normalize_url from homeassistant.components.fibaro.const import CONF_IMPORT_PLUGINS from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME @@ -362,3 +363,23 @@ async def test_reauth_auth_failure(hass): assert result["type"] == "form" assert result["step_id"] == "reauth_confirm" assert result["errors"] == {"base": "invalid_auth"} + + +async def test_normalize_url_does_not_touch_valid_url(): + """Test that a correctly entered url is not touched.""" + assert _normalize_url(TEST_URL) == TEST_URL + + +async def test_normalize_url_add_missing_slash_at_the_end(): + """Test that a / is added at the end.""" + assert _normalize_url("http://192.168.1.1/api") == "http://192.168.1.1/api/" + + +async def test_normalize_url_add_api(): + """Test that api/ is added.""" + assert _normalize_url("http://192.168.1.1/") == "http://192.168.1.1/api/" + + +async def test_normalize_url_add_api_with_leading_slash(): + """Test that /api/ is added.""" + assert _normalize_url("http://192.168.1.1") == "http://192.168.1.1/api/"