2010-01-24 02:38:52 +00:00
|
|
|
require 'test/test_helper'
|
|
|
|
|
|
|
|
class TokenAuthenticationTest < ActionController::IntegrationTest
|
|
|
|
|
|
|
|
test 'sign in user should authenticate with valid authentication token and proper authentication token key' do
|
2010-02-02 12:21:00 +00:00
|
|
|
swap Devise, :token_authentication_key => :secret_token do
|
|
|
|
sign_in_as_new_user_with_token(:auth_token_key => :secret_token)
|
2010-01-24 02:38:52 +00:00
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_template 'users/index'
|
|
|
|
assert_contain 'Welcome'
|
|
|
|
assert warden.authenticated?(:user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'user signing in with valid authentication token - but improper authentication token key - return to sign in form with error message' do
|
2010-02-02 12:21:00 +00:00
|
|
|
swap Devise, :token_authentication_key => :donald_duck_token do
|
|
|
|
sign_in_as_new_user_with_token(:auth_token_key => :secret_token)
|
|
|
|
assert_redirected_to new_user_session_path(:unauthenticated => true)
|
|
|
|
follow_redirect!
|
2010-01-24 02:38:52 +00:00
|
|
|
|
2010-02-02 12:21:00 +00:00
|
|
|
assert_contain 'You need to sign in or sign up before continuing'
|
|
|
|
assert_contain 'Sign in'
|
|
|
|
assert_not warden.authenticated?(:user)
|
|
|
|
end
|
2010-01-24 02:38:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'user signing in with invalid authentication token should return to sign in form with error message' do
|
|
|
|
store_translations :en, :devise => {:sessions => {:invalid_token => 'LOL, that was not a single character correct.'}} do
|
|
|
|
sign_in_as_new_user_with_token(:auth_token => '*** INVALID TOKEN ***')
|
|
|
|
assert_redirected_to new_user_session_path(:invalid_token => true)
|
|
|
|
follow_redirect!
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_contain 'LOL, that was not a single character correct.'
|
|
|
|
assert_contain 'Sign in'
|
|
|
|
assert_not warden.authenticated?(:user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def sign_in_as_new_user_with_token(options = {}, &block)
|
2010-02-02 12:21:00 +00:00
|
|
|
options[:auth_token_key] ||= Devise.token_authentication_key
|
|
|
|
options[:auth_token] ||= VALID_AUTHENTICATION_TOKEN
|
|
|
|
|
2010-01-24 02:38:52 +00:00
|
|
|
user = create_user(options)
|
|
|
|
user.authentication_token = VALID_AUTHENTICATION_TOKEN
|
|
|
|
user.save
|
2010-02-02 12:21:00 +00:00
|
|
|
|
|
|
|
visit users_path(options[:auth_token_key].to_sym => options[:auth_token])
|
2010-01-24 02:38:52 +00:00
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|