telegram_controller_test.rb 14 KB

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