calendar_spec.rb 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Calendar, type: :model do
  4. subject(:calendar) { create(:calendar) }
  5. describe 'attributes' do
  6. describe '#default' do
  7. before { expect(described_class.pluck(:default)).to eq([true]) }
  8. context 'when set to true on creation' do
  9. subject(:calendar) { build(:calendar, default: true) }
  10. it 'stays true and sets all other calendars to default: false' do
  11. expect { calendar.tap(&:save).reload }.not_to change(calendar, :default)
  12. expect(described_class.where(default: true) - [calendar]).to be_empty
  13. end
  14. end
  15. context 'when set to true on update' do
  16. subject(:calendar) { create(:calendar, default: false) }
  17. before { calendar.default = true }
  18. it 'stays true and sets all other calendars to default: false' do
  19. expect { calendar.tap(&:save).reload }.not_to change(calendar, :default)
  20. expect(described_class.where(default: true) - [calendar]).to be_empty
  21. end
  22. end
  23. context 'when set to false on update' do
  24. it 'sets default: true on earliest-created calendar' do
  25. expect { described_class.first.update(default: false) }
  26. .not_to change { described_class.first.default }
  27. end
  28. end
  29. context 'when default calendar is destroyed' do
  30. subject!(:calendar) { create(:calendar, default: false) }
  31. it 'sets default: true on earliest-created remaining calendar' do
  32. expect { described_class.first.destroy }
  33. .to change { calendar.reload.default }.to(true)
  34. end
  35. context 'when sla has destroyed calendar set' do
  36. let(:sla) { create(:sla, calendar: described_class.first) }
  37. before do
  38. sla
  39. end
  40. it 'sets the new default calendar to the sla' do
  41. expect { described_class.first.destroy }
  42. .to change { sla.reload.calendar }.to(calendar)
  43. end
  44. end
  45. end
  46. end
  47. describe '#public_holidays' do
  48. subject(:calendar) do
  49. create(:calendar, ical_url: Rails.root.join('test/data/calendar/calendar1.ics'))
  50. end
  51. before { travel_to Time.zone.parse('2017-08-24T01:04:44Z0') }
  52. context 'on creation' do
  53. it 'is computed from iCal event data (implicitly via #sync), from one year before to three years after' do
  54. expect(calendar.public_holidays).to eq(
  55. '2016-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  56. '2017-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  57. '2018-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  58. '2019-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  59. )
  60. end
  61. context 'with one-time and n-time (recurring) events' do
  62. subject(:calendar) do
  63. create(:calendar, ical_url: Rails.root.join('test/data/calendar/calendar3.ics'))
  64. end
  65. it 'accurately computes/imports events' do
  66. expect(calendar.public_holidays).to eq(
  67. '2016-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  68. '2016-12-26' => { 'active' => true, 'summary' => 'day3', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  69. '2016-12-28' => { 'active' => true, 'summary' => 'day5', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  70. '2017-01-26' => { 'active' => true, 'summary' => 'day3', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  71. '2017-02-26' => { 'active' => true, 'summary' => 'day3', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  72. '2017-03-26' => { 'active' => true, 'summary' => 'day3', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  73. '2017-04-26' => { 'active' => true, 'summary' => 'day3', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  74. '2017-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  75. '2018-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  76. '2019-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  77. )
  78. end
  79. end
  80. end
  81. end
  82. end
  83. describe '#sync' do
  84. subject(:calendar) do
  85. create(:calendar, ical_url: Rails.root.join('test/data/calendar/calendar1.ics'), default: false)
  86. end
  87. before { travel_to Time.zone.parse('2017-08-24T01:04:44Z0') }
  88. context 'when called explicitly after creation' do
  89. it 'writes #public_holidays to the cache (valid for 1 day)' do
  90. expect(Rails.cache.read("CalendarIcal::#{calendar.id}")).to be_nil
  91. expect { calendar.sync }
  92. .to change { Rails.cache.read("CalendarIcal::#{calendar.id}") }
  93. .to(calendar.attributes.slice('public_holidays', 'ical_url').symbolize_keys)
  94. end
  95. context 'and neither current date nor iCal URL have changed' do
  96. it 'is idempotent' do
  97. expect { calendar.sync }
  98. .not_to change(calendar, :public_holidays)
  99. end
  100. it 'does not create a background job for escalation rebuild' do
  101. calendar # create and sync (1 inital background job is created)
  102. expect { calendar.sync } # a second sync right after calendar create
  103. .to not_change { Delayed::Job.count }
  104. end
  105. end
  106. context 'and current date has changed but neither public_holidays nor iCal URL have changed (past cache expiry)' do
  107. before do
  108. calendar # create and sync
  109. travel 2.days
  110. end
  111. it 'is idempotent' do
  112. expect { calendar.sync }
  113. .not_to change(calendar, :public_holidays)
  114. end
  115. it 'does not create a background job for escalation rebuild' do
  116. expect { calendar.sync }
  117. .not_to change(Delayed::Job, :count)
  118. end
  119. end
  120. context 'and current date has changed (past cache expiry)', performs_jobs: true do
  121. before do
  122. calendar # create and sync
  123. clear_jobs # clear (speak: process) created jobs
  124. travel 1.year
  125. end
  126. it 'appends newly computed event data to #public_holidays' do
  127. expect { calendar.sync }.to change(calendar, :public_holidays).to(
  128. '2016-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  129. '2017-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  130. '2018-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  131. '2019-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  132. '2020-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  133. )
  134. end
  135. it 'does create a background job for escalation rebuild' do
  136. expect { calendar.sync }.to have_enqueued_job(TicketEscalationRebuildJob)
  137. end
  138. end
  139. context 'and iCal URL has changed' do
  140. before { calendar.assign_attributes(ical_url: Rails.root.join('test/data/calendar/calendar2.ics')) }
  141. it 'replaces #public_holidays with event data computed from new iCal URL' do
  142. expect { calendar.save }
  143. .to change(calendar, :public_holidays).to(
  144. '2016-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  145. '2016-12-25' => { 'active' => true, 'summary' => 'Christmas2', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  146. '2017-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  147. '2017-12-25' => { 'active' => true, 'summary' => 'Christmas2', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  148. '2018-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  149. '2018-12-25' => { 'active' => true, 'summary' => 'Christmas2', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  150. '2019-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  151. '2019-12-25' => { 'active' => true, 'summary' => 'Christmas2', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  152. )
  153. end
  154. end
  155. end
  156. end
  157. describe '#validate_hours' do
  158. context 'when business_hours are invalid' do
  159. it 'fails for hours ending at 00:00' do
  160. expect do
  161. create(:calendar,
  162. business_hours: {
  163. mon: {
  164. active: true,
  165. timeframes: [['09:00', '00:00']]
  166. },
  167. tue: {
  168. active: true,
  169. timeframes: [['09:00', '00:00']]
  170. },
  171. wed: {
  172. active: true,
  173. timeframes: [['09:00', '00:00']]
  174. },
  175. thu: {
  176. active: true,
  177. timeframes: [['09:00', '00:00']]
  178. },
  179. fri: {
  180. active: true,
  181. timeframes: [['09:00', '00:00']]
  182. },
  183. sat: {
  184. active: false,
  185. timeframes: [['09:00', '00:00']]
  186. },
  187. sun: {
  188. active: false,
  189. timeframes: [['09:00', '00:00']]
  190. }
  191. })
  192. end.to raise_error(ActiveRecord::RecordInvalid, %r{nonsensical hours provided})
  193. end
  194. it 'fails for blank structure' do
  195. expect do
  196. create(:calendar,
  197. business_hours: {})
  198. end.to raise_error(ActiveRecord::RecordInvalid, %r{There are no business hours configured.})
  199. end
  200. end
  201. end
  202. describe '#biz' do
  203. it 'overnight minutes are counted correctly' do
  204. travel_to Time.current.noon
  205. calendar = create(:calendar, '23:59/7')
  206. biz = calendar.biz
  207. expect(biz.time(24, :hours).after(Time.current)).to eq 1.day.from_now
  208. end
  209. end
  210. describe '#business_hours_to_hash' do
  211. it 'returns a hash with all weekdays' do
  212. calendar = create(:calendar, '23:59/7')
  213. hash = calendar.business_hours_to_hash
  214. expect(hash.keys).to eq %i[mon tue wed thu fri sat sun]
  215. end
  216. context 'with mocked hours' do
  217. let(:calendar) { create(:calendar, '23:59/7') }
  218. let(:result) { calendar.business_hours_to_hash }
  219. before do
  220. calendar.business_hours = {
  221. day_1: { active: true, timeframes: [['09:00', '17:00']] },
  222. day_2: { active: true, timeframes: [['00:01', '02:00'], ['09:00', '17:00']] },
  223. day_3: { active: false, timeframes: [['09:00', '17:00']] }
  224. }
  225. end
  226. it { expect(result.keys).to eq %i[day_1 day_2] }
  227. it { expect(result[:day_1]).to eq({ '09:00' => '17:00' }) }
  228. it { expect(result[:day_2]).to eq({ '09:00' => '17:00', '00:01' => '02:00' }) }
  229. end
  230. end
  231. context 'when updated Calendar no longer matches Ticket', :performs_jobs do
  232. subject(:ticket) { create(:ticket, created_at: '2016-11-01 13:56:21 UTC', updated_at: '2016-11-01 13:56:21 UTC') }
  233. let(:calendar) do
  234. create(:calendar,
  235. business_hours: {
  236. mon: {
  237. active: true,
  238. timeframes: [ ['08:00', '20:00'] ]
  239. },
  240. tue: {
  241. active: true,
  242. timeframes: [ ['08:00', '20:00'] ]
  243. },
  244. wed: {
  245. active: true,
  246. timeframes: [ ['08:00', '20:00'] ]
  247. },
  248. thu: {
  249. active: true,
  250. timeframes: [ ['08:00', '20:00'] ]
  251. },
  252. fri: {
  253. active: true,
  254. timeframes: [ ['08:00', '20:00'] ]
  255. },
  256. sat: {
  257. active: false,
  258. timeframes: [ ['08:00', '17:00'] ]
  259. },
  260. sun: {
  261. active: false,
  262. timeframes: [ ['08:00', '17:00'] ]
  263. },
  264. },
  265. public_holidays: {
  266. '2016-11-01' => {
  267. 'active' => true,
  268. 'summary' => 'test 1',
  269. },
  270. })
  271. end
  272. let(:sla) { create(:sla, condition: {}, calendar: calendar, first_response_time: 60, response_time: 120, solution_time: nil) }
  273. before do
  274. queue_adapter.perform_enqueued_jobs = true
  275. queue_adapter.perform_enqueued_at_jobs = true
  276. sla
  277. ticket
  278. create(:'ticket/article', :inbound_web, ticket: ticket, created_at: '2016-11-01 13:56:21 UTC', updated_at: '2016-11-01 13:56:21 UTC')
  279. ticket.reload
  280. create(:'ticket/article', :outbound_email, ticket: ticket, created_at: '2016-11-07 13:26:36 UTC', updated_at: '2016-11-07 13:26:36 UTC')
  281. ticket.reload
  282. end
  283. it 'calculates escalation_at attributes' do
  284. expect(ticket.escalation_at).to be_nil
  285. expect(ticket.first_response_escalation_at).to be_nil
  286. expect(ticket.update_escalation_at).to be_nil
  287. expect(ticket.close_escalation_at).to be_nil
  288. # set sla's for timezone "Europe/Berlin" wintertime (+1), so UTC times are 3:00-18:00
  289. calendar.update!(
  290. business_hours: {
  291. mon: {
  292. active: true,
  293. timeframes: [ ['04:00', '20:00'] ]
  294. },
  295. tue: {
  296. active: true,
  297. timeframes: [ ['04:00', '20:00'] ]
  298. },
  299. wed: {
  300. active: true,
  301. timeframes: [ ['04:00', '20:00'] ]
  302. },
  303. thu: {
  304. active: true,
  305. timeframes: [ ['04:00', '20:00'] ]
  306. },
  307. fri: {
  308. active: true,
  309. timeframes: [ ['04:00', '20:00'] ]
  310. },
  311. sat: {
  312. active: false,
  313. timeframes: [ ['04:00', '13:00'] ] # this changed from '17:00' => '13:00'
  314. },
  315. sun: {
  316. active: false,
  317. timeframes: [ ['04:00', '17:00'] ]
  318. },
  319. },
  320. public_holidays: {
  321. '2016-11-01' => {
  322. 'active' => true,
  323. 'summary' => 'test 1',
  324. },
  325. },
  326. )
  327. ticket.reload
  328. expect(ticket.escalation_at).to be_nil
  329. expect(ticket.first_response_escalation_at).to be_nil
  330. expect(ticket.update_escalation_at).to be_nil
  331. expect(ticket.close_escalation_at).to be_nil
  332. end
  333. end
  334. context 'when SLA relevant timezone holidays are configured' do
  335. let(:calendar) do
  336. create(:calendar,
  337. public_holidays: {
  338. '2015-09-22' => {
  339. 'active' => true,
  340. 'summary' => 'test 1',
  341. },
  342. '2015-09-23' => {
  343. 'active' => false,
  344. 'summary' => 'test 2',
  345. },
  346. '2015-09-24' => {
  347. 'removed' => false,
  348. 'summary' => 'test 3',
  349. },
  350. })
  351. end
  352. let(:sla) do
  353. create(:sla,
  354. calendar: calendar,
  355. condition: {},
  356. first_response_time: 120,
  357. response_time: 180,
  358. solution_time: 240)
  359. end
  360. before do
  361. sla
  362. ticket.reload
  363. end
  364. context 'when a Ticket is created in working hours but not affected by the configured holidays' do
  365. subject(:ticket) { create(:ticket, created_at: '2013-10-21 09:30:00 UTC', updated_at: '2013-10-21 09:30:00 UTC') }
  366. it 'calculates escalation_at attributes' do
  367. expect(ticket.escalation_at.gmtime.to_s).to eq('2013-10-21 11:30:00 UTC')
  368. expect(ticket.first_response_escalation_at.gmtime.to_s).to eq('2013-10-21 11:30:00 UTC')
  369. expect(ticket.update_escalation_at).to be_nil
  370. expect(ticket.close_escalation_at.gmtime.to_s).to eq('2013-10-21 13:30:00 UTC')
  371. end
  372. end
  373. context 'when a Ticket is created before the working hours but not affected by the configured holidays' do
  374. subject(:ticket) { create(:ticket, created_at: '2013-10-21 05:30:00 UTC', updated_at: '2013-10-21 05:30:00 UTC') }
  375. it 'calculates escalation_at attributes' do
  376. expect(ticket.escalation_at.gmtime.to_s).to eq('2013-10-21 09:00:00 UTC')
  377. expect(ticket.first_response_escalation_at.gmtime.to_s).to eq('2013-10-21 09:00:00 UTC')
  378. expect(ticket.update_escalation_at).to be_nil
  379. expect(ticket.close_escalation_at.gmtime.to_s).to eq('2013-10-21 11:00:00 UTC')
  380. end
  381. end
  382. context 'when a Ticket is created before the holidays but escalation should take place while holidays are' do
  383. subject(:ticket) { create(:ticket, created_at: '2015-09-21 14:30:00 UTC', updated_at: '2015-09-21 14:30:00 UTC') }
  384. it 'calculates escalation_at attributes' do
  385. expect(ticket.escalation_at.gmtime.to_s).to eq('2015-09-23 08:30:00 UTC')
  386. expect(ticket.first_response_escalation_at.gmtime.to_s).to eq('2015-09-23 08:30:00 UTC')
  387. expect(ticket.update_escalation_at).to be_nil
  388. expect(ticket.close_escalation_at.gmtime.to_s).to eq('2015-09-23 10:30:00 UTC')
  389. end
  390. end
  391. end
  392. end