Browse files

Speed up tests for apps using clearance with bcrypt

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...
1 parent 1b0c364 commit a441a2bed8f525349717016c395178c3db42ffde @derekprior derekprior committed Mar 6, 2013
View
14 lib/clearance/password_strategies/bcrypt.rb
@@ -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
View
2 spec/models/bcrypt_migration_from_sha1_spec.rb
@@ -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
View
23 spec/models/bcrypt_spec.rb
@@ -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

0 comments on commit a441a2b

Please sign in to comment.