mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 06:49:14 +08:00
DEV: Improve specs for validating a unique post.
Follow up to d2a7f29595
which caused post
edits to fail.
This commit is contained in:
parent
d2a7f29595
commit
1bc96177dd
|
@ -2,7 +2,8 @@ require 'rails_helper'
|
|||
require_dependency 'validators/post_validator'
|
||||
|
||||
describe Validators::PostValidator do
|
||||
let(:post) { build(:post, topic: Fabricate(:topic)) }
|
||||
let(:topic) { Fabricate(:topic) }
|
||||
let(:post) { build(:post, topic: topic) }
|
||||
let(:validator) { Validators::PostValidator.new({}) }
|
||||
|
||||
context "#post_body_validator" do
|
||||
|
@ -186,35 +187,51 @@ describe Validators::PostValidator do
|
|||
end
|
||||
|
||||
describe "unique_post_validator" do
|
||||
let(:user) { Fabricate(:user, id: 999) }
|
||||
let(:post) { Fabricate(:post, user: user, topic: topic) }
|
||||
|
||||
before do
|
||||
SiteSetting.unique_posts_mins = 5
|
||||
post.store_unique_post_key
|
||||
@key = post.unique_post_key
|
||||
end
|
||||
|
||||
after do
|
||||
$redis.del(@key)
|
||||
end
|
||||
|
||||
context "post is unique" do
|
||||
before do
|
||||
post.stubs(:matches_recent_post?).returns(false)
|
||||
let(:new_post) do
|
||||
Fabricate.build(:post, user: user, raw: "unique content", topic: topic)
|
||||
end
|
||||
|
||||
it "should not add an error" do
|
||||
validator.unique_post_validator(new_post)
|
||||
expect(new_post.errors.count).to eq(0)
|
||||
end
|
||||
|
||||
it 'should not add an error when changing an existing post' do
|
||||
post.raw = "changing raw"
|
||||
|
||||
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)
|
||||
let(:new_post) do
|
||||
Fabricate.build(:post, user: user, raw: post.raw, topic: topic)
|
||||
end
|
||||
|
||||
it "should add an error" do
|
||||
validator.unique_post_validator(post)
|
||||
expect(post.errors.count).to be > 0
|
||||
validator.unique_post_validator(new_post)
|
||||
expect(new_post.errors.keys).to contain_exactly(:raw)
|
||||
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)
|
||||
new_post.skip_unique_check = true
|
||||
validator.unique_post_validator(new_post)
|
||||
expect(new_post.errors.count).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user