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
94 session_for(token.user)
96 post "/oauth/revoke", :params => { :token => token.token }
97 assert_redirected_to oauth_clients_url(token.user.display_name)
98 token = OauthToken.find_by(:token => token.token)
99 assert_not_nil token.invalidated_at
101 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
102 assert_response :unauthorized
105 def oauth10_refused(client)
106 token = get_request_token(client)
108 get "/oauth/authorize", :params => { :oauth_token => token.token }
109 assert_response :success
110 assert_template :authorize
112 post "/oauth/authorize", :params => { :oauth_token => token.token }
113 assert_response :success
114 assert_template :authorize_failure
115 assert_select "p", "You have denied application #{client.name} access to your account."
117 assert_nil token.authorized_at
118 assert_not_nil token.invalidated_at
120 get "/oauth/authorize", :params => { :oauth_token => token.token }
121 assert_response :success
122 assert_template :authorize_failure
123 assert_select "p", "The authorization token is not valid."
125 assert_nil token.authorized_at
126 assert_not_nil token.invalidated_at
128 post "/oauth/authorize", :params => { :oauth_token => token.token }
129 assert_response :success
130 assert_template :authorize_failure
131 assert_select "p", "The authorization token is not valid."
133 assert_nil token.authorized_at
134 assert_not_nil token.invalidated_at
137 def oauth10_with_callback(client, callback_url)
138 token = get_request_token(client)
140 get "/oauth/authorize", :params => { :oauth_token => token.token }
141 assert_response :success
142 assert_template :authorize
144 post "/oauth/authorize",
145 :params => { :oauth_token => token.token, :oauth_callback => callback_url,
146 :allow_write_api => "1", :allow_read_gpx => "1" }
147 assert_response :redirect
148 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}"
150 assert_not_nil token.created_at
151 assert_not_nil token.authorized_at
152 assert_nil token.invalidated_at
153 assert_allowed token, [:allow_write_api, :allow_read_gpx]
155 signed_get "/oauth/access_token", :oauth => { :token => token }
156 assert_response :success
158 assert_not_nil token.created_at
159 assert_not_nil token.authorized_at
160 assert_not_nil token.invalidated_at
161 token = parse_token(response)
162 assert_instance_of AccessToken, token
163 assert_not_nil token.created_at
164 assert_not_nil token.authorized_at
165 assert_nil token.invalidated_at
166 assert_allowed token, [:allow_write_api, :allow_read_gpx]
170 trace = create(:trace, :user => token.user)
171 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
172 assert_response :success
174 signed_get "/api/0.6/user/details", :oauth => { :token => token }
175 assert_response :forbidden
177 session_for(token.user)
179 post "/oauth/revoke", :params => { :token => token.token }
180 assert_redirected_to oauth_clients_url(token.user.display_name)
181 token = OauthToken.find_by(:token => token.token)
182 assert_not_nil token.invalidated_at
184 signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
185 assert_response :unauthorized
188 def oauth10a_without_callback(client)
189 token = get_request_token(client, :oauth_callback => "oob")
191 get "/oauth/authorize", :params => { :oauth_token => token.token }
192 assert_response :success
193 assert_template :authorize
195 post "/oauth/authorize",
196 :params => { :oauth_token => token.token,
197 :allow_read_prefs => "1", :allow_write_prefs => "1" }
198 if client.callback_url
199 assert_response :redirect
200 verifier = parse_verifier(response)
201 assert_redirected_to "http://some.web.app.example.org/callback?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
203 assert_response :success
204 assert_template :authorize_success
205 m = response.body.match("<p>The verification code is ([A-Za-z0-9]+).</p>")
210 assert_not_nil token.created_at
211 assert_not_nil token.authorized_at
212 assert_nil token.invalidated_at
213 assert_allowed token, [:allow_read_prefs]
215 signed_get "/oauth/access_token", :oauth => { :token => token }
216 assert_response :unauthorized
218 signed_get "/oauth/access_token", :oauth => { :token => token, :oauth_verifier => verifier }
219 assert_response :success
221 assert_not_nil token.created_at
222 assert_not_nil token.authorized_at
223 assert_not_nil token.invalidated_at
224 token = parse_token(response)
225 assert_instance_of AccessToken, token
226 assert_not_nil token.created_at
227 assert_not_nil token.authorized_at
228 assert_nil token.invalidated_at
229 assert_allowed token, [:allow_read_prefs]
233 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
234 assert_response :success
236 trace = create(:trace, :user => token.user)
237 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
238 assert_response :forbidden
240 session_for(token.user)
242 post "/oauth/revoke", :params => { :token => token.token }
243 assert_redirected_to oauth_clients_url(token.user.display_name)
244 token = OauthToken.find_by(:token => token.token)
245 assert_not_nil token.invalidated_at
247 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
248 assert_response :unauthorized
251 def oauth10a_with_callback(client, callback_url)
252 token = get_request_token(client, :oauth_callback => callback_url)
254 get "/oauth/authorize", :params => { :oauth_token => token.token }
255 assert_response :success
256 assert_template :authorize
258 post "/oauth/authorize",
259 :params => { :oauth_token => token.token,
260 :allow_write_api => "1", :allow_read_gpx => "1" }
261 assert_response :redirect
262 verifier = parse_verifier(response)
263 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
265 assert_not_nil token.created_at
266 assert_not_nil token.authorized_at
267 assert_nil token.invalidated_at
268 assert_allowed token, [:allow_write_api, :allow_read_gpx]
270 signed_get "/oauth/access_token", :oauth => { :token => token }
271 assert_response :unauthorized
273 signed_get "/oauth/access_token", :oauth => { :token => token, :oauth_verifier => verifier }
274 assert_response :success
276 assert_not_nil token.created_at
277 assert_not_nil token.authorized_at
278 assert_not_nil token.invalidated_at
279 token = parse_token(response)
280 assert_instance_of AccessToken, token
281 assert_not_nil token.created_at
282 assert_not_nil token.authorized_at
283 assert_nil token.invalidated_at
284 assert_allowed token, [:allow_write_api, :allow_read_gpx]
288 trace = create(:trace, :user => token.user)
289 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
290 assert_response :success
292 signed_get "/api/0.6/user/details", :oauth => { :token => token }
293 assert_response :forbidden
295 session_for(token.user)
297 post "/oauth/revoke", :params => { :token => token.token }
298 assert_redirected_to oauth_clients_url(token.user.display_name)
299 token = OauthToken.find_by(:token => token.token)
300 assert_not_nil token.invalidated_at
302 signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
303 assert_response :unauthorized
306 def oauth10a_refused(client)
307 token = get_request_token(client, :oauth_callback => "oob")
309 get "/oauth/authorize", :params => { :oauth_token => token.token }
310 assert_response :success
311 assert_template :authorize
313 post "/oauth/authorize", :params => { :oauth_token => token.token }
314 assert_response :success
315 assert_template :authorize_failure
316 assert_select "p", "You have denied application #{client.name} access to your account."
318 assert_nil token.authorized_at
319 assert_not_nil token.invalidated_at
321 get "/oauth/authorize", :params => { :oauth_token => token.token }
322 assert_response :success
323 assert_template :authorize_failure
324 assert_select "p", "The authorization token is not valid."
326 assert_nil token.authorized_at
327 assert_not_nil token.invalidated_at
329 post "/oauth/authorize", :params => { :oauth_token => token.token }
330 assert_response :success
331 assert_template :authorize_failure
332 assert_select "p", "The authorization token is not valid."
334 assert_nil token.authorized_at
335 assert_not_nil token.invalidated_at
338 def get_request_token(client, options = {})
339 signed_get "/oauth/request_token", :oauth => options.merge(:consumer => client)
340 assert_response :success
341 token = parse_token(response)
342 assert_instance_of RequestToken, token
343 assert_not_nil token.created_at
344 assert_nil token.authorized_at
345 assert_nil token.invalidated_at
346 assert_equal_allowing_nil options[:oauth_callback], token.callback_url
347 assert_allowed token, client.permissions
352 def parse_token(response)
353 params = CGI.parse(response.body)
355 token = OauthToken.find_by(:token => params["oauth_token"].first)
356 assert_equal token.secret, params["oauth_token_secret"].first
361 def parse_verifier(response)
362 params = CGI.parse(URI.parse(response.location).query)
364 assert_not_nil params["oauth_verifier"]
365 assert_predicate params["oauth_verifier"].first, :present?
367 params["oauth_verifier"].first
370 def assert_allowed(token, allowed)
371 ClientApplication.all_permissions.each do |p|
372 assert_equal allowed.include?(p), token.attributes[p.to_s]