online_notifiaction_test.rb 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class OnlineNotificationTest < ActiveSupport::TestCase
  4. include BackgroundJobsHelper
  5. setup do
  6. role = Role.lookup(name: 'Agent')
  7. @group = Group.create_or_update(
  8. name: 'OnlineNotificationTest',
  9. updated_by_id: 1,
  10. created_by_id: 1
  11. )
  12. @agent_user1 = User.create_or_update(
  13. login: 'agent_online_notify1',
  14. firstname: 'Bob',
  15. lastname: 'Smith',
  16. email: 'agent_online_notify1@example.com',
  17. password: 'some_pass',
  18. active: true,
  19. role_ids: [role.id],
  20. group_ids: [@group.id],
  21. updated_by_id: 1,
  22. created_by_id: 1
  23. )
  24. @agent_user2 = User.create_or_update(
  25. login: 'agent_online_notify2',
  26. firstname: 'Bob',
  27. lastname: 'Smith',
  28. email: 'agent_online_notify2@example.com',
  29. password: 'some_pass',
  30. active: true,
  31. role_ids: [role.id],
  32. group_ids: [@group.id],
  33. updated_by_id: 1,
  34. created_by_id: 1
  35. )
  36. @customer_user = User.lookup(email: 'nicole.braun@zammad.org')
  37. calendar1 = Calendar.create_or_update(
  38. name: 'EU 1 - test',
  39. timezone: 'Europe/Berlin',
  40. business_hours: {
  41. mon: {
  42. active: true,
  43. timeframes: [ ['00:00', '23:59'] ]
  44. },
  45. tue: {
  46. active: true,
  47. timeframes: [ ['00:00', '23:59'] ]
  48. },
  49. wed: {
  50. active: true,
  51. timeframes: [ ['00:00', '23:59'] ]
  52. },
  53. thu: {
  54. active: true,
  55. timeframes: [ ['00:00', '23:59'] ]
  56. },
  57. fri: {
  58. active: true,
  59. timeframes: [ ['00:00', '23:59'] ]
  60. },
  61. sat: {
  62. active: true,
  63. timeframes: [ ['00:00', '23:59'] ]
  64. },
  65. sun: {
  66. active: true,
  67. timeframes: [ ['00:00', '23:59'] ]
  68. },
  69. },
  70. default: true,
  71. ical_url: nil,
  72. updated_by_id: 1,
  73. created_by_id: 1,
  74. )
  75. Sla.create_or_update(
  76. name: 'test sla 1',
  77. condition: {},
  78. first_response_time: 20,
  79. update_time: 60,
  80. solution_time: 120,
  81. calendar_id: calendar1.id,
  82. updated_by_id: 1,
  83. created_by_id: 1,
  84. )
  85. end
  86. test 'ticket notification' do
  87. ApplicationHandleInfo.current = 'application_server'
  88. # case #1
  89. ticket1 = Ticket.create(
  90. group: @group,
  91. customer_id: @customer_user.id,
  92. owner_id: User.lookup(login: '-').id,
  93. title: 'Unit Test 1 (äöüß)!',
  94. state_id: Ticket::State.lookup(name: 'closed').id,
  95. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  96. updated_by_id: @agent_user1.id,
  97. created_by_id: @agent_user1.id,
  98. )
  99. Ticket::Article.create(
  100. ticket_id: ticket1.id,
  101. updated_by_id: @agent_user1.id,
  102. created_by_id: @agent_user1.id,
  103. type_id: Ticket::Article::Type.lookup(name: 'phone').id,
  104. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  105. from: 'Unit Test <unittest@example.com>',
  106. body: 'Unit Test 123',
  107. internal: false
  108. )
  109. # remember ticket
  110. tickets = []
  111. tickets.push ticket1
  112. perform_enqueued_jobs commit_transaction: true
  113. # because it's already closed
  114. assert(OnlineNotification.all_seen?('Ticket', ticket1.id))
  115. assert_not(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket1.id, 'create', @agent_user1, false))
  116. assert_not(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket1.id, 'create', @agent_user1, true))
  117. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket1.id, 'create', @agent_user1, false))
  118. assert(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket1.id, 'create', @agent_user1, true))
  119. ticket1.update!(
  120. title: 'Unit Test 1 (äöüß) - update!',
  121. state_id: Ticket::State.lookup(name: 'open').id,
  122. priority_id: Ticket::Priority.lookup(name: '1 low').id,
  123. updated_by_id: @customer_user.id,
  124. )
  125. perform_enqueued_jobs commit_transaction: true
  126. # because it's already open
  127. assert_not(OnlineNotification.all_seen?('Ticket', ticket1.id))
  128. assert_not(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket1.id, 'update', @customer_user, true))
  129. assert(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket1.id, 'update', @customer_user, false))
  130. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket1.id, 'update', @customer_user, true))
  131. assert(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket1.id, 'update', @customer_user, false))
  132. # case #2
  133. ticket2 = Ticket.create(
  134. group: @group,
  135. customer_id: @customer_user.id,
  136. owner_id: @agent_user1.id,
  137. title: 'Unit Test 1 (äöüß)!',
  138. state_id: Ticket::State.lookup(name: 'closed').id,
  139. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  140. updated_by_id: @customer_user.id,
  141. created_by_id: @customer_user.id,
  142. )
  143. Ticket::Article.create(
  144. ticket_id: ticket2.id,
  145. updated_by_id: @customer_user.id,
  146. created_by_id: @customer_user.id,
  147. type_id: Ticket::Article::Type.lookup(name: 'phone').id,
  148. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  149. from: 'Unit Test <unittest@example.com>',
  150. body: 'Unit Test 123',
  151. internal: false
  152. )
  153. # remember ticket
  154. tickets = []
  155. tickets.push ticket2
  156. perform_enqueued_jobs commit_transaction: true
  157. # because it's already closed
  158. assert_not(OnlineNotification.all_seen?('Ticket', ticket2.id))
  159. assert(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket2.id, 'create', @customer_user, false))
  160. assert_not(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket2.id, 'create', @customer_user, true))
  161. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket2.id, 'create', @customer_user, false))
  162. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket2.id, 'create', @customer_user, true))
  163. ticket2.update!(
  164. title: 'Unit Test 1 (äöüß) - update!',
  165. state_id: Ticket::State.lookup(name: 'open').id,
  166. priority_id: Ticket::Priority.lookup(name: '1 low').id,
  167. updated_by_id: @customer_user.id,
  168. )
  169. perform_enqueued_jobs commit_transaction: true
  170. # because it's already open
  171. assert_not(OnlineNotification.all_seen?('Ticket', ticket2.id))
  172. assert(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket2.id, 'update', @customer_user, false))
  173. assert_not(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket2.id, 'update', @customer_user, true))
  174. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket2.id, 'update', @customer_user, true))
  175. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket2.id, 'update', @customer_user, false))
  176. # case #3
  177. ticket3 = Ticket.create(
  178. group: @group,
  179. customer_id: @customer_user.id,
  180. owner_id: User.lookup(login: '-').id,
  181. title: 'Unit Test 2 (äöüß)!',
  182. state_id: Ticket::State.lookup(name: 'new').id,
  183. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  184. updated_by_id: @agent_user1.id,
  185. created_by_id: @agent_user1.id,
  186. )
  187. Ticket::Article.create(
  188. ticket_id: ticket3.id,
  189. updated_by_id: @agent_user1.id,
  190. created_by_id: @agent_user1.id,
  191. type_id: Ticket::Article::Type.lookup(name: 'phone').id,
  192. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  193. from: 'Unit Test <unittest@example.com>',
  194. body: 'Unit Test 123',
  195. internal: false,
  196. )
  197. # remember ticket
  198. tickets.push ticket3
  199. perform_enqueued_jobs commit_transaction: true
  200. # because it's already new
  201. assert_not(OnlineNotification.all_seen?('Ticket', ticket3.id))
  202. assert_not(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket3.id, 'create', @agent_user1, false))
  203. assert_not(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket3.id, 'create', @agent_user1, true))
  204. assert(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket3.id, 'create', @agent_user1, false))
  205. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket3.id, 'create', @agent_user1, true))
  206. ticket3.update!(
  207. title: 'Unit Test 2 (äöüß) - update!',
  208. state_id: Ticket::State.lookup(name: 'closed').id,
  209. priority_id: Ticket::Priority.lookup(name: '1 low').id,
  210. updated_by_id: @customer_user.id,
  211. )
  212. perform_enqueued_jobs commit_transaction: true
  213. # because it's already closed
  214. assert(OnlineNotification.all_seen?('Ticket', ticket3.id))
  215. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket3, @agent_user1, 'update'))
  216. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket3, @agent_user2, 'update'))
  217. assert_not(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket3.id, 'update', @customer_user, false))
  218. assert(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket3.id, 'update', @customer_user, true))
  219. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket3.id, 'update', @customer_user, false))
  220. assert(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket3.id, 'update', @customer_user, true))
  221. Ticket::Article.create(
  222. ticket_id: ticket3.id,
  223. updated_by_id: @customer_user.id,
  224. created_by_id: @customer_user.id,
  225. type_id: Ticket::Article::Type.lookup(name: 'phone').id,
  226. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  227. from: 'Unit Test <unittest@example.com>',
  228. body: 'Unit Test 123 # 2',
  229. internal: false
  230. )
  231. perform_enqueued_jobs commit_transaction: true
  232. # because it's already closed but an follow-up arrived later
  233. assert_not(OnlineNotification.all_seen?('Ticket', ticket3.id))
  234. assert(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket3.id, 'update', @customer_user, false))
  235. assert(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket3.id, 'update', @customer_user, true))
  236. assert(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket3.id, 'update', @customer_user, false))
  237. assert(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket3.id, 'update', @customer_user, true))
  238. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket3, @agent_user1, 'update'))
  239. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket3, @agent_user2, 'update'))
  240. # case #4
  241. ticket4 = Ticket.create(
  242. group: @group,
  243. customer_id: @customer_user.id,
  244. owner_id: @agent_user1.id,
  245. title: 'Unit Test 3 (äöüß)!',
  246. state_id: Ticket::State.lookup(name: 'new').id,
  247. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  248. updated_by_id: @customer_user.id,
  249. created_by_id: @customer_user.id,
  250. )
  251. Ticket::Article.create(
  252. ticket_id: ticket4.id,
  253. updated_by_id: @customer_user.id,
  254. created_by_id: @customer_user.id,
  255. type_id: Ticket::Article::Type.lookup(name: 'phone').id,
  256. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  257. from: 'Unit Test <unittest@example.com>',
  258. body: 'Unit Test 123',
  259. internal: false,
  260. )
  261. # remember ticket
  262. tickets.push ticket4
  263. perform_enqueued_jobs commit_transaction: true
  264. # because it's already new
  265. assert_not(OnlineNotification.all_seen?('Ticket', ticket4.id))
  266. assert(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket4.id, 'create', @customer_user, false))
  267. assert_not(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket4.id, 'create', @customer_user, true))
  268. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket4.id, 'create', @customer_user, false))
  269. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket4.id, 'create', @customer_user, true))
  270. ticket4.update!(
  271. title: 'Unit Test 3 (äöüß) - update!',
  272. state_id: Ticket::State.lookup(name: 'open').id,
  273. priority_id: Ticket::Priority.lookup(name: '1 low').id,
  274. updated_by_id: @customer_user.id,
  275. )
  276. perform_enqueued_jobs commit_transaction: true
  277. # because it's already open
  278. assert_not(OnlineNotification.all_seen?('Ticket', ticket4.id))
  279. assert(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket4.id, 'update', @customer_user, false))
  280. assert_not(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket4.id, 'update', @customer_user, true))
  281. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket4.id, 'update', @customer_user, false))
  282. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket4.id, 'update', @customer_user, true))
  283. # case #5
  284. ticket5 = Ticket.create(
  285. group: @group,
  286. customer_id: @customer_user.id,
  287. owner_id: User.lookup(login: '-').id,
  288. title: 'Unit Test 4 (äöüß)!',
  289. state_id: Ticket::State.lookup(name: 'new').id,
  290. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  291. updated_by_id: @agent_user1.id,
  292. created_by_id: @agent_user1.id,
  293. )
  294. Ticket::Article.create(
  295. ticket_id: ticket5.id,
  296. updated_by_id: @agent_user1.id,
  297. created_by_id: @agent_user1.id,
  298. type_id: Ticket::Article::Type.lookup(name: 'phone').id,
  299. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  300. from: 'Unit Test <unittest@example.com>',
  301. body: 'Unit Test 123',
  302. internal: false,
  303. )
  304. # remember ticket
  305. tickets.push ticket5
  306. perform_enqueued_jobs commit_transaction: true
  307. # because it's already new
  308. assert_not(OnlineNotification.all_seen?('Ticket', ticket5.id))
  309. assert_not(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket5.id, 'create', @agent_user1, true))
  310. assert_not(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket5.id, 'create', @agent_user1, false))
  311. assert(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket5.id, 'create', @agent_user1, false))
  312. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket5.id, 'create', @agent_user1, true))
  313. ticket5.update!(
  314. title: 'Unit Test 4 (äöüß) - update!',
  315. state_id: Ticket::State.lookup(name: 'open').id,
  316. priority_id: Ticket::Priority.lookup(name: '1 low').id,
  317. updated_by_id: @customer_user.id,
  318. )
  319. perform_enqueued_jobs commit_transaction: true
  320. # because it's already open
  321. assert_not(OnlineNotification.all_seen?('Ticket', ticket5.id))
  322. assert(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket5.id, 'update', @customer_user, false))
  323. assert_not(OnlineNotification.exists?(@agent_user1, 'Ticket', ticket5.id, 'update', @customer_user, true))
  324. assert(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket5.id, 'update', @customer_user, false))
  325. assert_not(OnlineNotification.exists?(@agent_user2, 'Ticket', ticket5.id, 'update', @customer_user, true))
  326. # merge tickets - also remove notifications of merged tickets
  327. tickets[0].merge_to(
  328. ticket_id: tickets[1].id,
  329. user_id: 1,
  330. )
  331. perform_enqueued_jobs
  332. notifications = OnlineNotification.list_by_object('Ticket', tickets[0].id)
  333. assert(notifications.present?, 'should have notifications')
  334. assert(OnlineNotification.all_seen?('Ticket', tickets[0].id), 'still not seen notifications for merged ticket available')
  335. notifications = OnlineNotification.list_by_object('Ticket', tickets[1].id)
  336. assert(notifications.present?, 'should have notifications')
  337. assert_not(OnlineNotification.all_seen?('Ticket', tickets[1].id), 'no notifications for master ticket available')
  338. # delete tickets
  339. tickets.each do |ticket|
  340. ticket_id = ticket.id
  341. ticket.destroy
  342. found = Ticket.find_by(id: ticket_id)
  343. assert_not(found, 'Ticket destroyed')
  344. # check if notifications for ticket still exist
  345. perform_enqueued_jobs
  346. notifications = OnlineNotification.list_by_object('Ticket', ticket_id)
  347. assert(notifications.blank?, 'still notifications for destroyed ticket available')
  348. end
  349. end
  350. test 'ticket notification item check' do
  351. ticket1 = Ticket.create(
  352. title: 'some title',
  353. group: @group,
  354. customer_id: @customer_user.id,
  355. state: Ticket::State.lookup(name: 'new'),
  356. priority: Ticket::Priority.lookup(name: '2 normal'),
  357. updated_by_id: 1,
  358. created_by_id: 1,
  359. )
  360. assert(ticket1, 'ticket created')
  361. Ticket::Article.create(
  362. ticket_id: ticket1.id,
  363. from: 'some_sender@example.com',
  364. to: 'some_recipient@example.com',
  365. subject: 'some subject',
  366. message_id: 'some@id',
  367. body: 'some message article_inbound',
  368. internal: false,
  369. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  370. type: Ticket::Article::Type.lookup(name: 'email'),
  371. updated_by_id: 1,
  372. created_by_id: 1,
  373. )
  374. assert_equal(ticket1.online_notification_seen_state, false)
  375. assert_equal(ticket1.online_notification_seen_state(@agent_user1), false)
  376. assert_equal(ticket1.online_notification_seen_state(@agent_user2), false)
  377. # pending reminder, just let new owner to unseed
  378. ticket1.update!(
  379. owner_id: @agent_user1.id,
  380. state: Ticket::State.lookup(name: 'pending reminder'),
  381. updated_by_id: @agent_user2.id,
  382. )
  383. assert_equal(ticket1.online_notification_seen_state, true)
  384. assert_equal(ticket1.online_notification_seen_state(@agent_user1.id), false)
  385. assert_equal(ticket1.online_notification_seen_state(@agent_user2.id), true)
  386. # pending reminder, just let new owner to unseed
  387. ticket1.update!(
  388. owner_id: 1,
  389. state: Ticket::State.lookup(name: 'pending reminder'),
  390. updated_by_id: @agent_user2.id,
  391. )
  392. assert_equal(ticket1.online_notification_seen_state, true)
  393. assert_equal(ticket1.online_notification_seen_state(@agent_user1.id), false)
  394. assert_equal(ticket1.online_notification_seen_state(@agent_user2.id), false)
  395. # pending reminder, self done, all to unseed
  396. ticket1.update!(
  397. owner_id: @agent_user1.id,
  398. state: Ticket::State.lookup(name: 'pending reminder'),
  399. updated_by_id: @agent_user1.id,
  400. )
  401. assert_equal(ticket1.online_notification_seen_state, true)
  402. assert_equal(ticket1.online_notification_seen_state(@agent_user1.id), true)
  403. assert_equal(ticket1.online_notification_seen_state(@agent_user2.id), true)
  404. # pending close, all to unseen
  405. ticket1.update!(
  406. owner_id: @agent_user1.id,
  407. state: Ticket::State.lookup(name: 'pending close'),
  408. updated_by_id: @agent_user2.id,
  409. )
  410. assert_equal(ticket1.online_notification_seen_state, true)
  411. assert_equal(ticket1.online_notification_seen_state(@agent_user1.id), false)
  412. assert_equal(ticket1.online_notification_seen_state(@agent_user2.id), true)
  413. # to open, all to seen
  414. ticket1.update!(
  415. owner_id: @agent_user1.id,
  416. state: Ticket::State.lookup(name: 'open'),
  417. updated_by_id: @agent_user2.id,
  418. )
  419. assert_equal(ticket1.online_notification_seen_state, false)
  420. assert_equal(ticket1.online_notification_seen_state(@agent_user1.id), false)
  421. assert_equal(ticket1.online_notification_seen_state(@agent_user2.id), false)
  422. # to closed, all only others to seen
  423. ticket1.update!(
  424. owner_id: @agent_user1.id,
  425. state: Ticket::State.lookup(name: 'closed'),
  426. updated_by_id: @agent_user2.id,
  427. )
  428. assert_equal(ticket1.online_notification_seen_state, true)
  429. assert_equal(ticket1.online_notification_seen_state(@agent_user1.id), false)
  430. assert_equal(ticket1.online_notification_seen_state(@agent_user2.id), true)
  431. # to closed by owner self, all to seen
  432. ticket1.update!(
  433. owner_id: @agent_user1.id,
  434. state: Ticket::State.lookup(name: 'closed'),
  435. updated_by_id: @agent_user1.id,
  436. )
  437. assert_equal(ticket1.online_notification_seen_state, true)
  438. assert_equal(ticket1.online_notification_seen_state(@agent_user1.id), true)
  439. assert_equal(ticket1.online_notification_seen_state(@agent_user2.id), true)
  440. # to closed by owner self, all to seen
  441. ticket1.update!(
  442. owner_id: @agent_user1.id,
  443. state: Ticket::State.lookup(name: 'merged'),
  444. updated_by_id: @agent_user2.id,
  445. )
  446. assert_equal(ticket1.online_notification_seen_state, true)
  447. assert_equal(ticket1.online_notification_seen_state(@agent_user1.id), true)
  448. assert_equal(ticket1.online_notification_seen_state(@agent_user2.id), true)
  449. end
  450. test 'cleanup check' do
  451. ticket1 = Ticket.create(
  452. group: @group,
  453. customer_id: @customer_user.id,
  454. owner_id: User.lookup(login: '-').id,
  455. title: 'Unit Test 1 (äöüß)!',
  456. state_id: Ticket::State.lookup(name: 'closed').id,
  457. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  458. updated_by_id: @agent_user1.id,
  459. created_by_id: @agent_user1.id,
  460. )
  461. online_notification1 = OnlineNotification.add(
  462. type: 'create',
  463. object: 'Ticket',
  464. o_id: ticket1.id,
  465. seen: false,
  466. user_id: @agent_user1.id,
  467. created_by_id: 1,
  468. updated_by_id: 1,
  469. created_at: 10.months.ago,
  470. updated_at: 10.months.ago,
  471. )
  472. online_notification2 = OnlineNotification.add(
  473. type: 'create',
  474. object: 'Ticket',
  475. o_id: ticket1.id,
  476. seen: true,
  477. user_id: @agent_user1.id,
  478. created_by_id: 1,
  479. updated_by_id: 1,
  480. created_at: 10.months.ago,
  481. updated_at: 10.months.ago,
  482. )
  483. online_notification3 = OnlineNotification.add(
  484. type: 'create',
  485. object: 'Ticket',
  486. o_id: ticket1.id,
  487. seen: false,
  488. user_id: @agent_user1.id,
  489. created_by_id: 1,
  490. updated_by_id: 1,
  491. created_at: 2.days.ago,
  492. updated_at: 2.days.ago,
  493. )
  494. online_notification4 = OnlineNotification.add(
  495. type: 'create',
  496. object: 'Ticket',
  497. o_id: ticket1.id,
  498. seen: true,
  499. user_id: @agent_user1.id,
  500. created_by_id: @agent_user1.id,
  501. updated_by_id: @agent_user1.id,
  502. created_at: 2.days.ago,
  503. updated_at: 2.days.ago,
  504. )
  505. online_notification5 = OnlineNotification.add(
  506. type: 'create',
  507. object: 'Ticket',
  508. o_id: ticket1.id,
  509. seen: true,
  510. user_id: @agent_user1.id,
  511. created_by_id: @agent_user2.id,
  512. updated_by_id: @agent_user2.id,
  513. created_at: 2.days.ago,
  514. updated_at: 2.days.ago,
  515. )
  516. online_notification6 = OnlineNotification.add(
  517. type: 'create',
  518. object: 'Ticket',
  519. o_id: ticket1.id,
  520. seen: true,
  521. user_id: @agent_user1.id,
  522. created_by_id: @agent_user1.id,
  523. updated_by_id: @agent_user1.id,
  524. created_at: 5.minutes.ago,
  525. updated_at: 5.minutes.ago,
  526. )
  527. online_notification7 = OnlineNotification.add(
  528. type: 'create',
  529. object: 'Ticket',
  530. o_id: ticket1.id,
  531. seen: true,
  532. user_id: @agent_user1.id,
  533. created_by_id: @agent_user2.id,
  534. updated_by_id: @agent_user2.id,
  535. created_at: 5.minutes.ago,
  536. updated_at: 5.minutes.ago,
  537. )
  538. OnlineNotification.cleanup
  539. assert_not(OnlineNotification.find_by(id: online_notification1.id))
  540. assert_not(OnlineNotification.find_by(id: online_notification2.id))
  541. assert(OnlineNotification.find_by(id: online_notification3.id))
  542. assert_not(OnlineNotification.find_by(id: online_notification4.id))
  543. assert_not(OnlineNotification.find_by(id: online_notification5.id))
  544. assert(OnlineNotification.find_by(id: online_notification6.id))
  545. assert(OnlineNotification.find_by(id: online_notification7.id))
  546. OnlineNotification.destroy_all
  547. end
  548. test 'not existing object ref' do
  549. assert_raises(RuntimeError) do
  550. OnlineNotification.add(
  551. type: 'create',
  552. object: 'TicketNotExisting',
  553. o_id: 123,
  554. seen: false,
  555. user_id: @agent_user1.id,
  556. created_by_id: 1,
  557. updated_by_id: 1,
  558. created_at: 10.months.ago,
  559. updated_at: 10.months.ago,
  560. )
  561. end
  562. assert_raises(ActiveRecord::RecordNotFound) do
  563. OnlineNotification.add(
  564. type: 'create',
  565. object: 'Ticket',
  566. o_id: 123,
  567. seen: false,
  568. user_id: @agent_user1.id,
  569. created_by_id: 1,
  570. updated_by_id: 1,
  571. created_at: 10.months.ago,
  572. updated_at: 10.months.ago,
  573. )
  574. end
  575. end
  576. end