2020-03-11 16:37:02 +00:00
|
|
|
"""Tests for Plex server."""
|
|
|
|
import copy
|
|
|
|
|
2020-09-04 09:32:36 +00:00
|
|
|
from plexapi.exceptions import BadRequest, NotFound
|
2020-06-26 06:24:58 +00:00
|
|
|
from requests.exceptions import RequestException
|
2020-05-26 19:39:56 +00:00
|
|
|
|
2020-03-11 16:37:02 +00:00
|
|
|
from homeassistant.components.media_player import DOMAIN as MP_DOMAIN
|
2020-05-26 19:39:56 +00:00
|
|
|
from homeassistant.components.media_player.const import (
|
|
|
|
ATTR_MEDIA_CONTENT_ID,
|
|
|
|
ATTR_MEDIA_CONTENT_TYPE,
|
|
|
|
MEDIA_TYPE_EPISODE,
|
2020-09-04 09:32:36 +00:00
|
|
|
MEDIA_TYPE_MOVIE,
|
2020-05-26 19:39:56 +00:00
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
MEDIA_TYPE_PLAYLIST,
|
|
|
|
MEDIA_TYPE_VIDEO,
|
|
|
|
SERVICE_PLAY_MEDIA,
|
|
|
|
)
|
2020-03-11 16:37:02 +00:00
|
|
|
from homeassistant.components.plex.const import (
|
|
|
|
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,
|
2020-06-26 06:24:58 +00:00
|
|
|
CONF_SERVER,
|
2020-03-11 16:37:02 +00:00
|
|
|
DOMAIN,
|
|
|
|
SERVERS,
|
|
|
|
)
|
2020-05-26 19:39:56 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID
|
2020-03-11 16:37:02 +00:00
|
|
|
|
|
|
|
from .const import DEFAULT_DATA, DEFAULT_OPTIONS
|
2020-06-26 06:24:58 +00:00
|
|
|
from .helpers import trigger_plex_update
|
2020-05-26 19:39:56 +00:00
|
|
|
from .mock_classes import (
|
2020-06-17 19:04:47 +00:00
|
|
|
MockPlexAccount,
|
2020-08-26 21:24:44 +00:00
|
|
|
MockPlexAlbum,
|
2020-05-26 19:39:56 +00:00
|
|
|
MockPlexArtist,
|
|
|
|
MockPlexLibrary,
|
|
|
|
MockPlexLibrarySection,
|
2020-09-04 09:32:36 +00:00
|
|
|
MockPlexMediaItem,
|
2020-08-26 21:24:44 +00:00
|
|
|
MockPlexSeason,
|
2020-05-26 19:39:56 +00:00
|
|
|
MockPlexServer,
|
2020-08-26 21:24:44 +00:00
|
|
|
MockPlexShow,
|
2020-05-26 19:39:56 +00:00
|
|
|
)
|
2020-03-11 16:37:02 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
from tests.async_mock import patch
|
|
|
|
from tests.common import MockConfigEntry
|
2020-03-11 16:37:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_new_users_available(hass):
|
|
|
|
"""Test setting up when new users available on Plex server."""
|
|
|
|
|
|
|
|
MONITORED_USERS = {"Owner": {"enabled": True}}
|
|
|
|
OPTIONS_WITH_USERS = copy.deepcopy(DEFAULT_OPTIONS)
|
|
|
|
OPTIONS_WITH_USERS[MP_DOMAIN][CONF_MONITORED_USERS] = MONITORED_USERS
|
|
|
|
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=DEFAULT_DATA,
|
|
|
|
options=OPTIONS_WITH_USERS,
|
|
|
|
unique_id=DEFAULT_DATA["server_id"],
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_plex_server = MockPlexServer(config_entry=entry)
|
|
|
|
|
|
|
|
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
2020-06-26 00:10:40 +00:00
|
|
|
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()
|
2020-06-27 08:03:51 +00:00
|
|
|
), patch(
|
|
|
|
"homeassistant.components.plex.PlexWebsocket", autospec=True
|
|
|
|
) as mock_websocket:
|
2020-03-11 16:37:02 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
server_id = mock_plex_server.machineIdentifier
|
|
|
|
|
2020-06-27 08:03:51 +00:00
|
|
|
trigger_plex_update(mock_websocket)
|
2020-06-26 00:10:40 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-03-11 16:37:02 +00:00
|
|
|
|
|
|
|
monitored_users = hass.data[DOMAIN][SERVERS][server_id].option_monitored_users
|
|
|
|
|
|
|
|
ignored_users = [x for x in monitored_users if not monitored_users[x]["enabled"]]
|
|
|
|
assert len(monitored_users) == 1
|
|
|
|
assert len(ignored_users) == 0
|
|
|
|
|
|
|
|
sensor = hass.states.get("sensor.plex_plex_server_1")
|
|
|
|
assert sensor.state == str(len(mock_plex_server.accounts))
|
|
|
|
|
|
|
|
|
|
|
|
async def test_new_ignored_users_available(hass, caplog):
|
|
|
|
"""Test setting up when new users available on Plex server but are ignored."""
|
|
|
|
|
|
|
|
MONITORED_USERS = {"Owner": {"enabled": True}}
|
|
|
|
OPTIONS_WITH_USERS = copy.deepcopy(DEFAULT_OPTIONS)
|
|
|
|
OPTIONS_WITH_USERS[MP_DOMAIN][CONF_MONITORED_USERS] = MONITORED_USERS
|
|
|
|
OPTIONS_WITH_USERS[MP_DOMAIN][CONF_IGNORE_NEW_SHARED_USERS] = True
|
|
|
|
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=DEFAULT_DATA,
|
|
|
|
options=OPTIONS_WITH_USERS,
|
|
|
|
unique_id=DEFAULT_DATA["server_id"],
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_plex_server = MockPlexServer(config_entry=entry)
|
|
|
|
|
|
|
|
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
2020-06-26 00:10:40 +00:00
|
|
|
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()
|
2020-06-27 08:03:51 +00:00
|
|
|
), patch(
|
|
|
|
"homeassistant.components.plex.PlexWebsocket", autospec=True
|
|
|
|
) as mock_websocket:
|
2020-03-11 16:37:02 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
server_id = mock_plex_server.machineIdentifier
|
|
|
|
|
2020-06-27 08:03:51 +00:00
|
|
|
trigger_plex_update(mock_websocket)
|
2020-06-26 00:10:40 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-03-11 16:37:02 +00:00
|
|
|
|
|
|
|
monitored_users = hass.data[DOMAIN][SERVERS][server_id].option_monitored_users
|
|
|
|
|
|
|
|
ignored_users = [x for x in mock_plex_server.accounts if x not in monitored_users]
|
|
|
|
assert len(monitored_users) == 1
|
|
|
|
assert len(ignored_users) == 2
|
|
|
|
for ignored_user in ignored_users:
|
2020-04-24 01:12:39 +00:00
|
|
|
ignored_client = [
|
|
|
|
x.players[0]
|
|
|
|
for x in mock_plex_server.sessions()
|
2020-05-08 16:49:15 +00:00
|
|
|
if x.usernames[0] == ignored_user
|
2020-04-24 01:12:39 +00:00
|
|
|
][0]
|
|
|
|
assert (
|
|
|
|
f"Ignoring {ignored_client.product} client owned by '{ignored_user}'"
|
|
|
|
in caplog.text
|
|
|
|
)
|
2020-03-11 16:37:02 +00:00
|
|
|
|
|
|
|
sensor = hass.states.get("sensor.plex_plex_server_1")
|
|
|
|
assert sensor.state == str(len(mock_plex_server.accounts))
|
|
|
|
|
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
async def test_network_error_during_refresh(hass, caplog):
|
|
|
|
"""Test network failures during refreshes."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=DEFAULT_DATA,
|
|
|
|
options=DEFAULT_OPTIONS,
|
|
|
|
unique_id=DEFAULT_DATA["server_id"],
|
|
|
|
)
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
mock_plex_server = MockPlexServer()
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
|
|
|
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()
|
2020-06-27 08:03:51 +00:00
|
|
|
), patch(
|
|
|
|
"homeassistant.components.plex.PlexWebsocket", autospec=True
|
|
|
|
) as mock_websocket:
|
2020-06-26 06:24:58 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
server_id = mock_plex_server.machineIdentifier
|
|
|
|
loaded_server = hass.data[DOMAIN][SERVERS][server_id]
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-27 08:03:51 +00:00
|
|
|
trigger_plex_update(mock_websocket)
|
2020-06-26 06:24:58 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
sensor = hass.states.get("sensor.plex_plex_server_1")
|
|
|
|
assert sensor.state == str(len(mock_plex_server.accounts))
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
with patch.object(mock_plex_server, "clients", side_effect=RequestException):
|
|
|
|
await loaded_server._async_update_platforms()
|
|
|
|
await hass.async_block_till_done()
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
assert (
|
|
|
|
f"Could not connect to Plex server: {DEFAULT_DATA[CONF_SERVER]}" in caplog.text
|
|
|
|
)
|
2020-04-30 20:29:50 +00:00
|
|
|
|
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
async def test_mark_sessions_idle(hass):
|
|
|
|
"""Test marking media_players as idle when sessions end."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=DEFAULT_DATA,
|
|
|
|
options=DEFAULT_OPTIONS,
|
|
|
|
unique_id=DEFAULT_DATA["server_id"],
|
|
|
|
)
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
mock_plex_server = MockPlexServer()
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
|
|
|
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()
|
2020-06-27 08:03:51 +00:00
|
|
|
), patch(
|
|
|
|
"homeassistant.components.plex.PlexWebsocket", autospec=True
|
|
|
|
) as mock_websocket:
|
2020-06-26 06:24:58 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
server_id = mock_plex_server.machineIdentifier
|
|
|
|
loaded_server = hass.data[DOMAIN][SERVERS][server_id]
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-27 08:03:51 +00:00
|
|
|
trigger_plex_update(mock_websocket)
|
2020-06-26 06:24:58 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
sensor = hass.states.get("sensor.plex_plex_server_1")
|
|
|
|
assert sensor.state == str(len(mock_plex_server.accounts))
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
mock_plex_server.clear_clients()
|
|
|
|
mock_plex_server.clear_sessions()
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
await loaded_server._async_update_platforms()
|
|
|
|
await hass.async_block_till_done()
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2020-06-26 06:24:58 +00:00
|
|
|
sensor = hass.states.get("sensor.plex_plex_server_1")
|
|
|
|
assert sensor.state == "0"
|
2020-05-08 16:49:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_ignore_plex_web_client(hass):
|
|
|
|
"""Test option to ignore Plex Web clients."""
|
|
|
|
|
|
|
|
OPTIONS = copy.deepcopy(DEFAULT_OPTIONS)
|
|
|
|
OPTIONS[MP_DOMAIN][CONF_IGNORE_PLEX_WEB_CLIENTS] = True
|
|
|
|
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=DEFAULT_DATA,
|
|
|
|
options=OPTIONS,
|
|
|
|
unique_id=DEFAULT_DATA["server_id"],
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_plex_server = MockPlexServer(config_entry=entry)
|
|
|
|
|
|
|
|
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
2020-06-26 00:10:40 +00:00
|
|
|
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount(players=0)
|
2020-06-27 08:03:51 +00:00
|
|
|
), patch(
|
|
|
|
"homeassistant.components.plex.PlexWebsocket", autospec=True
|
|
|
|
) as mock_websocket:
|
2020-05-08 16:49:15 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-06-27 08:03:51 +00:00
|
|
|
trigger_plex_update(mock_websocket)
|
2020-06-26 00:10:40 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-08 16:49:15 +00:00
|
|
|
|
|
|
|
sensor = hass.states.get("sensor.plex_plex_server_1")
|
|
|
|
assert sensor.state == str(len(mock_plex_server.accounts))
|
|
|
|
|
|
|
|
media_players = hass.states.async_entity_ids("media_player")
|
|
|
|
|
|
|
|
assert len(media_players) == int(sensor.state) - 1
|
2020-05-26 19:39:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_media_lookups(hass):
|
|
|
|
"""Test media lookups to Plex server."""
|
|
|
|
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=DEFAULT_DATA,
|
|
|
|
options=DEFAULT_OPTIONS,
|
|
|
|
unique_id=DEFAULT_DATA["server_id"],
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_plex_server = MockPlexServer(config_entry=entry)
|
|
|
|
|
|
|
|
with patch("plexapi.server.PlexServer", return_value=mock_plex_server), patch(
|
2020-06-26 00:10:40 +00:00
|
|
|
"plexapi.myplex.MyPlexAccount", return_value=MockPlexAccount()
|
2020-06-27 08:03:51 +00:00
|
|
|
), patch(
|
|
|
|
"homeassistant.components.plex.PlexWebsocket", autospec=True
|
|
|
|
) as mock_websocket:
|
2020-05-26 19:39:56 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
server_id = mock_plex_server.machineIdentifier
|
|
|
|
loaded_server = hass.data[DOMAIN][SERVERS][server_id]
|
|
|
|
|
|
|
|
# Plex Key searches
|
2020-06-27 08:03:51 +00:00
|
|
|
trigger_plex_update(mock_websocket)
|
2020-06-26 00:10:40 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-05-26 19:39:56 +00:00
|
|
|
media_player_id = hass.states.async_entity_ids("media_player")[0]
|
|
|
|
with patch("homeassistant.components.plex.PlexServer.create_playqueue"):
|
|
|
|
assert await hass.services.async_call(
|
|
|
|
MP_DOMAIN,
|
|
|
|
SERVICE_PLAY_MEDIA,
|
|
|
|
{
|
|
|
|
ATTR_ENTITY_ID: media_player_id,
|
|
|
|
ATTR_MEDIA_CONTENT_TYPE: DOMAIN,
|
|
|
|
ATTR_MEDIA_CONTENT_ID: 123,
|
|
|
|
},
|
|
|
|
True,
|
|
|
|
)
|
|
|
|
with patch.object(MockPlexServer, "fetchItem", side_effect=NotFound):
|
|
|
|
assert await hass.services.async_call(
|
|
|
|
MP_DOMAIN,
|
|
|
|
SERVICE_PLAY_MEDIA,
|
|
|
|
{
|
|
|
|
ATTR_ENTITY_ID: media_player_id,
|
|
|
|
ATTR_MEDIA_CONTENT_TYPE: DOMAIN,
|
|
|
|
ATTR_MEDIA_CONTENT_ID: 123,
|
|
|
|
},
|
|
|
|
True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# TV show searches
|
|
|
|
with patch.object(MockPlexLibrary, "section", side_effect=NotFound):
|
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
2020-08-26 21:24:44 +00:00
|
|
|
MEDIA_TYPE_EPISODE, library_name="Not a Library", show_name="TV Show"
|
2020-05-26 19:39:56 +00:00
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
|
|
|
with patch.object(MockPlexLibrarySection, "get", side_effect=NotFound):
|
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_EPISODE, library_name="TV Shows", show_name="Not a TV Show"
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_EPISODE, library_name="TV Shows", episode_name="An Episode"
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
|
|
|
assert loaded_server.lookup_media(
|
2020-08-26 21:24:44 +00:00
|
|
|
MEDIA_TYPE_EPISODE, library_name="TV Shows", show_name="TV Show"
|
2020-05-26 19:39:56 +00:00
|
|
|
)
|
|
|
|
assert loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_EPISODE,
|
|
|
|
library_name="TV Shows",
|
2020-08-26 21:24:44 +00:00
|
|
|
show_name="TV Show",
|
2020-05-26 19:39:56 +00:00
|
|
|
season_number=2,
|
|
|
|
)
|
|
|
|
assert loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_EPISODE,
|
|
|
|
library_name="TV Shows",
|
2020-08-26 21:24:44 +00:00
|
|
|
show_name="TV Show",
|
2020-05-26 19:39:56 +00:00
|
|
|
season_number=2,
|
|
|
|
episode_number=3,
|
|
|
|
)
|
2020-08-26 21:24:44 +00:00
|
|
|
with patch.object(MockPlexShow, "season", side_effect=NotFound):
|
2020-05-26 19:39:56 +00:00
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_EPISODE,
|
|
|
|
library_name="TV Shows",
|
2020-08-26 21:24:44 +00:00
|
|
|
show_name="TV Show",
|
2020-05-26 19:39:56 +00:00
|
|
|
season_number=2,
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
2020-08-26 21:24:44 +00:00
|
|
|
with patch.object(MockPlexSeason, "episode", side_effect=NotFound):
|
2020-05-26 19:39:56 +00:00
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_EPISODE,
|
|
|
|
library_name="TV Shows",
|
2020-08-26 21:24:44 +00:00
|
|
|
show_name="TV Show",
|
2020-05-26 19:39:56 +00:00
|
|
|
season_number=2,
|
|
|
|
episode_number=1,
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
|
|
|
|
|
|
|
# Music searches
|
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
2020-08-26 21:24:44 +00:00
|
|
|
MEDIA_TYPE_MUSIC, library_name="Music", album_name="Album"
|
2020-05-26 19:39:56 +00:00
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
|
|
|
assert loaded_server.lookup_media(
|
2020-08-26 21:24:44 +00:00
|
|
|
MEDIA_TYPE_MUSIC, library_name="Music", artist_name="Artist"
|
2020-05-26 19:39:56 +00:00
|
|
|
)
|
|
|
|
assert loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
library_name="Music",
|
2020-08-26 21:24:44 +00:00
|
|
|
artist_name="Artist",
|
|
|
|
track_name="Track 3",
|
2020-05-26 19:39:56 +00:00
|
|
|
)
|
|
|
|
assert loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
library_name="Music",
|
2020-08-26 21:24:44 +00:00
|
|
|
artist_name="Artist",
|
|
|
|
album_name="Album",
|
2020-05-26 19:39:56 +00:00
|
|
|
)
|
|
|
|
with patch.object(MockPlexLibrarySection, "get", side_effect=NotFound):
|
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
library_name="Music",
|
|
|
|
artist_name="Not an Artist",
|
2020-08-26 21:24:44 +00:00
|
|
|
album_name="Album",
|
2020-05-26 19:39:56 +00:00
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
|
|
|
with patch.object(MockPlexArtist, "album", side_effect=NotFound):
|
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
library_name="Music",
|
2020-08-26 21:24:44 +00:00
|
|
|
artist_name="Artist",
|
2020-05-26 19:39:56 +00:00
|
|
|
album_name="Not an Album",
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
2020-08-26 21:24:44 +00:00
|
|
|
with patch.object(MockPlexAlbum, "track", side_effect=NotFound):
|
2020-05-26 19:39:56 +00:00
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
library_name="Music",
|
2020-08-26 21:24:44 +00:00
|
|
|
artist_name="Artist",
|
|
|
|
album_name=" Album",
|
2020-05-26 19:39:56 +00:00
|
|
|
track_name="Not a Track",
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
|
|
|
with patch.object(MockPlexArtist, "get", side_effect=NotFound):
|
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
library_name="Music",
|
2020-08-26 21:24:44 +00:00
|
|
|
artist_name="Artist",
|
2020-05-26 19:39:56 +00:00
|
|
|
track_name="Not a Track",
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
|
|
|
assert loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
library_name="Music",
|
2020-08-26 21:24:44 +00:00
|
|
|
artist_name="Artist",
|
|
|
|
album_name="Album",
|
2020-05-26 19:39:56 +00:00
|
|
|
track_number=3,
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
library_name="Music",
|
2020-08-26 21:24:44 +00:00
|
|
|
artist_name="Artist",
|
|
|
|
album_name="Album",
|
2020-05-26 19:39:56 +00:00
|
|
|
track_number=30,
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
|
|
|
assert loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
library_name="Music",
|
2020-08-26 21:24:44 +00:00
|
|
|
artist_name="Artist",
|
|
|
|
album_name="Album",
|
|
|
|
track_name="Track 3",
|
2020-05-26 19:39:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Playlist searches
|
|
|
|
assert loaded_server.lookup_media(MEDIA_TYPE_PLAYLIST, playlist_name="A Playlist")
|
|
|
|
assert loaded_server.lookup_media(MEDIA_TYPE_PLAYLIST) is None
|
|
|
|
with patch.object(MockPlexServer, "playlist", side_effect=NotFound):
|
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_PLAYLIST, playlist_name="Not a Playlist"
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
|
|
|
|
2020-09-04 09:32:36 +00:00
|
|
|
# Legacy Movie searches
|
2020-08-26 21:24:44 +00:00
|
|
|
assert loaded_server.lookup_media(MEDIA_TYPE_VIDEO, video_name="Movie") is None
|
2020-05-26 19:39:56 +00:00
|
|
|
assert loaded_server.lookup_media(MEDIA_TYPE_VIDEO, library_name="Movies") is None
|
|
|
|
assert loaded_server.lookup_media(
|
2020-08-26 21:24:44 +00:00
|
|
|
MEDIA_TYPE_VIDEO, library_name="Movies", video_name="Movie"
|
2020-05-26 19:39:56 +00:00
|
|
|
)
|
|
|
|
with patch.object(MockPlexLibrarySection, "get", side_effect=NotFound):
|
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_VIDEO, library_name="Movies", video_name="Not a Movie"
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
2020-09-04 09:32:36 +00:00
|
|
|
|
|
|
|
# Movie searches
|
|
|
|
assert loaded_server.lookup_media(MEDIA_TYPE_MOVIE, title="Movie") is None
|
|
|
|
assert loaded_server.lookup_media(MEDIA_TYPE_MOVIE, library_name="Movies") is None
|
|
|
|
assert loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MOVIE, library_name="Movies", title="Movie"
|
|
|
|
)
|
|
|
|
with patch.object(MockPlexLibrarySection, "search", side_effect=BadRequest):
|
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MOVIE, library_name="Movies", title="Not a Movie"
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
|
|
|
with patch.object(MockPlexLibrarySection, "search", return_value=[]):
|
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MOVIE, library_name="Movies", title="Not a Movie"
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
|
|
|
|
|
|
|
similar_movies = []
|
|
|
|
for title in "Duplicate Movie", "Duplicate Movie 2":
|
|
|
|
similar_movies.append(MockPlexMediaItem(title))
|
|
|
|
with patch.object(
|
|
|
|
loaded_server.library.section("Movies"), "search", return_value=similar_movies
|
|
|
|
):
|
|
|
|
found_media = loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MOVIE, library_name="Movies", title="Duplicate Movie"
|
|
|
|
)
|
|
|
|
assert found_media.title == "Duplicate Movie"
|
|
|
|
|
|
|
|
duplicate_movies = []
|
|
|
|
for title in "Duplicate Movie - Original", "Duplicate Movie - Remake":
|
|
|
|
duplicate_movies.append(MockPlexMediaItem(title))
|
|
|
|
with patch.object(
|
|
|
|
loaded_server.library.section("Movies"), "search", return_value=duplicate_movies
|
|
|
|
):
|
|
|
|
assert (
|
|
|
|
loaded_server.lookup_media(
|
|
|
|
MEDIA_TYPE_MOVIE, library_name="Movies", title="Duplicate Movie"
|
|
|
|
)
|
|
|
|
) is None
|