ticket_notification_test.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 => agent1.id,
  110. :created_by_id => agent1.id,
  111. )
  112. # execute ticket events
  113. Observer::Ticket::Notification.transaction
  114. #puts Delayed::Job.all.inspect
  115. Delayed::Worker.new.work_off
  116. # verify notifications to not to agent1 but to agent2
  117. assert_equal( 2, notification_check(ticket1, agent1), ticket1.id )
  118. assert_equal( 3, notification_check(ticket1, agent2), ticket1.id )
  119. # update ticket by user
  120. ticket1.owner_id = agent1.id
  121. ticket1.updated_by_id = agent1.id
  122. ticket1.save
  123. article_note = Ticket::Article.create(
  124. :ticket_id => ticket1.id,
  125. :from => 'some person',
  126. :subject => 'some note',
  127. :body => 'some message',
  128. :internal => true,
  129. :sender => Ticket::Article::Sender.where(:name => 'Agent').first,
  130. :type => Ticket::Article::Type.where(:name => 'note').first,
  131. :updated_by_id => agent1.id,
  132. :created_by_id => agent1.id,
  133. )
  134. # execute ticket events
  135. Observer::Ticket::Notification.transaction
  136. #puts Delayed::Job.all.inspect
  137. Delayed::Worker.new.work_off
  138. # verify notifications to not to agent1 but to agent2
  139. assert_equal( 2, notification_check(ticket1, agent1), ticket1.id )
  140. assert_equal( 3, notification_check(ticket1, agent2), ticket1.id )
  141. # create ticket with agent1 as owner
  142. ticket2 = Ticket.create(
  143. :title => 'some notification test 2',
  144. :group => Group.lookup( :name => 'Users'),
  145. :customer_id => 2,
  146. :owner_id => agent1.id,
  147. :state => Ticket::State.lookup( :name => 'new' ),
  148. :priority => Ticket::Priority.lookup( :name => '2 normal' ),
  149. :updated_by_id => agent1.id,
  150. :created_by_id => agent1.id,
  151. )
  152. article_inbound = Ticket::Article.create(
  153. :ticket_id => ticket2.id,
  154. :from => 'some_sender@example.com',
  155. :to => 'some_recipient@example.com',
  156. :subject => 'some subject',
  157. :message_id => 'some@id',
  158. :body => 'some message',
  159. :internal => false,
  160. :sender => Ticket::Article::Sender.where(:name => 'Agent').first,
  161. :type => Ticket::Article::Type.where(:name => 'phone').first,
  162. :updated_by_id => agent1.id,
  163. :created_by_id => agent1.id,
  164. )
  165. # execute ticket events
  166. Observer::Ticket::Notification.transaction
  167. #puts Delayed::Job.all.inspect
  168. Delayed::Worker.new.work_off
  169. assert( ticket2, "ticket created" )
  170. # verify notifications to no one
  171. assert_equal( 0, notification_check(ticket2, agent1), ticket2.id )
  172. assert_equal( 0, notification_check(ticket2, agent2), ticket2.id )
  173. # update ticket
  174. ticket2.title = "#{ticket2.title} - #2"
  175. ticket2.updated_by_id = agent1.id
  176. ticket2.priority = Ticket::Priority.lookup( :name => '3 high' )
  177. ticket2.save
  178. # execute ticket events
  179. Observer::Ticket::Notification.transaction
  180. #puts Delayed::Job.all.inspect
  181. Delayed::Worker.new.work_off
  182. # verify notifications to no one
  183. assert_equal( 0, notification_check(ticket2, agent1), ticket2.id )
  184. assert_equal( 0, notification_check(ticket2, agent2), ticket2.id )
  185. # update ticket
  186. ticket2.title = "#{ticket2.title} - #3"
  187. ticket2.updated_by_id = agent2.id
  188. ticket2.priority = Ticket::Priority.lookup( :name => '2 normal' )
  189. ticket2.save
  190. # execute ticket events
  191. Observer::Ticket::Notification.transaction
  192. #puts Delayed::Job.all.inspect
  193. Delayed::Worker.new.work_off
  194. # verify notifications to agent1 and not to agent2
  195. assert_equal( 1, notification_check(ticket2, agent1), ticket2.id )
  196. assert_equal( 0, notification_check(ticket2, agent2), ticket2.id )
  197. # create ticket with agent2 and agent1 as owner
  198. ticket3 = Ticket.create(
  199. :title => 'some notification test 3',
  200. :group => Group.lookup( :name => 'Users'),
  201. :customer_id => 2,
  202. :owner_id => agent1.id,
  203. :state => Ticket::State.lookup( :name => 'new' ),
  204. :priority => Ticket::Priority.lookup( :name => '2 normal' ),
  205. :updated_by_id => agent2.id,
  206. :created_by_id => agent2.id,
  207. )
  208. article_inbound = Ticket::Article.create(
  209. :ticket_id => ticket3.id,
  210. :from => 'some_sender@example.com',
  211. :to => 'some_recipient@example.com',
  212. :subject => 'some subject',
  213. :message_id => 'some@id',
  214. :body => 'some message',
  215. :internal => false,
  216. :sender => Ticket::Article::Sender.where(:name => 'Agent').first,
  217. :type => Ticket::Article::Type.where(:name => 'phone').first,
  218. :updated_by_id => agent2.id,
  219. :created_by_id => agent2.id,
  220. )
  221. # execute ticket events
  222. Observer::Ticket::Notification.transaction
  223. #puts Delayed::Job.all.inspect
  224. Delayed::Worker.new.work_off
  225. assert( ticket3, "ticket created" )
  226. # verify notifications to agent1 and not to agent2
  227. assert_equal( 1, notification_check(ticket3, agent1), ticket3.id )
  228. assert_equal( 0, notification_check(ticket3, agent2), ticket3.id )
  229. # update ticket
  230. ticket3.title = "#{ticket3.title} - #2"
  231. ticket3.updated_by_id = agent1.id
  232. ticket3.priority = Ticket::Priority.lookup( :name => '3 high' )
  233. ticket3.save
  234. # execute ticket events
  235. Observer::Ticket::Notification.transaction
  236. #puts Delayed::Job.all.inspect
  237. Delayed::Worker.new.work_off
  238. # verify notifications to no one
  239. assert_equal( 1, notification_check(ticket3, agent1), ticket3.id )
  240. assert_equal( 0, notification_check(ticket3, agent2), ticket3.id )
  241. # update ticket
  242. ticket3.title = "#{ticket3.title} - #3"
  243. ticket3.updated_by_id = agent2.id
  244. ticket3.priority = Ticket::Priority.lookup( :name => '2 normal' )
  245. ticket3.save
  246. # execute ticket events
  247. Observer::Ticket::Notification.transaction
  248. #puts Delayed::Job.all.inspect
  249. Delayed::Worker.new.work_off
  250. # verify notifications to agent1 and not to agent2
  251. assert_equal( 2, notification_check(ticket3, agent1), ticket3.id )
  252. assert_equal( 0, notification_check(ticket3, agent2), ticket3.id )
  253. # update article / not notification should be sent
  254. article_inbound.internal = true
  255. article_inbound.save
  256. # execute ticket events
  257. Observer::Ticket::Notification.transaction
  258. #puts Delayed::Job.all.inspect
  259. Delayed::Worker.new.work_off
  260. # verify notifications not to agent1 and not to agent2
  261. assert_equal( 2, notification_check(ticket3, agent1), ticket3.id )
  262. assert_equal( 0, notification_check(ticket3, agent2), ticket3.id )
  263. delete = ticket1.destroy
  264. assert( delete, "ticket1 destroy" )
  265. delete = ticket2.destroy
  266. assert( delete, "ticket2 destroy" )
  267. delete = ticket3.destroy
  268. assert( delete, "ticket3 destroy" )
  269. end
  270. test 'ticket notification events' do
  271. # create ticket in group
  272. ticket1 = Ticket.create(
  273. :title => 'some notification event test 1',
  274. :group => Group.lookup( :name => 'Users'),
  275. :customer => customer,
  276. :state => Ticket::State.lookup( :name => 'new' ),
  277. :priority => Ticket::Priority.lookup( :name => '2 normal' ),
  278. :updated_by_id => customer.id,
  279. :created_by_id => customer.id,
  280. )
  281. article_inbound = Ticket::Article.create(
  282. :ticket_id => ticket1.id,
  283. :from => 'some_sender@example.com',
  284. :to => 'some_recipient@example.com',
  285. :subject => 'some subject',
  286. :message_id => 'some@id',
  287. :body => 'some message',
  288. :internal => false,
  289. :sender => Ticket::Article::Sender.where(:name => 'Customer').first,
  290. :type => Ticket::Article::Type.where(:name => 'email').first,
  291. :updated_by_id => customer.id,
  292. :created_by_id => customer.id,
  293. )
  294. assert( ticket1, "ticket created" )
  295. # execute ticket events
  296. Observer::Ticket::Notification.transaction
  297. # update ticket attributes
  298. ticket1.title = "#{ticket1.title} - #2"
  299. ticket1.priority = Ticket::Priority.lookup( :name => '3 high' )
  300. ticket1.save
  301. list = EventBuffer.list
  302. listObjects = Observer::Ticket::Notification.get_uniq_changes(list)
  303. assert_equal( 'some notification event test 1', listObjects[ticket1.id][:changes]['title'][0] )
  304. assert_equal( 'some notification event test 1 - #2', listObjects[ticket1.id][:changes]['title'][1] )
  305. assert_not( listObjects[ticket1.id][:changes]['priority'] )
  306. assert_equal( 2, listObjects[ticket1.id][:changes]['priority_id'][0] )
  307. assert_equal( 3, listObjects[ticket1.id][:changes]['priority_id'][1] )
  308. # update ticket attributes
  309. ticket1.title = "#{ticket1.title} - #3"
  310. ticket1.priority = Ticket::Priority.lookup( :name => '1 low' )
  311. ticket1.save
  312. list = EventBuffer.list
  313. listObjects = Observer::Ticket::Notification.get_uniq_changes(list)
  314. assert_equal( 'some notification event test 1', listObjects[ticket1.id][:changes]['title'][0] )
  315. assert_equal( 'some notification event test 1 - #2 - #3', listObjects[ticket1.id][:changes]['title'][1] )
  316. assert_not( listObjects[ticket1.id][:changes]['priority'] )
  317. assert_equal( 2, listObjects[ticket1.id][:changes]['priority_id'][0] )
  318. assert_equal( 1, listObjects[ticket1.id][:changes]['priority_id'][1] )
  319. end
  320. test 'ticket notification template' do
  321. # create ticket in group
  322. ticket1 = Ticket.create(
  323. :title => 'some notification template test 1 Bobs\'s resumé',
  324. :group => Group.lookup( :name => 'Users'),
  325. :customer => customer,
  326. :state => Ticket::State.lookup( :name => 'new' ),
  327. :priority => Ticket::Priority.lookup( :name => '2 normal' ),
  328. :updated_by_id => customer.id,
  329. :created_by_id => customer.id,
  330. )
  331. article = Ticket::Article.create(
  332. :ticket_id => ticket1.id,
  333. :from => 'some_sender@example.com',
  334. :to => 'some_recipient@example.com',
  335. :subject => 'some subject',
  336. :message_id => 'some@id',
  337. :body => "some message\nnewline1 abc\nnewline2",
  338. :internal => false,
  339. :sender => Ticket::Article::Sender.where(:name => 'Customer').first,
  340. :type => Ticket::Article::Type.where(:name => 'email').first,
  341. :updated_by_id => customer.id,
  342. :created_by_id => customer.id,
  343. )
  344. assert( ticket1, "ticket created - ticket notification template" )
  345. bg = Observer::Ticket::Notification::BackgroundJob.new(
  346. :ticket_id => ticket1.id,
  347. :article_id => article.id,
  348. :type => 'update',
  349. :changes => {
  350. :priority_id => [1, 2],
  351. :pending_time => [nil, Time.parse("2015-01-11 23:33:47 UTC")],
  352. },
  353. )
  354. # check changed attributes
  355. human_changes = bg.human_changes(agent1,ticket1)
  356. assert( human_changes['Priority'], 'Check if attributes translated based on ObjectManager::Attribute' )
  357. assert( human_changes['Pending till'], 'Check if attributes translated based on ObjectManager::Attribute' )
  358. assert_equal( 'i18n(1 low)', human_changes['Priority'][0] )
  359. assert_equal( 'i18n(2 normal)', human_changes['Priority'][1] )
  360. assert_equal( 'i18n()', human_changes['Pending till'][0] )
  361. assert_equal( 'i18n(2015-01-11 23:33:47 UTC)', human_changes['Pending till'][1] )
  362. assert_not( human_changes['priority_id'] )
  363. assert_not( human_changes['pending_till'] )
  364. # en template
  365. template = bg.template_update(agent2, ticket1, article, human_changes)
  366. assert( template[:subject] )
  367. assert( template[:body] )
  368. assert_match( /Priority/, template[:body] )
  369. assert_match( /1 low/, template[:body] )
  370. assert_match( /2 normal/, template[:body] )
  371. assert_match( /Pending till/, template[:body] )
  372. assert_match( /2015-01-11 23:33:47 UTC/, template[:body] )
  373. assert_match( /updated/i, template[:subject] )
  374. # en notification
  375. subject = NotificationFactory.build(
  376. :locale => agent2.preferences[:locale],
  377. :string => template[:subject],
  378. :objects => {
  379. :ticket => ticket1,
  380. :article => article,
  381. :recipient => agent2,
  382. }
  383. )
  384. assert_match( /Bobs's resumé/, subject )
  385. body = NotificationFactory.build(
  386. :locale => agent2.preferences[:locale],
  387. :string => template[:body],
  388. :objects => {
  389. :ticket => ticket1,
  390. :article => article,
  391. :recipient => agent2,
  392. }
  393. )
  394. assert_match( /Priority/, body )
  395. assert_match( /1 low/, body )
  396. assert_match( /2 normal/, body )
  397. assert_match( /Pending till/, body )
  398. assert_match( /2015-01-11 23:33:47 UTC/, body )
  399. assert_match( /update/, body )
  400. assert_no_match( /i18n/, body )
  401. # de template
  402. template = bg.template_update(agent1, ticket1, article, human_changes)
  403. assert( template[:subject] )
  404. assert( template[:body] )
  405. assert_match( /Priority/, template[:body] )
  406. assert_match( /1 low/, template[:body] )
  407. assert_match( /2 normal/, template[:body] )
  408. assert_match( /Pending till/, template[:body] )
  409. assert_match( /2015-01-11 23:33:47 UTC/, template[:body] )
  410. assert_match( /aktualis/, template[:subject] )
  411. # de notification
  412. subject = NotificationFactory.build(
  413. :locale => agent1.preferences[:locale],
  414. :string => template[:subject],
  415. :objects => {
  416. :ticket => ticket1,
  417. :article => article,
  418. :recipient => agent2,
  419. }
  420. )
  421. assert_match( /Bobs's resumé/, subject )
  422. body = NotificationFactory.build(
  423. :locale => agent1.preferences[:locale],
  424. :string => template[:body],
  425. :objects => {
  426. :ticket => ticket1,
  427. :article => article,
  428. :recipient => agent1,
  429. }
  430. )
  431. assert_match( /Priorität/, body )
  432. assert_match( /1 niedrig/, body )
  433. assert_match( /2 normal/, body )
  434. assert_match( /Warten/, body )
  435. assert_match( /2015-01-11 23:33:47 UTC/, body )
  436. assert_match( /aktualis/, body )
  437. assert_no_match( /i18n/, body )
  438. bg = Observer::Ticket::Notification::BackgroundJob.new(
  439. :ticket_id => ticket1.id,
  440. :article_id => article.id,
  441. :type => 'update',
  442. :changes => {
  443. :title => ['some notification template test 1', 'some notification template test 1 #2'],
  444. :priority_id => [2, 3],
  445. },
  446. )
  447. human_changes = bg.human_changes(agent1,ticket1)
  448. puts "hc #{human_changes.inspect}"
  449. human_changes = bg.human_changes(agent2,ticket1)
  450. puts "hc2 #{human_changes.inspect}"
  451. template = bg.template_update(agent1, ticket1, article, human_changes)
  452. puts "t1 #{template.inspect}"
  453. template = bg.template_update(agent2, ticket1, article, human_changes)
  454. puts "t2 #{template.inspect}"
  455. end
  456. def notification_check(ticket, recipient)
  457. result = ticket.history_get()
  458. count = 0
  459. result.each {|item|
  460. next if item['type'] != 'notification'
  461. next if item['object'] != 'Ticket'
  462. next if item['value_to'] !~ /#{recipient.email}/i
  463. count += 1
  464. }
  465. count
  466. end
  467. end