Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix error in Blowfish, SHA1 password strategies

* Bug report:
  #222
* Remove duplication in spec suite.
* Extract `fake_model_with_password_strategy` spec helper method.
  • Loading branch information...
commit 1e9cc7c72f9d94c9fd8eeeec0dce4c6882520e6a 1 parent 5d5d5c0
@croaky croaky authored
View
6 lib/clearance/password_strategies/blowfish.rb
@@ -30,9 +30,13 @@ def generate_hash(string)
def initialize_salt_if_necessary
if salt.blank?
- self.salt = generate_random_code
+ self.salt = generate_salt
end
end
+
+ def generate_salt
+ SecureRandom.hex(20).encode('UTF-8')
+ end
end
end
end
View
6 lib/clearance/password_strategies/sha1.rb
@@ -30,9 +30,13 @@ def generate_hash(string)
def initialize_salt_if_necessary
if salt.blank?
- self.salt = generate_random_code
+ self.salt = generate_salt
end
end
+
+ def generate_salt
+ SecureRandom.hex(20).encode('UTF-8')
+ end
end
end
end
View
10 spec/models/bcrypt_migration_from_sha1_spec.rb
@@ -2,12 +2,9 @@
describe Clearance::PasswordStrategies::BCryptMigrationFromSHA1 do
subject do
- Class.new do
- attr_reader :password
- attr_accessor :encrypted_password
- attr_accessor :salt
- include Clearance::PasswordStrategies::BCryptMigrationFromSHA1
- end.new
+ fake_model_with_password_strategy(
+ Clearance::PasswordStrategies::BCryptMigrationFromSHA1
+ )
end
describe '#password=' do
@@ -86,5 +83,4 @@
end
end
end
-
end
View
5 spec/models/bcrypt_spec.rb
@@ -2,10 +2,7 @@
describe Clearance::PasswordStrategies::BCrypt do
subject do
- Class.new do
- attr_accessor :encrypted_password
- include Clearance::PasswordStrategies::BCrypt
- end.new
+ fake_model_with_password_strategy(Clearance::PasswordStrategies::BCrypt)
end
describe '#password=' do
View
7 spec/models/blowfish_spec.rb
@@ -2,12 +2,7 @@
describe Clearance::PasswordStrategies::Blowfish do
subject do
- Class.new do
- attr_accessor :salt, :encrypted_password
- include Clearance::PasswordStrategies::Blowfish
-
- def generate_random_code; 'code'; end
- end.new
+ fake_model_with_password_strategy(Clearance::PasswordStrategies::Blowfish)
end
describe '#password=' do
View
7 spec/models/sha1_spec.rb
@@ -2,12 +2,7 @@
describe Clearance::PasswordStrategies::SHA1 do
subject do
- Class.new do
- attr_accessor :salt, :encrypted_password
- include Clearance::PasswordStrategies::SHA1
-
- def generate_random_code; "code"; end
- end.new
+ fake_model_with_password_strategy(Clearance::PasswordStrategies::SHA1)
end
describe '#password=' do
View
14 spec/support/fake_model_with_password_strategy.rb
@@ -0,0 +1,14 @@
+module FakeModelWithPasswordStrategy
+ def fake_model_with_password_strategy(password_strategy)
+ Class.new do
+ attr_reader :password
+ attr_accessor :encrypted_password, :salt
+
+ include password_strategy
+ end.new
+ end
+end
+
+RSpec.configure do |config|
+ config.include FakeModelWithPasswordStrategy
+end
Please sign in to comment.
Something went wrong with that request. Please try again.