]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/oauth-plugin/generators/oauth_provider/templates/controller_spec_helper.rb
9e71284364153bfd8e7a3a0920085050f053e1a5
[rails.git] / vendor / plugins / oauth-plugin / generators / oauth_provider / templates / controller_spec_helper.rb
1 module OAuthControllerSpecHelper
2   def login
3     controller.stub!(:local_request?).and_return(true)
4     @user = mock_model(User)
5     controller.stub!(:current_user).and_return(@user)
6     @tokens = []
7     @tokens.stub!(:find).and_return(@tokens)
8     @user.stub!(:tokens).and_return(@tokens)
9     User.stub!(:find_by_id).and_return(@user)
10   end
11   
12   def login_as_application_owner
13     login
14     @client_application = mock_model(ClientApplication)
15     @client_applications = [@client_application]
16     
17     @user.stub!(:client_applications).and_return(@client_applications)
18     @client_applications.stub!(:find).and_return(@client_application)
19   end
20   
21   def setup_oauth
22     controller.stub!(:local_request?).and_return(true)
23     @user||=mock_model(User)
24     
25     User.stub!(:find_by_id).and_return(@user)
26     
27     @server = OAuth::Server.new "http://test.host"
28     @consumer = OAuth::Consumer.new('key', 'secret',{:site => "http://test.host"})
29
30     @client_application = mock_model(ClientApplication)
31     controller.stub!(:current_client_application).and_return(@client_application)
32     ClientApplication.stub!(:find_by_key).and_return(@client_application)
33     @client_application.stub!(:key).and_return(@consumer.key)
34     @client_application.stub!(:secret).and_return(@consumer.secret)
35     @client_application.stub!(:name).and_return("Client Application name")
36     @client_application.stub!(:callback_url).and_return("http://application/callback")
37     @request_token = mock_model(RequestToken, :token => 'request_token', :client_application => @client_application, :secret => "request_secret", :user => @user)
38     @request_token.stub!(:invalidated?).and_return(false)
39     ClientApplication.stub!(:find_token).and_return(@request_token)
40     
41     @request_token_string = "oauth_token = request_token&oauth_token_secret = request_secret"
42     @request_token.stub!(:to_query).and_return(@request_token_string)
43
44     @access_token = mock_model(AccessToken, :token => 'access_token', :client_application => @client_application, :secret => "access_secret", :user => @user)
45     @access_token.stub!(:invalidated?).and_return(false)
46     @access_token.stub!(:authorized?).and_return(true)
47     @access_token_string = "oauth_token = access_token&oauth_token_secret = access_secret"
48     @access_token.stub!(:to_query).and_return(@access_token_string)
49
50     @client_application.stub!(:authorize_request?).and_return(true)
51 #    @client_application.stub!(:sign_request_with_oauth_token).and_return(@request_token)
52     @client_application.stub!(:exchange_for_access_token).and_return(@access_token)
53   end
54   
55   def setup_oauth_for_user
56     login
57     setup_oauth
58     @tokens = [@request_token]
59     @tokens.stub!(:find).and_return(@tokens)
60     @tokens.stub!(:find_by_token).and_return(@request_token)
61     @user.stub!(:tokens).and_return(@tokens)
62   end
63   
64   def sign_request_with_oauth(token = nil)
65     ActionController::TestRequest.use_oauth = true
66     @request.configure_oauth(@consumer,token)
67   end
68     
69   def setup_to_authorize_request
70     setup_oauth
71     OauthToken.stub!(:find_by_token).with( @access_token.token).and_return(@access_token)
72     @access_token.stub!(:is_a?).and_return(true)
73   end
74 end