2009-10-10 17:07:25 -04:00
|
|
|
require 'test/test_helper'
|
2009-09-18 09:20:45 -04:00
|
|
|
|
|
|
|
class RecoverableTest < ActiveSupport::TestCase
|
|
|
|
|
|
|
|
def setup
|
|
|
|
setup_mailer
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should reset password and password confirmation from params' do
|
2009-10-07 22:50:42 -04:00
|
|
|
user = create_user
|
|
|
|
user.reset_password('123456789', '987654321')
|
|
|
|
assert_equal '123456789', user.password
|
|
|
|
assert_equal '987654321', user.password_confirmation
|
2009-09-18 09:20:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should reset password and save the record' do
|
2009-10-07 22:50:42 -04:00
|
|
|
assert create_user.reset_password!('123456789', '123456789')
|
2009-09-18 09:20:45 -04:00
|
|
|
end
|
|
|
|
|
2009-10-15 16:36:44 -04:00
|
|
|
test 'should clear perishable token while reseting the password' do
|
|
|
|
user = create_user
|
|
|
|
assert_present user.perishable_token
|
|
|
|
user.reset_password!('123456789', '123456789')
|
|
|
|
assert_nil user.perishable_token
|
|
|
|
end
|
|
|
|
|
2009-09-18 09:20:45 -04:00
|
|
|
test 'should not reset password with invalid data' do
|
2009-10-07 22:50:42 -04:00
|
|
|
user = create_user
|
|
|
|
user.stubs(:valid?).returns(false)
|
|
|
|
assert_not user.reset_password!('123456789', '987654321')
|
2009-09-18 09:20:45 -04:00
|
|
|
end
|
|
|
|
|
2009-09-18 09:47:12 -04:00
|
|
|
test 'should reset perishable token and send instructions by email' do
|
2009-10-07 22:50:42 -04:00
|
|
|
user = create_user
|
2009-09-18 09:47:12 -04:00
|
|
|
assert_email_sent do
|
2009-10-07 22:50:42 -04:00
|
|
|
token = user.perishable_token
|
|
|
|
user.send_reset_password_instructions
|
|
|
|
assert_not_equal token, user.perishable_token
|
2009-09-18 09:47:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-18 09:20:45 -04:00
|
|
|
test 'should find a user to send instructions by email' do
|
2009-10-07 22:50:42 -04:00
|
|
|
user = create_user
|
|
|
|
reset_password_user = User.send_reset_password_instructions(:email => user.email)
|
2009-09-18 09:20:45 -04:00
|
|
|
assert_not_nil reset_password_user
|
2009-10-07 22:50:42 -04:00
|
|
|
assert_equal reset_password_user, user
|
2009-09-18 09:20:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should return a new user if no email was found' do
|
2009-10-07 21:33:45 -04:00
|
|
|
reset_password_user = User.send_reset_password_instructions(:email => "invalid@email.com")
|
2009-09-18 09:20:45 -04:00
|
|
|
assert_not_nil reset_password_user
|
|
|
|
assert reset_password_user.new_record?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should add error to new user email if no email was found' do
|
2009-10-07 21:33:45 -04:00
|
|
|
reset_password_user = User.send_reset_password_instructions(:email => "invalid@email.com")
|
2009-09-18 09:20:45 -04:00
|
|
|
assert reset_password_user.errors[:email]
|
|
|
|
assert_equal 'not found', reset_password_user.errors[:email]
|
|
|
|
end
|
|
|
|
|
2009-09-18 09:47:12 -04:00
|
|
|
test 'should reset perishable token before send the reset instructions email' do
|
2009-10-07 22:50:42 -04:00
|
|
|
user = create_user
|
|
|
|
token = user.perishable_token
|
|
|
|
reset_password_user = User.send_reset_password_instructions(:email => user.email)
|
|
|
|
assert_not_equal token, user.reload.perishable_token
|
2009-09-18 09:20:45 -04:00
|
|
|
end
|
|
|
|
|
2009-09-18 09:47:12 -04:00
|
|
|
test 'should send email instructions to the user reset it\'s password' do
|
2009-10-07 22:50:42 -04:00
|
|
|
user = create_user
|
2009-09-18 09:20:45 -04:00
|
|
|
assert_email_sent do
|
2009-10-07 22:50:42 -04:00
|
|
|
User.send_reset_password_instructions(:email => user.email)
|
2009-09-18 09:20:45 -04:00
|
|
|
end
|
|
|
|
end
|
2009-09-18 09:47:12 -04:00
|
|
|
|
|
|
|
test 'should find a user to reset it\'s password based on perishable_token' do
|
2009-10-07 22:50:42 -04:00
|
|
|
user = create_user
|
|
|
|
reset_password_user = User.reset_password!(:perishable_token => user.perishable_token)
|
2009-09-18 09:47:12 -04:00
|
|
|
assert_not_nil reset_password_user
|
2009-10-07 22:50:42 -04:00
|
|
|
assert_equal reset_password_user, user
|
2009-09-18 09:47:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should return a new user when trying to reset it\'s password if no perishable_token is found' do
|
2009-10-07 22:01:45 -04:00
|
|
|
reset_password_user = User.reset_password!(:perishable_token => 'invalid_token')
|
2009-09-18 09:47:12 -04:00
|
|
|
assert_not_nil reset_password_user
|
|
|
|
assert reset_password_user.new_record?
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should add error to new user email if no perishable token was found' do
|
2009-10-07 22:01:45 -04:00
|
|
|
reset_password_user = User.reset_password!(:perishable_token => "invalid_token")
|
2009-09-18 09:47:12 -04:00
|
|
|
assert reset_password_user.errors[:perishable_token]
|
|
|
|
assert_equal 'invalid confirmation', reset_password_user.errors[:perishable_token]
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should reset successfully user password given the new password and confirmation' do
|
2009-10-07 22:50:42 -04:00
|
|
|
user = create_user
|
|
|
|
old_password = user.password
|
2009-10-07 22:01:45 -04:00
|
|
|
reset_password_user = User.reset_password!(
|
2009-10-07 22:50:42 -04:00
|
|
|
:perishable_token => user.perishable_token,
|
2009-10-07 21:33:45 -04:00
|
|
|
:password => 'new_password',
|
|
|
|
:password_confirmation => 'new_password'
|
|
|
|
)
|
2009-10-07 22:50:42 -04:00
|
|
|
user.reload
|
|
|
|
assert_not user.valid_password?(old_password)
|
|
|
|
assert user.valid_password?('new_password')
|
2009-09-18 09:47:12 -04:00
|
|
|
end
|
2009-09-18 09:20:45 -04:00
|
|
|
end
|