python-plexapi/tests/test_server.py

224 lines
7.3 KiB
Python
Raw Normal View History

2017-02-02 04:47:22 +00:00
# -*- coding: utf-8 -*-
2017-04-17 03:33:33 +00:00
import pytest, re, time
2017-01-31 00:02:22 +00:00
from plexapi.exceptions import BadRequest, NotFound
2017-04-17 03:33:33 +00:00
from plexapi.server import PlexServer
2017-01-09 14:21:54 +00:00
from plexapi.utils import download
2017-04-17 03:33:33 +00:00
from PIL import Image, ImageStat
from requests import Session
from . import conftest as utils
2017-04-17 03:33:33 +00:00
def test_server_attr(plex):
assert plex._baseurl == utils.SERVER_BASEURL
2017-04-17 03:33:33 +00:00
assert len(plex.friendlyName) >= 1
assert len(plex.machineIdentifier) == 40
assert plex.myPlex is True
assert plex.myPlexMappingState == 'mapped'
assert plex.myPlexSigninState == 'ok'
assert plex.myPlexSubscription == '0'
assert re.match(utils.REGEX_EMAIL, plex.myPlexUsername)
2017-04-17 03:33:33 +00:00
assert plex.platform in ('Linux', 'Windows')
assert len(plex.platformVersion) >= 5
assert plex._token == utils.SERVER_TOKEN
2017-04-17 03:33:33 +00:00
assert plex.transcoderActiveVideoSessions == 0
assert utils.is_datetime(plex.updatedAt)
2017-04-17 03:33:33 +00:00
assert len(plex.version) >= 5
def test_server_alert_listener(plex, movies):
try:
messages = []
listener = plex.startAlertListener(messages.append)
movies.refresh()
time.sleep(5)
assert len(messages) >= 3
finally:
listener.stop()
2017-01-09 14:21:54 +00:00
@pytest.mark.req_client
def test_server_session():
2017-04-17 03:33:33 +00:00
# TODO: Implement test_server_session
2017-01-09 14:21:54 +00:00
pass
2017-04-17 03:33:33 +00:00
def test_server_library(plex):
# TODO: Implement test_server_library
assert plex.library
2017-01-09 14:21:54 +00:00
2017-04-17 03:33:33 +00:00
def test_server_url(plex):
assert 'ohno' in plex.url('ohno')
2017-01-09 14:21:54 +00:00
2017-04-17 03:33:33 +00:00
def test_server_transcodeImage(tmpdir, plex, show):
2017-02-02 04:47:22 +00:00
width, height = 500, 500
2017-04-17 03:33:33 +00:00
imgurl = plex.transcodeImage(show.banner, height, width)
gray = imgurl = plex.transcodeImage(show.banner, height, width, saturation=0)
resized_img = download(imgurl, savepath=str(tmpdir), filename='resize_image')
original_img = download(show._server.url(show.banner), savepath=str(tmpdir), filename='original_img')
grayscale_img = download(gray, savepath=str(tmpdir), filename='grayscale_img')
with Image.open(resized_img) as image:
assert width, height == image.size
with Image.open(original_img) as image:
assert width, height != image.size
assert _detect_color_image(grayscale_img, thumb_size=150) == 'grayscale'
2017-01-09 14:21:54 +00:00
2017-02-02 04:47:22 +00:00
def _detect_color_image(file, thumb_size=150, MSE_cutoff=22, adjust_color_bias=True):
2017-04-17 03:33:33 +00:00
# http://stackoverflow.com/questions/20068945/detect-if-image-is-color-grayscale-or-black-and-white-with-python-pil
pilimg = Image.open(file)
bands = pilimg.getbands()
2017-02-02 04:47:22 +00:00
if bands == ('R', 'G', 'B') or bands == ('R', 'G', 'B', 'A'):
2017-04-17 03:33:33 +00:00
thumb = pilimg.resize((thumb_size, thumb_size))
sse, bias = 0, [0, 0, 0]
2017-02-02 04:47:22 +00:00
if adjust_color_bias:
bias = ImageStat.Stat(thumb).mean[:3]
bias = [b - sum(bias) / 3 for b in bias]
for pixel in thumb.getdata():
mu = sum(pixel) / 3
2017-04-17 03:33:33 +00:00
sse += sum((pixel[i] - mu - bias[i]) * (pixel[i] - mu - bias[i]) for i in [0, 1, 2])
mse = float(sse) / (thumb_size * thumb_size)
return 'grayscale' if mse <= MSE_cutoff else 'color'
2017-02-02 04:47:22 +00:00
elif len(bands) == 1:
return 'blackandwhite'
2017-01-09 14:21:54 +00:00
2017-04-17 03:33:33 +00:00
def test_server_search(plex):
assert plex.search('16 Blocks')
assert plex.search('16 blocks', mediatype='movie')
2017-01-31 00:02:22 +00:00
2017-01-09 14:21:54 +00:00
2017-04-17 03:33:33 +00:00
def test_server_playlist(plex, show):
episodes = show.episodes()
playlist = plex.createPlaylist('test_playlist', episodes[:3])
try:
assert playlist.title == 'test_playlist'
with pytest.raises(NotFound):
plex.playlist('<playlist-not-found>')
finally:
playlist.delete()
2017-01-31 00:02:22 +00:00
2017-01-09 14:21:54 +00:00
2017-04-17 03:33:33 +00:00
def test_server_playlists(plex, show):
playlists = plex.playlists()
assert len(playlists) == 0
episodes = show.episodes()
playlist = plex.createPlaylist('test_playlist', episodes[:3])
try:
playlists = plex.playlists()
assert len(playlists) == 1
finally:
playlist.delete()
2017-01-09 14:21:54 +00:00
2017-04-17 03:33:33 +00:00
def test_server_history(plex):
history = plex.history()
2017-01-09 14:21:54 +00:00
assert len(history)
2017-04-17 03:33:33 +00:00
def test_server_Server_query(plex):
assert plex.query('/')
2017-01-31 00:02:22 +00:00
with pytest.raises(BadRequest):
2017-04-17 03:33:33 +00:00
assert plex.query('/asdf/1234/asdf', headers={'random_headers': '1234'})
with pytest.raises(BadRequest):
2017-04-17 03:33:33 +00:00
# This is really requests.exceptions.HTTPError
# 401 Client Error: Unauthorized for url
PlexServer(utils.SERVER_BASEURL, '1234')
2017-01-31 00:02:22 +00:00
def test_server_Server_session():
2017-04-17 03:33:33 +00:00
# Mock Sesstion
2017-01-31 00:02:22 +00:00
class MySession(Session):
def __init__(self):
super(self.__class__, self).__init__()
self.plexapi_session_test = True
2017-04-17 03:33:33 +00:00
# Test Code
plex = PlexServer(utils.SERVER_BASEURL, utils.SERVER_TOKEN, session=MySession())
assert hasattr(plex._session, 'plexapi_session_test')
2017-01-31 00:02:22 +00:00
2017-04-17 03:33:33 +00:00
def test_server_token_in_headers(plex):
headers = plex._headers()
assert 'X-Plex-Token' in headers
assert len(headers['X-Plex-Token']) >= 1
2017-01-31 00:02:22 +00:00
2017-01-09 14:21:54 +00:00
2017-04-17 03:33:33 +00:00
def test_server_createPlayQueue(plex, movie):
playqueue = plex.createPlayQueue(movie, shuffle=1, repeat=1)
assert 'shuffle=1' in playqueue._initpath
assert 'repeat=1' in playqueue._initpath
assert playqueue.playQueueShuffled is True
2017-01-09 14:21:54 +00:00
2017-02-02 04:47:22 +00:00
2017-01-09 14:21:54 +00:00
def _test_server_createPlaylist():
2017-04-17 03:33:33 +00:00
# TODO: Implement _test_server_createPlaylist()
2017-01-09 14:21:54 +00:00
# see test_playlists.py
pass
2017-01-31 00:02:22 +00:00
2017-04-17 03:33:33 +00:00
def test_server_client_not_found(plex):
2017-01-31 00:02:22 +00:00
with pytest.raises(NotFound):
2017-04-17 03:33:33 +00:00
plex.client('<This-client-should-not-be-found>')
2017-01-31 00:02:22 +00:00
2017-01-09 14:21:54 +00:00
@pytest.mark.req_client
2017-04-17 03:33:33 +00:00
def test_server_client(plex):
assert plex.client('Plex Web (Chrome)')
2017-01-09 14:21:54 +00:00
2017-02-02 04:47:22 +00:00
2017-04-17 03:33:33 +00:00
def test_server_Server_sessions(plex):
assert len(plex.sessions()) == 0
2017-01-31 00:02:22 +00:00
2017-01-09 14:21:54 +00:00
@pytest.mark.req_client
2017-04-17 03:33:33 +00:00
def test_server_clients(plex):
assert len(plex.clients())
client = plex.clients()[0]
assert client._baseurl == 'http://127.0.0.1:32400'
assert client.device is None
assert client.deviceClass == 'pc'
assert client.machineIdentifier == '89hgkrbqxaxmf45o1q2949ru'
assert client.model is None
assert client.platform is None
assert client.platformVersion is None
assert client.product == 'Plex Web'
assert client.protocol == 'plex'
assert client.protocolCapabilities == ['timeline', 'playback', 'navigation', 'mirror', 'playqueues']
assert client.protocolVersion == '1'
assert client._server._baseurl == 'http://138.68.157.5:32400'
assert client.state is None
assert client.title == 'Plex Web (Chrome)'
assert client.token is None
assert client.vendor is None
assert client.version == '2.12.5'
def test_server_account(plex):
account = plex.account()
assert account.authToken
2017-02-02 04:47:22 +00:00
# TODO: Figure out why this is missing from time to time.
2017-04-17 03:33:33 +00:00
# assert account.mappingError == 'publisherror'
assert account.mappingErrorMessage is None
assert account.mappingState == 'mapped'
assert re.match(utils.REGEX_IPADDR, account.privateAddress)
2017-04-17 03:33:33 +00:00
assert int(account.privatePort) >= 1000
assert re.match(utils.REGEX_IPADDR, account.publicAddress)
2017-04-17 03:33:33 +00:00
assert int(account.publicPort) >= 1000
assert account.signInState == 'ok'
assert account.subscriptionActive is False
assert account.subscriptionFeatures == []
assert account.subscriptionState == 'Unknown'
assert re.match(utils.REGEX_EMAIL, account.username)
2017-04-17 03:33:33 +00:00
def test_server_downloadLogs(tmpdir, plex):
plex.downloadLogs(savepath=str(tmpdir), unpack=True)
assert len(tmpdir.listdir()) > 1
2017-02-26 20:01:54 +00:00
2017-04-17 03:33:33 +00:00
def test_server_downloadDatabases(tmpdir, plex):
plex.downloadDatabases(savepath=str(tmpdir), unpack=True)
assert len(tmpdir.listdir()) > 1