diff --git a/test/controllers/url_helpers_test.rb b/test/controllers/url_helpers_test.rb index 4baebeee..1b82bb8f 100644 --- a/test/controllers/url_helpers_test.rb +++ b/test/controllers/url_helpers_test.rb @@ -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), diff --git a/test/integration/oauthable_test.rb b/test/integration/oauthable_test.rb index bb7dde6e..23ba64a3 100644 --- a/test/integration/oauthable_test.rb +++ b/test/integration/oauthable_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class OAuthableTest < ActionController::IntegrationTest +class OAuthableIntegrationTest < ActionController::IntegrationTest FACEBOOK_INFO = { :username => 'usertest', :email => 'user@test.com' diff --git a/test/models/oauthable_test.rb b/test/models/oauthable_test.rb new file mode 100644 index 00000000..58a781ad --- /dev/null +++ b/test/models/oauthable_test.rb @@ -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 \ No newline at end of file diff --git a/test/oauth/config_test.rb b/test/oauth/config_test.rb new file mode 100644 index 00000000..bb7fdde1 --- /dev/null +++ b/test/oauth/config_test.rb @@ -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 \ No newline at end of file diff --git a/test/oauth/url_helpers_test.rb b/test/oauth/url_helpers_test.rb new file mode 100644 index 00000000..de03ed52 --- /dev/null +++ b/test/oauth/url_helpers_test.rb @@ -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 diff --git a/test/rails_app/lib/shared_user.rb b/test/rails_app/lib/shared_user.rb index 0aa0ebb4..1f8e4da6 100644 --- a/test/rails_app/lib/shared_user.rb +++ b/test/rails_app/lib/shared_user.rb @@ -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 diff --git a/test/routes_test.rb b/test/routes_test.rb index ff04e08d..f3e4a430 100644 --- a/test/routes_test.rb +++ b/test/routes_test.rb @@ -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