diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rb index b29467ceede..ee72eaf465b 100644 --- a/lib/guardian/post_guardian.rb +++ b/lib/guardian/post_guardian.rb @@ -149,7 +149,11 @@ module PostGuardian # Recovery Method def can_recover_post?(post) - is_staff? || (is_my_own?(post) && post.user_deleted && !post.deleted_at) + if is_staff? + post.deleted_at && post.user + else + is_my_own?(post) && post.user_deleted && !post.deleted_at + end end def can_delete_post_action?(post_action) diff --git a/lib/guardian/topic_guardian.rb b/lib/guardian/topic_guardian.rb index ea086ef1997..add956351be 100644 --- a/lib/guardian/topic_guardian.rb +++ b/lib/guardian/topic_guardian.rb @@ -52,7 +52,7 @@ module TopicGuardian # Recovery Method def can_recover_topic?(topic) - is_staff? + topic && topic.deleted_at && topic.user && is_staff? end def can_delete_topic?(topic) diff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rb index a140c3c0744..702344884ae 100644 --- a/spec/components/guardian_spec.rb +++ b/spec/components/guardian_spec.rb @@ -880,8 +880,30 @@ describe Guardian do expect(Guardian.new(user).can_recover_topic?(topic)).to be_falsey end - it "returns true for a moderator" do - expect(Guardian.new(moderator).can_recover_topic?(topic)).to be_truthy + context 'as a moderator' do + before do + topic.save! + post.save! + end + + describe 'when post has been deleted' do + it "should return the right value" do + expect(Guardian.new(moderator).can_recover_topic?(topic)).to be_falsey + + PostDestroyer.new(moderator, topic.first_post).destroy + + expect(Guardian.new(moderator).can_recover_topic?(topic.reload)).to be_truthy + end + end + + describe "when post's user has been deleted" do + it 'should return the right value' do + PostDestroyer.new(moderator, topic.first_post).destroy + topic.first_post.user.destroy! + + expect(Guardian.new(moderator).can_recover_topic?(topic.reload)).to be_falsey + end + end end end @@ -899,8 +921,32 @@ describe Guardian do expect(Guardian.new(user).can_recover_post?(post)).to be_falsey end - it "returns true for a moderator" do - expect(Guardian.new(moderator).can_recover_post?(post)).to be_truthy + context 'as a moderator' do + let(:other_post) { Fabricate(:post, topic: topic, user: topic.user) } + + before do + topic.save! + post.save! + end + + describe 'when post has been deleted' do + it "should return the right value" do + expect(Guardian.new(moderator).can_recover_post?(post)).to be_falsey + + PostDestroyer.new(moderator, post).destroy + + expect(Guardian.new(moderator).can_recover_post?(post.reload)).to be_truthy + end + + describe "when post's user has been deleted" do + it 'should return the right value' do + PostDestroyer.new(moderator, post).destroy + post.user.destroy! + + expect(Guardian.new(moderator).can_recover_post?(post.reload)).to be_falsey + end + end + end end end