forked from gear5labs/chenpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-webhooks.sh
More file actions
191 lines (174 loc) · 5.11 KB
/
test-webhooks.sh
File metadata and controls
191 lines (174 loc) · 5.11 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
# Test script for webhook idempotency
# Usage: ./test-webhooks.sh [base-url]
# Example: ./test-webhooks.sh http://localhost:3000
BASE_URL="${1:-http://localhost:3000}"
API_URL="${BASE_URL}/api"
echo "🧪 Testing Webhook Idempotency"
echo "================================"
echo "Base URL: $BASE_URL"
echo ""
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Test 1: Telegram webhook - first request
echo -e "${YELLOW}Test 1: Telegram webhook (first request)${NC}"
RESPONSE1=$(curl -s -X POST "${API_URL}/webhook/telegram" \
-H "Content-Type: application/json" \
-d '{
"update_id": 123456789,
"message": {
"message_id": 1,
"from": {
"id": 987654321,
"first_name": "Test",
"username": "testuser",
"is_bot": false
},
"chat": {
"id": 987654321,
"type": "private"
},
"date": 1640000000,
"text": "Hello, bot!"
}
}')
echo "Response: $RESPONSE1"
if echo "$RESPONSE1" | grep -q '"success":true'; then
echo -e "${GREEN}✅ First request processed successfully${NC}"
else
echo -e "${RED}❌ First request failed${NC}"
fi
echo ""
# Test 2: Telegram webhook - duplicate request
echo -e "${YELLOW}Test 2: Telegram webhook (duplicate request)${NC}"
RESPONSE2=$(curl -s -X POST "${API_URL}/webhook/telegram" \
-H "Content-Type: application/json" \
-d '{
"update_id": 123456789,
"message": {
"message_id": 1,
"from": {
"id": 987654321,
"first_name": "Test",
"username": "testuser",
"is_bot": false
},
"chat": {
"id": 987654321,
"type": "private"
},
"date": 1640000000,
"text": "Hello, bot!"
}
}')
echo "Response: $RESPONSE2"
if echo "$RESPONSE2" | grep -q "already processed"; then
echo -e "${GREEN}✅ Duplicate detected correctly${NC}"
else
echo -e "${RED}❌ Duplicate not detected${NC}"
fi
echo ""
# Test 3: Telegram webhook - new update_id
echo -e "${YELLOW}Test 3: Telegram webhook (new update_id)${NC}"
RESPONSE3=$(curl -s -X POST "${API_URL}/webhook/telegram" \
-H "Content-Type: application/json" \
-d '{
"update_id": 123456790,
"message": {
"message_id": 2,
"from": {
"id": 987654321,
"first_name": "Test",
"username": "testuser",
"is_bot": false
},
"chat": {
"id": 987654321,
"type": "private"
},
"date": 1640000001,
"text": "Another message"
}
}')
echo "Response: $RESPONSE3"
if echo "$RESPONSE3" | grep -q '"success":true' && ! echo "$RESPONSE3" | grep -q "already processed"; then
echo -e "${GREEN}✅ New message processed successfully${NC}"
else
echo -e "${RED}❌ New message not processed correctly${NC}"
fi
echo ""
# Test 4: Discord webhook - ping
echo -e "${YELLOW}Test 4: Discord webhook (ping)${NC}"
RESPONSE4=$(curl -s -X POST "${API_URL}/webhook/discord" \
-H "Content-Type: application/json" \
-d '{
"id": "1234567890123456789",
"type": 1,
"timestamp": "2024-01-01T00:00:00.000Z"
}')
echo "Response: $RESPONSE4"
if echo "$RESPONSE4" | grep -q '"type":1'; then
echo -e "${GREEN}✅ Discord ping handled correctly${NC}"
else
echo -e "${RED}❌ Discord ping not handled correctly${NC}"
fi
echo ""
# Test 5: Discord webhook - first interaction
echo -e "${YELLOW}Test 5: Discord webhook (first interaction)${NC}"
RESPONSE5=$(curl -s -X POST "${API_URL}/webhook/discord" \
-H "Content-Type: application/json" \
-d '{
"id": "9876543210987654321",
"type": 2,
"timestamp": "2024-01-01T00:00:00.000Z",
"channel_id": "1111111111111111111",
"guild_id": "2222222222222222222",
"author": {
"id": "3333333333333333333",
"username": "testuser",
"discriminator": "0001"
},
"content": "Hello, Discord bot!"
}')
echo "Response: $RESPONSE5"
if echo "$RESPONSE5" | grep -q '"success":true'; then
echo -e "${GREEN}✅ First Discord interaction processed${NC}"
else
echo -e "${RED}❌ First Discord interaction failed${NC}"
fi
echo ""
# Test 6: Discord webhook - duplicate interaction
echo -e "${YELLOW}Test 6: Discord webhook (duplicate interaction)${NC}"
RESPONSE6=$(curl -s -X POST "${API_URL}/webhook/discord" \
-H "Content-Type: application/json" \
-d '{
"id": "9876543210987654321",
"type": 2,
"timestamp": "2024-01-01T00:00:00.000Z",
"channel_id": "1111111111111111111",
"guild_id": "2222222222222222222",
"author": {
"id": "3333333333333333333",
"username": "testuser",
"discriminator": "0001"
},
"content": "Hello, Discord bot!"
}')
echo "Response: $RESPONSE6"
if echo "$RESPONSE6" | grep -q "already processed"; then
echo -e "${GREEN}✅ Discord duplicate detected correctly${NC}"
else
echo -e "${RED}❌ Discord duplicate not detected${NC}"
fi
echo ""
# Summary
echo "================================"
echo -e "${YELLOW}📊 Test Summary${NC}"
echo "================================"
echo "All tests completed. Review results above."
echo ""
echo "To verify in database:"
echo " psql -d your_database -c 'SELECT * FROM webhook_idempotency ORDER BY \"createdAt\" DESC LIMIT 10;'"