online_notifiaction_test.rb 26 KB

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