2018-10-09 06:34:34 +02:00
|
|
|
package tgbotapi
|
2016-04-14 21:00:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2020-09-30 22:19:29 +02:00
|
|
|
func TestNewWebhook(t *testing.T) {
|
|
|
|
result, err := NewWebhook("https://example.com/token")
|
|
|
|
|
|
|
|
if err != nil ||
|
|
|
|
result.URL.String() != "https://example.com/token" ||
|
|
|
|
result.Certificate != interface{}(nil) ||
|
|
|
|
result.MaxConnections != 0 ||
|
|
|
|
len(result.AllowedUpdates) != 0 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewWebhookWithCert(t *testing.T) {
|
2021-08-20 21:31:52 +02:00
|
|
|
exampleFile := FileID("123")
|
2020-09-30 22:19:29 +02:00
|
|
|
result, err := NewWebhookWithCert("https://example.com/token", exampleFile)
|
|
|
|
|
|
|
|
if err != nil ||
|
|
|
|
result.URL.String() != "https://example.com/token" ||
|
|
|
|
result.Certificate != exampleFile ||
|
|
|
|
result.MaxConnections != 0 ||
|
|
|
|
len(result.AllowedUpdates) != 0 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 21:00:45 +02:00
|
|
|
func TestNewInlineQueryResultArticle(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
result := NewInlineQueryResultArticle("id", "title", "message")
|
2016-04-14 21:00:45 +02:00
|
|
|
|
|
|
|
if result.Type != "article" ||
|
|
|
|
result.ID != "id" ||
|
|
|
|
result.Title != "title" ||
|
2018-10-09 06:34:34 +02:00
|
|
|
result.InputMessageContent.(InputTextMessageContent).Text != "message" {
|
2016-06-02 22:41:55 +02:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewInlineQueryResultArticleMarkdown(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
result := NewInlineQueryResultArticleMarkdown("id", "title", "*message*")
|
2016-06-02 22:41:55 +02:00
|
|
|
|
|
|
|
if result.Type != "article" ||
|
|
|
|
result.ID != "id" ||
|
|
|
|
result.Title != "title" ||
|
2018-10-09 06:34:34 +02:00
|
|
|
result.InputMessageContent.(InputTextMessageContent).Text != "*message*" ||
|
|
|
|
result.InputMessageContent.(InputTextMessageContent).ParseMode != "Markdown" {
|
2016-04-14 21:00:45 +02:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 22:41:55 +02:00
|
|
|
func TestNewInlineQueryResultArticleHTML(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
result := NewInlineQueryResultArticleHTML("id", "title", "<b>message</b>")
|
2016-06-02 22:41:55 +02:00
|
|
|
|
|
|
|
if result.Type != "article" ||
|
|
|
|
result.ID != "id" ||
|
|
|
|
result.Title != "title" ||
|
2018-10-09 06:34:34 +02:00
|
|
|
result.InputMessageContent.(InputTextMessageContent).Text != "<b>message</b>" ||
|
|
|
|
result.InputMessageContent.(InputTextMessageContent).ParseMode != "HTML" {
|
2016-06-02 22:41:55 +02:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 21:00:45 +02:00
|
|
|
func TestNewInlineQueryResultGIF(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
result := NewInlineQueryResultGIF("id", "google.com")
|
2016-04-14 21:00:45 +02:00
|
|
|
|
|
|
|
if result.Type != "gif" ||
|
|
|
|
result.ID != "id" ||
|
|
|
|
result.URL != "google.com" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewInlineQueryResultMPEG4GIF(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
result := NewInlineQueryResultMPEG4GIF("id", "google.com")
|
2016-04-14 21:00:45 +02:00
|
|
|
|
|
|
|
if result.Type != "mpeg4_gif" ||
|
|
|
|
result.ID != "id" ||
|
|
|
|
result.URL != "google.com" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewInlineQueryResultPhoto(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
result := NewInlineQueryResultPhoto("id", "google.com")
|
2016-04-14 21:00:45 +02:00
|
|
|
|
|
|
|
if result.Type != "photo" ||
|
|
|
|
result.ID != "id" ||
|
|
|
|
result.URL != "google.com" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 20:44:48 +02:00
|
|
|
func TestNewInlineQueryResultPhotoWithThumb(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
result := NewInlineQueryResultPhotoWithThumb("id", "google.com", "thumb.com")
|
2016-07-26 20:44:48 +02:00
|
|
|
|
|
|
|
if result.Type != "photo" ||
|
|
|
|
result.ID != "id" ||
|
|
|
|
result.URL != "google.com" ||
|
|
|
|
result.ThumbURL != "thumb.com" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 21:00:45 +02:00
|
|
|
func TestNewInlineQueryResultVideo(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
result := NewInlineQueryResultVideo("id", "google.com")
|
2016-04-14 21:00:45 +02:00
|
|
|
|
|
|
|
if result.Type != "video" ||
|
|
|
|
result.ID != "id" ||
|
|
|
|
result.URL != "google.com" {
|
2016-04-14 21:04:35 +02:00
|
|
|
t.Fail()
|
2016-04-14 21:00:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewInlineQueryResultAudio(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
result := NewInlineQueryResultAudio("id", "google.com", "title")
|
2016-04-14 21:00:45 +02:00
|
|
|
|
|
|
|
if result.Type != "audio" ||
|
|
|
|
result.ID != "id" ||
|
|
|
|
result.URL != "google.com" ||
|
|
|
|
result.Title != "title" {
|
2016-04-14 21:04:35 +02:00
|
|
|
t.Fail()
|
2016-04-14 21:00:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewInlineQueryResultVoice(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
result := NewInlineQueryResultVoice("id", "google.com", "title")
|
2016-04-14 21:00:45 +02:00
|
|
|
|
|
|
|
if result.Type != "voice" ||
|
|
|
|
result.ID != "id" ||
|
|
|
|
result.URL != "google.com" ||
|
|
|
|
result.Title != "title" {
|
2016-04-14 21:04:35 +02:00
|
|
|
t.Fail()
|
2016-04-14 21:00:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewInlineQueryResultDocument(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
result := NewInlineQueryResultDocument("id", "google.com", "title", "mime/type")
|
2016-04-14 21:00:45 +02:00
|
|
|
|
|
|
|
if result.Type != "document" ||
|
|
|
|
result.ID != "id" ||
|
|
|
|
result.URL != "google.com" ||
|
|
|
|
result.Title != "title" ||
|
|
|
|
result.MimeType != "mime/type" {
|
2016-04-14 21:04:35 +02:00
|
|
|
t.Fail()
|
2016-04-14 21:00:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewInlineQueryResultLocation(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
result := NewInlineQueryResultLocation("id", "name", 40, 50)
|
2016-04-14 21:00:45 +02:00
|
|
|
|
|
|
|
if result.Type != "location" ||
|
|
|
|
result.ID != "id" ||
|
|
|
|
result.Title != "name" ||
|
|
|
|
result.Latitude != 40 ||
|
2017-12-29 20:00:02 +01:00
|
|
|
result.Longitude != 50 {
|
2016-04-14 21:04:35 +02:00
|
|
|
t.Fail()
|
2016-04-14 21:00:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 15:41:36 +02:00
|
|
|
func TestNewInlineKeyboardButtonLoginURL(t *testing.T) {
|
|
|
|
result := NewInlineKeyboardButtonLoginURL("text", LoginURL{
|
|
|
|
URL: "url",
|
|
|
|
ForwardText: "ForwardText",
|
|
|
|
BotUsername: "username",
|
|
|
|
RequestWriteAccess: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
if result.Text != "text" ||
|
|
|
|
result.LoginURL.URL != "url" ||
|
|
|
|
result.LoginURL.ForwardText != "ForwardText" ||
|
|
|
|
result.LoginURL.BotUsername != "username" ||
|
|
|
|
result.LoginURL.RequestWriteAccess != false {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 21:00:45 +02:00
|
|
|
func TestNewEditMessageText(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
edit := NewEditMessageText(ChatID, ReplyToMessageID, "new text")
|
2016-04-14 21:00:45 +02:00
|
|
|
|
|
|
|
if edit.Text != "new text" ||
|
|
|
|
edit.BaseEdit.ChatID != ChatID ||
|
|
|
|
edit.BaseEdit.MessageID != ReplyToMessageID {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewEditMessageCaption(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
edit := NewEditMessageCaption(ChatID, ReplyToMessageID, "new caption")
|
2016-04-14 21:00:45 +02:00
|
|
|
|
|
|
|
if edit.Caption != "new caption" ||
|
|
|
|
edit.BaseEdit.ChatID != ChatID ||
|
|
|
|
edit.BaseEdit.MessageID != ReplyToMessageID {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewEditMessageReplyMarkup(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
markup := InlineKeyboardMarkup{
|
|
|
|
InlineKeyboard: [][]InlineKeyboardButton{
|
2020-07-21 21:20:12 +02:00
|
|
|
{
|
|
|
|
{Text: "test"},
|
2016-04-14 21:00:45 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:34:34 +02:00
|
|
|
edit := NewEditMessageReplyMarkup(ChatID, ReplyToMessageID, markup)
|
2016-04-14 21:00:45 +02:00
|
|
|
|
|
|
|
if edit.ReplyMarkup.InlineKeyboard[0][0].Text != "test" ||
|
|
|
|
edit.BaseEdit.ChatID != ChatID ||
|
|
|
|
edit.BaseEdit.MessageID != ReplyToMessageID {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-11-06 05:29:48 +01:00
|
|
|
|
|
|
|
func TestNewDice(t *testing.T) {
|
|
|
|
dice := NewDice(42)
|
|
|
|
|
|
|
|
if dice.ChatID != 42 ||
|
|
|
|
dice.Emoji != "" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDiceWithEmoji(t *testing.T) {
|
|
|
|
dice := NewDiceWithEmoji(42, "🏀")
|
|
|
|
|
|
|
|
if dice.ChatID != 42 ||
|
|
|
|
dice.Emoji != "🏀" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
2022-07-03 01:37:12 +02:00
|
|
|
|
|
|
|
func TestValidateWebAppData(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
token := "5473903189:AAFnHnISQMP5UQQ5MEaoEWvxeiwNgz2CN2U"
|
|
|
|
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"
|
|
|
|
result, err := ValidateWebAppData(token, initData)
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if !result {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error", func(t *testing.T) {
|
|
|
|
token := "5473903189:AAFnHnISQMP5UQQ5MEaoEWvxeiwNgz2CN2U"
|
|
|
|
initData := "asdfasdfasdfasdfasdf"
|
|
|
|
result, err := ValidateWebAppData(token, initData)
|
|
|
|
if err == nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if result {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|