telegram_controller_test.rb 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # encoding: utf-8
  2. require 'test_helper'
  3. require 'rexml/document'
  4. class TelegramControllerTest < ActionDispatch::IntegrationTest
  5. setup do
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. # configure telegram channel
  8. token = ENV['TELEGRAM_TOKEN']
  9. group_id = Group.find_by(name: 'Users').id
  10. #bot = Telegram.check_token(token)
  11. #Setting.set('http_type', 'http')
  12. Setting.set('http_type', 'https')
  13. Setting.set('fqdn', 'me.zammad.com')
  14. Channel.where(area: 'Telegram::Bot').destroy_all
  15. UserInfo.current_user_id = 1
  16. @channel = Telegram.create_or_update_channel(token, { group_id: group_id, welcome: 'hi!' })
  17. groups = Group.where(name: 'Users')
  18. roles = Role.where(name: %w(Agent))
  19. agent = User.create_or_update(
  20. login: 'telegram-agent@example.com',
  21. firstname: 'E',
  22. lastname: 'S',
  23. email: 'telegram-agent@example.com',
  24. password: 'agentpw',
  25. active: true,
  26. roles: roles,
  27. groups: groups,
  28. )
  29. UserInfo.current_user_id = nil
  30. end
  31. test 'basic call' do
  32. Ticket.destroy_all
  33. # start communication #1
  34. post '/api/v1/channels/telegram_webhook', read_messaage('personal1_message_start'), @headers
  35. assert_response(404)
  36. result = JSON.parse(@response.body)
  37. post '/api/v1/channels_telegram_webhook/not_existing', read_messaage('personal1_message_start'), @headers
  38. assert_response(422)
  39. result = JSON.parse(@response.body)
  40. assert_equal('bot param missing', result['error'])
  41. callback_url = "/api/v1/channels_telegram_webhook/not_existing?bid=#{@channel.options[:bot][:id]}"
  42. post callback_url, read_messaage('personal1_message_start'), @headers
  43. assert_response(422)
  44. result = JSON.parse(@response.body)
  45. assert_equal('invalid callback token', result['error'])
  46. callback_url = "/api/v1/channels_telegram_webhook/#{@channel.options[:callback_token]}?bid=#{@channel.options[:bot][:id]}"
  47. post callback_url, read_messaage('personal1_message_start'), @headers
  48. assert_response(200)
  49. # send message1
  50. post callback_url, read_messaage('personal1_message_content1'), @headers
  51. assert_response(200)
  52. assert_equal(1, Ticket.count)
  53. ticket = Ticket.last
  54. assert_equal('Hello, I need your Help', ticket.title)
  55. assert_equal('new', ticket.state.name)
  56. assert_equal(1, ticket.articles.count)
  57. assert_equal('Hello, I need your Help', ticket.articles.first.body)
  58. assert_equal('text/plain', ticket.articles.first.content_type)
  59. # send same message again, ignore it
  60. post callback_url, read_messaage('personal1_message_content1'), @headers
  61. assert_response(200)
  62. ticket = Ticket.last
  63. assert_equal('Hello, I need your Help', ticket.title)
  64. assert_equal('new', ticket.state.name)
  65. assert_equal(1, ticket.articles.count)
  66. assert_equal('Hello, I need your Help', ticket.articles.first.body)
  67. assert_equal('text/plain', ticket.articles.first.content_type)
  68. # send message2
  69. post callback_url, read_messaage('personal1_message_content2'), @headers
  70. assert_response(200)
  71. ticket = Ticket.last
  72. assert_equal('Hello, I need your Help', ticket.title)
  73. assert_equal('new', ticket.state.name)
  74. assert_equal(2, ticket.articles.count)
  75. assert_equal('Hello, I need your Help 2', ticket.articles.last.body)
  76. assert_equal('text/plain', ticket.articles.last.content_type)
  77. # send end message
  78. post callback_url, read_messaage('personal1_message_end'), @headers
  79. assert_response(200)
  80. ticket = Ticket.last
  81. assert_equal('Hello, I need your Help', ticket.title)
  82. assert_equal('closed', ticket.state.name)
  83. assert_equal(2, ticket.articles.count)
  84. assert_equal('Hello, I need your Help 2', ticket.articles.last.body)
  85. assert_equal('text/plain', ticket.articles.last.content_type)
  86. # start communication #2
  87. post callback_url, read_messaage('personal2_message_start'), @headers
  88. assert_response(200)
  89. # send message1
  90. post callback_url, read_messaage('personal2_message_content1'), @headers
  91. assert_response(200)
  92. assert_equal(2, Ticket.count)
  93. ticket = Ticket.last
  94. assert_equal('Can you help me with my feature?', ticket.title)
  95. assert_equal('new', ticket.state.name)
  96. assert_equal(1, ticket.articles.count)
  97. assert_equal('Can you help me with my feature?', ticket.articles.first.body)
  98. assert_equal('text/plain', ticket.articles.first.content_type)
  99. # send message2
  100. post callback_url, read_messaage('personal2_message_content2'), @headers
  101. assert_response(200)
  102. assert_equal(2, Ticket.count)
  103. ticket = Ticket.last
  104. assert_equal('Can you help me with my feature?', ticket.title)
  105. assert_equal('new', ticket.state.name)
  106. assert_equal(2, ticket.articles.count)
  107. assert_equal('Yes of course! <b>lalal</b>', ticket.articles.last.body)
  108. assert_equal('text/plain', ticket.articles.last.content_type)
  109. # start communication #3
  110. post callback_url, read_messaage('personal3_message_start'), @headers
  111. assert_response(200)
  112. # send message1
  113. post callback_url, read_messaage('personal3_message_content1'), @headers
  114. assert_response(200)
  115. assert_equal(3, Ticket.count)
  116. ticket = Ticket.last
  117. assert_equal('Can you help me with my feature?', ticket.title)
  118. assert_equal('new', ticket.state.name)
  119. assert_equal(1, ticket.articles.count)
  120. assert_equal('Can you help me with my feature?', ticket.articles.last.body)
  121. assert_equal('text/plain', ticket.articles.last.content_type)
  122. # send message2
  123. post callback_url, read_messaage('personal3_message_content2'), @headers
  124. assert_response(200)
  125. assert_equal(3, Ticket.count)
  126. ticket = Ticket.last
  127. assert_equal('Can you help me with my feature?', ticket.title)
  128. assert_equal('new', ticket.state.name)
  129. assert_equal(2, ticket.articles.count)
  130. assert_match(/<img style="width:360px;height:327px;"/i, ticket.articles.last.body)
  131. assert_equal('text/html', ticket.articles.last.content_type)
  132. # send message3
  133. post callback_url, read_messaage('personal3_message_content3'), @headers
  134. assert_response(200)
  135. assert_equal(3, Ticket.count)
  136. ticket = Ticket.last
  137. assert_equal('Can you help me with my feature?', ticket.title)
  138. assert_equal('new', ticket.state.name)
  139. assert_equal(3, ticket.articles.count)
  140. assert_match(/<img style="width:200px;height:200px;"/i, ticket.articles.last.body)
  141. assert_equal('text/html', ticket.articles.last.content_type)
  142. assert_equal(1, ticket.articles.last.attachments.count)
  143. # update message1
  144. post callback_url, read_messaage('personal3_message_content4'), @headers
  145. assert_response(200)
  146. assert_equal(3, Ticket.count)
  147. ticket = Ticket.last
  148. assert_equal('Can you help me with my feature?', ticket.title)
  149. assert_equal('new', ticket.state.name)
  150. assert_equal(3, ticket.articles.count)
  151. assert_match(/<img style="width:200px;height:200px;"/i, ticket.articles.last.body)
  152. assert_equal('text/html', ticket.articles.last.content_type)
  153. assert_equal(1, ticket.articles.last.attachments.count)
  154. assert_equal('UPDATE: 1231444', ticket.articles.first.body)
  155. assert_equal('text/plain', ticket.articles.first.content_type)
  156. # send voice5
  157. post callback_url, read_messaage('personal3_message_content5'), @headers
  158. assert_response(200)
  159. assert_equal(3, Ticket.count)
  160. ticket = Ticket.last
  161. assert_equal('Can you help me with my feature?', ticket.title)
  162. assert_equal('new', ticket.state.name)
  163. assert_equal(4, ticket.articles.count)
  164. assert_equal('text/html', ticket.articles.last.content_type)
  165. assert_equal(1, ticket.articles.last.attachments.count)
  166. # start communication #4 - with sticker
  167. post callback_url, read_messaage('personal4_message_content1'), @headers
  168. assert_response(200)
  169. assert_equal(4, Ticket.count)
  170. ticket = Ticket.last
  171. assert_equal('💻', ticket.title)
  172. assert_equal('new', ticket.state.name)
  173. assert_equal(1, ticket.articles.count)
  174. assert_match(/<img style="/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. # start communication #5 - with photo
  178. post callback_url, read_messaage('personal5_message_content1'), @headers
  179. assert_response(200)
  180. assert_equal(5, Ticket.count)
  181. ticket = Ticket.last
  182. assert_equal('-', ticket.title)
  183. assert_equal('new', ticket.state.name)
  184. assert_equal(1, ticket.articles.count)
  185. assert_match(/<img style="/i, ticket.articles.last.body)
  186. assert_equal('text/html', ticket.articles.last.content_type)
  187. assert_equal(0, ticket.articles.last.attachments.count)
  188. post callback_url, read_messaage('personal5_message_content2'), @headers
  189. assert_response(200)
  190. assert_equal(5, Ticket.count)
  191. ticket = Ticket.last
  192. assert_equal('Hello, I need your Help', ticket.title)
  193. assert_equal('new', ticket.state.name)
  194. assert_equal(2, ticket.articles.count)
  195. assert_match(/Hello, I need your Help/i, ticket.articles.last.body)
  196. assert_equal('text/plain', ticket.articles.last.content_type)
  197. assert_equal(0, ticket.articles.last.attachments.count)
  198. end
  199. def read_messaage(file)
  200. File.read("test/fixtures/telegram/#{file}.json")
  201. end
  202. end