calendar_subscription_test.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class CalendarSubscriptionTest < ActiveSupport::TestCase
  4. test 'default test' do
  5. # create base
  6. group_default = Group.lookup(name: 'Users')
  7. group_calendar = Group.create!(
  8. name: 'CalendarSubscription',
  9. updated_by_id: 1,
  10. created_by_id: 1,
  11. )
  12. roles = Role.where(name: 'Agent')
  13. agent1 = User.create!(
  14. login: 'ticket-calendar-subscription-agent1@example.com',
  15. firstname: 'Notification',
  16. lastname: 'Agent1',
  17. email: 'ticket-calendar-subscription-agent1@example.com',
  18. password: 'agentpw',
  19. active: true,
  20. roles: roles,
  21. groups: [group_calendar],
  22. preferences: {},
  23. updated_by_id: 1,
  24. created_by_id: 1,
  25. )
  26. agent2 = User.create!(
  27. login: 'ticket-calendar-subscription-agent2@example.com',
  28. firstname: 'Notification',
  29. lastname: 'Agent2',
  30. email: 'ticket-calendar-subscription-agent2@example.com',
  31. password: 'agentpw',
  32. active: true,
  33. roles: roles,
  34. groups: [group_default],
  35. preferences: {},
  36. updated_at: '2016-02-05 16:38:00',
  37. updated_by_id: 1,
  38. created_by_id: 1,
  39. )
  40. roles = Role.where(name: 'Customer')
  41. organization1 = Organization.create_if_not_exists(
  42. name: 'Selector Org',
  43. updated_at: '2016-02-05 16:37:00',
  44. updated_by_id: 1,
  45. created_by_id: 1,
  46. )
  47. customer1 = User.create!(
  48. login: 'ticket-calendar-subscription-customer1@example.com',
  49. firstname: 'Notification',
  50. lastname: 'Customer1',
  51. email: 'ticket-calendar-subscription-customer1@example.com',
  52. password: 'customerpw',
  53. active: true,
  54. organization_id: organization1.id,
  55. roles: roles,
  56. preferences: {},
  57. updated_at: '2016-02-05 16:37:00',
  58. updated_by_id: 1,
  59. created_by_id: 1,
  60. )
  61. User.create!(
  62. login: 'ticket-calendar-subscription-customer2@example.com',
  63. firstname: 'Notification',
  64. lastname: 'Customer2',
  65. email: 'ticket-calendar-subscription-customer2@example.com',
  66. password: 'customerpw',
  67. active: true,
  68. organization_id: nil,
  69. roles: roles,
  70. preferences: {},
  71. updated_at: '2016-02-05 16:37:00',
  72. updated_by_id: 1,
  73. created_by_id: 1,
  74. )
  75. Ticket.destroy_all
  76. ticket1 = Ticket.create!(
  77. title: 'some title1 - new - group_calendar',
  78. group: group_calendar,
  79. customer_id: customer1.id,
  80. owner_id: agent1.id,
  81. state: Ticket::State.lookup(name: 'new'),
  82. priority: Ticket::Priority.lookup(name: '2 normal'),
  83. created_at: '2016-02-05 16:37:00',
  84. updated_at: '2016-02-05 16:37:00',
  85. updated_by_id: 1,
  86. created_by_id: 1,
  87. )
  88. ticket2 = Ticket.create!(
  89. title: 'some title1 - new - group_default',
  90. group: group_default,
  91. customer_id: customer1.id,
  92. owner_id: agent2.id,
  93. state: Ticket::State.lookup(name: 'new'),
  94. priority: Ticket::Priority.lookup(name: '2 normal'),
  95. created_at: '2016-02-05 16:38:00',
  96. updated_at: '2016-02-05 16:38:00',
  97. updated_by_id: 1,
  98. created_by_id: 1,
  99. )
  100. ticket3 = Ticket.create!(
  101. title: 'some title1 - pending - group_calendar',
  102. group: group_calendar,
  103. customer_id: customer1.id,
  104. owner_id: agent1.id,
  105. state: Ticket::State.lookup(name: 'pending reminder'),
  106. pending_time: '2016-02-07 16:37:00',
  107. priority: Ticket::Priority.lookup(name: '2 normal'),
  108. created_at: '2016-02-05 16:39:00',
  109. updated_at: '2016-02-05 16:39:00',
  110. updated_by_id: 1,
  111. created_by_id: 1,
  112. )
  113. ticket4 = Ticket.create!(
  114. title: 'some title1 - pending - group_default',
  115. group: group_default,
  116. customer_id: customer1.id,
  117. owner_id: agent2.id,
  118. state: Ticket::State.lookup(name: 'pending reminder'),
  119. pending_time: '2016-02-07 16:38:00',
  120. priority: Ticket::Priority.lookup(name: '2 normal'),
  121. created_at: '2016-02-05 16:40:00',
  122. updated_at: '2016-02-05 16:40:00',
  123. updated_by_id: 1,
  124. created_by_id: 1,
  125. )
  126. ticket5 = Ticket.create!(
  127. title: 'some title1 - escalation - group_calendar',
  128. group: group_calendar,
  129. customer_id: customer1.id,
  130. owner_id: agent1.id,
  131. state: Ticket::State.lookup(name: 'new'),
  132. priority: Ticket::Priority.lookup(name: '2 normal'),
  133. created_at: '2016-02-05 16:41:00',
  134. updated_at: '2016-02-05 16:41:00',
  135. updated_by_id: 1,
  136. created_by_id: 1,
  137. )
  138. ticket5.update_columns(escalation_at: '2016-02-07 17:39:00')
  139. ticket6 = Ticket.create!(
  140. title: 'some title1 - escalation - group_default',
  141. group: group_default,
  142. customer_id: customer1.id,
  143. owner_id: agent2.id,
  144. state: Ticket::State.lookup(name: 'new'),
  145. priority: Ticket::Priority.lookup(name: '2 normal'),
  146. created_at: '2016-02-05 16:42:00',
  147. updated_at: '2016-02-05 16:42:00',
  148. updated_by_id: 1,
  149. created_by_id: 1,
  150. )
  151. ticket6.update_columns(escalation_at: '2016-02-07 16:37:00')
  152. ticket7 = Ticket.create!(
  153. title: 'some title2 - new - group_calendar',
  154. group: group_calendar,
  155. customer_id: customer1.id,
  156. owner_id: 1,
  157. state: Ticket::State.lookup(name: 'new'),
  158. priority: Ticket::Priority.lookup(name: '2 normal'),
  159. created_at: '2016-02-05 17:37:00',
  160. updated_at: '2016-02-05 17:37:00',
  161. updated_by_id: 1,
  162. created_by_id: 1,
  163. )
  164. ticket8 = Ticket.create!(
  165. title: 'some title2 - new - group_default',
  166. group: group_default,
  167. customer_id: customer1.id,
  168. owner_id: 1,
  169. state: Ticket::State.lookup(name: 'new'),
  170. priority: Ticket::Priority.lookup(name: '2 normal'),
  171. created_at: '2016-02-05 17:38:00',
  172. updated_at: '2016-02-05 17:38:00',
  173. updated_by_id: 1,
  174. created_by_id: 1,
  175. )
  176. ticket9 = Ticket.create!(
  177. title: 'some title2 - pending - group_calendar',
  178. group: group_calendar,
  179. customer_id: customer1.id,
  180. owner_id: 1,
  181. state: Ticket::State.lookup(name: 'pending reminder'),
  182. pending_time: '2016-02-08 16:37:00',
  183. priority: Ticket::Priority.lookup(name: '2 normal'),
  184. created_at: '2016-02-05 17:39:00',
  185. updated_at: '2016-02-05 17:39:00',
  186. updated_by_id: 1,
  187. created_by_id: 1,
  188. )
  189. ticket10 = Ticket.create!(
  190. title: 'some title2 - pending - group_default',
  191. group: group_default,
  192. customer_id: customer1.id,
  193. owner_id: 1,
  194. state: Ticket::State.lookup(name: 'pending reminder'),
  195. pending_time: '2016-02-08 16:38:00',
  196. priority: Ticket::Priority.lookup(name: '2 normal'),
  197. created_at: '2016-02-05 17:40:00',
  198. updated_at: '2016-02-05 17:40:00',
  199. updated_by_id: 1,
  200. created_by_id: 1,
  201. )
  202. ticket11 = Ticket.create!(
  203. title: 'some title2 - escalation - group_calendar',
  204. group: group_calendar,
  205. customer_id: customer1.id,
  206. owner_id: 1,
  207. state: Ticket::State.lookup(name: 'new'),
  208. priority: Ticket::Priority.lookup(name: '2 normal'),
  209. created_at: '2016-02-05 17:41:00',
  210. updated_at: '2016-02-05 17:41:00',
  211. updated_by_id: 1,
  212. created_by_id: 1,
  213. )
  214. ticket11.update_columns(escalation_at: '2016-02-08 18:37:00')
  215. ticket12 = Ticket.create!(
  216. title: 'some title2 - escalation - group_default',
  217. group: group_default,
  218. customer_id: customer1.id,
  219. owner_id: 1,
  220. state: Ticket::State.lookup(name: 'new'),
  221. priority: Ticket::Priority.lookup(name: '2 normal'),
  222. created_at: '2016-02-05 17:42:00',
  223. updated_at: '2016-02-05 17:42:00',
  224. updated_by_id: 1,
  225. created_by_id: 1,
  226. )
  227. ticket12.update_columns(escalation_at: '2016-02-08 18:38:00')
  228. Cache.clear # set escalation_at manually, to clear cache to have correct content later
  229. # check agent 1
  230. calendar_subscriptions = CalendarSubscriptions.new(agent1)
  231. ical_file = calendar_subscriptions.all
  232. cals = Icalendar::Calendar.parse(ical_file)
  233. assert_equal(cals.count, 1)
  234. cal = cals.first
  235. assert_equal(cals.count, 1)
  236. assert_equal(cal.events.count, 4)
  237. assert_equal(cal.events[0].dtstart, Time.zone.today)
  238. assert_equal(cal.events[0].summary, 'new ticket: \'some title1 - escalation - group_calendar\'')
  239. assert_equal(cal.events[0].description, "T##{ticket5.number}")
  240. assert_equal(cal.events[0].has_alarm?, false)
  241. assert_equal(cal.events[1].dtstart, Time.zone.today)
  242. assert_equal(cal.events[1].summary, 'new ticket: \'some title1 - new - group_calendar\'')
  243. assert_equal(cal.events[1].description, "T##{ticket1.number}")
  244. assert_equal(cal.events[1].has_alarm?, false)
  245. assert_equal(cal.events[2].dtstart, Time.zone.today)
  246. assert_equal(cal.events[2].summary, 'pending reminder ticket: \'some title1 - pending - group_calendar\' customer: Notification Customer1 (Selector Org)')
  247. assert_equal(cal.events[2].description, "T##{ticket3.number}")
  248. assert_equal(cal.events[2].has_alarm?, false)
  249. assert_equal(cal.events[3].dtstart, Time.zone.today)
  250. assert_equal(cal.events[3].summary, 'ticket escalation: \'some title1 - escalation - group_calendar\' customer: Notification Customer1 (Selector Org)')
  251. assert_equal(cal.events[3].description, "T##{ticket5.number}")
  252. assert_equal(cal.events[3].has_alarm?, false)
  253. if !agent1.preferences[:calendar_subscriptions]
  254. agent1.preferences[:calendar_subscriptions] = {}
  255. end
  256. agent1.preferences[:calendar_subscriptions][:tickets] = {
  257. escalation: {
  258. own: true,
  259. not_assigned: true,
  260. },
  261. new_open: {
  262. own: true,
  263. not_assigned: true,
  264. },
  265. pending: {
  266. own: true,
  267. not_assigned: true,
  268. },
  269. alarm: true,
  270. }
  271. agent1.save!
  272. calendar_subscriptions = CalendarSubscriptions.new(agent1)
  273. ical_file = calendar_subscriptions.all
  274. cals = Icalendar::Calendar.parse(ical_file)
  275. assert_equal(cals.count, 1)
  276. cal = cals.first
  277. assert_equal(cals.count, 1)
  278. assert_equal(cal.events.count, 8)
  279. assert_equal(cal.events[0].dtstart, Time.zone.today)
  280. assert_equal(cal.events[0].summary, 'new ticket: \'some title2 - escalation - group_calendar\'')
  281. assert_equal(cal.events[0].description, "T##{ticket11.number}")
  282. assert_equal(cal.events[0].has_alarm?, false)
  283. assert_equal(cal.events[1].dtstart, Time.zone.today)
  284. assert_equal(cal.events[1].summary, 'new ticket: \'some title2 - new - group_calendar\'')
  285. assert_equal(cal.events[1].description, "T##{ticket7.number}")
  286. assert_equal(cal.events[1].has_alarm?, false)
  287. assert_equal(cal.events[2].dtstart, Time.zone.today)
  288. assert_equal(cal.events[2].summary, 'new ticket: \'some title1 - escalation - group_calendar\'')
  289. assert_equal(cal.events[2].description, "T##{ticket5.number}")
  290. assert_equal(cal.events[2].has_alarm?, false)
  291. assert_equal(cal.events[3].dtstart, Time.zone.today)
  292. assert_equal(cal.events[3].summary, 'new ticket: \'some title1 - new - group_calendar\'')
  293. assert_equal(cal.events[3].description, "T##{ticket1.number}")
  294. assert_equal(cal.events[3].has_alarm?, false)
  295. assert_equal(cal.events[4].dtstart, Time.zone.today)
  296. assert_equal(cal.events[4].summary, 'pending reminder ticket: \'some title2 - pending - group_calendar\' customer: Notification Customer1 (Selector Org)')
  297. assert_equal(cal.events[4].description, "T##{ticket9.number}")
  298. assert_equal(cal.events[4].has_alarm?, true)
  299. assert_equal(cal.events[5].dtstart, Time.zone.today)
  300. assert_equal(cal.events[5].summary, 'pending reminder ticket: \'some title1 - pending - group_calendar\' customer: Notification Customer1 (Selector Org)')
  301. assert_equal(cal.events[5].description, "T##{ticket3.number}")
  302. assert_equal(cal.events[5].has_alarm?, true)
  303. assert_equal(cal.events[6].dtstart, Time.zone.today)
  304. assert_equal(cal.events[6].summary, 'ticket escalation: \'some title2 - escalation - group_calendar\' customer: Notification Customer1 (Selector Org)')
  305. assert_equal(cal.events[6].description, "T##{ticket11.number}")
  306. assert_equal(cal.events[6].has_alarm?, true)
  307. assert_equal(cal.events[7].dtstart, Time.zone.today)
  308. assert_equal(cal.events[7].summary, 'ticket escalation: \'some title1 - escalation - group_calendar\' customer: Notification Customer1 (Selector Org)')
  309. assert_equal(cal.events[7].description, "T##{ticket5.number}")
  310. assert_equal(cal.events[7].has_alarm?, true)
  311. # check agent 2
  312. calendar_subscriptions = CalendarSubscriptions.new(agent2)
  313. ical_file = calendar_subscriptions.all
  314. cals = Icalendar::Calendar.parse(ical_file)
  315. assert_equal(cals.count, 1)
  316. cal = cals.first
  317. assert_equal(cals.count, 1)
  318. assert_equal(cal.events.count, 4)
  319. assert_equal(cal.events[0].dtstart, Time.zone.today)
  320. assert_equal(cal.events[0].summary, 'new ticket: \'some title1 - escalation - group_default\'')
  321. assert_equal(cal.events[0].description, "T##{ticket6.number}")
  322. assert_equal(cal.events[1].dtstart, Time.zone.today)
  323. assert_equal(cal.events[1].summary, 'new ticket: \'some title1 - new - group_default\'')
  324. assert_equal(cal.events[1].description, "T##{ticket2.number}")
  325. assert_equal(cal.events[2].dtstart, Time.zone.today)
  326. assert_equal(cal.events[2].summary, 'pending reminder ticket: \'some title1 - pending - group_default\' customer: Notification Customer1 (Selector Org)')
  327. assert_equal(cal.events[2].description, "T##{ticket4.number}")
  328. assert_equal(cal.events[3].dtstart, Time.zone.today)
  329. assert_equal(cal.events[3].summary, 'ticket escalation: \'some title1 - escalation - group_default\' customer: Notification Customer1 (Selector Org)')
  330. assert_equal(cal.events[3].description, "T##{ticket6.number}")
  331. if !agent2.preferences[:calendar_subscriptions]
  332. agent2.preferences[:calendar_subscriptions] = {}
  333. end
  334. agent2.preferences[:calendar_subscriptions][:tickets] = {
  335. escalation: {
  336. own: true,
  337. not_assigned: true,
  338. },
  339. new_open: {
  340. own: true,
  341. not_assigned: true,
  342. },
  343. pending: {
  344. own: true,
  345. not_assigned: true,
  346. },
  347. alarm: false,
  348. }
  349. agent2.save!
  350. calendar_subscriptions = CalendarSubscriptions.new(agent2)
  351. ical_file = calendar_subscriptions.all
  352. cals = Icalendar::Calendar.parse(ical_file)
  353. assert_equal(cals.count, 1)
  354. cal = cals.first
  355. assert_equal(cals.count, 1)
  356. assert_equal(cal.events.count, 8)
  357. assert_equal(cal.events[0].dtstart, Time.zone.today)
  358. assert_equal(cal.events[0].summary, 'new ticket: \'some title2 - escalation - group_default\'')
  359. assert_equal(cal.events[0].description, "T##{ticket12.number}")
  360. assert_equal(cal.events[0].has_alarm?, false)
  361. assert_equal(cal.events[1].dtstart, Time.zone.today)
  362. assert_equal(cal.events[1].summary, 'new ticket: \'some title2 - new - group_default\'')
  363. assert_equal(cal.events[1].description, "T##{ticket8.number}")
  364. assert_equal(cal.events[1].has_alarm?, false)
  365. assert_equal(cal.events[2].dtstart, Time.zone.today)
  366. assert_equal(cal.events[2].summary, 'new ticket: \'some title1 - escalation - group_default\'')
  367. assert_equal(cal.events[2].description, "T##{ticket6.number}")
  368. assert_equal(cal.events[2].has_alarm?, false)
  369. assert_equal(cal.events[3].dtstart, Time.zone.today)
  370. assert_equal(cal.events[3].summary, 'new ticket: \'some title1 - new - group_default\'')
  371. assert_equal(cal.events[3].description, "T##{ticket2.number}")
  372. assert_equal(cal.events[3].has_alarm?, false)
  373. assert_equal(cal.events[4].dtstart, Time.zone.today)
  374. assert_equal(cal.events[4].summary, 'pending reminder ticket: \'some title2 - pending - group_default\' customer: Notification Customer1 (Selector Org)')
  375. assert_equal(cal.events[4].description, "T##{ticket10.number}")
  376. assert_equal(cal.events[4].has_alarm?, false)
  377. assert_equal(cal.events[5].dtstart, Time.zone.today)
  378. assert_equal(cal.events[5].summary, 'pending reminder ticket: \'some title1 - pending - group_default\' customer: Notification Customer1 (Selector Org)')
  379. assert_equal(cal.events[5].description, "T##{ticket4.number}")
  380. assert_equal(cal.events[5].has_alarm?, false)
  381. assert_equal(cal.events[6].dtstart, Time.zone.today)
  382. assert_equal(cal.events[6].summary, 'ticket escalation: \'some title2 - escalation - group_default\' customer: Notification Customer1 (Selector Org)')
  383. assert_equal(cal.events[6].description, "T##{ticket12.number}")
  384. assert_equal(cal.events[6].has_alarm?, false)
  385. assert_equal(cal.events[7].dtstart, Time.zone.today)
  386. assert_equal(cal.events[7].summary, 'ticket escalation: \'some title1 - escalation - group_default\' customer: Notification Customer1 (Selector Org)')
  387. assert_equal(cal.events[7].description, "T##{ticket6.number}")
  388. assert_equal(cal.events[7].has_alarm?, false)
  389. end
  390. end