helpers_test.go 6.3 KB

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