telegram_controller_test.rb 17 KB

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