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