Skip to content

Commit

Permalink
Speed up tests for apps using clearance with bcrypt
Browse files Browse the repository at this point in the history
If clearance detects that it's being run in the test environment, it
will lower the bcyrpt cost factor (essentially, how much it will sleep)
to the minimum.
  • Loading branch information
derekprior committed Mar 6, 2013
1 parent 1b0c364 commit a441a2b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
14 changes: 13 additions & 1 deletion lib/clearance/password_strategies/bcrypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ def password=(new_password)
private

def encrypt(password)
::BCrypt::Password.create(password)
::BCrypt::Password.create(password, :cost => cost)
end

def cost
if test_environment?
::BCrypt::Engine::MIN_COST
else
::BCrypt::Engine::DEFAULT_COST
end
end

def test_environment?
defined?(::Rails) && ::Rails.env.test?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/bcrypt_migration_from_sha1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
end

it 'encrypts with BCrypt' do
BCrypt::Password.should have_received(:create).with(password)
BCrypt::Password.should have_received(:create).with(password, anything)
end

it 'sets the pasword on the subject' do
Expand Down
23 changes: 20 additions & 3 deletions spec/models/bcrypt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,32 @@

before do
BCrypt::Password.stubs :create => encrypted_password
subject.password = password
end

it 'encrypts the password into encrypted_password' do
subject.password = password

subject.encrypted_password.should == encrypted_password
end

it 'encrypts with BCrypt' do
BCrypt::Password.should have_received(:create).with(password)
it 'encrypts with BCrypt using default cost in non test environments' do
Rails.stubs :env => ActiveSupport::StringInquirer.new("production")

subject.password = password

BCrypt::Password.should have_received(:create).with(
password,
:cost => ::BCrypt::Engine::DEFAULT_COST
)
end

it 'encrypts with BCrypt using minimum cost in test environment' do
subject.password = password

BCrypt::Password.should have_received(:create).with(
password,
:cost => ::BCrypt::Engine::MIN_COST
)
end
end

Expand Down

0 comments on commit a441a2b

Please sign in to comment.