Add limitation for start_in keyword

This commit is contained in:
Shinya Maeda 2018-10-04 18:59:34 +09:00
parent 28f895e495
commit e84230ebb6
4 changed files with 27 additions and 5 deletions

View file

@ -36,7 +36,7 @@ module Gitlab
validates :extends, type: String
end
validates :start_in, duration: true, if: :delayed?
validates :start_in, duration: { limit: '1 day' }, if: :delayed?
validates :start_in, absence: true, unless: :delayed?
end

View file

@ -11,6 +11,15 @@ module Gitlab
false
end
def validate_duration_limit(value, limit)
return false unless value.is_a?(String)
ChronicDuration.parse(value).second.from_now <
ChronicDuration.parse(limit).second.from_now
rescue ChronicDuration::DurationParseError
false
end
def validate_array_of_strings(values)
values.is_a?(Array) && values.all? { |value| validate_string(value) }
end

View file

@ -49,6 +49,12 @@ module Gitlab
unless validate_duration(value)
record.errors.add(attribute, 'should be a duration')
end
if options[:limit]
unless validate_duration_limit(value, options[:limit])
record.errors.add(attribute, 'should not exceed the limit')
end
end
end
end

View file

@ -44,9 +44,7 @@ describe Gitlab::Ci::Config::Entry::Job do
context 'when start_in is specified' do
let(:config) { { script: 'echo', when: 'delayed', start_in: '1 day' } }
it 'returns error about invalid type' do
expect(entry).to be_valid
end
it { expect(entry).to be_valid }
end
end
end
@ -158,7 +156,7 @@ describe Gitlab::Ci::Config::Entry::Job do
end
end
context 'when start_in is not formateed ad a duration' do
context 'when start_in is not formatted as a duration' do
let(:config) { { when: 'delayed', start_in: 'test' } }
it 'returns error about invalid type' do
@ -166,6 +164,15 @@ describe Gitlab::Ci::Config::Entry::Job do
expect(entry.errors).to include 'job start in should be a duration'
end
end
context 'when start_in is longer than one day' do
let(:config) { { when: 'delayed', start_in: '2 days' } }
it 'returns error about exceeding the limit' do
expect(entry).not_to be_valid
expect(entry.errors).to include 'job start in should not exceed the limit'
end
end
end
context 'when start_in specified without delayed specification' do