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

View File

@ -154,9 +154,22 @@ describe Guardian do
expect(Guardian.new(user).can_send_private_message?(user)).to be_truthy
end
it "returns false when you are untrusted" do
user.trust_level = TrustLevel[0]
expect(Guardian.new(user).can_send_private_message?(another_user)).to be_falsey
context "when user is untrusted " do
before do
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
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
@ -180,6 +193,10 @@ describe Guardian do
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
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
context "target user is suspended" do

View File

@ -3,11 +3,12 @@ require 'rails_helper'
describe TopicCreator do
let(:user) { Fabricate(:user, trust_level: TrustLevel[2]) }
let(:user2) { Fabricate(:user, trust_level: TrustLevel[2]) }
let(:moderator) { Fabricate(:moderator) }
let(:admin) { Fabricate(:admin) }
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
{

View File

@ -42,62 +42,88 @@ describe PostAction do
expect { PostAction.act(admin, post, PostActionType.types[:notify_user], message: "WAT") }.not_to raise_error
end
it "notify moderators integration test" do
post = create_post
mod = moderator
Group.refresh_automatic_groups!
context "notify moderators integration test" do
let!(:mod) { moderator }
action = PostAction.act(codinghorror, post, PostActionType.types[:notify_moderators], message: "this is my special long message")
before { Group.refresh_automatic_groups! }
posts = Post.joins(:topic)
.select('posts.id, topics.subtype, posts.topic_id')
.where('topics.archetype' => Archetype.private_message)
.to_a
it "flag from TL1 user" do
post = create_post
expect(posts.count).to eq(1)
expect(action.related_post_id).to eq(posts[0].id.to_i)
expect(posts[0].subtype).to eq(TopicSubtype.notify_moderators)
action = PostAction.act(codinghorror, post, PostActionType.types[:notify_moderators], message: "this is my special long message")
topic = posts[0].topic
posts = Post.joins(:topic)
.select('posts.id, topics.subtype, posts.topic_id')
.where('topics.archetype' => Archetype.private_message)
.to_a
# Moderators should be invited to the private topic, otherwise they're not permitted to see it
topic_user_ids = topic.reload.topic_users.map { |x| x.user_id }
expect(topic_user_ids).to include(codinghorror.id)
expect(topic_user_ids).to include(mod.id)
expect(posts.count).to eq(1)
expect(action.related_post_id).to eq(posts[0].id.to_i)
expect(posts[0].subtype).to eq(TopicSubtype.notify_moderators)
expect(topic.topic_users.where(user_id: mod.id)
.pluck(:notification_level).first).to eq(TopicUser.notification_levels[:tracking])
topic = posts[0].topic
expect(topic.topic_users.where(user_id: codinghorror.id)
.pluck(:notification_level).first).to eq(TopicUser.notification_levels[:watching])
# Moderators should be invited to the private topic, otherwise they're not permitted to see it
topic_user_ids = topic.reload.topic_users.map { |x| x.user_id }
expect(topic_user_ids).to include(codinghorror.id)
expect(topic_user_ids).to include(mod.id)
# reply to PM should not clear flag
PostCreator.new(mod, topic_id: posts[0].topic_id, raw: "This is my test reply to the user, it should clear flags").create
action.reload
expect(action.deleted_at).to eq(nil)
expect(topic.topic_users.where(user_id: mod.id)
.pluck(:notification_level).first).to eq(TopicUser.notification_levels[:tracking])
# Acting on the flag should not post an automated status message (since a moderator already replied)
expect(topic.posts.count).to eq(2)
PostAction.agree_flags!(post, admin)
topic.reload
expect(topic.posts.count).to eq(2)
expect(topic.topic_users.where(user_id: codinghorror.id)
.pluck(:notification_level).first).to eq(TopicUser.notification_levels[:watching])
# Clearing the flags should not post an automated status message
PostAction.act(mod, post, PostActionType.types[:notify_moderators], message: "another special message")
PostAction.clear_flags!(post, admin)
topic.reload
expect(topic.posts.count).to eq(2)
# reply to PM should not clear flag
PostCreator.new(mod, topic_id: posts[0].topic_id, raw: "This is my test reply to the user, it should clear flags").create
action.reload
expect(action.deleted_at).to eq(nil)
# Acting on the flag should post an automated status message
another_post = create_post
action = PostAction.act(codinghorror, another_post, PostActionType.types[:notify_moderators], message: "foobar")
topic = action.related_post.topic
# Acting on the flag should not post an automated status message (since a moderator already replied)
expect(topic.posts.count).to eq(2)
PostAction.agree_flags!(post, admin)
topic.reload
expect(topic.posts.count).to eq(2)
expect(topic.posts.count).to eq(1)
PostAction.agree_flags!(another_post, admin)
topic.reload
expect(topic.posts.count).to eq(2)
expect(topic.posts.last.post_type).to eq(Post.types[:moderator_action])
# Clearing the flags should not post an automated status message
PostAction.act(mod, post, PostActionType.types[:notify_moderators], message: "another special message")
PostAction.clear_flags!(post, admin)
topic.reload
expect(topic.posts.count).to eq(2)
# Acting on the flag should post an automated status message
another_post = create_post
action = PostAction.act(codinghorror, another_post, PostActionType.types[:notify_moderators], message: "foobar")
topic = action.related_post.topic
expect(topic.posts.count).to eq(1)
PostAction.agree_flags!(another_post, admin)
topic.reload
expect(topic.posts.count).to eq(2)
expect(topic.posts.last.post_type).to eq(Post.types[:moderator_action])
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