From fb86f772e72a25c7243d7ad020684e610f79e1c1 Mon Sep 17 00:00:00 2001 From: RStankov Date: Sun, 25 Jul 2010 20:56:00 +0300 Subject: [PATCH] make User#send_unlock_instructions to require all authentication_keys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- lib/devise/models/lockable.rb | 2 +- test/models/lockable_test.rb | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/devise/models/lockable.rb b/lib/devise/models/lockable.rb index 8e1bc904..3570e9eb 100644 --- a/lib/devise/models/lockable.rb +++ b/lib/devise/models/lockable.rb @@ -132,7 +132,7 @@ module Devise # with an email not found error. # Options must contain the user email def send_unlock_instructions(attributes={}) - lockable = find_or_initialize_with_error_by(:email, attributes[:email], :not_found) + lockable = find_or_initialize_with_errors(authentication_keys, attributes, :not_found) lockable.resend_unlock_token if lockable.persisted? lockable end diff --git a/test/models/lockable_test.rb b/test/models/lockable_test.rb index 7abbd667..6f09c41e 100644 --- a/test/models/lockable_test.rb +++ b/test/models/lockable_test.rb @@ -175,7 +175,24 @@ class LockableTest < ActiveSupport::TestCase test 'should add error to new user email if no email was found' do unlock_user = User.send_unlock_instructions(:email => "invalid@email.com") - assert_equal 'not found', unlock_user.errors[:email].join + assert_equal 'not found', unlock_user.errors[:base].join + end + + test 'should find a user to send unlock instructions by authentication_keys' do + swap Devise, :authentication_keys => [:username, :email] do + user = create_user + unlock_user = User.send_unlock_instructions(:email => user.email, :username => user.username) + assert_equal unlock_user, user + end + end + + test 'should require all authentication_keys' do + swap Devise, :authentication_keys => [:username, :email] do + user = create_user + unlock_user = User.send_unlock_instructions(:email => user.email) + assert_not unlock_user.persisted? + assert_equal "can't be blank", unlock_user.errors[:username].join + end end test 'should not be able to send instructions if the user is not locked' do