Skip to content

Commit

Permalink
Merge remote-tracking branch 'dkastner/omniauth-1.0' into omniauth-1.0.0
Browse files Browse the repository at this point in the history
Conflicts:
	test/omniauth/url_helpers_test.rb
  • Loading branch information
Denis Kiselev committed Nov 7, 2011
2 parents 58d61c2 + c9902f3 commit ea9e8c0
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 27 deletions.
5 changes: 3 additions & 2 deletions Gemfile
Expand Up @@ -3,8 +3,9 @@ source "http://rubygems.org"
gemspec

gem "rails", "~> 3.1.0"
gem "oa-oauth", '~> 0.2.0', :require => "omniauth/oauth"
gem "oa-openid", '~> 0.2.0', :require => "omniauth/openid"
gem 'omniauth', '~> 1.0.0.pr2'
gem 'omniauth-contrib', :git => 'http://github.com/intridea/omniauth-contrib.git' #'~> 1.0.0.pr2'
gem 'omniauth-oauth2'

gem "rdoc"

Expand Down
9 changes: 5 additions & 4 deletions lib/devise/omniauth.rb
@@ -1,12 +1,13 @@
begin
require "omniauth/core"
require "omniauth"
require 'omniauth/version'
rescue LoadError => e
warn "Could not load 'omniauth/core'. Please ensure you have the oa-core gem installed and listed in your Gemfile."
warn "Could not load 'omniauth'. Please ensure you have the omniauth gem >= 1.0.0 installed and listed in your Gemfile."
raise
end

unless OmniAuth.config.respond_to? :test_mode
raise "You are using an old OmniAuth version, please ensure you have 0.2.0.beta version or later installed."
unless OmniAuth::VERSION =~ /^1\./
raise "You are using an old OmniAuth version, please ensure you have 1.0.0.pr2 version or later installed."
end

# Clean up the default path_prefix. It will be automatically set by Devise.
Expand Down
32 changes: 27 additions & 5 deletions lib/devise/omniauth/config.rb
Expand Up @@ -2,23 +2,45 @@ module Devise
module OmniAuth
class Config
attr_accessor :strategy
attr_reader :args
attr_reader :args, :options, :provider

def initialize(provider, args)
@provider = provider
@args = args
@strategy = nil
@options = @args.last.is_a?(Hash) ? @args.last : {}
end

# open_id strategy can have configurable name
def strategy_name
options = @args.last.is_a?(Hash) && @args.last
options && options[:name] ? options[:name] : @provider
options[:name] || @provider
end

def strategy_class
::OmniAuth::Strategies.const_get("#{::OmniAuth::Utils.camelize(@provider.to_s)}")
find_strategy || require_strategy
end

def find_strategy
::OmniAuth.strategies.find do |strategy_class|
strategy_class.to_s =~ /#{::OmniAuth::Utils.camelize(strategy_name)}$/ ||
strategy_class.default_options[:name] == strategy_name
end
end

def require_strategy
if [:facebook, :github, :twitter].include?(provider.to_sym)
require "omniauth/strategies/#{provider}"
elsif options[:require]
require options[:require]
else
require "omniauth-#{provider}"
end
find_strategy || autoload_strategy
end

def autoload_strategy
::OmniAuth::Strategies.const_get(::OmniAuth::Utils.camelize(provider.to_s))
end
end
end
end
end
15 changes: 6 additions & 9 deletions test/integration/omniauthable_test.rb
@@ -1,5 +1,6 @@
require 'test_helper'


