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

Add a small connection stubbing API.

This commit is contained in:
José Valim 2010-07-15 18:32:11 +02:00
parent ef841ca17d
commit 0d6f303735
3 changed files with 29 additions and 8 deletions

View file

@ -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

View file

@ -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

View file

@ -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"