From 7df973648c1b36dc759aaf1e5ea06afb2163bbe2 Mon Sep 17 00:00:00 2001 From: Andrew Jackson Date: Mon, 14 Oct 2024 12:20:25 +0100 Subject: [PATCH] Strip path from Mastodon base url (#127994) --- .../components/mastodon/config_flow.py | 10 +++++- tests/components/mastodon/test_config_flow.py | 33 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/mastodon/config_flow.py b/homeassistant/components/mastodon/config_flow.py index 5e1af5fae92..7c0985570f7 100644 --- a/homeassistant/components/mastodon/config_flow.py +++ b/homeassistant/components/mastodon/config_flow.py @@ -6,6 +6,7 @@ from typing import Any from mastodon.Mastodon import MastodonNetworkError, MastodonUnauthorizedError import voluptuous as vol +from yarl import URL from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult from homeassistant.const import ( @@ -42,6 +43,11 @@ STEP_USER_DATA_SCHEMA = vol.Schema( ) +def base_url_from_url(url: str) -> str: + """Return the base url from a url.""" + return str(URL(url).origin()) + + class MastodonConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow.""" @@ -105,6 +111,8 @@ class MastodonConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a flow initialized by the user.""" errors: dict[str, str] | None = None if user_input: + user_input[CONF_BASE_URL] = base_url_from_url(user_input[CONF_BASE_URL]) + instance, account, errors = await self.hass.async_add_executor_job( self.check_connection, user_input[CONF_BASE_URL], @@ -130,7 +138,7 @@ class MastodonConfigFlow(ConfigFlow, domain=DOMAIN): LOGGER.debug("Importing Mastodon from configuration.yaml") - base_url = str(import_data.get(CONF_BASE_URL, DEFAULT_URL)) + base_url = base_url_from_url(str(import_data.get(CONF_BASE_URL, DEFAULT_URL))) client_id = str(import_data.get(CONF_CLIENT_ID)) client_secret = str(import_data.get(CONF_CLIENT_SECRET)) access_token = str(import_data.get(CONF_ACCESS_TOKEN)) diff --git a/tests/components/mastodon/test_config_flow.py b/tests/components/mastodon/test_config_flow.py index 073a6534d7d..33f73812348 100644 --- a/tests/components/mastodon/test_config_flow.py +++ b/tests/components/mastodon/test_config_flow.py @@ -47,6 +47,39 @@ async def test_full_flow( assert result["result"].unique_id == "trwnh_mastodon_social" +async def test_full_flow_with_path( + hass: HomeAssistant, + mock_mastodon_client: AsyncMock, + mock_setup_entry: AsyncMock, +) -> None: + """Test full flow, where a path is accidentally specified.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_BASE_URL: "https://mastodon.social/home", + CONF_CLIENT_ID: "client_id", + CONF_CLIENT_SECRET: "client_secret", + CONF_ACCESS_TOKEN: "access_token", + }, + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "@trwnh@mastodon.social" + assert result["data"] == { + CONF_BASE_URL: "https://mastodon.social", + CONF_CLIENT_ID: "client_id", + CONF_CLIENT_SECRET: "client_secret", + CONF_ACCESS_TOKEN: "access_token", + } + assert result["result"].unique_id == "trwnh_mastodon_social" + + @pytest.mark.parametrize( ("exception", "error"), [