slack_test.rb 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'integration_test_helper'
  3. require 'slack-ruby-client' # Only load this gem when it is really used.
  4. class SlackTest < ActiveSupport::TestCase
  5. # needed to check correct behavior
  6. slack_group = Group.create_if_not_exists(
  7. name: 'Slack',
  8. updated_by_id: 1,
  9. created_by_id: 1
  10. )
  11. # check
  12. test 'base' do
  13. if !ENV['SLACK_CI_CHANNEL']
  14. raise "ERROR: Need SLACK_CI_CHANNEL - hint SLACK_CI_CHANNEL='ci-zammad'"
  15. end
  16. if !ENV['SLACK_CI_WEBHOOK']
  17. raise "ERROR: Need SLACK_CI_WEBHOOK - hint SLACK_CI_WEBHOOK='https://hooks.slack.com/services/...'"
  18. end
  19. if !ENV['SLACK_CI_CHECKER_TOKEN']
  20. raise "ERROR: Need SLACK_CI_CHECKER_TOKEN - hint SLACK_CI_CHECKER_TOKEN='...'"
  21. end
  22. channel = ENV['SLACK_CI_CHANNEL']
  23. webhook = ENV['SLACK_CI_WEBHOOK']
  24. # set system mode to done / to activate
  25. Setting.set('system_init_done', true)
  26. Setting.set('slack_integration', true)
  27. items = [
  28. {
  29. group_ids: [slack_group.id],
  30. types: %w[create update reminder_reached],
  31. webhook: webhook,
  32. channel: channel,
  33. username: 'zammad bot',
  34. expand: false,
  35. }
  36. ]
  37. Setting.set('slack_config', { items: items })
  38. # case 1
  39. customer = User.find(2)
  40. hash = hash_gen
  41. text = "#{rand_word}... #{hash}"
  42. default_group = Group.first
  43. ticket1 = Ticket.create(
  44. title: text,
  45. customer_id: customer.id,
  46. group_id: default_group.id,
  47. state: Ticket::State.find_by(name: 'new'),
  48. priority: Ticket::Priority.find_by(name: '2 normal'),
  49. updated_by_id: 1,
  50. created_by_id: 1,
  51. )
  52. Ticket::Article.create(
  53. ticket_id: ticket1.id,
  54. body: text,
  55. type: Ticket::Article::Type.find_by(name: 'note'),
  56. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  57. internal: false,
  58. updated_by_id: 1,
  59. created_by_id: 1,
  60. )
  61. TransactionDispatcher.commit
  62. Scheduler.worker(true)
  63. # check if message exists
  64. assert_equal(0, slack_check(channel, hash))
  65. ticket1.state = Ticket::State.find_by(name: 'open')
  66. ticket1.save
  67. TransactionDispatcher.commit
  68. Scheduler.worker(true)
  69. # check if message exists
  70. assert_equal(0, slack_check(channel, hash))
  71. # case 2
  72. hash = hash_gen
  73. text = "#{rand_word}... #{hash}"
  74. ticket2 = Ticket.create(
  75. title: text,
  76. customer_id: customer.id,
  77. group_id: slack_group.id,
  78. state: Ticket::State.find_by(name: 'new'),
  79. priority: Ticket::Priority.find_by(name: '2 normal'),
  80. updated_by_id: 1,
  81. created_by_id: 1,
  82. )
  83. Ticket::Article.create(
  84. ticket_id: ticket2.id,
  85. body: text,
  86. type: Ticket::Article::Type.find_by(name: 'note'),
  87. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  88. internal: false,
  89. updated_by_id: 1,
  90. created_by_id: 1,
  91. )
  92. TransactionDispatcher.commit
  93. Scheduler.worker(true)
  94. # check if message exists
  95. assert_equal(1, slack_check(channel, hash))
  96. hash = hash_gen
  97. text = "#{rand_word}... #{hash}"
  98. ticket2.title = text
  99. ticket2.save
  100. TransactionDispatcher.commit
  101. Scheduler.worker(true)
  102. # check if message exists
  103. assert_equal(1, slack_check(channel, hash))
  104. ticket2.state = Ticket::State.find_by(name: 'pending reminder')
  105. ticket2.pending_time = 2.days.ago
  106. ticket2.save
  107. TransactionDispatcher.commit
  108. Scheduler.worker(true)
  109. # check if message exists
  110. assert_equal(2, slack_check(channel, hash))
  111. Ticket.process_pending
  112. TransactionDispatcher.commit
  113. Scheduler.worker(true)
  114. # check if message exists
  115. assert_equal(3, slack_check(channel, hash))
  116. Ticket.process_pending
  117. TransactionDispatcher.commit
  118. Scheduler.worker(true)
  119. # check if message exists
  120. assert_equal(3, slack_check(channel, hash))
  121. items = [
  122. {
  123. group_ids: slack_group.id.to_s,
  124. types: 'create',
  125. webhook: webhook,
  126. channel: channel,
  127. username: 'zammad bot',
  128. expand: false,
  129. }
  130. ]
  131. Setting.set('slack_config', { items: items })
  132. # case 3
  133. customer = User.find(2)
  134. hash = hash_gen
  135. text = "#{rand_word}... #{hash}"
  136. default_group = Group.first
  137. ticket3 = Ticket.create(
  138. title: text,
  139. customer_id: customer.id,
  140. group_id: default_group.id,
  141. state: Ticket::State.find_by(name: 'new'),
  142. priority: Ticket::Priority.find_by(name: '2 normal'),
  143. updated_by_id: 1,
  144. created_by_id: 1,
  145. )
  146. Ticket::Article.create(
  147. ticket_id: ticket3.id,
  148. body: text,
  149. type: Ticket::Article::Type.find_by(name: 'note'),
  150. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  151. internal: false,
  152. updated_by_id: 1,
  153. created_by_id: 1,
  154. )
  155. TransactionDispatcher.commit
  156. Scheduler.worker(true)
  157. # check if message exists
  158. assert_equal(0, slack_check(channel, hash))
  159. ticket3.state = Ticket::State.find_by(name: 'open')
  160. ticket3.save
  161. TransactionDispatcher.commit
  162. Scheduler.worker(true)
  163. # check if message exists
  164. assert_equal(0, slack_check(channel, hash))
  165. # case 4
  166. hash = hash_gen
  167. text = "#{rand_word}... #{hash}"
  168. ticket4 = Ticket.create(
  169. title: text,
  170. customer_id: customer.id,
  171. group_id: slack_group.id,
  172. state: Ticket::State.find_by(name: 'new'),
  173. priority: Ticket::Priority.find_by(name: '2 normal'),
  174. updated_by_id: 1,
  175. created_by_id: 1,
  176. )
  177. Ticket::Article.create(
  178. ticket_id: ticket4.id,
  179. body: text,
  180. type: Ticket::Article::Type.find_by(name: 'note'),
  181. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  182. internal: false,
  183. updated_by_id: 1,
  184. created_by_id: 1,
  185. )
  186. TransactionDispatcher.commit
  187. Scheduler.worker(true)
  188. # check if message exists
  189. assert_equal(1, slack_check(channel, hash))
  190. hash = hash_gen
  191. text = "#{rand_word}... #{hash}"
  192. ticket4.title = text
  193. ticket4.save
  194. TransactionDispatcher.commit
  195. Scheduler.worker(true)
  196. # check if message exists
  197. assert_equal(0, slack_check(channel, hash))
  198. end
  199. def hash_gen
  200. SecureRandom.hex(10)
  201. end
  202. def rand_word
  203. words = [
  204. 'dog',
  205. 'cat',
  206. 'house',
  207. 'home',
  208. 'yesterday',
  209. 'tomorrow',
  210. 'new york',
  211. 'berlin',
  212. 'coffee script',
  213. 'java script',
  214. 'bob smith',
  215. 'be open',
  216. 'really nice',
  217. 'stay tuned',
  218. 'be a good boy',
  219. 'invent new things',
  220. ]
  221. words.sample
  222. end
  223. def slack_check(channel_name, search_for)
  224. Slack.configure do |config|
  225. config.token = ENV['SLACK_CI_CHECKER_TOKEN']
  226. end
  227. client = Slack::Web::Client.new
  228. client.auth_test
  229. channels = client.conversations_list['channels']
  230. channel_id = nil
  231. channels.each do |channel|
  232. next if channel['name'] != channel_name
  233. channel_id = channel['id']
  234. end
  235. if !channel_id
  236. raise "ERROR: No such channel '#{channel_name}'"
  237. end
  238. channel_history = client.conversations_history(channel: channel_id)
  239. if !channel_history
  240. raise "ERROR: No history for channel #{channel_name}/#{channel_id}"
  241. end
  242. if !channel_history['messages']
  243. raise "ERROR: No history messages for channel #{channel_name}/#{channel_id}"
  244. end
  245. message_count = 0
  246. channel_history['messages'].each do |message|
  247. next if !message['text']
  248. if message['text'].match?(%r{#{search_for}}i)
  249. message_count += 1
  250. p "SUCCESS: message with #{search_for} found #{message_count} time(s)!"
  251. end
  252. end
  253. message_count
  254. end
  255. end