Skip to content

A platform for community discussion. Free, open, simple.

DEV: Fix up TopicGuardian#can_delete_topic? (#33965)

Ted Johanssond33bf177f406
diff --git a/lib/guardian/topic_guardian.rb b/lib/guardian/topic_guardian.rb
index 57a59fa5133..ef1adfcb9f9 100644
--- a/lib/guardian/topic_guardian.rb
+++ b/lib/guardian/topic_guardian.rb
@@ -156,14 +156,13 @@ module TopicGuardian
end
def can_delete_topic?(topic)
- !topic.trashed? &&
- (
- (
- is_my_own?(topic) && topic.posts_count <= 1 && topic.created_at &&
- topic.created_at > 24.hours.ago
- ) || is_category_group_moderator?(topic.category) ||
- user&.in_any_groups?(SiteSetting.delete_all_posts_and_topics_allowed_groups_map)
- ) && !topic.is_category_topic? && !Discourse.static_doc_topic_ids.include?(topic.id)
+ return false if topic.trashed?
+ return false if topic.is_category_topic?
+ return false if Discourse.static_doc_topic_ids.include?(topic.id)
+ return true if is_category_group_moderator?(topic.category)
+ return true if user&.in_any_groups?(SiteSetting.delete_all_posts_and_topics_allowed_groups_map)
+
+ is_my_own?(topic) && can_delete_own_topic?(topic)
end
def can_permanently_delete_topic?(topic)
@@ -367,6 +366,10 @@ module TopicGuardian
private
+ def can_delete_own_topic?(topic)
+ topic.posts_count <= 1 && topic.created_at? && topic.created_at > 24.hours.ago
+ end
+
def private_message_topic_scope(scope)
pm_scope = scope.private_messages_for_user(user)
diff --git a/spec/lib/guardian/topic_guardian_spec.rb b/spec/lib/guardian/topic_guardian_spec.rb
index 5e3e22fcfe9..28b6fd4b5f9 100644
--- a/spec/lib/guardian/topic_guardian_spec.rb
+++ b/spec/lib/guardian/topic_guardian_spec.rb
@@ -173,6 +173,82 @@ RSpec.describe TopicGuardian do
end
end
+ describe "#can_delete_topic?" do
+ it "returns false for an unauthenticated user" do
+ expect(Guardian.new.can_delete_topic?(topic)).to be_falsey
+ end
+
+ it "returns false for a regular user" do
+ expect(Guardian.new(Fabricate(:user)).can_delete_topic?(topic)).to be_falsey
+ end
+
+ it "returns true for a moderator" do
+ expect(Guardian.new(moderator).can_delete_topic?(topic)).to be_truthy
+ end
+
+ it "returns true for an admin" do
+ expect(Guardian.new(admin).can_delete_topic?(topic)).to be_truthy
+ end
+
+ it "returns false for static doc topics" do
+ tos_topic = Fabricate(:topic, user: Discourse.system_user)
+ SiteSetting.tos_topic_id = tos_topic.id
+ expect(Guardian.new(admin).can_delete_topic?(tos_topic)).to be_falsey
+ end
+
+ it "returns false when topic is trashed" do
+ topic.stubs(:trashed?).returns(true)
+ expect(Guardian.new(admin).can_delete_topic?(topic)).to be_falsey
+ end
+
+ it "returns false when topic is category topic" do
+ topic.stubs(:is_category_topic?).returns(true)
+ expect(Guardian.new(admin).can_delete_topic?(topic)).to be_falsey
+ end
+
+ it "returns true for own topic with no replies" do
+ topic.update_attribute(:posts_count, 1)
+ topic.update_attribute(:created_at, Time.zone.now)
+ expect(Guardian.new(topic.user).can_delete_topic?(topic)).to be_truthy
+ end
+
+ it "returns false for own topic with replies" do
+ topic.update!(posts_count: 2, created_at: Time.zone.now)
+ expect(Guardian.new(topic.user).can_delete_topic?(topic)).to be_falsey
+ end
+
+ it "returns false for own topic within cooldown period" do
+ topic.update!(posts_count: 1, created_at: 48.hours.ago)
+ expect(Guardian.new(topic.user).can_delete_topic?(topic)).to be_falsey
+ end
+
+ it "returns true for user in allowed groups" do
+ SiteSetting.delete_all_posts_and_topics_allowed_groups = Group::AUTO_GROUPS[:trust_level_4]
+ expect(Guardian.new(tl4_user).can_delete_topic?(topic)).to be_truthy
+ end
+
+ it "returns false for user not in allowed groups" do
+ SiteSetting.delete_all_posts_and_topics_allowed_groups = Group::AUTO_GROUPS[:trust_level_4]
+ expect(Guardian.new(tl3_user).can_delete_topic?(topic)).to be_falsey
+ end
+
+ context "when category group moderation is enabled" do
+ fab!(:group_user)
+
+ before { SiteSetting.enable_category_group_moderation = true }
+
+ it "returns false if user is not a member of the appropriate group" do
+ expect(Guardian.new(group_user.user).can_delete_topic?(topic)).to be_falsey
+ end
+
+ it "returns true if user is a member of the appropriate group" do
+ Fabricate(:category_moderation_group, category: topic.category, group: group_user.group)
+
+ expect(Guardian.new(group_user.user).can_delete_topic?(topic)).to be_truthy
+ end
+ end
+ end
+
describe "#is_in_edit_topic_groups?" do
it "returns true if the user is in edit_all_topic_groups" do
group.add(user)
diff --git a/spec/lib/guardian_spec.rb b/spec/lib/guardian_spec.rb
index deb36d945fd..88deb8426a6 100644
--- a/spec/lib/guardian_spec.rb
+++ b/spec/lib/guardian_spec.rb
@@ -1509,68 +1509,6 @@ RSpec.describe Guardian do
expect(Guardian.new(user).can_delete?(nil)).to be_falsey
end
- context "with a Topic" do
- it "returns false when not logged in" do
- expect(Guardian.new.can_delete?(topic)).to be_falsey
- end
-
- it "returns false when not a moderator" do
- expect(Guardian.new(Fabricate(:user)).can_delete?(topic)).to be_falsey
- end
-
- it "returns true when a moderator" do
- expect(Guardian.new(moderator).can_delete?(topic)).to be_truthy
- end
-
- it "returns true when an admin" do
- expect(Guardian.new(admin).can_delete?(topic)).to be_truthy
- end
-
- it "returns false for static doc topics" do
- tos_topic = Fabricate(:topic, user: Discourse.system_user)
- 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 topic has replies" do
- topic.update!(posts_count: 2, created_at: Time.zone.now)
- expect(Guardian.new(topic.user).can_delete?(topic)).to be_falsey
- end
-
- it "returns true when tl4 can delete posts and topics" do
- expect(Guardian.new(trust_level_4).can_delete?(topic)).to be_falsey
- SiteSetting.delete_all_posts_and_topics_allowed_groups = Group::AUTO_GROUPS[:trust_level_4]
- expect(Guardian.new(trust_level_4).can_delete?(topic)).to be_truthy
- end
-
- it "returns false if topic was created > 24h ago" do
- topic.update!(posts_count: 1, created_at: 48.hours.ago)
- expect(Guardian.new(topic.user).can_delete?(topic)).to be_falsey
- end
-
- context "when category group moderation is enabled" do
- fab!(:group_user)
-
- before { SiteSetting.enable_category_group_moderation = true }
-
- it "returns false if user is not a member of the appropriate group" do
- expect(Guardian.new(group_user.user).can_delete?(topic)).to be_falsey
- end
-
- it "returns true if user is a member of the appropriate group" do
- Fabricate(:category_moderation_group, category: topic.category, group: group_user.group)
-
- expect(Guardian.new(group_user.user).can_delete?(topic)).to be_truthy
- end
- end
- end
-
context "with a Post" do
before { post.post_number = 2 }