discourse/spec/jobs/toggle_topic_closed_spec.rb
Guo Xiang Tan 5bca1aec48
FIX: Clean up topic_timers when no longer valid.
This was causing a bug where the jobs for the topic_timers will keep
getting enqueued over and over again.
2020-08-26 12:18:51 +08:00

120 lines
3.1 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
describe Jobs::ToggleTopicClosed do
fab!(:admin) { Fabricate(:admin) }
fab!(:topic) do
Fabricate(:topic_timer, user: admin).topic
end
it 'should be able to close a topic' do
topic
freeze_time(61.minutes.from_now) do
described_class.new.execute(
topic_timer_id: topic.public_topic_timer.id,
state: true
)
expect(topic.reload.closed).to eq(true)
expect(Post.last.raw).to eq(I18n.t(
'topic_statuses.autoclosed_enabled_minutes', count: 61
))
end
end
describe 'opening a topic' do
it 'should be work' do
topic.update!(closed: true)
freeze_time(61.minutes.from_now) do
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(
'topic_statuses.autoclosed_disabled_minutes', count: 61
))
end
end
describe 'when category has auto close configured' do
fab!(:category) { Fabricate(:category, auto_close_hours: 5) }
fab!(:topic) { Fabricate(:topic, category: category, closed: true) }
it "should restore the category's auto close timer" do
Fabricate(:topic_timer,
status_type: TopicTimer.types[:open],
topic: topic,
user: admin
)
freeze_time(61.minutes.from_now) do
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])
expect(topic_timer.execute_at).to eq_time(5.hours.from_now)
end
end
end
end
describe 'when trying to close a topic that has already been closed' do
it 'should delete the topic timer' do
freeze_time(topic.public_topic_timer.execute_at + 1.minute)
topic.update!(closed: true)
expect do
described_class.new.execute(
topic_timer_id: topic.public_topic_timer.id,
state: true
)
end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)
end
end
describe 'when trying to close a topic that has been deleted' do
it 'should delete the topic timer' do
freeze_time(topic.public_topic_timer.execute_at + 1.minute)
topic.trash!
expect do
described_class.new.execute(
topic_timer_id: topic.public_topic_timer.id,
state: true
)
end.to change { TopicTimer.exists?(topic_id: topic.id) }.from(true).to(false)
end
end
describe 'when user is not authorized to close topics' do
fab!(:topic) do
Fabricate(:topic_timer, execute_at: 2.hours.from_now).topic
end
it 'should not do anything' do
described_class.new.execute(
topic_timer_id: topic.public_topic_timer.id,
state: false
)
expect(topic.reload.closed).to eq(false)
end
end
end