-
Notifications
You must be signed in to change notification settings - Fork 196
/
test_myplex.py
367 lines (285 loc) · 12.1 KB
/
test_myplex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-
import pytest
from plexapi.exceptions import BadRequest, NotFound, Unauthorized
from plexapi.myplex import MyPlexInvite
from . import conftest as utils
from .payloads import MYPLEX_INVITE
def test_myplex_accounts(account, plex):
assert account, "Must specify username, password & resource to run this test."
print("MyPlexAccount:")
print(f"username: {account.username}")
print(f"email: {account.email}")
print(f"home: {account.home}")
assert account.username, "Account has no username"
assert account.authenticationToken, "Account has no authenticationToken"
assert account.email, "Account has no email"
assert account.home is not None, "Account has no home"
account = plex.account()
print("Local PlexServer.account():")
print(f"username: {account.username}")
# print('authToken: %s' % account.authToken)
print(f"signInState: {account.signInState}")
assert account.username, "Account has no username"
assert account.authToken, "Account has no authToken"
assert account.signInState, "Account has no signInState"
def test_myplex_resources(account):
assert account, "Must specify username, password & resource to run this test."
resources = account.resources()
for resource in resources:
name = resource.name or "Unknown"
connections = [c.uri for c in resource.connections]
connections = ", ".join(connections) if connections else "None"
print(f"{name} ({resource.product}): {connections}")
assert resources, f"No resources found for account: {account.username}"
def test_myplex_connect_to_resource(plex, account):
servername = plex.friendlyName
for resource in account.resources():
if resource.name == servername:
break
assert resource.connect(timeout=10)
def test_myplex_devices(account):
devices = account.devices()
for device in devices:
name = device.name or "Unknown"
connections = ", ".join(device.connections) if device.connections else "None"
print(f"{name} ({device.product}): {connections}")
assert devices, f"No devices found for account: {account.name}"
def test_myplex_device(account, plex):
assert account.device(plex.friendlyName)
def _test_myplex_connect_to_device(account):
devices = account.devices()
for device in devices:
if device.name == "some client name" and len(device.connections):
break
client = device.connect()
assert client, "Unable to connect to device"
def test_myplex_users(account):
users = account.users()
if not len(users):
return pytest.skip("You have to add a shared account into your MyPlex")
print(f"Found {len(users)} users.")
user = account.user(users[0].title)
print(f"Found user: {user}")
assert user, f"Could not find user {users[0].title}"
try:
users[0].servers[0]
except IndexError:
return pytest.skip(f"{users[0].title} shared user does not have access to any servers")
assert (
len(users[0].servers[0].sections()) > 0
), "Couldn't info about the shared libraries"
def test_myplex_resource(account, plex):
assert account.resource(plex.friendlyName)
def test_myplex_webhooks(account):
if account.subscriptionActive:
assert isinstance(account.webhooks(), list)
else:
with pytest.raises(BadRequest):
account.webhooks()
def test_myplex_addwebhooks(account):
if account.subscriptionActive:
assert "http://example.com" in account.addWebhook("http://example.com")
else:
with pytest.raises(BadRequest):
account.addWebhook("http://example.com")
def test_myplex_deletewebhooks(account):
if account.subscriptionActive:
assert "http://example.com" not in account.deleteWebhook("http://example.com")
else:
with pytest.raises(BadRequest):
account.deleteWebhook("http://example.com")
def test_myplex_optout(account_once):
def enabled():
ele = account_once.query("https://plex.tv/api/v2/user/privacy")
lib = ele.attrib.get("optOutLibraryStats")
play = ele.attrib.get("optOutPlayback")
return bool(int(lib)), bool(int(play))
account_once.optOut(library=True, playback=True)
utils.wait_until(lambda: enabled() == (True, True))
account_once.optOut(library=False, playback=False)
utils.wait_until(lambda: enabled() == (False, False))
@pytest.mark.authenticated
@pytest.mark.xfail(reason="Test account is missing online media sources?")
def test_myplex_onlineMediaSources_optOut(account):
onlineMediaSources = account.onlineMediaSources()
for optOut in onlineMediaSources:
if optOut.key == 'tv.plex.provider.news':
# News is no longer available
continue
optOutValue = optOut.value
optOut.optIn()
assert optOut.value == 'opt_in'
optOut.optOut()
assert optOut.value == 'opt_out'
if optOut.key == 'tv.plex.provider.music':
with pytest.raises(BadRequest):
optOut.optOutManaged()
else:
optOut.optOutManaged()
assert optOut.value == 'opt_out_managed'
# Reset original value
optOut._updateOptOut(optOutValue)
with pytest.raises(NotFound):
onlineMediaSources[0]._updateOptOut('unknown')
def test_myplex_inviteFriend(account, plex, mocker):
inv_user = "hellowlol"
vid_filter = {"contentRating": ["G"], "label": ["foo"]}
secs = plex.library.sections()
ids = account._getSectionIds(plex.machineIdentifier, secs)
mocker.patch.object(account, "_getSectionIds", return_value=ids)
with utils.callable_http_patch():
account.inviteFriend(
inv_user,
plex,
secs,
allowSync=True,
allowCameraUpload=True,
allowChannels=False,
filterMovies=vid_filter,
filterTelevision=vid_filter,
filterMusic={"label": ["foo"]},
)
assert inv_user not in [u.title for u in account.users()]
def test_myplex_acceptInvite(account, requests_mock):
url = MyPlexInvite.REQUESTS
requests_mock.get(url, text=MYPLEX_INVITE)
invite = account.pendingInvite('testuser', includeSent=False)
with utils.callable_http_patch():
account.acceptInvite(invite)
def test_myplex_cancelInvite(account, requests_mock):
url = MyPlexInvite.REQUESTED
requests_mock.get(url, text=MYPLEX_INVITE)
invite = account.pendingInvite('testuser', includeReceived=False)
with utils.callable_http_patch():
account.cancelInvite(invite)
def test_myplex_updateFriend(account, plex, mocker, shared_username):
vid_filter = {"contentRating": ["G"], "label": ["foo"]}
secs = plex.library.sections()
user = account.user(shared_username)
ids = account._getSectionIds(plex.machineIdentifier, secs)
mocker.patch.object(account, "_getSectionIds", return_value=ids)
mocker.patch.object(account, "user", return_value=user)
with utils.callable_http_patch():
account.updateFriend(
shared_username,
plex,
secs,
allowSync=True,
removeSections=True,
allowCameraUpload=True,
allowChannels=False,
filterMovies=vid_filter,
filterTelevision=vid_filter,
filterMusic={"label": ["foo"]},
)
with utils.callable_http_patch():
account.removeFriend(shared_username)
def test_myplex_createExistingUser(account, plex, shared_username):
user = account.user(shared_username)
url = f"https://plex.tv/api/invites/requested/{user.id}?friend=0&server=0&home=1"
account.createExistingUser(user, plex)
assert shared_username in [u.username for u in account.users() if u.home is True]
# Remove Home invite
account.query(url, account._session.delete)
# Confirm user was removed from home and has returned to friend
assert shared_username not in [
u.username for u in plex.myPlexAccount().users() if u.home is True
]
assert shared_username in [
u.username for u in plex.myPlexAccount().users() if u.home is False
]
@pytest.mark.skip(reason="broken test?")
def test_myplex_createHomeUser_remove(account, plex):
homeuser = "New Home User"
account.createHomeUser(homeuser, plex)
assert homeuser in [u.title for u in plex.myPlexAccount().users() if u.home is True]
account.removeHomeUser(homeuser)
assert homeuser not in [
u.title for u in plex.myPlexAccount().users() if u.home is True
]
def test_myplex_plexpass_attributes(account_plexpass):
assert account_plexpass.subscriptionActive
assert account_plexpass.subscriptionStatus == "Active"
assert account_plexpass.subscriptionPlan
assert "sync" in account_plexpass.subscriptionFeatures
assert "premium_music_metadata" in account_plexpass.subscriptionFeatures
assert "plexpass" in account_plexpass.roles
assert utils.ENTITLEMENTS <= set(account_plexpass.entitlements)
def test_myplex_claimToken(account):
assert account.claimToken().startswith("claim-")
def test_myplex_watchlist(account, movie, show, artist):
# Ensure watchlist is cleared before tests
for item in account.watchlist():
account.removeFromWatchlist(item)
assert not account.watchlist()
# Add to watchlist from account
account.addToWatchlist(movie)
assert account.onWatchlist(movie)
# Add to watchlist from object
show.addToWatchlist(account)
assert show.onWatchlist(account)
# Remove from watchlist from account
account.removeFromWatchlist(show)
assert not account.onWatchlist(show)
# Remove from watchlist from object
movie.removeFromWatchlist(account)
assert not movie.onWatchlist(account)
# Add multiple items to watchlist
account.addToWatchlist([movie, show])
assert movie.onWatchlist(account) and show.onWatchlist(account)
# Filter and sort watchlist
watchlist = account.watchlist(filter='released', sort='titleSort', libtype='movie')
guids = [i.guid for i in watchlist]
assert movie.guid in guids and show.guid not in guids
# Test adding existing item to watchlist
with pytest.raises(BadRequest):
account.addToWatchlist(movie)
# Test retrieving maxresults from watchlist
watchlist = account.watchlist(maxresults=1)
assert len(watchlist) == 1
# Remove multiple items from watchlist
account.removeFromWatchlist([movie, show])
assert not movie.onWatchlist(account) and not show.onWatchlist(account)
# Test removing non-existing item from watchlist
with pytest.raises(BadRequest):
account.removeFromWatchlist(movie)
# Test adding invalid item to watchlist
with pytest.raises(BadRequest):
account.addToWatchlist(artist)
def test_myplex_searchDiscover(account, movie, show):
guids = lambda x: [r.guid for r in x]
results = account.searchDiscover(movie.title)
assert movie.guid in guids(results)
results = account.searchDiscover(movie.title, libtype="show")
assert movie.guid not in guids(results)
results = account.searchDiscover(show.title)
assert show.guid in [r.guid for r in results]
results = account.searchDiscover(show.title, libtype="movie")
assert show.guid not in guids(results)
@pytest.mark.authenticated
def test_myplex_viewStateSync(account):
account.enableViewStateSync()
assert account.viewStateSync is True
account.disableViewStateSync()
assert account.viewStateSync is False
@pytest.mark.authenticated
def test_myplex_pin(account, plex):
assert account.pin is None
account.setPin("0000")
with pytest.raises(Unauthorized):
account.setPin("1111")
account.setPin("1111", currentPin="0000")
with pytest.raises(Unauthorized):
account.removePin("0000")
account.removePin("1111")
homeuser = "Test PIN User"
try:
account.createHomeUser(homeuser, plex)
account.setManagedUserPin(homeuser, "0000")
account.removeManagedUserPin(homeuser)
finally:
account.removeHomeUser(homeuser)
def test_myplex_geoip(account):
assert account.geoip(account.publicIP())
def test_myplex_ping(account):
assert account.ping()