-
Notifications
You must be signed in to change notification settings - Fork 523
Expand file tree
/
Copy pathwebhook_test.go
More file actions
160 lines (127 loc) · 3.12 KB
/
webhook_test.go
File metadata and controls
160 lines (127 loc) · 3.12 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
package telebot
import (
"bytes"
"encoding/json"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWebhook_Poll(t *testing.T) {
t.Run("graceful shutdown", func(t *testing.T) {
webhook := &Webhook{
Listen: "127.0.0.1:80",
IgnoreSetWebhook: true,
}
pref := defaultSettings()
pref.Offline = true
pref.Poller = webhook
b, err := NewBot(pref)
require.NoError(t, err)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
b.Start()
}()
// Give bot time to start
time.Sleep(100 * time.Millisecond)
b.Stop()
wg.Wait()
})
t.Run("no Listen", func(t *testing.T) {
webhook := &Webhook{
Listen: "",
IgnoreSetWebhook: true,
}
pref := defaultSettings()
pref.Offline = true
pref.Poller = webhook
b, err := NewBot(pref)
require.NoError(t, err)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
b.Start()
}()
// Give bot time to start
time.Sleep(100 * time.Millisecond)
b.Stop()
wg.Wait()
})
}
func TestWebhook_ServeHTTP(t *testing.T) {
webhook := &Webhook{
IgnoreSetWebhook: true,
}
pref := defaultSettings()
pref.Offline = true
b, err := NewBot(pref)
require.NoError(t, err)
dest := make(chan Update, 1)
webhook.dest = dest
webhook.bot = b
t.Run("valid update", func(t *testing.T) {
update := Update{
ID: 123,
Message: &Message{
ID: 1,
Text: "test message",
Chat: &Chat{ID: 456},
},
}
body, _ := json.Marshal(update)
req := httptest.NewRequest("POST", "/", bytes.NewReader(body))
w := httptest.NewRecorder()
webhook.ServeHTTP(w, req)
select {
case received := <-dest:
assert.Equal(t, update.ID, received.ID)
assert.Equal(t, update.Message.Text, received.Message.Text)
case <-time.After(time.Second):
t.Fatal("timeout waiting for update")
}
})
t.Run("invalid JSON", func(t *testing.T) {
req := httptest.NewRequest("POST", "/", bytes.NewReader([]byte("invalid json")))
w := httptest.NewRecorder()
webhook.ServeHTTP(w, req)
select {
case <-dest:
t.Fatal("should not receive update for invalid JSON")
case <-time.After(100 * time.Millisecond):
// Expected - no update should be sent
}
})
t.Run("secret token validation", func(t *testing.T) {
webhook.SecretToken = "secret123"
update := Update{ID: 123}
body, _ := json.Marshal(update)
// Without secret token
req := httptest.NewRequest("POST", "/", bytes.NewReader(body))
w := httptest.NewRecorder()
webhook.ServeHTTP(w, req)
select {
case <-dest:
t.Fatal("should not receive update without secret token")
case <-time.After(100 * time.Millisecond):
// Expected
}
// With correct secret token
req = httptest.NewRequest("POST", "/", bytes.NewReader(body))
req.Header.Set("X-Telegram-Bot-Api-Secret-Token", "secret123")
w = httptest.NewRecorder()
webhook.ServeHTTP(w, req)
select {
case received := <-dest:
assert.Equal(t, update.ID, received.ID)
case <-time.After(time.Second):
t.Fatal("timeout waiting for update")
}
// Reset for other tests
webhook.SecretToken = ""
})
}