Sha256: 37cf38de7c51bac36cafbbbe556e7a3b953942b82c193cb2f159a6448bf0a777

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require 'test_helper'
ActiveValidators.activate(:password)

describe "Password Validation" do
  STRENGTHS = {
    :weak   => { :valid => 'sixchr',   :invalid => 'foo' },
    :medium => { :valid => 'chrs123',  :invalid => 'sixchr' },
    :strong => { :valid => 'HQSij2323#$%', :invalid => 'chrs123' }
  }

  STRENGTHS.each_pair do |strength, passwords|
    describe "#{strength} mode" do
      describe "valid passwords" do
        it "accepts a #{strength} password like #{passwords[:valid]}" do
          subject = build_password_record strength, :password => passwords[:valid]
          _(subject.valid?).must_equal(true)
          _(subject.errors.size).must_equal(0)
        end
      end

      describe "invalid passwords" do
        it "rejects invalid passwords like #{passwords[:invalid]}" do
          subject = build_password_record strength, :password => passwords[:invalid]
          _(subject.valid?).must_equal(false)
          _(subject.errors.size).must_equal(1)
        end

        it "generates an error message of type invalid" do
          subject = build_password_record strength, :password => passwords[:invalid]
          _(subject.valid?).must_equal(false)
          _(subject.errors[:password].include?(subject.errors.generate_message(:password, :invalid))).must_equal(true)
        end
      end
    end
  end
  def build_password_record(strength, attrs = {})
    TestRecord.reset_callbacks(:validate)
    TestRecord.validates :password, :password => { :strength => strength }
    TestRecord.new attrs
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activevalidators-6.0.0 test/validations/password_test.rb