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
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-05-15 15:19:41 -04:00
|
|
|
|
|
|
|
describe Topic do
|
2017-03-21 23:12:02 -04:00
|
|
|
let(:job_klass) { Jobs::ToggleTopicClosed }
|
2013-05-15 15:19:41 -04:00
|
|
|
|
|
|
|
context '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
|
|
|
|
|
|
|
context '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
|
|
|
|
|
|
|
|
context '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
|
|
|
|
|
|
|
|
context 'jobs may be queued' do
|
|
|
|
before do
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context '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)
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(topic.public_topic_timer.execute_at).to eq_time(2.hours.from_now)
|
2017-03-21 23:12:02 -04:00
|
|
|
|
|
|
|
args = job_klass.jobs.last['args'].first
|
|
|
|
|
2017-05-16 14:49:42 -04:00
|
|
|
expect(args["topic_timer_id"]).to eq(topic.public_topic_timer.id)
|
2017-03-21 23:12:02 -04:00
|
|
|
expect(args["state"]).to eq(true)
|
2016-12-12 20:59:38 -05:00
|
|
|
end
|
2013-05-15 15:19:41 -04:00
|
|
|
|
|
|
|
context '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)
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(topic_status_update.execute_at).to eq_time(2.hours.from_now)
|
2020-08-26 00:36:51 -04:00
|
|
|
expect(topic_status_update.user).to eq(Discourse.system_user)
|
2017-03-21 23:12:02 -04:00
|
|
|
|
|
|
|
args = job_klass.jobs.last['args'].first
|
|
|
|
|
2017-05-11 18:23:18 -04:00
|
|
|
expect(args["topic_timer_id"]).to eq(topic_status_update.id)
|
2017-03-21 23:12:02 -04:00
|
|
|
expect(args["state"]).to eq(true)
|
2016-12-12 20:59:38 -05:00
|
|
|
end
|
2013-07-03 16:44:45 -04:00
|
|
|
|
|
|
|
context '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)
|
2020-03-10 17:13:17 -04:00
|
|
|
.to eq_time(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
|
|
|
|
|
|
|
|
context '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)
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(topic_status_update.execute_at).to eq_time(2.hours.from_now)
|
2017-03-21 23:12:02 -04:00
|
|
|
expect(topic_status_update.user).to eq(Discourse.system_user)
|
2013-05-15 15:19:41 -04:00
|
|
|
|
2017-03-21 23:12:02 -04:00
|
|
|
args = job_klass.jobs.last['args'].first
|
2016-12-12 20:59:38 -05:00
|
|
|
|
2017-05-11 18:23:18 -04:00
|
|
|
expect(args["topic_timer_id"]).to eq(topic_status_update.id)
|
2017-03-21 23:12:02 -04:00
|
|
|
expect(args["state"]).to eq(true)
|
2016-12-12 20:59:38 -05:00
|
|
|
end
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|