mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Refactoring recoverable tests to access database only when needed.
This commit is contained in:
parent
98fa7bf336
commit
095f7026cb
2 changed files with 31 additions and 24 deletions
|
@ -4,8 +4,8 @@ module Devise
|
|||
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
extend ClassMethods
|
||||
include ::Devise::Perishable
|
||||
extend ClassMethods
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -3,37 +3,40 @@ require 'test_helper'
|
|||
class RecoverableTest < ActiveSupport::TestCase
|
||||
|
||||
def setup
|
||||
@user = create_user
|
||||
setup_mailer
|
||||
end
|
||||
|
||||
test 'should reset password and password confirmation from params' do
|
||||
@user.reset_password('123456789', '987654321')
|
||||
assert_equal '123456789', @user.password
|
||||
assert_equal '987654321', @user.password_confirmation
|
||||
user = create_user
|
||||
user.reset_password('123456789', '987654321')
|
||||
assert_equal '123456789', user.password
|
||||
assert_equal '987654321', user.password_confirmation
|
||||
end
|
||||
|
||||
test 'should reset password and save the record' do
|
||||
assert @user.reset_password!('123456789', '123456789')
|
||||
assert create_user.reset_password!('123456789', '123456789')
|
||||
end
|
||||
|
||||
test 'should not reset password with invalid data' do
|
||||
@user.stubs(:valid?).returns(false)
|
||||
assert_not @user.reset_password!('123456789', '987654321')
|
||||
user = create_user
|
||||
user.stubs(:valid?).returns(false)
|
||||
assert_not user.reset_password!('123456789', '987654321')
|
||||
end
|
||||
|
||||
test 'should reset perishable token and send instructions by email' do
|
||||
user = create_user
|
||||
assert_email_sent do
|
||||
token = @user.perishable_token
|
||||
@user.send_reset_password_instructions
|
||||
assert_not_equal token, @user.perishable_token
|
||||
token = user.perishable_token
|
||||
user.send_reset_password_instructions
|
||||
assert_not_equal token, user.perishable_token
|
||||
end
|
||||
end
|
||||
|
||||
test 'should find a user to send instructions by email' do
|
||||
reset_password_user = User.send_reset_password_instructions(:email => @user.email)
|
||||
user = create_user
|
||||
reset_password_user = User.send_reset_password_instructions(:email => user.email)
|
||||
assert_not_nil reset_password_user
|
||||
assert_equal reset_password_user, @user
|
||||
assert_equal reset_password_user, user
|
||||
end
|
||||
|
||||
test 'should return a new user if no email was found' do
|
||||
|
@ -49,21 +52,24 @@ class RecoverableTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
test 'should reset perishable token before send the reset instructions email' do
|
||||
token = @user.perishable_token
|
||||
reset_password_user = User.send_reset_password_instructions(:email => @user.email)
|
||||
assert_not_equal token, @user.reload.perishable_token
|
||||
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
|
||||
end
|
||||
|
||||
test 'should send email instructions to the user reset it\'s password' do
|
||||
user = create_user
|
||||
assert_email_sent do
|
||||
User.send_reset_password_instructions(:email => @user.email)
|
||||
User.send_reset_password_instructions(:email => user.email)
|
||||
end
|
||||
end
|
||||
|
||||
test 'should find a user to reset it\'s password based on perishable_token' do
|
||||
reset_password_user = User.reset_password!(:perishable_token => @user.perishable_token)
|
||||
user = create_user
|
||||
reset_password_user = User.reset_password!(:perishable_token => user.perishable_token)
|
||||
assert_not_nil reset_password_user
|
||||
assert_equal reset_password_user, @user
|
||||
assert_equal reset_password_user, user
|
||||
end
|
||||
|
||||
test 'should return a new user when trying to reset it\'s password if no perishable_token is found' do
|
||||
|
@ -79,14 +85,15 @@ class RecoverableTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
test 'should reset successfully user password given the new password and confirmation' do
|
||||
old_password = @user.password
|
||||
user = create_user
|
||||
old_password = user.password
|
||||
reset_password_user = User.reset_password!(
|
||||
:perishable_token => @user.perishable_token,
|
||||
:perishable_token => user.perishable_token,
|
||||
:password => 'new_password',
|
||||
:password_confirmation => 'new_password'
|
||||
)
|
||||
@user.reload
|
||||
assert_not @user.valid_password?(old_password)
|
||||
assert @user.valid_password?('new_password')
|
||||
user.reload
|
||||
assert_not user.valid_password?(old_password)
|
||||
assert user.valid_password?('new_password')
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue