calendar_spec.rb 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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(SlaTicketRebuildEscalationJob)
  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(Exceptions::UnprocessableEntity, '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(Exceptions::UnprocessableEntity, 'No configured business hours found!')
  188. end
  189. end
  190. end
  191. end