11import base64
22import hashlib
3- from datetime import datetime , timedelta
3+ from datetime import datetime , timedelta , UTC # Import UTC for timezone-aware datetime
44
55import pytest # type: ignore
66from livekit .api import AccessToken , TokenVerifier , WebhookReceiver
@@ -99,8 +99,11 @@ def test_mismatched_api_key_secret():
9999 token .claims .sha256 = hash64
100100 jwt = token .to_jwt ()
101101
102- # Now using broad Exception, as requested
103- with pytest .raises (Exception , match = "could not verify token signature" ):
102+ # The LiveKit API internally catches jwt.exceptions.InvalidSignatureError
103+ # and re-raises it as a LiveKitError with a message starting "could not verify token: "
104+ # followed by the original JWT error message.
105+ expected_match = r"could not verify token: Signature verification failed"
106+ with pytest .raises (Exception , match = expected_match ):
104107 receiver .receive (TEST_EVENT , jwt )
105108
106109
@@ -116,10 +119,13 @@ def test_expired_token():
116119 token .claims .sha256 = hash64
117120
118121 # Set the token's expiration to a time in the past
119- token .claims .exp = datetime .utcnow () - timedelta (seconds = 60 ) # 1 minute ago
122+ # Using datetime.now(UTC) to address the DeprecationWarning
123+ token .claims .exp = datetime .now (UTC ) - timedelta (seconds = 60 ) # 1 minute ago
120124
121125 jwt = token .to_jwt ()
122126
123- # Now using broad Exception, as requested
124- with pytest .raises (Exception ):
127+ # Similar to mismatched key, LiveKit API wraps the ExpiredSignatureError
128+ # The message will start with "could not verify token: "
129+ expected_match = r"could not verify token: Signature has expired"
130+ with pytest .raises (Exception , match = expected_match ):
125131 receiver .receive (TEST_EVENT , jwt )
0 commit comments