2010-03-26 06:27:19 -04:00
|
|
|
require 'test_helper'
|
2009-12-30 12:19:33 -05:00
|
|
|
|
|
|
|
class LockableTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
setup_mailer
|
|
|
|
end
|
2010-01-23 20:40:32 -05:00
|
|
|
|
2009-12-30 12:19:33 -05:00
|
|
|
test "should respect maximum attempts configuration" do
|
|
|
|
user = create_user
|
2015-04-19 10:41:06 -04:00
|
|
|
user.confirm
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, maximum_attempts: 2 do
|
2014-01-06 23:51:45 -05:00
|
|
|
2.times { user.valid_for_authentication?{ false } }
|
2010-03-10 10:13:54 -05:00
|
|
|
assert user.reload.access_locked?
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
|
|
|
end
|
2010-01-23 20:40:32 -05:00
|
|
|
|
2015-09-30 18:31:49 -04:00
|
|
|
test "should increment failed_attempts on successful validation if the user is already locked" do
|
2011-12-29 13:25:16 -05:00
|
|
|
user = create_user
|
2015-04-19 10:41:06 -04:00
|
|
|
user.confirm
|
2011-12-29 13:25:16 -05:00
|
|
|
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, maximum_attempts: 2 do
|
2014-01-06 23:51:45 -05:00
|
|
|
2.times { user.valid_for_authentication?{ false } }
|
2011-12-29 13:25:16 -05:00
|
|
|
assert user.reload.access_locked?
|
|
|
|
end
|
|
|
|
|
|
|
|
user.valid_for_authentication?{ true }
|
2014-01-06 23:51:45 -05:00
|
|
|
assert_equal 3, user.reload.failed_attempts
|
2011-12-29 13:25:16 -05:00
|
|
|
end
|
|
|
|
|
2010-03-31 05:54:11 -04:00
|
|
|
test "should not touch failed_attempts if lock_strategy is none" do
|
|
|
|
user = create_user
|
2015-04-19 10:41:06 -04:00
|
|
|
user.confirm
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, lock_strategy: :none, maximum_attempts: 2 do
|
2010-03-31 05:54:11 -04:00
|
|
|
3.times { user.valid_for_authentication?{ false } }
|
|
|
|
assert !user.access_locked?
|
|
|
|
assert_equal 0, user.failed_attempts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-29 17:44:47 -04:00
|
|
|
test 'should be valid for authentication with a unlocked user' do
|
|
|
|
user = create_user
|
|
|
|
user.lock_access!
|
|
|
|
user.unlock_access!
|
|
|
|
assert user.valid_for_authentication?{ true }
|
|
|
|
end
|
|
|
|
|
2010-03-28 17:09:28 -04:00
|
|
|
test "should verify whether a user is locked or not" do
|
2009-12-30 12:19:33 -05:00
|
|
|
user = create_user
|
2016-05-03 12:57:10 -04:00
|
|
|
refute user.access_locked?
|
2010-03-10 10:13:54 -05:00
|
|
|
user.lock_access!
|
|
|
|
assert user.access_locked?
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
|
|
|
|
2011-03-25 10:39:08 -04:00
|
|
|
test "active_for_authentication? should be the opposite of locked?" do
|
2009-12-30 12:19:33 -05:00
|
|
|
user = create_user
|
2015-04-19 10:41:06 -04:00
|
|
|
user.confirm
|
2011-03-25 10:39:08 -04:00
|
|
|
assert user.active_for_authentication?
|
2010-03-10 10:13:54 -05:00
|
|
|
user.lock_access!
|
2016-05-03 12:57:10 -04:00
|
|
|
refute user.active_for_authentication?
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
|
|
|
|
2013-04-18 00:54:38 -04:00
|
|
|
test "should unlock a user by cleaning locked_at, failed_attempts and unlock_token" do
|
2009-12-30 12:19:33 -05:00
|
|
|
user = create_user
|
2010-03-10 10:13:54 -05:00
|
|
|
user.lock_access!
|
2009-12-30 12:19:33 -05:00
|
|
|
assert_not_nil user.reload.locked_at
|
|
|
|
assert_not_nil user.reload.unlock_token
|
2010-02-16 11:00:36 -05:00
|
|
|
|
2010-03-10 10:13:54 -05:00
|
|
|
user.unlock_access!
|
2009-12-30 12:19:33 -05:00
|
|
|
assert_nil user.reload.locked_at
|
|
|
|
assert_nil user.reload.unlock_token
|
2010-06-08 19:26:54 -04:00
|
|
|
assert_equal 0, user.reload.failed_attempts
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
2010-01-23 20:40:32 -05:00
|
|
|
|
2009-12-30 12:19:33 -05:00
|
|
|
test "new user should not be locked and should have zero failed_attempts" do
|
2016-05-03 12:57:10 -04:00
|
|
|
refute new_user.access_locked?
|
2009-12-30 12:19:33 -05:00
|
|
|
assert_equal 0, create_user.failed_attempts
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should unlock user after unlock_in period" do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_in: 3.hours do
|
2009-12-30 12:19:33 -05:00
|
|
|
user = new_user
|
|
|
|
user.locked_at = 2.hours.ago
|
2010-03-10 10:13:54 -05:00
|
|
|
assert user.access_locked?
|
2009-12-30 12:19:33 -05:00
|
|
|
|
|
|
|
Devise.unlock_in = 1.hour
|
2016-05-03 12:57:10 -04:00
|
|
|
refute user.access_locked?
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should not unlock in 'unlock_in' if :time unlock strategy is not set" do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_strategy: :email do
|
2009-12-30 12:19:33 -05:00
|
|
|
user = new_user
|
|
|
|
user.locked_at = 2.hours.ago
|
2010-03-10 10:13:54 -05:00
|
|
|
assert user.access_locked?
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should set unlock_token when locking" do
|
|
|
|
user = create_user
|
|
|
|
assert_nil user.unlock_token
|
2010-03-10 10:13:54 -05:00
|
|
|
user.lock_access!
|
2009-12-30 12:19:33 -05:00
|
|
|
assert_not_nil user.unlock_token
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should never generate the same unlock token for different users" do
|
|
|
|
unlock_tokens = []
|
|
|
|
3.times do
|
|
|
|
user = create_user
|
2010-03-10 10:13:54 -05:00
|
|
|
user.lock_access!
|
2009-12-30 12:19:33 -05:00
|
|
|
token = user.unlock_token
|
|
|
|
assert !unlock_tokens.include?(token)
|
|
|
|
unlock_tokens << token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should not generate unlock_token when :email is not an unlock strategy" do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_strategy: :time do
|
2009-12-30 12:19:33 -05:00
|
|
|
user = create_user
|
2010-03-10 10:13:54 -05:00
|
|
|
user.lock_access!
|
2009-12-30 12:19:33 -05:00
|
|
|
assert_nil user.unlock_token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should send email with unlock instructions when :email is an unlock strategy" do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_strategy: :email do
|
2009-12-30 12:19:33 -05:00
|
|
|
user = create_user
|
|
|
|
assert_email_sent do
|
2010-03-10 10:13:54 -05:00
|
|
|
user.lock_access!
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-23 19:46:15 -05:00
|
|
|
test "doesn't send email when you pass option send_instructions to false" do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_strategy: :email do
|
2013-12-23 19:46:15 -05:00
|
|
|
user = create_user
|
|
|
|
assert_email_not_sent do
|
|
|
|
user.lock_access! send_instructions: false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "sends email when you pass options other than send_instructions" do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_strategy: :email do
|
2013-12-23 19:46:15 -05:00
|
|
|
user = create_user
|
|
|
|
assert_email_sent do
|
|
|
|
user.lock_access! foo: :bar, bar: :foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-30 12:19:33 -05:00
|
|
|
test "should not send email with unlock instructions when :email is not an unlock strategy" do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_strategy: :time do
|
2009-12-30 12:19:33 -05:00
|
|
|
user = create_user
|
|
|
|
assert_email_not_sent do
|
2010-03-10 10:13:54 -05:00
|
|
|
user.lock_access!
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-05 12:56:07 -04:00
|
|
|
test 'should find and unlock a user automatically based on raw token' do
|
|
|
|
user = create_user
|
|
|
|
raw = user.send_unlock_instructions
|
|
|
|
locked_user = User.unlock_access_by_token(raw)
|
|
|
|
assert_equal locked_user, user
|
2016-05-03 12:57:10 -04:00
|
|
|
refute user.reload.access_locked?
|
2013-08-05 12:56:07 -04:00
|
|
|
end
|
|
|
|
|
2009-12-30 12:19:33 -05:00
|
|
|
test 'should return a new record with errors when a invalid token is given' do
|
2010-03-10 10:13:54 -05:00
|
|
|
locked_user = User.unlock_access_by_token('invalid_token')
|
2016-05-03 12:57:10 -04:00
|
|
|
refute locked_user.persisted?
|
2010-02-16 11:00:36 -05:00
|
|
|
assert_equal "is invalid", locked_user.errors[:unlock_token].join
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should return a new record with errors when a blank token is given' do
|
2010-03-10 10:13:54 -05:00
|
|
|
locked_user = User.unlock_access_by_token('')
|
2016-05-03 12:57:10 -04:00
|
|
|
refute locked_user.persisted?
|
2010-02-16 11:00:36 -05:00
|
|
|
assert_equal "can't be blank", locked_user.errors[:unlock_token].join
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should find a user to send unlock instructions' do
|
|
|
|
user = create_user
|
2010-03-10 10:13:54 -05:00
|
|
|
user.lock_access!
|
2014-02-25 11:42:55 -05:00
|
|
|
unlock_user = User.send_unlock_instructions(email: user.email)
|
2009-12-30 12:19:33 -05:00
|
|
|
assert_equal unlock_user, user
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should return a new user if no email was found' do
|
2014-02-25 11:42:55 -05:00
|
|
|
unlock_user = User.send_unlock_instructions(email: "invalid@example.com")
|
2016-05-03 12:57:10 -04:00
|
|
|
refute unlock_user.persisted?
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should add error to new user email if no email was found' do
|
2014-02-25 11:42:55 -05:00
|
|
|
unlock_user = User.send_unlock_instructions(email: "invalid@example.com")
|
2010-09-21 06:05:17 -04:00
|
|
|
assert_equal 'not found', unlock_user.errors[:email].join
|
2010-07-25 13:56:00 -04:00
|
|
|
end
|
2010-09-21 06:05:17 -04:00
|
|
|
|
2010-07-25 13:56:00 -04:00
|
|
|
test 'should find a user to send unlock instructions by authentication_keys' do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, authentication_keys: [:username, :email] do
|
2010-07-25 13:56:00 -04:00
|
|
|
user = create_user
|
2014-02-25 11:42:55 -05:00
|
|
|
unlock_user = User.send_unlock_instructions(email: user.email, username: user.username)
|
2010-07-25 13:56:00 -04:00
|
|
|
assert_equal unlock_user, user
|
|
|
|
end
|
|
|
|
end
|
2010-09-21 06:05:17 -04:00
|
|
|
|
2010-12-29 12:15:43 -05:00
|
|
|
test 'should require all unlock_keys' do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_keys: [:username, :email] do
|
2013-05-14 07:58:20 -04:00
|
|
|
user = create_user
|
2014-02-25 11:42:55 -05:00
|
|
|
unlock_user = User.send_unlock_instructions(email: user.email)
|
2016-05-03 12:57:10 -04:00
|
|
|
refute unlock_user.persisted?
|
2013-05-14 07:58:20 -04:00
|
|
|
assert_equal "can't be blank", unlock_user.errors[:username].join
|
|
|
|
end
|
2010-12-29 12:15:43 -05:00
|
|
|
end
|
|
|
|
|
2009-12-30 12:19:33 -05:00
|
|
|
test 'should not be able to send instructions if the user is not locked' do
|
|
|
|
user = create_user
|
2016-05-03 12:57:10 -04:00
|
|
|
refute user.resend_unlock_instructions
|
|
|
|
refute user.access_locked?
|
2010-02-16 11:00:36 -05:00
|
|
|
assert_equal 'was not locked', user.errors[:email].join
|
2009-12-30 12:19:33 -05:00
|
|
|
end
|
2011-02-25 15:59:27 -05:00
|
|
|
|
2013-05-14 07:58:20 -04:00
|
|
|
test 'should not be able to send instructions if the user if not locked and have username as unlock key' do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_keys: [:username] do
|
2013-05-14 07:58:20 -04:00
|
|
|
user = create_user
|
2016-05-03 12:57:10 -04:00
|
|
|
refute user.resend_unlock_instructions
|
|
|
|
refute user.access_locked?
|
2013-05-14 07:58:20 -04:00
|
|
|
assert_equal 'was not locked', user.errors[:username].join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-25 15:59:27 -05:00
|
|
|
test 'should unlock account if lock has expired and increase attempts on failure' do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_in: 1.minute do
|
2011-02-25 15:59:27 -05:00
|
|
|
user = create_user
|
2015-04-19 10:41:06 -04:00
|
|
|
user.confirm
|
2011-02-25 15:59:27 -05:00
|
|
|
|
|
|
|
user.failed_attempts = 2
|
|
|
|
user.locked_at = 2.minutes.ago
|
|
|
|
|
|
|
|
user.valid_for_authentication? { false }
|
|
|
|
assert_equal 1, user.failed_attempts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should unlock account if lock has expired on success' do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_in: 1.minute do
|
2011-02-25 15:59:27 -05:00
|
|
|
user = create_user
|
2015-04-19 10:41:06 -04:00
|
|
|
user.confirm
|
2011-02-25 15:59:27 -05:00
|
|
|
|
|
|
|
user.failed_attempts = 2
|
|
|
|
user.locked_at = 2.minutes.ago
|
|
|
|
|
|
|
|
user.valid_for_authentication? { true }
|
|
|
|
assert_equal 0, user.failed_attempts
|
|
|
|
assert_nil user.locked_at
|
|
|
|
end
|
|
|
|
end
|
2012-02-20 06:20:19 -05:00
|
|
|
|
2012-03-13 14:50:13 -04:00
|
|
|
test 'required_fields should contain the all the fields when all the strategies are enabled' do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_strategy: :both do
|
|
|
|
swap Devise, lock_strategy: :failed_attempts do
|
2016-05-03 13:18:35 -04:00
|
|
|
assert_equal Devise::Models::Lockable.required_fields(User), [
|
2012-03-13 14:50:13 -04:00
|
|
|
:failed_attempts,
|
2012-06-22 21:31:56 -04:00
|
|
|
:locked_at,
|
2012-03-13 14:50:13 -04:00
|
|
|
:unlock_token
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-22 21:31:56 -04:00
|
|
|
test 'required_fields should contain only failed_attempts and locked_at when the strategies are time and failed_attempts are enabled' do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_strategy: :time do
|
|
|
|
swap Devise, lock_strategy: :failed_attempts do
|
2016-05-03 13:18:35 -04:00
|
|
|
assert_equal Devise::Models::Lockable.required_fields(User), [
|
2012-03-13 14:50:13 -04:00
|
|
|
:failed_attempts,
|
2012-06-22 21:31:56 -04:00
|
|
|
:locked_at
|
2012-03-13 14:50:13 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'required_fields should contain only failed_attempts and unlock_token when the strategies are token and failed_attempts are enabled' do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, unlock_strategy: :email do
|
|
|
|
swap Devise, lock_strategy: :failed_attempts do
|
2016-05-03 13:18:35 -04:00
|
|
|
assert_equal Devise::Models::Lockable.required_fields(User), [
|
2012-03-13 14:50:13 -04:00
|
|
|
:failed_attempts,
|
|
|
|
:unlock_token
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-10-25 17:51:02 -04:00
|
|
|
|
|
|
|
test 'should not return a locked unauthenticated message if in paranoid mode' do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, paranoid: :true do
|
2012-10-25 17:51:02 -04:00
|
|
|
user = create_user
|
|
|
|
user.failed_attempts = Devise.maximum_attempts + 1
|
|
|
|
user.lock_access!
|
|
|
|
|
|
|
|
assert_equal :invalid, user.unauthenticated_message
|
|
|
|
end
|
|
|
|
end
|
2013-10-11 19:22:43 -04:00
|
|
|
|
|
|
|
test 'should return last attempt message if user made next-to-last attempt of password entering' do
|
2014-10-02 17:50:48 -04:00
|
|
|
swap Devise, last_attempt_warning: true, lock_strategy: :failed_attempts do
|
2014-10-02 17:42:46 -04:00
|
|
|
user = create_user
|
|
|
|
user.failed_attempts = Devise.maximum_attempts - 2
|
|
|
|
assert_equal :invalid, user.unauthenticated_message
|
2013-10-11 19:22:43 -04:00
|
|
|
|
2014-10-02 17:42:46 -04:00
|
|
|
user.failed_attempts = Devise.maximum_attempts - 1
|
|
|
|
assert_equal :last_attempt, user.unauthenticated_message
|
2013-10-21 17:43:56 -04:00
|
|
|
|
2014-10-02 17:42:46 -04:00
|
|
|
user.failed_attempts = Devise.maximum_attempts
|
|
|
|
assert_equal :locked, user.unauthenticated_message
|
2013-10-11 19:22:43 -04:00
|
|
|
end
|
|
|
|
end
|
2014-05-15 17:57:18 -04:00
|
|
|
|
2014-10-02 17:50:48 -04:00
|
|
|
test 'should not return last attempt message if last_attempt_warning is disabled' do
|
|
|
|
swap Devise, last_attempt_warning: false, lock_strategy: :failed_attempts do
|
|
|
|
user = create_user
|
|
|
|
user.failed_attempts = Devise.maximum_attempts - 1
|
|
|
|
assert_equal :invalid, user.unauthenticated_message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-15 17:57:18 -04:00
|
|
|
test 'should return locked message if user was programatically locked' do
|
2014-05-15 18:02:11 -04:00
|
|
|
user = create_user
|
|
|
|
user.lock_access!
|
|
|
|
assert_equal :locked, user.unauthenticated_message
|
2014-05-15 17:57:18 -04:00
|
|
|
end
|
2016-04-29 17:31:33 -04:00
|
|
|
|
|
|
|
test 'unlock_strategy_enabled? should return true for both, email, and time strategies if :both is used' do
|
|
|
|
swap Devise, unlock_strategy: :both do
|
|
|
|
user = create_user
|
|
|
|
assert_equal true, user.unlock_strategy_enabled?(:both)
|
|
|
|
assert_equal true, user.unlock_strategy_enabled?(:time)
|
|
|
|
assert_equal true, user.unlock_strategy_enabled?(:email)
|
|
|
|
assert_equal false, user.unlock_strategy_enabled?(:none)
|
|
|
|
assert_equal false, user.unlock_strategy_enabled?(:an_undefined_strategy)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'unlock_strategy_enabled? should return true only for the configured strategy' do
|
|
|
|
swap Devise, unlock_strategy: :email do
|
|
|
|
user = create_user
|
|
|
|
assert_equal false, user.unlock_strategy_enabled?(:both)
|
|
|
|
assert_equal false, user.unlock_strategy_enabled?(:time)
|
|
|
|
assert_equal true, user.unlock_strategy_enabled?(:email)
|
|
|
|
assert_equal false, user.unlock_strategy_enabled?(:none)
|
|
|
|
assert_equal false, user.unlock_strategy_enabled?(:an_undefined_strategy)
|
|
|
|
end
|
|
|
|
end
|
2012-03-13 14:50:13 -04:00
|
|
|
end
|