2017-02-02 04:47:22 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-04-29 05:47:21 +00:00
|
|
|
# Running these tests requires a few things in your Plex Library.
|
2017-05-02 03:26:27 +00:00
|
|
|
# Movies section containing both movies:
|
|
|
|
# * Sintel - https://durian.blender.org/
|
|
|
|
# * Elephants Dream - https://orange.blender.org/
|
|
|
|
# * Sita Sings the Blues - http://www.sitasingstheblues.com/
|
|
|
|
# * Big Buck Bunny - https://peach.blender.org/
|
|
|
|
# TV Show section containing the shows:
|
|
|
|
# * Game of Thrones (Season 1 and 2)
|
|
|
|
# * The 100 (Seasons 1 and 2)
|
|
|
|
# * (or symlink the above movies with proper names)
|
|
|
|
# Music section containing the albums:
|
|
|
|
# Infinite State - Unmastered Impulses - https://github.com/kennethreitz/unmastered-impulses
|
|
|
|
# Broke For Free - Layers - http://freemusicarchive.org/music/broke_for_free/Layers/
|
|
|
|
# 3. A Photos section containing the photoalbums:
|
|
|
|
# Cats (with cute cat photos inside)
|
2017-04-29 05:47:21 +00:00
|
|
|
# 4. A TV Shows section containing at least two seasons of The 100.
|
2017-10-28 20:58:47 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import requests
|
|
|
|
|
|
|
|
try:
|
|
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
except ImportError:
|
|
|
|
from mock import patch, MagicMock
|
|
|
|
|
|
|
|
|
|
|
|
import plexapi
|
2017-04-23 05:18:53 +00:00
|
|
|
from plexapi import compat
|
2017-04-26 03:09:37 +00:00
|
|
|
from plexapi.client import PlexClient
|
2017-10-28 20:58:47 +00:00
|
|
|
|
2017-04-15 00:47:59 +00:00
|
|
|
from plexapi.server import PlexServer
|
2017-10-28 20:58:47 +00:00
|
|
|
|
2017-01-09 14:21:54 +00:00
|
|
|
|
2017-04-15 00:47:59 +00:00
|
|
|
SERVER_BASEURL = plexapi.CONFIG.get('auth.server_baseurl')
|
|
|
|
SERVER_TOKEN = plexapi.CONFIG.get('auth.server_token')
|
2017-04-26 03:23:57 +00:00
|
|
|
CLIENT_BASEURL = plexapi.CONFIG.get('auth.client_baseurl')
|
|
|
|
CLIENT_TOKEN = plexapi.CONFIG.get('auth.client_token')
|
2017-01-09 14:21:54 +00:00
|
|
|
|
2017-05-13 03:25:57 +00:00
|
|
|
MIN_DATETIME = datetime(1999, 1, 1)
|
2017-04-23 05:18:53 +00:00
|
|
|
REGEX_EMAIL = r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)'
|
|
|
|
REGEX_IPADDR = r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'
|
|
|
|
|
2017-04-29 05:47:21 +00:00
|
|
|
AUDIOCHANNELS = {2, 6}
|
|
|
|
AUDIOLAYOUTS = {'5.1', '5.1(side)', 'stereo'}
|
|
|
|
CODECS = {'aac', 'ac3', 'dca', 'h264', 'mp3', 'mpeg4'}
|
|
|
|
CONTAINERS = {'avi', 'mp4', 'mkv'}
|
2017-05-02 03:26:27 +00:00
|
|
|
CONTENTRATINGS = {'TV-14', 'TV-MA', 'G', 'NR'}
|
2017-04-29 05:47:21 +00:00
|
|
|
FRAMERATES = {'24p', 'PAL'}
|
2017-05-02 03:26:27 +00:00
|
|
|
PROFILES = {'advanced simple', 'main', 'constrained baseline'}
|
|
|
|
RESOLUTIONS = {'sd', '480', '576', '720', '1080'}
|
2017-04-23 05:18:53 +00:00
|
|
|
|
2017-01-09 14:21:54 +00:00
|
|
|
|
2017-04-15 00:47:59 +00:00
|
|
|
def pytest_addoption(parser):
|
2017-04-26 03:23:57 +00:00
|
|
|
parser.addoption('--client', action='store_true', default=False, help='Run client tests.')
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
2017-04-15 00:47:59 +00:00
|
|
|
def pytest_runtest_setup(item):
|
2017-04-26 03:09:37 +00:00
|
|
|
if 'client' in item.keywords and not item.config.getvalue('client'):
|
|
|
|
return pytest.skip('Need --client option to run.')
|
2017-01-31 00:02:22 +00:00
|
|
|
|
|
|
|
|
2017-06-06 01:40:52 +00:00
|
|
|
# ---------------------------------
|
|
|
|
# Fixtures
|
|
|
|
# ---------------------------------
|
2017-01-09 14:21:54 +00:00
|
|
|
|
2017-04-15 00:47:59 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def account():
|
2017-06-06 01:40:52 +00:00
|
|
|
return plex().myPlexAccount()
|
|
|
|
# assert MYPLEX_USERNAME, 'Required MYPLEX_USERNAME not specified.'
|
|
|
|
# assert MYPLEX_PASSWORD, 'Required MYPLEX_PASSWORD not specified.'
|
|
|
|
# return MyPlexAccount(MYPLEX_USERNAME, MYPLEX_PASSWORD)
|
2017-01-09 14:21:54 +00:00
|
|
|
|
2017-04-15 00:47:59 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def plex():
|
|
|
|
assert SERVER_BASEURL, 'Required SERVER_BASEURL not specified.'
|
|
|
|
assert SERVER_TOKEN, 'Requred SERVER_TOKEN not specified.'
|
|
|
|
session = requests.Session()
|
|
|
|
return PlexServer(SERVER_BASEURL, SERVER_TOKEN, session=session)
|
2017-01-09 14:21:54 +00:00
|
|
|
|
2017-02-27 05:36:20 +00:00
|
|
|
|
2017-07-17 14:11:03 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def fresh_plex():
|
|
|
|
return PlexServer
|
|
|
|
|
|
|
|
|
2017-01-09 14:21:54 +00:00
|
|
|
@pytest.fixture()
|
2017-04-15 00:47:59 +00:00
|
|
|
def plex2():
|
|
|
|
return plex()
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
2017-04-26 03:09:37 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def client(request):
|
2017-05-13 03:25:57 +00:00
|
|
|
return PlexClient(plex(), baseurl=CLIENT_BASEURL, token=CLIENT_TOKEN)
|
2017-04-26 03:09:37 +00:00
|
|
|
|
|
|
|
|
2017-01-09 14:21:54 +00:00
|
|
|
@pytest.fixture()
|
2017-04-15 00:47:59 +00:00
|
|
|
def tvshows(plex):
|
|
|
|
return plex.library.section('TV Shows')
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2017-04-15 00:47:59 +00:00
|
|
|
def movies(plex):
|
|
|
|
return plex.library.section('Movies')
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2017-04-15 00:47:59 +00:00
|
|
|
def music(plex):
|
|
|
|
return plex.library.section('Music')
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2017-04-15 00:47:59 +00:00
|
|
|
def photos(plex):
|
|
|
|
return plex.library.section('Photos')
|
2017-01-31 00:02:22 +00:00
|
|
|
|
2017-02-27 05:36:20 +00:00
|
|
|
|
2017-01-31 00:02:22 +00:00
|
|
|
@pytest.fixture()
|
2017-04-15 00:47:59 +00:00
|
|
|
def movie(movies):
|
2017-05-02 03:26:27 +00:00
|
|
|
return movies.get('Elephants Dream')
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2017-04-15 00:47:59 +00:00
|
|
|
def artist(music):
|
|
|
|
return music.get('Infinite State')
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2017-04-15 00:47:59 +00:00
|
|
|
def album(artist):
|
|
|
|
return artist.album('Unmastered Impulses')
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2017-04-15 00:47:59 +00:00
|
|
|
def track(album):
|
|
|
|
return album.track('Holy Moment')
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2017-04-15 00:47:59 +00:00
|
|
|
def show(tvshows):
|
2017-05-02 03:26:27 +00:00
|
|
|
return tvshows.get('Game of Thrones')
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2017-04-15 00:47:59 +00:00
|
|
|
def episode(show):
|
2017-05-02 03:26:27 +00:00
|
|
|
return show.get('Winter Is Coming')
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2017-04-15 00:47:59 +00:00
|
|
|
def photoalbum(photos):
|
2017-04-23 05:54:53 +00:00
|
|
|
try:
|
|
|
|
return photos.get('Cats')
|
|
|
|
except:
|
|
|
|
return photos.get('photo_album1')
|
2017-01-09 14:21:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def monkeydownload(request, monkeypatch):
|
|
|
|
monkeypatch.setattr('plexapi.utils.download', partial(plexapi.utils.download, mocked=True))
|
|
|
|
yield
|
|
|
|
monkeypatch.undo()
|
2017-04-15 00:47:59 +00:00
|
|
|
|
|
|
|
|
2017-10-28 20:58:47 +00:00
|
|
|
def callable_http_patch():
|
2017-11-01 22:18:18 +00:00
|
|
|
"""This intented to stop some http requests inside some tests."""
|
2017-10-28 20:58:47 +00:00
|
|
|
return patch('plexapi.server.requests.sessions.Session.send',
|
|
|
|
return_value=MagicMock(status_code=200,
|
|
|
|
text='<xml><child></child></xml>'))
|
|
|
|
|
2017-10-26 21:55:59 +00:00
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def empty_response(mocker):
|
|
|
|
response = mocker.MagicMock(status_code=200, text='<xml><child></child></xml>')
|
|
|
|
return response
|
|
|
|
|
2017-10-26 23:09:17 +00:00
|
|
|
|
2017-10-26 21:55:59 +00:00
|
|
|
@pytest.fixture()
|
2017-10-28 22:07:06 +00:00
|
|
|
def patched_http_call(mocker):
|
2017-11-01 22:18:18 +00:00
|
|
|
"""This will stop any http calls inside any test."""
|
2017-10-28 22:07:06 +00:00
|
|
|
return mocker.patch('plexapi.server.requests.sessions.Session.send',
|
|
|
|
return_value=MagicMock(status_code=200,
|
|
|
|
text='<xml><child></child></xml>')
|
|
|
|
)
|
2017-10-26 21:55:59 +00:00
|
|
|
|
|
|
|
|
2017-06-06 01:40:52 +00:00
|
|
|
# ---------------------------------
|
|
|
|
# Utility Functions
|
|
|
|
# ---------------------------------
|
2017-04-15 00:47:59 +00:00
|
|
|
def is_datetime(value):
|
|
|
|
return value > MIN_DATETIME
|
|
|
|
|
|
|
|
|
2017-04-23 05:18:53 +00:00
|
|
|
def is_int(value, gte=1):
|
|
|
|
return int(value) >= gte
|
|
|
|
|
2017-04-15 00:47:59 +00:00
|
|
|
|
2017-04-23 05:18:53 +00:00
|
|
|
def is_float(value, gte=1.0):
|
|
|
|
return float(value) >= gte
|
2017-04-15 00:47:59 +00:00
|
|
|
|
2017-04-23 05:18:53 +00:00
|
|
|
|
|
|
|
def is_metadata(key, prefix='/library/metadata/', contains='', suffix=''):
|
|
|
|
try:
|
|
|
|
assert key.startswith(prefix)
|
|
|
|
assert contains in key
|
|
|
|
assert key.endswith(suffix)
|
|
|
|
return True
|
|
|
|
except AssertionError:
|
|
|
|
return False
|
2017-04-15 00:47:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_part(key):
|
2017-04-23 05:18:53 +00:00
|
|
|
return is_metadata(key, prefix='/library/parts/')
|
|
|
|
|
|
|
|
|
|
|
|
def is_section(key):
|
|
|
|
return is_metadata(key, prefix='/library/sections/')
|
|
|
|
|
|
|
|
|
|
|
|
def is_string(value, gte=1):
|
|
|
|
return isinstance(value, compat.string_type) and len(value) >= gte
|
2017-04-15 00:47:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_thumb(key):
|
2017-04-23 05:18:53 +00:00
|
|
|
return is_metadata(key, contains='/thumb/')
|