Strip path from Mastodon base url (#127994)

This commit is contained in:
Andrew Jackson 2024-10-14 12:20:25 +01:00 committed by GitHub
parent 25aea140be
commit 7df973648c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 1 deletions

View File

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

View File

@ -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"),
[