Sha256: 627880473bcc1124d9f8395f9029e9eef6d87cd40cab68d3479102d8c7255680

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

require 'test_helper'

class RulesRegistrationTest < Minitest::Test
  def setup
    LIVR.register_default_rules({
      'strong_password' => lambda do |args|
        lambda do |value, unuse, unuse_|
          return if value.nil? or value.eql?('')
          return 'WEAK_PASSWORD' if value.to_s.length < 6
        end
      end
    })

    @validator = LIVR.new({
      'code'       => 'alphanumeric',
      'password'   => 'strong_password',
      'address'    => { 'nested_object' => {
        'street'   => 'alphanumeric',
        'password' => 'strong_password'
      }}
    })

    @validator.register_rules({
      'alphanumeric' => lambda do |args|
        lambda do |value, unuse, unuse_|
          return if value.nil? or value.eql?('')
          return 'NOT_ALPHANUMERIC' unless $value =~ /^[a-z0-9]+$/
        end
      end
    })
  end

  def test_rules_registration
    default_rules = LIVR.get_default_rules
    assert(default_rules['strong_password'].respond_to?(:call), 'Default rules should contain "strong_password" rule')
    assert(!default_rules['alphanumeric'].respond_to?(:call), 'Default rules should not contain "alphanumeric" rule')

    rules = @validator.get_rules
    assert(rules['strong_password'].respond_to?(:call), 'Validator rules should contain "strong_password" rule')
    assert(rules['alphanumeric'].respond_to?(:call), 'Validator rules should contain "alphanumeric" rule')

    output = @validator.validate({
      'code' => '!qwe',
      'password' => 123,
      'address' => {
        'street'   => 'Some Street!',
        'password' => 'qwer'
      }
    })

    assert(!output, 'should return false due to validation errors')
    assert_equal(@validator.get_errors, 
      {
        'code'      =>'NOT_ALPHANUMERIC',
        'password'  => 'WEAK_PASSWORD',
        'address'   => {
          'street'   => 'NOT_ALPHANUMERIC',
          'password' => 'WEAK_PASSWORD'
        }
      }, 'Should contain error codes'
    )
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
livr-0.0.2 test/03-test_rules_registration.rb