Skip to content

Commit e096783

Browse files
committed
simple test
1 parent 08f7e84 commit e096783

File tree

1 file changed

+5
-42
lines changed

1 file changed

+5
-42
lines changed

livekit-api/tests/test_webhook.py

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -81,51 +81,14 @@ def test_invalid_body():
8181
with pytest.raises(Exception): # Using a broad Exception for existing test
8282
receiver.receive(body, jwt)
8383

84-
85-
def test_mismatched_api_key_secret():
86-
"""
87-
Test that receiving a webhook with a token signed by a different API key/secret
88-
raises an error.
89-
"""
90-
TEST_API_KEY_BAD = "badkey"
91-
TEST_API_SECRET_BAD = "badsecret"
92-
93-
token_verifier = TokenVerifier(TEST_API_KEY, TEST_API_SECRET)
94-
receiver = WebhookReceiver(token_verifier)
95-
96-
# Token signed with incorrect credentials
97-
token = AccessToken(TEST_API_KEY_BAD, TEST_API_SECRET_BAD)
98-
hash64 = base64.b64encode(hashlib.sha256(TEST_EVENT.encode()).digest()).decode()
99-
token.claims.sha256 = hash64
100-
jwt = token.to_jwt()
101-
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):
107-
receiver.receive(TEST_EVENT, jwt)
108-
109-
110-
def test_expired_token():
84+
def test_malformed_jwt():
11185
"""
112-
Test that receiving a webhook with an expired token raises an ExpiredSignatureError.
86+
Test that receiving a webhook with a malformed JWT string raises an error.
11387
"""
11488
token_verifier = TokenVerifier(TEST_API_KEY, TEST_API_SECRET)
11589
receiver = WebhookReceiver(token_verifier)
11690

117-
token = AccessToken(TEST_API_KEY, TEST_API_SECRET)
118-
hash64 = base64.b64encode(hashlib.sha256(TEST_EVENT.encode()).digest()).decode()
119-
token.claims.sha256 = hash64
120-
121-
# Set the token's expiration to a time in the past
122-
# Using datetime.now(UTC) to address the DeprecationWarning
123-
token.claims.exp = datetime.now(UTC) - timedelta(seconds=60) # 1 minute ago
124-
125-
jwt = token.to_jwt()
91+
malformed_jwt = "this.is.not.a.valid.jwt" # A clearly malformed string
12692

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):
131-
receiver.receive(TEST_EVENT, jwt)
93+
with pytest.raises(Exception):
94+
receiver.receive(TEST_EVENT, malformed_jwt)

0 commit comments

Comments
 (0)