Sha256: fb2be7aff451dc03c9ed5711aea049147af8a233268d7fed8f1b9999bf0f5aa4

Contents?: true

Size: 1.56 KB

Versions: 39

Compression:

Stored size: 1.56 KB

Contents

require File.dirname(__FILE__) + '/test_helper.rb'

class RandomTest < ActiveSupport::TestCase
  def test_random_tokens_have_consisten_length
    with_any_random do
      assert_equal 128, Authlogic::Random.hex_token.length
      assert_equal 20,  Authlogic::Random.friendly_token.length
    end
  end
  
  def test_random_tokens_are_indeed_random
    # this might fail if you are *really* unlucky :)
    with_any_random do
      assert_not_equal Authlogic::Random.hex_token,       Authlogic::Random.hex_token
      assert_not_equal Authlogic::Random.friendly_token,  Authlogic::Random.friendly_token
    end
  end

  private
    def with_any_random(&block)
      [true, false].each {|val| with_secure_random_enabled(val, &block)}
    end

    def with_secure_random_enabled(enabled = true)
      # can't really test SecureRandom if we don't have an implementation
      return if enabled && !Authlogic::Random::SecureRandom
    
      current_sec_rand = Authlogic::Random::SecureRandom
      reload_authlogic_with_sec_random!(current_sec_rand, enabled)
  
      yield
    ensure
      reload_authlogic_with_sec_random!(current_sec_rand)
    end

    def reload_authlogic_with_sec_random!(secure_random, enabled = true)
      silence_warnings do
        secure_random.parent.const_set(secure_random.name.sub("#{secure_random.parent}::", ''), enabled ? secure_random : nil)
        load(File.dirname(__FILE__) + '/../lib/authlogic/random.rb')
      end
    end

    def silence_warnings
      old_verbose, $VERBOSE = $VERBOSE, nil
      yield
    ensure
      $VERBOSE = old_verbose
    end
end

Version data entries

39 entries across 39 versions & 14 rubygems

Version Path
binarylogic-authlogic-2.1.0 test/random_test.rb
binarylogic-authlogic-2.1.1 test/random_test.rb
crankharder-authlogic-2.1.1 test/random_test.rb
jlecour-authlogic-2.1.2 test/random_test.rb
kschrader-authlogic-2.1.2 test/random_test.rb
kschrader-authlogic-2.1.3 test/random_test.rb
wulffeld_authlogic-2.1.3 test/random_test.rb
lockbox_middleware-1.2.1 vendor/gems/authlogic-2.1.3/test/random_test.rb
Empact-authlogic-2.1.5 test/random_test.rb
authlogic-2.1.5 test/random_test.rb
authlogic-2.1.4 test/random_test.rb
Empact-authlogic-2.1.4 test/random_test.rb
namxam-authlogic-2.1.3.1 test/random_test.rb
drogus-authlogic-2.1.3 test/random_test.rb
novelys_authlogic-2.1.6 test/random_test.rb
novelys_authlogic-2.1.5 test/random_test.rb
novelys_authlogic-2.1.4 test/random_test.rb
novelys_authlogic-2.1.3 test/random_test.rb
ginst-2009.12.8 vendor/plugins/authlogic/test/random_test.rb
skippy-authlogic-2.1.3 test/random_test.rb