2013-12-19 15:12:03 -05:00
|
|
|
require 'spec_helper'
|
2013-12-20 16:34:34 -05:00
|
|
|
require_dependency "common_passwords/common_passwords"
|
2013-12-19 15:12:03 -05:00
|
|
|
|
|
|
|
describe PasswordValidator do
|
|
|
|
|
|
|
|
let(:validator) { described_class.new({attributes: :password}) }
|
|
|
|
subject(:validate) { validator.validate_each(record,:password,@password) }
|
|
|
|
|
|
|
|
context "password required" do
|
|
|
|
let(:record) { u = Fabricate.build(:user, password: @password); u.password_required!; u }
|
|
|
|
|
2013-12-20 16:34:34 -05:00
|
|
|
context "password is not common" do
|
|
|
|
before do
|
2013-12-27 11:15:53 -05:00
|
|
|
CommonPasswords.stubs(:common_password?).returns(false)
|
2013-12-20 16:34:34 -05:00
|
|
|
end
|
2013-12-19 16:15:36 -05:00
|
|
|
|
2013-12-20 16:34:34 -05:00
|
|
|
context "min password length is 8" do
|
|
|
|
before { SiteSetting.stubs(:min_password_length).returns(8) }
|
|
|
|
|
|
|
|
it "doesn't add an error when password is good" do
|
|
|
|
@password = "weron235alsfn234"
|
|
|
|
validate
|
|
|
|
record.errors[:password].should_not be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds an error when password is too short" do
|
|
|
|
@password = "p"
|
|
|
|
validate
|
|
|
|
record.errors[:password].should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds an error when password is blank" do
|
|
|
|
@password = ''
|
|
|
|
validate
|
|
|
|
record.errors[:password].should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds an error when password is nil" do
|
|
|
|
@password = nil
|
|
|
|
validate
|
|
|
|
record.errors[:password].should be_present
|
|
|
|
end
|
2013-12-19 16:15:36 -05:00
|
|
|
end
|
|
|
|
|
2013-12-20 16:34:34 -05:00
|
|
|
context "min password length is 12" do
|
|
|
|
before { SiteSetting.stubs(:min_password_length).returns(12) }
|
|
|
|
|
|
|
|
it "adds an error when password length is 11" do
|
|
|
|
@password = "gt38sdt92bv"
|
|
|
|
validate
|
|
|
|
record.errors[:password].should be_present
|
|
|
|
end
|
2013-12-19 16:15:36 -05:00
|
|
|
end
|
2013-12-20 16:34:34 -05:00
|
|
|
end
|
2013-12-19 16:15:36 -05:00
|
|
|
|
2013-12-20 16:34:34 -05:00
|
|
|
context "password is commonly used" do
|
|
|
|
before do
|
2013-12-27 11:15:53 -05:00
|
|
|
CommonPasswords.stubs(:common_password?).returns(true)
|
2013-12-19 16:15:36 -05:00
|
|
|
end
|
|
|
|
|
2013-12-20 16:34:34 -05:00
|
|
|
it "adds an error when block_common_passwords is enabled" do
|
|
|
|
SiteSetting.stubs(:block_common_passwords).returns(true)
|
|
|
|
@password = "password"
|
2013-12-19 16:15:36 -05:00
|
|
|
validate
|
|
|
|
record.errors[:password].should be_present
|
|
|
|
end
|
2013-12-19 15:12:03 -05:00
|
|
|
|
2013-12-20 16:34:34 -05:00
|
|
|
it "doesn't add an error when block_common_passwords is disabled" do
|
|
|
|
SiteSetting.stubs(:block_common_passwords).returns(false)
|
|
|
|
@password = "password"
|
2013-12-19 16:15:36 -05:00
|
|
|
validate
|
2013-12-20 16:34:34 -05:00
|
|
|
record.errors[:password].should_not be_present
|
2013-12-19 16:15:36 -05:00
|
|
|
end
|
2013-12-19 15:12:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "password not required" do
|
|
|
|
let(:record) { Fabricate.build(:user, password: @password) }
|
|
|
|
|
|
|
|
it "doesn't add an error if password is not required" do
|
|
|
|
@password = nil
|
|
|
|
validate
|
|
|
|
record.errors[:password].should_not be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|