2010-12-18 16:38:05 -05:00
|
|
|
require 'cases/helper'
|
|
|
|
require 'models/user'
|
2012-07-31 16:16:21 -04:00
|
|
|
require 'models/oauthed_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
|
2012-05-08 18:54:47 -04:00
|
|
|
@visitor = Visitor.new
|
2012-07-31 16:16:21 -04:00
|
|
|
@oauthed_user = OauthedUser.new
|
2010-12-18 16:38:05 -05:00
|
|
|
end
|
|
|
|
|
2011-04-14 17:54:25 -04:00
|
|
|
test "blank password" do
|
2012-05-08 18:54:47 -04:00
|
|
|
@user.password = @visitor.password = ''
|
|
|
|
assert !@user.valid?(:create), 'user should be invalid'
|
|
|
|
assert @visitor.valid?(:create), 'visitor should be valid'
|
2011-04-14 17:54:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "nil password" do
|
2012-05-08 18:54:47 -04:00
|
|
|
@user.password = @visitor.password = nil
|
|
|
|
assert !@user.valid?(:create), 'user should be invalid'
|
|
|
|
assert @visitor.valid?(:create), 'visitor should be valid'
|
2011-04-14 17:54:25 -04:00
|
|
|
end
|
|
|
|
|
2012-04-24 13:16:01 -04:00
|
|
|
test "blank password doesn't override previous password" do
|
|
|
|
@user.password = 'test'
|
|
|
|
@user.password = ''
|
|
|
|
assert_equal @user.password, 'test'
|
|
|
|
end
|
|
|
|
|
2010-12-18 16:38:05 -05:00
|
|
|
test "password must be present" do
|
2012-05-08 18:54:47 -04:00
|
|
|
assert !@user.valid?(:create)
|
2010-12-18 16:38:05 -05:00
|
|
|
assert_equal 1, @user.errors.size
|
|
|
|
end
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2012-05-08 18:54:47 -04:00
|
|
|
test "match confirmation" do
|
|
|
|
@user.password = @visitor.password = "thiswillberight"
|
|
|
|
@user.password_confirmation = @visitor.password_confirmation = "wrong"
|
2010-12-19 04:39:54 -05:00
|
|
|
|
2010-12-18 16:38:05 -05:00
|
|
|
assert !@user.valid?
|
2012-05-08 18:54:47 -04:00
|
|
|
assert @visitor.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
|
|
|
|
2012-05-08 18:54:47 -04:00
|
|
|
test "User should not be created with blank digest" do
|
2012-10-12 03:56:39 -04:00
|
|
|
assert_raise RuntimeError do
|
2012-05-08 18:54:47 -04:00
|
|
|
@user.run_callbacks :create
|
|
|
|
end
|
|
|
|
@user.password = "supersecretpassword"
|
|
|
|
assert_nothing_raised do
|
|
|
|
@user.run_callbacks :create
|
|
|
|
end
|
|
|
|
end
|
2012-07-31 16:16:21 -04:00
|
|
|
|
|
|
|
test "Oauthed user can be created with blank digest" do
|
|
|
|
assert_nothing_raised do
|
|
|
|
@oauthed_user.run_callbacks :create
|
|
|
|
end
|
|
|
|
end
|
2010-12-19 11:58:14 -05:00
|
|
|
end
|