1 require File.dirname(__FILE__) + '/../spec_helper'
2 require File.dirname(__FILE__) + '/oauth_controller_spec_helper'
3 require 'oauth/client/action_controller_request'
5 describe OauthController, "getting a request token" do
6 include OAuthControllerSpecHelper
9 sign_request_with_oauth
10 @client_application.stub!(:create_request_token).and_return(@request_token)
17 it "should be successful" do
19 response.should be_success
22 it "should query for client_application" do
23 ClientApplication.should_receive(:find_by_key).with('key').and_return(@client_application)
27 it "should request token from client_application" do
28 @client_application.should_receive(:create_request_token).and_return(@request_token)
32 it "should return token string" do
34 response.body.should == @request_token_string
38 describe OauthController, "token authorization" do
39 include OAuthControllerSpecHelper
43 RequestToken.stub!(:find_by_token).and_return(@request_token)
47 get :authorize, :oauth_token => @request_token.token
51 @request_token.should_receive(:authorize!).with(@user)
52 post :authorize, :oauth_token => @request_token.token, :authorize => "1"
55 def do_post_without_user_authorization
56 @request_token.should_receive(:invalidate!)
57 post :authorize, :oauth_token => @request_token.token, :authorize => "0"
60 def do_post_with_callback
61 @request_token.should_receive(:authorize!).with(@user)
62 post :authorize, :oauth_token => @request_token.token, :oauth_callback => "http://application/alternative", :authorize => "1"
65 def do_post_with_no_application_callback
66 @request_token.should_receive(:authorize!).with(@user)
67 @client_application.stub!(:callback_url).and_return(nil)
68 post :authorize, :oauth_token => @request_token.token, :authorize => "1"
71 it "should be successful" do
73 response.should be_success
76 it "should query for client_application" do
77 RequestToken.should_receive(:find_by_token).and_return(@request_token)
81 it "should assign token" do
83 assigns[:token].should equal(@request_token)
86 it "should render authorize template" do
88 response.should render_template('authorize')
91 it "should redirect to default callback" do
93 response.should be_redirect
94 response.should redirect_to("http://application/callback?oauth_token=#{@request_token.token}")
97 it "should redirect to callback in query" do
99 response.should be_redirect
100 response.should redirect_to("http://application/alternative?oauth_token=#{@request_token.token}")
103 it "should be successful on authorize without any application callback" do
104 do_post_with_no_application_callback
105 response.should be_success
108 it "should be successful on authorize without any application callback" do
109 do_post_with_no_application_callback
110 response.should render_template('authorize_success')
113 it "should render failure screen on user invalidation" do
114 do_post_without_user_authorization
115 response.should render_template('authorize_failure')
118 it "should render failure screen if token is invalidated" do
119 @request_token.should_receive(:invalidated?).and_return(true)
121 response.should render_template('authorize_failure')
128 describe OauthController, "getting an access token" do
129 include OAuthControllerSpecHelper
132 sign_request_with_oauth @request_token
133 @request_token.stub!(:exchange!).and_return(@access_token)
140 it "should be successful" do
142 response.should be_success
145 it "should query for client_application" do
146 ClientApplication.should_receive(:find_token).with(@request_token.token).and_return(@request_token)
150 it "should request token from client_application" do
151 @request_token.should_receive(:exchange!).and_return(@access_token)
155 it "should return token string" do
157 response.body.should == @access_token_string
161 class OauthorizedController<ApplicationController
162 before_filter :login_or_oauth_required, :only => :both
163 before_filter :login_required, :only => :interactive
164 before_filter :oauth_required, :only => :token_only
176 describe OauthorizedController, " access control" do
177 include OAuthControllerSpecHelper
182 it "should have access_token set up correctly" do
183 setup_to_authorize_request
184 @access_token.is_a?(AccessToken).should == true
185 @access_token.should be_authorized
186 @access_token.should_not be_invalidated
187 @access_token.user.should == @user
188 @access_token.client_application.should == @client_application
191 it "should return false for oauth? by default" do
192 controller.send(:oauth?).should == false
195 it "should return nil for current_token by default" do
196 controller.send(:current_token).should be_nil
199 it "should allow oauth when using login_or_oauth_required" do
200 setup_to_authorize_request
201 sign_request_with_oauth(@access_token)
202 ClientApplication.should_receive(:find_token).with(@access_token.token).and_return(@access_token)
204 controller.send(:current_token).should == @access_token
205 controller.send(:current_token).is_a?(AccessToken).should == true
206 controller.send(:current_user).should == @user
207 controller.send(:current_client_application).should == @client_application
208 response.code.should == '200'
209 response.should be_success
212 it "should allow interactive when using login_or_oauth_required" do
215 response.should be_success
216 controller.send(:current_user).should == @user
217 controller.send(:current_token).should be_nil
221 it "should allow oauth when using oauth_required" do
222 setup_to_authorize_request
223 sign_request_with_oauth(@access_token)
224 ClientApplication.should_receive(:find_token).with(@access_token.token).and_return(@access_token)
226 controller.send(:current_token).should == @access_token
227 controller.send(:current_client_application).should == @client_application
228 controller.send(:current_user).should == @user
229 response.code.should == '200'
230 response.should be_success
233 it "should disallow oauth using RequestToken when using oauth_required" do
234 setup_to_authorize_request
235 ClientApplication.should_receive(:find_token).with(@request_token.token).and_return(@request_token)
236 sign_request_with_oauth(@request_token)
238 response.code.should == '401'
241 it "should disallow interactive when using oauth_required" do
244 response.code.should == '401'
246 controller.send(:current_user).should == @user
247 controller.send(:current_token).should be_nil
250 it "should disallow oauth when using login_required" do
251 setup_to_authorize_request
252 sign_request_with_oauth(@access_token)
254 response.code.should == "302"
255 controller.send(:current_user).should be_nil
256 controller.send(:current_token).should be_nil
259 it "should allow interactive when using login_required" do
262 response.should be_success
263 controller.send(:current_user).should == @user
264 controller.send(:current_token).should be_nil
269 describe OauthController, "revoke" do
270 include OAuthControllerSpecHelper
273 @request_token.stub!(:invalidate!)
277 post :revoke, :token => "TOKEN STRING"
280 it "should redirect to index" do
282 response.should be_redirect
283 response.should redirect_to('http://test.host/oauth_clients')
286 it "should query current_users tokens" do
287 @tokens.should_receive(:find_by_token).and_return(@request_token)
291 it "should call invalidate on token" do
292 @request_token.should_receive(:invalidate!)