mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Unit tests for OAuth.
This commit is contained in:
parent
9222601c5b
commit
4ac6b6e407
7 changed files with 120 additions and 5 deletions
|
@ -20,7 +20,7 @@ class RoutesTest < ActionController::TestCase
|
|||
send(:"#{prepend_path}user_#{name}_url", :param => 123)
|
||||
|
||||
@request.path = nil
|
||||
# With an AR object
|
||||
# With an object
|
||||
assert_equal @controller.send(:"#{prepend_path}#{name}_path", User.new),
|
||||
send(:"#{prepend_path}user_#{name}_path")
|
||||
assert_equal @controller.send(:"#{prepend_path}#{name}_url", User.new),
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
require 'test_helper'
|
||||
|
||||
class OAuthableTest < ActionController::IntegrationTest
|
||||
class OAuthableIntegrationTest < ActionController::IntegrationTest
|
||||
FACEBOOK_INFO = {
|
||||
:username => 'usertest',
|
||||
:email => 'user@test.com'
|
||||
|
|
21
test/models/oauthable_test.rb
Normal file
21
test/models/oauthable_test.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
require 'test_helper'
|
||||
|
||||
class OauthableTest < ActiveSupport::TestCase
|
||||
teardown { Devise::Oauth.reset_stubs! }
|
||||
|
||||
test "oauth_configs returns all configurations relative to that model" do
|
||||
swap User, :oauth_providers => [:github] do
|
||||
assert_equal User.oauth_configs, Devise.oauth_configs.slice(:github)
|
||||
end
|
||||
end
|
||||
|
||||
test "oauth_access_token returns the token object for the given provider" do
|
||||
Devise::Oauth.stub!(:facebook) do |b|
|
||||
b.get('/me?access_token=plataformatec') { [200, {}, {}.to_json] }
|
||||
end
|
||||
|
||||
access_token = User.oauth_access_token(:facebook, "plataformatec")
|
||||
assert_kind_of OAuth2::AccessToken, access_token
|
||||
assert_equal "{}", access_token.get("/me")
|
||||
end
|
||||
end
|
44
test/oauth/config_test.rb
Normal file
44
test/oauth/config_test.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
require 'test_helper'
|
||||
|
||||
class OauthConfigTest < ActiveSupport::TestCase
|
||||
ACCESS_TOKEN = {
|
||||
:access_token => "plataformatec"
|
||||
}
|
||||
|
||||
setup { @config = Devise.oauth_configs[:facebook] }
|
||||
teardown { Devise::Oauth.reset_stubs! }
|
||||
|
||||
test "stored OAuth2::Client" do
|
||||
assert_kind_of OAuth2::Client, @config.client
|
||||
end
|
||||
|
||||
test "build authorize url" do
|
||||
url = @config.authorize_url(:redirect_uri => "foo")
|
||||
assert_match "https://graph.facebook.com/oauth/authorize?", url
|
||||
assert_match "scope=email%2Coffline_access", url
|
||||
assert_match "client_id=APP_ID", url
|
||||
assert_match "type=web_server", url
|
||||
assert_match "redirect_uri=foo", url
|
||||
end
|
||||
|
||||
test "retrieves access token object by code" do
|
||||
Devise::Oauth.stub!(:facebook) do |b|
|
||||
b.post('/oauth/access_token') { [200, {}, ACCESS_TOKEN.to_json] }
|
||||
b.get('/me?access_token=plataformatec') { [200, {}, {}.to_json] }
|
||||
end
|
||||
|
||||
access_token = @config.access_token_by_code("12345")
|
||||
assert_kind_of OAuth2::AccessToken, access_token
|
||||
assert_equal "{}", access_token.get("/me")
|
||||
end
|
||||
|
||||
test "retrieves access token object by token" do
|
||||
Devise::Oauth.stub!(:facebook) do |b|
|
||||
b.get('/me?access_token=plataformatec') { [200, {}, {}.to_json] }
|
||||
end
|
||||
|
||||
access_token = @config.access_token_by_token("plataformatec")
|
||||
assert_kind_of OAuth2::AccessToken, access_token
|
||||
assert_equal "{}", access_token.get("/me")
|
||||
end
|
||||
end
|
47
test/oauth/url_helpers_test.rb
Normal file
47
test/oauth/url_helpers_test.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
require 'test_helper'
|
||||
|
||||
class OauthRoutesTest < ActionController::TestCase
|
||||
tests ApplicationController
|
||||
|
||||
def assert_path_and_url(action, provider)
|
||||
# Resource param
|
||||
assert_equal @controller.send(action, :user, provider),
|
||||
@controller.send("user_#{action}", provider)
|
||||
|
||||
# Default url params
|
||||
assert_equal @controller.send(action, :user, provider, :param => 123),
|
||||
@controller.send("user_#{action}", provider, :param => 123)
|
||||
|
||||
# With an object
|
||||
assert_equal @controller.send(action, User.new, provider, :param => 123),
|
||||
@controller.send("user_#{action}", provider, :param => 123)
|
||||
end
|
||||
|
||||
test 'should alias oauth_callback to mapped user auth_callback' do
|
||||
assert_path_and_url :oauth_callback_path, :github
|
||||
assert_path_and_url :oauth_callback_url, :github
|
||||
assert_path_and_url :oauth_callback_path, :facebook
|
||||
assert_path_and_url :oauth_callback_url, :facebook
|
||||
end
|
||||
|
||||
test 'should alias oauth_authorize to mapped user auth_authorize' do
|
||||
assert_path_and_url :oauth_authorize_url, :github
|
||||
assert_path_and_url :oauth_authorize_url, :facebook
|
||||
end
|
||||
|
||||
test 'should adds scope, provider and redirect_uri to authorize urls' do
|
||||
url = @controller.oauth_authorize_url(:user, :github)
|
||||
assert_match "https://github.com/login/oauth/authorize?", url
|
||||
assert_match "scope=user%2Cpublic_repo", url
|
||||
assert_match "client_id=APP_ID", url
|
||||
assert_match "type=web_server", url
|
||||
assert_match "redirect_uri=http%3A%2F%2Ftest.host%2Fusers%2Foauth%2Fgithub%2Fcallback", url
|
||||
|
||||
url = @controller.oauth_authorize_url(:user, :facebook)
|
||||
assert_match "https://graph.facebook.com/oauth/authorize?", url
|
||||
assert_match "scope=email%2Coffline_access", url
|
||||
assert_match "client_id=APP_ID", url
|
||||
assert_match "type=web_server", url
|
||||
assert_match "redirect_uri=http%3A%2F%2Ftest.host%2Fusers%2Foauth%2Ffacebook%2Fcallback", url
|
||||
end
|
||||
end
|
|
@ -4,7 +4,7 @@ module SharedUser
|
|||
included do
|
||||
devise :database_authenticatable, :confirmable, :lockable, :recoverable,
|
||||
:registerable, :rememberable, :timeoutable, :token_authenticatable,
|
||||
:trackable, :validatable, :oauthable, :oauth_providers => [:github, :facebook]
|
||||
:trackable, :validatable, :oauthable
|
||||
|
||||
# They need to be included after Devise is called.
|
||||
extend ExtendMethods
|
||||
|
|
|
@ -93,7 +93,10 @@ class DefaultRoutingTest < ActionController::TestCase
|
|||
|
||||
test 'map oauth callbacks' do
|
||||
assert_recognizes({:controller => 'devise/oauth_callbacks', :action => 'facebook'}, {:path => 'users/oauth/facebook/callback', :method => :get})
|
||||
assert_named_route "/users/oauth/facebook/callback", :user_oauth_callback_path, :facebook
|
||||
|
||||
assert_recognizes({:controller => 'devise/oauth_callbacks', :action => 'github'}, {:path => 'users/oauth/github/callback', :method => :get})
|
||||
assert_named_route "/users/oauth/github/callback", :user_oauth_callback_path, :github
|
||||
|
||||
assert_raise ActionController::RoutingError do
|
||||
assert_recognizes({:controller => 'devise/oauth_callbacks', :action => 'twitter'}, {:path => 'users/oauth/twitter/callback', :method => :get})
|
||||
|
@ -102,8 +105,8 @@ class DefaultRoutingTest < ActionController::TestCase
|
|||
|
||||
protected
|
||||
|
||||
def assert_named_route(result, name)
|
||||
assert_equal result, @routes.url_helpers.send(name)
|
||||
def assert_named_route(result, *args)
|
||||
assert_equal result, @routes.url_helpers.send(*args)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue