Merge pull request #3985 from plataformatec/lm-omniauth-route-helpers

Do not use the dynamic `:action` segment on Omniauth routes.
This commit is contained in:
Lucas Mazza 2016-03-07 11:50:35 -03:00
commit 76f76249e7
4 changed files with 20 additions and 24 deletions

View File

@ -4,14 +4,14 @@ module Devise
def self.define_helpers(mapping)
end
def omniauth_authorize_path(resource_or_scope, *args)
def omniauth_authorize_path(resource_or_scope, provider, *args)
scope = Devise::Mapping.find_scope!(resource_or_scope)
_devise_route_context.send("#{scope}_omniauth_authorize_path", *args)
_devise_route_context.send("#{scope}_#{provider}_omniauth_authorize_path", *args)
end
def omniauth_callback_path(resource_or_scope, *args)
def omniauth_callback_path(resource_or_scope, provider, *args)
scope = Devise::Mapping.find_scope!(resource_or_scope)
_devise_route_context.send("#{scope}_omniauth_callback_path", *args)
_devise_route_context.send("#{scope}_#{provider}_omniauth_callback_path", *args)
end
end
end

View File

@ -441,19 +441,17 @@ ERROR
set_omniauth_path_prefix!(path_prefix)
providers = Regexp.union(mapping.to.omniauth_providers.map(&:to_s))
mapping.to.omniauth_providers.each do |provider|
match "#{path_prefix}/#{provider}",
to: "#{controllers[:omniauth_callbacks]}#passthru",
as: "#{provider}_omniauth_authorize",
via: [:get, :post]
match "#{path_prefix}/:provider",
constraints: { provider: providers },
to: "#{controllers[:omniauth_callbacks]}#passthru",
as: :omniauth_authorize,
via: [:get, :post]
match "#{path_prefix}/:action/callback",
constraints: { action: providers },
to: "#{controllers[:omniauth_callbacks]}#:action",
as: :omniauth_callback,
via: [:get, :post]
match "#{path_prefix}/#{provider}/callback",
to: "#{controllers[:omniauth_callbacks]}##{provider}",
as: "#{provider}_omniauth_callback",
via: [:get, :post]
end
ensure
@scope = current_scope
end

View File

@ -1,23 +1,21 @@
require 'test_helper'
class OmniAuthRoutesTest < ActionController::TestCase
ExpectedUrlGeneratiorError = ActionController::UrlGenerationError
tests ApplicationController
def assert_path(action, provider, with_param=true)
# Resource param
assert_equal @controller.send(action, :user, provider),
@controller.send("user_#{action}", provider)
@controller.send("user_#{provider}_#{action}")
# With an object
assert_equal @controller.send(action, User.new, provider),
@controller.send("user_#{action}", provider)
@controller.send("user_#{provider}_#{action}")
if with_param
# Default url params
assert_equal @controller.send(action, :user, provider, param: 123),
@controller.send("user_#{action}", provider, param: 123)
@controller.send("user_#{provider}_#{action}", param: 123)
end
end
@ -32,7 +30,7 @@ class OmniAuthRoutesTest < ActionController::TestCase
test 'should generate authorization path' do
assert_match "/users/auth/facebook", @controller.omniauth_authorize_path(:user, :facebook)
assert_raise ExpectedUrlGeneratiorError do
assert_raise NoMethodError do
@controller.omniauth_authorize_path(:user, :github)
end
end

View File

@ -96,12 +96,12 @@ class DefaultRoutingTest < ActionController::TestCase
test 'map omniauth callbacks' do
assert_recognizes({controller: 'users/omniauth_callbacks', action: 'facebook'}, {path: 'users/auth/facebook/callback', method: :get})
assert_recognizes({controller: 'users/omniauth_callbacks', action: 'facebook'}, {path: 'users/auth/facebook/callback', method: :post})
assert_named_route "/users/auth/facebook/callback", :user_omniauth_callback_path, :facebook
assert_named_route "/users/auth/facebook/callback", :user_facebook_omniauth_callback_path
# named open_id
assert_recognizes({controller: 'users/omniauth_callbacks', action: 'google'}, {path: 'users/auth/google/callback', method: :get})
assert_recognizes({controller: 'users/omniauth_callbacks', action: 'google'}, {path: 'users/auth/google/callback', method: :post})
assert_named_route "/users/auth/google/callback", :user_omniauth_callback_path, :google
assert_named_route "/users/auth/google/callback", :user_google_omniauth_callback_path
assert_raise ExpectedRoutingError do
assert_recognizes({controller: 'ysers/omniauth_callbacks', action: 'twitter'}, {path: 'users/auth/twitter/callback', method: :get})