mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-12-20 20:42:44 -05:00
3e50313fdc
Since rspec-rails 3, the default installation creates two helper files: * `spec_helper.rb` * `rails_helper.rb` `spec_helper.rb` is intended as a way of running specs that do not require Rails, whereas `rails_helper.rb` loads Rails (as Discourse's current `spec_helper.rb` does). For more information: https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files In this commit, I've simply replaced all instances of `spec_helper` with `rails_helper`, and renamed the original `spec_helper.rb`. This brings the Discourse project closer to the standard usage of RSpec in a Rails app. At present, every spec relies on loading Rails, but there are likely many that don't need to. In a future pull request, I hope to introduce a separate, minimal `spec_helper.rb` which can be used in tests which don't rely on Rails.
112 lines
3.2 KiB
Ruby
112 lines
3.2 KiB
Ruby
require 'rails_helper'
|
|
require_dependency 'validators/post_validator'
|
|
|
|
describe Validators::PostValidator do
|
|
let(:post) { build(:post) }
|
|
let(:validator) { Validators::PostValidator.new({}) }
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
context "invalid post" do
|
|
it "should be invalid" do
|
|
validator.validate(post)
|
|
expect(post.errors.count).to be > 0
|
|
end
|
|
end
|
|
|
|
describe "unique_post_validator" do
|
|
before do
|
|
SiteSetting.stubs(:unique_posts_mins).returns(5)
|
|
end
|
|
|
|
context "post is unique" do
|
|
before do
|
|
post.stubs(:matches_recent_post?).returns(false)
|
|
end
|
|
|
|
it "should not add an error" do
|
|
validator.unique_post_validator(post)
|
|
expect(post.errors.count).to eq(0)
|
|
end
|
|
end
|
|
|
|
context "post is not unique" do
|
|
before do
|
|
post.stubs(:matches_recent_post?).returns(true)
|
|
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)
|
|
expect(post.errors.count).to eq(0)
|
|
end
|
|
end
|
|
end
|
|
|
|
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
|
|
post.acting_user = build(:admin)
|
|
SiteSetting.stubs(:tos_topic_id).returns(post.topic_id)
|
|
end
|
|
|
|
include_examples "almost no validations"
|
|
end
|
|
|
|
context "staged user" do
|
|
before { post.acting_user = build(:user, staged: true) }
|
|
include_examples "almost no validations"
|
|
end
|
|
|
|
end
|