Permalink
Browse files

fixed controller specs

  • Loading branch information...
1 parent 804a7cd commit a1252a8c289883c1b8a651e635ab2775579c59f9 @pelle committed Jul 12, 2009
Showing with 131 additions and 6 deletions.
  1. +130 −5 generators/oauth_provider/templates/controller_spec.rb
  2. +1 −1 lib/oauth/rails/controller_methods.rb
View
135 generators/oauth_provider/templates/controller_spec.rb
@@ -2,12 +2,122 @@
require File.dirname(__FILE__) + '/oauth_controller_spec_helper'
require 'oauth/client/action_controller_request'
+module OAuthControllerSpecHelpers
+ def login
+ controller.stub!(:local_request?).and_return(true)
+ @user=mock_model(User)
+ controller.stub!(:current_user).and_return(@user)
+ @tokens=[]
+ @tokens.stub!(:find).and_return(@tokens)
+ @user.stub!(:tokens).and_return(@tokens)
+ User.stub!(:find_by_id).and_return(@user)
+ end
+
+ def setup_oauth
+ controller.stub!(:local_request?).and_return(true)
+ @user||=mock_model(User)
+
+ User.stub!(:find_by_id).and_return(@user)
+
+ @server=OAuth::Server.new "http://test.host"
+ @consumer=OAuth::Consumer.new('key','secret',{:site=>"http://test.host"})
+
+ @client_application=mock_model(ClientApplication)
+ controller.stub!(:current_client_application).and_return(@client_application)
+ ClientApplication.stub!(:find_by_key).and_return(@client_application)
+ @client_application.stub!(:key).and_return(@consumer.key)
+ @client_application.stub!(:secret).and_return(@consumer.secret)
+ @client_application.stub!(:name).and_return("Client Application name")
+ @client_application.stub!(:callback_url).and_return("http://application/callback")
+ @request_token=mock_model(RequestToken,:token=>'request_token',:client_application=>@client_application,:secret=>"request_secret",:user=>@user)
+ @request_token.stub!(:invalidated?).and_return(false)
+ ClientApplication.stub!(:find_token).and_return(@request_token)
+
+ @request_token_string="oauth_token=request_token&oauth_token_secret=request_secret"
+ @request_token.stub!(:to_query).and_return(@request_token_string)
+ @request_token.stub!(:expired?).and_return(false)
+ @request_token.stub!(:callback_url).and_return(nil)
+ @request_token.stub!(:verifier).and_return("verifyme")
+
+ @access_token=mock_model(AccessToken,:token=>'access_token',:client_application=>@client_application,:secret=>"access_secret",:user=>@user)
+ @access_token.stub!(:invalidated?).and_return(false)
+ @access_token.stub!(:authorized?).and_return(true)
+ @access_token.stub!(:expired?).and_return(false)
+ @access_token_string="oauth_token=access_token&oauth_token_secret=access_secret"
+ @access_token.stub!(:to_query).and_return(@access_token_string)
+
+ @client_application.stub!(:authorize_request?).and_return(true)
+# @client_application.stub!(:sign_request_with_oauth_token).and_return(@request_token)
+ @client_application.stub!(:exchange_for_access_token).and_return(@access_token)
+ end
+
+ def setup_oauth_for_user
+ login
+ setup_oauth
+ @tokens=[@request_token]
+ @tokens.stub!(:find).and_return(@tokens)
+ @tokens.stub!(:find_by_token).and_return(@request_token)
+ @user.stub!(:tokens).and_return(@tokens)
+ end
+
+ def sign_request_with_oauth(token=nil,options={})
+ ActionController::TestRequest.use_oauth=true
+ @request.configure_oauth(@consumer,token,options)
+ end
+
+ def setup_to_authorize_request
+ setup_oauth
+ OauthToken.stub!(:find_by_token).with( @access_token.token).and_return(@access_token)
+ @access_token.stub!(:is_a?).and_return(true)
+ end
+end
+
describe OauthController, "getting a request token" do
include OAuthControllerSpecHelper
before(:each) do
setup_oauth
sign_request_with_oauth
@client_application.stub!(:create_request_token).and_return(@request_token)
+ @client_application.stub!(:token_callback_url=)
+ end
+
+ def do_get
+ get :request_token
+ end
+
+ it "should be successful" do
+ do_get
+ response.should be_success
+ end
+
+ it "should query for client_application" do
+ ClientApplication.should_receive(:find_by_key).with('key').and_return(@client_application)
+ do_get
+ end
+
+ it "should request token from client_application" do
+ @client_application.should_receive(:create_request_token).and_return(@request_token)
+ do_get
+ end
+
+ it "should return token string" do
+ do_get
+ response.body.should==@request_token_string
+ end
+
+ it "should not set token_callback_url" do
+ @client_application.should_not_receive(:token_callback_url=).with(nil)
+ do_get
+ end
+end
+
+describe OauthController, "getting a request token passing a oauth_callback url" do
+ include OAuthControllerSpecHelpers
+ before(:each) do
+ setup_oauth
+ sign_request_with_oauth nil, {:oauth_callback=>"http://test.com/alternative_callback"}
+ @client_application.stub!(:create_request_token).and_return(@request_token)
+ @client_application.stub!(:token_callback_url=)
end
def do_get
@@ -31,10 +141,16 @@ def do_get
it "should return token string" do
do_get
- response.body.should == @request_token_string
+ response.body.should==@request_token_string
+ end
+
+ it "should set token_callback_url with received oauth_callback" do
+ @client_application.should_receive(:token_callback_url=).with("http://test.com/alternative_callback")
+ do_get
end
end
+
describe OauthController, "token authorization" do
include OAuthControllerSpecHelper
before(:each) do
@@ -91,13 +207,20 @@ def do_post_with_no_application_callback
it "should redirect to default callback" do
do_post
response.should be_redirect
- response.should redirect_to("http://application/callback?oauth_token=#{@request_token.token}")
+ response.should redirect_to("http://application/callback?oauth_token=#{@request_token.token}&oauth_verifier=verifyme")
end
- it "should redirect to callback in query" do
+ it "should redirect to request_token callback" do
+ @request_token.stub!(:callback_url).and_return("http://alternative/callback")
+ do_post
+ response.should be_redirect
+ response.should redirect_to("http://alternative/callback?oauth_token=#{@request_token.token}&oauth_verifier=verifyme")
+ end
+
+ it "should ignore callback in query but redirect to default" do
do_post_with_callback
response.should be_redirect
- response.should redirect_to("http://application/alternative?oauth_token=#{@request_token.token}")
+ response.should redirect_to("http://application/callback?oauth_token=#{@request_token.token}&oauth_verifier=verifyme")
end
it "should be successful on authorize without any application callback" do
@@ -116,7 +239,8 @@ def do_post_with_no_application_callback
end
it "should render failure screen if token is invalidated" do
- @request_token.should_receive(:invalidated?).and_return(true)
+ @request_token.stub!(:authorized?).and_return(false)
+ @request_token.stub!(:invalidated?).and_return(true)
do_get
response.should render_template('authorize_failure')
end
@@ -294,3 +418,4 @@ def do_post
end
end
+
View
2 lib/oauth/rails/controller_methods.rb
@@ -56,7 +56,7 @@ def verify_oauth_consumer_signature
@current_client_application = ClientApplication.find_by_key(request_proxy.consumer_key)
# Store this temporarily in client_application object for use in request token generation
- @current_client_application.token_callback_url=request_proxy.oauth_callback
+ @current_client_application.token_callback_url=request_proxy.oauth_callback if request_proxy.oauth_callback
# return the token secret and the consumer secret
[nil, @current_client_application.secret]

0 comments on commit a1252a8

Please sign in to comment.