slack_test.rb 7.8 KB

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