Skip to content

Commit 3cd50a9

Browse files
committed
Fix JWT test
1 parent f5a1d7e commit 3cd50a9

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

plexapi/myplex.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,12 +2069,12 @@ def generateKeypair(self, keyfiles=(None, None), overwrite=False):
20692069

20702070
privateKey = ed25519.Ed25519PrivateKey.generate()
20712071
publicKey = privateKey.public_key()
2072-
self._privateKey = privateKey.private_bytes(
2072+
_privateKey = privateKey.private_bytes(
20732073
encoding=serialization.Encoding.Raw,
20742074
format=serialization.PrivateFormat.Raw,
20752075
encryption_algorithm=serialization.NoEncryption()
20762076
)
2077-
self._publicKey = publicKey.public_bytes(
2077+
_publicKey = publicKey.public_bytes(
20782078
encoding=serialization.Encoding.Raw,
20792079
format=serialization.PublicFormat.Raw
20802080
)
@@ -2084,8 +2084,11 @@ def generateKeypair(self, keyfiles=(None, None), overwrite=False):
20842084
raise FileExistsError('Keypair files already exist, set overwrite=True to overwrite them.')
20852085

20862086
with open(keyfiles[0], 'wb') as privateFile, open(keyfiles[1], 'wb') as publicFile:
2087-
privateFile.write(self._privateKey)
2088-
publicFile.write(self._publicKey)
2087+
privateFile.write(_privateKey)
2088+
publicFile.write(_publicKey)
2089+
2090+
self._privateKey = _privateKey
2091+
self._publicKey = _publicKey
20892092

20902093
@property
20912094
def _clientIdentifier(self):

tests/test_myplex.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
from plexapi.exceptions import BadRequest, NotFound, Unauthorized
55
from plexapi.myplex import MyPlexAccount, MyPlexInvite, MyPlexJWTLogin
6+
from plexapi.utils import createMyPlexDevice
67

78
from . import conftest as utils
89
from .payloads import MYPLEX_INVITE
@@ -369,31 +370,40 @@ def test_myplex_ping(account):
369370

370371

371372
def test_myplex_jwt_login(account, tmp_path, monkeypatch):
373+
# Create a new MyPlexDevice for JWT tests
374+
device = createMyPlexDevice(account=account)
375+
376+
privkey = tmp_path / 'private.key'
377+
pubkey = tmp_path / 'public.key'
378+
372379
jwtlogin = MyPlexJWTLogin(
373-
token=account.authToken,
380+
headers={'X-Plex-Client-Identifier': device.clientIdentifier},
381+
token=device.token,
374382
scopes=['username', 'email', 'friendly_name']
375383
)
376-
jwtlogin.generateKeypair(keyfiles=(tmp_path / 'private.key', tmp_path / 'public.key'), overwrite=True)
384+
jwtlogin.generateKeypair(keyfiles=(privkey, pubkey), overwrite=True)
377385
with pytest.raises(FileExistsError):
378-
jwtlogin.generateKeypair(keyfiles=(tmp_path / 'private.key', tmp_path / 'public.key'))
386+
jwtlogin.generateKeypair(keyfiles=(privkey, pubkey))
379387
jwtlogin.registerDevice()
380388
jwtToken = jwtlogin.refreshJWT()
381389
assert jwtlogin.decodedJWT['user']['username'] == account.username
382-
assert MyPlexAccount(token=jwtToken) == account
390+
new_account = MyPlexAccount(token=jwtToken)
391+
assert new_account.username == account.username
383392

384393
jwtlogin = MyPlexJWTLogin(
394+
headers={'X-Plex-Client-Identifier': device.clientIdentifier},
385395
jwtToken=jwtToken,
386-
keypair=(tmp_path / 'private.key', tmp_path / 'public.key'),
396+
keypair=(privkey, pubkey),
387397
scopes=['username', 'email', 'friendly_name']
388398
)
389399
assert jwtlogin.verifyJWT()
390400
newjwtToken = jwtlogin.refreshJWT()
391401
assert newjwtToken != jwtToken
392-
assert MyPlexAccount(token=newjwtToken) == account
402+
new_account = MyPlexAccount(token=newjwtToken)
403+
assert new_account.username == account.username
393404

394405
plexPublicJWKs = jwtlogin._getPlexPublicJWK()
395-
invalidJWK = plexPublicJWKs[0].copy()
396-
invalidJWK['x'] += 'invalid'
406+
invalidJWK = jwtlogin._publicJWK._jwk_data.copy()
397407
monkeypatch.setattr(MyPlexJWTLogin, "_getPlexPublicJWK", lambda self: plexPublicJWKs + [invalidJWK])
398408
assert jwtlogin.decodePlexJWT()
399409

0 commit comments

Comments
 (0)