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
|
|
|
|
|
|
|
describe Topic do
|
2017-03-21 23:12:02 -04:00
|
|
|
let(:job_klass) { Jobs::ToggleTopicClosed }
|
2013-05-15 15:19:41 -04:00
|
|
|
|
2016-12-12 20:59:38 -05:00
|
|
|
before do
|
2017-03-21 23:12:02 -04:00
|
|
|
job_klass.jobs.clear
|
2016-12-12 20:59:38 -05:00
|
|
|
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
|
2017-03-21 23:12:02 -04:00
|
|
|
expect(topic.topic_status_update).to eq(nil)
|
|
|
|
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-03-21 23:12:02 -04:00
|
|
|
expect(topic.topic_status_update).to eq(nil)
|
|
|
|
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-03-21 23:12:02 -04:00
|
|
|
SiteSetting.queue_jobs = true
|
2013-05-15 15:19:41 -04:00
|
|
|
Timecop.freeze(Time.zone.now)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Timecop.return
|
|
|
|
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
|
|
|
|
|
|
|
|
topic_status_update = TopicStatusUpdate.last
|
|
|
|
|
|
|
|
expect(topic_status_update.topic).to eq(topic)
|
|
|
|
expect(topic.topic_status_update.execute_at).to be_within_one_second_of(2.hours.from_now)
|
|
|
|
|
|
|
|
args = job_klass.jobs.last['args'].first
|
|
|
|
|
|
|
|
expect(args["topic_status_update_id"]).to eq(topic.topic_status_update.id)
|
|
|
|
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
|
|
|
|
|
|
|
|
topic_status_update = TopicStatusUpdate.last
|
|
|
|
|
|
|
|
expect(topic_status_update.topic).to eq(staff_topic)
|
|
|
|
expect(topic_status_update.execute_at).to be_within_one_second_of(2.hours.from_now)
|
|
|
|
expect(topic_status_update.user).to eq(admin)
|
|
|
|
|
|
|
|
args = job_klass.jobs.last['args'].first
|
|
|
|
|
|
|
|
expect(args["topic_status_update_id"]).to eq(topic_status_update.id)
|
|
|
|
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-03-21 23:12:02 -04:00
|
|
|
Timecop.freeze do
|
|
|
|
staff_topic.update_status('closed', true, admin)
|
|
|
|
|
|
|
|
expect(staff_topic.topic_status_update.reload.deleted_at)
|
|
|
|
.to be_within(1.second).of(Time.zone.now)
|
|
|
|
end
|
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-03-21 23:12:02 -04:00
|
|
|
topic_status_update = TopicStatusUpdate.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)
|
|
|
|
expect(topic_status_update.execute_at).to be_within_one_second_of(2.hours.from_now)
|
|
|
|
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-03-21 23:12:02 -04:00
|
|
|
expect(args["topic_status_update_id"]).to eq(topic_status_update.id)
|
|
|
|
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
|