ticket_notification_test.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class TicketNotificationTest < ActiveSupport::TestCase
  4. # create agent1 & agent2
  5. groups = Group.where( :name => 'Users' )
  6. roles = Role.where( :name => 'Agent' )
  7. agent1 = User.create_or_update(
  8. :login => 'ticket-notification-agent1@example.com',
  9. :firstname => 'Notification',
  10. :lastname => 'Agent1',
  11. :email => 'ticket-notification-agent1@example.com',
  12. :password => 'agentpw',
  13. :active => true,
  14. :roles => roles,
  15. :groups => groups,
  16. :preferences => {
  17. :locale => 'de',
  18. },
  19. :updated_by_id => 1,
  20. :created_by_id => 1,
  21. )
  22. agent2 = User.create_or_update(
  23. :login => 'ticket-notification-agent2@example.com',
  24. :firstname => 'Notification',
  25. :lastname => 'Agent2',
  26. :email => 'ticket-notification-agent2@example.com',
  27. :password => 'agentpw',
  28. :active => true,
  29. :roles => roles,
  30. :groups => groups,
  31. :preferences => {
  32. :locale => 'en_CA',
  33. },
  34. :updated_by_id => 1,
  35. :created_by_id => 1,
  36. )
  37. Group.create_if_not_exists(
  38. :name => 'WithoutAccess',
  39. :note => 'Test for notification check.',
  40. :updated_by_id => 1,
  41. :created_by_id => 1
  42. )
  43. # create customer
  44. roles = Role.where( :name => 'Customer' )
  45. customer = User.create_or_update(
  46. :login => 'ticket-notification-customer@example.com',
  47. :firstname => 'Notification',
  48. :lastname => 'Customer',
  49. :email => 'ticket-notification-customer@example.com',
  50. :password => 'agentpw',
  51. :active => true,
  52. :roles => roles,
  53. :groups => groups,
  54. :updated_by_id => 1,
  55. :created_by_id => 1,
  56. )
  57. test 'ticket notification simple' do
  58. # create ticket in group
  59. ticket1 = Ticket.create(
  60. :title => 'some notification test 1',
  61. :group => Group.lookup( :name => 'Users'),
  62. :customer => customer,
  63. :state => Ticket::State.lookup( :name => 'new' ),
  64. :priority => Ticket::Priority.lookup( :name => '2 normal' ),
  65. :updated_by_id => customer.id,
  66. :created_by_id => customer.id,
  67. )
  68. article_inbound = Ticket::Article.create(
  69. :ticket_id => ticket1.id,
  70. :from => 'some_sender@example.com',
  71. :to => 'some_recipient@example.com',
  72. :subject => 'some subject',
  73. :message_id => 'some@id',
  74. :body => 'some message',
  75. :internal => false,
  76. :sender => Ticket::Article::Sender.where(:name => 'Customer').first,
  77. :type => Ticket::Article::Type.where(:name => 'email').first,
  78. :updated_by_id => customer.id,
  79. :created_by_id => customer.id,
  80. )
  81. assert( ticket1, "ticket created - ticket notification simple" )
  82. # execute ticket events
  83. Observer::Ticket::Notification.transaction
  84. #puts Delayed::Job.all.inspect
  85. Delayed::Worker.new.work_off
  86. # verify notifications to agent1 + agent2
  87. assert_equal( 1, notification_check(ticket1, agent1), ticket1.id )
  88. assert_equal( 1, notification_check(ticket1, agent2), ticket1.id )
  89. # update ticket attributes
  90. ticket1.title = "#{ticket1.title} - #2"
  91. ticket1.priority = Ticket::Priority.lookup( :name => '3 high' )
  92. ticket1.save
  93. # execute ticket events
  94. Observer::Ticket::Notification.transaction
  95. #puts Delayed::Job.all.inspect
  96. Delayed::Worker.new.work_off
  97. # verify notifications to agent1 + agent2
  98. assert_equal( 2, notification_check(ticket1, agent1), ticket1.id )
  99. assert_equal( 2, notification_check(ticket1, agent2), ticket1.id )
  100. # add article to ticket
  101. article_note = Ticket::Article.create(
  102. :ticket_id => ticket1.id,
  103. :from => 'some person',
  104. :subject => 'some note',
  105. :body => 'some message',
  106. :internal => true,
  107. :sender => Ticket::Article::Sender.where(:name => 'Agent').first,
  108. :type => Ticket::Article::Type.where(:name => 'note').first,
  109. :updated_by_id => 1,
  110. :created_by_id => 1,
  111. )
  112. # verify notifications to agent1 + agent2
  113. # create ticket with agent1 as owner
  114. ticket2 = Ticket.create(
  115. :title => 'some notification test 2',
  116. :group => Group.lookup( :name => 'Users'),
  117. :customer_id => 2,
  118. :owner_id => agent1.id,
  119. :state => Ticket::State.lookup( :name => 'new' ),
  120. :priority => Ticket::Priority.lookup( :name => '2 normal' ),
  121. :updated_by_id => agent1.id,
  122. :created_by_id => agent1.id,
  123. )
  124. article_inbound = Ticket::Article.create(
  125. :ticket_id => ticket2.id,
  126. :from => 'some_sender@example.com',
  127. :to => 'some_recipient@example.com',
  128. :subject => 'some subject',
  129. :message_id => 'some@id',
  130. :body => 'some message',
  131. :internal => false,
  132. :sender => Ticket::Article::Sender.where(:name => 'Agent').first,
  133. :type => Ticket::Article::Type.where(:name => 'phone').first,
  134. :updated_by_id => agent1.id,
  135. :created_by_id => agent1.id,
  136. )
  137. # execute ticket events
  138. Observer::Ticket::Notification.transaction
  139. #puts Delayed::Job.all.inspect
  140. Delayed::Worker.new.work_off
  141. assert( ticket2, "ticket created" )
  142. # verify notifications to no one
  143. assert_equal( 0, notification_check(ticket2, agent1), ticket2.id )
  144. assert_equal( 0, notification_check(ticket2, agent2), ticket2.id )
  145. # update ticket
  146. ticket2.title = "#{ticket2.title} - #2"
  147. ticket2.updated_by_id = agent1.id
  148. ticket2.priority = Ticket::Priority.lookup( :name => '3 high' )
  149. ticket2.save
  150. # execute ticket events
  151. Observer::Ticket::Notification.transaction
  152. #puts Delayed::Job.all.inspect
  153. Delayed::Worker.new.work_off
  154. # verify notifications to no one
  155. assert_equal( 0, notification_check(ticket2, agent1), ticket2.id )
  156. assert_equal( 0, notification_check(ticket2, agent2), ticket2.id )
  157. # update ticket
  158. ticket2.title = "#{ticket2.title} - #3"
  159. ticket2.updated_by_id = agent2.id
  160. ticket2.priority = Ticket::Priority.lookup( :name => '2 normal' )
  161. ticket2.save
  162. # execute ticket events
  163. Observer::Ticket::Notification.transaction
  164. #puts Delayed::Job.all.inspect
  165. Delayed::Worker.new.work_off
  166. # verify notifications to agent1 and not to agent2
  167. assert_equal( 1, notification_check(ticket2, agent1), ticket2.id )
  168. assert_equal( 0, notification_check(ticket2, agent2), ticket2.id )
  169. # create ticket with agent2 and agent1 as owner
  170. ticket3 = Ticket.create(
  171. :title => 'some notification test 3',
  172. :group => Group.lookup( :name => 'Users'),
  173. :customer_id => 2,
  174. :owner_id => agent1.id,
  175. :state => Ticket::State.lookup( :name => 'new' ),
  176. :priority => Ticket::Priority.lookup( :name => '2 normal' ),
  177. :updated_by_id => agent2.id,
  178. :created_by_id => agent2.id,
  179. )
  180. article_inbound = Ticket::Article.create(
  181. :ticket_id => ticket3.id,
  182. :from => 'some_sender@example.com',
  183. :to => 'some_recipient@example.com',
  184. :subject => 'some subject',
  185. :message_id => 'some@id',
  186. :body => 'some message',
  187. :internal => false,
  188. :sender => Ticket::Article::Sender.where(:name => 'Agent').first,
  189. :type => Ticket::Article::Type.where(:name => 'phone').first,
  190. :updated_by_id => agent2.id,
  191. :created_by_id => agent2.id,
  192. )
  193. # execute ticket events
  194. Observer::Ticket::Notification.transaction
  195. #puts Delayed::Job.all.inspect
  196. Delayed::Worker.new.work_off
  197. assert( ticket3, "ticket created" )
  198. # verify notifications to agent1 and not to agent2
  199. assert_equal( 1, notification_check(ticket3, agent1), ticket3.id )
  200. assert_equal( 0, notification_check(ticket3, agent2), ticket3.id )
  201. # update ticket
  202. ticket3.title = "#{ticket3.title} - #2"
  203. ticket3.updated_by_id = agent1.id
  204. ticket3.priority = Ticket::Priority.lookup( :name => '3 high' )
  205. ticket3.save
  206. # execute ticket events
  207. Observer::Ticket::Notification.transaction
  208. #puts Delayed::Job.all.inspect
  209. Delayed::Worker.new.work_off
  210. # verify notifications to no one
  211. assert_equal( 1, notification_check(ticket3, agent1), ticket3.id )
  212. assert_equal( 0, notification_check(ticket3, agent2), ticket3.id )
  213. # update ticket
  214. ticket3.title = "#{ticket3.title} - #3"
  215. ticket3.updated_by_id = agent2.id
  216. ticket3.priority = Ticket::Priority.lookup( :name => '2 normal' )
  217. ticket3.save
  218. # execute ticket events
  219. Observer::Ticket::Notification.transaction
  220. #puts Delayed::Job.all.inspect
  221. Delayed::Worker.new.work_off
  222. # verify notifications to agent1 and not to agent2
  223. assert_equal( 2, notification_check(ticket3, agent1), ticket3.id )
  224. assert_equal( 0, notification_check(ticket3, agent2), ticket3.id )
  225. delete = ticket1.destroy
  226. assert( delete, "ticket1 destroy" )
  227. delete = ticket2.destroy
  228. assert( delete, "ticket2 destroy" )
  229. delete = ticket3.destroy
  230. assert( delete, "ticket3 destroy" )
  231. end
  232. test 'ticket notification events' do
  233. # create ticket in group
  234. ticket1 = Ticket.create(
  235. :title => 'some notification event test 1',
  236. :group => Group.lookup( :name => 'Users'),
  237. :customer => customer,
  238. :state => Ticket::State.lookup( :name => 'new' ),
  239. :priority => Ticket::Priority.lookup( :name => '2 normal' ),
  240. :updated_by_id => customer.id,
  241. :created_by_id => customer.id,
  242. )
  243. article_inbound = Ticket::Article.create(
  244. :ticket_id => ticket1.id,
  245. :from => 'some_sender@example.com',
  246. :to => 'some_recipient@example.com',
  247. :subject => 'some subject',
  248. :message_id => 'some@id',
  249. :body => 'some message',
  250. :internal => false,
  251. :sender => Ticket::Article::Sender.where(:name => 'Customer').first,
  252. :type => Ticket::Article::Type.where(:name => 'email').first,
  253. :updated_by_id => customer.id,
  254. :created_by_id => customer.id,
  255. )
  256. assert( ticket1, "ticket created" )
  257. # execute ticket events
  258. Observer::Ticket::Notification.transaction
  259. # update ticket attributes
  260. ticket1.title = "#{ticket1.title} - #2"
  261. ticket1.priority = Ticket::Priority.lookup( :name => '3 high' )
  262. ticket1.save
  263. list = EventBuffer.list
  264. listObjects = Observer::Ticket::Notification.get_uniq_changes(list)
  265. assert_equal( 'some notification event test 1', listObjects[ticket1.id][:changes]['title'][0] )
  266. assert_equal( 'some notification event test 1 - #2', listObjects[ticket1.id][:changes]['title'][1] )
  267. assert_not( listObjects[ticket1.id][:changes]['priority'] )
  268. assert_equal( 2, listObjects[ticket1.id][:changes]['priority_id'][0] )
  269. assert_equal( 3, listObjects[ticket1.id][:changes]['priority_id'][1] )
  270. # update ticket attributes
  271. ticket1.title = "#{ticket1.title} - #3"
  272. ticket1.priority = Ticket::Priority.lookup( :name => '1 low' )
  273. ticket1.save
  274. list = EventBuffer.list
  275. listObjects = Observer::Ticket::Notification.get_uniq_changes(list)
  276. assert_equal( 'some notification event test 1', listObjects[ticket1.id][:changes]['title'][0] )
  277. assert_equal( 'some notification event test 1 - #2 - #3', listObjects[ticket1.id][:changes]['title'][1] )
  278. assert_not( listObjects[ticket1.id][:changes]['priority'] )
  279. assert_equal( 2, listObjects[ticket1.id][:changes]['priority_id'][0] )
  280. assert_equal( 1, listObjects[ticket1.id][:changes]['priority_id'][1] )
  281. end
  282. test 'ticket notification template' do
  283. # create ticket in group
  284. ticket1 = Ticket.create(
  285. :title => 'some notification template test 1',
  286. :group => Group.lookup( :name => 'Users'),
  287. :customer => customer,
  288. :state => Ticket::State.lookup( :name => 'new' ),
  289. :priority => Ticket::Priority.lookup( :name => '2 normal' ),
  290. :updated_by_id => customer.id,
  291. :created_by_id => customer.id,
  292. )
  293. article = Ticket::Article.create(
  294. :ticket_id => ticket1.id,
  295. :from => 'some_sender@example.com',
  296. :to => 'some_recipient@example.com',
  297. :subject => 'some subject',
  298. :message_id => 'some@id',
  299. :body => 'some message',
  300. :internal => false,
  301. :sender => Ticket::Article::Sender.where(:name => 'Customer').first,
  302. :type => Ticket::Article::Type.where(:name => 'email').first,
  303. :updated_by_id => customer.id,
  304. :created_by_id => customer.id,
  305. )
  306. assert( ticket1, "ticket created - ticket notification template" )
  307. bg = Observer::Ticket::Notification::BackgroundJob.new(
  308. :ticket_id => ticket1.id,
  309. :article_id => article.id,
  310. :type => 'update',
  311. :changes => {
  312. :priority_id => [1, 2],
  313. },
  314. )
  315. # check changed attributes
  316. human_changes = bg.human_changes(agent1,ticket1)
  317. assert( human_changes['Priority'], 'Check if attributes translated based on ObjectManager::Attribute' )
  318. assert_equal( 'i18n(1 low)', human_changes['Priority'][0] )
  319. assert_equal( 'i18n(2 normal)', human_changes['Priority'][1] )
  320. assert_not( human_changes['priority_id'] )
  321. # en template
  322. template = bg.template_update(agent2, ticket1, article, human_changes)
  323. assert( template[:subject] )
  324. assert( template[:body] )
  325. assert_match( /Priority/, template[:body] )
  326. assert_match( /1 low/, template[:body] )
  327. assert_match( /2 normal/, template[:body] )
  328. # en notification
  329. body = NotificationFactory.build(
  330. :locale => agent2.preferences[:locale],
  331. :string => template[:body],
  332. :objects => {
  333. :ticket => ticket1,
  334. :article => article,
  335. :recipient => agent2,
  336. }
  337. )
  338. assert_match( /Priority/, body )
  339. assert_match( /1 low/, body )
  340. assert_match( /2 normal/, body )
  341. # de template
  342. template = bg.template_update(agent1, ticket1, article, human_changes)
  343. assert( template[:subject] )
  344. assert( template[:body] )
  345. assert_match( /Priority/, template[:body] )
  346. assert_match( /1 low/, template[:body] )
  347. assert_match( /2 normal/, template[:body] )
  348. # de notification
  349. body = NotificationFactory.build(
  350. :locale => agent1.preferences[:locale],
  351. :string => template[:body],
  352. :objects => {
  353. :ticket => ticket1,
  354. :article => article,
  355. :recipient => agent1,
  356. }
  357. )
  358. assert_match( /Priorität/, body )
  359. assert_match( /1 niedrig/, body )
  360. assert_match( /2 normal/, body )
  361. end
  362. def notification_check(ticket, recipient)
  363. result = ticket.history_get()
  364. count = 0
  365. result.each {|item|
  366. next if item['type'] != 'notification'
  367. next if item['object'] != 'Ticket'
  368. next if item['value_to'] !~ /#{recipient.email}/i
  369. count += 1
  370. }
  371. count
  372. end
  373. end