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_redirected_to "#{client.callback_url}?oauth_token=#{token.token}"
63 assert_response :success
64 assert_template :authorize_success
67 assert_not_nil token.created_at
68 assert_not_nil token.authorized_at
69 assert_nil token.invalidated_at
70 assert_allowed token, [:allow_read_prefs]
72 signed_get "/oauth/access_token", :oauth => { :token => token }
73 assert_response :success
75 assert_not_nil token.created_at
76 assert_not_nil token.authorized_at
77 assert_not_nil token.invalidated_at
78 token = parse_token(response)
79 assert_instance_of AccessToken, token
80 assert_not_nil token.created_at
81 assert_not_nil token.authorized_at
82 assert_nil token.invalidated_at
83 assert_allowed token, [:allow_read_prefs]
87 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
88 assert_response :success
90 signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
91 assert_response :forbidden
94 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
95 assert_response :forbidden
98 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
99 assert_response :forbidden
102 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
103 assert_response :success
105 session_for(token.user)
107 post "/oauth/revoke", :params => { :token => token.token }
108 assert_redirected_to oauth_clients_url(token.user.display_name)
109 token = OauthToken.find_by(:token => token.token)
110 assert_not_nil token.invalidated_at
112 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
113 assert_response :unauthorized
116 def oauth10_refused(client)
117 token = get_request_token(client)
119 get "/oauth/authorize", :params => { :oauth_token => token.token }
120 assert_response :success
121 assert_template :authorize
123 post "/oauth/authorize", :params => { :oauth_token => token.token }
124 assert_response :success
125 assert_template :authorize_failure
126 assert_select "p", "You have denied application #{client.name} access to your account."
128 assert_nil token.authorized_at
129 assert_not_nil token.invalidated_at
131 get "/oauth/authorize", :params => { :oauth_token => token.token }
132 assert_response :success
133 assert_template :authorize_failure
134 assert_select "p", "The authorization token is not valid."
136 assert_nil token.authorized_at
137 assert_not_nil token.invalidated_at
139 post "/oauth/authorize", :params => { :oauth_token => token.token }
140 assert_response :success
141 assert_template :authorize_failure
142 assert_select "p", "The authorization token is not valid."
144 assert_nil token.authorized_at
145 assert_not_nil token.invalidated_at
148 def oauth10_with_callback(client, callback_url)
149 token = get_request_token(client)
151 get "/oauth/authorize", :params => { :oauth_token => token.token }
152 assert_response :success
153 assert_template :authorize
155 post "/oauth/authorize",
156 :params => { :oauth_token => token.token, :oauth_callback => callback_url,
157 :allow_write_api => "1", :allow_read_gpx => "1" }
158 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}"
160 assert_not_nil token.created_at
161 assert_not_nil token.authorized_at
162 assert_nil token.invalidated_at
163 assert_allowed token, [:allow_write_api, :allow_read_gpx]
165 signed_get "/oauth/access_token", :oauth => { :token => token }
166 assert_response :success
168 assert_not_nil token.created_at
169 assert_not_nil token.authorized_at
170 assert_not_nil token.invalidated_at
171 token = parse_token(response)
172 assert_instance_of AccessToken, token
173 assert_not_nil token.created_at
174 assert_not_nil token.authorized_at
175 assert_nil token.invalidated_at
176 assert_allowed token, [:allow_write_api, :allow_read_gpx]
180 trace = create(:trace, :user => token.user)
181 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
182 assert_response :success
184 signed_get "/api/0.6/user/details", :oauth => { :token => token }
185 assert_response :forbidden
188 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
189 assert_response :forbidden
192 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
193 assert_response :forbidden
196 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
197 assert_response :success
199 session_for(token.user)
201 post "/oauth/revoke", :params => { :token => token.token }
202 assert_redirected_to oauth_clients_url(token.user.display_name)
203 token = OauthToken.find_by(:token => token.token)
204 assert_not_nil token.invalidated_at
206 signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
207 assert_response :unauthorized
210 def oauth10a_without_callback(client)
211 token = get_request_token(client, :oauth_callback => "oob")
213 get "/oauth/authorize", :params => { :oauth_token => token.token }
214 assert_response :success
215 assert_template :authorize
217 post "/oauth/authorize",
218 :params => { :oauth_token => token.token,
219 :allow_read_prefs => "1", :allow_write_prefs => "1" }
220 if client.callback_url
221 assert_response :redirect
222 verifier = parse_verifier(response)
223 assert_redirected_to "http://some.web.app.example.org/callback?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
225 assert_response :success
226 assert_template :authorize_success
227 m = response.body.match("<p>The verification code is ([A-Za-z0-9]+).</p>")
232 assert_not_nil token.created_at
233 assert_not_nil token.authorized_at
234 assert_nil token.invalidated_at
235 assert_allowed token, [:allow_read_prefs]
237 signed_get "/oauth/access_token", :oauth => { :token => token }
238 assert_response :unauthorized
240 signed_get "/oauth/access_token", :oauth => { :token => token, :oauth_verifier => verifier }
241 assert_response :success
243 assert_not_nil token.created_at
244 assert_not_nil token.authorized_at
245 assert_not_nil token.invalidated_at
246 token = parse_token(response)
247 assert_instance_of AccessToken, token
248 assert_not_nil token.created_at
249 assert_not_nil token.authorized_at
250 assert_nil token.invalidated_at
251 assert_allowed token, [:allow_read_prefs]
255 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
256 assert_response :success
258 trace = create(:trace, :user => token.user)
259 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
260 assert_response :forbidden
263 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
264 assert_response :forbidden
267 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
268 assert_response :forbidden
271 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
272 assert_response :success
274 session_for(token.user)
276 post "/oauth/revoke", :params => { :token => token.token }
277 assert_redirected_to oauth_clients_url(token.user.display_name)
278 token = OauthToken.find_by(:token => token.token)
279 assert_not_nil token.invalidated_at
281 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
282 assert_response :unauthorized
285 def oauth10a_with_callback(client, callback_url)
286 token = get_request_token(client, :oauth_callback => callback_url)
288 get "/oauth/authorize", :params => { :oauth_token => token.token }
289 assert_response :success
290 assert_template :authorize
292 post "/oauth/authorize",
293 :params => { :oauth_token => token.token,
294 :allow_write_api => "1", :allow_read_gpx => "1" }
295 assert_response :redirect
296 verifier = parse_verifier(response)
297 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
299 assert_not_nil token.created_at
300 assert_not_nil token.authorized_at
301 assert_nil token.invalidated_at
302 assert_allowed token, [:allow_write_api, :allow_read_gpx]
304 signed_get "/oauth/access_token", :oauth => { :token => token }
305 assert_response :unauthorized
307 signed_get "/oauth/access_token", :oauth => { :token => token, :oauth_verifier => verifier }
308 assert_response :success
310 assert_not_nil token.created_at
311 assert_not_nil token.authorized_at
312 assert_not_nil token.invalidated_at
313 token = parse_token(response)
314 assert_instance_of AccessToken, token
315 assert_not_nil token.created_at
316 assert_not_nil token.authorized_at
317 assert_nil token.invalidated_at
318 assert_allowed token, [:allow_write_api, :allow_read_gpx]
322 trace = create(:trace, :user => token.user)
323 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
324 assert_response :success
326 signed_get "/api/0.6/user/details", :oauth => { :token => token }
327 assert_response :forbidden
330 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
331 assert_response :forbidden
334 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
335 assert_response :forbidden
338 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
339 assert_response :success
341 session_for(token.user)
343 post "/oauth/revoke", :params => { :token => token.token }
344 assert_redirected_to oauth_clients_url(token.user.display_name)
345 token = OauthToken.find_by(:token => token.token)
346 assert_not_nil token.invalidated_at
348 signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
349 assert_response :unauthorized
352 def oauth10a_refused(client)
353 token = get_request_token(client, :oauth_callback => "oob")
355 get "/oauth/authorize", :params => { :oauth_token => token.token }
356 assert_response :success
357 assert_template :authorize
359 post "/oauth/authorize", :params => { :oauth_token => token.token }
360 assert_response :success
361 assert_template :authorize_failure
362 assert_select "p", "You have denied application #{client.name} access to your account."
364 assert_nil token.authorized_at
365 assert_not_nil token.invalidated_at
367 get "/oauth/authorize", :params => { :oauth_token => token.token }
368 assert_response :success
369 assert_template :authorize_failure
370 assert_select "p", "The authorization token is not valid."
372 assert_nil token.authorized_at
373 assert_not_nil token.invalidated_at
375 post "/oauth/authorize", :params => { :oauth_token => token.token }
376 assert_response :success
377 assert_template :authorize_failure
378 assert_select "p", "The authorization token is not valid."
380 assert_nil token.authorized_at
381 assert_not_nil token.invalidated_at
384 def get_request_token(client, options = {})
385 signed_get "/oauth/request_token", :oauth => options.merge(:consumer => client)
386 assert_response :success
387 token = parse_token(response)
388 assert_instance_of RequestToken, token
389 assert_not_nil token.created_at
390 assert_nil token.authorized_at
391 assert_nil token.invalidated_at
392 assert_equal_allowing_nil options[:oauth_callback], token.callback_url
393 assert_allowed token, client.permissions
398 def parse_token(response)
399 params = CGI.parse(response.body)
401 token = OauthToken.find_by(:token => params["oauth_token"].first)
402 assert_equal token.secret, params["oauth_token_secret"].first
407 def parse_verifier(response)
408 params = CGI.parse(URI.parse(response.location).query)
410 assert_not_nil params["oauth_verifier"]
411 assert_predicate params["oauth_verifier"].first, :present?
413 params["oauth_verifier"].first
416 def assert_allowed(token, allowed)
417 ClientApplication.all_permissions.each do |p|
418 assert_equal allowed.include?(p), token.attributes[p.to_s]