oauth-plugin 0.3.14 → 0.4.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. data/CHANGELOG +10 -0
  2. data/README.rdoc +44 -9
  3. data/Rakefile +2 -2
  4. data/VERSION +1 -1
  5. data/generators/oauth_consumer/templates/migration.rb +1 -1
  6. data/generators/oauth_provider/oauth_provider_generator.rb +6 -0
  7. data/generators/oauth_provider/templates/access_token.rb +2 -2
  8. data/generators/oauth_provider/templates/client_application.rb +8 -6
  9. data/generators/oauth_provider/templates/client_applications.yml +6 -6
  10. data/generators/oauth_provider/templates/clients_controller_spec.rb +130 -193
  11. data/generators/oauth_provider/templates/controller.rb +12 -0
  12. data/generators/oauth_provider/templates/controller_spec.rb +762 -291
  13. data/generators/oauth_provider/templates/controller_spec_helper.rb +49 -63
  14. data/generators/oauth_provider/templates/migration.rb +6 -5
  15. data/generators/oauth_provider/templates/oauth2_authorize.html.erb +16 -0
  16. data/generators/oauth_provider/templates/oauth2_authorize.html.haml +17 -0
  17. data/generators/oauth_provider/templates/oauth2_token.rb +6 -0
  18. data/generators/oauth_provider/templates/oauth2_token_spec.rb +29 -0
  19. data/generators/oauth_provider/templates/oauth2_verifier.rb +28 -0
  20. data/generators/oauth_provider/templates/oauth2_verifier_spec.rb +54 -0
  21. data/generators/oauth_provider/templates/oauth_nonce_test.rb +1 -1
  22. data/generators/oauth_provider/templates/oauth_token.rb +3 -4
  23. data/generators/oauth_provider/templates/request_token.rb +1 -1
  24. data/lib/generators/active_record/oauth_consumer_generator.rb +33 -0
  25. data/lib/generators/active_record/oauth_consumer_templates/consumer_token.rb +5 -0
  26. data/lib/generators/active_record/oauth_consumer_templates/migration.rb +20 -0
  27. data/lib/generators/active_record/oauth_provider_generator.rb +39 -0
  28. data/lib/generators/active_record/oauth_provider_templates/access_token.rb +16 -0
  29. data/lib/generators/active_record/oauth_provider_templates/client_application.rb +57 -0
  30. data/lib/generators/active_record/oauth_provider_templates/migration.rb +47 -0
  31. data/lib/generators/active_record/oauth_provider_templates/oauth2_token.rb +5 -0
  32. data/lib/generators/active_record/oauth_provider_templates/oauth2_verifier.rb +28 -0
  33. data/lib/generators/active_record/oauth_provider_templates/oauth_nonce.rb +13 -0
  34. data/lib/generators/active_record/oauth_provider_templates/oauth_token.rb +30 -0
  35. data/lib/generators/active_record/oauth_provider_templates/request_token.rb +40 -0
  36. data/lib/generators/erb/oauth_consumer_generator.rb +14 -0
  37. data/lib/generators/erb/oauth_consumer_templates/index.html.erb +29 -0
  38. data/lib/generators/erb/oauth_consumer_templates/show.html.erb +7 -0
  39. data/lib/generators/erb/oauth_provider_generator.rb +21 -0
  40. data/lib/generators/erb/oauth_provider_templates/_form.html.erb +17 -0
  41. data/lib/generators/erb/oauth_provider_templates/authorize.html.erb +14 -0
  42. data/lib/generators/erb/oauth_provider_templates/authorize_failure.html.erb +1 -0
  43. data/lib/generators/erb/oauth_provider_templates/authorize_success.html.erb +1 -0
  44. data/lib/generators/erb/oauth_provider_templates/edit.html.erb +7 -0
  45. data/lib/generators/erb/oauth_provider_templates/index.html.erb +43 -0
  46. data/lib/generators/erb/oauth_provider_templates/new.html.erb +5 -0
  47. data/lib/generators/erb/oauth_provider_templates/oauth2_authorize.html.erb +16 -0
  48. data/lib/generators/erb/oauth_provider_templates/show.html.erb +27 -0
  49. data/lib/generators/haml/oauth_consumer_generator.rb +21 -0
  50. data/lib/generators/haml/oauth_consumer_templates/index.html.haml +18 -0
  51. data/lib/generators/haml/oauth_consumer_templates/show.html.haml +8 -0
  52. data/lib/generators/haml/oauth_provider_generator.rb +28 -0
  53. data/lib/generators/haml/oauth_provider_templates/_form.html.haml +21 -0
  54. data/lib/generators/haml/oauth_provider_templates/authorize.html.haml +16 -0
  55. data/lib/generators/haml/oauth_provider_templates/authorize_failure.html.haml +1 -0
  56. data/lib/generators/haml/oauth_provider_templates/authorize_success.html.haml +1 -0
  57. data/lib/generators/haml/oauth_provider_templates/edit.html.haml +4 -0
  58. data/lib/generators/haml/oauth_provider_templates/index.html.haml +39 -0
  59. data/lib/generators/haml/oauth_provider_templates/new.html.haml +5 -0
  60. data/lib/generators/haml/oauth_provider_templates/oauth2_authorize.html.haml +17 -0
  61. data/lib/generators/haml/oauth_provider_templates/show.html.haml +30 -0
  62. data/lib/generators/mongoid/oauth_consumer_generator.rb +15 -0
  63. data/lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb +14 -0
  64. data/lib/generators/mongoid/oauth_provider_generator.rb +21 -0
  65. data/lib/generators/mongoid/oauth_provider_templates/access_token.rb +16 -0
  66. data/lib/generators/mongoid/oauth_provider_templates/client_application.rb +71 -0
  67. data/lib/generators/mongoid/oauth_provider_templates/oauth2_token.rb +5 -0
  68. data/lib/generators/mongoid/oauth_provider_templates/oauth2_verifier.rb +25 -0
  69. data/lib/generators/mongoid/oauth_provider_templates/oauth_nonce.rb +24 -0
  70. data/lib/generators/mongoid/oauth_provider_templates/oauth_token.rb +44 -0
  71. data/lib/generators/mongoid/oauth_provider_templates/request_token.rb +36 -0
  72. data/lib/generators/oauth_consumer/USAGE +8 -0
  73. data/lib/generators/oauth_consumer/oauth_consumer_generator.rb +27 -0
  74. data/lib/generators/oauth_consumer/templates/controller.rb +19 -0
  75. data/lib/generators/oauth_consumer/templates/oauth_config.rb +46 -0
  76. data/lib/generators/oauth_inflections.rb +6 -0
  77. data/lib/generators/oauth_plugin.rb +0 -0
  78. data/lib/generators/oauth_provider/USAGE +18 -0
  79. data/lib/generators/oauth_provider/oauth_provider_generator.rb +47 -0
  80. data/lib/generators/oauth_provider/templates/clients_controller.rb +52 -0
  81. data/lib/generators/oauth_provider/templates/controller.rb +23 -0
  82. data/lib/generators/rspec/oauth_provider_generator.rb +39 -0
  83. data/lib/generators/rspec/templates/client_application_spec.rb +29 -0
  84. data/lib/generators/rspec/templates/client_applications.yml +23 -0
  85. data/lib/generators/rspec/templates/clients_controller_spec.rb +176 -0
  86. data/lib/generators/rspec/templates/controller_spec.rb +838 -0
  87. data/lib/generators/rspec/templates/controller_spec_helper.rb +66 -0
  88. data/lib/generators/rspec/templates/oauth2_token_spec.rb +29 -0
  89. data/lib/generators/rspec/templates/oauth2_verifier_spec.rb +54 -0
  90. data/lib/generators/rspec/templates/oauth_nonce_spec.rb +24 -0
  91. data/lib/generators/rspec/templates/oauth_nonces.yml +13 -0
  92. data/lib/generators/rspec/templates/oauth_token_spec.rb +309 -0
  93. data/lib/generators/rspec/templates/oauth_tokens.yml +17 -0
  94. data/lib/generators/test_unit/oauth_provider_generator.rb +37 -0
  95. data/lib/generators/test_unit/templates/client_application_test.rb +42 -0
  96. data/lib/generators/test_unit/templates/client_applications.yml +23 -0
  97. data/lib/generators/test_unit/templates/clients_controller_test.rb +280 -0
  98. data/lib/generators/test_unit/templates/controller_test.rb +310 -0
  99. data/lib/generators/test_unit/templates/controller_test_helper.rb +115 -0
  100. data/lib/generators/test_unit/templates/oauth_nonce_test.rb +26 -0
  101. data/lib/generators/test_unit/templates/oauth_nonces.yml +13 -0
  102. data/lib/generators/test_unit/templates/oauth_token_test.rb +57 -0
  103. data/lib/generators/test_unit/templates/oauth_tokens.yml +17 -0
  104. data/lib/oauth/controllers/application_controller_methods.rb +169 -66
  105. data/lib/oauth/controllers/provider_controller.rb +154 -38
  106. data/lib/oauth/models/consumers/service_loader.rb +2 -0
  107. data/lib/oauth/models/consumers/token.rb +0 -1
  108. data/oauth-plugin.gemspec +97 -10
  109. data/rails/init.rb +6 -2
  110. metadata +119 -12
