helpers_test.go (view raw)
1package tgbotapi
2
3import (
4 "testing"
5)
6
7func TestNewWebhook(t *testing.T) {
8 result, err := NewWebhook("https://example.com/token")
9
10 if err != nil ||
11 result.URL.String() != "https://example.com/token" ||
12 result.Certificate != interface{}(nil) ||
13 result.MaxConnections != 0 ||
14 len(result.AllowedUpdates) != 0 {
15 t.Fail()
16 }
17}
18
19func TestNewWebhookWithCert(t *testing.T) {
20 exampleFile := FileID("123")
21 result, err := NewWebhookWithCert("https://example.com/token", exampleFile)
22
23 if err != nil ||
24 result.URL.String() != "https://example.com/token" ||
25 result.Certificate != exampleFile ||
26 result.MaxConnections != 0 ||
27 len(result.AllowedUpdates) != 0 {
28 t.Fail()
29 }
30}
31
32func TestNewInlineQueryResultArticle(t *testing.T) {
33 result := NewInlineQueryResultArticle("id", "title", "message")
34
35 if result.Type != "article" ||
36 result.ID != "id" ||
37 result.Title != "title" ||
38 result.InputMessageContent.(InputTextMessageContent).Text != "message" {
39 t.Fail()
40 }
41}
42
43func TestNewInlineQueryResultArticleMarkdown(t *testing.T) {
44 result := NewInlineQueryResultArticleMarkdown("id", "title", "*message*")
45
46 if result.Type != "article" ||
47 result.ID != "id" ||
48 result.Title != "title" ||
49 result.InputMessageContent.(InputTextMessageContent).Text != "*message*" ||
50 result.InputMessageContent.(InputTextMessageContent).ParseMode != "Markdown" {
51 t.Fail()
52 }
53}
54
55func TestNewInlineQueryResultArticleHTML(t *testing.T) {
56 result := NewInlineQueryResultArticleHTML("id", "title", "<b>message</b>")
57
58 if result.Type != "article" ||
59 result.ID != "id" ||
60 result.Title != "title" ||
61 result.InputMessageContent.(InputTextMessageContent).Text != "<b>message</b>" ||
62 result.InputMessageContent.(InputTextMessageContent).ParseMode != "HTML" {
63 t.Fail()
64 }
65}
66
67func TestNewInlineQueryResultGIF(t *testing.T) {
68 result := NewInlineQueryResultGIF("id", "google.com")
69
70 if result.Type != "gif" ||
71 result.ID != "id" ||
72 result.URL != "google.com" {
73 t.Fail()
74 }
75}
76
77func TestNewInlineQueryResultMPEG4GIF(t *testing.T) {
78 result := NewInlineQueryResultMPEG4GIF("id", "google.com")
79
80 if result.Type != "mpeg4_gif" ||
81 result.ID != "id" ||
82 result.URL != "google.com" {
83 t.Fail()
84 }
85}
86
87func TestNewInlineQueryResultPhoto(t *testing.T) {
88 result := NewInlineQueryResultPhoto("id", "google.com")
89
90 if result.Type != "photo" ||
91 result.ID != "id" ||
92 result.URL != "google.com" {
93 t.Fail()
94 }
95}
96
97func TestNewInlineQueryResultPhotoWithThumb(t *testing.T) {
98 result := NewInlineQueryResultPhotoWithThumb("id", "google.com", "thumb.com")
99
100 if result.Type != "photo" ||
101 result.ID != "id" ||
102 result.URL != "google.com" ||
103 result.ThumbURL != "thumb.com" {
104 t.Fail()
105 }
106}
107
108func TestNewInlineQueryResultVideo(t *testing.T) {
109 result := NewInlineQueryResultVideo("id", "google.com")
110
111 if result.Type != "video" ||
112 result.ID != "id" ||
113 result.URL != "google.com" {
114 t.Fail()
115 }
116}
117
118func TestNewInlineQueryResultAudio(t *testing.T) {
119 result := NewInlineQueryResultAudio("id", "google.com", "title")
120
121 if result.Type != "audio" ||
122 result.ID != "id" ||
123 result.URL != "google.com" ||
124 result.Title != "title" {
125 t.Fail()
126 }
127}
128
129func TestNewInlineQueryResultVoice(t *testing.T) {
130 result := NewInlineQueryResultVoice("id", "google.com", "title")
131
132 if result.Type != "voice" ||
133 result.ID != "id" ||
134 result.URL != "google.com" ||
135 result.Title != "title" {
136 t.Fail()
137 }
138}
139
140func TestNewInlineQueryResultDocument(t *testing.T) {
141 result := NewInlineQueryResultDocument("id", "google.com", "title", "mime/type")
142
143 if result.Type != "document" ||
144 result.ID != "id" ||
145 result.URL != "google.com" ||
146 result.Title != "title" ||
147 result.MimeType != "mime/type" {
148 t.Fail()
149 }
150}
151
152func TestNewInlineQueryResultLocation(t *testing.T) {
153 result := NewInlineQueryResultLocation("id", "name", 40, 50)
154
155 if result.Type != "location" ||
156 result.ID != "id" ||
157 result.Title != "name" ||
158 result.Latitude != 40 ||
159 result.Longitude != 50 {
160 t.Fail()
161 }
162}
163
164func TestNewInlineKeyboardButtonLoginURL(t *testing.T) {
165 result := NewInlineKeyboardButtonLoginURL("text", LoginURL{
166 URL: "url",
167 ForwardText: "ForwardText",
168 BotUsername: "username",
169 RequestWriteAccess: false,
170 })
171
172 if result.Text != "text" ||
173 result.LoginURL.URL != "url" ||
174 result.LoginURL.ForwardText != "ForwardText" ||
175 result.LoginURL.BotUsername != "username" ||
176 result.LoginURL.RequestWriteAccess != false {
177 t.Fail()
178 }
179}
180
181func TestNewEditMessageText(t *testing.T) {
182 edit := NewEditMessageText(ChatID, ReplyToMessageID, "new text")
183
184 if edit.Text != "new text" ||
185 edit.BaseEdit.ChatID != ChatID ||
186 edit.BaseEdit.MessageID != ReplyToMessageID {
187 t.Fail()
188 }
189}
190
191func TestNewEditMessageCaption(t *testing.T) {
192 edit := NewEditMessageCaption(ChatID, ReplyToMessageID, "new caption")
193
194 if edit.Caption != "new caption" ||
195 edit.BaseEdit.ChatID != ChatID ||
196 edit.BaseEdit.MessageID != ReplyToMessageID {
197 t.Fail()
198 }
199}
200
201func TestNewEditMessageReplyMarkup(t *testing.T) {
202 markup := InlineKeyboardMarkup{
203 InlineKeyboard: [][]InlineKeyboardButton{
204 {
205 {Text: "test"},
206 },
207 },
208 }
209
210 edit := NewEditMessageReplyMarkup(ChatID, ReplyToMessageID, markup)
211
212 if edit.ReplyMarkup.InlineKeyboard[0][0].Text != "test" ||
213 edit.BaseEdit.ChatID != ChatID ||
214 edit.BaseEdit.MessageID != ReplyToMessageID {
215 t.Fail()
216 }
217
218}
219
220func TestNewDice(t *testing.T) {
221 dice := NewDice(42)
222
223 if dice.ChatID != 42 ||
224 dice.Emoji != "" {
225 t.Fail()
226 }
227}
228
229func TestNewDiceWithEmoji(t *testing.T) {
230 dice := NewDiceWithEmoji(42, "🏀")
231
232 if dice.ChatID != 42 ||
233 dice.Emoji != "🏀" {
234 t.Fail()
235 }
236}
237
238func TestValidateWebAppData(t *testing.T) {
239 t.Run("success", func(t *testing.T) {
240 token := "5473903189:AAFnHnISQMP5UQQ5MEaoEWvxeiwNgz2CN2U"
241 initData := "query_id=AAG1bpMJAAAAALVukwmZ_H2t&user=%7B%22id%22%3A160657077%2C%22first_name%22%3A%22Yury%20R%22%2C%22last_name%22%3A%22%22%2C%22username%22%3A%22crashiura%22%2C%22language_code%22%3A%22en%22%7D&auth_date=1656804462&hash=8d6960760a573d3212deb05e20d1a34959c83d24c1bc44bb26dde49a42aa9b34"
242 result, err := ValidateWebAppData(token, initData)
243 if err != nil {
244 t.Fail()
245 }
246 if !result {
247 t.Fail()
248 }
249 })
250
251 t.Run("error", func(t *testing.T) {
252 token := "5473903189:AAFnHnISQMP5UQQ5MEaoEWvxeiwNgz2CN2U"
253 initData := "asdfasdfasdfasdfasdf"
254 result, err := ValidateWebAppData(token, initData)
255 if err == nil {
256 t.Fail()
257 }
258 if result {
259 t.Fail()
260 }
261 })
262}