From 0d6f303735606bcfe09ec37ef4b7e7848dfb052b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 15 Jul 2010 18:32:11 +0200 Subject: [PATCH] Add a small connection stubbing API. --- lib/devise/oauth.rb | 15 +++++++++++++++ lib/devise/oauth/config.rb | 4 ++++ test/integration/oauthable_test.rb | 18 ++++++++++-------- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/devise/oauth.rb b/lib/devise/oauth.rb index 11f68956..8b81d0fb 100644 --- a/lib/devise/oauth.rb +++ b/lib/devise/oauth.rb @@ -21,6 +21,21 @@ module Devise ActiveSupport.on_load(:action_controller) { include Devise::Oauth::TestHelpers } ActiveSupport.on_load(:action_view) { include Devise::Oauth::TestHelpers } end + + def stub!(provider, stubs=nil, &block) + raise "You either need to pass stubs as a block or as a parameter" unless block_given? || stubs + stubs ||= Faraday::Adapter::Test::Stubs.new(&block) + Devise.oauth_configs[provider].build_connection do |b| + b.adapter :test, stubs + end + end + + def reset_stubs!(*providers) + target = providers.any? ? Devise.oauth_configs.slice(*providers) : Devise.oauth_configs + target.each_value do |v| + v.build_connection { |b| b.adapter Faraday.default_adapter } + end + end end end end \ No newline at end of file diff --git a/lib/devise/oauth/config.rb b/lib/devise/oauth/config.rb index ba0af0b8..d9af7878 100644 --- a/lib/devise/oauth/config.rb +++ b/lib/devise/oauth/config.rb @@ -24,6 +24,10 @@ module Devise def access_token_by_token(token) OAuth2::AccessToken.new(client, token) end + + def build_connection(&block) + client.connection.build(&block) + end end end end \ No newline at end of file diff --git a/test/integration/oauthable_test.rb b/test/integration/oauthable_test.rb index 6a3e2120..547f93bd 100644 --- a/test/integration/oauthable_test.rb +++ b/test/integration/oauthable_test.rb @@ -10,18 +10,20 @@ class OAuthableTest < ActionController::IntegrationTest :access_token => "plataformatec" } - stubs = Faraday::Adapter::Test::Stubs.new do |stub| - stub.post('/oauth/access_token') { [200, {}, ACCESS_TOKEN.to_json] } - stub.get('/me?access_token=plataformatec') { [200, {}, FACEBOOK_INFO.to_json] } + setup do + Devise::Oauth.short_circuit_authorizers! + + Devise::Oauth.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 end - User.oauth_configs[:facebook].client.connection.build do |b| - b.adapter :test, stubs + teardown do + Devise::Oauth.unshort_circuit_authorizers! + Devise::Oauth.reset_stubs! end - setup { Devise::Oauth.short_circuit_authorizers! } - teardown { Devise::Oauth.unshort_circuit_authorizers! } - test "omg" do assert_difference "User.count", 1 do get "/users/sign_up"