slack_test.rb 7.7 KB

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