Sha256: 9a0c39dadd0590cc8b29646c8d2e1733a14d014accc4c5c667c982aeb9ef1d75

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require File.join(File.dirname(__FILE__), "..", "spec_helper")

context "Creating a hashed password" do
  
  setup do
    @secret = "wheedle"
    @password = BCrypt::Password.create(@secret, :cost => 4)
  end
  
  specify "should return a BCrypt::Password" do
    @password.should be_an_instance_of(BCrypt::Password)
  end
  
  specify "should return a valid bcrypt password" do
    lambda { BCrypt::Password.new(@password) }.should_not raise_error
  end
  
  specify "should raise an InvalidSecret exception if the secret is nil" do
    lambda { BCrypt::Password.create(nil) }.should raise_error(BCrypt::Errors::InvalidSecret)
  end
end

context "Reading a hashed password" do
  setup do
    @secret = "U*U"
    @hash = "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW"
  end
  
  specify "should read the version, cost, salt, and hash" do
    password = BCrypt::Password.new(@hash)
    password.version.should eql("2a")
    password.cost.should equal(5)
    password.salt.should eql("$2a$05$CCCCCCCCCCCCCCCCCCCCC.")
    password.to_s.should_eql @hash
  end
  
  specify "should raise an InvalidHashError when given an invalid hash" do
    lambda { BCrypt::Password.new('weedle') }.should raise_error(BCrypt::Errors::InvalidHash)
  end
end

context "Comparing a hashed password with a secret" do
  setup do
    @secret = "U*U"
    @hash = "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW"
    @password = BCrypt::Password.create(@secret)
  end
  
  specify "should compare successfully to the original secret" do
    (@password == @secret).should be(true)
  end
  
  specify "should compare unsuccessfully to anything besides original secret" do
    (@password == "@secret").should be(false)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bcrypt-ruby-2.0.0 spec/bcrypt/password_spec.rb