calendar_spec.rb 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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(Calendar.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(Calendar.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(Calendar.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 { Calendar.first.update(default: false) }
  25. .not_to change { Calendar.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 { Calendar.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)' do
  110. before do
  111. calendar # create and sync
  112. travel 1.year
  113. end
  114. it 'appends newly computed event data to #public_holidays' do
  115. expect { calendar.sync }.to change(calendar, :public_holidays).to(
  116. '2016-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  117. '2017-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  118. '2018-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  119. '2019-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  120. '2020-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  121. )
  122. end
  123. it 'does create a background job for escalation rebuild' do
  124. expect { calendar.sync }.to change { Delayed::Job.count }.by(1)
  125. end
  126. end
  127. context 'and iCal URL has changed' do
  128. before { calendar.assign_attributes(ical_url: Rails.root.join('test', 'data', 'calendar', 'calendar2.ics')) }
  129. it 'replaces #public_holidays with event data computed from new iCal URL' do
  130. expect { calendar.save }
  131. .to change(calendar, :public_holidays).to(
  132. '2016-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  133. '2016-12-25' => { 'active' => true, 'summary' => 'Christmas2', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  134. '2017-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  135. '2017-12-25' => { 'active' => true, 'summary' => 'Christmas2', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  136. '2018-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  137. '2018-12-25' => { 'active' => true, 'summary' => 'Christmas2', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  138. '2019-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  139. '2019-12-25' => { 'active' => true, 'summary' => 'Christmas2', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
  140. )
  141. end
  142. end
  143. end
  144. end
  145. describe '#validate_hours' do
  146. context 'when business_hours are invalid' do
  147. it 'fails for hours ending at 00:00' do
  148. expect do
  149. create(:calendar,
  150. business_hours: {
  151. mon: {
  152. active: true,
  153. timeframes: [['09:00', '00:00']]
  154. },
  155. tue: {
  156. active: true,
  157. timeframes: [['09:00', '00:00']]
  158. },
  159. wed: {
  160. active: true,
  161. timeframes: [['09:00', '00:00']]
  162. },
  163. thu: {
  164. active: true,
  165. timeframes: [['09:00', '00:00']]
  166. },
  167. fri: {
  168. active: true,
  169. timeframes: [['09:00', '00:00']]
  170. },
  171. sat: {
  172. active: false,
  173. timeframes: [['09:00', '00:00']]
  174. },
  175. sun: {
  176. active: false,
  177. timeframes: [['09:00', '00:00']]
  178. }
  179. })
  180. end.to raise_error(Exceptions::UnprocessableEntity, 'nonsensical hours provided')
  181. end
  182. it 'fails for blank structure' do
  183. expect do
  184. create(:calendar,
  185. business_hours: {})
  186. end.to raise_error(Exceptions::UnprocessableEntity, 'No configured business hours found!')
  187. end
  188. end
  189. end
  190. end