1
0
mirror of https://github.com/home-assistant/core.git synced 2026-07-06 14:25:34 +01:00
Files
core/tests/components/jellyfin/test_init.py
T
2026-06-08 15:43:58 +02:00

142 lines
4.5 KiB
Python

"""Tests for the Jellyfin integration."""
from unittest.mock import MagicMock
from homeassistant.components.jellyfin.const import DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.setup import async_setup_component
from . import async_load_json_fixture
from .const import TEST_PASSWORD, TEST_URL, TEST_USERNAME
from tests.common import MockConfigEntry
from tests.typing import WebSocketGenerator
async def test_config_entry_not_ready(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_jellyfin: MagicMock,
mock_client: MagicMock,
) -> None:
"""Test the Jellyfin configuration entry not ready."""
mock_client.auth.connect_to_address.return_value = await async_load_json_fixture(
hass,
"auth-connect-address-failure.json",
)
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
async def test_invalid_auth(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_jellyfin: MagicMock,
mock_client: MagicMock,
) -> None:
"""Test the Jellyfin integration handling invalid credentials."""
mock_client.auth.connect_to_address.return_value = await async_load_json_fixture(
hass,
"auth-connect-address.json",
)
mock_client.auth.login.return_value = await async_load_json_fixture(
hass,
"auth-login-failure.json",
)
mock_config_entry.add_to_hass(hass)
assert not await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
assert flows[0]["context"]["source"] == SOURCE_REAUTH
async def test_load_unload_config_entry(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_jellyfin: MagicMock,
) -> None:
"""Test the Jellyfin configuration entry loading/unloading."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED
await hass.config_entries.async_unload(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
async def test_migrate_strips_trailing_slash_from_url(
hass: HomeAssistant,
mock_jellyfin: MagicMock,
mock_client: MagicMock,
) -> None:
"""Test migrating an entry strips a trailing slash from the stored URL."""
config_entry = MockConfigEntry(
title="Jellyfin",
domain=DOMAIN,
data={
CONF_URL: f"{TEST_URL}/",
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
},
unique_id="USER-UUID",
version=1,
minor_version=1,
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.minor_version == 2
assert config_entry.data[CONF_URL] == TEST_URL
async def test_device_remove_devices(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
mock_config_entry: MockConfigEntry,
mock_jellyfin: MagicMock,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test we can only remove a device that no longer exists."""
assert await async_setup_component(hass, "config", {})
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
device_entry = device_registry.async_get_device(
identifiers={
(
DOMAIN,
"DEVICE-UUID",
)
},
)
client = await hass_ws_client(hass)
response = await client.remove_device(device_entry.id, mock_config_entry.entry_id)
assert not response["success"]
old_device_entry = device_registry.async_get_or_create(
config_entry_id=mock_config_entry.entry_id,
identifiers={(DOMAIN, "OLD-DEVICE-UUID")},
)
response = await client.remove_device(
old_device_entry.id, mock_config_entry.entry_id
)
assert response["success"]