FIX: Abort CensoredWordsValidator early if censored_words_regexp nil (#8375)

* Abort CensoredWordsValidator early if censored_words_regexp nil. Sometimes censored_words_regex can end up nil, erroring the validator. This handles the nil condition and also adds a spec for the validator
This commit is contained in:
Martin Brennan
2019-11-20 20:05:06 +10:00
committed by GitHub
parent 7543db086a
commit d45bd7f131
2 changed files with 56 additions and 2 deletions

View File

@@ -0,0 +1,50 @@
# frozen_string_literal: true
require 'rails_helper'
describe CensoredWordsValidator do
let(:value) { 'some new bad text' }
let(:record) { Fabricate(:post, raw: 'this is a test') }
let(:attribute) { :raw }
describe "#validate_each" do
context "when there are censored words for action" do
let!(:watched_word) { Fabricate(:watched_word, action: WatchedWord.actions[:censor], word: 'bad') }
context "when there is a nil word_matcher_regexp" do
before do
WordWatcher.stubs(:word_matcher_regexp).returns(nil)
end
it "adds no errors to the record" do
validate
expect(record.errors.empty?).to eq(true)
end
end
context "when there is word_matcher_regexp" do
context "when the new value does not contain the watched word" do
let(:value) { 'some new good text' }
it "adds no errors to the record" do
validate
expect(record.errors.empty?).to eq(true)
end
end
context "when the new value does contain the watched word" do
let(:value) { 'some new bad text' }
it "adds errors to the record" do
validate
expect(record.errors.empty?).to eq(false)
end
end
end
end
end
def validate
described_class.new(attributes: :test).validate_each(record, attribute, value)
end
end