all repos — telegram-bot-api @ 17d3e395d52d40e76b22f043424937a4e8d2f58c

Golang bindings for the Telegram Bot API

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 TestNewInlineKeyboardButtonSwitchInlineQueryChoosenChat(t *testing.T) {
182	result := NewInlineKeyboardButtonSwitchInlineQueryChoosenChat("text", SwitchInlineQueryChosenChat{
183		Query:             "query",
184		AllowUserChats:    false,
185		AllowBotChats:     false,
186		AllowGroupChats:   false,
187		AllowChannelChats: false,
188	})
189
190	if result.Text != "text" ||
191		result.SwitchInlineQueryChosenChat.Query != "query" ||
192		result.SwitchInlineQueryChosenChat.AllowUserChats != false ||
193		result.SwitchInlineQueryChosenChat.AllowBotChats != false ||
194		result.SwitchInlineQueryChosenChat.AllowGroupChats != false ||
195		result.SwitchInlineQueryChosenChat.AllowChannelChats != false {
196		t.Fail()
197	}
198}
199
200func TestNewEditMessageText(t *testing.T) {
201	edit := NewEditMessageText(ChatID, ReplyToMessageID, "new text")
202
203	if edit.Text != "new text" ||
204		edit.BaseEdit.ChatID != ChatID ||
205		edit.BaseEdit.MessageID != ReplyToMessageID {
206		t.Fail()
207	}
208}
209
210func TestNewEditMessageCaption(t *testing.T) {
211	edit := NewEditMessageCaption(ChatID, ReplyToMessageID, "new caption")
212
213	if edit.Caption != "new caption" ||
214		edit.BaseEdit.ChatID != ChatID ||
215		edit.BaseEdit.MessageID != ReplyToMessageID {
216		t.Fail()
217	}
218}
219
220func TestNewEditMessageReplyMarkup(t *testing.T) {
221	markup := InlineKeyboardMarkup{
222		InlineKeyboard: [][]InlineKeyboardButton{
223			{
224				{Text: "test"},
225			},
226		},
227	}
228
229	edit := NewEditMessageReplyMarkup(ChatID, ReplyToMessageID, markup)
230
231	if edit.ReplyMarkup.InlineKeyboard[0][0].Text != "test" ||
232		edit.BaseEdit.ChatID != ChatID ||
233		edit.BaseEdit.MessageID != ReplyToMessageID {
234		t.Fail()
235	}
236
237}
238
239func TestNewDice(t *testing.T) {
240	dice := NewDice(42)
241
242	if dice.ChatID != 42 ||
243		dice.Emoji != "" {
244		t.Fail()
245	}
246}
247
248func TestNewDiceWithEmoji(t *testing.T) {
249	dice := NewDiceWithEmoji(42, "🏀")
250
251	if dice.ChatID != 42 ||
252		dice.Emoji != "🏀" {
253		t.Fail()
254	}
255}
256
257func TestValidateWebAppData(t *testing.T) {
258	t.Run("success", func(t *testing.T) {
259		token := "5473903189:AAFnHnISQMP5UQQ5MEaoEWvxeiwNgz2CN2U"
260		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"
261		result, err := ValidateWebAppData(token, initData)
262		if err != nil {
263			t.Fail()
264		}
265		if !result {
266			t.Fail()
267		}
268	})
269
270	t.Run("error", func(t *testing.T) {
271		token := "5473903189:AAFnHnISQMP5UQQ5MEaoEWvxeiwNgz2CN2U"
272		initData := "asdfasdfasdfasdfasdf"
273		result, err := ValidateWebAppData(token, initData)
274		if err == nil {
275			t.Fail()
276		}
277		if result {
278			t.Fail()
279		}
280	})
281}