Skip to content
Browse files

BCryptHash now autoloads rather than being automatically required

This stops applications which require 'dm-types' from breaking automatically
on applying this update, by attempting to require the bcrypt gem.

specs are tweaked to only run if the file can be loaded too, otherwise they are
pending.
  • Loading branch information...
1 parent 5cc5f8c commit 0ec0edd865573555b6e077a5f0f9feba52504d9c @namelessjon namelessjon committed Jul 20, 2008
Showing with 77 additions and 52 deletions.
  1. +7 −1 dm-types/lib/dm-types.rb
  2. +37 −27 dm-types/spec/integration/bcrypt_hash_spec.rb
  3. +33 −24 dm-types/spec/unit/bcrypt_hash_spec.rb
View
8 dm-types/lib/dm-types.rb
@@ -16,4 +16,10 @@
require dir / 'uri'
require dir / 'yaml'
require dir / 'serial'
-require dir / 'bcrypt_hash'
+
+# this looks a little ugly, but everyone who uses dm-types shouldn't have to have ruby-bcrypt installed
+module DataMapper
+ module Types
+ autoload(:BCryptHash, File.join(Pathname(__FILE__).dirname.expand_path, 'dm-types', 'bcrypt_hash'))
+ end
+end
View
64 dm-types/spec/integration/bcrypt_hash_spec.rb
@@ -2,39 +2,49 @@
require 'pathname'
require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
-describe DataMapper::Types::BCryptHash do
- describe "with no options" do
- before(:each) do
- class User
- include DataMapper::Resource
+begin
+ require 'bcrypt'
+rescue LoadError
+ skip_tests = true
+end
+
+describe "DataMapper::Types::BCryptHash" do
+ unless skip_tests
+ describe "with no options" do
+ before(:each) do
+ class User
+ include DataMapper::Resource
- property :id, Serial
- property :password, BCryptHash
+ property :id, Serial
+ property :password, BCryptHash
+ end
+ User.auto_migrate!
+ User.create!(:password => "DataMapper R0cks!")
end
- User.auto_migrate!
- User.create!(:password => "DataMapper R0cks!")
- end
- it "should save a password to the DB on creation" do
- repository(:default) do
- User.create!(:password => "password1")
+ it "should save a password to the DB on creation" do
+ repository(:default) do
+ User.create!(:password => "password1")
+ end
+ user = User.all
+ user[0].password.should == "DataMapper R0cks!"
+ user[1].password.should == "password1"
end
- user = User.all
- user[0].password.should == "DataMapper R0cks!"
- user[1].password.should == "password1"
- end
- it "should change the password on attribute update" do
- @user = User.first
- @user.attribute_set(:password, "D@t@Mapper R0cks!")
- @user.save
- @user.password.should_not == "DataMapper R0cks!"
- @user.password.should == "D@t@Mapper R0cks!"
- end
+ it "should change the password on attribute update" do
+ @user = User.first
+ @user.attribute_set(:password, "D@t@Mapper R0cks!")
+ @user.save
+ @user.password.should_not == "DataMapper R0cks!"
+ @user.password.should == "D@t@Mapper R0cks!"
+ end
- it "should have a cost of BCrypt::Engine::DEFAULT_COST" do
- @user = User.first
- @user.password.cost.should == BCrypt::Engine::DEFAULT_COST
+ it "should have a cost of BCrypt::Engine::DEFAULT_COST" do
+ @user = User.first
+ @user.password.cost.should == BCrypt::Engine::DEFAULT_COST
+ end
end
+ else
+ it "Needs the bcrypt-ruby gem installed"
end
end
View
57 dm-types/spec/unit/bcrypt_hash_spec.rb
@@ -3,39 +3,48 @@
include DataMapper::Types
-describe DataMapper::Types::BCryptHash do
+begin
+ require 'bcrypt'
+rescue LoadError
+ skip_tests = true
+end
- before(:each) do
- @clear_password = "DataMapper R0cks!"
- @crypted_password = BCrypt::Password.create(@clear_password)
- end
+describe "DataMapper::Types::BCryptHash" do
+ unless skip_tests
- describe ".dump" do
- it "should return a crypted hash as a BCrypt::Password" do
- BCryptHash.dump(@clear_password, :property).should be_an_instance_of(BCrypt::Password)
+ before(:each) do
+ @clear_password = "DataMapper R0cks!"
+ @crypted_password = BCrypt::Password.create(@clear_password)
end
- it "should return a string that is 60 characters long" do
- BCryptHash.dump(@clear_password, :property).should have(60).characters
- end
+ describe ".dump" do
+ it "should return a crypted hash as a BCrypt::Password" do
+ BCryptHash.dump(@clear_password, :property).should be_an_instance_of(BCrypt::Password)
+ end
- it "should return nil if nil is passed" do
- BCryptHash.dump(nil, :property).should be_nil
- end
- end
+ it "should return a string that is 60 characters long" do
+ BCryptHash.dump(@clear_password, :property).should have(60).characters
+ end
- describe ".load" do
- it "should return the password as a BCrypt::Password" do
- BCryptHash.load(@crypted_password, :property).should be_an_instance_of(BCrypt::Password)
+ it "should return nil if nil is passed" do
+ BCryptHash.dump(nil, :property).should be_nil
+ end
end
- it "should return the password as a password which matches" do
- BCryptHash.load(@crypted_password, :property).should == @clear_password
- end
+ describe ".load" do
+ it "should return the password as a BCrypt::Password" do
+ BCryptHash.load(@crypted_password, :property).should be_an_instance_of(BCrypt::Password)
+ end
- it "should return nil if given nil" do
- FilePath.load(nil, :property).should be_nil
- end
+ it "should return the password as a password which matches" do
+ BCryptHash.load(@crypted_password, :property).should == @clear_password
+ end
+ it "should return nil if given nil" do
+ FilePath.load(nil, :property).should be_nil
+ end
+ end
+ else
+ it "requires the bcrypt-ruby gem to test"
end
end

0 comments on commit 0ec0edd

Please sign in to comment.
Something went wrong with that request. Please try again.