FIX: don't let admins skip post validations, unless it's faq, tos, or privacy

This commit is contained in:
Neil Lalonde 2014-08-01 14:53:35 -04:00
parent 46a88e0c70
commit 443caaa8f7
5 changed files with 35 additions and 18 deletions

View File

@ -135,10 +135,10 @@ Discourse.Composer = Discourse.Model.extend({
@property titleLengthValid @property titleLengthValid
**/ **/
titleLengthValid: function() { titleLengthValid: function() {
if (Discourse.User.currentProp('admin') && this.get('titleLength') > 0) return true; if (Discourse.User.currentProp('admin') && this.get('post.static_doc') && this.get('titleLength') > 0) return true;
if (this.get('titleLength') < this.get('minimumTitleLength')) return false; if (this.get('titleLength') < this.get('minimumTitleLength')) return false;
return (this.get('titleLength') <= Discourse.SiteSettings.max_topic_title_length); return (this.get('titleLength') <= Discourse.SiteSettings.max_topic_title_length);
}.property('minimumTitleLength', 'titleLength'), }.property('minimumTitleLength', 'titleLength', 'post.static_doc'),
// The text for the save button // The text for the save button
saveText: function() { saveText: function() {

View File

@ -49,7 +49,8 @@ class PostSerializer < BasicPostSerializer
:edit_reason, :edit_reason,
:can_view_edit_history, :can_view_edit_history,
:wiki, :wiki,
:user_custom_fields :user_custom_fields,
:static_doc
def moderator? def moderator?
!!(object.user && object.user.moderator?) !!(object.user && object.user.moderator?)
@ -231,6 +232,14 @@ class PostSerializer < BasicPostSerializer
custom_fields && custom_fields[object.user_id] custom_fields && custom_fields[object.user_id]
end end
def static_doc
true
end
def include_static_doc?
object.post_number == 1 && Discourse.static_doc_topic_ids.include?(object.topic_id)
end
private private
def post_actions def post_actions

View File

@ -3,7 +3,7 @@ module Validators; end
class Validators::PostValidator < ActiveModel::Validator class Validators::PostValidator < ActiveModel::Validator
def validate(record) def validate(record)
presence(record) presence(record)
unless record.acting_user.try(:admin?) unless Discourse.static_doc_topic_ids.include?(record.topic_id) && record.acting_user.try(:admin?)
stripped_length(record) stripped_length(record)
raw_quality(record) raw_quality(record)
max_posts_validator(record) max_posts_validator(record)

View File

@ -86,21 +86,24 @@ describe Validators::PostValidator do
end end
end end
context "acting_user is an admin" do context "post is for a static page and acting_user is an admin" do
before do before do
post.acting_user = Fabricate(:admin) @tos_post = build(:post)
@tos_post.acting_user = Fabricate(:admin)
SiteSetting.stubs(:tos_topic_id).returns(@tos_post.topic_id)
end end
it "skips most validations" do it "skips most validations" do
validator.expects(:stripped_length).never v = Validators::PostValidator.new({})
validator.expects(:raw_quality).never v.expects(:stripped_length).never
validator.expects(:max_posts_validator).never v.expects(:raw_quality).never
validator.expects(:max_mention_validator).never v.expects(:max_posts_validator).never
validator.expects(:max_images_validator).never v.expects(:max_mention_validator).never
validator.expects(:max_attachments_validator).never v.expects(:max_images_validator).never
validator.expects(:max_links_validator).never v.expects(:max_attachments_validator).never
validator.expects(:unique_post_validator).never v.expects(:max_links_validator).never
validator.validate(post) v.expects(:unique_post_validator).never
v.validate(@tos_post)
end end
end end

View File

@ -243,19 +243,24 @@ test('open with a quote', function() {
module("Discourse.Composer as admin", { module("Discourse.Composer as admin", {
setup: function() { setup: function() {
Discourse.SiteSettings.min_topic_title_length = 5;
Discourse.SiteSettings.max_topic_title_length = 10;
sandbox.stub(Discourse.User, 'currentProp').withArgs('admin').returns(true); sandbox.stub(Discourse.User, 'currentProp').withArgs('admin').returns(true);
}, },
teardown: function() { teardown: function() {
Discourse.SiteSettings.min_topic_title_length = 15;
Discourse.SiteSettings.max_topic_title_length = 255;
Discourse.User.currentProp.restore(); Discourse.User.currentProp.restore();
} }
}); });
test("Title length for regular topics as admin", function() { test("Title length for static page topics as admin", function() {
Discourse.SiteSettings.min_topic_title_length = 5;
Discourse.SiteSettings.max_topic_title_length = 10;
var composer = Discourse.Composer.create(); var composer = Discourse.Composer.create();
var post = Discourse.Post.create({id: 123, post_number: 2, static_doc: true});
composer.setProperties({post: post, action: Discourse.Composer.EDIT });
composer.set('title', 'asdf'); composer.set('title', 'asdf');
ok(composer.get('titleLengthValid'), "admins can use short titles"); ok(composer.get('titleLengthValid'), "admins can use short titles");