2019-09-19 21:29:26 +00:00
|
|
|
"""Tests for Plex config flow."""
|
2020-02-19 00:46:45 +00:00
|
|
|
import copy
|
2020-05-05 22:42:21 +00:00
|
|
|
import ssl
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2019-10-01 15:20:30 +00:00
|
|
|
|
2019-09-19 21:29:26 +00:00
|
|
|
import plexapi.exceptions
|
2021-04-30 21:15:59 +00:00
|
|
|
import pytest
|
2019-09-19 21:29:26 +00:00
|
|
|
import requests.exceptions
|
|
|
|
|
2020-03-11 16:37:02 +00:00
|
|
|
from homeassistant.components.media_player import DOMAIN as MP_DOMAIN
|
2019-09-19 21:29:26 +00:00
|
|
|
from homeassistant.components.plex import config_flow
|
2020-03-11 16:37:02 +00:00
|
|
|
from homeassistant.components.plex.const import (
|
2020-05-05 22:42:21 +00:00
|
|
|
AUTOMATIC_SETUP_STRING,
|
2020-03-11 16:37:02 +00:00
|
|
|
CONF_IGNORE_NEW_SHARED_USERS,
|
2020-05-08 16:49:15 +00:00
|
|
|
CONF_IGNORE_PLEX_WEB_CLIENTS,
|
2020-03-11 16:37:02 +00:00
|
|
|
CONF_MONITORED_USERS,
|
|
|
|
CONF_SERVER,
|
|
|
|
CONF_SERVER_IDENTIFIER,
|
|
|
|
CONF_USE_EPISODE_ART,
|
|
|
|
DOMAIN,
|
2020-05-05 22:42:21 +00:00
|
|
|
MANUAL_SETUP_STRING,
|
2020-03-11 16:37:02 +00:00
|
|
|
PLEX_SERVER_CONFIG,
|
|
|
|
SERVERS,
|
|
|
|
)
|
2020-05-13 13:11:00 +00:00
|
|
|
from homeassistant.config_entries import (
|
|
|
|
ENTRY_STATE_LOADED,
|
|
|
|
SOURCE_INTEGRATION_DISCOVERY,
|
2020-09-28 14:14:54 +00:00
|
|
|
SOURCE_REAUTH,
|
2021-04-25 09:27:40 +00:00
|
|
|
SOURCE_USER,
|
2020-05-13 13:11:00 +00:00
|
|
|
)
|
2020-05-05 22:42:21 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_SSL,
|
|
|
|
CONF_TOKEN,
|
|
|
|
CONF_URL,
|
|
|
|
CONF_VERIFY_SSL,
|
|
|
|
)
|
2021-01-07 18:56:52 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
from .const import DEFAULT_OPTIONS, MOCK_SERVERS, MOCK_TOKEN, PLEX_DIRECT_URL
|
2020-12-02 18:00:13 +00:00
|
|
|
from .helpers import trigger_plex_update, wait_for_debouncer
|
2021-01-07 18:56:52 +00:00
|
|
|
from .mock_classes import MockGDM
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2019-12-08 22:31:56 +00:00
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2021-04-30 21:15:59 +00:00
|
|
|
async def test_bad_credentials(hass, current_request_with_host):
|
2019-09-19 21:29:26 +00:00
|
|
|
"""Test when provided credentials are rejected."""
|
2019-10-14 04:59:25 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2019-10-14 04:59:25 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["step_id"] == "user"
|
2019-10-14 04:59:25 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"plexapi.myplex.MyPlexAccount", side_effect=plexapi.exceptions.Unauthorized
|
2020-03-11 16:37:02 +00:00
|
|
|
), patch("plexauth.PlexAuth.initiate_auth"), patch(
|
2019-10-07 18:29:12 +00:00
|
|
|
"plexauth.PlexAuth.token", return_value="BAD TOKEN"
|
2019-09-19 21:29:26 +00:00
|
|
|
):
|
2020-05-05 22:42:21 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
2019-10-21 23:29:04 +00:00
|
|
|
assert result["type"] == "external"
|
|
|
|
|
2019-10-07 18:29:12 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == "external_done"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
|
|
|
|
assert result["type"] == "form"
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"][CONF_TOKEN] == "faulty_credentials"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
|
|
|
|
2021-04-30 21:15:59 +00:00
|
|
|
async def test_bad_hostname(hass, mock_plex_calls, current_request_with_host):
|
2020-06-03 16:20:21 +00:00
|
|
|
"""Test when an invalid address is provided."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2020-06-03 16:20:21 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "user"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
|
|
|
with patch(
|
2021-01-07 18:56:52 +00:00
|
|
|
"plexapi.myplex.MyPlexResource.connect",
|
|
|
|
side_effect=requests.exceptions.ConnectionError,
|
|
|
|
), patch("plexauth.PlexAuth.initiate_auth"), patch(
|
2020-06-03 16:20:21 +00:00
|
|
|
"plexauth.PlexAuth.token", return_value=MOCK_TOKEN
|
2019-09-19 21:29:26 +00:00
|
|
|
):
|
2020-06-03 16:20:21 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
2019-09-19 21:29:26 +00:00
|
|
|
)
|
2020-06-03 16:20:21 +00:00
|
|
|
assert result["type"] == "external"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == "external_done"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"][CONF_HOST] == "not_found"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
|
|
|
|
2021-04-30 21:15:59 +00:00
|
|
|
async def test_unknown_exception(hass, current_request_with_host):
|
2019-09-19 21:29:26 +00:00
|
|
|
"""Test when an unknown exception is encountered."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2019-09-19 21:29:26 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["step_id"] == "user"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2020-03-11 16:37:02 +00:00
|
|
|
with patch("plexapi.myplex.MyPlexAccount", side_effect=Exception), patch(
|
2019-10-14 04:59:25 +00:00
|
|
|
"plexauth.PlexAuth.initiate_auth"
|
2020-03-11 16:37:02 +00:00
|
|
|
), patch("plexauth.PlexAuth.token", return_value="MOCK_TOKEN"):
|
2020-05-05 22:42:21 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
2019-10-21 23:29:04 +00:00
|
|
|
assert result["type"] == "external"
|
|
|
|
|
2019-10-07 18:29:12 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == "external_done"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
2019-09-19 21:29:26 +00:00
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "unknown"
|
|
|
|
|
|
|
|
|
2021-04-30 21:15:59 +00:00
|
|
|
async def test_no_servers_found(
|
|
|
|
hass, mock_plex_calls, requests_mock, empty_payload, current_request_with_host
|
|
|
|
):
|
2019-09-19 21:29:26 +00:00
|
|
|
"""Test when no servers are on an account."""
|
2021-01-07 18:56:52 +00:00
|
|
|
requests_mock.get("https://plex.tv/api/resources", text=empty_payload)
|
2019-09-19 21:29:26 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2019-09-19 21:29:26 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["step_id"] == "user"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
with patch("plexauth.PlexAuth.initiate_auth"), patch(
|
2019-10-01 15:20:30 +00:00
|
|
|
"plexauth.PlexAuth.token", return_value=MOCK_TOKEN
|
|
|
|
):
|
2020-05-05 22:42:21 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
2019-10-21 23:29:04 +00:00
|
|
|
assert result["type"] == "external"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2019-10-01 15:20:30 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == "external_done"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2019-10-01 15:20:30 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
2019-09-19 21:29:26 +00:00
|
|
|
assert result["type"] == "form"
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["step_id"] == "user"
|
2019-09-19 21:29:26 +00:00
|
|
|
assert result["errors"]["base"] == "no_servers"
|
|
|
|
|
|
|
|
|
2021-04-30 21:15:59 +00:00
|
|
|
async def test_single_available_server(
|
|
|
|
hass, mock_plex_calls, current_request_with_host
|
|
|
|
):
|
2019-09-19 21:29:26 +00:00
|
|
|
"""Test creating an entry with one server available."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2019-09-19 21:29:26 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["step_id"] == "user"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
with patch("plexauth.PlexAuth.initiate_auth"), patch(
|
2019-10-01 15:20:30 +00:00
|
|
|
"plexauth.PlexAuth.token", return_value=MOCK_TOKEN
|
|
|
|
):
|
2020-05-05 22:42:21 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
2019-10-21 23:29:04 +00:00
|
|
|
assert result["type"] == "external"
|
|
|
|
|
2019-10-01 15:20:30 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == "external_done"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2019-10-01 15:20:30 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
2019-09-19 21:29:26 +00:00
|
|
|
assert result["type"] == "create_entry"
|
2021-01-07 18:56:52 +00:00
|
|
|
|
|
|
|
server_id = result["data"][CONF_SERVER_IDENTIFIER]
|
|
|
|
mock_plex_server = hass.data[DOMAIN][SERVERS][server_id]
|
|
|
|
|
2021-02-24 19:57:02 +00:00
|
|
|
assert result["title"] == mock_plex_server.url_in_use
|
2021-01-07 18:56:52 +00:00
|
|
|
assert result["data"][CONF_SERVER] == mock_plex_server.friendly_name
|
|
|
|
assert (
|
|
|
|
result["data"][CONF_SERVER_IDENTIFIER]
|
|
|
|
== mock_plex_server.machine_identifier
|
|
|
|
)
|
2019-09-19 21:29:26 +00:00
|
|
|
assert (
|
2021-01-07 18:56:52 +00:00
|
|
|
result["data"][PLEX_SERVER_CONFIG][CONF_URL] == mock_plex_server.url_in_use
|
2019-09-19 21:29:26 +00:00
|
|
|
)
|
2020-03-11 16:37:02 +00:00
|
|
|
assert result["data"][PLEX_SERVER_CONFIG][CONF_TOKEN] == MOCK_TOKEN
|
2019-09-19 21:29:26 +00:00
|
|
|
|
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
async def test_multiple_servers_with_selection(
|
2021-04-30 21:15:59 +00:00
|
|
|
hass,
|
|
|
|
mock_plex_calls,
|
|
|
|
requests_mock,
|
|
|
|
plextv_resources_base,
|
|
|
|
current_request_with_host,
|
2021-01-07 18:56:52 +00:00
|
|
|
):
|
2019-09-19 21:29:26 +00:00
|
|
|
"""Test creating an entry with multiple servers available."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2019-09-19 21:29:26 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["step_id"] == "user"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
requests_mock.get(
|
|
|
|
"https://plex.tv/api/resources",
|
|
|
|
text=plextv_resources_base.format(second_server_enabled=1),
|
|
|
|
)
|
|
|
|
with patch("plexauth.PlexAuth.initiate_auth"), patch(
|
2019-10-01 15:20:30 +00:00
|
|
|
"plexauth.PlexAuth.token", return_value=MOCK_TOKEN
|
|
|
|
):
|
2020-05-05 22:42:21 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
2019-10-21 23:29:04 +00:00
|
|
|
assert result["type"] == "external"
|
|
|
|
|
2019-10-01 15:20:30 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == "external_done"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
2019-09-19 21:29:26 +00:00
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "select_server"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
|
|
|
user_input={CONF_SERVER: MOCK_SERVERS[0][CONF_SERVER]},
|
2019-09-19 21:29:26 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == "create_entry"
|
2021-01-07 18:56:52 +00:00
|
|
|
|
|
|
|
server_id = result["data"][CONF_SERVER_IDENTIFIER]
|
|
|
|
mock_plex_server = hass.data[DOMAIN][SERVERS][server_id]
|
|
|
|
|
2021-02-24 19:57:02 +00:00
|
|
|
assert result["title"] == mock_plex_server.url_in_use
|
2021-01-07 18:56:52 +00:00
|
|
|
assert result["data"][CONF_SERVER] == mock_plex_server.friendly_name
|
|
|
|
assert (
|
|
|
|
result["data"][CONF_SERVER_IDENTIFIER]
|
|
|
|
== mock_plex_server.machine_identifier
|
|
|
|
)
|
2019-09-19 21:29:26 +00:00
|
|
|
assert (
|
2021-01-07 18:56:52 +00:00
|
|
|
result["data"][PLEX_SERVER_CONFIG][CONF_URL] == mock_plex_server.url_in_use
|
2019-09-19 21:29:26 +00:00
|
|
|
)
|
2020-03-11 16:37:02 +00:00
|
|
|
assert result["data"][PLEX_SERVER_CONFIG][CONF_TOKEN] == MOCK_TOKEN
|
2019-09-19 21:29:26 +00:00
|
|
|
|
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
async def test_adding_last_unconfigured_server(
|
2021-04-30 21:15:59 +00:00
|
|
|
hass,
|
|
|
|
mock_plex_calls,
|
|
|
|
requests_mock,
|
|
|
|
plextv_resources_base,
|
|
|
|
current_request_with_host,
|
2021-01-07 18:56:52 +00:00
|
|
|
):
|
2019-09-19 21:29:26 +00:00
|
|
|
"""Test automatically adding last unconfigured server when multiple servers on account."""
|
|
|
|
MockConfigEntry(
|
2020-03-11 16:37:02 +00:00
|
|
|
domain=DOMAIN,
|
2019-09-19 21:29:26 +00:00
|
|
|
data={
|
2020-03-11 16:37:02 +00:00
|
|
|
CONF_SERVER_IDENTIFIER: MOCK_SERVERS[1][CONF_SERVER_IDENTIFIER],
|
|
|
|
CONF_SERVER: MOCK_SERVERS[1][CONF_SERVER],
|
2019-09-19 21:29:26 +00:00
|
|
|
},
|
|
|
|
).add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2019-09-19 21:29:26 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["step_id"] == "user"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
requests_mock.get(
|
|
|
|
"https://plex.tv/api/resources",
|
|
|
|
text=plextv_resources_base.format(second_server_enabled=1),
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch("plexauth.PlexAuth.initiate_auth"), patch(
|
2019-10-01 15:20:30 +00:00
|
|
|
"plexauth.PlexAuth.token", return_value=MOCK_TOKEN
|
|
|
|
):
|
2020-05-05 22:42:21 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
2019-10-21 23:29:04 +00:00
|
|
|
assert result["type"] == "external"
|
|
|
|
|
2019-10-01 15:20:30 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == "external_done"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2019-10-01 15:20:30 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
2019-09-19 21:29:26 +00:00
|
|
|
assert result["type"] == "create_entry"
|
2021-01-07 18:56:52 +00:00
|
|
|
|
|
|
|
server_id = result["data"][CONF_SERVER_IDENTIFIER]
|
|
|
|
mock_plex_server = hass.data[DOMAIN][SERVERS][server_id]
|
|
|
|
|
2021-02-24 19:57:02 +00:00
|
|
|
assert result["title"] == mock_plex_server.url_in_use
|
2021-01-07 18:56:52 +00:00
|
|
|
assert result["data"][CONF_SERVER] == mock_plex_server.friendly_name
|
|
|
|
assert (
|
|
|
|
result["data"][CONF_SERVER_IDENTIFIER]
|
|
|
|
== mock_plex_server.machine_identifier
|
|
|
|
)
|
2019-09-19 21:29:26 +00:00
|
|
|
assert (
|
2021-01-07 18:56:52 +00:00
|
|
|
result["data"][PLEX_SERVER_CONFIG][CONF_URL] == mock_plex_server.url_in_use
|
2019-09-19 21:29:26 +00:00
|
|
|
)
|
2020-03-11 16:37:02 +00:00
|
|
|
assert result["data"][PLEX_SERVER_CONFIG][CONF_TOKEN] == MOCK_TOKEN
|
2019-09-19 21:29:26 +00:00
|
|
|
|
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
async def test_all_available_servers_configured(
|
2021-04-30 21:15:59 +00:00
|
|
|
hass,
|
|
|
|
entry,
|
|
|
|
requests_mock,
|
|
|
|
plextv_account,
|
|
|
|
plextv_resources_base,
|
|
|
|
current_request_with_host,
|
2021-01-07 18:56:52 +00:00
|
|
|
):
|
2019-09-19 21:29:26 +00:00
|
|
|
"""Test when all available servers are already configured."""
|
2021-01-07 18:56:52 +00:00
|
|
|
entry.add_to_hass(hass)
|
2019-09-19 21:29:26 +00:00
|
|
|
|
|
|
|
MockConfigEntry(
|
2020-03-11 16:37:02 +00:00
|
|
|
domain=DOMAIN,
|
2019-09-19 21:29:26 +00:00
|
|
|
data={
|
2020-03-11 16:37:02 +00:00
|
|
|
CONF_SERVER_IDENTIFIER: MOCK_SERVERS[1][CONF_SERVER_IDENTIFIER],
|
|
|
|
CONF_SERVER: MOCK_SERVERS[1][CONF_SERVER],
|
2019-09-19 21:29:26 +00:00
|
|
|
},
|
|
|
|
).add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2019-09-19 21:29:26 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["step_id"] == "user"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
requests_mock.get("https://plex.tv/users/account", text=plextv_account)
|
|
|
|
requests_mock.get(
|
|
|
|
"https://plex.tv/api/resources",
|
|
|
|
text=plextv_resources_base.format(second_server_enabled=1),
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch("plexauth.PlexAuth.initiate_auth"), patch(
|
2019-10-01 15:20:30 +00:00
|
|
|
"plexauth.PlexAuth.token", return_value=MOCK_TOKEN
|
|
|
|
):
|
2020-05-05 22:42:21 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
2019-10-21 23:29:04 +00:00
|
|
|
assert result["type"] == "external"
|
|
|
|
|
2019-10-01 15:20:30 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == "external_done"
|
2019-09-19 21:29:26 +00:00
|
|
|
|
2019-10-01 15:20:30 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
2019-09-19 21:29:26 +00:00
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "all_configured"
|
2019-09-22 22:23:14 +00:00
|
|
|
|
|
|
|
|
2020-09-23 03:14:41 +00:00
|
|
|
async def test_option_flow(hass, entry, mock_plex_server):
|
2020-02-19 00:46:45 +00:00
|
|
|
"""Test config options flow selection."""
|
2020-06-11 22:45:00 +00:00
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
|
|
|
assert entry.state == ENTRY_STATE_LOADED
|
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_init(
|
|
|
|
entry.entry_id, context={"source": "test"}, data=None
|
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "plex_mp_settings"
|
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_USE_EPISODE_ART: True,
|
|
|
|
CONF_IGNORE_NEW_SHARED_USERS: True,
|
|
|
|
CONF_MONITORED_USERS: list(mock_plex_server.accounts),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert result["type"] == "create_entry"
|
|
|
|
assert result["data"] == {
|
|
|
|
MP_DOMAIN: {
|
|
|
|
CONF_USE_EPISODE_ART: True,
|
|
|
|
CONF_IGNORE_NEW_SHARED_USERS: True,
|
|
|
|
CONF_MONITORED_USERS: {
|
|
|
|
user: {"enabled": True} for user in mock_plex_server.accounts
|
|
|
|
},
|
|
|
|
CONF_IGNORE_PLEX_WEB_CLIENTS: False,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-23 03:14:41 +00:00
|
|
|
async def test_missing_option_flow(hass, entry, mock_plex_server):
|
2020-06-11 22:45:00 +00:00
|
|
|
"""Test config options flow selection when no options stored."""
|
2020-03-11 16:37:02 +00:00
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
|
|
|
assert entry.state == ENTRY_STATE_LOADED
|
2020-02-19 00:46:45 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_init(
|
|
|
|
entry.entry_id, context={"source": "test"}, data=None
|
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "plex_mp_settings"
|
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
2020-03-11 16:37:02 +00:00
|
|
|
CONF_USE_EPISODE_ART: True,
|
|
|
|
CONF_IGNORE_NEW_SHARED_USERS: True,
|
|
|
|
CONF_MONITORED_USERS: list(mock_plex_server.accounts),
|
2020-02-19 00:46:45 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
assert result["type"] == "create_entry"
|
|
|
|
assert result["data"] == {
|
2020-03-11 16:37:02 +00:00
|
|
|
MP_DOMAIN: {
|
|
|
|
CONF_USE_EPISODE_ART: True,
|
|
|
|
CONF_IGNORE_NEW_SHARED_USERS: True,
|
|
|
|
CONF_MONITORED_USERS: {
|
2020-02-19 00:46:45 +00:00
|
|
|
user: {"enabled": True} for user in mock_plex_server.accounts
|
|
|
|
},
|
2020-05-08 16:49:15 +00:00
|
|
|
CONF_IGNORE_PLEX_WEB_CLIENTS: False,
|
2020-02-19 00:46:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
async def test_option_flow_new_users_available(hass, entry, setup_plex_server):
|
2020-03-11 16:37:02 +00:00
|
|
|
"""Test config options multiselect defaults when new Plex users are seen."""
|
|
|
|
OPTIONS_OWNER_ONLY = copy.deepcopy(DEFAULT_OPTIONS)
|
2021-01-07 18:56:52 +00:00
|
|
|
OPTIONS_OWNER_ONLY[MP_DOMAIN][CONF_MONITORED_USERS] = {"User 1": {"enabled": True}}
|
2020-09-23 03:14:41 +00:00
|
|
|
entry.options = OPTIONS_OWNER_ONLY
|
2020-02-19 00:46:45 +00:00
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
mock_plex_server = await setup_plex_server(config_entry=entry)
|
|
|
|
await hass.async_block_till_done()
|
2020-02-19 00:46:45 +00:00
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
server_id = mock_plex_server.machine_identifier
|
2020-03-11 16:37:02 +00:00
|
|
|
monitored_users = hass.data[DOMAIN][SERVERS][server_id].option_monitored_users
|
2020-02-19 00:46:45 +00:00
|
|
|
|
2020-03-11 16:37:02 +00:00
|
|
|
new_users = [x for x in mock_plex_server.accounts if x not in monitored_users]
|
|
|
|
assert len(monitored_users) == 1
|
|
|
|
assert len(new_users) == 2
|
2020-02-19 00:46:45 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_init(
|
|
|
|
entry.entry_id, context={"source": "test"}, data=None
|
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "plex_mp_settings"
|
2020-03-11 16:37:02 +00:00
|
|
|
multiselect_defaults = result["data_schema"].schema["monitored_users"].options
|
2020-02-19 00:46:45 +00:00
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
assert "[Owner]" in multiselect_defaults["User 1"]
|
2020-03-11 16:37:02 +00:00
|
|
|
for user in new_users:
|
|
|
|
assert "[New]" in multiselect_defaults[user]
|
2019-10-01 15:20:30 +00:00
|
|
|
|
|
|
|
|
2021-04-30 21:15:59 +00:00
|
|
|
async def test_external_timed_out(hass, current_request_with_host):
|
2019-10-01 15:20:30 +00:00
|
|
|
"""Test when external flow times out."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2019-10-01 15:20:30 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["step_id"] == "user"
|
2019-10-01 15:20:30 +00:00
|
|
|
|
2020-03-11 16:37:02 +00:00
|
|
|
with patch("plexauth.PlexAuth.initiate_auth"), patch(
|
2019-10-01 15:20:30 +00:00
|
|
|
"plexauth.PlexAuth.token", return_value=None
|
|
|
|
):
|
2020-05-05 22:42:21 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
2019-10-21 23:29:04 +00:00
|
|
|
assert result["type"] == "external"
|
|
|
|
|
2019-10-01 15:20:30 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == "external_done"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "token_request_timeout"
|
|
|
|
|
|
|
|
|
2021-04-30 21:15:59 +00:00
|
|
|
async def test_callback_view(hass, aiohttp_client, current_request_with_host):
|
2019-10-01 15:20:30 +00:00
|
|
|
"""Test callback view."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2019-10-01 15:20:30 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["step_id"] == "user"
|
2019-10-01 15:20:30 +00:00
|
|
|
|
2020-03-11 16:37:02 +00:00
|
|
|
with patch("plexauth.PlexAuth.initiate_auth"), patch(
|
2019-10-01 15:20:30 +00:00
|
|
|
"plexauth.PlexAuth.token", return_value=MOCK_TOKEN
|
|
|
|
):
|
2020-05-05 22:42:21 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
2019-10-21 23:29:04 +00:00
|
|
|
assert result["type"] == "external"
|
|
|
|
|
2019-10-01 15:20:30 +00:00
|
|
|
client = await aiohttp_client(hass.http.app)
|
|
|
|
forward_url = f'{config_flow.AUTH_CALLBACK_PATH}?flow_id={result["flow_id"]}'
|
|
|
|
|
|
|
|
resp = await client.get(forward_url)
|
|
|
|
assert resp.status == 200
|
2019-12-01 06:07:12 +00:00
|
|
|
|
|
|
|
|
2021-04-30 21:15:59 +00:00
|
|
|
async def test_manual_config(hass, mock_plex_calls, current_request_with_host):
|
2020-05-05 22:42:21 +00:00
|
|
|
"""Test creating via manual configuration."""
|
|
|
|
|
|
|
|
class WrongCertValidaitionException(requests.exceptions.SSLError):
|
|
|
|
"""Mock the exception showing an unmatched error."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.__context__ = ssl.SSLCertVerificationError(
|
|
|
|
"some random message that doesn't match"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Basic mode
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
config_flow.DOMAIN, context={"source": SOURCE_USER}
|
2020-05-05 22:42:21 +00:00
|
|
|
)
|
|
|
|
|
2020-06-02 22:37:10 +00:00
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "user"
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["data_schema"] is None
|
|
|
|
hass.config_entries.flow.async_abort(result["flow_id"])
|
|
|
|
|
|
|
|
# Advanced automatic
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
config_flow.DOMAIN,
|
|
|
|
context={"source": SOURCE_USER, "show_advanced_options": True},
|
2020-05-05 22:42:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result["data_schema"] is not None
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "user_advanced"
|
|
|
|
|
2020-06-02 22:37:10 +00:00
|
|
|
with patch("plexauth.PlexAuth.initiate_auth"):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={"setup_method": AUTOMATIC_SETUP_STRING}
|
|
|
|
)
|
2020-05-05 22:42:21 +00:00
|
|
|
|
|
|
|
assert result["type"] == "external"
|
|
|
|
hass.config_entries.flow.async_abort(result["flow_id"])
|
|
|
|
|
|
|
|
# Advanced manual
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
config_flow.DOMAIN,
|
|
|
|
context={"source": SOURCE_USER, "show_advanced_options": True},
|
2020-05-05 22:42:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result["data_schema"] is not None
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "user_advanced"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={"setup_method": MANUAL_SETUP_STRING}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "manual_setup"
|
|
|
|
|
|
|
|
MANUAL_SERVER = {
|
|
|
|
CONF_HOST: MOCK_SERVERS[0][CONF_HOST],
|
|
|
|
CONF_PORT: MOCK_SERVERS[0][CONF_PORT],
|
|
|
|
CONF_SSL: False,
|
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
CONF_TOKEN: MOCK_TOKEN,
|
|
|
|
}
|
|
|
|
|
|
|
|
MANUAL_SERVER_NO_HOST_OR_TOKEN = {
|
|
|
|
CONF_PORT: MOCK_SERVERS[0][CONF_PORT],
|
|
|
|
CONF_SSL: False,
|
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
}
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input=MANUAL_SERVER_NO_HOST_OR_TOKEN
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "manual_setup"
|
|
|
|
assert result["errors"]["base"] == "host_or_token"
|
|
|
|
|
|
|
|
with patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"plexapi.server.PlexServer",
|
|
|
|
side_effect=requests.exceptions.SSLError,
|
2020-05-05 22:42:21 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input=MANUAL_SERVER
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "manual_setup"
|
|
|
|
assert result["errors"]["base"] == "ssl_error"
|
|
|
|
|
|
|
|
with patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"plexapi.server.PlexServer",
|
|
|
|
side_effect=WrongCertValidaitionException,
|
2020-05-05 22:42:21 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input=MANUAL_SERVER
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "manual_setup"
|
|
|
|
assert result["errors"]["base"] == "ssl_error"
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.plex.PlexServer.connect",
|
|
|
|
side_effect=requests.exceptions.SSLError,
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input=MANUAL_SERVER
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "manual_setup"
|
|
|
|
assert result["errors"]["base"] == "ssl_error"
|
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
with patch("homeassistant.components.plex.PlexWebsocket", autospec=True), patch(
|
2020-10-14 13:46:52 +00:00
|
|
|
"homeassistant.components.plex.GDM", return_value=MockGDM(disabled=True)
|
|
|
|
):
|
2020-05-05 22:42:21 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input=MANUAL_SERVER
|
|
|
|
)
|
2021-03-31 11:57:16 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-05 22:42:21 +00:00
|
|
|
|
|
|
|
assert result["type"] == "create_entry"
|
2021-01-07 18:56:52 +00:00
|
|
|
|
|
|
|
server_id = result["data"][CONF_SERVER_IDENTIFIER]
|
|
|
|
mock_plex_server = hass.data[DOMAIN][SERVERS][server_id]
|
|
|
|
|
2021-02-24 19:57:02 +00:00
|
|
|
assert result["title"] == mock_plex_server.url_in_use
|
2021-01-07 18:56:52 +00:00
|
|
|
assert result["data"][CONF_SERVER] == mock_plex_server.friendly_name
|
|
|
|
assert result["data"][CONF_SERVER_IDENTIFIER] == mock_plex_server.machine_identifier
|
|
|
|
assert result["data"][PLEX_SERVER_CONFIG][CONF_URL] == mock_plex_server.url_in_use
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["data"][PLEX_SERVER_CONFIG][CONF_TOKEN] == MOCK_TOKEN
|
|
|
|
|
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
async def test_manual_config_with_token(hass, mock_plex_calls):
|
2020-05-05 22:42:21 +00:00
|
|
|
"""Test creating via manual configuration with only token."""
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
config_flow.DOMAIN,
|
|
|
|
context={"source": SOURCE_USER, "show_advanced_options": True},
|
2020-05-05 22:42:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "user_advanced"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={"setup_method": MANUAL_SETUP_STRING}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "manual_setup"
|
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
with patch(
|
2020-10-14 13:46:52 +00:00
|
|
|
"homeassistant.components.plex.GDM", return_value=MockGDM(disabled=True)
|
2021-01-07 18:56:52 +00:00
|
|
|
), patch("homeassistant.components.plex.PlexWebsocket", autospec=True):
|
2020-05-05 22:42:21 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_TOKEN: MOCK_TOKEN}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == "create_entry"
|
2021-01-07 18:56:52 +00:00
|
|
|
|
|
|
|
server_id = result["data"][CONF_SERVER_IDENTIFIER]
|
|
|
|
mock_plex_server = hass.data[DOMAIN][SERVERS][server_id]
|
|
|
|
|
2021-02-24 19:57:02 +00:00
|
|
|
assert result["title"] == mock_plex_server.url_in_use
|
2021-01-07 18:56:52 +00:00
|
|
|
assert result["data"][CONF_SERVER] == mock_plex_server.friendly_name
|
|
|
|
assert result["data"][CONF_SERVER_IDENTIFIER] == mock_plex_server.machine_identifier
|
|
|
|
assert result["data"][PLEX_SERVER_CONFIG][CONF_URL] == mock_plex_server.url_in_use
|
2020-05-05 22:42:21 +00:00
|
|
|
assert result["data"][PLEX_SERVER_CONFIG][CONF_TOKEN] == MOCK_TOKEN
|
2020-05-07 01:46:00 +00:00
|
|
|
|
|
|
|
|
2020-09-23 03:14:41 +00:00
|
|
|
async def test_setup_with_limited_credentials(hass, entry, setup_plex_server):
|
2020-05-07 01:46:00 +00:00
|
|
|
"""Test setup with a user with limited permissions."""
|
2021-01-07 18:56:52 +00:00
|
|
|
with patch(
|
|
|
|
"plexapi.server.PlexServer.systemAccounts",
|
|
|
|
side_effect=plexapi.exceptions.Unauthorized,
|
2020-09-23 03:14:41 +00:00
|
|
|
) as mock_accounts:
|
|
|
|
mock_plex_server = await setup_plex_server()
|
2020-05-07 01:46:00 +00:00
|
|
|
|
|
|
|
assert mock_accounts.called
|
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
plex_server = hass.data[DOMAIN][SERVERS][mock_plex_server.machine_identifier]
|
2020-05-07 01:46:00 +00:00
|
|
|
assert len(plex_server.accounts) == 0
|
|
|
|
assert plex_server.owner is None
|
|
|
|
|
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
|
|
|
assert entry.state == ENTRY_STATE_LOADED
|
2020-05-13 13:11:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_integration_discovery(hass):
|
|
|
|
"""Test integration self-discovery."""
|
|
|
|
mock_gdm = MockGDM()
|
|
|
|
|
|
|
|
with patch("homeassistant.components.plex.config_flow.GDM", return_value=mock_gdm):
|
|
|
|
await config_flow.async_discover(hass)
|
|
|
|
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
|
|
|
|
assert len(flows) == 1
|
|
|
|
|
|
|
|
flow = flows[0]
|
|
|
|
|
|
|
|
assert flow["handler"] == DOMAIN
|
|
|
|
assert flow["context"]["source"] == SOURCE_INTEGRATION_DISCOVERY
|
|
|
|
assert (
|
|
|
|
flow["context"]["unique_id"]
|
|
|
|
== mock_gdm.entries[0]["data"]["Resource-Identifier"]
|
|
|
|
)
|
|
|
|
assert flow["step_id"] == "user"
|
2020-09-28 14:14:54 +00:00
|
|
|
|
|
|
|
|
2021-04-30 21:15:59 +00:00
|
|
|
async def test_trigger_reauth(
|
|
|
|
hass, entry, mock_plex_server, mock_websocket, current_request_with_host
|
|
|
|
):
|
2020-09-28 14:14:54 +00:00
|
|
|
"""Test setup and reauthorization of a Plex token."""
|
2021-01-07 18:56:52 +00:00
|
|
|
await async_setup_component(hass, "persistent_notification", {})
|
2020-09-28 14:14:54 +00:00
|
|
|
|
|
|
|
assert entry.state == ENTRY_STATE_LOADED
|
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
with patch(
|
|
|
|
"plexapi.server.PlexServer.clients", side_effect=plexapi.exceptions.Unauthorized
|
2020-09-28 14:14:54 +00:00
|
|
|
), patch("plexapi.server.PlexServer", side_effect=plexapi.exceptions.Unauthorized):
|
|
|
|
trigger_plex_update(mock_websocket)
|
2020-12-02 18:00:13 +00:00
|
|
|
await wait_for_debouncer(hass)
|
2020-09-28 14:14:54 +00:00
|
|
|
|
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
|
|
|
assert entry.state != ENTRY_STATE_LOADED
|
|
|
|
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
assert len(flows) == 1
|
|
|
|
assert flows[0]["context"]["source"] == SOURCE_REAUTH
|
|
|
|
|
|
|
|
flow_id = flows[0]["flow_id"]
|
|
|
|
|
2021-01-07 18:56:52 +00:00
|
|
|
with patch("plexauth.PlexAuth.initiate_auth"), patch(
|
2020-09-28 14:14:54 +00:00
|
|
|
"plexauth.PlexAuth.token", return_value="BRAND_NEW_TOKEN"
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_configure(flow_id, user_input={})
|
|
|
|
assert result["type"] == "external"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == "external_done"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
assert result["flow_id"] == flow_id
|
|
|
|
|
|
|
|
assert len(hass.config_entries.flow.async_progress()) == 0
|
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
|
|
|
|
|
|
|
assert entry.state == ENTRY_STATE_LOADED
|
2021-01-07 18:56:52 +00:00
|
|
|
assert entry.data[CONF_SERVER] == mock_plex_server.friendly_name
|
|
|
|
assert entry.data[CONF_SERVER_IDENTIFIER] == mock_plex_server.machine_identifier
|
|
|
|
assert entry.data[PLEX_SERVER_CONFIG][CONF_URL] == PLEX_DIRECT_URL
|
2020-09-28 14:14:54 +00:00
|
|
|
assert entry.data[PLEX_SERVER_CONFIG][CONF_TOKEN] == "BRAND_NEW_TOKEN"
|
2021-04-30 21:15:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_client_request_missing(hass):
|
|
|
|
"""Test when client headers are not set properly."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
with patch("plexauth.PlexAuth.initiate_auth"), patch(
|
|
|
|
"plexauth.PlexAuth.token", return_value=None
|
|
|
|
):
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_client_header_issues(hass, current_request_with_host):
|
|
|
|
"""Test when client headers are not set properly."""
|
|
|
|
|
|
|
|
class MockRequest:
|
|
|
|
headers = {}
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
with patch("plexauth.PlexAuth.initiate_auth"), patch(
|
|
|
|
"plexauth.PlexAuth.token", return_value=None
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.http.current_request.get", return_value=MockRequest()
|
|
|
|
):
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={}
|
|
|
|
)
|