Cleaning up test helpers related to OmniAuth.

Developers should rely on OmniAuth's new testing API. Check
https://github.com/intridea/omniauth/wiki/Integration-Testing
for more details.
This commit is contained in:
Vinicius Baggio 2011-02-24 16:50:22 -03:00
parent b5e289c9a8
commit 352edc024b
8 changed files with 98 additions and 115 deletions

View File

@ -3,8 +3,8 @@ source "http://rubygems.org"
gemspec
gem "rails", "~> 3.0.4"
gem "oa-oauth", :require => "omniauth/oauth"
gem "oa-openid", :require => "omniauth/openid"
gem "oa-oauth", '~> 0.2.0.beta1', :require => "omniauth/oauth"
gem "oa-openid", '~> 0.2.0.beta1', :require => "omniauth/openid"
group :test do
gem "webrat", "0.7.2", :require => false

View File

@ -49,7 +49,7 @@ GEM
columnize (0.3.2)
erubis (2.6.6)
abstract (>= 1.0.0)
faraday (0.5.5)
faraday (0.5.6)
addressable (~> 2.2.4)
multipart-post (~> 1.1.0)
rack (>= 1.1.0, < 2)
@ -75,16 +75,16 @@ GEM
nokogiri (1.4.4)
nokogiri (1.4.4-java)
weakling (>= 0.0.3)
oa-core (0.1.6)
oa-core (0.2.0.beta4)
rack (~> 1.1)
oa-oauth (0.1.6)
oa-oauth (0.2.0.beta4)
multi_json (~> 0.0.2)
nokogiri (~> 1.4.2)
oa-core (= 0.1.6)
oa-core (= 0.2.0.beta4)
oauth (~> 0.4.0)
oauth2 (~> 0.1.0)
oa-openid (0.1.6)
oa-core (= 0.1.6)
oauth2 (~> 0.1.1)
oa-openid (0.2.0.beta4)
oa-core (= 0.2.0.beta4)
rack-openid (~> 1.2.0)
ruby-openid-apps-discovery
oauth (0.4.4)
@ -150,8 +150,8 @@ DEPENDENCIES
mocha
mongo (= 1.1.2)
mongoid (= 2.0.0.beta.20)
oa-oauth
oa-openid
oa-oauth (~> 0.2.0.beta1)
oa-openid (~> 0.2.0.beta1)
rails (~> 3.0.4)
ruby-debug (>= 0.10.3)
sqlite3-ruby

View File

@ -9,18 +9,18 @@ class Devise::OmniauthCallbacksController < ApplicationController
protected
def failed_strategy
env["omniauth.failed_strategy"]
env["omniauth.error.strategy"]
end
def failure_message
exception = env["omniauth.error"]
error = exception.error_reason if exception.respond_to?(:error_reason)
error ||= exception.error if exception.respond_to?(:error)
error ||= env["omniauth.failure_key"]
error ||= env["omniauth.error.type"].to_s
error.to_s.humanize if error
end
def after_omniauth_failure_path_for(scope)
new_session_path(scope)
end
end
end

View File

@ -5,29 +5,14 @@ rescue LoadError => e
raise
end
module OmniAuth
# TODO HAXES Backport to OmniAuth
module Strategy #:nodoc:
def initialize(app, name, *args)
@app = app
@name = name.to_sym
@options = args.last.is_a?(Hash) ? args.pop : {}
yield self if block_given?
end
def fail!(message_key, exception = nil)
self.env['omniauth.error'] = exception
self.env['omniauth.failure_key'] = message_key
self.env['omniauth.failed_strategy'] = self
OmniAuth.config.on_failure.call(self.env, message_key.to_sym)
end
end
unless OmniAuth.config.respond_to? :test_mode
warn "Devise's OmniAuth testing support is deprecated. You should use Omniauth's own support, please ensure you have 0.2.0.beta version or later installed."
end
# Clean up the default path_prefix. It will be automatically set by Devise.
OmniAuth.config.path_prefix = nil
OmniAuth.config.on_failure = Proc.new do |env, key|
OmniAuth.config.on_failure = Proc.new do |env|
env['devise.mapping'] = Devise::Mapping.find_by_path!(env['PATH_INFO'], :path)
controller_klass = "#{env['devise.mapping'].controllers[:omniauth_callbacks].camelize}Controller"
controller_klass.constantize.action(:failure).call(env)

