2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-12 06:23:18 +08:00
|
|
|
RSpec.describe TopicTimer, type: :model do
|
2020-04-21 12:38:35 +08:00
|
|
|
fab!(:topic_timer) { Fabricate(:topic_timer) }
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2015-12-08 20:43:23 +08:00
|
|
|
|
2020-04-21 12:38:35 +08:00
|
|
|
before { freeze_time }
|
2018-06-05 15:29:17 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
context "validations" do
|
2021-01-19 11:30:58 +08:00
|
|
|
describe "pending_timers scope" do
|
|
|
|
it "does not return deleted timers" do
|
|
|
|
topic_timer.trash!
|
|
|
|
expect(TopicTimer.pending_timers.pluck(:id)).not_to include(topic_timer.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not return timers in the future of the provided before time" do
|
|
|
|
topic_timer.update!(execute_at: 3.days.from_now)
|
|
|
|
expect(TopicTimer.pending_timers.pluck(:id)).not_to include(topic_timer.id)
|
|
|
|
expect(TopicTimer.pending_timers(2.days.from_now).pluck(:id)).not_to include(topic_timer.id)
|
|
|
|
topic_timer.update!(execute_at: 1.minute.ago, created_at: 10.minutes.ago)
|
|
|
|
expect(TopicTimer.pending_timers.pluck(:id)).to include(topic_timer.id)
|
|
|
|
end
|
2021-02-12 07:05:14 +08:00
|
|
|
|
|
|
|
describe "duration values" do
|
|
|
|
it "does not allow durations <= 0" do
|
|
|
|
topic_timer.duration_minutes = -1
|
|
|
|
topic_timer.save
|
|
|
|
expect(topic_timer.errors.full_messages.first).to include("Duration minutes must be greater than 0.")
|
|
|
|
end
|
|
|
|
|
2021-02-18 09:52:30 +08:00
|
|
|
it "does not allow crazy big durations (20 years in minutes)" do
|
|
|
|
topic_timer.duration_minutes = 21.years.to_i / 60
|
2021-02-12 07:05:14 +08:00
|
|
|
topic_timer.save
|
2021-02-18 09:52:30 +08:00
|
|
|
expect(topic_timer.errors.full_messages.first).to include("Duration minutes cannot be more than 20 years.")
|
2021-02-12 07:05:14 +08:00
|
|
|
end
|
|
|
|
end
|
2021-01-19 11:30:58 +08:00
|
|
|
end
|
2017-03-22 11:12:02 +08:00
|
|
|
describe '#status_type' do
|
2017-05-17 02:49:42 +08:00
|
|
|
it 'should ensure that only one active public topic status update exists' do
|
2017-05-12 06:23:18 +08:00
|
|
|
topic_timer.update!(topic: topic)
|
|
|
|
Fabricate(:topic_timer, deleted_at: Time.zone.now, topic: topic)
|
2014-10-30 04:26:32 +08:00
|
|
|
|
2017-05-12 06:23:18 +08:00
|
|
|
expect { Fabricate(:topic_timer, topic: topic) }
|
2017-03-22 11:12:02 +08:00
|
|
|
.to raise_error(ActiveRecord::RecordInvalid)
|
|
|
|
end
|
|
|
|
end
|
2014-10-30 04:26:32 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
describe '#execute_at' do
|
|
|
|
describe 'when #execute_at is greater than #created_at' do
|
|
|
|
it 'should be valid' do
|
2017-05-12 06:23:18 +08:00
|
|
|
topic_timer = Fabricate.build(:topic_timer,
|
2017-03-22 11:12:02 +08:00
|
|
|
execute_at: Time.zone.now + 1.hour,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
topic: Fabricate(:topic)
|
|
|
|
)
|
2013-07-04 09:47:12 +08:00
|
|
|
|
2017-05-12 06:23:18 +08:00
|
|
|
expect(topic_timer).to be_valid
|
2017-03-22 11:12:02 +08:00
|
|
|
end
|
|
|
|
end
|
2013-07-04 09:47:12 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
describe 'when #execute_at is smaller than #created_at' do
|
|
|
|
it 'should not be valid' do
|
2017-05-12 06:23:18 +08:00
|
|
|
topic_timer = Fabricate.build(:topic_timer,
|
2017-03-22 11:12:02 +08:00
|
|
|
execute_at: Time.zone.now - 1.hour,
|
|
|
|
created_at: Time.zone.now,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
topic: Fabricate(:topic)
|
|
|
|
)
|
2013-07-04 09:47:12 +08:00
|
|
|
|
2017-05-12 06:23:18 +08:00
|
|
|
expect(topic_timer).to_not be_valid
|
2017-03-22 11:12:02 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-07 15:26:15 +08:00
|
|
|
|
|
|
|
describe '#category_id' do
|
|
|
|
describe 'when #status_type is publish_to_category' do
|
|
|
|
describe 'when #category_id is not present' do
|
|
|
|
it 'should not be valid' do
|
2017-05-12 06:23:18 +08:00
|
|
|
topic_timer = Fabricate.build(:topic_timer,
|
|
|
|
status_type: TopicTimer.types[:publish_to_category]
|
2017-04-07 15:26:15 +08:00
|
|
|
)
|
|
|
|
|
2017-05-12 06:23:18 +08:00
|
|
|
expect(topic_timer).to_not be_valid
|
2019-04-30 14:58:18 +08:00
|
|
|
expect(topic_timer.errors).to include(:category_id)
|
2017-04-07 15:26:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when #category_id is present' do
|
|
|
|
it 'should be valid' do
|
2017-05-12 06:23:18 +08:00
|
|
|
topic_timer = Fabricate.build(:topic_timer,
|
|
|
|
status_type: TopicTimer.types[:publish_to_category],
|
2017-04-07 15:26:15 +08:00
|
|
|
category_id: Fabricate(:category).id,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
topic: Fabricate(:topic)
|
|
|
|
)
|
|
|
|
|
2017-05-12 06:23:18 +08:00
|
|
|
expect(topic_timer).to be_valid
|
2017-04-07 15:26:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-07-04 09:47:12 +08:00
|
|
|
end
|
2014-10-30 04:26:32 +08:00
|
|
|
|
2017-03-22 11:12:02 +08:00
|
|
|
context 'callbacks' do
|
|
|
|
describe 'when #execute_at and #user_id are not changed' do
|
|
|
|
it 'should not schedule another to update topic' do
|
2020-04-21 12:38:35 +08:00
|
|
|
Jobs.expects(:enqueue_at).never
|
2017-03-22 11:12:02 +08:00
|
|
|
|
2017-05-12 06:23:18 +08:00
|
|
|
topic_timer.update!(topic: Fabricate(:topic))
|
2017-03-22 11:12:02 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-26 09:32:11 +08:00
|
|
|
describe 'when a open topic status update is created for an open topic' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:topic) { Fabricate(:topic, closed: false) }
|
2020-04-21 12:38:35 +08:00
|
|
|
fab!(:topic_timer) do
|
2017-05-12 06:23:18 +08:00
|
|
|
Fabricate(:topic_timer,
|
2017-04-03 09:06:15 +08:00
|
|
|
status_type: described_class.types[:open],
|
|
|
|
topic: topic
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-05-31 15:53:49 +08:00
|
|
|
before do
|
2019-03-14 22:47:38 +08:00
|
|
|
Jobs.run_immediately!
|
2018-05-31 15:53:49 +08:00
|
|
|
end
|
|
|
|
|
2020-08-26 09:32:11 +08:00
|
|
|
it 'should close the topic' do
|
2021-01-19 11:30:58 +08:00
|
|
|
topic_timer.send(:schedule_auto_open_job)
|
2020-08-26 09:32:11 +08:00
|
|
|
expect(topic.reload.closed).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2017-04-03 09:06:15 +08:00
|
|
|
|
2020-08-26 09:32:11 +08:00
|
|
|
describe 'when a close topic status update is created for a closed topic' do
|
|
|
|
fab!(:topic) { Fabricate(:topic, closed: true) }
|
|
|
|
fab!(:topic_timer) do
|
|
|
|
Fabricate(:topic_timer,
|
|
|
|
status_type: described_class.types[:close],
|
|
|
|
topic: topic
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
Jobs.run_immediately!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should open the topic' do
|
2021-01-19 11:30:58 +08:00
|
|
|
topic_timer.send(:schedule_auto_close_job)
|
2020-08-26 09:32:11 +08:00
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
|
|
end
|
2017-04-03 09:06:15 +08:00
|
|
|
end
|
2017-05-17 02:49:42 +08:00
|
|
|
|
2017-05-17 09:37:11 +08:00
|
|
|
describe '#public_type' do
|
2017-05-17 02:49:42 +08:00
|
|
|
[:close, :open, :delete].each do |public_type|
|
|
|
|
it "is true for #{public_type}" do
|
|
|
|
timer = Fabricate(:topic_timer, status_type: described_class.types[public_type])
|
|
|
|
expect(timer.public_type).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is true for publish_to_category" do
|
|
|
|
timer = Fabricate(:topic_timer, status_type: described_class.types[:publish_to_category], category: Fabricate(:category))
|
|
|
|
expect(timer.public_type).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
described_class.private_types.keys.each do |private_type|
|
|
|
|
it "is false for #{private_type}" do
|
|
|
|
timer = Fabricate(:topic_timer, status_type: described_class.types[private_type])
|
|
|
|
expect(timer.public_type).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-10-30 04:26:32 +08:00
|
|
|
end
|
|
|
|
|
2021-01-13 06:49:29 +08:00
|
|
|
describe "runnable?" do
|
|
|
|
it "returns false if execute_at > now" do
|
|
|
|
topic_timer = Fabricate.build(:topic_timer,
|
|
|
|
execute_at: Time.zone.now + 1.hour,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
topic: Fabricate(:topic)
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(topic_timer.runnable?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if timer is deleted" do
|
|
|
|
topic_timer = Fabricate.create(:topic_timer,
|
|
|
|
execute_at: Time.zone.now - 1.hour,
|
|
|
|
created_at: Time.zone.now - 2.hour,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
topic: Fabricate(:topic)
|
|
|
|
)
|
|
|
|
topic_timer.trash!
|
|
|
|
|
|
|
|
expect(topic_timer.runnable?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true if execute_at < now" do
|
|
|
|
topic_timer = Fabricate.build(:topic_timer,
|
|
|
|
execute_at: Time.zone.now - 1.hour,
|
|
|
|
created_at: Time.zone.now - 2.hour,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
topic: Fabricate(:topic)
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(topic_timer.runnable?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2013-07-04 09:47:12 +08:00
|
|
|
end
|