skip most post validations for staged accounts

This commit is contained in:
Régis Hanol 2015-12-01 10:40:23 +01:00
parent 46d2e17194
commit 5b9594277a
2 changed files with 35 additions and 47 deletions

View File

@ -1,19 +1,23 @@
require_dependency 'validators/stripped_length_validator'
module Validators; end
class Validators::PostValidator < ActiveModel::Validator
def validate(record)
presence(record)
unless Discourse.static_doc_topic_ids.include?(record.topic_id) && record.acting_user.try(:admin?)
stripped_length(record)
raw_quality(record)
max_posts_validator(record)
max_mention_validator(record)
max_images_validator(record)
max_attachments_validator(record)
max_links_validator(record)
unique_post_validator(record)
end
return if record.acting_user.try(:staged?)
return if Discourse.static_doc_topic_ids.include?(record.topic_id) && record.acting_user.try(:admin?)
stripped_length(record)
raw_quality(record)
max_posts_validator(record)
max_mention_validator(record)
max_images_validator(record)
max_attachments_validator(record)
max_links_validator(record)
unique_post_validator(record)
end
def presence(post)
@ -95,7 +99,7 @@ class Validators::PostValidator < ActiveModel::Validator
private
def acting_user_is_trusted?(post)
post.acting_user.present? && (post.acting_user.has_trust_level?(TrustLevel[1]) || post.acting_user.staged?)
post.acting_user.present? && post.acting_user.has_trust_level?(TrustLevel[1])
end
def add_error_if_count_exceeded(post, not_allowed_translation_key, limit_translation_key, current_count, max_count)

View File

@ -81,48 +81,32 @@ describe Validators::PostValidator do
end
end
context "post is for a static page and acting_user is an admin" do
shared_examples "almost no validations" do
it "skips most validations" do
validator.expects(:stripped_length).never
validator.expects(:raw_quality).never
validator.expects(:max_posts_validator).never
validator.expects(:max_mention_validator).never
validator.expects(:max_images_validator).never
validator.expects(:max_attachments_validator).never
validator.expects(:max_links_validator).never
validator.expects(:unique_post_validator).never
validator.validate(post)
end
end
context "admin editing a static page" do
before do
@tos_post = build(:post)
@tos_post.acting_user = Fabricate(:admin)
SiteSetting.stubs(:tos_topic_id).returns(@tos_post.topic_id)
post.acting_user = build(:admin)
SiteSetting.stubs(:tos_topic_id).returns(post.topic_id)
end
it "skips most validations" do
v = Validators::PostValidator.new({})
v.expects(:stripped_length).never
v.expects(:raw_quality).never
v.expects(:max_posts_validator).never
v.expects(:max_mention_validator).never
v.expects(:max_images_validator).never
v.expects(:max_attachments_validator).never
v.expects(:max_links_validator).never
v.expects(:unique_post_validator).never
v.validate(@tos_post)
end
include_examples "almost no validations"
end
context "staged user" do
it "trust staged users" do
post.acting_user = build(:user, staged: true)
post.expects(:raw_mentions).returns(Array.new(SiteSetting.newuser_max_mentions_per_post + 1))
validator.max_mention_validator(post)
expect(post.errors.count).to eq(0)
post.expects(:image_count).never
validator.max_images_validator(post)
expect(post.errors.count).to eq(0)
post.expects(:attachment_count).never
validator.max_attachments_validator(post)
expect(post.errors.count).to eq(0)
post.expects(:link_count).never
validator.max_links_validator(post)
expect(post.errors.count).to eq(0)
end
before { post.acting_user = build(:user, staged: true) }
include_examples "almost no validations"
end
end