FIX: TL0 users' messages to moderators were not being posted when flagging private messages

This commit is contained in:
Neil Lalonde
2017-10-13 11:55:49 -04:00
parent 6f923d5964
commit b124e5f19f
4 changed files with 96 additions and 50 deletions

View File

@ -289,8 +289,10 @@ class Guardian
(target.is_a?(Group) || target.is_a?(User)) && (target.is_a?(Group) || target.is_a?(User)) &&
# User is authenticated # User is authenticated
authenticated? && authenticated? &&
# Have to be a basic level at least # Have to be a basic level at least, or are contacting moderators
@user.has_trust_level?(SiteSetting.min_trust_to_send_messages) && (@user.has_trust_level?(SiteSetting.min_trust_to_send_messages) ||
(target.is_a?(User) && target.moderator?) ||
(target.name == Group[:moderators].name)) &&
# PMs are enabled # PMs are enabled
(is_staff? || SiteSetting.enable_private_messages) && (is_staff? || SiteSetting.enable_private_messages) &&
# Can't send PMs to suspended users # Can't send PMs to suspended users

View File

@ -154,11 +154,24 @@ describe Guardian do
expect(Guardian.new(user).can_send_private_message?(user)).to be_truthy expect(Guardian.new(user).can_send_private_message?(user)).to be_truthy
end end
it "returns false when you are untrusted" do context "when user is untrusted " do
before do
user.trust_level = TrustLevel[0] user.trust_level = TrustLevel[0]
end
it "returns false to another user" do
expect(Guardian.new(user).can_send_private_message?(another_user)).to be_falsey expect(Guardian.new(user).can_send_private_message?(another_user)).to be_falsey
end end
it "returns true to moderator user" do
expect(Guardian.new(user).can_send_private_message?(moderator)).to be_truthy
end
it "returns true to moderator group" do
expect(Guardian.new(user).can_send_private_message?(Group[:moderators])).to be_truthy
end
end
it "returns true to another user" do it "returns true to another user" do
expect(Guardian.new(user).can_send_private_message?(another_user)).to be_truthy expect(Guardian.new(user).can_send_private_message?(another_user)).to be_truthy
end end
@ -180,6 +193,10 @@ describe Guardian do
expect(Guardian.new(moderator).can_send_private_message?(another_user)).to be_truthy expect(Guardian.new(moderator).can_send_private_message?(another_user)).to be_truthy
expect(Guardian.new(admin).can_send_private_message?(another_user)).to be_truthy expect(Guardian.new(admin).can_send_private_message?(another_user)).to be_truthy
end end
it "returns false even to a moderator" do
expect(Guardian.new(trust_level_4).can_send_private_message?(moderator)).to be_falsey
end
end end
context "target user is suspended" do context "target user is suspended" do

View File

@ -3,11 +3,12 @@ require 'rails_helper'
describe TopicCreator do describe TopicCreator do
let(:user) { Fabricate(:user, trust_level: TrustLevel[2]) } let(:user) { Fabricate(:user, trust_level: TrustLevel[2]) }
let(:user2) { Fabricate(:user, trust_level: TrustLevel[2]) }
let(:moderator) { Fabricate(:moderator) } let(:moderator) { Fabricate(:moderator) }
let(:admin) { Fabricate(:admin) } let(:admin) { Fabricate(:admin) }
let(:valid_attrs) { Fabricate.attributes_for(:topic) } let(:valid_attrs) { Fabricate.attributes_for(:topic) }
let(:pm_valid_attrs) { { raw: 'this is a new post', title: 'this is a new title', archetype: Archetype.private_message, target_usernames: moderator.username } } let(:pm_valid_attrs) { { raw: 'this is a new post', title: 'this is a new title', archetype: Archetype.private_message, target_usernames: user2.username } }
let(:pm_to_email_valid_attrs) do let(:pm_to_email_valid_attrs) do
{ {

View File

@ -42,10 +42,13 @@ describe PostAction do
expect { PostAction.act(admin, post, PostActionType.types[:notify_user], message: "WAT") }.not_to raise_error expect { PostAction.act(admin, post, PostActionType.types[:notify_user], message: "WAT") }.not_to raise_error
end end
it "notify moderators integration test" do context "notify moderators integration test" do
let!(:mod) { moderator }
before { Group.refresh_automatic_groups! }
it "flag from TL1 user" do
post = create_post post = create_post
mod = moderator
Group.refresh_automatic_groups!
action = PostAction.act(codinghorror, post, PostActionType.types[:notify_moderators], message: "this is my special long message") action = PostAction.act(codinghorror, post, PostActionType.types[:notify_moderators], message: "this is my special long message")
@ -100,6 +103,29 @@ describe PostAction do
expect(topic.posts.last.post_type).to eq(Post.types[:moderator_action]) expect(topic.posts.last.post_type).to eq(Post.types[:moderator_action])
end end
it "flag from TL0 user" do
tl0_user = Fabricate(:user, trust_level: 0)
first_post = Fabricate(:post, user: Fabricate(:user, trust_level: 1))
pm_topic = Fabricate(:private_message_topic,
first_post: first_post,
topic_allowed_users: [
Fabricate.build(:topic_allowed_user, user: first_post.user),
Fabricate.build(:topic_allowed_user, user: tl0_user),
]
)
action = PostAction.act(tl0_user, first_post, PostActionType.types[:notify_moderators], message: 'my special message')
expect(action.related_post_id).to be_present
notify_moderators_post = action.related_post
expect(notify_moderators_post.topic&.subtype).to eq(TopicSubtype.notify_moderators)
expect(notify_moderators_post.raw).to include('my special message')
end
end
describe 'notify_moderators' do describe 'notify_moderators' do
before do before do
PostAction.stubs(:create) PostAction.stubs(:create)