@@ -0,0 +1,280 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require File.dirname(__FILE__) + '/../oauth_controller_test_helper'
3
+ require 'oauth/client/action_controller_request'
4
+
5
+ class OauthClientsController; def rescue_action(e) raise e end; end
6
+
7
+ class OauthClientsControllerIndexTest < ActionController::TestCase
8
+ include OAuthControllerTestHelper
9
+ tests OauthClientsController
10
+
11
+ def setup
12
+ @controller = OauthClientsController.new
13
+ @request = ActionController::TestRequest.new
14
+ @response = ActionController::TestResponse.new
15
+
16
+ login_as_application_owner
17
+ end
18
+
19
+ def do_get
20
+ get :index
21
+ end
22
+
23
+ def test_should_be_successful
24
+ do_get
25
+ assert @response.success?
26
+ end
27
+
28
+ def test_should_query_current_users_client_applications
29
+ @user.expects(:client_applications).returns(@client_applications)
30
+ do_get
31
+ end
32
+
33
+ def test_should_assign_client_applications
34
+ do_get
35
+ assert_equal @client_applications, assigns(:client_applications)
36
+ end
37
+
38
+ def test_should_render_index_template
39
+ do_get
40
+ assert_template 'index'
41
+ end
42
+ end
43
+
44
+ class OauthClientsControllerShowTest < ActionController::TestCase
45
+ include OAuthControllerTestHelper
46
+ tests OauthClientsController
47
+
48
+ def setup
49
+ @controller = OauthClientsController.new
50
+ @request = ActionController::TestRequest.new
51
+ @response = ActionController::TestResponse.new
52
+
53
+ login_as_application_owner
54
+ end
55
+
56
+ def do_get
57
+ get :show, :id=>'3'
58
+ end
59
+
60
+ def test_should_be_successful
61
+ do_get
62
+ assert @response.success?
63
+ end
64
+
65
+ def test_should_query_current_users_client_applications
66
+ @user.expects(:client_applications).returns(@client_applications)
67
+ @client_applications.expects(:find).with('3').returns(@client_application)
68
+ do_get
69
+ end
70
+
71
+ def test_should_assign_client_applications
72
+ do_get
73
+ assert_equal @client_application, assigns(:client_application)
74
+ end
75
+
76
+ def test_should_render_show_template
77
+ do_get
78
+ assert_template 'show'
79
+ end
80
+
81
+ end
82
+
83
+ class OauthClientsControllerNewTest < ActionController::TestCase
84
+ include OAuthControllerTestHelper
85
+ tests OauthClientsController
86
+
87
+ def setup
88
+ @controller = OauthClientsController.new
89
+ @request = ActionController::TestRequest.new
90
+ @response = ActionController::TestResponse.new
91
+
92
+ login_as_application_owner
93
+ ClientApplication.stubs(:new).returns(@client_application)
94
+ end
95
+
96
+ def do_get
97
+ get :new
98
+ end
99
+
100
+ def test_should_be_successful
101
+ do_get
102
+ assert @response.success?
103
+ end
104
+
105
+ def test_should_assign_client_applications
106
+ do_get
107
+ assert_equal @client_application, assigns(:client_application)
108
+ end
109
+
110
+ def test_should_render_show_template
111
+ do_get
112
+ assert_template 'new'
113
+ end
114
+
115
+ end
116
+
117
+ class OauthClientsControllerEditTest < ActionController::TestCase
118
+ include OAuthControllerTestHelper
119
+ tests OauthClientsController
120
+
121
+ def setup
122
+ @controller = OauthClientsController.new
123
+ @request = ActionController::TestRequest.new
124
+ @response = ActionController::TestResponse.new
125
+
126
+ login_as_application_owner
127
+ end
128
+
129
+ def do_get
130
+ get :edit, :id=>'3'
131
+ end
132
+
133
+ def test_should_be_successful
134
+ do_get
135
+ assert @response.success?
136
+ end
137
+
138
+ def test_should_query_current_users_client_applications
139
+ @user.expects(:client_applications).returns(@client_applications)
140
+ @client_applications.expects(:find).with('3').returns(@client_application)
141
+ do_get
142
+ end
143
+
144
+ def test_should_assign_client_applications
145
+ do_get
146
+ assert_equal @client_application, assigns(:client_application)
147
+ end
148
+
149
+ def test_should_render_edit_template
150
+ do_get
151
+ assert_template 'edit'
152
+ end
153
+
154
+ end
155
+
156
+ class OauthClientsControllerCreateTest < ActionController::TestCase
157
+ include OAuthControllerTestHelper
158
+ tests OauthClientsController
159
+
160
+ def setup
161
+ @controller = OauthClientsController.new
162
+ @request = ActionController::TestRequest.new
163
+ @response = ActionController::TestResponse.new
164
+
165
+ login_as_application_owner
166
+ @client_applications.stubs(:build).returns(@client_application)
167
+ @client_application.stubs(:save).returns(true)
168
+ end
169
+
170
+ def do_valid_post
171
+ @client_application.expects(:save).returns(true)
172
+ post :create,'client_application'=>{'name'=>'my site'}
173
+ end
174
+
175
+ def do_invalid_post
176
+ @client_application.expects(:save).returns(false)
177
+ post :create,:client_application=>{:name=>'my site'}
178
+ end
179
+
180
+ def test_should_query_current_users_client_applications
181
+ @client_applications.expects(:build).returns(@client_application)
182
+ do_valid_post
183
+ end
184
+
185
+ def test_should_redirect_to_new_client_application
186
+ do_valid_post
187
+ assert_response :redirect
188
+ assert_redirected_to(:action => "show", :id => @client_application.id)
189
+ end
190
+
191
+ def test_should_assign_client_applications
192
+ do_invalid_post
193
+ assert_equal @client_application, assigns(:client_application)
194
+ end
195
+
196
+ def test_should_render_show_template
197
+ do_invalid_post
198
+ assert_template('new')
199
+ end
200
+ end
201
+
202
+ class OauthClientsControllerDestroyTest < ActionController::TestCase
203
+ include OAuthControllerTestHelper
204
+ tests OauthClientsController
205
+
206
+ def setup
207
+ @controller = OauthClientsController.new
208
+ @request = ActionController::TestRequest.new
209
+ @response = ActionController::TestResponse.new
210
+
211
+ login_as_application_owner
212
+ @client_application.stubs(:destroy)
213
+ end
214
+
215
+ def do_delete
216
+ delete :destroy,:id=>'3'
217
+ end
218
+
219
+ def test_should_query_current_users_client_applications
220
+ @user.expects(:client_applications).returns(@client_applications)
221
+ @client_applications.expects(:find).with('3').returns(@client_application)
222
+ do_delete
223
+ end
224
+
225
+ def test_should_destroy_client_applications
226
+ @client_application.expects(:destroy)
227
+ do_delete
228
+ end
229
+
230
+ def test_should_redirect_to_list
231
+ do_delete
232
+ assert_response :redirect
233
+ assert_redirected_to :action => 'index'
234
+ end
235
+
236
+ end
237
+
238
+ class OauthClientsControllerUpdateTest < ActionController::TestCase
239
+ include OAuthControllerTestHelper
240
+ tests OauthClientsController
241
+
242
+ def setup
243
+ @controller = OauthClientsController.new
244
+ @request = ActionController::TestRequest.new
245
+ @response = ActionController::TestResponse.new
246
+ login_as_application_owner
247
+ end
248
+
249
+ def do_valid_update
250
+ @client_application.expects(:update_attributes).returns(true)
251
+ put :update, :id => '1', 'client_application' => {'name'=>'my site'}
252
+ end
253
+
254
+ def do_invalid_update
255
+ @client_application.expects(:update_attributes).returns(false)
256
+ put :update, :id=>'1', 'client_application' => {'name'=>'my site'}
257
+ end
258
+
259
+ def test_should_query_current_users_client_applications
260
+ @user.expects(:client_applications).returns(@client_applications)
261
+ @client_applications.expects(:find).with('1').returns(@client_application)
262
+ do_valid_update
263
+ end
264
+
265
+ def test_should_redirect_to_new_client_application
266
+ do_valid_update
267
+ assert_response :redirect
268
+ assert_redirected_to :action => "show", :id => @client_application.id
269
+ end
270
+
271
+ def test_should_assign_client_applications
272
+ do_invalid_update
273
+ assert_equal @client_application, assigns(:client_application)
274
+ end
275
+
276
+ def test_should_render_show_template
277
+ do_invalid_update
278
+ assert_template('edit')
279
+ end
280
+ end
@@ -0,0 +1,310 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require File.dirname(__FILE__) + '/../oauth_controller_test_helper'
3
+ require 'oauth/client/action_controller_request'
4
+
5
+ class OauthController; def rescue_action(e) raise e end; end
6
+
7
+ class OauthControllerRequestTokenTest < ActionController::TestCase
8
+ include OAuthControllerTestHelper
9
+ tests OauthController
10
+
11
+ def setup
12
+ @controller = OauthController.new
13
+ setup_oauth
14
+ sign_request_with_oauth
15
+ @client_application.stubs(:create_request_token).returns(@request_token)
16
+ end
17
+
18
+ def do_get
19
+ get :request_token
20
+ end
21
+
22
+ def test_should_be_successful
23
+ do_get
24
+ assert @response.success?
25
+ end
26
+
27
+ def test_should_query_for_client_application
28
+ ClientApplication.expects(:find_by_key).with('key').returns(@client_application)
29
+ do_get
30
+ end
31
+
32
+ def test_should_request_token_from_client_application
33
+ @client_application.expects(:create_request_token).returns(@request_token)
34
+ do_get
35
+ end
36
+
37
+ def test_should_return_token_string
38
+ do_get
39
+ assert_equal @request_token_string, @response.body
40
+ end
41
+ end
42
+
43
+ class OauthControllerTokenAuthorizationTest < ActionController::TestCase
44
+ include OAuthControllerTestHelper
45
+ tests OauthController
46
+
47
+ def setup
48
+ @controller = OauthController.new
49
+ login
50
+ setup_oauth
51
+ RequestToken.stubs(:find_by_token).returns(@request_token)
52
+ end
53
+
54
+ def do_get
55
+ get :authorize, :oauth_token => @request_token.token
56
+ end
57
+
58
+ def do_post
59
+ @request_token.expects(:authorize!).with(@user)
60
+ post :authorize,:oauth_token=>@request_token.token,:authorize=>"1"
61
+ end
62
+
63
+ def do_post_without_user_authorization
64
+ @request_token.expects(:invalidate!)
65
+ post :authorize,:oauth_token=>@request_token.token,:authorize=>"0"
66
+ end
67
+
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"
71
+ end
72
+
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"
77
+ end
78
+
79
+ def test_should_be_successful
80
+ do_get
81
+ assert @response.success?
82
+ end
83
+
84
+ def test_should_query_for_client_application
85
+ RequestToken.expects(:find_by_token).returns(@request_token)
86
+ do_get
87
+ end
88
+
89
+ def test_should_assign_token
90
+ do_get
91
+ assert_equal @request_token, assigns(:token)
92
+ end
93
+
94
+ def test_should_render_authorize_template
95
+ do_get
96
+ assert_template('authorize')
97
+ end
98
+
99
+ def test_should_redirect_to_default_callback
100
+ do_post
101
+ assert_response :redirect
102
+ assert_redirected_to("http://application/callback?oauth_token=#{@request_token.token}")
103
+ end
104
+
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}")
109
+ end
110
+
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')
115
+ end
116
+
117
+ def test_should_render_failure_screen_on_user_invalidation
118
+ do_post_without_user_authorization
119
+ assert_template('authorize_failure')
120
+ end
121
+
122
+ def test_should_render_failure_screen_if_token_is_invalidated
123
+ @request_token.expects(:invalidated?).returns(true)
124
+ do_get
125
+ assert_template('authorize_failure')
126
+ end
127
+
128
+
129
+ end
130
+
131
+ class OauthControllerGetAccessTokenTest < ActionController::TestCase
132
+ include OAuthControllerTestHelper
133
+ tests OauthController
134
+
135
+ def setup
136
+ @controller = OauthController.new
137
+ setup_oauth
138
+ sign_request_with_oauth @request_token
139
+ @request_token.stubs(:exchange!).returns(@access_token)
140
+ end
141
+
142
+ def do_get
143
+ get :access_token
144
+ end
145
+
146
+ def test_should_be_successful
147
+ do_get
148
+ assert @response.success?
149
+ end
150
+
151
+ def test_should_query_for_client_application
152
+ ClientApplication.expects(:find_token).with(@request_token.token).returns(@request_token)
153
+ do_get
154
+ end
155
+
156
+ def test_should_request_token_from_client_application
157
+ @request_token.expects(:exchange!).returns(@access_token)
158
+ do_get
159
+ end
160
+
161
+ def test_should__return_token_string
162
+ do_get
163
+ assert_equal @access_token_string, @response.body
164
+ end
165
+ end
166
+
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
171
+
172
+ def interactive
173
+ render :text => "interactive"
174
+ end
175
+
176
+ def token_only
177
+ render :text => "token"
178
+ end
179
+
180
+ def both
181
+ render :text => "both"
182
+ end
183
+ end
184
+
185
+
186
+ class OauthControllerAccessControlTest < ActionController::TestCase
187
+ include OAuthControllerTestHelper
188
+ tests OauthorizedController
189
+
190
+ def setup
191
+ @controller = OauthorizedController.new
192
+ end
193
+
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
201
+ end
202
+
203
+ def test_should_return_false_for_oauth_by_default
204
+ assert_equal false, @controller.send(:oauth?)
205
+ end
206
+
207
+ def test_should_return_nil_for_current_token_by_default
208
+ assert_nil @controller.send(:current_token)
209
+ end
210
+
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)
215
+ get :both
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?
222
+ end
223
+
224
+ def test_should_allow_interactive_when_using_login_or_oauth_required
225
+ login
226
+ get :both
227
+ assert @response.success?
228
+ assert_equal @user, @controller.send(:current_user)
229
+ assert_nil @controller.send(:current_token)
230
+ end
231
+
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)
236
+ get :token_only
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?
242
+ end
243
+
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)
248
+ get :token_only
249
+ assert_equal '401', @response.code
250
+ end
251
+
252
+ def test_should_disallow_interactive_when_using_oauth_required
253
+ login
254
+ get :token_only
255
+ assert_equal '401', @response.code
256
+
257
+ assert_equal @user, @controller.send(:current_user)
258
+ assert_nil @controller.send(:current_token)
259
+ end
260
+
261
+ def test_should_disallow_oauth_when_using_login_required
262
+ setup_to_authorize_request
263
+ sign_request_with_oauth(@access_token)
264
+ get :interactive
265
+ assert_equal "302",@response.code
266
+ assert_nil @controller.send(:current_user)
267
+ assert_nil @controller.send(:current_token)
268
+ end
269
+
270
+ def test_should_allow_interactive_when_using_login_required
271
+ login
272
+ get :interactive
273
+ assert @response.success?
274
+ assert_equal @user, @controller.send(:current_user)
275
+ assert_nil @controller.send(:current_token)
276
+ end
277
+
278
+ end
279
+
280
+ class OauthControllerRevokeTest < ActionController::TestCase
281
+ include OAuthControllerTestHelper
282
+ tests OauthController
283
+
284
+ def setup
285
+ @controller = OauthController.new
286
+ setup_oauth_for_user
287
+ @request_token.stubs(:invalidate!)
288
+ end
289
+
290
+ def do_post
291
+ post :revoke, :token => "TOKEN STRING"
292
+ end
293
+
294
+ def test_should_redirect_to_index
295
+ do_post
296
+ assert_response :redirect
297
+ assert_redirected_to('http://test.host/oauth_clients')
298
+ end
299
+
300
+ def test_should_query_current_users_tokens
301
+ @tokens.expects(:find_by_token).returns(@request_token)
302
+ do_post
303
+ end
304
+
305
+ def test_should_call_invalidate_on_token
306
+ @request_token.expects(:invalidate!)
307
+ do_post
308
+ end
309
+
310
+ end
@@ -0,0 +1,115 @@
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