Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 0ec0edd865573555b6e077a5f0f9feba52504d9c 1 parent 5cc5f8c
Jon namelessjon authored
8 dm-types/lib/dm-types.rb
@@ -16,4 +16,10 @@
16 16 require dir / 'uri'
17 17 require dir / 'yaml'
18 18 require dir / 'serial'
19   -require dir / 'bcrypt_hash'
  19 +
  20 +# this looks a little ugly, but everyone who uses dm-types shouldn't have to have ruby-bcrypt installed
  21 +module DataMapper
  22 + module Types
  23 + autoload(:BCryptHash, File.join(Pathname(__FILE__).dirname.expand_path, 'dm-types', 'bcrypt_hash'))
  24 + end
  25 +end
64 dm-types/spec/integration/bcrypt_hash_spec.rb
@@ -2,39 +2,49 @@
2 2 require 'pathname'
3 3 require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
4 4
5   -describe DataMapper::Types::BCryptHash do
6   - describe "with no options" do
7   - before(:each) do
8   - class User
9   - include DataMapper::Resource
  5 +begin
  6 + require 'bcrypt'
  7 +rescue LoadError
  8 + skip_tests = true
  9 +end
  10 +
  11 +describe "DataMapper::Types::BCryptHash" do
  12 + unless skip_tests
  13 + describe "with no options" do
  14 + before(:each) do
  15 + class User
  16 + include DataMapper::Resource
10 17
11   - property :id, Serial
12   - property :password, BCryptHash
  18 + property :id, Serial
  19 + property :password, BCryptHash
  20 + end
  21 + User.auto_migrate!
  22 + User.create!(:password => "DataMapper R0cks!")
13 23 end
14   - User.auto_migrate!
15   - User.create!(:password => "DataMapper R0cks!")
16   - end
17 24
18   - it "should save a password to the DB on creation" do
19   - repository(:default) do
20   - User.create!(:password => "password1")
  25 + it "should save a password to the DB on creation" do
  26 + repository(:default) do
  27 + User.create!(:password => "password1")
  28 + end
  29 + user = User.all
  30 + user[0].password.should == "DataMapper R0cks!"
  31 + user[1].password.should == "password1"
21 32 end
22   - user = User.all
23   - user[0].password.should == "DataMapper R0cks!"
24   - user[1].password.should == "password1"
25   - end
26 33
27   - it "should change the password on attribute update" do
28   - @user = User.first
29   - @user.attribute_set(:password, "D@t@Mapper R0cks!")
30   - @user.save
31   - @user.password.should_not == "DataMapper R0cks!"
32   - @user.password.should == "D@t@Mapper R0cks!"
33   - end
  34 + it "should change the password on attribute update" do
  35 + @user = User.first
  36 + @user.attribute_set(:password, "D@t@Mapper R0cks!")
  37 + @user.save
  38 + @user.password.should_not == "DataMapper R0cks!"
  39 + @user.password.should == "D@t@Mapper R0cks!"
  40 + end
34 41
35   - it "should have a cost of BCrypt::Engine::DEFAULT_COST" do
36   - @user = User.first
37   - @user.password.cost.should == BCrypt::Engine::DEFAULT_COST
  42 + it "should have a cost of BCrypt::Engine::DEFAULT_COST" do
  43 + @user = User.first
  44 + @user.password.cost.should == BCrypt::Engine::DEFAULT_COST
  45 + end
38 46 end
  47 + else
  48 + it "Needs the bcrypt-ruby gem installed"
39 49 end
40 50 end
57 dm-types/spec/unit/bcrypt_hash_spec.rb
@@ -3,39 +3,48 @@
3 3
4 4 include DataMapper::Types
5 5
6   -describe DataMapper::Types::BCryptHash do
  6 +begin
  7 + require 'bcrypt'
  8 +rescue LoadError
  9 + skip_tests = true
  10 +end
7 11
8   - before(:each) do
9   - @clear_password = "DataMapper R0cks!"
10   - @crypted_password = BCrypt::Password.create(@clear_password)
11   - end
  12 +describe "DataMapper::Types::BCryptHash" do
  13 + unless skip_tests
12 14
13   - describe ".dump" do
14   - it "should return a crypted hash as a BCrypt::Password" do
15   - BCryptHash.dump(@clear_password, :property).should be_an_instance_of(BCrypt::Password)
  15 + before(:each) do
  16 + @clear_password = "DataMapper R0cks!"
  17 + @crypted_password = BCrypt::Password.create(@clear_password)
16 18 end
17 19
18   - it "should return a string that is 60 characters long" do
19   - BCryptHash.dump(@clear_password, :property).should have(60).characters
20   - end
  20 + describe ".dump" do
  21 + it "should return a crypted hash as a BCrypt::Password" do
  22 + BCryptHash.dump(@clear_password, :property).should be_an_instance_of(BCrypt::Password)
  23 + end
21 24
22   - it "should return nil if nil is passed" do
23   - BCryptHash.dump(nil, :property).should be_nil
24   - end
25   - end
  25 + it "should return a string that is 60 characters long" do
  26 + BCryptHash.dump(@clear_password, :property).should have(60).characters
  27 + end
26 28
27   - describe ".load" do
28   - it "should return the password as a BCrypt::Password" do
29   - BCryptHash.load(@crypted_password, :property).should be_an_instance_of(BCrypt::Password)
  29 + it "should return nil if nil is passed" do
  30 + BCryptHash.dump(nil, :property).should be_nil
  31 + end
30 32 end
31 33
32   - it "should return the password as a password which matches" do
33   - BCryptHash.load(@crypted_password, :property).should == @clear_password
34   - end
  34 + describe ".load" do
  35 + it "should return the password as a BCrypt::Password" do
  36 + BCryptHash.load(@crypted_password, :property).should be_an_instance_of(BCrypt::Password)
  37 + end
35 38
36   - it "should return nil if given nil" do
37   - FilePath.load(nil, :property).should be_nil
38   - end
  39 + it "should return the password as a password which matches" do
  40 + BCryptHash.load(@crypted_password, :property).should == @clear_password
  41 + end
39 42
  43 + it "should return nil if given nil" do
  44 + FilePath.load(nil, :property).should be_nil
  45 + end
  46 + end
  47 + else
  48 + it "requires the bcrypt-ruby gem to test"
40 49 end
41 50 end

0 comments on commit 0ec0edd

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