Skip to content

A platform for community discussion. Free, open, simple.

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

Mark VanLandingham9f786306bd28
diff --git a/app/models/topic.rb b/app/models/topic.rb
index 7cf68865200..8e761c33f03 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -1339,10 +1339,9 @@ class Topic < ActiveRecord::Base
topic_timer.execute_at = num_hours.hours.from_now if num_hours > 0
else
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"
topic_timer.execute_at = timestamp
- topic_timer.errors.add(:execute_at, :invalid) if timestamp < utc.now
end
end
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index 582db5785ac..8ccac8ac3db 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -1756,10 +1756,13 @@ describe Topic do
it "sets a validation error when given a timestamp in the past" do
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(topic.topic_timers.first.errors[:execute_at]).to be_present
+ expect do
+ topic.set_or_create_timer(
+ TopicTimer.types[:close],
+ '2013-11-19 5:00', by_user: admin
+ )
+ end.to raise_error(Discourse::InvalidParameters)
end
it "sets a validation error when give a timestamp of an invalid format" do
diff --git a/spec/requests/api/topics_spec.rb b/spec/requests/api/topics_spec.rb
index 22a0b64b5ec..a2592cae036 100644
--- a/spec/requests/api/topics_spec.rb
+++ b/spec/requests/api/topics_spec.rb
@@ -954,7 +954,7 @@ describe 'topics' do
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(:id) { topic_post.topic.id }
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 214263be022..92d1eed19dd 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -2932,6 +2932,19 @@ RSpec.describe TopicsController do
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
before do
freeze_time