File size: 4,623 Bytes
9410704
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import json
import time
import hmac
import hashlib
from unittest import mock

from elevenlabs.client import ElevenLabs
from elevenlabs.errors import BadRequestError

import pytest


def test_construct_event_valid_signature():
    """Test webhook event construction with valid signature."""
    # Setup
    client = ElevenLabs()
    webhook_secret = "test_secret"
    payload = {"event_type": "speech.completed", "id": "123456"}

    # Create a valid signature
    body = json.dumps(payload)
    timestamp = str(int(time.time()))
    message = f"{timestamp}.{body}"
    signature = "v0=" + hmac.new(
        webhook_secret.encode('utf-8'),
        message.encode('utf-8'),
        hashlib.sha256
    ).hexdigest()
    sig_header = f"t={timestamp},{signature}"

    # Verify event construction
    event = client.webhooks.construct_event(body, sig_header, webhook_secret)
    assert event == payload, "Event should match the original payload"


def test_construct_event_missing_signature():
    """Test webhook event construction with missing signature header."""
    client = ElevenLabs()
    webhook_secret = "test_secret"
    payload = {"event_type": "speech.completed", "id": "123456"}

    with pytest.raises(BadRequestError) as excinfo:
        client.webhooks.construct_event(payload, "", webhook_secret)

    assert "Missing signature header" in str(excinfo.value)


def test_construct_event_invalid_signature_format():
    """Test webhook event construction with invalid signature format."""
    client = ElevenLabs()
    webhook_secret = "test_secret"
    payload = {"event_type": "speech.completed", "id": "123456"}
    body = json.dumps(payload)
    sig_header = "invalid_format"

    with pytest.raises(BadRequestError) as excinfo:
        client.webhooks.construct_event(body, sig_header, webhook_secret)

    assert "No signature hash found with expected scheme v0" in str(excinfo.value)


def test_construct_event_expired_timestamp():
    """Test webhook event construction with expired timestamp."""
    client = ElevenLabs()
    webhook_secret = "test_secret"
    payload = {"event_type": "speech.completed", "id": "123456"}

    # Create an expired timestamp (31 minutes old)
    expired_time = int(time.time()) - 31 * 60
    timestamp = str(expired_time)

    body = json.dumps(payload)
    message = f"{timestamp}.{body}"
    signature = "v0=" + hmac.new(
        webhook_secret.encode('utf-8'),
        message.encode('utf-8'),
        hashlib.sha256
    ).hexdigest()
    sig_header = f"t={timestamp},{signature}"

    with pytest.raises(BadRequestError) as excinfo:
        client.webhooks.construct_event(body, sig_header, webhook_secret)

    assert "Timestamp outside the tolerance zone" in str(excinfo.value)


def test_construct_event_invalid_signature():
    """Test webhook event construction with invalid signature."""
    client = ElevenLabs()
    webhook_secret = "test_secret"
    payload = {"event_type": "speech.completed", "id": "123456"}
    body = json.dumps(payload)

    timestamp = str(int(time.time()))
    sig_header = f"t={timestamp},v0=invalid_signature"

    with pytest.raises(BadRequestError) as excinfo:
        client.webhooks.construct_event(body, sig_header, webhook_secret)

    assert "Signature hash does not match" in str(excinfo.value)


def test_construct_event_missing_secret():
    """Test webhook event construction with missing secret."""
    client = ElevenLabs()
    payload = {"event_type": "speech.completed", "id": "123456"}
    body = json.dumps(payload)

    timestamp = str(int(time.time()))
    sig_header = f"t={timestamp},v0=some_signature"

    with pytest.raises(BadRequestError) as excinfo:
        client.webhooks.construct_event(body, sig_header, "")

    assert "Webhook secret not configured" in str(excinfo.value)


@mock.patch('time.time')
def test_construct_event_mocked_time(mock_time):
    """Test webhook event construction with mocked time."""
    mock_time.return_value = 1600000000

    client = ElevenLabs()
    webhook_secret = "test_secret"
    payload = {"event_type": "speech.completed", "id": "123456"}

    # Create a valid signature with fixed timestamp
    body = json.dumps(payload)
    timestamp = "1600000000"
    message = f"{timestamp}.{body}"
    signature = "v0=" + hmac.new(
        webhook_secret.encode('utf-8'),
        message.encode('utf-8'),
        hashlib.sha256
    ).hexdigest()
    sig_header = f"t={timestamp},{signature}"

    # Verify event construction
    event = client.webhooks.construct_event(body, sig_header, webhook_secret)
    assert event == payload, "Event should match the original payload"