2019-01-04 07:37:30 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Calendar, type: :model do
|
|
|
|
subject(:calendar) { create(:calendar) }
|
|
|
|
|
|
|
|
describe 'attributes' do
|
|
|
|
describe '#default' do
|
2019-09-16 15:04:17 +00:00
|
|
|
before { expect(described_class.pluck(:default)).to eq([true]) }
|
2019-01-04 07:37:30 +00:00
|
|
|
|
|
|
|
context 'when set to true on creation' do
|
|
|
|
subject(:calendar) { build(:calendar, default: true) }
|
|
|
|
|
|
|
|
it 'stays true and sets all other calendars to default: false' do
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { calendar.tap(&:save).reload }.not_to change(calendar, :default)
|
2019-09-16 15:04:17 +00:00
|
|
|
expect(described_class.where(default: true) - [calendar]).to be_empty
|
2019-01-04 07:37:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when set to true on update' do
|
|
|
|
subject(:calendar) { create(:calendar, default: false) }
|
2019-04-15 01:41:17 +00:00
|
|
|
|
2019-01-04 07:37:30 +00:00
|
|
|
before { calendar.default = true }
|
|
|
|
|
|
|
|
it 'stays true and sets all other calendars to default: false' do
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { calendar.tap(&:save).reload }.not_to change(calendar, :default)
|
2019-09-16 15:04:17 +00:00
|
|
|
expect(described_class.where(default: true) - [calendar]).to be_empty
|
2019-01-04 07:37:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when set to false on update' do
|
|
|
|
it 'sets default: true on earliest-created calendar' do
|
2019-09-16 15:04:17 +00:00
|
|
|
expect { described_class.first.update(default: false) }
|
|
|
|
.not_to change { described_class.first.default }
|
2019-01-04 07:37:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when default calendar is destroyed' do
|
|
|
|
subject!(:calendar) { create(:calendar, default: false) }
|
|
|
|
|
|
|
|
it 'sets default: true on earliest-created remaining calendar' do
|
2019-09-16 15:04:17 +00:00
|
|
|
expect { described_class.first.destroy }
|
2019-01-04 07:37:30 +00:00
|
|
|
.to change { calendar.reload.default }.to(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#public_holidays' do
|
|
|
|
subject(:calendar) do
|
|
|
|
create(:calendar, ical_url: Rails.root.join('test', 'data', 'calendar', 'calendar1.ics'))
|
|
|
|
end
|
|
|
|
|
|
|
|
before { travel_to Time.zone.parse('2017-08-24T01:04:44Z0') }
|
|
|
|
|
|
|
|
context 'on creation' do
|
|
|
|
it 'is computed from iCal event data (implicitly via #sync), from one year before to three years after' do
|
|
|
|
expect(calendar.public_holidays).to eq(
|
|
|
|
'2016-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2017-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2018-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2019-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with one-time and n-time (recurring) events' do
|
|
|
|
subject(:calendar) do
|
|
|
|
create(:calendar, ical_url: Rails.root.join('test', 'data', 'calendar', 'calendar3.ics'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accurately computes/imports events' do
|
|
|
|
expect(calendar.public_holidays).to eq(
|
|
|
|
'2016-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2016-12-26' => { 'active' => true, 'summary' => 'day3', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2016-12-28' => { 'active' => true, 'summary' => 'day5', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2017-01-26' => { 'active' => true, 'summary' => 'day3', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2017-02-26' => { 'active' => true, 'summary' => 'day3', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2017-03-26' => { 'active' => true, 'summary' => 'day3', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2017-04-26' => { 'active' => true, 'summary' => 'day3', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2017-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2018-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2019-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#sync' do
|
|
|
|
subject(:calendar) do
|
2019-05-20 12:18:15 +00:00
|
|
|
create(:calendar, ical_url: Rails.root.join('test', 'data', 'calendar', 'calendar1.ics'), default: false)
|
2019-01-04 07:37:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
before { travel_to Time.zone.parse('2017-08-24T01:04:44Z0') }
|
|
|
|
|
|
|
|
context 'when called explicitly after creation' do
|
|
|
|
it 'writes #public_holidays to the cache (valid for 1 day)' do
|
|
|
|
expect(Cache.get("CalendarIcal::#{calendar.id}")).to be(nil)
|
|
|
|
|
|
|
|
expect { calendar.sync }
|
|
|
|
.to change { Cache.get("CalendarIcal::#{calendar.id}") }
|
|
|
|
.to(calendar.attributes.slice('public_holidays', 'ical_url').symbolize_keys)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and neither current date nor iCal URL have changed' do
|
|
|
|
it 'is idempotent' do
|
|
|
|
expect { calendar.sync }
|
2019-04-15 01:41:17 +00:00
|
|
|
.not_to change(calendar, :public_holidays)
|
2019-01-04 07:37:30 +00:00
|
|
|
end
|
2019-05-20 12:18:15 +00:00
|
|
|
it 'does not create a background job for escalation rebuild' do
|
|
|
|
calendar # create and sync (1 inital background job is created)
|
|
|
|
expect { calendar.sync } # a second sync right after calendar create
|
|
|
|
.to not_change { Delayed::Job.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and current date has changed but neither public_holidays nor iCal URL have changed (past cache expiry)' do
|
|
|
|
before do
|
|
|
|
calendar # create and sync
|
|
|
|
travel 2.days
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is idempotent' do
|
|
|
|
expect { calendar.sync }
|
2019-04-15 01:41:17 +00:00
|
|
|
.not_to change(calendar, :public_holidays)
|
2019-05-20 12:18:15 +00:00
|
|
|
end
|
|
|
|
it 'does not create a background job for escalation rebuild' do
|
|
|
|
expect { calendar.sync }
|
|
|
|
.not_to change { Delayed::Job.count }
|
|
|
|
end
|
2019-01-04 07:37:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'and current date has changed (past cache expiry)' do
|
|
|
|
before do
|
|
|
|
calendar # create and sync
|
|
|
|
travel 1.year
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends newly computed event data to #public_holidays' do
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { calendar.sync }.to change(calendar, :public_holidays).to(
|
2019-01-04 07:37:30 +00:00
|
|
|
'2016-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2017-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2018-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2019-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2020-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
)
|
|
|
|
end
|
2019-05-20 12:18:15 +00:00
|
|
|
|
|
|
|
it 'does create a background job for escalation rebuild' do
|
|
|
|
expect { calendar.sync }.to change { Delayed::Job.count }.by(1)
|
|
|
|
end
|
2019-01-04 07:37:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'and iCal URL has changed' do
|
|
|
|
before { calendar.assign_attributes(ical_url: Rails.root.join('test', 'data', 'calendar', 'calendar2.ics')) }
|
|
|
|
|
|
|
|
it 'replaces #public_holidays with event data computed from new iCal URL' do
|
|
|
|
expect { calendar.save }
|
2019-04-15 01:41:17 +00:00
|
|
|
.to change(calendar, :public_holidays).to(
|
2019-01-04 07:37:30 +00:00
|
|
|
'2016-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2016-12-25' => { 'active' => true, 'summary' => 'Christmas2', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2017-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2017-12-25' => { 'active' => true, 'summary' => 'Christmas2', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2018-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2018-12-25' => { 'active' => true, 'summary' => 'Christmas2', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2019-12-24' => { 'active' => true, 'summary' => 'Christmas1', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
'2019-12-25' => { 'active' => true, 'summary' => 'Christmas2', 'feed' => Digest::MD5.hexdigest(calendar.ical_url) },
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-07 09:00:56 +00:00
|
|
|
|
|
|
|
describe '#validate_hours' do
|
|
|
|
context 'when business_hours are invalid' do
|
|
|
|
|
|
|
|
it 'fails for hours ending at 00:00' do
|
|
|
|
expect do
|
|
|
|
create(:calendar,
|
|
|
|
business_hours: {
|
|
|
|
mon: {
|
|
|
|
active: true,
|
|
|
|
timeframes: [['09:00', '00:00']]
|
|
|
|
},
|
|
|
|
tue: {
|
|
|
|
active: true,
|
|
|
|
timeframes: [['09:00', '00:00']]
|
|
|
|
},
|
|
|
|
wed: {
|
|
|
|
active: true,
|
|
|
|
timeframes: [['09:00', '00:00']]
|
|
|
|
},
|
|
|
|
thu: {
|
|
|
|
active: true,
|
|
|
|
timeframes: [['09:00', '00:00']]
|
|
|
|
},
|
|
|
|
fri: {
|
|
|
|
active: true,
|
|
|
|
timeframes: [['09:00', '00:00']]
|
|
|
|
},
|
|
|
|
sat: {
|
|
|
|
active: false,
|
|
|
|
timeframes: [['09:00', '00:00']]
|
|
|
|
},
|
|
|
|
sun: {
|
|
|
|
active: false,
|
|
|
|
timeframes: [['09:00', '00:00']]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end.to raise_error(Exceptions::UnprocessableEntity, 'nonsensical hours provided')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails for blank structure' do
|
|
|
|
expect do
|
|
|
|
create(:calendar,
|
|
|
|
business_hours: {})
|
|
|
|
end.to raise_error(Exceptions::UnprocessableEntity, 'No configured business hours found!')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-01-04 07:37:30 +00:00
|
|
|
end
|