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