1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00
heartcombo--devise/test/models/lockable_test.rb

189 lines
5.4 KiB
Ruby
Raw Normal View History

require 'test_helper'
2009-12-30 12:19:33 -05:00
class LockableTest < ActiveSupport::TestCase
def setup
setup_mailer
end
2009-12-30 12:19:33 -05:00
test "should respect maximum attempts configuration" do
user = create_user
user.confirm!
2009-12-30 12:19:33 -05:00
swap Devise, :maximum_attempts => 2 do
3.times { user.valid_for_authentication?{ false } }
assert user.reload.access_locked?
2009-12-30 12:19:33 -05:00
end
end
test "should clear failed_attempts on successfull validation" do
2009-12-30 12:19:33 -05:00
user = create_user
user.confirm!
user.valid_for_authentication?{ false }
2009-12-30 12:19:33 -05:00
assert_equal 1, user.reload.failed_attempts
user.valid_for_authentication?{ true }
2009-12-30 12:19:33 -05:00
assert_equal 0, user.reload.failed_attempts
end
2010-03-31 05:54:11 -04:00
test "should not touch failed_attempts if lock_strategy is none" do
user = create_user
user.confirm!
2010-03-31 05:54:11 -04:00
swap Devise, :lock_strategy => :none, :maximum_attempts => 2 do
3.times { user.valid_for_authentication?{ false } }
assert !user.access_locked?
assert_equal 0, user.failed_attempts
end
end
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
assert_not user.access_locked?
user.lock_access!
assert user.access_locked?
2009-12-30 12:19:33 -05:00
end
test "active? should be the opposite of locked?" do
user = create_user
user.confirm!
assert user.active?
user.lock_access!
2009-12-30 12:19:33 -05:00
assert_not user.active?
end
test "should unlock an user by cleaning locked_at, falied_attempts and unlock_token" do
user = create_user
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
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
test 'should not unlock an unlocked user' do
2009-12-30 12:19:33 -05:00
user = create_user
assert_not user.unlock_access!
assert_match "was not locked", user.errors[:email].join
2009-12-30 12:19:33 -05:00
end
test "new user should not be locked and should have zero failed_attempts" do
assert_not 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
swap Devise, :unlock_in => 3.hours do
user = new_user
user.locked_at = 2.hours.ago
assert user.access_locked?
2009-12-30 12:19:33 -05:00
Devise.unlock_in = 1.hour
assert_not 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
swap Devise, :unlock_strategy => :email do
user = new_user
user.locked_at = 2.hours.ago
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
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
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
swap Devise, :unlock_strategy => :time do
user = create_user
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
swap Devise, :unlock_strategy => :email do
user = create_user
assert_email_sent do
user.lock_access!
2009-12-30 12:19:33 -05:00
end
end
end
test "should not send email with unlock instructions when :email is not an unlock strategy" do
swap Devise, :unlock_strategy => :time do
user = create_user
assert_email_not_sent do
user.lock_access!
2009-12-30 12:19:33 -05:00
end
end
end
test 'should find and unlock an user automatically' do
user = create_user
user.lock_access!
locked_user = User.unlock_access_by_token(user.unlock_token)
2009-12-30 12:19:33 -05:00
assert_equal locked_user, user
assert_not user.reload.access_locked?
2009-12-30 12:19:33 -05:00
end
test 'should return a new record with errors when a invalid token is given' do
locked_user = User.unlock_access_by_token('invalid_token')
assert_not locked_user.persisted?
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
locked_user = User.unlock_access_by_token('')
assert_not locked_user.persisted?
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
user.lock_access!
2009-12-30 12:19:33 -05:00
unlock_user = User.send_unlock_instructions(:email => user.email)
assert_equal unlock_user, user
end
test 'should return a new user if no email was found' do
unlock_user = User.send_unlock_instructions(:email => "invalid@email.com")
assert_not 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
unlock_user = User.send_unlock_instructions(:email => "invalid@email.com")
assert_equal 'not found', unlock_user.errors[:email].join
2009-12-30 12:19:33 -05:00
end
test 'should not be able to send instructions if the user is not locked' do
user = create_user
assert_not user.resend_unlock_token
assert_not user.access_locked?
assert_equal 'was not locked', user.errors[:email].join
2009-12-30 12:19:33 -05:00
end
end