Skip to content

Commit

Permalink
Check that common passwords list is greater than 0
Browse files Browse the repository at this point in the history
  • Loading branch information
nlalonde committed Dec 30, 2013
1 parent 8a1593b commit 47e1d00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/common_passwords/common_passwords.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def include?(password)

def self.password_list
@mutex.synchronize do
load_passwords unless redis.exists(LIST_KEY)
load_passwords unless redis.scard(LIST_KEY) > 0
end
RedisPasswordList.new
end
Expand Down
16 changes: 14 additions & 2 deletions spec/components/common_passwords/common_passwords_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,37 @@
it "loads the passwords file if redis doesn't have it" do
mock_redis = mock("redis")
mock_redis.stubs(:exists).returns(false)
mock_redis.stubs(:scard).returns(0)
described_class.stubs(:redis).returns(mock_redis)
described_class.expects(:load_passwords).returns([])
described_class.expects(:load_passwords).returns(['password'])
list = described_class.password_list
list.should respond_to(:include?)
end

it "doesn't load the passwords file if redis has it" do
mock_redis = mock("redis")
mock_redis.stubs(:exists).returns(true)
mock_redis.stubs(:scard).returns(5000)
described_class.stubs(:redis).returns(mock_redis)
described_class.expects(:load_passwords).never
list = described_class.password_list
list.should respond_to(:include?)
end

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
list.should respond_to(:include?)
end
end

context "missing password file" do
it "tolerates it" do
described_class.stubs(:redis).returns(stub_everything(sismember: false))
described_class.stubs(:redis).returns(stub_everything(sismember: false, exists: false, scard: 0))
File.stubs(:readlines).with(described_class::PASSWORD_FILE).raises(Errno::ENOENT)
described_class.common_password?("password").should eq(false)
end
Expand Down

0 comments on commit 47e1d00

Please sign in to comment.