Deadmon commited on
Commit
848219e
·
verified ·
1 Parent(s): 0b42f07

Upload test_webhook_and_start.py

Browse files
Files changed (1) hide show
  1. test_webhook_and_start.py +141 -0
test_webhook_and_start.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ import json
3
+ import hmac
4
+ import hashlib
5
+ import requests
6
+ from unittest.mock import patch
7
+ from http.client import HTTPConnection
8
+ import logging
9
+
10
+ # Configure logging for tests
11
+ logging.basicConfig(level=logging.INFO)
12
+ logger = logging.getLogger(__name__)
13
+
14
+ # Base URL of your Hugging Face Space
15
+ BASE_URL = "https://<your-space>.hf.space" # Replace with your Space URL
16
+ WEBHOOK_SECRET = "your-base64-encoded-secret" # Replace with your webhook secret
17
+
18
+ class TestPipecatWebhookAndStart(unittest.TestCase):
19
+ def setUp(self):
20
+ """Set up test case with common variables."""
21
+ self.webhook_url = f"{BASE_URL}/webhook"
22
+ self.start_url = f"{BASE_URL}/start"
23
+ self.valid_payload = {
24
+ "event": "dialin.connected",
25
+ "callId": "test-call-123",
26
+ "callDomain": "test.daily.co",
27
+ "From": "+12345678901",
28
+ "To": "+12345678902",
29
+ "timestamp": "2025-05-11T01:33:07.000Z"
30
+ }
31
+ self.stopped_payload = {
32
+ "event": "dialin.stopped",
33
+ "callId": "test-call-123",
34
+ "callDomain": "test.daily.co",
35
+ "timestamp": "2025-05-11T01:33:07.000Z"
36
+ }
37
+
38
+ def generate_signature(self, payload):
39
+ """Generate HMAC-SHA256 signature for a payload."""
40
+ payload_str = json.dumps(payload)
41
+ computed = hmac.new(
42
+ WEBHOOK_SECRET.encode('utf-8'),
43
+ payload_str.encode('utf-8'),
44
+ hashlib.sha256
45
+ ).hexdigest()
46
+ return computed
47
+
48
+ def test_server_availability(self):
49
+ """Test that the server is running and accessible."""
50
+ try:
51
+ response = requests.get(BASE_URL, timeout=5)
52
+ # Allow 404 for root since the app doesn't serve it
53
+ self.assertIn(response.status_code, [200, 404], f"Server not accessible: {response.status_code}")
54
+ logger.info("Server availability test passed")
55
+ except requests.RequestException as e:
56
+ self.fail(f"Server not reachable: {str(e)}")
57
+
58
+ def test_webhook_dialin_connected(self):
59
+ """Test webhook handling of dialin.connected event."""
60
+ headers = {"X-Daily-Signature": self.generate_signature(self.valid_payload)}
61
+ with patch('requests.post') as mocked_post:
62
+ mocked_post.return_value.status_code = 200
63
+ mocked_post.return_value.text = '{"status": "Bot started"}'
64
+
65
+ response = requests.post(
66
+ self.webhook_url,
67
+ json=self.valid_payload,
68
+ headers=headers,
69
+ timeout=5
70
+ )
71
+
72
+ self.assertEqual(response.status_code, 200, f"Webhook failed: {response.text}")
73
+ self.assertEqual(response.json(), {"status": "Event received"})
74
+ mocked_post.assert_called_once_with(
75
+ self.start_url,
76
+ json=self.valid_payload,
77
+ timeout=5
78
+ )
79
+ logger.info("Webhook dialin.connected test passed")
80
+
81
+ def test_webhook_dialin_stopped(self):
82
+ """Test webhook handling of dialin.stopped event."""
83
+ headers = {"X-Daily-Signature": self.generate_signature(self.stopped_payload)}
84
+ response = requests.post(
85
+ self.webhook_url,
86
+ json=self.stopped_payload,
87
+ headers=headers,
88
+ timeout=5
89
+ )
90
+
91
+ self.assertEqual(response.status_code, 200, f"Webhook failed: {response.text}")
92
+ self.assertEqual(response.json(), {"status": "Event received"})
93
+ logger.info("Webhook dialin.stopped test passed")
94
+
95
+ def test_webhook_invalid_signature(self):
96
+ """Test webhook rejects requests with invalid signature."""
97
+ headers = {"X-Daily-Signature": "invalid-signature"}
98
+ response = requests.post(
99
+ self.webhook_url,
100
+ json=self.valid_payload,
101
+ headers=headers,
102
+ timeout=5
103
+ )
104
+
105
+ self.assertEqual(response.status_code, 401, f"Expected 401 for invalid signature: {response.status_code}")
106
+ logger.info("Webhook invalid signature test passed")
107
+
108
+ def test_start_endpoint_valid_payload(self):
109
+ """Test /start endpoint with valid dial-in payload."""
110
+ response = requests.post(
111
+ self.start_url,
112
+ json=self.valid_payload,
113
+ timeout=5
114
+ )
115
+
116
+ self.assertEqual(response.status_code, 200, f"Start endpoint failed: {response.text}")
117
+ response_json = response.json()
118
+ self.assertEqual(response_json.get("status"), "Bot started")
119
+ self.assertEqual(response_json.get("bot_type"), "call_transfer")
120
+ logger.info("Start endpoint valid payload test passed")
121
+
122
+ def test_start_endpoint_invalid_payload(self):
123
+ """Test /start endpoint with invalid payload."""
124
+ invalid_payload = {"invalid": "data"}
125
+ response = requests.post(
126
+ self.start_url,
127
+ json=invalid_payload,
128
+ timeout=5
129
+ )
130
+
131
+ self.assertEqual(response.status_code, 400, f"Expected 400 for invalid payload: {response.status_code}")
132
+ logger.info("Start endpoint invalid payload test passed")
133
+
134
+ def test_404_on_root(self):
135
+ """Test that root endpoint returns 404 (as seen in logs)."""
136
+ response = requests.get(f"{BASE_URL}/?__theme=dark", timeout=5)
137
+ self.assertEqual(response.status_code, 404, f"Expected 404 for root endpoint: {response.status_code}")
138
+ logger.info("Root endpoint 404 test passed")
139
+
140
+ if __name__ == '__main__':
141
+ unittest.main()