1 require File.dirname(__FILE__) + '/../test_helper'
2 require File.dirname(__FILE__) + '/../oauth_controller_test_helper'
3 require 'oauth/client/action_controller_request'
5 class OauthController; def rescue_action(e) raise e end; end
7 class OauthControllerRequestTokenTest < ActionController::TestCase
8 include OAuthControllerTestHelper
12 @controller = OauthController.new
14 sign_request_with_oauth
15 @client_application.stubs(:create_request_token).returns(@request_token)
22 def test_should_be_successful
24 assert @response.success?
27 def test_should_query_for_client_application
28 ClientApplication.expects(:find_by_key).with('key').returns(@client_application)
32 def test_should_request_token_from_client_application
33 @client_application.expects(:create_request_token).returns(@request_token)
37 def test_should_return_token_string
39 assert_equal @request_token_string, @response.body
43 class OauthControllerTokenAuthorizationTest < ActionController::TestCase
44 include OAuthControllerTestHelper
48 @controller = OauthController.new
51 RequestToken.stubs(:find_by_token).returns(@request_token)
55 get :authorize, :oauth_token => @request_token.token
59 @request_token.expects(:authorize!).with(@user)
60 post :authorize,:oauth_token=>@request_token.token,:authorize=>"1"
63 def do_post_without_user_authorization
64 @request_token.expects(:invalidate!)
65 post :authorize,:oauth_token=>@request_token.token,:authorize=>"0"
68 def do_post_with_callback
69 @request_token.expects(:authorize!).with(@user)
70 post :authorize,:oauth_token=>@request_token.token,:oauth_callback=>"http://application/alternative",:authorize=>"1"
73 def do_post_with_no_application_callback
74 @request_token.expects(:authorize!).with(@user)
75 @client_application.stubs(:callback_url).returns(nil)
76 post :authorize, :oauth_token => @request_token.token, :authorize=>"1"
79 def test_should_be_successful
81 assert @response.success?
84 def test_should_query_for_client_application
85 RequestToken.expects(:find_by_token).returns(@request_token)
89 def test_should_assign_token
91 assert_equal @request_token, assigns(:token)
94 def test_should_render_authorize_template
96 assert_template('authorize')
99 def test_should_redirect_to_default_callback
101 assert_response :redirect
102 assert_redirected_to("http://application/callback?oauth_token=#{@request_token.token}")
105 def test_should_redirect_to_callback_in_query
106 do_post_with_callback
107 assert_response :redirect
108 assert_redirected_to("http://application/alternative?oauth_token=#{@request_token.token}")
111 def test_should_be_successful_on_authorize_without_any_application_callback
112 do_post_with_no_application_callback
113 assert @response.success?
114 assert_template('authorize_success')
117 def test_should_render_failure_screen_on_user_invalidation
118 do_post_without_user_authorization
119 assert_template('authorize_failure')
122 def test_should_render_failure_screen_if_token_is_invalidated
123 @request_token.expects(:invalidated?).returns(true)
125 assert_template('authorize_failure')
131 class OauthControllerGetAccessTokenTest < ActionController::TestCase
132 include OAuthControllerTestHelper
133 tests OauthController
136 @controller = OauthController.new
138 sign_request_with_oauth @request_token
139 @request_token.stubs(:exchange!).returns(@access_token)
146 def test_should_be_successful
148 assert @response.success?
151 def test_should_query_for_client_application
152 ClientApplication.expects(:find_token).with(@request_token.token).returns(@request_token)
156 def test_should_request_token_from_client_application
157 @request_token.expects(:exchange!).returns(@access_token)
161 def test_should__return_token_string
163 assert_equal @access_token_string, @response.body
167 class OauthorizedController < ApplicationController
168 before_filter :login_or_oauth_required,:only=>:both
169 before_filter :login_required,:only=>:interactive
170 before_filter :oauth_required,:only=>:token_only
173 render :text => "interactive"
177 render :text => "token"
181 render :text => "both"
186 class OauthControllerAccessControlTest < ActionController::TestCase
187 include OAuthControllerTestHelper
188 tests OauthorizedController
191 @controller = OauthorizedController.new
194 def test_should__have_access_token_set_up_correctly
195 setup_to_authorize_request
196 assert @access_token.is_a?(AccessToken)
197 assert @access_token.authorized?
198 assert !@access_token.invalidated?
199 assert_equal @user, @access_token.user
200 assert_equal @client_application, @access_token.client_application
203 def test_should_return_false_for_oauth_by_default
204 assert_equal false, @controller.send(:oauth?)
207 def test_should_return_nil_for_current_token_by_default
208 assert_nil @controller.send(:current_token)
211 def test_should_allow_oauth_when_using_login_or_oauth_required
212 setup_to_authorize_request
213 sign_request_with_oauth(@access_token)
214 ClientApplication.expects(:find_token).with(@access_token.token).returns(@access_token)
216 assert_equal @access_token, @controller.send(:current_token)
217 assert @controller.send(:current_token).is_a?(AccessToken)
218 assert_equal @user, @controller.send(:current_user)
219 assert_equal @client_application, @controller.send(:current_client_application)
220 assert_equal '200', @response.code
221 assert @response.success?
224 def test_should_allow_interactive_when_using_login_or_oauth_required
227 assert @response.success?
228 assert_equal @user, @controller.send(:current_user)
229 assert_nil @controller.send(:current_token)
232 def test_should_allow_oauth_when_using_oauth_required
233 setup_to_authorize_request
234 sign_request_with_oauth(@access_token)
235 ClientApplication.expects(:find_token).with(@access_token.token).returns(@access_token)
237 assert_equal @access_token, @controller.send(:current_token)
238 assert_equal @client_application, @controller.send(:current_client_application)
239 assert_equal @user, @controller.send(:current_user)
240 assert_equal '200', @response.code
241 assert @response.success?
244 def test_should_disallow_oauth_using_request_token_when_using_oauth_required
245 setup_to_authorize_request
246 ClientApplication.expects(:find_token).with(@request_token.token).returns(@request_token)
247 sign_request_with_oauth(@request_token)
249 assert_equal '401', @response.code
252 def test_should_disallow_interactive_when_using_oauth_required
255 assert_equal '401', @response.code
257 assert_equal @user, @controller.send(:current_user)
258 assert_nil @controller.send(:current_token)
261 def test_should_disallow_oauth_when_using_login_required
262 setup_to_authorize_request
263 sign_request_with_oauth(@access_token)
265 assert_equal "302",@response.code
266 assert_nil @controller.send(:current_user)
267 assert_nil @controller.send(:current_token)
270 def test_should_allow_interactive_when_using_login_required
273 assert @response.success?
274 assert_equal @user, @controller.send(:current_user)
275 assert_nil @controller.send(:current_token)
280 class OauthControllerRevokeTest < ActionController::TestCase
281 include OAuthControllerTestHelper
282 tests OauthController
285 @controller = OauthController.new
287 @request_token.stubs(:invalidate!)
291 post :revoke, :token => "TOKEN STRING"
294 def test_should_redirect_to_index
296 assert_response :redirect
297 assert_redirected_to('http://test.host/oauth_clients')
300 def test_should_query_current_users_tokens
301 @tokens.expects(:find_by_token).returns(@request_token)
305 def test_should_call_invalidate_on_token
306 @request_token.expects(:invalidate!)