3 class OAuthTest < ActionDispatch::IntegrationTest
6 def test_oauth10_web_app
7 client = create(:client_application, :callback_url => "http://some.web.app.example.org/callback", :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)
12 oauth10_without_callback(client)
13 oauth10_with_callback(client, "http://another.web.app.example.org/callback")
14 oauth10_refused(client)
17 def test_oauth10_desktop_app
18 client = create(:client_application, :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)
23 oauth10_without_callback(client)
24 oauth10_refused(client)
27 def test_oauth10a_web_app
28 client = create(:client_application, :callback_url => "http://some.web.app.example.org/callback", :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)
33 oauth10a_without_callback(client)
34 oauth10a_with_callback(client, "http://another.web.app.example.org/callback")
35 oauth10a_refused(client)
38 def test_oauth10a_desktop_app
39 client = create(:client_application, :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)
44 oauth10a_without_callback(client)
45 oauth10a_refused(client)
50 def oauth10_without_callback(client)
51 token = get_request_token(client)
53 get "/oauth/authorize", :params => { :oauth_token => token.token }
54 assert_response :success
55 assert_template :authorize
57 post "/oauth/authorize",
58 :params => { :oauth_token => token.token,
59 :allow_read_prefs => "1", :allow_write_prefs => "1" }
60 if client.callback_url
61 assert_response :redirect
62 assert_redirected_to "#{client.callback_url}?oauth_token=#{token.token}"
64 assert_response :success
65 assert_template :authorize_success
68 assert_not_nil token.created_at
69 assert_not_nil token.authorized_at
70 assert_nil token.invalidated_at
71 assert_allowed token, [:allow_read_prefs]
73 signed_get "/oauth/access_token", :oauth => { :token => token }
74 assert_response :success
76 assert_not_nil token.created_at
77 assert_not_nil token.authorized_at
78 assert_not_nil token.invalidated_at
79 token = parse_token(response)
80 assert_instance_of AccessToken, token
81 assert_not_nil token.created_at
82 assert_not_nil token.authorized_at
83 assert_nil token.invalidated_at
84 assert_allowed token, [:allow_read_prefs]
88 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
89 assert_response :success
91 signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
92 assert_response :forbidden
95 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
96 assert_response :forbidden
99 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
100 assert_response :forbidden
103 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
104 assert_response :success
106 session_for(token.user)
108 post "/oauth/revoke", :params => { :token => token.token }
109 assert_redirected_to oauth_clients_url(token.user.display_name)
110 token = OauthToken.find_by(:token => token.token)
111 assert_not_nil token.invalidated_at
113 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
114 assert_response :unauthorized
117 def oauth10_refused(client)
118 token = get_request_token(client)
120 get "/oauth/authorize", :params => { :oauth_token => token.token }
121 assert_response :success
122 assert_template :authorize
124 post "/oauth/authorize", :params => { :oauth_token => token.token }
125 assert_response :success
126 assert_template :authorize_failure
127 assert_select "p", "You have denied application #{client.name} access to your account."
129 assert_nil token.authorized_at
130 assert_not_nil token.invalidated_at
132 get "/oauth/authorize", :params => { :oauth_token => token.token }
133 assert_response :success
134 assert_template :authorize_failure
135 assert_select "p", "The authorization token is not valid."
137 assert_nil token.authorized_at
138 assert_not_nil token.invalidated_at
140 post "/oauth/authorize", :params => { :oauth_token => token.token }
141 assert_response :success
142 assert_template :authorize_failure
143 assert_select "p", "The authorization token is not valid."
145 assert_nil token.authorized_at
146 assert_not_nil token.invalidated_at
149 def oauth10_with_callback(client, callback_url)
150 token = get_request_token(client)
152 get "/oauth/authorize", :params => { :oauth_token => token.token }
153 assert_response :success
154 assert_template :authorize
156 post "/oauth/authorize",
157 :params => { :oauth_token => token.token, :oauth_callback => callback_url,
158 :allow_write_api => "1", :allow_read_gpx => "1" }
159 assert_response :redirect
160 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}"
162 assert_not_nil token.created_at
163 assert_not_nil token.authorized_at
164 assert_nil token.invalidated_at
165 assert_allowed token, [:allow_write_api, :allow_read_gpx]
167 signed_get "/oauth/access_token", :oauth => { :token => token }
168 assert_response :success
170 assert_not_nil token.created_at
171 assert_not_nil token.authorized_at
172 assert_not_nil token.invalidated_at
173 token = parse_token(response)
174 assert_instance_of AccessToken, token
175 assert_not_nil token.created_at
176 assert_not_nil token.authorized_at
177 assert_nil token.invalidated_at
178 assert_allowed token, [:allow_write_api, :allow_read_gpx]
182 trace = create(:trace, :user => token.user)
183 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
184 assert_response :success
186 signed_get "/api/0.6/user/details", :oauth => { :token => token }
187 assert_response :forbidden
190 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
191 assert_response :forbidden
194 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
195 assert_response :forbidden
198 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
199 assert_response :success
201 session_for(token.user)
203 post "/oauth/revoke", :params => { :token => token.token }
204 assert_redirected_to oauth_clients_url(token.user.display_name)
205 token = OauthToken.find_by(:token => token.token)
206 assert_not_nil token.invalidated_at
208 signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
209 assert_response :unauthorized
212 def oauth10a_without_callback(client)
213 token = get_request_token(client, :oauth_callback => "oob")
215 get "/oauth/authorize", :params => { :oauth_token => token.token }
216 assert_response :success
217 assert_template :authorize
219 post "/oauth/authorize",
220 :params => { :oauth_token => token.token,
221 :allow_read_prefs => "1", :allow_write_prefs => "1" }
222 if client.callback_url
223 assert_response :redirect
224 verifier = parse_verifier(response)
225 assert_redirected_to "http://some.web.app.example.org/callback?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
227 assert_response :success
228 assert_template :authorize_success
229 m = response.body.match("<p>The verification code is ([A-Za-z0-9]+).</p>")
234 assert_not_nil token.created_at
235 assert_not_nil token.authorized_at
236 assert_nil token.invalidated_at
237 assert_allowed token, [:allow_read_prefs]
239 signed_get "/oauth/access_token", :oauth => { :token => token }
240 assert_response :unauthorized
242 signed_get "/oauth/access_token", :oauth => { :token => token, :oauth_verifier => verifier }
243 assert_response :success
245 assert_not_nil token.created_at
246 assert_not_nil token.authorized_at
247 assert_not_nil token.invalidated_at
248 token = parse_token(response)
249 assert_instance_of AccessToken, token
250 assert_not_nil token.created_at
251 assert_not_nil token.authorized_at
252 assert_nil token.invalidated_at
253 assert_allowed token, [:allow_read_prefs]
257 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
258 assert_response :success
260 trace = create(:trace, :user => token.user)
261 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
262 assert_response :forbidden
265 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
266 assert_response :forbidden
269 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
270 assert_response :forbidden
273 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
274 assert_response :success
276 session_for(token.user)
278 post "/oauth/revoke", :params => { :token => token.token }
279 assert_redirected_to oauth_clients_url(token.user.display_name)
280 token = OauthToken.find_by(:token => token.token)
281 assert_not_nil token.invalidated_at
283 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
284 assert_response :unauthorized
287 def oauth10a_with_callback(client, callback_url)
288 token = get_request_token(client, :oauth_callback => callback_url)
290 get "/oauth/authorize", :params => { :oauth_token => token.token }
291 assert_response :success
292 assert_template :authorize
294 post "/oauth/authorize",
295 :params => { :oauth_token => token.token,
296 :allow_write_api => "1", :allow_read_gpx => "1" }
297 assert_response :redirect
298 verifier = parse_verifier(response)
299 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
301 assert_not_nil token.created_at
302 assert_not_nil token.authorized_at
303 assert_nil token.invalidated_at
304 assert_allowed token, [:allow_write_api, :allow_read_gpx]
306 signed_get "/oauth/access_token", :oauth => { :token => token }
307 assert_response :unauthorized
309 signed_get "/oauth/access_token", :oauth => { :token => token, :oauth_verifier => verifier }
310 assert_response :success
312 assert_not_nil token.created_at
313 assert_not_nil token.authorized_at
314 assert_not_nil token.invalidated_at
315 token = parse_token(response)
316 assert_instance_of AccessToken, token
317 assert_not_nil token.created_at
318 assert_not_nil token.authorized_at
319 assert_nil token.invalidated_at
320 assert_allowed token, [:allow_write_api, :allow_read_gpx]
324 trace = create(:trace, :user => token.user)
325 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
326 assert_response :success
328 signed_get "/api/0.6/user/details", :oauth => { :token => token }
329 assert_response :forbidden
332 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
333 assert_response :forbidden
336 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
337 assert_response :forbidden
340 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
341 assert_response :success
343 session_for(token.user)
345 post "/oauth/revoke", :params => { :token => token.token }
346 assert_redirected_to oauth_clients_url(token.user.display_name)
347 token = OauthToken.find_by(:token => token.token)
348 assert_not_nil token.invalidated_at
350 signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
351 assert_response :unauthorized
354 def oauth10a_refused(client)
355 token = get_request_token(client, :oauth_callback => "oob")
357 get "/oauth/authorize", :params => { :oauth_token => token.token }
358 assert_response :success
359 assert_template :authorize
361 post "/oauth/authorize", :params => { :oauth_token => token.token }
362 assert_response :success
363 assert_template :authorize_failure
364 assert_select "p", "You have denied application #{client.name} access to your account."
366 assert_nil token.authorized_at
367 assert_not_nil token.invalidated_at
369 get "/oauth/authorize", :params => { :oauth_token => token.token }
370 assert_response :success
371 assert_template :authorize_failure
372 assert_select "p", "The authorization token is not valid."
374 assert_nil token.authorized_at
375 assert_not_nil token.invalidated_at
377 post "/oauth/authorize", :params => { :oauth_token => token.token }
378 assert_response :success
379 assert_template :authorize_failure
380 assert_select "p", "The authorization token is not valid."
382 assert_nil token.authorized_at
383 assert_not_nil token.invalidated_at
386 def get_request_token(client, options = {})
387 signed_get "/oauth/request_token", :oauth => options.merge(:consumer => client)
388 assert_response :success
389 token = parse_token(response)
390 assert_instance_of RequestToken, token
391 assert_not_nil token.created_at
392 assert_nil token.authorized_at
393 assert_nil token.invalidated_at
394 assert_equal_allowing_nil options[:oauth_callback], token.callback_url
395 assert_allowed token, client.permissions
400 def parse_token(response)
401 params = CGI.parse(response.body)
403 token = OauthToken.find_by(:token => params["oauth_token"].first)
404 assert_equal token.secret, params["oauth_token_secret"].first
409 def parse_verifier(response)
410 params = CGI.parse(URI.parse(response.location).query)
412 assert_not_nil params["oauth_verifier"]
413 assert_predicate params["oauth_verifier"].first, :present?
415 params["oauth_verifier"].first
418 def assert_allowed(token, allowed)
419 ClientApplication.all_permissions.each do |p|
420 assert_equal allowed.include?(p), token.attributes[p.to_s]