Skip to content

Commit

Permalink
Protect against setting retirement dates that have already past.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcculloug committed Sep 5, 2018
1 parent 24340d4 commit 01761c8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/models/service/dialog_properties/retirement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ def retirement_warn_properties
end

def time_parse(value)
with_user_timezone { Time.zone.parse(value).utc }
with_user_timezone do
Time.zone.parse(value).utc.tap do |time|
raise "Retirement date cannot be set in the past" if time < time_now
end
end
end

def time_now
Expand Down
12 changes: 11 additions & 1 deletion spec/models/service/dialog_properties/retirement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
expect(parsed_results[:retirement_warn]).to be_nil
end
end

it 'with an invalid date that has already past' do
Timecop.freeze(time) do
options = {'dialog_service_retires_on' => "2000-01-01"}
parsed_results = described_class.parse(options, nil)

expect(parsed_results[:retires_on]).to be_nil
expect(parsed_results[:retirement_warn]).to be_nil
end
end
end

describe 'retires_in_hours' do
Expand Down Expand Up @@ -76,7 +86,7 @@
context 'when setting retirement warn date' do
it 'with retirement_warn_on' do
user = FactoryGirl.create(:user)
expect(user).to receive(:with_my_timezone).and_yield.twice
expect(user).to receive(:with_my_timezone).exactly(3).times.and_yield

Timecop.freeze(time) do
options = {'dialog_service_retires_in_days' => 5,
Expand Down

0 comments on commit 01761c8

Please sign in to comment.