Sha256: 93072f97a4bd306c68333a28d0b661d4f23b5c98924beef9e96502a945475e90

Contents?: true

Size: 1.58 KB

Versions: 13

Compression:

Stored size: 1.58 KB

Contents

require "active_model"
require "calagator/blacklist_validator"

module Calagator

describe BlacklistValidator do
  let(:klass) do
    Class.new do
      include ActiveModel::Validations
      validates :title, blacklist: true
      attr_accessor :title
    end
  end

  subject { klass.new }

  describe "with default blacklist" do
    it "should be valid when clean" do
      subject.title = "Title"
      expect(subject).to be_valid
    end

    it "should not be valid when it features blacklisted word" do
      subject.title = "Foo bar cialis"
      expect(subject).not_to be_valid
    end
  end

  describe "with custom blacklist" do
    before do
      klass.validates :title, blacklist: { patterns: [/Kltpzyxm/i] }
    end

    it "should be valid when clean" do
      subject.title = "Title"
      expect(subject).to be_valid
    end

    it "should not be valid when it features custom blacklisted word" do
      subject.title = "fooKLTPZYXMbar"
      expect(subject).not_to be_valid
    end
  end

  describe "created with custom blacklist file" do
    let(:blacklist_file_path) { Rails.root.join("config/blacklist.txt") }

    before do
      allow(File).to receive(:exists?).with(blacklist_file_path).and_return(true)
      expect(File).to receive(:readlines).with(blacklist_file_path).and_return(["Kltpzyxm"])
    end

    it "should be valid when clean" do
      subject.title = "Title"
      expect(subject).to be_valid
    end

    it "should not be valid when it features custom blacklisted word" do
      subject.title = "fooKLTPZYXMbar"
      expect(subject).not_to be_valid
    end
  end
end

end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
calagator-1.0.0 spec/lib/calagator/blacklist_validator_spec.rb
grokus-1.0.0.9 spec/lib/calagator/blacklist_validator_spec.rb
grokus-1.0.0.8 spec/lib/calagator/blacklist_validator_spec.rb
grokus-1.0.0.7 spec/lib/calagator/blacklist_validator_spec.rb
grokus-1.0.0.6 spec/lib/calagator/blacklist_validator_spec.rb
grokus-1.0.0.5 spec/lib/calagator/blacklist_validator_spec.rb
grokus-1.0.0.3 spec/lib/calagator/blacklist_validator_spec.rb
grokus-1.0.0.2 spec/lib/calagator/blacklist_validator_spec.rb
grokus-1.0.0.1 spec/lib/calagator/blacklist_validator_spec.rb
calagator-1.0.0.rc3 spec/lib/calagator/blacklist_validator_spec.rb
calagator-1.0.0.rc2 spec/lib/calagator/blacklist_validator_spec.rb
calagator-1.0.0.rc1 spec/lib/calagator/blacklist_validator_spec.rb
calagator-0.0.1.pre1 spec/lib/calagator/blacklist_validator_spec.rb