FIX: Guardian#post_can_act? shouldn't raise an error if user of post has been deleted.

This commit is contained in:
Guo Xiang Tan 2018-08-17 15:10:07 +08:00
parent 70155f0923
commit fae8757cd4
2 changed files with 20 additions and 10 deletions

View File

@ -43,7 +43,7 @@ module PostGuardian
# post made by staff, but we don't allow staff flags
return false if is_flag &&
(!SiteSetting.allow_flagging_staff?) &&
post.user.staff?
post&.user&.staff?
if [:notify_user, :notify_moderators].include?(action_key) &&
(!SiteSetting.enable_personal_messages? ||

View File

@ -120,16 +120,26 @@ describe Guardian do
expect(Guardian.new(user).post_can_act?(staff_post, :spam)).to be_truthy
end
it "doesn't allow flagging of staff posts when allow_flagging_staff is false" do
SiteSetting.allow_flagging_staff = false
staff_post = Fabricate(:post, user: Fabricate(:moderator))
expect(Guardian.new(user).post_can_act?(staff_post, :spam)).to eq(false)
end
describe 'when allow_flagging_staff is false' do
let(:staff_post) { Fabricate(:post, user: Fabricate(:moderator)) }
it "allows liking of staff when allow_flagging_staff is false" do
SiteSetting.allow_flagging_staff = false
staff_post = Fabricate(:post, user: Fabricate(:moderator))
expect(Guardian.new(user).post_can_act?(staff_post, :like)).to eq(true)
before do
SiteSetting.allow_flagging_staff = false
end
it "doesn't allow flagging of staff posts" do
expect(Guardian.new(user).post_can_act?(staff_post, :spam)).to eq(false)
end
it "allows flagging of staff posts when staff has been deleted" do
staff_post.user.destroy!
staff_post.reload
expect(Guardian.new(user).post_can_act?(staff_post, :spam)).to eq(true)
end
it "allows liking of staff" do
expect(Guardian.new(user).post_can_act?(staff_post, :like)).to eq(true)
end
end
it "returns false when liking yourself" do