online_notifiaction_test.rb 25 KB

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