calendar_subscription_test.rb 17 KB

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