class OmniauthableIntegrationTest < ActionController::IntegrationTest
FACEBOOK_INFO = {
"id" => '12345',
Expand All @@ -12,14 +13,6 @@ class OmniauthableIntegrationTest < ActionController::IntegrationTest

setup do
OmniAuth.config.test_mode = true
stub_facebook!
end

teardown do
OmniAuth.config.test_mode = false
end

def stub_facebook!
OmniAuth.config.mock_auth[:facebook] = {
"uid" => '12345',
"provider" => 'facebook',
Expand All @@ -29,6 +22,10 @@ def stub_facebook!
}
end

teardown do
OmniAuth.config.test_mode = false
end

def stub_action!(name)
Users::OmniauthCallbacksController.class_eval do
alias_method :__old_facebook, :facebook
Expand Down Expand Up @@ -128,7 +125,7 @@ def stub_action!(name)
OmniAuth.config.mock_auth[:facebook] = :invalid_credentials

visit "/users/sign_in"
click_link "Sign in with facebook"
click_link "Sign in with Facebook"

assert_current_url "/users/sign_in"
assert_contain 'Could not authorize you from Facebook because "Invalid credentials".'
Expand Down
58 changes: 58 additions & 0 deletions test/omniauth/config_test.rb
@@ -0,0 +1,58 @@
require 'test_helper'

class OmniAuthConfigTest < ActiveSupport::TestCase
def self.context(name, &block)
instance_eval(&block)
end

setup do
$: << File.dirname(__FILE__)
end

context 'Devise::OmniAuth::Config#strategy_name' do
test 'returns provider if no options given' do
config = Devise::OmniAuth::Config.new :facebook, [{}]
assert_equal :facebook, config.strategy_name
end
test 'returns provider if no name option given' do
config = Devise::OmniAuth::Config.new :facebook, [{ :other => :option }]
assert_equal :facebook, config.strategy_name
end
test 'returns name option' do
config = Devise::OmniAuth::Config.new :facebook, [{ :name => :github }]
assert_equal :github, config.strategy_name
end
end

context 'Devise::OmniAuth::Config#strategy_class' do
test "finds contrib strategies" do
config = Devise::OmniAuth::Config.new :facebook, [{}]
assert_equal OmniAuth::Strategies::Facebook, config.strategy_class
end
test "finds the strategy in OmniAuth's list by name" do
NamedTestStrategy = Class.new
NamedTestStrategy.send :include, OmniAuth::Strategy
NamedTestStrategy.option :name, :the_one

config = Devise::OmniAuth::Config.new :the_one, [{}]
assert_equal NamedTestStrategy, config.strategy_class
end
test "finds the strategy in OmniAuth's list by class name" do
UnNamedTestStrategy = Class.new
UnNamedTestStrategy.send :include, OmniAuth::Strategy

config = Devise::OmniAuth::Config.new :un_named_test_strategy, [{}]
assert_equal UnNamedTestStrategy, config.strategy_class
end
test 'attempts to load an as-yet not loaded plugin' do
config = Devise::OmniAuth::Config.new :my_strategy, [{}]
config_class = config.strategy_class
assert_equal MyStrategy, config_class
end
test 'allows the user to define a custom require path' do
config = Devise::OmniAuth::Config.new :my_other_strategy, [{:require => 'my_other_strategy'}]
config_class = config.strategy_class
assert_equal MyOtherStrategy, config_class
end
end
end
5 changes: 5 additions & 0 deletions test/omniauth/my_other_strategy.rb
@@ -0,0 +1,5 @@
require 'omniauth'

class MyOtherStrategy
include OmniAuth::Strategy
end
5 changes: 5 additions & 0 deletions test/omniauth/omniauth-my_strategy.rb
@@ -0,0 +1,5 @@
require 'omniauth'

class MyStrategy
include OmniAuth::Strategy
end
7 changes: 4 additions & 3 deletions test/rails_app/config/initializers/devise.rb
@@ -1,3 +1,5 @@
require 'omniauth-contrib'

# Use this hook to configure devise mailer, warden hooks and so forth. The first
# four configuration values can also be set straight in your models.
Devise.setup do |config|
Expand Down Expand Up @@ -176,9 +178,8 @@
# config.sign_out_via = :get

# ==> OmniAuth
config.omniauth :facebook, 'APP_ID', 'APP_SECRET', :scope => 'email,offline_access'
config.omniauth :open_id
config.omniauth :open_id, :name => 'google', :identifier => 'https://www.google.com/accounts/o8/id'
config.omniauth :facebook, 'APP_ID', 'APP_SECRET'
config.omniauth :facebook, 'APP_ID', 'APP_SECRET', :name => 'other_facebook'

# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
Expand Down
8 changes: 4 additions & 4 deletions test/routes_test.rb
Expand Up @@ -96,10 +96,10 @@ class DefaultRoutingTest < ActionController::TestCase
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

# 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
# named strategy
assert_recognizes({:controller => 'users/omniauth_callbacks', :action => 'other_facebook'}, {:path => 'users/auth/other_facebook/callback', :method => :get})
assert_recognizes({:controller => 'users/omniauth_callbacks', :action => 'other_facebook'}, {:path => 'users/auth/other_facebook/callback', :method => :post})
assert_named_route "/users/auth/other_facebook/callback", :user_omniauth_callback_path, :other_facebook

assert_raise ActionController::RoutingError do
assert_recognizes({:controller => 'ysers/omniauth_callbacks', :action => 'twitter'}, {:path => 'users/auth/twitter/callback', :method => :get})
Expand Down

0 comments on commit ea9e8c0

Please sign in to comment.