calendar_subscription_test.rb 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. # encoding: utf-8
  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_or_update(
  8. name: 'CalendarSubscription',
  9. updated_by_id: 1,
  10. created_by_id: 1,
  11. )
  12. roles = Role.where(name: 'Agent')
  13. agent1 = User.create_or_update(
  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_or_update(
  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_or_update(
  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. customer2 = User.create_or_update(
  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_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_by_id: 1,
  96. created_by_id: 1,
  97. )
  98. ticket3 = Ticket.create!(
  99. title: 'some title1 - pending - group_calendar',
  100. group: group_calendar,
  101. customer_id: customer1.id,
  102. owner_id: agent1.id,
  103. state: Ticket::State.lookup(name: 'pending reminder'),
  104. pending_time: '2016-02-07 16:37:00',
  105. priority: Ticket::Priority.lookup(name: '2 normal'),
  106. created_at: '2016-02-05 16:39:00',
  107. updated_by_id: 1,
  108. created_by_id: 1,
  109. )
  110. ticket4 = Ticket.create!(
  111. title: 'some title1 - pending - group_default',
  112. group: group_default,
  113. customer_id: customer1.id,
  114. owner_id: agent2.id,
  115. state: Ticket::State.lookup(name: 'pending reminder'),
  116. pending_time: '2016-02-07 16:38:00',
  117. priority: Ticket::Priority.lookup(name: '2 normal'),
  118. created_at: '2016-02-05 16:40:00',
  119. updated_by_id: 1,
  120. created_by_id: 1,
  121. )
  122. ticket5 = Ticket.create!(
  123. title: 'some title1 - escalation - group_calendar',
  124. group: group_calendar,
  125. customer_id: customer1.id,
  126. owner_id: agent1.id,
  127. state: Ticket::State.lookup(name: 'new'),
  128. priority: Ticket::Priority.lookup(name: '2 normal'),
  129. created_at: '2016-02-05 16:41:00',
  130. updated_by_id: 1,
  131. created_by_id: 1,
  132. )
  133. ticket5.update_columns(escalation_at: '2016-02-07 17:39:00')
  134. ticket6 = Ticket.create!(
  135. title: 'some title1 - escalation - group_default',
  136. group: group_default,
  137. customer_id: customer1.id,
  138. owner_id: agent2.id,
  139. state: Ticket::State.lookup(name: 'new'),
  140. priority: Ticket::Priority.lookup(name: '2 normal'),
  141. created_at: '2016-02-05 16:42:00',
  142. updated_by_id: 1,
  143. created_by_id: 1,
  144. )
  145. ticket6.update_columns(escalation_at: '2016-02-07 16:37:00')
  146. ticket7 = Ticket.create!(
  147. title: 'some title2 - new - group_calendar',
  148. group: group_calendar,
  149. customer_id: customer1.id,
  150. owner_id: 1,
  151. state: Ticket::State.lookup(name: 'new'),
  152. priority: Ticket::Priority.lookup(name: '2 normal'),
  153. created_at: '2016-02-05 17:37:00',
  154. updated_by_id: 1,
  155. created_by_id: 1,
  156. )
  157. ticket8 = Ticket.create!(
  158. title: 'some title2 - new - group_default',
  159. group: group_default,
  160. customer_id: customer1.id,
  161. owner_id: 1,
  162. state: Ticket::State.lookup(name: 'new'),
  163. priority: Ticket::Priority.lookup(name: '2 normal'),
  164. created_at: '2016-02-05 17:38:00',
  165. updated_by_id: 1,
  166. created_by_id: 1,
  167. )
  168. ticket9 = Ticket.create!(
  169. title: 'some title2 - pending - group_calendar',
  170. group: group_calendar,
  171. customer_id: customer1.id,
  172. owner_id: 1,
  173. state: Ticket::State.lookup(name: 'pending reminder'),
  174. pending_time: '2016-02-08 16:37:00',
  175. priority: Ticket::Priority.lookup(name: '2 normal'),
  176. created_at: '2016-02-05 17:39:00',
  177. updated_by_id: 1,
  178. created_by_id: 1,
  179. )
  180. ticket10 = Ticket.create!(
  181. title: 'some title2 - pending - group_default',
  182. group: group_default,
  183. customer_id: customer1.id,
  184. owner_id: 1,
  185. state: Ticket::State.lookup(name: 'pending reminder'),
  186. pending_time: '2016-02-08 16:38:00',
  187. priority: Ticket::Priority.lookup(name: '2 normal'),
  188. created_at: '2016-02-05 17:40:00',
  189. updated_by_id: 1,
  190. created_by_id: 1,
  191. )
  192. ticket11 = Ticket.create!(
  193. title: 'some title2 - escalation - group_calendar',
  194. group: group_calendar,
  195. customer_id: customer1.id,
  196. owner_id: 1,
  197. state: Ticket::State.lookup(name: 'new'),
  198. priority: Ticket::Priority.lookup(name: '2 normal'),
  199. created_at: '2016-02-05 17:41:00',
  200. updated_by_id: 1,
  201. created_by_id: 1,
  202. )
  203. ticket11.update_columns(escalation_at: '2016-02-08 18:37:00')
  204. ticket12 = Ticket.create!(
  205. title: 'some title2 - escalation - group_default',
  206. group: group_default,
  207. customer_id: customer1.id,
  208. owner_id: 1,
  209. state: Ticket::State.lookup(name: 'new'),
  210. priority: Ticket::Priority.lookup(name: '2 normal'),
  211. created_at: '2016-02-05 17:42:00',
  212. updated_by_id: 1,
  213. created_by_id: 1,
  214. )
  215. ticket12.update_columns(escalation_at: '2016-02-08 18:38:00')
  216. Cache.clear # set escalation_at manually, to clear cache to have correct content later
  217. # check agent 1
  218. calendar_subscriptions = CalendarSubscriptions.new(agent1)
  219. ical_file = calendar_subscriptions.all
  220. cals = Icalendar::Calendar.parse(ical_file)
  221. assert_equal(cals.count, 1)
  222. cal = cals.first
  223. assert_equal(cals.count, 1)
  224. assert_equal(cal.events.count, 4)
  225. assert_equal(cal.events[0].dtstart, Time.zone.today)
  226. assert_equal(cal.events[0].summary, 'new ticket: \'some title1 - escalation - group_calendar\'')
  227. assert_equal(cal.events[0].description, "T##{ticket5.number}")
  228. assert_equal(cal.events[0].has_alarm?, false)
  229. assert_equal(cal.events[1].dtstart, Time.zone.today)
  230. assert_equal(cal.events[1].summary, 'new ticket: \'some title1 - new - group_calendar\'')
  231. assert_equal(cal.events[1].description, "T##{ticket1.number}")
  232. assert_equal(cal.events[1].has_alarm?, false)
  233. assert_equal(cal.events[2].dtstart, Time.zone.today)
  234. assert_equal(cal.events[2].summary, 'pending reminder ticket: \'some title1 - pending - group_calendar\' customer: Notification Customer1 (Selector Org)')
  235. assert_equal(cal.events[2].description, "T##{ticket3.number}")
  236. assert_equal(cal.events[2].has_alarm?, false)
  237. assert_equal(cal.events[3].dtstart, Time.zone.today)
  238. assert_equal(cal.events[3].summary, 'ticket escalation: \'some title1 - escalation - group_calendar\' customer: Notification Customer1 (Selector Org)')
  239. assert_equal(cal.events[3].description, "T##{ticket5.number}")
  240. assert_equal(cal.events[3].has_alarm?, false)
  241. if !agent1.preferences[:calendar_subscriptions]
  242. agent1.preferences[:calendar_subscriptions] = {}
  243. end
  244. agent1.preferences[:calendar_subscriptions][:tickets] = {
  245. escalation: {
  246. own: true,
  247. not_assigned: true,
  248. },
  249. new_open: {
  250. own: true,
  251. not_assigned: true,
  252. },
  253. pending: {
  254. own: true,
  255. not_assigned: true,
  256. },
  257. alarm: true,
  258. }
  259. agent1.save!
  260. calendar_subscriptions = CalendarSubscriptions.new(agent1)
  261. ical_file = calendar_subscriptions.all
  262. cals = Icalendar::Calendar.parse(ical_file)
  263. assert_equal(cals.count, 1)
  264. cal = cals.first
  265. assert_equal(cals.count, 1)
  266. assert_equal(cal.events.count, 8)
  267. assert_equal(cal.events[0].dtstart, Time.zone.today)
  268. assert_equal(cal.events[0].summary, 'new ticket: \'some title2 - escalation - group_calendar\'')
  269. assert_equal(cal.events[0].description, "T##{ticket11.number}")
  270. assert_equal(cal.events[0].has_alarm?, false)
  271. assert_equal(cal.events[1].dtstart, Time.zone.today)
  272. assert_equal(cal.events[1].summary, 'new ticket: \'some title2 - new - group_calendar\'')
  273. assert_equal(cal.events[1].description, "T##{ticket7.number}")
  274. assert_equal(cal.events[1].has_alarm?, false)
  275. assert_equal(cal.events[2].dtstart, Time.zone.today)
  276. assert_equal(cal.events[2].summary, 'new ticket: \'some title1 - escalation - group_calendar\'')
  277. assert_equal(cal.events[2].description, "T##{ticket5.number}")
  278. assert_equal(cal.events[2].has_alarm?, false)
  279. assert_equal(cal.events[3].dtstart, Time.zone.today)
  280. assert_equal(cal.events[3].summary, 'new ticket: \'some title1 - new - group_calendar\'')
  281. assert_equal(cal.events[3].description, "T##{ticket1.number}")
  282. assert_equal(cal.events[3].has_alarm?, false)
  283. assert_equal(cal.events[4].dtstart, Time.zone.today)
  284. assert_equal(cal.events[4].summary, 'pending reminder ticket: \'some title2 - pending - group_calendar\' customer: Notification Customer1 (Selector Org)')
  285. assert_equal(cal.events[4].description, "T##{ticket9.number}")
  286. assert_equal(cal.events[4].has_alarm?, true)
  287. assert_equal(cal.events[5].dtstart, Time.zone.today)
  288. assert_equal(cal.events[5].summary, 'pending reminder ticket: \'some title1 - pending - group_calendar\' customer: Notification Customer1 (Selector Org)')
  289. assert_equal(cal.events[5].description, "T##{ticket3.number}")
  290. assert_equal(cal.events[5].has_alarm?, true)
  291. assert_equal(cal.events[6].dtstart, Time.zone.today)
  292. assert_equal(cal.events[6].summary, 'ticket escalation: \'some title2 - escalation - group_calendar\' customer: Notification Customer1 (Selector Org)')
  293. assert_equal(cal.events[6].description, "T##{ticket11.number}")
  294. assert_equal(cal.events[6].has_alarm?, true)
  295. assert_equal(cal.events[7].dtstart, Time.zone.today)
  296. assert_equal(cal.events[7].summary, 'ticket escalation: \'some title1 - escalation - group_calendar\' customer: Notification Customer1 (Selector Org)')
  297. assert_equal(cal.events[7].description, "T##{ticket5.number}")
  298. assert_equal(cal.events[7].has_alarm?, true)
  299. # check agent 2
  300. calendar_subscriptions = CalendarSubscriptions.new(agent2)
  301. ical_file = calendar_subscriptions.all
  302. cals = Icalendar::Calendar.parse(ical_file)
  303. assert_equal(cals.count, 1)
  304. cal = cals.first
  305. assert_equal(cals.count, 1)
  306. assert_equal(cal.events.count, 4)
  307. assert_equal(cal.events[0].dtstart, Time.zone.today)
  308. assert_equal(cal.events[0].summary, 'new ticket: \'some title1 - escalation - group_default\'')
  309. assert_equal(cal.events[0].description, "T##{ticket6.number}")
  310. assert_equal(cal.events[1].dtstart, Time.zone.today)
  311. assert_equal(cal.events[1].summary, 'new ticket: \'some title1 - new - group_default\'')
  312. assert_equal(cal.events[1].description, "T##{ticket2.number}")
  313. assert_equal(cal.events[2].dtstart, Time.zone.today)
  314. assert_equal(cal.events[2].summary, 'pending reminder ticket: \'some title1 - pending - group_default\' customer: Notification Customer1 (Selector Org)')
  315. assert_equal(cal.events[2].description, "T##{ticket4.number}")
  316. assert_equal(cal.events[3].dtstart, Time.zone.today)
  317. assert_equal(cal.events[3].summary, 'ticket escalation: \'some title1 - escalation - group_default\' customer: Notification Customer1 (Selector Org)')
  318. assert_equal(cal.events[3].description, "T##{ticket6.number}")
  319. if !agent2.preferences[:calendar_subscriptions]
  320. agent2.preferences[:calendar_subscriptions] = {}
  321. end
  322. agent2.preferences[:calendar_subscriptions][:tickets] = {
  323. escalation: {
  324. own: true,
  325. not_assigned: true,
  326. },
  327. new_open: {
  328. own: true,
  329. not_assigned: true,
  330. },
  331. pending: {
  332. own: true,
  333. not_assigned: true,
  334. },
  335. alarm: false,
  336. }
  337. agent2.save!
  338. calendar_subscriptions = CalendarSubscriptions.new(agent2)
  339. ical_file = calendar_subscriptions.all
  340. cals = Icalendar::Calendar.parse(ical_file)
  341. assert_equal(cals.count, 1)
  342. cal = cals.first
  343. assert_equal(cals.count, 1)
  344. assert_equal(cal.events.count, 8)
  345. assert_equal(cal.events[0].dtstart, Time.zone.today)
  346. assert_equal(cal.events[0].summary, 'new ticket: \'some title2 - escalation - group_default\'')
  347. assert_equal(cal.events[0].description, "T##{ticket12.number}")
  348. assert_equal(cal.events[0].has_alarm?, false)
  349. assert_equal(cal.events[1].dtstart, Time.zone.today)
  350. assert_equal(cal.events[1].summary, 'new ticket: \'some title2 - new - group_default\'')
  351. assert_equal(cal.events[1].description, "T##{ticket8.number}")
  352. assert_equal(cal.events[1].has_alarm?, false)
  353. assert_equal(cal.events[2].dtstart, Time.zone.today)
  354. assert_equal(cal.events[2].summary, 'new ticket: \'some title1 - escalation - group_default\'')
  355. assert_equal(cal.events[2].description, "T##{ticket6.number}")
  356. assert_equal(cal.events[2].has_alarm?, false)
  357. assert_equal(cal.events[3].dtstart, Time.zone.today)
  358. assert_equal(cal.events[3].summary, 'new ticket: \'some title1 - new - group_default\'')
  359. assert_equal(cal.events[3].description, "T##{ticket2.number}")
  360. assert_equal(cal.events[3].has_alarm?, false)
  361. assert_equal(cal.events[4].dtstart, Time.zone.today)
  362. assert_equal(cal.events[4].summary, 'pending reminder ticket: \'some title2 - pending - group_default\' customer: Notification Customer1 (Selector Org)')
  363. assert_equal(cal.events[4].description, "T##{ticket10.number}")
  364. assert_equal(cal.events[4].has_alarm?, false)
  365. assert_equal(cal.events[5].dtstart, Time.zone.today)
  366. assert_equal(cal.events[5].summary, 'pending reminder ticket: \'some title1 - pending - group_default\' customer: Notification Customer1 (Selector Org)')
  367. assert_equal(cal.events[5].description, "T##{ticket4.number}")
  368. assert_equal(cal.events[5].has_alarm?, false)
  369. assert_equal(cal.events[6].dtstart, Time.zone.today)
  370. assert_equal(cal.events[6].summary, 'ticket escalation: \'some title2 - escalation - group_default\' customer: Notification Customer1 (Selector Org)')
  371. assert_equal(cal.events[6].description, "T##{ticket12.number}")
  372. assert_equal(cal.events[6].has_alarm?, false)
  373. assert_equal(cal.events[7].dtstart, Time.zone.today)
  374. assert_equal(cal.events[7].summary, 'ticket escalation: \'some title1 - escalation - group_default\' customer: Notification Customer1 (Selector Org)')
  375. assert_equal(cal.events[7].description, "T##{ticket6.number}")
  376. assert_equal(cal.events[7].has_alarm?, false)
  377. end
  378. end