DEV: Improve specs for validating a unique post.

Follow up to d2a7f29595 which caused post
edits to fail.
This commit is contained in:
Guo Xiang Tan 2019-03-14 10:39:51 +08:00
parent d2a7f29595
commit 1bc96177dd

View File

@ -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