2010-03-26 06:27:19 -04:00
|
|
|
require 'test_helper'
|
2010-01-23 21:38:52 -05:00
|
|
|
|
|
|
|
class TokenAuthenticatableTest < ActiveSupport::TestCase
|
|
|
|
|
|
|
|
test 'should reset authentication token' do
|
|
|
|
user = new_user
|
2010-02-02 07:21:00 -05:00
|
|
|
user.reset_authentication_token
|
2010-01-23 21:38:52 -05:00
|
|
|
previous_token = user.authentication_token
|
2010-02-02 07:21:00 -05:00
|
|
|
user.reset_authentication_token
|
2010-01-23 21:38:52 -05:00
|
|
|
assert_not_equal previous_token, user.authentication_token
|
|
|
|
end
|
|
|
|
|
2010-02-02 07:21:00 -05:00
|
|
|
test 'should ensure authentication token' do
|
|
|
|
user = new_user
|
|
|
|
user.ensure_authentication_token
|
|
|
|
previous_token = user.authentication_token
|
|
|
|
user.ensure_authentication_token
|
|
|
|
assert_equal previous_token, user.authentication_token
|
|
|
|
end
|
|
|
|
|
2010-01-23 21:38:52 -05:00
|
|
|
test 'should authenticate a valid user with authentication token and return it' do
|
|
|
|
user = create_user
|
2010-04-06 07:26:56 -04:00
|
|
|
user.ensure_authentication_token!
|
2010-02-02 07:21:00 -05:00
|
|
|
user.confirm!
|
2010-04-06 07:26:56 -04:00
|
|
|
authenticated_user = User.find_for_token_authentication(:auth_token => user.authentication_token)
|
2010-01-23 21:38:52 -05:00
|
|
|
assert_equal authenticated_user, user
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should return nil when authenticating an invalid user by authentication token' do
|
|
|
|
user = create_user
|
2010-04-06 07:26:56 -04:00
|
|
|
user.ensure_authentication_token!
|
2010-02-02 07:21:00 -05:00
|
|
|
user.confirm!
|
2010-04-06 07:26:56 -04:00
|
|
|
authenticated_user = User.find_for_token_authentication(:auth_token => user.authentication_token.reverse)
|
2010-01-23 21:38:52 -05:00
|
|
|
assert_nil authenticated_user
|
|
|
|
end
|
|
|
|
|
2011-03-07 18:54:55 -05:00
|
|
|
test 'should not be subject to injection' do
|
2011-03-11 14:46:08 -05:00
|
|
|
user1 = create_user
|
|
|
|
user1.ensure_authentication_token!
|
|
|
|
user1.confirm!
|
2011-03-07 18:54:55 -05:00
|
|
|
|
2011-03-11 14:46:08 -05:00
|
|
|
user2 = create_user
|
|
|
|
user2.ensure_authentication_token!
|
|
|
|
user2.confirm!
|
2011-03-07 18:54:55 -05:00
|
|
|
|
2011-03-11 14:46:08 -05:00
|
|
|
user = User.find_for_token_authentication(:auth_token => {'$ne' => user1.authentication_token})
|
|
|
|
assert_nil user
|
2011-03-07 18:54:55 -05:00
|
|
|
end
|
2012-02-17 12:50:15 -05:00
|
|
|
|
|
|
|
test 'required_fields should contain the fields that Devise uses' do
|
2012-02-20 07:12:26 -05:00
|
|
|
assert_same_content Devise::Models::TokenAuthenticatable.required_fields(User), [
|
2012-02-17 12:50:15 -05:00
|
|
|
:authentication_token
|
|
|
|
]
|
|
|
|
end
|
2011-02-25 16:17:14 -05:00
|
|
|
end
|