* Initial commit after scaffold setup * Add initial config flow * Create initial entity * Ready for testing * Can browse, no result yet * Further improvements. Browsing is working. Now need to work on proper stream URL * Two valid URLs. Do not play in HA * First working version for music * Add thumbnail * Includes Artist->Album hierarchy * Add sorting of artists, albums and tracks * Remove code for video libraries * Improved code styling * Optimize configuration flow * Fix unit tests for config flow * Fix import order * Conform to style requirements * Use empty string as media type for non playables * 100% code coverage config_flow * Type async_get_media_source * Final docsctring fix after rebase * Add __init__ and media_source files to .coveragerc * Fix testing issues after rebase * Fix string format issues and relative const import * Remove unused manifest entries * Raise ConfigEntry exceptions, not log errors * Upgrade dependency to avoid WARNING on startup * Change to builtin tuple and list (deprecation) * Log broad exceptions * Add strict typing * Further type fixes after rebase * Retry when cannot connect, otherwise fail setup * Remove unused CONFIG_SCHEMA * Enable strict typing checks * FlowResultDict -> FlowResult * Code quality improvements * Resolve mypy.ini merge conflict * Use unique userid generated by Jellyfin * Update homeassistant/components/jellyfin/config_flow.py Remove connection class from config flow Co-authored-by: Milan Meulemans <milan.meulemans@live.be> * Minor changes for additional checks after rebase * Remove title from string and translations * Changes wrt review * Fixes based on rebase and review suggestions * Move client initialization to separate file * Remove persistent_notification, add test const.py Co-authored-by: Milan Meulemans <milan.meulemans@live.be>
164 lines
5.6 KiB
Python
164 lines
5.6 KiB
Python
"""Test the jellyfin config flow."""
|
|
from unittest.mock import patch
|
|
|
|
from homeassistant import config_entries, data_entry_flow
|
|
from homeassistant.components.jellyfin.const import DOMAIN
|
|
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from .const import (
|
|
MOCK_SUCCESFUL_CONNECTION_STATE,
|
|
MOCK_SUCCESFUL_LOGIN_RESPONSE,
|
|
MOCK_UNSUCCESFUL_CONNECTION_STATE,
|
|
MOCK_UNSUCCESFUL_LOGIN_RESPONSE,
|
|
MOCK_USER_SETTINGS,
|
|
TEST_PASSWORD,
|
|
TEST_URL,
|
|
TEST_USERNAME,
|
|
)
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
async def test_abort_if_existing_entry(hass: HomeAssistant):
|
|
"""Check flow abort when an entry already exist."""
|
|
MockConfigEntry(domain=DOMAIN).add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
assert result["reason"] == "single_instance_allowed"
|
|
|
|
|
|
async def test_form(hass: HomeAssistant):
|
|
"""Test the complete configuration form."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
assert result["type"] == "form"
|
|
assert result["errors"] == {}
|
|
|
|
with patch(
|
|
"homeassistant.components.jellyfin.client_wrapper.ConnectionManager.connect_to_address",
|
|
return_value=MOCK_SUCCESFUL_CONNECTION_STATE,
|
|
) as mock_connect, patch(
|
|
"homeassistant.components.jellyfin.client_wrapper.ConnectionManager.login",
|
|
return_value=MOCK_SUCCESFUL_LOGIN_RESPONSE,
|
|
) as mock_login, patch(
|
|
"homeassistant.components.jellyfin.async_setup_entry",
|
|
return_value=True,
|
|
) as mock_setup_entry, patch(
|
|
"homeassistant.components.jellyfin.client_wrapper.API.get_user_settings",
|
|
return_value=MOCK_USER_SETTINGS,
|
|
) as mock_set_id:
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_URL: TEST_URL,
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert result2["type"] == "create_entry"
|
|
assert result2["title"] == TEST_URL
|
|
assert result2["data"] == {
|
|
CONF_URL: TEST_URL,
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
}
|
|
|
|
assert len(mock_connect.mock_calls) == 1
|
|
assert len(mock_login.mock_calls) == 1
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
assert len(mock_set_id.mock_calls) == 1
|
|
|
|
|
|
async def test_form_cannot_connect(hass: HomeAssistant):
|
|
"""Test we handle an unreachable server."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
assert result["type"] == "form"
|
|
assert result["errors"] == {}
|
|
|
|
with patch(
|
|
"homeassistant.components.jellyfin.client_wrapper.ConnectionManager.connect_to_address",
|
|
return_value=MOCK_UNSUCCESFUL_CONNECTION_STATE,
|
|
) as mock_connect:
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_URL: TEST_URL,
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert result2["type"] == "form"
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|
|
|
|
assert len(mock_connect.mock_calls) == 1
|
|
|
|
|
|
async def test_form_invalid_auth(hass: HomeAssistant):
|
|
"""Test that we can handle invalid credentials."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
assert result["type"] == "form"
|
|
assert result["errors"] == {}
|
|
|
|
with patch(
|
|
"homeassistant.components.jellyfin.client_wrapper.ConnectionManager.connect_to_address",
|
|
return_value=MOCK_SUCCESFUL_CONNECTION_STATE,
|
|
) as mock_connect, patch(
|
|
"homeassistant.components.jellyfin.client_wrapper.ConnectionManager.login",
|
|
return_value=MOCK_UNSUCCESFUL_LOGIN_RESPONSE,
|
|
) as mock_login:
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_URL: TEST_URL,
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert result2["type"] == "form"
|
|
assert result2["errors"] == {"base": "invalid_auth"}
|
|
|
|
assert len(mock_connect.mock_calls) == 1
|
|
assert len(mock_login.mock_calls) == 1
|
|
|
|
|
|
async def test_form_exception(hass: HomeAssistant):
|
|
"""Test we handle an unexpected exception during server setup."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
assert result["type"] == "form"
|
|
assert result["errors"] == {}
|
|
|
|
with patch(
|
|
"homeassistant.components.jellyfin.client_wrapper.ConnectionManager.connect_to_address",
|
|
side_effect=Exception("UnknownException"),
|
|
) as mock_connect:
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_URL: TEST_URL,
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
},
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert result2["type"] == "form"
|
|
assert result2["errors"] == {"base": "unknown"}
|
|
|
|
assert len(mock_connect.mock_calls) == 1
|