FIX: admins bypass some post validations. This allows them to edit legal docs even if those docs are longer than max post length, for example.

This commit is contained in:
Neil Lalonde 2014-07-28 16:40:06 -04:00
parent 81ff121fe8
commit e1be478ef4
4 changed files with 67 additions and 9 deletions

View file

@ -135,6 +135,7 @@ Discourse.Composer = Discourse.Model.extend({
@property titleLengthValid
**/
titleLengthValid: function() {
if (Discourse.User.currentProp('admin') && this.get('titleLength') > 0) return true;
if (this.get('titleLength') < this.get('minimumTitleLength')) return false;
return (this.get('titleLength') <= Discourse.SiteSettings.max_topic_title_length);
}.property('minimumTitleLength', 'titleLength'),

View file

@ -3,14 +3,16 @@ module Validators; end
class Validators::PostValidator < ActiveModel::Validator
def validate(record)
presence(record)
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)
unless 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
end
def presence(post)

View file

@ -86,4 +86,22 @@ describe Validators::PostValidator do
end
end
context "acting_user is an admin" do
before do
post.acting_user = Fabricate(:admin)
end
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
end

View file

@ -1,4 +1,12 @@
module("Discourse.Composer");
module("Discourse.Composer", {
setup: function() {
sinon.stub(Discourse.User, 'currentProp').withArgs('admin').returns(false);
},
teardown: function() {
Discourse.User.currentProp.restore();
}
});
test('replyLength', function() {
var replyLength = function(val, expectedLength) {
@ -231,3 +239,32 @@ test('open with a quote', function() {
equal(new_composer().get('originalText'), quote, "originalText is the quote" );
equal(new_composer().get('replyDirty'), false, "replyDirty is initally false with a quote" );
});
module("Discourse.Composer as admin", {
setup: function() {
sinon.stub(Discourse.User, 'currentProp').withArgs('admin').returns(true);
},
teardown: function() {
Discourse.User.currentProp.restore();
}
});
test("Title length for regular topics as admin", function() {
Discourse.SiteSettings.min_topic_title_length = 5;
Discourse.SiteSettings.max_topic_title_length = 10;
var composer = Discourse.Composer.create();
composer.set('title', 'asdf');
ok(composer.get('titleLengthValid'), "admins can use short titles");
composer.set('title', 'this is a long title');
ok(composer.get('titleLengthValid'), "admins can use long titles");
composer.set('title', 'just right');
ok(composer.get('titleLengthValid'), "in the range is okay");
composer.set('title', '');
ok(!composer.get('titleLengthValid'), "admins must set title to at least 1 character");
});