2010-12-18 16:38:05 -05:00
|
|
|
require 'cases/helper'
|
|
|
|
require 'models/user'
|
2011-01-25 21:35:02 -05:00
|
|
|
require 'models/visitor'
|
|
|
|
require 'models/administrator'
|
2010-12-18 16:38:05 -05:00
|
|
|
|
|
|
|
class SecurePasswordTest < ActiveModel::TestCase
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2010-12-18 16:38:05 -05:00
|
|
|
setup do
|
|
|
|
@user = User.new
|
|
|
|
end
|
|
|
|
|
2011-04-14 17:54:25 -04:00
|
|
|
test "blank password" do
|
|
|
|
user = User.new
|
|
|
|
user.password = ''
|
|
|
|
assert !user.valid?, 'user should be invalid'
|
|
|
|
end
|
|
|
|
|
|
|
|
test "nil password" do
|
|
|
|
user = User.new
|
|
|
|
user.password = nil
|
|
|
|
assert !user.valid?, 'user should be invalid'
|
|
|
|
end
|
|
|
|
|
2010-12-18 16:38:05 -05:00
|
|
|
test "password must be present" do
|
|
|
|
assert !@user.valid?
|
|
|
|
assert_equal 1, @user.errors.size
|
|
|
|
end
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2010-12-18 16:38:05 -05:00
|
|
|
test "password must match confirmation" do
|
|
|
|
@user.password = "thiswillberight"
|
|
|
|
@user.password_confirmation = "wrong"
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2010-12-18 16:38:05 -05:00
|
|
|
assert !@user.valid?
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2010-12-18 16:38:05 -05:00
|
|
|
@user.password_confirmation = "thiswillberight"
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2010-12-18 16:38:05 -05:00
|
|
|
assert @user.valid?
|
|
|
|
end
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2010-12-18 16:38:05 -05:00
|
|
|
test "authenticate" do
|
|
|
|
@user.password = "secret"
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2010-12-18 16:38:05 -05:00
|
|
|
assert !@user.authenticate("wrong")
|
|
|
|
assert @user.authenticate("secret")
|
|
|
|
end
|
2011-01-25 21:35:02 -05:00
|
|
|
|
|
|
|
test "visitor#password_digest should be protected against mass assignment" do
|
2011-04-23 06:50:29 -04:00
|
|
|
assert Visitor.active_authorizers[:default].kind_of?(ActiveModel::MassAssignmentSecurity::BlackList)
|
|
|
|
assert Visitor.active_authorizers[:default].include?(:password_digest)
|
2011-01-25 21:35:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "Administrator's mass_assignment_authorizer should be WhiteList" do
|
2011-04-23 06:50:29 -04:00
|
|
|
active_authorizer = Administrator.active_authorizers[:default]
|
|
|
|
assert active_authorizer.kind_of?(ActiveModel::MassAssignmentSecurity::WhiteList)
|
|
|
|
assert !active_authorizer.include?(:password_digest)
|
|
|
|
assert active_authorizer.include?(:name)
|
2011-01-25 21:35:02 -05:00
|
|
|
end
|
2010-12-19 11:58:14 -05:00
|
|
|
end
|