2020-05-08 20:09:39 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe CronValidator do
|
2020-05-08 20:09:39 -04:00
|
|
|
subject do
|
|
|
|
Class.new do
|
|
|
|
include ActiveModel::Model
|
|
|
|
include ActiveModel::Validations
|
|
|
|
attr_accessor :cron
|
2022-02-28 13:14:03 -05:00
|
|
|
|
2020-05-08 20:09:39 -04:00
|
|
|
validates :cron, cron: true
|
|
|
|
|
|
|
|
def cron_timezone
|
|
|
|
'UTC'
|
|
|
|
end
|
|
|
|
end.new
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates valid crontab' do
|
|
|
|
subject.cron = '0 23 * * 5'
|
|
|
|
|
|
|
|
expect(subject.valid?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates invalid crontab' do
|
|
|
|
subject.cron = 'not a cron'
|
|
|
|
|
|
|
|
expect(subject.valid?).to be_falsy
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'cron field is not whitelisted' do
|
|
|
|
subject do
|
|
|
|
Class.new do
|
|
|
|
include ActiveModel::Model
|
|
|
|
include ActiveModel::Validations
|
|
|
|
attr_accessor :cron_partytime
|
2022-02-28 13:14:03 -05:00
|
|
|
|
2020-05-08 20:09:39 -04:00
|
|
|
validates :cron_partytime, cron: true
|
|
|
|
end.new
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
subject.cron_partytime = '0 23 * * 5'
|
|
|
|
|
|
|
|
expect { subject.valid? }.to raise_error(StandardError, "Non-whitelisted attribute")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|