2015-10-11 10:41:23 +01:00
require " rails_helper "
2013-12-20 16:34:34 -05:00
require_dependency " common_passwords/common_passwords "
describe CommonPasswords do
it " the passwords file should exist " do
2015-01-09 13:34:37 -03:00
expect ( File . exists? ( described_class :: PASSWORD_FILE ) ) . to eq ( true )
2013-12-20 16:34:34 -05:00
end
describe " # common_password? " do
before { described_class . stubs ( :redis ) . returns ( stub_everything ) }
subject { described_class . common_password? @password }
it " returns false if password isn't in the common passwords list " do
described_class . stubs ( :password_list ) . returns ( stub_everything ( :include? = > false ) )
@password = 'uncommonPassword'
2015-01-09 13:34:37 -03:00
expect ( subject ) . to eq ( false )
2013-12-20 16:34:34 -05:00
end
it " returns false if password is nil " do
described_class . expects ( :password_list ) . never
@password = nil
2015-01-09 13:34:37 -03:00
expect ( subject ) . to eq ( false )
2013-12-20 16:34:34 -05:00
end
it " returns false if password is blank " do
described_class . expects ( :password_list ) . never
@password = " "
2015-01-09 13:34:37 -03:00
expect ( subject ) . to eq ( false )
2013-12-20 16:34:34 -05:00
end
it " returns true if password is in the common passwords list " do
described_class . stubs ( :password_list ) . returns ( stub_everything ( :include? = > true ) )
@password = " password "
2015-01-09 13:34:37 -03:00
expect ( subject ) . to eq ( true )
2013-12-20 16:34:34 -05:00
end
end
describe '#password_list' do
it " loads the passwords file if redis doesn't have it " do
mock_redis = mock ( " redis " )
mock_redis . stubs ( :exists ) . returns ( false )
2013-12-30 14:25:50 -05:00
mock_redis . stubs ( :scard ) . returns ( 0 )
2013-12-20 16:34:34 -05:00
described_class . stubs ( :redis ) . returns ( mock_redis )
2013-12-30 14:25:50 -05:00
described_class . expects ( :load_passwords ) . returns ( [ 'password' ] )
2013-12-20 16:34:34 -05:00
list = described_class . password_list
2015-01-09 13:34:37 -03:00
expect ( list ) . to respond_to ( :include? )
2013-12-20 16:34:34 -05:00
end
it " doesn't load the passwords file if redis has it " do
mock_redis = mock ( " redis " )
mock_redis . stubs ( :exists ) . returns ( true )
2014-05-02 12:01:21 -04:00
mock_redis . stubs ( :scard ) . returns ( 10000 )
2013-12-20 16:34:34 -05:00
described_class . stubs ( :redis ) . returns ( mock_redis )
described_class . expects ( :load_passwords ) . never
list = described_class . password_list
2015-01-09 13:34:37 -03:00
expect ( list ) . to respond_to ( :include? )
2013-12-20 16:34:34 -05:00
end
2013-12-30 14:25:50 -05:00
it " loads the passwords file if redis has an empty list " do
mock_redis = mock ( " redis " )
mock_redis . stubs ( :exists ) . returns ( true )
mock_redis . stubs ( :scard ) . returns ( 0 )
described_class . stubs ( :redis ) . returns ( mock_redis )
described_class . expects ( :load_passwords ) . returns ( [ 'password' ] )
list = described_class . password_list
2015-01-09 13:34:37 -03:00
expect ( list ) . to respond_to ( :include? )
2013-12-30 14:25:50 -05:00
end
2013-12-20 16:34:34 -05:00
end
context " missing password file " do
it " tolerates it " do
2013-12-30 14:25:50 -05:00
described_class . stubs ( :redis ) . returns ( stub_everything ( sismember : false , exists : false , scard : 0 ) )
2013-12-20 16:34:34 -05:00
File . stubs ( :readlines ) . with ( described_class :: PASSWORD_FILE ) . raises ( Errno :: ENOENT )
2015-01-09 13:34:37 -03:00
expect ( described_class . common_password? ( " password " ) ) . to eq ( false )
2013-12-20 16:34:34 -05:00
end
end
end