2013-05-15 15:19:41 -04:00
|
|
|
# encoding: UTF-8
|
2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
2013-05-15 15:19:41 -04:00
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Topic do
|
2021-01-12 17:49:29 -05:00
|
|
|
let(:job_klass) { Jobs::CloseTopic }
|
2013-05-15 15:19:41 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when creating a topic without auto-close' do
|
2016-12-12 20:59:38 -05:00
|
|
|
let(:topic) { Fabricate(:topic, category: category) }
|
2013-05-15 15:19:41 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when uncategorized' do
|
2016-12-12 20:59:38 -05:00
|
|
|
let(:category) { nil }
|
|
|
|
|
|
|
|
it 'should not schedule the topic to auto-close' do
|
2017-05-16 14:49:42 -04:00
|
|
|
expect(topic.public_topic_timer).to eq(nil)
|
2017-03-21 23:12:02 -04:00
|
|
|
expect(job_klass.jobs).to eq([])
|
2016-12-12 20:59:38 -05:00
|
|
|
end
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'with category without default auto-close' do
|
2016-12-12 20:59:38 -05:00
|
|
|
let(:category) { Fabricate(:category, auto_close_hours: nil) }
|
|
|
|
|
|
|
|
it 'should not schedule the topic to auto-close' do
|
2017-05-16 14:49:42 -04:00
|
|
|
expect(topic.public_topic_timer).to eq(nil)
|
2017-03-21 23:12:02 -04:00
|
|
|
expect(job_klass.jobs).to eq([])
|
2016-12-12 20:59:38 -05:00
|
|
|
end
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when jobs may be queued' do
|
2013-05-15 15:19:41 -04:00
|
|
|
before do
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when category has a default auto-close' do
|
2016-12-12 20:59:38 -05:00
|
|
|
let(:category) { Fabricate(:category, auto_close_hours: 2.0) }
|
|
|
|
|
|
|
|
it 'should schedule the topic to auto-close' do
|
2017-03-21 23:12:02 -04:00
|
|
|
topic
|
|
|
|
|
2017-05-11 18:23:18 -04:00
|
|
|
topic_status_update = TopicTimer.last
|
2017-03-21 23:12:02 -04:00
|
|
|
|
|
|
|
expect(topic_status_update.topic).to eq(topic)
|
2021-02-16 16:51:39 -05:00
|
|
|
expect(topic.public_topic_timer.execute_at).to be_within_one_second_of(2.hours.from_now)
|
2016-12-12 20:59:38 -05:00
|
|
|
end
|
2013-05-15 15:19:41 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when topic was created by staff user' do
|
2016-12-12 20:59:38 -05:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:staff_topic) { Fabricate(:topic, user: admin, category: category) }
|
|
|
|
|
|
|
|
it 'should schedule the topic to auto-close' do
|
2017-03-21 23:12:02 -04:00
|
|
|
staff_topic
|
|
|
|
|
2017-05-11 18:23:18 -04:00
|
|
|
topic_status_update = TopicTimer.last
|
2017-03-21 23:12:02 -04:00
|
|
|
|
|
|
|
expect(topic_status_update.topic).to eq(staff_topic)
|
2021-02-16 16:51:39 -05:00
|
|
|
expect(topic_status_update.execute_at).to be_within_one_second_of(2.hours.from_now)
|
2020-08-26 00:36:51 -04:00
|
|
|
expect(topic_status_update.user).to eq(Discourse.system_user)
|
2016-12-12 20:59:38 -05:00
|
|
|
end
|
2013-07-03 16:44:45 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when topic is closed manually' do
|
2016-12-12 20:59:38 -05:00
|
|
|
it 'should remove the schedule to auto-close the topic' do
|
2017-07-24 09:17:42 -04:00
|
|
|
topic_timer_id = staff_topic.public_topic_timer.id
|
2017-05-11 22:28:51 -04:00
|
|
|
|
2017-07-24 09:17:42 -04:00
|
|
|
staff_topic.update_status('closed', true, admin)
|
2017-03-21 23:12:02 -04:00
|
|
|
|
2017-07-24 09:17:42 -04:00
|
|
|
expect(TopicTimer.with_deleted.find(topic_timer_id).deleted_at)
|
2021-02-16 16:51:39 -05:00
|
|
|
.to be_within_one_second_of(Time.zone.now)
|
2016-12-12 20:59:38 -05:00
|
|
|
end
|
2013-07-03 16:44:45 -04:00
|
|
|
end
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context 'when topic was created by a non-staff user' do
|
2016-12-12 20:59:38 -05:00
|
|
|
let(:regular_user) { Fabricate(:user) }
|
|
|
|
let(:regular_user_topic) { Fabricate(:topic, user: regular_user, category: category) }
|
|
|
|
|
|
|
|
it 'should schedule the topic to auto-close' do
|
2017-03-21 23:12:02 -04:00
|
|
|
regular_user_topic
|
2013-05-15 15:19:41 -04:00
|
|
|
|
2017-05-11 18:23:18 -04:00
|
|
|
topic_status_update = TopicTimer.last
|
2016-12-12 20:59:38 -05:00
|
|
|
|
2017-03-21 23:12:02 -04:00
|
|
|
expect(topic_status_update.topic).to eq(regular_user_topic)
|
2021-02-16 16:51:39 -05:00
|
|
|
expect(topic_status_update.execute_at).to be_within_one_second_of(2.hours.from_now)
|
2017-03-21 23:12:02 -04:00
|
|
|
expect(topic_status_update.user).to eq(Discourse.system_user)
|
2016-12-12 20:59:38 -05:00
|
|
|
end
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|