From 034b8a7ecc406d83b0134940e81e83848b4b3279 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Fri, 29 Mar 2019 21:59:19 +0200 Subject: [PATCH] FIX: Let users delete topics. Follow-up to 31053f30dea876395774fc646c50ba3b638acd97. --- lib/guardian/topic_guardian.rb | 2 +- spec/components/guardian_spec.rb | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/guardian/topic_guardian.rb b/lib/guardian/topic_guardian.rb index e58b158955b..d001ab5909c 100644 --- a/lib/guardian/topic_guardian.rb +++ b/lib/guardian/topic_guardian.rb @@ -95,7 +95,7 @@ module TopicGuardian def can_delete_topic?(topic) !topic.trashed? && - (is_staff? || (topic.posts_count <= 1 && topic.created_at && topic.created_at > 24.hours.ago)) && + (is_staff? || (is_my_own?(topic) && topic.posts_count <= 1 && topic.created_at && topic.created_at > 24.hours.ago)) && !topic.is_category_topic? && !Discourse.static_doc_topic_ids.include?(topic.id) end diff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rb index 48d033402fa..9fa878dd2b1 100644 --- a/spec/components/guardian_spec.rb +++ b/spec/components/guardian_spec.rb @@ -1747,6 +1747,24 @@ describe Guardian do SiteSetting.tos_topic_id = tos_topic.id expect(Guardian.new(admin).can_delete?(tos_topic)).to be_falsey end + + it "returns true for own topics" do + topic.update_attribute(:posts_count, 1) + topic.update_attribute(:created_at, Time.zone.now) + expect(Guardian.new(topic.user).can_delete?(topic)).to be_truthy + end + + it "returns false if delete their own topics" do + topic.update_attribute(:posts_count, 2) + topic.update_attribute(:created_at, Time.zone.now) + expect(Guardian.new(topic.user).can_delete?(topic)).to be_falsey + end + + it "returns false if delete their own topics" do + topic.update_attribute(:posts_count, 1) + topic.update_attribute(:created_at, 48.hours.ago) + expect(Guardian.new(topic.user).can_delete?(topic)).to be_falsey + end end context 'a Post' do @@ -1774,13 +1792,8 @@ describe Guardian do expect(Guardian.new(Fabricate(:user)).can_delete?(post)).to be_falsey end - it "returns true when it's the OP" do - post.update!(post_number: 1) - expect(Guardian.new(moderator).can_delete?(post)).to be_falsey - end - it "returns false when it's the OP, even as a moderator if there are at least two posts" do - post.update!(post_number: 1) + post.update_attribute(:post_number, 1) Fabricate(:post, topic: post.topic) expect(Guardian.new(moderator).can_delete?(post)).to be_falsey end