2017-03-22 11:12:02 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Jobs::ToggleTopicClosed do
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
let(:topic) do
|
2017-07-23 10:47:16 +08:00
|
|
|
Fabricate(:topic_timer, user: admin).topic
|
2017-03-22 11:12:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to close a topic' do
|
|
|
|
topic
|
|
|
|
|
2018-06-05 15:29:17 +08:00
|
|
|
freeze_time(61.minutes.from_now) do
|
2017-03-22 11:12:02 +08:00
|
|
|
described_class.new.execute(
|
2017-05-17 02:49:42 +08:00
|
|
|
topic_timer_id: topic.public_topic_timer.id,
|
2017-03-22 11:12:02 +08:00
|
|
|
state: true
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(true)
|
|
|
|
|
|
|
|
expect(Post.last.raw).to eq(I18n.t(
|
2018-06-05 15:29:17 +08:00
|
|
|
'topic_statuses.autoclosed_enabled_minutes', count: 61
|
2017-03-22 11:12:02 +08:00
|
|
|
))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-27 15:31:59 +08:00
|
|
|
describe 'opening a topic' do
|
|
|
|
it 'should be work' do
|
|
|
|
topic.update!(closed: true)
|
|
|
|
|
2018-06-14 14:10:30 +08:00
|
|
|
freeze_time(61.minutes.from_now) do
|
2018-02-27 15:31:59 +08:00
|
|
|
described_class.new.execute(
|
|
|
|
topic_timer_id: topic.public_topic_timer.id,
|
|
|
|
state: false
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
|
|
|
|
|
|
expect(Post.last.raw).to eq(I18n.t(
|
2018-06-14 14:10:30 +08:00
|
|
|
'topic_statuses.autoclosed_disabled_minutes', count: 61
|
2018-02-27 15:31:59 +08:00
|
|
|
))
|
|
|
|
end
|
|
|
|
end
|
2017-03-22 11:12:02 +08:00
|
|
|
|
2018-02-27 15:31:59 +08:00
|
|
|
describe 'when category has auto close configured' do
|
|
|
|
let(:category) { Fabricate(:category, auto_close_hours: 5) }
|
|
|
|
let(:topic) { Fabricate(:topic, category: category, closed: true) }
|
2017-03-22 11:12:02 +08:00
|
|
|
|
2018-02-27 15:31:59 +08:00
|
|
|
it "should restore the category's auto close timer" do
|
|
|
|
Fabricate(:topic_timer,
|
|
|
|
status_type: TopicTimer.types[:open],
|
|
|
|
topic: topic,
|
|
|
|
user: admin
|
|
|
|
)
|
2017-03-22 11:12:02 +08:00
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
freeze_time(61.minutes.from_now) do
|
2018-02-27 15:31:59 +08:00
|
|
|
described_class.new.execute(
|
|
|
|
topic_timer_id: topic.public_topic_timer.id,
|
|
|
|
state: false
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
|
|
|
|
|
|
topic_timer = topic.public_topic_timer
|
|
|
|
|
|
|
|
expect(topic_timer.status_type).to eq(TopicTimer.types[:close])
|
2019-03-28 14:28:01 +08:00
|
|
|
expect(topic_timer.execute_at).to eq_time(5.hours.from_now)
|
2018-02-27 15:31:59 +08:00
|
|
|
end
|
|
|
|
end
|
2017-03-22 11:12:02 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when trying to close a topic that has been deleted' do
|
|
|
|
it 'should not do anything' do
|
|
|
|
topic.trash!
|
|
|
|
|
|
|
|
Topic.any_instance.expects(:update_status).never
|
|
|
|
|
|
|
|
described_class.new.execute(
|
2017-05-17 02:49:42 +08:00
|
|
|
topic_timer_id: topic.public_topic_timer.id,
|
2017-03-22 11:12:02 +08:00
|
|
|
state: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when user is not authorized to close topics' do
|
|
|
|
let(:topic) do
|
2017-07-24 14:56:08 +08:00
|
|
|
Fabricate(:topic_timer, execute_at: 2.hours.from_now).topic
|
2017-03-22 11:12:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not do anything' do
|
|
|
|
described_class.new.execute(
|
2017-05-17 02:49:42 +08:00
|
|
|
topic_timer_id: topic.public_topic_timer.id,
|
2017-03-22 11:12:02 +08:00
|
|
|
state: false
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(topic.reload.closed).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|