FIX: Clean up toggle closed topic timer when user is not authorized.

This commit is contained in:
Guo Xiang Tan 2020-08-26 12:59:05 +08:00
parent 83314d1534
commit 3cc761ac44
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
2 changed files with 48 additions and 7 deletions

View File

@ -29,6 +29,13 @@ module Jobs
end end
topic.inherit_auto_close_from_category if state == false 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 end
end end

View File

@ -45,7 +45,13 @@ describe Jobs::ToggleTopicClosed do
end end
describe 'when category has auto close configured' do 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) } fab!(:topic) { Fabricate(:topic, category: category, closed: true) }
it "should restore the category's auto close timer" do it "should restore the category's auto close timer" do
@ -102,17 +108,45 @@ describe Jobs::ToggleTopicClosed do
end end
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 fab!(:topic) do
Fabricate(:topic_timer, execute_at: 2.hours.from_now).topic Fabricate(:topic_timer, user: user).topic
end end
it 'should not do anything' do it 'should destroy the topic timer' do
described_class.new.execute( freeze_time(topic.public_topic_timer.execute_at + 1.minute)
topic_timer_id: topic.public_topic_timer.id,
state: false 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) expect(topic.reload.closed).to eq(false)
end end
end end