Commit 4949e2b2 authored by Shinya Maeda's avatar Shinya Maeda

Separate cron_valid? and cron_time_zone_valid?

parent 3d3df097
......@@ -12,7 +12,7 @@ module Ci
validates :trigger, presence: { unless: :importing? }
validates :cron, cron: true, presence: { unless: :importing? }
validates :cron_time_zone, presence: { unless: :importing? }
validates :cron_time_zone, cron_time_zone: true, presence: { unless: :importing? }
validates :ref, presence: { unless: :importing? }
after_create :schedule_next_run!
......
# CronTimeZoneValidator
#
# Custom validator for CronTimeZone.
class CronTimeZoneValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
cron_parser = Gitlab::Ci::CronParser.new(record.cron, record.cron_time_zone)
record.errors.add(attribute, " is invalid syntax") unless cron_parser.cron_time_zone_valid?
end
end
......@@ -4,12 +4,6 @@
class CronValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
cron_parser = Gitlab::Ci::CronParser.new(record.cron, record.cron_time_zone)
is_valid_cron, is_valid_cron_time_zone = cron_parser.validation
if !is_valid_cron
record.errors.add(:cron, " is invalid syntax")
elsif !is_valid_cron_time_zone
record.errors.add(:cron_time_zone, " is invalid timezone")
end
record.errors.add(attribute, " is invalid syntax") unless cron_parser.cron_valid?
end
end
......@@ -14,10 +14,12 @@ module Gitlab
cron_line.next_time(time).in_time_zone(Time.zone) if cron_line.present?
end
def validation
is_valid_cron = try_parse_cron(@cron, VALID_SYNTAX_SAMPLE_TIME_ZONE).present?
is_valid_cron_time_zone = try_parse_cron(VALID_SYNTAX_SAMPLE_CRON, @cron_time_zone).present?
return is_valid_cron, is_valid_cron_time_zone
def cron_valid?
try_parse_cron(@cron, VALID_SYNTAX_SAMPLE_TIME_ZONE).present?
end
def cron_time_zone_valid?
try_parse_cron(VALID_SYNTAX_SAMPLE_CRON, @cron_time_zone).present?
end
private
......
......@@ -82,23 +82,35 @@ describe Gitlab::Ci::CronParser do
end
end
describe '#validation' do
it 'returns results' do
is_valid_cron, is_valid_cron_time_zone = described_class.new('* * * * *', 'Europe/Istanbul').validation
expect(is_valid_cron).to eq(true)
expect(is_valid_cron_time_zone).to eq(true)
describe '#cron_valid?' do
subject { described_class.new(cron, Gitlab::Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE).cron_valid? }
context 'when cron is valid' do
let(:cron) { '* * * * *' }
it { is_expected.to eq(true) }
end
it 'returns results' do
is_valid_cron, is_valid_cron_time_zone = described_class.new('*********', 'Europe/Istanbul').validation
expect(is_valid_cron).to eq(false)
expect(is_valid_cron_time_zone).to eq(true)
context 'when cron is invalid' do
let(:cron) { '*********' }
it { is_expected.to eq(false) }
end
end
describe '#cron_time_zone_valid?' do
subject { described_class.new(Gitlab::Ci::CronParser::VALID_SYNTAX_SAMPLE_CRON, cron_time_zone).cron_time_zone_valid? }
context 'when cron is valid' do
let(:cron_time_zone) { 'Europe/Istanbul' }
it { is_expected.to eq(true) }
end
context 'when cron is invalid' do
let(:cron_time_zone) { 'Invalid-zone' }
it 'returns results' do
is_valid_cron, is_valid_cron_time_zone = described_class.new('* * * * *', 'Invalid-zone').validation
expect(is_valid_cron).to eq(true)
expect(is_valid_cron_time_zone).to eq(false)
it { is_expected.to eq(false) }
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment