|
| 1 | +import json |
| 2 | +import time |
| 3 | +import hmac |
| 4 | +import hashlib |
| 5 | +from unittest import mock |
| 6 | + |
| 7 | +from elevenlabs.client import ElevenLabs |
| 8 | +from elevenlabs.errors import BadRequestError |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | + |
| 13 | +def test_construct_event_valid_signature(): |
| 14 | + """Test webhook event construction with valid signature.""" |
| 15 | + # Setup |
| 16 | + client = ElevenLabs() |
| 17 | + webhook_secret = "test_secret" |
| 18 | + payload = {"event_type": "speech.completed", "id": "123456"} |
| 19 | + |
| 20 | + # Create a valid signature |
| 21 | + body = json.dumps(payload) |
| 22 | + timestamp = str(int(time.time())) |
| 23 | + message = f"{timestamp}.{body}" |
| 24 | + signature = "v0=" + hmac.new( |
| 25 | + webhook_secret.encode('utf-8'), |
| 26 | + message.encode('utf-8'), |
| 27 | + hashlib.sha256 |
| 28 | + ).hexdigest() |
| 29 | + sig_header = f"t={timestamp},{signature}" |
| 30 | + |
| 31 | + # Verify event construction |
| 32 | + event = client.webhooks.construct_event(body, sig_header, webhook_secret) |
| 33 | + assert event == payload, "Event should match the original payload" |
| 34 | + |
| 35 | + |
| 36 | +def test_construct_event_missing_signature(): |
| 37 | + """Test webhook event construction with missing signature header.""" |
| 38 | + client = ElevenLabs() |
| 39 | + webhook_secret = "test_secret" |
| 40 | + payload = {"event_type": "speech.completed", "id": "123456"} |
| 41 | + |
| 42 | + with pytest.raises(BadRequestError) as excinfo: |
| 43 | + client.webhooks.construct_event(payload, "", webhook_secret) |
| 44 | + |
| 45 | + assert "Missing signature header" in str(excinfo.value) |
| 46 | + |
| 47 | + |
| 48 | +def test_construct_event_invalid_signature_format(): |
| 49 | + """Test webhook event construction with invalid signature format.""" |
| 50 | + client = ElevenLabs() |
| 51 | + webhook_secret = "test_secret" |
| 52 | + payload = {"event_type": "speech.completed", "id": "123456"} |
| 53 | + body = json.dumps(payload) |
| 54 | + sig_header = "invalid_format" |
| 55 | + |
| 56 | + with pytest.raises(BadRequestError) as excinfo: |
| 57 | + client.webhooks.construct_event(body, sig_header, webhook_secret) |
| 58 | + |
| 59 | + assert "No signature hash found with expected scheme v0" in str(excinfo.value) |
| 60 | + |
| 61 | + |
| 62 | +def test_construct_event_expired_timestamp(): |
| 63 | + """Test webhook event construction with expired timestamp.""" |
| 64 | + client = ElevenLabs() |
| 65 | + webhook_secret = "test_secret" |
| 66 | + payload = {"event_type": "speech.completed", "id": "123456"} |
| 67 | + |
| 68 | + # Create an expired timestamp (31 minutes old) |
| 69 | + expired_time = int(time.time()) - 31 * 60 |
| 70 | + timestamp = str(expired_time) |
| 71 | + |
| 72 | + body = json.dumps(payload) |
| 73 | + message = f"{timestamp}.{body}" |
| 74 | + signature = "v0=" + hmac.new( |
| 75 | + webhook_secret.encode('utf-8'), |
| 76 | + message.encode('utf-8'), |
| 77 | + hashlib.sha256 |
| 78 | + ).hexdigest() |
| 79 | + sig_header = f"t={timestamp},{signature}" |
| 80 | + |
| 81 | + with pytest.raises(BadRequestError) as excinfo: |
| 82 | + client.webhooks.construct_event(body, sig_header, webhook_secret) |
| 83 | + |
| 84 | + assert "Timestamp outside the tolerance zone" in str(excinfo.value) |
| 85 | + |
| 86 | + |
| 87 | +def test_construct_event_invalid_signature(): |
| 88 | + """Test webhook event construction with invalid signature.""" |
| 89 | + client = ElevenLabs() |
| 90 | + webhook_secret = "test_secret" |
| 91 | + payload = {"event_type": "speech.completed", "id": "123456"} |
| 92 | + body = json.dumps(payload) |
| 93 | + |
| 94 | + timestamp = str(int(time.time())) |
| 95 | + sig_header = f"t={timestamp},v0=invalid_signature" |
| 96 | + |
| 97 | + with pytest.raises(BadRequestError) as excinfo: |
| 98 | + client.webhooks.construct_event(body, sig_header, webhook_secret) |
| 99 | + |
| 100 | + assert "Signature hash does not match" in str(excinfo.value) |
| 101 | + |
| 102 | + |
| 103 | +def test_construct_event_missing_secret(): |
| 104 | + """Test webhook event construction with missing secret.""" |
| 105 | + client = ElevenLabs() |
| 106 | + payload = {"event_type": "speech.completed", "id": "123456"} |
| 107 | + body = json.dumps(payload) |
| 108 | + |
| 109 | + timestamp = str(int(time.time())) |
| 110 | + sig_header = f"t={timestamp},v0=some_signature" |
| 111 | + |
| 112 | + with pytest.raises(BadRequestError) as excinfo: |
| 113 | + client.webhooks.construct_event(body, sig_header, "") |
| 114 | + |
| 115 | + assert "Webhook secret not configured" in str(excinfo.value) |
| 116 | + |
| 117 | + |
| 118 | +@mock.patch('time.time') |
| 119 | +def test_construct_event_mocked_time(mock_time): |
| 120 | + """Test webhook event construction with mocked time.""" |
| 121 | + mock_time.return_value = 1600000000 |
| 122 | + |
| 123 | + client = ElevenLabs() |
| 124 | + webhook_secret = "test_secret" |
| 125 | + payload = {"event_type": "speech.completed", "id": "123456"} |
| 126 | + |
| 127 | + # Create a valid signature with fixed timestamp |
| 128 | + body = json.dumps(payload) |
| 129 | + timestamp = "1600000000" |
| 130 | + message = f"{timestamp}.{body}" |
| 131 | + signature = "v0=" + hmac.new( |
| 132 | + webhook_secret.encode('utf-8'), |
| 133 | + message.encode('utf-8'), |
| 134 | + hashlib.sha256 |
| 135 | + ).hexdigest() |
| 136 | + sig_header = f"t={timestamp},{signature}" |
| 137 | + |
| 138 | + # Verify event construction |
| 139 | + event = client.webhooks.construct_event(body, sig_header, webhook_secret) |
| 140 | + assert event == payload, "Event should match the original payload" |
0 commit comments