telegram_controller_test.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # encoding: utf-8
  2. require 'test_helper'
  3. require 'rexml/document'
  4. require 'webmock/minitest'
  5. class TelegramControllerTest < ActionDispatch::IntegrationTest
  6. setup do
  7. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  8. end
  9. test 'basic call' do
  10. Ticket.destroy_all
  11. # configure telegram channel
  12. token = 'valid_token'
  13. bot_id = 123_456_789
  14. group_id = Group.find_by(name: 'Users').id
  15. UserInfo.current_user_id = 1
  16. Channel.where(area: 'Telegram::Bot').destroy_all
  17. # try with invalid token
  18. stub_request(:post, 'https://api.telegram.org/botnot_existing/getMe')
  19. .to_return(status: 404, body: '{"ok":false,"error_code":404,"description":"Not Found"}', headers: {})
  20. assert_raises(RuntimeError) do
  21. Telegram.check_token('not_existing')
  22. end
  23. # try valid token
  24. stub_request(:post, "https://api.telegram.org/bot#{token}/getMe")
  25. .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\"}}", headers: {})
  26. bot = Telegram.check_token(token)
  27. assert_equal(bot_id, bot['id'])
  28. stub_request(:post, "https://api.telegram.org/bot#{token}/getMe")
  29. .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\"}}", headers: {})
  30. Setting.set('http_type', 'http')
  31. assert_raises(RuntimeError) do
  32. Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!' })
  33. end
  34. # try invalid port
  35. stub_request(:post, "https://api.telegram.org:443/bot#{token}/getMe")
  36. .to_return(status: 200, body: "{\"ok\":true,\"result\":{\"id\":#{bot_id},\"first_name\":\"Chrispresso Customer Service\",\"username\":\"ChrispressoBot\"}}", headers: {})
  37. stub_request(:post, "https://api.telegram.org:443/bot#{token}/setWebhook")
  38. .with(body: { 'url' => "https://somehost.example.com:12345/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id}" })
  39. .to_return(status: 400, body: '{"ok":false,"error_code":400,"description":"Bad Request: bad webhook: Webhook can be set up only on ports 80, 88, 443 or 8443"}', headers: {})
  40. Setting.set('http_type', 'https')
  41. Setting.set('fqdn', 'somehost.example.com:12345')
  42. assert_raises(RuntimeError) do
  43. Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!' })
  44. end
  45. # try invalid host
  46. stub_request(:post, "https://api.telegram.org:443/bot#{token}/setWebhook")
  47. .with(body: { 'url' => "https://somehost.example.com/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id}" })
  48. .to_return(status: 400, body: '{"ok":false,"error_code":400,"description":"Bad Request: bad webhook: getaddrinfo: Name or service not known"}', headers: {})
  49. Setting.set('fqdn', 'somehost.example.com')
  50. assert_raises(RuntimeError) do
  51. Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!' })
  52. end
  53. # valid token, host and port
  54. stub_request(:post, "https://api.telegram.org:443/bot#{token}/setWebhook")
  55. .with(body: { 'url' => "https://example.com/api/v1/channels_telegram_webhook/callback_token?bid=#{bot_id}" })
  56. .to_return(status: 200, body: '{"ok":true,"result":true,"description":"Webhook was set"}', headers: {})
  57. Setting.set('fqdn', 'example.com')
  58. channel = Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!' })
  59. UserInfo.current_user_id = nil
  60. # start communication #1
  61. post '/api/v1/channels/telegram_webhook', params: read_messaage('personal1_message_start'), headers: @headers
  62. assert_response(404)
  63. result = JSON.parse(@response.body)
  64. post '/api/v1/channels_telegram_webhook/not_existing', params: read_messaage('personal1_message_start'), headers: @headers
  65. assert_response(422)
  66. result = JSON.parse(@response.body)
  67. assert_equal('bot param missing', result['error'])
  68. callback_url = "/api/v1/channels_telegram_webhook/not_existing?bid=#{channel.options[:bot][:id]}"
  69. post callback_url, params: read_messaage('personal1_message_start'), headers: @headers
  70. assert_response(422)
  71. result = JSON.parse(@response.body)
  72. assert_equal('invalid callback token', result['error'])
  73. callback_url = "/api/v1/channels_telegram_webhook/#{channel.options[:callback_token]}?bid=#{channel.options[:bot][:id]}"
  74. post callback_url, params: read_messaage('personal1_message_start'), headers: @headers
  75. assert_response(200)
  76. # send message1
  77. post callback_url, params: read_messaage('personal1_message_content1'), headers: @headers
  78. assert_response(200)
  79. assert_equal(1, Ticket.count)
  80. ticket = Ticket.last
  81. assert_equal('Hello, I need your Help', ticket.title)
  82. assert_equal('new', ticket.state.name)
  83. assert_equal(1, ticket.articles.count)
  84. assert_equal('Hello, I need your Help', ticket.articles.first.body)
  85. assert_equal('text/plain', ticket.articles.first.content_type)
  86. # send same message again, ignore it
  87. post callback_url, params: read_messaage('personal1_message_content1'), headers: @headers
  88. assert_response(200)
  89. ticket = Ticket.last
  90. assert_equal('Hello, I need your Help', ticket.title)
  91. assert_equal('new', ticket.state.name)
  92. assert_equal(1, ticket.articles.count)
  93. assert_equal('Hello, I need your Help', ticket.articles.first.body)
  94. assert_equal('text/plain', ticket.articles.first.content_type)
  95. # send message2
  96. post callback_url, params: read_messaage('personal1_message_content2'), headers: @headers
  97. assert_response(200)
  98. ticket = Ticket.last
  99. assert_equal('Hello, I need your Help', ticket.title)
  100. assert_equal('new', ticket.state.name)
  101. assert_equal(2, ticket.articles.count)
  102. assert_equal('Hello, I need your Help 2', ticket.articles.last.body)
  103. assert_equal('text/plain', ticket.articles.last.content_type)
  104. # send end message
  105. post callback_url, params: read_messaage('personal1_message_end'), headers: @headers
  106. assert_response(200)
  107. ticket = Ticket.last
  108. assert_equal('Hello, I need your Help', ticket.title)
  109. assert_equal('closed', ticket.state.name)
  110. assert_equal(2, ticket.articles.count)
  111. assert_equal('Hello, I need your Help 2', ticket.articles.last.body)
  112. assert_equal('text/plain', ticket.articles.last.content_type)
  113. # start communication #2
  114. post callback_url, params: read_messaage('personal2_message_start'), headers: @headers
  115. assert_response(200)
  116. # send message1
  117. post callback_url, params: read_messaage('personal2_message_content1'), headers: @headers
  118. assert_response(200)
  119. assert_equal(2, Ticket.count)
  120. ticket = Ticket.last
  121. assert_equal('Can you help me with my feature?', ticket.title)
  122. assert_equal('new', ticket.state.name)
  123. assert_equal(1, ticket.articles.count)
  124. assert_equal('Can you help me with my feature?', ticket.articles.first.body)
  125. assert_equal('text/plain', ticket.articles.first.content_type)
  126. # send message2
  127. post callback_url, params: read_messaage('personal2_message_content2'), headers: @headers
  128. assert_response(200)
  129. assert_equal(2, Ticket.count)
  130. ticket = Ticket.last
  131. assert_equal('Can you help me with my feature?', ticket.title)
  132. assert_equal('new', ticket.state.name)
  133. assert_equal(2, ticket.articles.count)
  134. assert_equal('Yes of course! <b>lalal</b>', ticket.articles.last.body)
  135. assert_equal('text/plain', ticket.articles.last.content_type)
  136. # start communication #3
  137. post callback_url, params: read_messaage('personal3_message_start'), headers: @headers
  138. assert_response(200)
  139. # send message1
  140. post callback_url, params: read_messaage('personal3_message_content1'), headers: @headers
  141. assert_response(200)
  142. assert_equal(3, Ticket.count)
  143. ticket = Ticket.last
  144. assert_equal('Can you help me with my feature?', ticket.title)
  145. assert_equal('new', ticket.state.name)
  146. assert_equal(1, ticket.articles.count)
  147. assert_equal('Can you help me with my feature?', ticket.articles.last.body)
  148. assert_equal('text/plain', ticket.articles.last.content_type)
  149. # send message2
  150. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  151. .with(body: { 'file_id' => 'ABC-123VabcOcv123w0ABHywrcPqfrbAYIABC' })
  152. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123VabcOcv123w0ABHywrcPqfrbAYIABC","file_path":"abc123"}}', headers: {})
  153. stub_request(:get, "https://api.telegram.org/file/bot#{token}/abc123")
  154. .to_return(status: 200, body: 'ABC1', headers: {})
  155. post callback_url, params: read_messaage('personal3_message_content2'), headers: @headers
  156. assert_response(200)
  157. assert_equal(3, Ticket.count)
  158. ticket = Ticket.last
  159. assert_equal('Can you help me with my feature?', ticket.title)
  160. assert_equal('new', ticket.state.name)
  161. assert_equal(2, ticket.articles.count)
  162. assert_match(/<img style="width:360px;height:327px;"/i, ticket.articles.last.body)
  163. assert_equal('text/html', ticket.articles.last.content_type)
  164. # send message3
  165. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  166. .with(body: { 'file_id' => 'AAQCABO0I4INAATATQAB5HWPq4XgxQACAg' })
  167. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123AAQCABO0I4INAATATQAB5HWPq4XgxQACAg","file_path":"abc123"}}', headers: {})
  168. stub_request(:get, "https://api.telegram.org/file/bot#{token}/abc123")
  169. .to_return(status: 200, body: 'ABC2', headers: {})
  170. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  171. .with(body: { 'file_id' => 'BQADAgADDgAD7x6ZSC_-1LMkOEmoAg' })
  172. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123BQADAgADDgAD7x6ZSC_-1LMkOEmoAg","file_path":"abc123"}}', headers: {})
  173. post callback_url, params: read_messaage('personal3_message_content3'), headers: @headers
  174. assert_response(200)
  175. assert_equal(3, Ticket.count)
  176. ticket = Ticket.last
  177. assert_equal('Can you help me with my feature?', ticket.title)
  178. assert_equal('new', ticket.state.name)
  179. assert_equal(3, ticket.articles.count)
  180. assert_match(/<img style="width:200px;height:200px;"/i, ticket.articles.last.body)
  181. assert_equal('text/html', ticket.articles.last.content_type)
  182. assert_equal(1, ticket.articles.last.attachments.count)
  183. # update message1
  184. post callback_url, params: read_messaage('personal3_message_content4'), headers: @headers
  185. assert_response(200)
  186. assert_equal(3, Ticket.count)
  187. ticket = Ticket.last
  188. assert_equal('Can you help me with my feature?', ticket.title)
  189. assert_equal('new', ticket.state.name)
  190. assert_equal(3, ticket.articles.count)
  191. assert_match(/<img style="width:200px;height:200px;"/i, ticket.articles.last.body)
  192. assert_equal('text/html', ticket.articles.last.content_type)
  193. assert_equal(1, ticket.articles.last.attachments.count)
  194. assert_equal('UPDATE: 1231444', ticket.articles.first.body)
  195. assert_equal('text/plain', ticket.articles.first.content_type)
  196. # send voice5
  197. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  198. .with(body: { 'file_id' => 'AwADAgADVQADCEIYSZwyOmSZK9iZAg' })
  199. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123AwADAgADVQADCEIYSZwyOmSZK9iZAg","file_path":"abc123"}}', headers: {})
  200. post callback_url, params: read_messaage('personal3_message_content5'), headers: @headers
  201. assert_response(200)
  202. assert_equal(3, Ticket.count)
  203. ticket = Ticket.last
  204. assert_equal('Can you help me with my feature?', ticket.title)
  205. assert_equal('new', ticket.state.name)
  206. assert_equal(4, ticket.articles.count)
  207. assert_equal('text/html', ticket.articles.last.content_type)
  208. assert_equal(1, ticket.articles.last.attachments.count)
  209. # start communication #4 - with sticker
  210. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  211. .with(body: { 'file_id' => 'AAQDABO3-e4qAASs6ZOjJUT7tQ4lAAIC' })
  212. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123AAQDABO3-e4qAASs6ZOjJUT7tQ4lAAIC","file_path":"abc123"}}', headers: {})
  213. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  214. .with(body: { 'file_id' => 'BQADAwAD0QIAAqbJWAAB8OkQqgtDQe0C' })
  215. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123BQADAwAD0QIAAqbJWAAB8OkQqgtDQe0C","file_path":"abc123"}}', headers: {})
  216. post callback_url, params: read_messaage('personal4_message_content1'), headers: @headers
  217. assert_response(200)
  218. assert_equal(4, Ticket.count)
  219. ticket = Ticket.last
  220. if Rails.application.config.db_4bytes_utf8
  221. assert_equal('💻', ticket.title)
  222. else
  223. assert_equal('', ticket.title)
  224. end
  225. assert_equal('new', ticket.state.name)
  226. assert_equal(1, ticket.articles.count)
  227. assert_match(/<img style="/i, ticket.articles.last.body)
  228. assert_equal('text/html', ticket.articles.last.content_type)
  229. assert_equal(1, ticket.articles.last.attachments.count)
  230. # start communication #5 - with photo
  231. stub_request(:post, "https://api.telegram.org/bot#{token}/getFile")
  232. .with(body: { 'file_id' => 'AgADAgADwacxGxk5MUmim45lijOwsKk1Sw0ABNQoaI8BwR_z_2MFAAEC' })
  233. .to_return(status: 200, body: '{"result":{"file_size":123,"file_id":"ABC-123AgADAgADwacxGxk5MUmim45lijOwsKk1Sw0ABNQoaI8BwR_z_2MFAAEC","file_path":"abc123"}}', headers: {})
  234. post callback_url, params: read_messaage('personal5_message_content1'), headers: @headers
  235. assert_response(200)
  236. assert_equal(5, Ticket.count)
  237. ticket = Ticket.last
  238. assert_equal('-', ticket.title)
  239. assert_equal('new', ticket.state.name)
  240. assert_equal(1, ticket.articles.count)
  241. assert_match(/<img style="/i, ticket.articles.last.body)
  242. assert_equal('text/html', ticket.articles.last.content_type)
  243. assert_equal(0, ticket.articles.last.attachments.count)
  244. post callback_url, params: read_messaage('personal5_message_content2'), headers: @headers
  245. assert_response(200)
  246. assert_equal(5, Ticket.count)
  247. ticket = Ticket.last
  248. assert_equal('Hello, I need your Help', ticket.title)
  249. assert_equal('new', ticket.state.name)
  250. assert_equal(2, ticket.articles.count)
  251. assert_match(/Hello, I need your Help/i, ticket.articles.last.body)
  252. assert_equal('text/plain', ticket.articles.last.content_type)
  253. assert_equal(0, ticket.articles.last.attachments.count)
  254. end
  255. def read_messaage(file)
  256. File.read("test/fixtures/telegram/#{file}.json")
  257. end
  258. end