11import base64
22import hashlib
3+
34import pytest # type: ignore
45from livekit .api import AccessToken , TokenVerifier , WebhookReceiver
56
6- TEST_API_KEY = " myapikey"
7- TEST_API_SECRET = " thiskeyistotallyunsafe"
7+ TEST_API_KEY = ' myapikey'
8+ TEST_API_SECRET = ' thiskeyistotallyunsafe'
89TEST_EVENT = """
910{
1011 "event": "room_started",
@@ -60,10 +61,10 @@ def test_bad_hash():
6061 receiver = WebhookReceiver (token_verifier )
6162
6263 token = AccessToken (TEST_API_KEY , TEST_API_SECRET )
63- hash64 = base64 .b64encode (hashlib .sha256 (" wrong_hash" .encode ()).digest ()).decode ()
64+ hash64 = base64 .b64encode (hashlib .sha256 (' wrong_hash' .encode ()).digest ()).decode ()
6465 token .claims .sha256 = hash64
6566 jwt = token .to_jwt ()
66- with pytest .raises (Exception ): # Using a broad Exception for existing test
67+ with pytest .raises (Exception ): # Using a broad Exception for existing test
6768 receiver .receive (TEST_EVENT , jwt )
6869
6970
@@ -72,21 +73,22 @@ def test_invalid_body():
7273 receiver = WebhookReceiver (token_verifier )
7374
7475 token = AccessToken (TEST_API_KEY , TEST_API_SECRET )
75- body = " invalid body"
76+ body = ' invalid body'
7677 hash64 = base64 .b64encode (hashlib .sha256 (body .encode ()).digest ()).decode ()
7778 token .claims .sha256 = hash64
7879 jwt = token .to_jwt ()
79- with pytest .raises (Exception ): # Using a broad Exception for existing test
80+ with pytest .raises (Exception ): # Using a broad Exception for existing test
8081 receiver .receive (body , jwt )
8182
83+
8284def test_malformed_jwt ():
8385 """
8486 Test that receiving a webhook with a malformed JWT string raises an error.
8587 """
8688 token_verifier = TokenVerifier (TEST_API_KEY , TEST_API_SECRET )
8789 receiver = WebhookReceiver (token_verifier )
8890
89- malformed_jwt = " this.is.not.a.valid.jwt" # A clearly malformed string
91+ malformed_jwt = ' this.is.not.a.valid.jwt' # A clearly malformed string
9092
9193 with pytest .raises (Exception ):
9294 receiver .receive (TEST_EVENT , malformed_jwt )
0 commit comments