From 99c4252ba6c283a66fc9c1407314da22f0b70745 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Mon, 1 Feb 2016 21:07:49 +0530 Subject: [PATCH] FEATURE: Staff should be exempt from user mention limit --- lib/validators/post_validator.rb | 2 + .../validators/post_validator_spec.rb | 49 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/validators/post_validator.rb b/lib/validators/post_validator.rb index d7cd910ed87..206d0bfbc98 100644 --- a/lib/validators/post_validator.rb +++ b/lib/validators/post_validator.rb @@ -54,6 +54,8 @@ class Validators::PostValidator < ActiveModel::Validator # Ensure maximum amount of mentions in a post def max_mention_validator(post) + return if post.acting_user.try(:staff?) + if acting_user_is_trusted?(post) add_error_if_count_exceeded(post, :no_mentions_allowed, :too_many_mentions, post.raw_mentions.size, SiteSetting.max_mentions_per_post) else diff --git a/spec/components/validators/post_validator_spec.rb b/spec/components/validators/post_validator_spec.rb index f320a1705da..d7e8e68fc7e 100644 --- a/spec/components/validators/post_validator_spec.rb +++ b/spec/components/validators/post_validator_spec.rb @@ -40,6 +40,55 @@ describe Validators::PostValidator do end end + context "too_many_mentions" do + before do + SiteSetting.newuser_max_mentions_per_post = 2 + SiteSetting.max_mentions_per_post = 3 + end + + it "should be invalid when new user exceeds max mentions limit" do + post.acting_user = build(:newuser) + post.expects(:raw_mentions).returns(['jake', 'finn', 'jake_old']) + validator.max_mention_validator(post) + expect(post.errors.count).to be > 0 + end + + it "should be invalid when elder user exceeds max mentions limit" do + post.acting_user = build(:trust_level_4) + post.expects(:raw_mentions).returns(['jake', 'finn', 'jake_old', 'jake_new']) + validator.max_mention_validator(post) + expect(post.errors.count).to be > 0 + end + + it "should be valid when new user does not exceed max mentions limit" do + post.acting_user = build(:newuser) + post.expects(:raw_mentions).returns(['jake', 'finn']) + validator.max_mention_validator(post) + expect(post.errors.count).to be(0) + end + + it "should be valid when elder user does not exceed max mentions limit" do + post.acting_user = build(:trust_level_4) + post.expects(:raw_mentions).returns(['jake', 'finn', 'jake_old']) + validator.max_mention_validator(post) + expect(post.errors.count).to be(0) + end + + it "should be valid for moderator in all cases" do + post.acting_user = build(:moderator) + post.expects(:raw_mentions).never + validator.max_mention_validator(post) + expect(post.errors.count).to be(0) + end + + it "should be valid for admin in all cases" do + post.acting_user = build(:admin) + post.expects(:raw_mentions).never + validator.max_mention_validator(post) + expect(post.errors.count).to be(0) + end + end + context "invalid post" do it "should be invalid" do validator.validate(post)