1919 from mock import patch , MagicMock , mock_open
2020
2121
22- SERVER_BASEURL = plexapi .CONFIG .get (' auth.server_baseurl' )
23- MYPLEX_USERNAME = plexapi .CONFIG .get (' auth.myplex_username' )
24- MYPLEX_PASSWORD = plexapi .CONFIG .get (' auth.myplex_password' )
25- CLIENT_BASEURL = plexapi .CONFIG .get (' auth.client_baseurl' )
26- CLIENT_TOKEN = plexapi .CONFIG .get (' auth.client_token' )
22+ SERVER_BASEURL = plexapi .CONFIG .get (" auth.server_baseurl" )
23+ MYPLEX_USERNAME = plexapi .CONFIG .get (" auth.myplex_username" )
24+ MYPLEX_PASSWORD = plexapi .CONFIG .get (" auth.myplex_password" )
25+ CLIENT_BASEURL = plexapi .CONFIG .get (" auth.client_baseurl" )
26+ CLIENT_TOKEN = plexapi .CONFIG .get (" auth.client_token" )
2727
2828MIN_DATETIME = datetime (1999 , 1 , 1 )
29- REGEX_EMAIL = r' (^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)'
30- REGEX_IPADDR = r' ^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'
29+ REGEX_EMAIL = r" (^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
30+ REGEX_IPADDR = r" ^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
3131
3232AUDIOCHANNELS = {2 , 6 }
33- AUDIOLAYOUTS = {'5.1' , '5.1(side)' , 'stereo' }
34- CODECS = {'aac' , 'ac3' , 'dca' , 'h264' , 'mp3' , 'mpeg4' }
35- CONTAINERS = {'avi' , 'mp4' , 'mkv' }
36- CONTENTRATINGS = {'TV-14' , 'TV-MA' , 'G' , 'NR' , 'Not Rated' }
37- FRAMERATES = {'24p' , 'PAL' , 'NTSC' }
38- PROFILES = {'advanced simple' , 'main' , 'constrained baseline' }
39- RESOLUTIONS = {'sd' , '480' , '576' , '720' , '1080' }
40- ENTITLEMENTS = {'ios' , 'roku' , 'android' , 'xbox_one' , 'xbox_360' , 'windows' , 'windows_phone' }
41-
42- TEST_AUTHENTICATED = 'authenticated'
43- TEST_ANONYMOUSLY = 'anonymously'
33+ AUDIOLAYOUTS = {"5.1" , "5.1(side)" , "stereo" }
34+ CODECS = {"aac" , "ac3" , "dca" , "h264" , "mp3" , "mpeg4" }
35+ CONTAINERS = {"avi" , "mp4" , "mkv" }
36+ CONTENTRATINGS = {"TV-14" , "TV-MA" , "G" , "NR" , "Not Rated" }
37+ FRAMERATES = {"24p" , "PAL" , "NTSC" }
38+ PROFILES = {"advanced simple" , "main" , "constrained baseline" }
39+ RESOLUTIONS = {"sd" , "480" , "576" , "720" , "1080" }
40+ ENTITLEMENTS = {
41+ "ios" ,
42+ "roku" ,
43+ "android" ,
44+ "xbox_one" ,
45+ "xbox_360" ,
46+ "windows" ,
47+ "windows_phone" ,
48+ }
49+
50+ TEST_AUTHENTICATED = "authenticated"
51+ TEST_ANONYMOUSLY = "anonymously"
4452ANON_PARAM = pytest .param (TEST_ANONYMOUSLY , marks = pytest .mark .anonymous )
4553AUTH_PARAM = pytest .param (TEST_AUTHENTICATED , marks = pytest .mark .authenticated )
4654
4755
4856def pytest_addoption (parser ):
49- parser .addoption ('--client' , action = 'store_true' , default = False , help = 'Run client tests.' )
57+ parser .addoption (
58+ "--client" , action = "store_true" , default = False , help = "Run client tests."
59+ )
5060
5161
5262def pytest_generate_tests (metafunc ):
53- if 'plex' in metafunc .fixturenames :
54- if 'account' in metafunc .fixturenames or TEST_AUTHENTICATED in metafunc .definition .keywords :
55- metafunc .parametrize ('plex' , [AUTH_PARAM ], indirect = True )
63+ if "plex" in metafunc .fixturenames :
64+ if (
65+ "account" in metafunc .fixturenames
66+ or TEST_AUTHENTICATED in metafunc .definition .keywords
67+ ):
68+ metafunc .parametrize ("plex" , [AUTH_PARAM ], indirect = True )
5669 else :
57- metafunc .parametrize (' plex' , [ANON_PARAM , AUTH_PARAM ], indirect = True )
58- elif ' account' in metafunc .fixturenames :
59- metafunc .parametrize (' account' , [AUTH_PARAM ], indirect = True )
70+ metafunc .parametrize (" plex" , [ANON_PARAM , AUTH_PARAM ], indirect = True )
71+ elif " account" in metafunc .fixturenames :
72+ metafunc .parametrize (" account" , [AUTH_PARAM ], indirect = True )
6073
6174
6275def pytest_runtest_setup (item ):
63- if 'client' in item .keywords and not item .config .getvalue ('client' ):
64- return pytest .skip ('Need --client option to run.' )
65- if TEST_AUTHENTICATED in item .keywords and not (MYPLEX_USERNAME and MYPLEX_PASSWORD ):
66- return pytest .skip ('You have to specify MYPLEX_USERNAME and MYPLEX_PASSWORD to run authenticated tests' )
76+ if "client" in item .keywords and not item .config .getvalue ("client" ):
77+ return pytest .skip ("Need --client option to run." )
78+ if TEST_AUTHENTICATED in item .keywords and not (
79+ MYPLEX_USERNAME and MYPLEX_PASSWORD
80+ ):
81+ return pytest .skip (
82+ "You have to specify MYPLEX_USERNAME and MYPLEX_PASSWORD to run authenticated tests"
83+ )
6784 if TEST_ANONYMOUSLY in item .keywords and MYPLEX_USERNAME and MYPLEX_PASSWORD :
68- return pytest .skip ('Anonymous tests should be ran on unclaimed server, without providing MYPLEX_USERNAME and '
69- 'MYPLEX_PASSWORD' )
85+ return pytest .skip (
86+ "Anonymous tests should be ran on unclaimed server, without providing MYPLEX_USERNAME and "
87+ "MYPLEX_PASSWORD"
88+ )
7089
7190
7291# ---------------------------------
@@ -78,41 +97,50 @@ def get_account():
7897 return MyPlexAccount ()
7998
8099
81- @pytest .fixture (scope = ' session' )
100+ @pytest .fixture (scope = " session" )
82101def account ():
83- assert MYPLEX_USERNAME , ' Required MYPLEX_USERNAME not specified.'
84- assert MYPLEX_PASSWORD , ' Required MYPLEX_PASSWORD not specified.'
102+ assert MYPLEX_USERNAME , " Required MYPLEX_USERNAME not specified."
103+ assert MYPLEX_PASSWORD , " Required MYPLEX_PASSWORD not specified."
85104 return get_account ()
86105
87106
88- @pytest .fixture (scope = ' session' )
107+ @pytest .fixture (scope = " session" )
89108def account_once (account ):
90- if environ .get (' TEST_ACCOUNT_ONCE' ) != '1' and environ .get ('CI' ) == ' true' :
91- pytest .skip (' Do not forget to test this by providing TEST_ACCOUNT_ONCE=1' )
109+ if environ .get (" TEST_ACCOUNT_ONCE" ) != "1" and environ .get ("CI" ) == " true" :
110+ pytest .skip (" Do not forget to test this by providing TEST_ACCOUNT_ONCE=1" )
92111 return account
93112
94113
95- @pytest .fixture (scope = ' session' )
114+ @pytest .fixture (scope = " session" )
96115def account_plexpass (account ):
97116 if not account .subscriptionActive :
98- pytest .skip ('PlexPass subscription is not active, unable to test sync-stuff, be careful!' )
117+ pytest .skip (
118+ "PlexPass subscription is not active, unable to test sync-stuff, be careful!"
119+ )
99120 return account
100121
101122
102- @pytest .fixture (scope = ' session' )
123+ @pytest .fixture (scope = " session" )
103124def account_synctarget (account_plexpass ):
104- assert 'sync-target' in plexapi .X_PLEX_PROVIDES , 'You have to set env var ' \
105- 'PLEXAPI_HEADER_PROVIDES=sync-target,controller'
106- assert 'sync-target' in plexapi .BASE_HEADERS ['X-Plex-Provides' ]
107- assert 'iOS' == plexapi .X_PLEX_PLATFORM , 'You have to set env var PLEXAPI_HEADER_PLATFORM=iOS'
108- assert '11.4.1' == plexapi .X_PLEX_PLATFORM_VERSION , 'You have to set env var PLEXAPI_HEADER_PLATFORM_VERSION=11.4.1'
109- assert 'iPhone' == plexapi .X_PLEX_DEVICE , 'You have to set env var PLEXAPI_HEADER_DEVICE=iPhone'
125+ assert "sync-target" in plexapi .X_PLEX_PROVIDES , (
126+ "You have to set env var " "PLEXAPI_HEADER_PROVIDES=sync-target,controller"
127+ )
128+ assert "sync-target" in plexapi .BASE_HEADERS ["X-Plex-Provides" ]
129+ assert (
130+ "iOS" == plexapi .X_PLEX_PLATFORM
131+ ), "You have to set env var PLEXAPI_HEADER_PLATFORM=iOS"
132+ assert (
133+ "11.4.1" == plexapi .X_PLEX_PLATFORM_VERSION
134+ ), "You have to set env var PLEXAPI_HEADER_PLATFORM_VERSION=11.4.1"
135+ assert (
136+ "iPhone" == plexapi .X_PLEX_DEVICE
137+ ), "You have to set env var PLEXAPI_HEADER_DEVICE=iPhone"
110138 return account_plexpass
111139
112140
113- @pytest .fixture (scope = ' session' )
141+ @pytest .fixture (scope = " session" )
114142def plex (request ):
115- assert SERVER_BASEURL , ' Required SERVER_BASEURL not specified.'
143+ assert SERVER_BASEURL , " Required SERVER_BASEURL not specified."
116144 session = requests .Session ()
117145 if request .param == TEST_AUTHENTICATED :
118146 token = get_account ().authenticationToken
@@ -159,122 +187,130 @@ def client(request, plex):
159187
160188@pytest .fixture ()
161189def tvshows (plex ):
162- return plex .library .section (' TV Shows' )
190+ return plex .library .section (" TV Shows" )
163191
164192
165193@pytest .fixture ()
166194def movies (plex ):
167- return plex .library .section (' Movies' )
195+ return plex .library .section (" Movies" )
168196
169197
170198@pytest .fixture ()
171199def music (plex ):
172- return plex .library .section (' Music' )
200+ return plex .library .section (" Music" )
173201
174202
175203@pytest .fixture ()
176204def photos (plex ):
177- return plex .library .section (' Photos' )
205+ return plex .library .section (" Photos" )
178206
179207
180208@pytest .fixture ()
181209def movie (movies ):
182- return movies .get (' Elephants Dream' )
210+ return movies .get (" Elephants Dream" )
183211
184212
185213@pytest .fixture ()
186- def collection (plex , movie ):
187-
214+ def collection (plex ):
188215 try :
189- return plex .library .section (' Movies' ).collection ()[0 ]
216+ return plex .library .section (" Movies" ).collection ()[0 ]
190217 except IndexError :
218+ movie = plex .library .section ("Movies" ).get ("Elephants Dream" )
191219 movie .addCollection (["marvel" ])
192220
193- n = plex .library .section (' Movies' ).reload ()
221+ n = plex .library .section (" Movies" ).reload ()
194222 return n .collection ()[0 ]
195223
196224
197225@pytest .fixture ()
198226def artist (music ):
199- return music .get (' Infinite State' )
227+ return music .get (" Infinite State" )
200228
201229
202230@pytest .fixture ()
203231def album (artist ):
204- return artist .album (' Unmastered Impulses' )
232+ return artist .album (" Unmastered Impulses" )
205233
206234
207235@pytest .fixture ()
208236def track (album ):
209- return album .track (' Holy Moment' )
237+ return album .track (" Holy Moment" )
210238
211239
212240@pytest .fixture ()
213241def show (tvshows ):
214- return tvshows .get (' Game of Thrones' )
242+ return tvshows .get (" Game of Thrones" )
215243
216244
217245@pytest .fixture ()
218246def episode (show ):
219- return show .get (' Winter Is Coming' )
247+ return show .get (" Winter Is Coming" )
220248
221249
222250@pytest .fixture ()
223251def photoalbum (photos ):
224252 try :
225- return photos .get (' Cats' )
253+ return photos .get (" Cats" )
226254 except Exception :
227- return photos .get (' photo_album1' )
255+ return photos .get (" photo_album1" )
228256
229257
230258@pytest .fixture ()
231259def subtitle ():
232260 mopen = mock_open ()
233- with patch (' __main__.open' , mopen ):
234- with open (' subtitle.srt' , 'w' ) as handler :
235- handler .write (' test' )
261+ with patch (" __main__.open" , mopen ):
262+ with open (" subtitle.srt" , "w" ) as handler :
263+ handler .write (" test" )
236264 return handler
237265
238266
239267@pytest .fixture ()
240268def shared_username (account ):
241- username = environ .get (' SHARED_USERNAME' , ' PKKid' )
269+ username = environ .get (" SHARED_USERNAME" , " PKKid" )
242270 for user in account .users ():
243271 if user .title .lower () == username .lower ():
244272 return username
245- elif (user .username and user .email and user .id and username .lower () in
246- (user .username .lower (), user .email .lower (), str (user .id ))):
273+ elif (
274+ user .username
275+ and user .email
276+ and user .id
277+ and username .lower ()
278+ in (user .username .lower (), user .email .lower (), str (user .id ))
279+ ):
247280 return username
248- pytest .skip (' Shared user %s wasn`t found in your MyPlex account' % username )
281+ pytest .skip (" Shared user %s wasn`t found in your MyPlex account" % username )
249282
250283
251284@pytest .fixture ()
252285def monkeydownload (request , monkeypatch ):
253- monkeypatch .setattr ('plexapi.utils.download' , partial (plexapi .utils .download , mocked = True ))
286+ monkeypatch .setattr (
287+ "plexapi.utils.download" , partial (plexapi .utils .download , mocked = True )
288+ )
254289 yield
255290 monkeypatch .undo ()
256291
257292
258293def callable_http_patch ():
259294 """This intented to stop some http requests inside some tests."""
260- return patch ('plexapi.server.requests.sessions.Session.send' ,
261- return_value = MagicMock (status_code = 200 ,
262- text = '<xml><child></child></xml>' ))
295+ return patch (
296+ "plexapi.server.requests.sessions.Session.send" ,
297+ return_value = MagicMock (status_code = 200 , text = "<xml><child></child></xml>" ),
298+ )
263299
264300
265301@pytest .fixture ()
266302def empty_response (mocker ):
267- response = mocker .MagicMock (status_code = 200 , text = ' <xml><child></child></xml>' )
303+ response = mocker .MagicMock (status_code = 200 , text = " <xml><child></child></xml>" )
268304 return response
269305
270306
271307@pytest .fixture ()
272308def patched_http_call (mocker ):
273309 """This will stop any http calls inside any test."""
274- return mocker .patch ('plexapi.server.requests.sessions.Session.send' ,
275- return_value = MagicMock ( status_code = 200 ,
276- text = ' <xml><child></child></xml>' )
277- )
310+ return mocker .patch (
311+ "plexapi.server.requests.sessions.Session.send" ,
312+ return_value = MagicMock ( status_code = 200 , text = " <xml><child></child></xml>" ),
313+ )
278314
279315
280316# ---------------------------------
@@ -292,7 +328,7 @@ def is_float(value, gte=1.0):
292328 return float (value ) >= gte
293329
294330
295- def is_metadata (key , prefix = ' /library/metadata/' , contains = '' , suffix = '' ):
331+ def is_metadata (key , prefix = " /library/metadata/" , contains = "" , suffix = "" ):
296332 try :
297333 assert key .startswith (prefix )
298334 assert contains in key
@@ -303,19 +339,19 @@ def is_metadata(key, prefix='/library/metadata/', contains='', suffix=''):
303339
304340
305341def is_part (key ):
306- return is_metadata (key , prefix = ' /library/parts/' )
342+ return is_metadata (key , prefix = " /library/parts/" )
307343
308344
309345def is_section (key ):
310- return is_metadata (key , prefix = ' /library/sections/' )
346+ return is_metadata (key , prefix = " /library/sections/" )
311347
312348
313349def is_string (value , gte = 1 ):
314350 return isinstance (value , compat .string_type ) and len (value ) >= gte
315351
316352
317353def is_thumb (key ):
318- return is_metadata (key , contains = ' /thumb/' )
354+ return is_metadata (key , contains = " /thumb/" )
319355
320356
321357def wait_until (condition_function , delay = 0.25 , timeout = 1 , * args , ** kwargs ):
@@ -327,6 +363,9 @@ def wait_until(condition_function, delay=0.25, timeout=1, *args, **kwargs):
327363 time .sleep (delay )
328364 ready = condition_function (* args , ** kwargs )
329365
330- assert ready , 'Wait timeout after %d retries, %.2f seconds' % (retries , time .time () - start )
366+ assert ready , "Wait timeout after %d retries, %.2f seconds" % (
367+ retries ,
368+ time .time () - start ,
369+ )
331370
332371 return ready
0 commit comments