View File

@ -1,57 +1,31 @@
module Devise
module OmniAuth
module TestHelpers
DEPRECATION_MESSAGE = "Faraday changed the way mocks work in a way incompatible to Devise. Luckily, Omniauth now supports a new test mode, please use it in your tests instead: https://github.com/intridea/omniauth/wiki/Integration-Testing"
DeprecationError = Class.new(StandardError)
def self.stub!(*args)
raise DeprecationError, DEPRECATION_MESSAGE
end
def self.reset_stubs!(*args)
raise DeprecationError, DEPRECATION_MESSAGE
end
def self.test_mode!
Faraday.default_adapter = :test if defined?(Faraday)
ActiveSupport.on_load(:action_controller) { include Devise::OmniAuth::TestHelpers }
ActiveSupport.on_load(:action_view) { include Devise::OmniAuth::TestHelpers }
end
def self.stub!(provider, stubs=nil, &block)
raise "You either need to pass stubs as a block or as a parameter" unless block_given? || stubs
config = Devise.omniauth_configs[provider]
raise "Could not find configuration for #{provider.to_s} omniauth provider" unless config
config.check_if_allow_stubs!
stubs ||= Faraday::Adapter::Test::Stubs.new(&block)
config.build_connection do |b|
b.adapter :test, stubs
end
end
def self.reset_stubs!(*providers)
target = providers.any? ? Devise.omniauth_configs.slice(*providers) : Devise.omniauth_configs
target.each_value do |config|
next unless config.allow_stubs?
config.build_connection { |b| b.adapter Faraday.default_adapter }
end
warn DEPRECATION_MESSAGE
end
def self.short_circuit_authorizers!
module_eval <<-ALIASES, __FILE__, __LINE__ + 1
def omniauth_authorize_path(*args)
omniauth_callback_path(*args)
end
ALIASES
Devise.mappings.each_value do |m|
next unless m.omniauthable?
module_eval <<-ALIASES, __FILE__, __LINE__ + 1
def #{m.name}_omniauth_authorize_path(provider, params = {})
#{m.name}_omniauth_callback_path(provider, params)
end
ALIASES
end
::OmniAuth.config.test_mode = true
warn DEPRECATION_MESSAGE
end
def self.unshort_circuit_authorizers!
module_eval do
instance_methods.each { |m| remove_method(m) }
end
::OmniAuth.config.test_mode = false
warn DEPRECATION_MESSAGE
end
end
end
end
end

View File

