]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/oauth-plugin/generators/oauth_provider/templates/controller_test_helper.rb
Merge 17067 from trunk.
[rails.git] / vendor / plugins / oauth-plugin / generators / oauth_provider / templates / controller_test_helper.rb
1 require "mocha"
2 module OAuthControllerTestHelper
3   
4   # Some custom stuff since we're using Mocha
5   def mock_model(model_class, options_and_stubs = {})
6     id = rand(10000)
7     options_and_stubs.reverse_merge! :id => id,
8       :to_param => id.to_s,
9       :new_record? => false,
10       :errors => stub("errors", :count => 0)
11       
12     m = stub("#{model_class.name}_#{options_and_stubs[:id]}", options_and_stubs)
13     m.instance_eval <<-CODE
14       def is_a?(other)
15         #{model_class}.ancestors.include?(other)
16       end
17       def kind_of?(other)
18         #{model_class}.ancestors.include?(other)
19       end
20       def instance_of?(other)
21         other == #{model_class}
22       end
23       def class
24         #{model_class}
25       end
26     CODE
27     yield m if block_given?
28     m
29   end
30     
31   def mock_full_client_application
32     mock_model(ClientApplication, 
33                 :name => "App1", 
34                 :url => "http://app.com", 
35                 :callback_url => "http://app.com/callback",
36                 :support_url => "http://app.com/support",
37                 :key => "asd23423yy",
38                 :secret => "secret",
39                 :oauth_server => OAuth::Server.new("http://kowabunga.com")
40               )
41   end
42   
43   def login
44     @controller.stubs(:local_request?).returns(true)
45     @user = mock_model(User, :login => "ron")
46     @controller.stubs(:current_user).returns(@user)
47     @tokens=[]
48     @tokens.stubs(:find).returns(@tokens)
49     @user.stubs(:tokens).returns(@tokens)
50     User.stubs(:find_by_id).returns(@user)
51   end
52   
53   def login_as_application_owner
54     login
55     @client_application = mock_full_client_application
56     @client_applications = [@client_application]
57     
58     @user.stubs(:client_applications).returns(@client_applications)
59     @client_applications.stubs(:find).returns(@client_application)
60   end
61   
62   def setup_oauth
63     @controller.stubs(:local_request?).returns(true)
64     @user||=mock_model(User)
65     
66     User.stubs(:find_by_id).returns(@user)
67     
68     @server=OAuth::Server.new "http://test.host"
69     @consumer=OAuth::Consumer.new('key','secret',{:site=>"http://test.host"})
70
71     @client_application = mock_full_client_application
72     @controller.stubs(:current_client_application).returns(@client_application)
73     ClientApplication.stubs(:find_by_key).returns(@client_application)
74     @client_application.stubs(:key).returns(@consumer.key)
75     @client_application.stubs(:secret).returns(@consumer.secret)
76     @client_application.stubs(:name).returns("Client Application name")
77     @client_application.stubs(:callback_url).returns("http://application/callback")
78     @request_token=mock_model(RequestToken,:token=>'request_token',:client_application=>@client_application,:secret=>"request_secret",:user=>@user)
79     @request_token.stubs(:invalidated?).returns(false)
80     ClientApplication.stubs(:find_token).returns(@request_token)
81     
82     @request_token_string="oauth_token=request_token&oauth_token_secret=request_secret"
83     @request_token.stubs(:to_query).returns(@request_token_string)
84
85     @access_token=mock_model(AccessToken,:token=>'access_token',:client_application=>@client_application,:secret=>"access_secret",:user=>@user)
86     @access_token.stubs(:invalidated?).returns(false)
87     @access_token.stubs(:authorized?).returns(true)
88     @access_token_string="oauth_token=access_token&oauth_token_secret=access_secret"
89     @access_token.stubs(:to_query).returns(@access_token_string)
90
91     @client_application.stubs(:authorize_request?).returns(true)
92 #    @client_application.stubs(:sign_request_with_oauth_token).returns(@request_token)
93     @client_application.stubs(:exchange_for_access_token).returns(@access_token)
94   end
95   
96   def setup_oauth_for_user
97     login
98     setup_oauth
99     @tokens=[@request_token]
100     @tokens.stubs(:find).returns(@tokens)
101     @tokens.stubs(:find_by_token).returns(@request_token)
102     @user.stubs(:tokens).returns(@tokens)
103   end
104   
105   def sign_request_with_oauth(token=nil)
106     ActionController::TestRequest.use_oauth=true
107     @request.configure_oauth(@consumer, token)
108   end
109     
110   def setup_to_authorize_request
111     setup_oauth
112     OauthToken.stubs(:find_by_token).with( @access_token.token).returns(@access_token)
113     @access_token.stubs(:is_a?).returns(true)
114   end
115 end