From 143bfdfb5b03e615e6f995be61e96394b9d30b49 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 3 Mar 2020 03:54:19 -0600 Subject: [PATCH] forum: fix mods not being able to lock forum topics. --- app/models/forum_post.rb | 7 +------ test/functional/forum_topics_controller_test.rb | 9 +++++++++ test/unit/forum_post_test.rb | 15 +++++++-------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index cc2782240..a23b4189c 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -18,7 +18,6 @@ class ForumPost < ApplicationRecord validates_presence_of :body validate :validate_topic_is_unlocked validate :topic_is_not_restricted, :on => :create - before_destroy :validate_topic_is_unlocked after_save :delete_topic_if_original_post after_update(:if => ->(rec) {rec.updater_id != rec.creator_id}) do |rec| ModAction.log("#{CurrentUser.name} updated forum ##{rec.id}", :forum_post_update) @@ -103,12 +102,8 @@ class ForumPost < ApplicationRecord end def validate_topic_is_unlocked - return if creator.is_moderator? - return if topic.nil? - - if topic.is_locked? + if topic.is_locked? && !updater.is_moderator? errors[:topic] << "is locked" - throw :abort end end diff --git a/test/functional/forum_topics_controller_test.rb b/test/functional/forum_topics_controller_test.rb index f31a09d40..e2393ae0b 100644 --- a/test/functional/forum_topics_controller_test.rb +++ b/test/functional/forum_topics_controller_test.rb @@ -211,6 +211,15 @@ class ForumTopicsControllerTest < ActionDispatch::IntegrationTest end end + context "update action" do + should "allow mods to lock forum topics" do + put_auth forum_topic_path(@forum_topic), @mod, params: { forum_topic: { is_locked: true }} + + assert_redirected_to forum_topic_path(@forum_topic) + assert_equal(true, @forum_topic.reload.is_locked) + end + end + context "destroy action" do setup do as_user do diff --git a/test/unit/forum_post_test.rb b/test/unit/forum_post_test.rb index 38e0119db..15b048253 100644 --- a/test/unit/forum_post_test.rb +++ b/test/unit/forum_post_test.rb @@ -104,21 +104,20 @@ class ForumPostTest < ActiveSupport::TestCase context "belonging to a locked topic" do setup do - @post = FactoryBot.create(:forum_post, :topic_id => @topic.id, :body => "zzz") - @topic.update_attribute(:is_locked, true) - @post.reload + @post = create(:forum_post, topic: @topic, body: "zzz") + @topic.update(is_locked: true) end should "not be updateable" do @post.update(body: "xxx") - @post.reload - assert_equal("zzz", @post.body) + assert_equal(true, @post.invalid?) + assert_equal("zzz", @post.reload.body) end should "not be deletable" do - assert_difference("ForumPost.count", 0) do - @post.destroy - end + @post.delete! + assert_equal(true, @post.invalid?) + assert_equal(false, @post.reload.is_deleted) end end