mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 06:37:52 +00:00
Strip path from Mastodon base url (#127994)
This commit is contained in:
parent
25aea140be
commit
7df973648c
@ -6,6 +6,7 @@ from typing import Any
|
|||||||
|
|
||||||
from mastodon.Mastodon import MastodonNetworkError, MastodonUnauthorizedError
|
from mastodon.Mastodon import MastodonNetworkError, MastodonUnauthorizedError
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
from yarl import URL
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
|
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
|
||||||
from homeassistant.const import (
|
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):
|
class MastodonConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow."""
|
"""Handle a config flow."""
|
||||||
|
|
||||||
@ -105,6 +111,8 @@ class MastodonConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
"""Handle a flow initialized by the user."""
|
"""Handle a flow initialized by the user."""
|
||||||
errors: dict[str, str] | None = None
|
errors: dict[str, str] | None = None
|
||||||
if user_input:
|
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(
|
instance, account, errors = await self.hass.async_add_executor_job(
|
||||||
self.check_connection,
|
self.check_connection,
|
||||||
user_input[CONF_BASE_URL],
|
user_input[CONF_BASE_URL],
|
||||||
@ -130,7 +138,7 @@ class MastodonConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
LOGGER.debug("Importing Mastodon from configuration.yaml")
|
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_id = str(import_data.get(CONF_CLIENT_ID))
|
||||||
client_secret = str(import_data.get(CONF_CLIENT_SECRET))
|
client_secret = str(import_data.get(CONF_CLIENT_SECRET))
|
||||||
access_token = str(import_data.get(CONF_ACCESS_TOKEN))
|
access_token = str(import_data.get(CONF_ACCESS_TOKEN))
|
||||||
|
@ -47,6 +47,39 @@ async def test_full_flow(
|
|||||||
assert result["result"].unique_id == "trwnh_mastodon_social"
|
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(
|
@pytest.mark.parametrize(
|
||||||
("exception", "error"),
|
("exception", "error"),
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user