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)
9 post "/login", :params => { :username => client.user.email, :password => "test" }
12 assert_response :success
14 oauth10_without_callback(client)
15 oauth10_with_callback(client, "http://another.web.app.example.org/callback")
16 oauth10_refused(client)
19 def test_oauth10_desktop_app
20 client = create(:client_application, :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)
22 post "/login", :params => { :username => client.user.email, :password => "test" }
25 assert_response :success
27 oauth10_without_callback(client)
28 oauth10_refused(client)
31 def test_oauth10a_web_app
32 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)
34 post "/login", :params => { :username => client.user.email, :password => "test" }
37 assert_response :success
39 oauth10a_without_callback(client)
40 oauth10a_with_callback(client, "http://another.web.app.example.org/callback")
41 oauth10a_refused(client)
44 def test_oauth10a_desktop_app
45 client = create(:client_application, :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)
47 post "/login", :params => { :username => client.user.email, :password => "test" }
50 assert_response :success
52 oauth10a_without_callback(client)
53 oauth10a_refused(client)
58 def oauth10_without_callback(client)
59 token = get_request_token(client)
61 get "/oauth/authorize", :params => { :oauth_token => token.token }
62 assert_response :success
63 assert_template :authorize
65 post "/oauth/authorize",
66 :params => { :oauth_token => token.token,
67 :allow_read_prefs => true, :allow_write_prefs => true }
68 if client.callback_url
69 assert_response :redirect
70 assert_redirected_to "#{client.callback_url}?oauth_token=#{token.token}"
72 assert_response :success
73 assert_template :authorize_success
76 assert_not_nil token.created_at
77 assert_not_nil token.authorized_at
78 assert_nil token.invalidated_at
79 assert_allowed token, [:allow_read_prefs]
81 signed_get "/oauth/access_token", :consumer => client, :token => token
82 assert_response :success
84 assert_not_nil token.created_at
85 assert_not_nil token.authorized_at
86 assert_not_nil token.invalidated_at
87 token = parse_token(response)
88 assert_instance_of AccessToken, token
89 assert_not_nil token.created_at
90 assert_not_nil token.authorized_at
91 assert_nil token.invalidated_at
92 assert_allowed token, [:allow_read_prefs]
94 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
95 assert_response :success
97 signed_get "/api/0.6/gpx/2", :consumer => client, :token => token
98 assert_response :forbidden
100 post "/oauth/revoke", :params => { :token => token.token }
101 assert_redirected_to oauth_clients_url(token.user.display_name)
102 token = OauthToken.find_by(:token => token.token)
103 assert_not_nil token.invalidated_at
105 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
106 assert_response :unauthorized
109 def oauth10_refused(client)
110 token = get_request_token(client)
112 get "/oauth/authorize", :params => { :oauth_token => token.token }
113 assert_response :success
114 assert_template :authorize
116 post "/oauth/authorize", :params => { :oauth_token => token.token }
117 assert_response :success
118 assert_template :authorize_failure
119 assert_select "p", "You have denied application #{client.name} access to your account."
121 assert_nil token.authorized_at
122 assert_not_nil token.invalidated_at
124 get "/oauth/authorize", :params => { :oauth_token => token.token }
125 assert_response :success
126 assert_template :authorize_failure
127 assert_select "p", "The authorization token is not valid."
129 assert_nil token.authorized_at
130 assert_not_nil token.invalidated_at
132 post "/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
141 def oauth10_with_callback(client, callback_url)
142 token = get_request_token(client)
144 get "/oauth/authorize", :params => { :oauth_token => token.token }
145 assert_response :success
146 assert_template :authorize
148 post "/oauth/authorize",
149 :params => { :oauth_token => token.token, :oauth_callback => callback_url,
150 :allow_write_api => true, :allow_read_gpx => true }
151 assert_response :redirect
152 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}"
154 assert_not_nil token.created_at
155 assert_not_nil token.authorized_at
156 assert_nil token.invalidated_at
157 assert_allowed token, [:allow_write_api, :allow_read_gpx]
159 signed_get "/oauth/access_token", :consumer => client, :token => token
160 assert_response :success
162 assert_not_nil token.created_at
163 assert_not_nil token.authorized_at
164 assert_not_nil token.invalidated_at
165 token = parse_token(response)
166 assert_instance_of AccessToken, token
167 assert_not_nil token.created_at
168 assert_not_nil token.authorized_at
169 assert_nil token.invalidated_at
170 assert_allowed token, [:allow_write_api, :allow_read_gpx]
172 trace = create(:trace, :user => client.user)
173 signed_get "/api/0.6/gpx/#{trace.id}", :consumer => client, :token => token
174 assert_response :success
176 signed_get "/api/0.6/user/details", :consumer => client, :token => token
177 assert_response :forbidden
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", :consumer => client, :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 => true, :allow_write_prefs => true }
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", :consumer => client, :token => token
216 assert_response :unauthorized
218 signed_get "/oauth/access_token",
219 :consumer => client, :token => token, :oauth_verifier => verifier
220 assert_response :success
222 assert_not_nil token.created_at
223 assert_not_nil token.authorized_at
224 assert_not_nil token.invalidated_at
225 token = parse_token(response)
226 assert_instance_of AccessToken, token
227 assert_not_nil token.created_at
228 assert_not_nil token.authorized_at
229 assert_nil token.invalidated_at
230 assert_allowed token, [:allow_read_prefs]
232 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
233 assert_response :success
235 trace = create(:trace, :user => client.user)
236 signed_get "/api/0.6/gpx/#{trace.id}", :consumer => client, :token => token
237 assert_response :forbidden
239 post "/oauth/revoke", :params => { :token => token.token }
240 assert_redirected_to oauth_clients_url(token.user.display_name)
241 token = OauthToken.find_by(:token => token.token)
242 assert_not_nil token.invalidated_at
244 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
245 assert_response :unauthorized
248 def oauth10a_with_callback(client, callback_url)
249 token = get_request_token(client, :oauth_callback => callback_url)
251 get "/oauth/authorize", :params => { :oauth_token => token.token }
252 assert_response :success
253 assert_template :authorize
255 post "/oauth/authorize",
256 :params => { :oauth_token => token.token,
257 :allow_write_api => true, :allow_read_gpx => true }
258 assert_response :redirect
259 verifier = parse_verifier(response)
260 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
262 assert_not_nil token.created_at
263 assert_not_nil token.authorized_at
264 assert_nil token.invalidated_at
265 assert_allowed token, [:allow_write_api, :allow_read_gpx]
267 signed_get "/oauth/access_token", :consumer => client, :token => token
268 assert_response :unauthorized
270 signed_get "/oauth/access_token",
271 :consumer => client, :token => token, :oauth_verifier => verifier
272 assert_response :success
274 assert_not_nil token.created_at
275 assert_not_nil token.authorized_at
276 assert_not_nil token.invalidated_at
277 token = parse_token(response)
278 assert_instance_of AccessToken, token
279 assert_not_nil token.created_at
280 assert_not_nil token.authorized_at
281 assert_nil token.invalidated_at
282 assert_allowed token, [:allow_write_api, :allow_read_gpx]
284 trace = create(:trace, :user => client.user)
285 signed_get "/api/0.6/gpx/#{trace.id}", :consumer => client, :token => token
286 assert_response :success
288 signed_get "/api/0.6/user/details", :consumer => client, :token => token
289 assert_response :forbidden
291 post "/oauth/revoke", :params => { :token => token.token }
292 assert_redirected_to oauth_clients_url(token.user.display_name)
293 token = OauthToken.find_by(:token => token.token)
294 assert_not_nil token.invalidated_at
296 signed_get "/api/0.6/gpx/2", :consumer => client, :token => token
297 assert_response :unauthorized
300 def oauth10a_refused(client)
301 token = get_request_token(client, :oauth_callback => "oob")
303 get "/oauth/authorize", :params => { :oauth_token => token.token }
304 assert_response :success
305 assert_template :authorize
307 post "/oauth/authorize", :params => { :oauth_token => token.token }
308 assert_response :success
309 assert_template :authorize_failure
310 assert_select "p", "You have denied application #{client.name} access to your account."
312 assert_nil token.authorized_at
313 assert_not_nil token.invalidated_at
315 get "/oauth/authorize", :params => { :oauth_token => token.token }
316 assert_response :success
317 assert_template :authorize_failure
318 assert_select "p", "The authorization token is not valid."
320 assert_nil token.authorized_at
321 assert_not_nil token.invalidated_at
323 post "/oauth/authorize", :params => { :oauth_token => token.token }
324 assert_response :success
325 assert_template :authorize_failure
326 assert_select "p", "The authorization token is not valid."
328 assert_nil token.authorized_at
329 assert_not_nil token.invalidated_at
332 def get_request_token(client, options = {})
333 signed_get "/oauth/request_token", options.merge(:consumer => client)
334 assert_response :success
335 token = parse_token(response)
336 assert_instance_of RequestToken, token
337 assert_not_nil token.created_at
338 assert_nil token.authorized_at
339 assert_nil token.invalidated_at
340 assert_equal_allowing_nil options[:oauth_callback], token.callback_url
341 assert_allowed token, client.permissions
346 def signed_get(uri, options)
348 uri.scheme ||= "http"
349 uri.host ||= "www.example.com"
351 helper = OAuth::Client::Helper.new(nil, options)
353 request = OAuth::RequestProxy.proxy(
356 "parameters" => helper.oauth_parameters
359 request.sign!(options)
361 get request.signed_uri
364 def parse_token(response)
365 params = CGI.parse(response.body)
367 token = OauthToken.find_by(:token => params["oauth_token"].first)
368 assert_equal token.secret, params["oauth_token_secret"].first
373 def parse_verifier(response)
374 params = CGI.parse(URI.parse(response.location).query)
376 assert_not_nil params["oauth_verifier"]
377 assert params["oauth_verifier"].first.present?
379 params["oauth_verifier"].first
382 def assert_allowed(token, allowed)
383 ClientApplication.all_permissions.each do |p|
384 assert_equal allowed.include?(p), token.attributes[p.to_s]