Skip to content

Commit

Permalink
Fix error in Blowfish, SHA1 password strategies
Browse files Browse the repository at this point in the history
* Bug report:
  thoughtbot#222
* Remove duplication in spec suite.
* Extract `fake_model_with_password_strategy` spec helper method.
  • Loading branch information
Dan Croak committed Feb 27, 2013
1 parent 5d5d5c0 commit 1e9cc7c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 25 deletions.
6 changes: 5 additions & 1 deletion lib/clearance/password_strategies/blowfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion lib/clearance/password_strategies/sha1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 3 additions & 7 deletions spec/models/bcrypt_migration_from_sha1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,5 +83,4 @@
end
end
end

end
5 changes: 1 addition & 4 deletions spec/models/bcrypt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions spec/models/blowfish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions spec/models/sha1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/support/fake_model_with_password_strategy.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1e9cc7c

Please sign in to comment.