FIX: Actually error when topic timer time is in the past (#11434)

This commit is contained in:
Mark VanLandingham 2020-12-08 11:13:45 -06:00 committed by GitHub
parent b824af02d4
commit 9f786306bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 6 deletions

View File

@ -1339,10 +1339,9 @@ class Topic < ActiveRecord::Base
topic_timer.execute_at = num_hours.hours.from_now if num_hours > 0 topic_timer.execute_at = num_hours.hours.from_now if num_hours > 0
else else
timestamp = utc.parse(time) timestamp = utc.parse(time)
raise Discourse::InvalidParameters unless timestamp raise Discourse::InvalidParameters unless timestamp && timestamp > utc.now
# a timestamp in client's time zone, like "2015-5-27 12:00" # a timestamp in client's time zone, like "2015-5-27 12:00"
topic_timer.execute_at = timestamp topic_timer.execute_at = timestamp
topic_timer.errors.add(:execute_at, :invalid) if timestamp < utc.now
end end
end end

View File

@ -1756,10 +1756,13 @@ describe Topic do
it "sets a validation error when given a timestamp in the past" do it "sets a validation error when given a timestamp in the past" do
freeze_time now freeze_time now
topic.set_or_create_timer(TopicTimer.types[:close], '2013-11-19 5:00', by_user: admin)
expect(topic.topic_timers.first.execute_at).to eq_time(Time.zone.local(2013, 11, 19, 5, 0)) expect do
expect(topic.topic_timers.first.errors[:execute_at]).to be_present topic.set_or_create_timer(
TopicTimer.types[:close],
'2013-11-19 5:00', by_user: admin
)
end.to raise_error(Discourse::InvalidParameters)
end end
it "sets a validation error when give a timestamp of an invalid format" do it "sets a validation error when give a timestamp of an invalid format" do

View File

@ -954,7 +954,7 @@ describe 'topics' do
category_id: { type: :string, nullable: true }, category_id: { type: :string, nullable: true },
} }
let(:request_body) { { time: '2020-07-11+18:00-06:00', status_type: 'close' } } let(:request_body) { { time: Time.current + 1.day, status_type: 'close' } }
let!(:topic_post) { Fabricate(:post) } let!(:topic_post) { Fabricate(:post) }
let(:id) { topic_post.topic.id } let(:id) { topic_post.topic.id }

View File

@ -2932,6 +2932,19 @@ RSpec.describe TopicsController do
end end
end end
context 'when time is in the past' do
it 'returns an error' do
freeze_time
sign_in(admin)
post "/t/#{topic.id}/timer.json", params: {
time: Time.current - 1.day,
status_type: TopicTimer.types[1]
}
expect(response.status).to eq(400)
end
end
context 'when logged in as an admin' do context 'when logged in as an admin' do
before do before do
freeze_time freeze_time