From 3cc761ac4490ee42c1c3c111ff801e8da7bb7d3c Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 26 Aug 2020 12:59:05 +0800 Subject: [PATCH] FIX: Clean up toggle closed topic timer when user is not authorized. --- app/jobs/regular/toggle_topic_closed.rb | 7 ++++ spec/jobs/toggle_topic_closed_spec.rb | 48 +++++++++++++++++++++---- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/app/jobs/regular/toggle_topic_closed.rb b/app/jobs/regular/toggle_topic_closed.rb index 7e459bc8c6d..d636360e943 100644 --- a/app/jobs/regular/toggle_topic_closed.rb +++ b/app/jobs/regular/toggle_topic_closed.rb @@ -29,6 +29,13 @@ module Jobs end topic.inherit_auto_close_from_category if state == false + else + topic_timer.destroy! + topic.reload + + if topic_timer.based_on_last_post + topic.inherit_auto_close_from_category + end end end end diff --git a/spec/jobs/toggle_topic_closed_spec.rb b/spec/jobs/toggle_topic_closed_spec.rb index 5cbd3e25105..4ae5122ed01 100644 --- a/spec/jobs/toggle_topic_closed_spec.rb +++ b/spec/jobs/toggle_topic_closed_spec.rb @@ -45,7 +45,13 @@ describe Jobs::ToggleTopicClosed do end describe 'when category has auto close configured' do - fab!(:category) { Fabricate(:category, auto_close_hours: 5) } + fab!(:category) do + Fabricate(:category, + auto_close_based_on_last_post: true, + auto_close_hours: 5 + ) + end + fab!(:topic) { Fabricate(:topic, category: category, closed: true) } it "should restore the category's auto close timer" do @@ -102,17 +108,45 @@ describe Jobs::ToggleTopicClosed do end end - describe 'when user is not authorized to close topics' do + describe 'when user is no longer authorized to close topics' do + fab!(:user) { Fabricate(:user) } + fab!(:topic) do - Fabricate(:topic_timer, execute_at: 2.hours.from_now).topic + Fabricate(:topic_timer, user: user).topic end - it 'should not do anything' do - described_class.new.execute( - topic_timer_id: topic.public_topic_timer.id, - state: false + it 'should destroy the topic timer' do + freeze_time(topic.public_topic_timer.execute_at + 1.minute) + + 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) + + expect(topic.reload.closed).to eq(false) + end + + it "should reconfigure topic timer if category's topics are set to autoclose" do + category = Fabricate(:category, + auto_close_based_on_last_post: true, + auto_close_hours: 5 ) + topic = Fabricate(:topic, category: category) + topic.public_topic_timer.update!(user: user) + + freeze_time(topic.public_topic_timer.execute_at + 1.minute) + + expect do + described_class.new.execute( + topic_timer_id: topic.public_topic_timer.id, + state: true + ) + end.to change { topic.reload.public_topic_timer.user }.from(user).to(Discourse.system_user) + .and change { topic.public_topic_timer.id } + expect(topic.reload.closed).to eq(false) end end