2010-03-26 06:27:19 -04:00
|
|
|
require 'test_helper'
|
2009-11-22 19:19:29 -05:00
|
|
|
require 'ostruct'
|
2009-10-22 18:12:00 -04:00
|
|
|
|
|
|
|
class FailureTest < ActiveSupport::TestCase
|
2010-04-01 11:30:55 -04:00
|
|
|
def self.context(name, &block)
|
|
|
|
instance_eval(&block)
|
|
|
|
end
|
2009-10-22 18:12:00 -04:00
|
|
|
|
|
|
|
def call_failure(env_params={})
|
2010-02-16 11:00:36 -05:00
|
|
|
env = {
|
|
|
|
'REQUEST_URI' => 'http://test.host/',
|
2010-02-19 03:52:04 -05:00
|
|
|
'HTTP_HOST' => 'test.host',
|
2010-02-16 11:00:36 -05:00
|
|
|
'REQUEST_METHOD' => 'GET',
|
2010-05-16 13:13:20 -04:00
|
|
|
'warden.options' => { :scope => :user },
|
2010-04-01 11:30:55 -04:00
|
|
|
'rack.session' => {},
|
2011-02-15 04:26:28 -05:00
|
|
|
'action_dispatch.request.formats' => Array(env_params.delete('formats') || Mime::HTML),
|
2010-04-01 11:30:55 -04:00
|
|
|
'rack.input' => "",
|
|
|
|
'warden' => OpenStruct.new(:message => nil)
|
2010-02-16 11:00:36 -05:00
|
|
|
}.merge!(env_params)
|
2010-08-31 17:55:25 -04:00
|
|
|
|
2010-04-03 05:43:31 -04:00
|
|
|
@response = Devise::FailureApp.call(env).to_a
|
|
|
|
@request = ActionDispatch::Request.new(env)
|
2009-10-22 18:12:00 -04:00
|
|
|
end
|
|
|
|
|
2010-04-01 11:30:55 -04:00
|
|
|
context 'When redirecting' do
|
|
|
|
test 'return 302 status' do
|
2010-04-03 05:43:31 -04:00
|
|
|
call_failure
|
|
|
|
assert_equal 302, @response.first
|
2010-04-01 11:30:55 -04:00
|
|
|
end
|
2009-10-22 18:12:00 -04:00
|
|
|
|
2010-11-08 16:45:42 -05:00
|
|
|
test 'return 302 status for wildcard requests' do
|
|
|
|
call_failure 'action_dispatch.request.formats' => nil, 'HTTP_ACCEPT' => '*/*'
|
|
|
|
assert_equal 302, @response.first
|
|
|
|
end
|
|
|
|
|
2010-04-01 11:30:55 -04:00
|
|
|
test 'return to the default redirect location' do
|
2010-04-03 05:43:31 -04:00
|
|
|
call_failure
|
|
|
|
assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
|
|
|
|
assert_equal 'http://test.host/users/sign_in', @response.second['Location']
|
2011-04-29 07:22:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'return to the default redirect location for wildcard requests' do
|
|
|
|
call_failure 'action_dispatch.request.formats' => nil, 'HTTP_ACCEPT' => '*/*'
|
|
|
|
assert_equal 'http://test.host/users/sign_in', @response.second['Location']
|
2010-04-01 11:30:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'uses the proxy failure message as symbol' do
|
2010-04-03 05:43:31 -04:00
|
|
|
call_failure('warden' => OpenStruct.new(:message => :test))
|
|
|
|
assert_equal 'test', @request.flash[:alert]
|
|
|
|
assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
|
2010-04-01 11:30:55 -04:00
|
|
|
end
|
2009-10-22 18:12:00 -04:00
|
|
|
|
2010-04-01 11:30:55 -04:00
|
|
|
test 'uses the proxy failure message as string' do
|
2010-04-03 05:43:31 -04:00
|
|
|
call_failure('warden' => OpenStruct.new(:message => 'Hello world'))
|
|
|
|
assert_equal 'Hello world', @request.flash[:alert]
|
|
|
|
assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
|
2010-04-01 11:30:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'set content type to default text/html' do
|
2010-04-03 05:43:31 -04:00
|
|
|
call_failure
|
|
|
|
assert_equal 'text/html; charset=utf-8', @response.second['Content-Type']
|
2010-04-01 11:30:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'setup a default message' do
|
2010-04-03 05:43:31 -04:00
|
|
|
call_failure
|
|
|
|
assert_match /You are being/, @response.last.body
|
|
|
|
assert_match /redirected/, @response.last.body
|
|
|
|
assert_match /users\/sign_in/, @response.last.body
|
2010-04-01 11:30:55 -04:00
|
|
|
end
|
2010-05-16 13:13:20 -04:00
|
|
|
|
|
|
|
test 'works for any navigational format' do
|
|
|
|
swap Devise, :navigational_formats => [:xml] do
|
|
|
|
call_failure('formats' => :xml)
|
|
|
|
assert_equal 302, @response.first
|
|
|
|
end
|
|
|
|
end
|
2011-01-07 19:40:21 -05:00
|
|
|
|
|
|
|
test 'redirects the correct format if it is a non-html format request' do
|
|
|
|
swap Devise, :navigational_formats => [:js] do
|
|
|
|
call_failure('formats' => :js)
|
|
|
|
assert_equal 'http://test.host/users/sign_in.js', @response.second["Location"]
|
|
|
|
end
|
|
|
|
end
|
2009-11-22 19:19:29 -05:00
|
|
|
end
|
|
|
|
|
2010-04-01 11:30:55 -04:00
|
|
|
context 'For HTTP request' do
|
|
|
|
test 'return 401 status' do
|
2010-05-16 13:13:20 -04:00
|
|
|
call_failure('formats' => :xml)
|
2010-04-03 05:43:31 -04:00
|
|
|
assert_equal 401, @response.first
|
2010-04-01 11:30:55 -04:00
|
|
|
end
|
|
|
|
|
2011-04-17 12:06:29 -04:00
|
|
|
test 'return appropriate body for xml' do
|
|
|
|
call_failure('formats' => :xml)
|
|
|
|
result = %(<?xml version="1.0" encoding="UTF-8"?>\n<errors>\n <error>You need to sign in or sign up before continuing.</error>\n</errors>\n)
|
|
|
|
assert_equal result, @response.last.body
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'return appropriate body for json' do
|
|
|
|
call_failure('formats' => :json)
|
|
|
|
result = %({"error":"You need to sign in or sign up before continuing."})
|
|
|
|
assert_equal result, @response.last.body
|
|
|
|
end
|
|
|
|
|
2010-11-09 17:30:53 -05:00
|
|
|
test 'return 401 status for unknown formats' do
|
|
|
|
call_failure 'formats' => []
|
|
|
|
assert_equal 401, @response.first
|
|
|
|
end
|
|
|
|
|
2010-08-31 17:55:25 -04:00
|
|
|
test 'return WWW-authenticate headers if model allows' do
|
2010-05-16 13:13:20 -04:00
|
|
|
call_failure('formats' => :xml)
|
2010-04-03 05:43:31 -04:00
|
|
|
assert_equal 'Basic realm="Application"', @response.second["WWW-Authenticate"]
|
2010-04-01 11:30:55 -04:00
|
|
|
end
|
|
|
|
|
2010-08-31 17:55:25 -04:00
|
|
|
test 'does not return WWW-authenticate headers if model does not allow' do
|
|
|
|
swap Devise, :http_authenticatable => false do
|
|
|
|
call_failure('formats' => :xml)
|
|
|
|
assert_nil @response.second["WWW-Authenticate"]
|
2010-08-31 11:44:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-31 17:55:25 -04:00
|
|
|
test 'works for any non navigational format' do
|
|
|
|
swap Devise, :navigational_formats => [] do
|
|
|
|
call_failure('formats' => :html)
|
2010-07-07 15:32:13 -04:00
|
|
|
assert_equal 401, @response.first
|
2010-07-07 15:05:30 -04:00
|
|
|
end
|
|
|
|
end
|
2010-08-31 17:55:25 -04:00
|
|
|
|
|
|
|
test 'uses the failure message as response body' do
|
2010-05-16 13:13:20 -04:00
|
|
|
call_failure('formats' => :xml, 'warden' => OpenStruct.new(:message => :invalid))
|
|
|
|
assert_match '<error>Invalid email or password.</error>', @response.third.body
|
|
|
|
end
|
|
|
|
|
2010-08-31 17:55:25 -04:00
|
|
|
context 'on ajax call' do
|
|
|
|
context 'when http_authenticatable_on_xhr is false' do
|
|
|
|
test 'dont return 401 with navigational formats' do
|
|
|
|
swap Devise, :http_authenticatable_on_xhr => false do
|
|
|
|
call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
|
|
|
assert_equal 302, @response.first
|
|
|
|
assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'dont return 401 with non navigational formats' do
|
|
|
|
swap Devise, :http_authenticatable_on_xhr => false do
|
|
|
|
call_failure('formats' => :json, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
|
|
|
assert_equal 302, @response.first
|
2011-01-07 19:40:21 -05:00
|
|
|
assert_equal 'http://test.host/users/sign_in.json', @response.second["Location"]
|
2010-08-31 17:55:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when http_authenticatable_on_xhr is true' do
|
|
|
|
test 'return 401' do
|
|
|
|
swap Devise, :http_authenticatable_on_xhr => true do
|
|
|
|
call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
|
|
|
assert_equal 401, @response.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'skip WWW-Authenticate header' do
|
|
|
|
swap Devise, :http_authenticatable_on_xhr => true do
|
|
|
|
call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
|
|
|
assert_nil @response.second['WWW-Authenticate']
|
|
|
|
end
|
|
|
|
end
|
2010-05-16 13:13:20 -04:00
|
|
|
end
|
|
|
|
end
|
2009-10-22 18:12:00 -04:00
|
|
|
end
|
|
|
|
|
2010-04-01 11:30:55 -04:00
|
|
|
context 'With recall' do
|
2010-12-22 13:17:11 -05:00
|
|
|
test 'calls the original controller if invalid email or password' do
|
2010-04-01 11:30:55 -04:00
|
|
|
env = {
|
2010-09-25 05:21:51 -04:00
|
|
|
"warden.options" => { :recall => "devise/sessions#new", :attempted_path => "/users/sign_in" },
|
2010-07-05 19:27:20 -04:00
|
|
|
"devise.mapping" => Devise.mappings[:user],
|
2010-04-01 11:30:55 -04:00
|
|
|
"warden" => stub_everything
|
|
|
|
}
|
2010-04-03 05:43:31 -04:00
|
|
|
call_failure(env)
|
|
|
|
assert @response.third.body.include?('<h2>Sign in</h2>')
|
|
|
|
assert @response.third.body.include?('Invalid email or password.')
|
2010-04-01 11:30:55 -04:00
|
|
|
end
|
2010-12-22 13:17:11 -05:00
|
|
|
|
|
|
|
test 'calls the original controller if not confirmed email' do
|
|
|
|
env = {
|
|
|
|
"warden.options" => { :recall => "devise/sessions#new", :attempted_path => "/users/sign_in", :message => :unconfirmed },
|
|
|
|
"devise.mapping" => Devise.mappings[:user],
|
|
|
|
"warden" => stub_everything
|
|
|
|
}
|
|
|
|
call_failure(env)
|
|
|
|
assert @response.third.body.include?('<h2>Sign in</h2>')
|
|
|
|
assert @response.third.body.include?('You have to confirm your account before continuing.')
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'calls the original controller if inactive account' do
|
|
|
|
env = {
|
|
|
|
"warden.options" => { :recall => "devise/sessions#new", :attempted_path => "/users/sign_in", :message => :inactive },
|
|
|
|
"devise.mapping" => Devise.mappings[:user],
|
|
|
|
"warden" => stub_everything
|
|
|
|
}
|
|
|
|
call_failure(env)
|
|
|
|
assert @response.third.body.include?('<h2>Sign in</h2>')
|
|
|
|
assert @response.third.body.include?('Your account was not activated yet.')
|
|
|
|
end
|
2009-10-22 18:12:00 -04:00
|
|
|
end
|
|
|
|
end
|