@ -2,33 +2,31 @@ require 'test_helper'
class OmniauthableIntegrationTest < ActionController::IntegrationTest
FACEBOOK_INFO = {
:id => '12345',
:link => 'http://facebook.com/josevalim',
:email => 'user@example.com',
:first_name => 'Jose',
:last_name => 'Valim',
:website => 'http://blog.plataformatec.com.br'
}
ACCESS_TOKEN = {
:access_token => "plataformatec"
"id" => '12345',
"link" => 'http://facebook.com/josevalim',
"email" => 'user@example.com',
"first_name" => 'Jose',
"last_name" => 'Valim',
"website" => 'http://blog.plataformatec.com.br'
}
setup do
OmniAuth.config.test_mode = true
stub_facebook!
Devise::OmniAuth.short_circuit_authorizers!
end
teardown do
Devise::OmniAuth.unshort_circuit_authorizers!
Devise::OmniAuth.reset_stubs!
OmniAuth.config.test_mode = false
end
def stub_facebook!
Devise::OmniAuth.stub!(:facebook) do |b|
b.post('/oauth/access_token') { [200, {}, ACCESS_TOKEN.to_json] }
b.get('/me?access_token=plataformatec') { [200, {}, FACEBOOK_INFO.to_json] }
end
OmniAuth.config.mock_auth[:facebook] = {
"uid" => '12345',
"provider" => 'facebook',
"user_info" => {"nickname" => 'josevalim'},
"credentials" => {"token" => 'plataformatec'},
"extra" => {"user_hash" => FACEBOOK_INFO}
}
end
test "can access omniauth.auth in the env hash" do
@ -40,11 +38,11 @@ class OmniauthableIntegrationTest < ActionController::IntegrationTest
assert_equal "12345", json["uid"]
assert_equal "facebook", json["provider"]
assert_equal "josevalim", json["user_info"]["nickname"]
assert_equal FACEBOOK_INFO, json["extra"]["user_hash"].symbolize_keys
assert_equal FACEBOOK_INFO, json["extra"]["user_hash"]
assert_equal "plataformatec", json["credentials"]["token"]
end
test "cleans up session on sign up" do
test "cleans up session on sign up" do
assert_no_difference "User.count" do
visit "/users/sign_in"
click_link "Sign in with Facebook"
@ -65,7 +63,7 @@ class OmniauthableIntegrationTest < ActionController::IntegrationTest
assert_not session["devise.facebook_data"]
end
test "cleans up session on cancel" do
test "cleans up session on cancel" do
assert_no_difference "User.count" do
visit "/users/sign_in"
click_link "Sign in with Facebook"
@ -76,7 +74,7 @@ class OmniauthableIntegrationTest < ActionController::IntegrationTest
assert !session["devise.facebook_data"]
end
test "cleans up session on sign in" do
test "cleans up session on sign in" do
assert_no_difference "User.count" do
visit "/users/sign_in"
click_link "Sign in with Facebook"
@ -87,21 +85,24 @@ class OmniauthableIntegrationTest < ActionController::IntegrationTest
assert !session["devise.facebook_data"]
end
test "handles callback error parameter according to the specification" do
visit "/users/auth/facebook/callback?error=access_denied"
assert_current_url "/users/sign_in"
assert_contain 'Could not authorize you from Facebook because "Access denied".'
end
# The following two tests are commented because OmniAuth's test
# support is not yet able to support failure scenarios.
#
# test "handles callback error parameter according to the specification" do
# visit "/users/auth/facebook/callback?error=access_denied"
# assert_current_url "/users/sign_in"
# assert_contain 'Could not authorize you from Facebook because "Access denied".'
# end
test "handles other exceptions from omniauth" do
Devise::OmniAuth.stub!(:facebook) do |b|
b.post('/oauth/access_token') { [401, {}, {}.to_json] }
end
# test "handles other exceptions from omniauth" do
# Devise::OmniAuth.stub!(:facebook) do |b|
# b.post('/oauth/access_token') { [401, {}, {}.to_json] }
# end
visit "/users/sign_in"
click_link "Sign in with facebook"
# visit "/users/sign_in"
# click_link "Sign in with facebook"
assert_current_url "/users/sign_in"
assert_contain 'Could not authorize you from Facebook because "Invalid credentials".'
end
end
# assert_current_url "/users/sign_in"
# assert_contain 'Could not authorize you from Facebook because "Invalid credentials".'
# end
end

View File

@ -0,0 +1,25 @@
require 'test_helper'
class OmniAuthTestHelpersTest < ActiveSupport::TestCase
test "Assert that stub! raises deprecation error" do
assert_raises Devise::OmniAuth::TestHelpers::DeprecationError do
Devise::OmniAuth::TestHelpers.stub!
end
end
test "Assert that reset_stubs! raises deprecation error" do
assert_raises Devise::OmniAuth::TestHelpers::DeprecationError do
Devise::OmniAuth::TestHelpers.reset_stubs!
end
end
test "Assert that short_circuit_authorizers! warns about deprecation" do
Devise::OmniAuth::TestHelpers.short_circuit_authorizers!
assert ::OmniAuth.config.test_mode
end
test "Assert that unshort_circuit_authorizers! warns about deprecation" do
Devise::OmniAuth::TestHelpers.unshort_circuit_authorizers!
assert ! ::OmniAuth.config.test_mode
end
end

View File

@ -17,8 +17,6 @@ Webrat.configure do |config|
config.open_error_files = false
end
Devise::OmniAuth.test_mode!
# Add support to load paths so we can overwrite broken webrat setup
$:.unshift File.expand_path('../support', __FILE__)
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@ -26,4 +24,4 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
# For generators
require "rails/generators/test_case"
require "generators/devise/install_generator"
require "generators/devise/views_generator"
require "generators/devise/views_generator"