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

Use _url routes instead of _path

This commit is contained in:
Kamil Giszczak 2014-03-31 18:32:45 +02:00
parent d019e1ed1a
commit a38f2f96b5
3 changed files with 44 additions and 6 deletions

View file

@ -96,15 +96,15 @@ module Devise
request.referrer
end
path || scope_path
path || scope_url
else
scope_path
scope_url
end
end
def scope_path
def scope_url
opts = {}
route = :"new_#{scope}_session_path"
route = :"new_#{scope}_session_url"
opts[:format] = request_format unless skip_format?
config = Rails.application.config
@ -114,8 +114,8 @@ module Devise
if context.respond_to?(route)
context.send(route, opts)
elsif respond_to?(:root_path)
root_path(opts)
elsif respond_to?(:root_url)
root_url(opts)
else
"/"
end

View file

@ -8,6 +8,16 @@ class FailureTest < ActiveSupport::TestCase
end
end
class FailureWithSubdomain < RootFailureApp
routes = ActionDispatch::Routing::RouteSet.new
routes.draw do
root to: 'foo#bar', constraints: { subdomain: 'sub' }
end
include routes.url_helpers
end
class FailureWithI18nOptions < Devise::FailureApp
def i18n_options(options)
options.merge(name: 'Steve')
@ -42,6 +52,13 @@ class FailureTest < ActiveSupport::TestCase
assert_equal 'http://test.host/users/sign_in', @response.second['Location']
end
test 'returns to the default redirect location considering subdomain' do
call_failure('warden.options' => { scope: :subdomain_user })
assert_equal 302, @response.first
assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
assert_equal 'http://sub.test.host/subdomain_users/sign_in', @response.second['Location']
end
test 'returns to the default redirect location for wildcard requests' do
call_failure 'action_dispatch.request.formats' => nil, 'HTTP_ACCEPT' => '*/*'
assert_equal 302, @response.first
@ -57,6 +74,15 @@ class FailureTest < ActiveSupport::TestCase
end
end
test 'returns to the root path considering subdomain if no session path is available' do
swap Devise, router_name: :fake_app do
call_failure app: FailureWithSubdomain
assert_equal 302, @response.first
assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
assert_equal 'http://sub.test.host/', @response.second['Location']
end
end
if Rails.application.config.respond_to?(:relative_url_root)
test 'returns to the default redirect location considering the relative url root' do
swap Rails.application.config, relative_url_root: "/sample" do
@ -65,6 +91,14 @@ class FailureTest < ActiveSupport::TestCase
assert_equal 'http://test.host/sample/users/sign_in', @response.second['Location']
end
end
test 'returns to the default redirect location considering the relative url root and subdomain' do
swap Rails.application.config, relative_url_root: "/sample" do
call_failure('warden.options' => { scope: :subdomain_user })
assert_equal 302, @response.first
assert_equal 'http://sub.test.host/sample/subdomain_users/sign_in', @response.second['Location']
end
end
end
test 'uses the proxy failure message as symbol' do

View file

@ -66,6 +66,10 @@ Rails.application.routes.draw do
devise_for :homebase_admin, class_name: "Admin", path: "homebase"
end
constraints(subdomain: 'sub') do
devise_for :subdomain_users, class_name: "User", only: [:sessions]
end
devise_for :skip_admin, class_name: "Admin", skip: :all
# Routes for format=false testing