2013-06-13 04:18:17 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
require_dependency 'validators/post_validator'
|
|
|
|
|
|
|
|
describe Validators::PostValidator do
|
|
|
|
let :post do
|
|
|
|
build(:post)
|
|
|
|
end
|
|
|
|
|
|
|
|
let :validator do
|
|
|
|
Validators::PostValidator.new({})
|
|
|
|
end
|
|
|
|
|
|
|
|
context "stripped_length" do
|
|
|
|
it "adds an error for short raw" do
|
|
|
|
post.raw = "abc"
|
|
|
|
validator.stripped_length(post)
|
|
|
|
expect(post.errors.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds no error for long raw" do
|
|
|
|
post.raw = "this is a long topic body testing 123"
|
|
|
|
validator.stripped_length(post)
|
|
|
|
expect(post.errors.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-19 13:45:55 -05:00
|
|
|
context "too_many_posts" do
|
|
|
|
it "should be invalid when the user has posted too much" do
|
|
|
|
post.user.expects(:posted_too_much_in_topic?).returns(true)
|
|
|
|
validator.max_posts_validator(post)
|
|
|
|
expect(post.errors.count).to be > 0
|
|
|
|
end
|
|
|
|
|
2013-12-20 11:29:44 -05:00
|
|
|
it "should be allowed to edit when the user has posted too much" do
|
|
|
|
post.user.stubs(:posted_too_much_in_topic?).returns(true)
|
|
|
|
post.expects(:new_record?).returns(false)
|
|
|
|
validator.max_posts_validator(post)
|
|
|
|
expect(post.errors.count).to be(0)
|
|
|
|
end
|
|
|
|
|
2013-12-19 13:45:55 -05:00
|
|
|
it "should be valid when the user hasn't posted too much" do
|
|
|
|
post.user.expects(:posted_too_much_in_topic?).returns(false)
|
|
|
|
validator.max_posts_validator(post)
|
|
|
|
expect(post.errors.count).to be(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-13 04:18:17 -04:00
|
|
|
context "invalid post" do
|
|
|
|
it "should be invalid" do
|
|
|
|
validator.validate(post)
|
|
|
|
expect(post.errors.count).to be > 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-06 11:50:05 -04:00
|
|
|
describe "unique_post_validator" do
|
|
|
|
before do
|
|
|
|
SiteSetting.stubs(:unique_posts_mins).returns(5)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "post is unique" do
|
|
|
|
before do
|
2013-09-09 16:17:31 -04:00
|
|
|
post.stubs(:matches_recent_post?).returns(false)
|
2013-09-06 11:50:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not add an error" do
|
|
|
|
validator.unique_post_validator(post)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.errors.count).to eq(0)
|
2013-09-06 11:50:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "post is not unique" do
|
|
|
|
before do
|
2013-09-09 16:17:31 -04:00
|
|
|
post.stubs(:matches_recent_post?).returns(true)
|
2013-09-06 11:50:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should add an error" do
|
|
|
|
validator.unique_post_validator(post)
|
|
|
|
expect(post.errors.count).to be > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not add an error if post.skip_unique_check is true" do
|
|
|
|
post.skip_unique_check = true
|
|
|
|
validator.unique_post_validator(post)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(post.errors.count).to eq(0)
|
2013-09-06 11:50:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-01 14:53:35 -04:00
|
|
|
context "post is for a static page and acting_user is an admin" do
|
2014-07-28 16:40:06 -04:00
|
|
|
before do
|
2014-08-01 14:53:35 -04:00
|
|
|
@tos_post = build(:post)
|
|
|
|
@tos_post.acting_user = Fabricate(:admin)
|
|
|
|
SiteSetting.stubs(:tos_topic_id).returns(@tos_post.topic_id)
|
2014-07-28 16:40:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "skips most validations" do
|
2014-08-01 14:53:35 -04:00
|
|
|
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)
|
2014-07-28 16:40:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-13 04:18:17 -04:00
|
|
|
end
|