2010-03-26 11:27:19 +01:00
|
|
|
require 'test_helper'
|
2009-09-17 09:24:33 -03:00
|
|
|
require 'digest/sha1'
|
|
|
|
|
2010-03-29 16:13:19 +02:00
|
|
|
class DatabaseAuthenticatableTest < ActiveSupport::TestCase
|
2010-11-20 15:54:01 +01:00
|
|
|
test 'should downcase case insensitive keys when saving' do
|
|
|
|
# case_insensitive_keys is set to :email by default.
|
|
|
|
email = 'Foo@Bar.com'
|
|
|
|
user = new_user(:email => email)
|
2011-04-16 16:13:17 +10:00
|
|
|
|
2010-11-20 15:54:01 +01:00
|
|
|
assert_equal email, user.email
|
|
|
|
user.save!
|
|
|
|
assert_equal email.downcase, user.email
|
|
|
|
end
|
2011-04-16 16:13:17 +10:00
|
|
|
|
2009-09-17 09:24:33 -03:00
|
|
|
test 'should respond to password and password confirmation' do
|
|
|
|
user = new_user
|
|
|
|
assert user.respond_to?(:password)
|
|
|
|
assert user.respond_to?(:password_confirmation)
|
|
|
|
end
|
|
|
|
|
2010-09-25 16:08:46 +02:00
|
|
|
test 'should generate encrypted password while setting password' do
|
2009-11-24 23:19:12 -02:00
|
|
|
user = new_user
|
|
|
|
assert_present user.encrypted_password
|
2009-10-08 20:57:10 -03:00
|
|
|
end
|
|
|
|
|
2010-11-11 22:51:19 +01:00
|
|
|
test 'allow authenticatable_salt to work even with nil encrypted password' do
|
|
|
|
user = User.new
|
|
|
|
user.encrypted_password = nil
|
|
|
|
assert_nil user.authenticatable_salt
|
|
|
|
end
|
|
|
|
|
2009-11-24 23:19:12 -02:00
|
|
|
test 'should not generate encrypted password if password is blank' do
|
|
|
|
assert_blank new_user(:password => nil).encrypted_password
|
|
|
|
assert_blank new_user(:password => '').encrypted_password
|
2009-09-17 09:24:33 -03:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should encrypt password again if password has changed' do
|
|
|
|
user = create_user
|
|
|
|
encrypted_password = user.encrypted_password
|
2009-10-08 20:57:10 -03:00
|
|
|
user.password = user.password_confirmation = 'new_password'
|
|
|
|
user.save!
|
2009-09-17 09:24:33 -03:00
|
|
|
assert_not_equal encrypted_password, user.encrypted_password
|
|
|
|
end
|
|
|
|
|
2009-09-17 11:06:46 -03:00
|
|
|
test 'should test for a valid password' do
|
|
|
|
user = create_user
|
2009-09-18 12:03:41 -03:00
|
|
|
assert user.valid_password?('123456')
|
|
|
|
assert_not user.valid_password?('654321')
|
2009-09-17 11:06:46 -03:00
|
|
|
end
|
|
|
|
|
2011-04-16 16:13:17 +10:00
|
|
|
test 'should not raise error with an empty password' do
|
|
|
|
user = create_user
|
|
|
|
user.encrypted_password = ''
|
|
|
|
assert_nothing_raised { user.valid_password?('123456') }
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should be an invalid password if the user has an empty password' do
|
|
|
|
user = create_user
|
|
|
|
user.encrypted_password = ''
|
|
|
|
assert_not user.valid_password?('654321')
|
|
|
|
end
|
|
|
|
|
2010-02-08 20:38:47 +01:00
|
|
|
test 'should respond to current password' do
|
|
|
|
assert new_user.respond_to?(:current_password)
|
2009-12-14 23:25:45 -02:00
|
|
|
end
|
|
|
|
|
2010-02-08 23:14:03 +01:00
|
|
|
test 'should update password with valid current password' do
|
2009-12-14 22:55:55 -02:00
|
|
|
user = create_user
|
2010-02-08 20:38:47 +01:00
|
|
|
assert user.update_with_password(:current_password => '123456',
|
2009-12-14 22:55:55 -02:00
|
|
|
:password => 'pass321', :password_confirmation => 'pass321')
|
|
|
|
assert user.reload.valid_password?('pass321')
|
|
|
|
end
|
|
|
|
|
2010-02-08 23:14:03 +01:00
|
|
|
test 'should add an error to current password when it is invalid' do
|
2009-12-14 22:55:55 -02:00
|
|
|
user = create_user
|
2010-02-08 20:38:47 +01:00
|
|
|
assert_not user.update_with_password(:current_password => 'other',
|
2009-12-14 22:55:55 -02:00
|
|
|
:password => 'pass321', :password_confirmation => 'pass321')
|
|
|
|
assert user.reload.valid_password?('123456')
|
2010-02-16 17:00:36 +01:00
|
|
|
assert_match "is invalid", user.errors[:current_password].join
|
2009-12-14 22:55:55 -02:00
|
|
|
end
|
|
|
|
|
2010-02-08 23:14:03 +01:00
|
|
|
test 'should add an error to current password when it is blank' do
|
|
|
|
user = create_user
|
|
|
|
assert_not user.update_with_password(:password => 'pass321',
|
|
|
|
:password_confirmation => 'pass321')
|
|
|
|
assert user.reload.valid_password?('123456')
|
2010-02-16 17:00:36 +01:00
|
|
|
assert_match "can't be blank", user.errors[:current_password].join
|
2010-02-08 23:14:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should ignore password and its confirmation if they are blank' do
|
|
|
|
user = create_user
|
2011-04-18 09:14:56 +08:00
|
|
|
assert user.update_with_password(:current_password => '123456', :email => "new@example.com")
|
|
|
|
assert_equal "new@example.com", user.email
|
2010-02-08 23:14:03 +01:00
|
|
|
end
|
|
|
|
|
2009-12-14 22:55:55 -02:00
|
|
|
test 'should not update password with invalid confirmation' do
|
|
|
|
user = create_user
|
2010-02-08 20:38:47 +01:00
|
|
|
assert_not user.update_with_password(:current_password => '123456',
|
2009-12-14 22:55:55 -02:00
|
|
|
:password => 'pass321', :password_confirmation => 'other')
|
|
|
|
assert user.reload.valid_password?('123456')
|
|
|
|
end
|
2010-02-08 23:14:03 +01:00
|
|
|
|
|
|
|
test 'should clean up password fields on failure' do
|
|
|
|
user = create_user
|
|
|
|
assert_not user.update_with_password(:current_password => '123456',
|
|
|
|
:password => 'pass321', :password_confirmation => 'other')
|
|
|
|
assert user.password.blank?
|
|
|
|
assert user.password_confirmation.blank?
|
|
|
|
end
|
2011-04-16 12:52:59 +02:00
|
|
|
|
|
|
|
test 'downcase_keys with validation' do
|
|
|
|
user = User.create(:email => "HEllO@example.com", :password => "123456")
|
|
|
|
user = User.create(:email => "HEllO@example.com", :password => "123456")
|
|
|
|
assert !user.valid?
|
|
|
|
end
|
2009-09-17 09:24:33 -03:00
|
|
|
end
|