calendar_spec.rb 17 KB

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