2013-05-15 15:19:41 -04:00
|
|
|
# encoding: UTF-8
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-05-15 15:19:41 -04:00
|
|
|
require 'sidekiq/testing'
|
|
|
|
|
|
|
|
describe Topic do
|
|
|
|
|
|
|
|
def scheduled_jobs_for(job_name, params={})
|
2015-12-31 23:48:49 -05:00
|
|
|
"Jobs::#{job_name.to_s.camelcase}".constantize.jobs.select do |job|
|
2015-12-31 23:40:12 -05:00
|
|
|
job_args = job['args'][0]
|
2015-12-31 23:48:49 -05:00
|
|
|
matched = true
|
|
|
|
params.each do |key, value|
|
|
|
|
unless job_args[key.to_s] == value
|
|
|
|
matched = false
|
|
|
|
break
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
end
|
2015-12-31 23:48:49 -05:00
|
|
|
matched
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-12 20:59:38 -05:00
|
|
|
before do
|
|
|
|
@original_value = SiteSetting.queue_jobs
|
2015-12-31 23:40:12 -05:00
|
|
|
SiteSetting.queue_jobs = true
|
2015-12-31 23:48:49 -05:00
|
|
|
Jobs::CloseTopic.jobs.clear
|
2016-12-12 20:59:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
SiteSetting.queue_jobs = @original_value
|
|
|
|
end
|
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
|
|
|
|
expect(topic.auto_close_at).to eq(nil)
|
|
|
|
expect(scheduled_jobs_for(:close_topic)).to be_empty
|
|
|
|
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
|
|
|
|
expect(topic.auto_close_at).to eq(nil)
|
|
|
|
expect(scheduled_jobs_for(:close_topic)).to be_empty
|
|
|
|
end
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'jobs may be queued' do
|
|
|
|
before do
|
|
|
|
Timecop.freeze(Time.zone.now)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Timecop.return
|
|
|
|
Sidekiq::Extensions::DelayedClass.jobs.clear
|
|
|
|
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
|
|
|
|
expect(topic.auto_close_at).to be_within_one_second_of(2.hours.from_now)
|
|
|
|
expect(topic.auto_close_started_at).to eq(Time.zone.now)
|
|
|
|
expect(scheduled_jobs_for(:close_topic, {topic_id: topic.id}).size).to eq(1)
|
|
|
|
expect(scheduled_jobs_for(:close_topic, {topic_id: category.topic.id})).to be_empty
|
|
|
|
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
|
|
|
|
expect(scheduled_jobs_for(:close_topic, {topic_id: staff_topic.id, user_id: admin.id}).size).to eq(1)
|
|
|
|
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
|
|
|
|
staff_topic.update_status('closed', true, admin)
|
|
|
|
expect(staff_topic.reload.auto_close_at).to eq(nil)
|
|
|
|
expect(staff_topic.auto_close_started_at).to eq(nil)
|
|
|
|
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
|
|
|
|
expect(scheduled_jobs_for(:close_topic, {topic_id: regular_user_topic.id, user_id: Discourse.system_user.id}).size).to eq(1)
|
|
|
|
end
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
|
2013-12-06 16:39:35 -05:00
|
|
|
context 'auto_close_hours of topic was set to 0' do
|
2016-12-12 20:59:38 -05:00
|
|
|
let(:dont_close_topic) { Fabricate(:topic, auto_close_hours: 0, category: category) }
|
|
|
|
|
|
|
|
it 'should not schedule the topic to auto-close' do
|
|
|
|
expect(scheduled_jobs_for(:close_topic)).to be_empty
|
|
|
|
end
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'two topics in the category' do
|
2016-12-12 20:59:38 -05:00
|
|
|
let!(:other_topic) { Fabricate(:topic, category: category) }
|
|
|
|
|
|
|
|
it 'should schedule the topic to auto-close' do
|
|
|
|
topic
|
|
|
|
|
|
|
|
expect(scheduled_jobs_for(:close_topic).size).to eq(2)
|
|
|
|
end
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
end
|
2013-12-19 11:24:21 -05:00
|
|
|
|
|
|
|
context 'a topic that has been auto-closed' do
|
2016-12-12 20:59:38 -05:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let!(:auto_closed_topic) { Fabricate(:topic, user: admin, closed: true, auto_close_at: 1.day.ago, auto_close_user_id: admin.id, auto_close_started_at: 6.days.ago) }
|
|
|
|
|
|
|
|
it 'should set the right attributes' do
|
|
|
|
auto_closed_topic.update_status('closed', false, admin)
|
|
|
|
expect(auto_closed_topic.reload.auto_close_at).to eq(nil)
|
|
|
|
expect(auto_closed_topic.auto_close_started_at).to eq(nil)
|
|
|
|
end
|
2013-12-19 11:24:21 -05:00
|
|
|
end
|
2013-05-15 15:19:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|