BUGFIX: topic specs were dependent on Time.now

which would fail when DST was around
This commit is contained in:
Régis Hanol 2014-03-06 12:44:52 +01:00
parent aeafca6649
commit ac99947979
2 changed files with 11 additions and 10 deletions

View File

@ -658,11 +658,11 @@ class Topic < ActiveRecord::Base
# * A timestamp with timezone in JSON format. (e.g., "2013-11-26T21:00:00.000Z") # * A timestamp with timezone in JSON format. (e.g., "2013-11-26T21:00:00.000Z")
# * nil, to prevent the topic from automatically closing. # * nil, to prevent the topic from automatically closing.
def set_auto_close(arg, by_user=nil) def set_auto_close(arg, by_user=nil)
if arg.is_a?(String) and matches = /^([\d]{1,2}):([\d]{1,2})$/.match(arg.strip) if arg.is_a?(String) && matches = /^([\d]{1,2}):([\d]{1,2})$/.match(arg.strip)
now = Time.zone.now now = Time.zone.now
self.auto_close_at = Time.zone.local(now.year, now.month, now.day, matches[1].to_i, matches[2].to_i) self.auto_close_at = Time.zone.local(now.year, now.month, now.day, matches[1].to_i, matches[2].to_i)
self.auto_close_at += 1.day if self.auto_close_at < now self.auto_close_at += 1.day if self.auto_close_at < now
elsif arg.is_a?(String) and arg.include?('-') and timestamp = Time.zone.parse(arg) elsif arg.is_a?(String) && arg.include?('-') && timestamp = Time.zone.parse(arg)
self.auto_close_at = timestamp self.auto_close_at = timestamp
self.errors.add(:auto_close_at, :invalid) if timestamp < Time.zone.now self.errors.add(:auto_close_at, :invalid) if timestamp < Time.zone.now
else else
@ -672,7 +672,7 @@ class Topic < ActiveRecord::Base
unless self.auto_close_at.nil? unless self.auto_close_at.nil?
self.auto_close_started_at ||= Time.zone.now self.auto_close_started_at ||= Time.zone.now
if by_user and by_user.staff? if by_user && by_user.staff?
self.auto_close_user = by_user self.auto_close_user = by_user
else else
self.auto_close_user ||= (self.user.staff? ? self.user : Discourse.system_user) self.auto_close_user ||= (self.user.staff? ? self.user : Discourse.system_user)

View File

@ -1089,46 +1089,47 @@ describe Topic do
let(:topic) { Fabricate.build(:topic) } let(:topic) { Fabricate.build(:topic) }
let(:closing_topic) { Fabricate.build(:topic, auto_close_hours: 5) } let(:closing_topic) { Fabricate.build(:topic, auto_close_hours: 5) }
let(:admin) { Fabricate.build(:user, id: 123) } let(:admin) { Fabricate.build(:user, id: 123) }
let(:now) { Time.zone.local(2013,11,20,8,0) }
before { Discourse.stubs(:system_user).returns(admin) } before { Discourse.stubs(:system_user).returns(admin) }
it 'can take a number of hours as an integer' do it 'can take a number of hours as an integer' do
Timecop.freeze(Time.zone.now) do Timecop.freeze(now) do
topic.set_auto_close(72, admin) topic.set_auto_close(72, admin)
expect(topic.auto_close_at).to eq(3.days.from_now) expect(topic.auto_close_at).to eq(3.days.from_now)
end end
end end
it 'can take a number of hours as a string' do it 'can take a number of hours as a string' do
Timecop.freeze(Time.zone.now) do Timecop.freeze(now) do
topic.set_auto_close('18', admin) topic.set_auto_close('18', admin)
expect(topic.auto_close_at).to eq(18.hours.from_now) expect(topic.auto_close_at).to eq(18.hours.from_now)
end end
end end
it "can take a time later in the day" do it "can take a time later in the day" do
Timecop.freeze(Time.zone.local(2013,11,20,8,0)) do Timecop.freeze(now) do
topic.set_auto_close('13:00', admin) topic.set_auto_close('13:00', admin)
topic.auto_close_at.should == Time.zone.local(2013,11,20,13,0) topic.auto_close_at.should == Time.zone.local(2013,11,20,13,0)
end end
end end
it "can take a time for the next day" do it "can take a time for the next day" do
Timecop.freeze(Time.zone.local(2013,11,20,8,0)) do Timecop.freeze(now) do
topic.set_auto_close('5:00', admin) topic.set_auto_close('5:00', admin)
topic.auto_close_at.should == Time.zone.local(2013,11,21,5,0) topic.auto_close_at.should == Time.zone.local(2013,11,21,5,0)
end end
end end
it "can take a timestamp for a future time" do it "can take a timestamp for a future time" do
Timecop.freeze(Time.zone.local(2013,11,20,8,0)) do Timecop.freeze(now) do
topic.set_auto_close('2013-11-22 5:00', admin) topic.set_auto_close('2013-11-22 5:00', admin)
topic.auto_close_at.should == Time.zone.local(2013,11,22,5,0) topic.auto_close_at.should == Time.zone.local(2013,11,22,5,0)
end end
end end
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
Timecop.freeze(Time.zone.local(2013,11,20,8,0)) do Timecop.freeze(now) do
topic.set_auto_close('2013-11-19 5:00', admin) topic.set_auto_close('2013-11-19 5:00', admin)
topic.auto_close_at.should == Time.zone.local(2013,11,19,5,0) topic.auto_close_at.should == Time.zone.local(2013,11,19,5,0)
topic.errors[:auto_close_at].should be_present topic.errors[:auto_close_at].should be_present
@ -1136,7 +1137,7 @@ describe Topic do
end end
it "can take a timestamp with timezone" do it "can take a timestamp with timezone" do
Timecop.freeze(Time.utc(2013,11,20,12,0)) do Timecop.freeze(now) do
topic.set_auto_close('2013-11-25T01:35:00-08:00', admin) topic.set_auto_close('2013-11-25T01:35:00-08:00', admin)
topic.auto_close_at.should == Time.utc(2013,11,25,9,35) topic.auto_close_at.should == Time.utc(2013,11,25,9,35)
end end