calendar_spec.rb 17 KB

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