3 class OAuthTest < ActionDispatch::IntegrationTest
4 fixtures :users, :client_applications
8 def test_oauth10_web_app
9 client = client_applications(:oauth_web_app)
11 post_via_redirect "/login", :username => client.user.email, :password => "test"
12 assert_response :success
14 oauth10_without_callback(client)
15 oauth10_with_callback(client, "http://another.web.app.org/callback")
16 oauth10_refused(client)
19 def test_oauth10_desktop_app
20 client = client_applications(:oauth_desktop_app)
22 post_via_redirect "/login", :username => client.user.email, :password => "test"
23 assert_response :success
25 oauth10_without_callback(client)
26 oauth10_refused(client)
29 def test_oauth10a_web_app
30 client = client_applications(:oauth_web_app)
32 post_via_redirect "/login", :username => client.user.email, :password => "test"
33 assert_response :success
35 oauth10a_without_callback(client)
36 oauth10a_with_callback(client, "http://another.web.app.org/callback")
37 oauth10a_refused(client)
40 def test_oauth10a_desktop_app
41 client = client_applications(:oauth_desktop_app)
43 post_via_redirect "/login", :username => client.user.email, :password => "test"
44 assert_response :success
46 oauth10a_without_callback(client)
47 oauth10a_refused(client)
52 def oauth10_without_callback(client)
53 token = get_request_token(client)
55 get "/oauth/authorize", :oauth_token => token.token
56 assert_response :success
57 assert_template :authorize
59 post "/oauth/authorize",
60 :oauth_token => token.token,
61 :allow_read_prefs => true, :allow_write_prefs => true
62 if client.callback_url
63 assert_response :redirect
64 assert_redirected_to "#{client.callback_url}?oauth_token=#{token.token}"
66 assert_response :success
67 assert_template :authorize_success
70 assert_not_nil token.created_at
71 assert_not_nil token.authorized_at
72 assert_nil token.invalidated_at
73 assert_allowed token, [:allow_read_prefs]
75 signed_get "/oauth/access_token", :consumer => client, :token => token
76 assert_response :success
78 assert_not_nil token.created_at
79 assert_not_nil token.authorized_at
80 assert_not_nil token.invalidated_at
81 token = parse_token(response)
82 assert_instance_of AccessToken, token
83 assert_not_nil token.created_at
84 assert_not_nil token.authorized_at
85 assert_nil token.invalidated_at
86 assert_allowed token, [:allow_read_prefs]
88 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
89 assert_response :success
91 signed_get "/api/0.6/gpx/2", :consumer => client, :token => token
92 assert_response :forbidden
94 post "/oauth/revoke", :token => token.token
95 assert_redirected_to oauth_clients_url(token.user.display_name)
96 token = OauthToken.find_by(:token => token.token)
97 assert_not_nil token.invalidated_at
99 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
100 assert_response :unauthorized
103 def oauth10_refused(client)
104 token = get_request_token(client)
106 get "/oauth/authorize", :oauth_token => token.token
107 assert_response :success
108 assert_template :authorize
110 post "/oauth/authorize", :oauth_token => token.token
111 assert_response :success
112 assert_template :authorize_failure
113 assert_select "p", "You have denied application #{client.name} access to your account."
115 assert_nil token.authorized_at
116 assert_not_nil token.invalidated_at
118 get "/oauth/authorize", :oauth_token => token.token
119 assert_response :success
120 assert_template :authorize_failure
121 assert_select "p", "The authorization token is not valid."
123 assert_nil token.authorized_at
124 assert_not_nil token.invalidated_at
126 post "/oauth/authorize", :oauth_token => token.token
127 assert_response :success
128 assert_template :authorize_failure
129 assert_select "p", "The authorization token is not valid."
131 assert_nil token.authorized_at
132 assert_not_nil token.invalidated_at
135 def oauth10_with_callback(client, callback_url)
136 token = get_request_token(client)
138 get "/oauth/authorize", :oauth_token => token.token
139 assert_response :success
140 assert_template :authorize
142 post "/oauth/authorize",
143 :oauth_token => token.token, :oauth_callback => callback_url,
144 :allow_write_api => true, :allow_read_gpx => true
145 assert_response :redirect
146 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}"
148 assert_not_nil token.created_at
149 assert_not_nil token.authorized_at
150 assert_nil token.invalidated_at
151 assert_allowed token, [:allow_write_api, :allow_read_gpx]
153 signed_get "/oauth/access_token", :consumer => client, :token => token
154 assert_response :success
156 assert_not_nil token.created_at
157 assert_not_nil token.authorized_at
158 assert_not_nil token.invalidated_at
159 token = parse_token(response)
160 assert_instance_of AccessToken, token
161 assert_not_nil token.created_at
162 assert_not_nil token.authorized_at
163 assert_nil token.invalidated_at
164 assert_allowed token, [:allow_write_api, :allow_read_gpx]
166 trace = create(:trace, :user => users(:public_user))
167 signed_get "/api/0.6/gpx/#{trace.id}", :consumer => client, :token => token
168 assert_response :success
170 signed_get "/api/0.6/user/details", :consumer => client, :token => token
171 assert_response :forbidden
173 post "/oauth/revoke", :token => token.token
174 assert_redirected_to oauth_clients_url(token.user.display_name)
175 token = OauthToken.find_by(:token => token.token)
176 assert_not_nil token.invalidated_at
178 signed_get "/api/0.6/gpx/2", :consumer => client, :token => token
179 assert_response :unauthorized
182 def oauth10a_without_callback(client)
183 token = get_request_token(client, :oauth_callback => "oob")
185 get "/oauth/authorize", :oauth_token => token.token
186 assert_response :success
187 assert_template :authorize
189 post "/oauth/authorize",
190 :oauth_token => token.token,
191 :allow_read_prefs => true, :allow_write_prefs => true
192 if client.callback_url
193 assert_response :redirect
194 verifier = parse_verifier(response)
195 assert_redirected_to "http://some.web.app.org/callback?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
197 assert_response :success
198 assert_template :authorize_success
199 m = response.body.match("<p>The verification code is ([A-Za-z0-9]+).</p>")
204 assert_not_nil token.created_at
205 assert_not_nil token.authorized_at
206 assert_nil token.invalidated_at
207 assert_allowed token, [:allow_read_prefs]
209 signed_get "/oauth/access_token", :consumer => client, :token => token
210 assert_response :unauthorized
212 signed_get "/oauth/access_token",
213 :consumer => client, :token => token, :oauth_verifier => verifier
214 assert_response :success
216 assert_not_nil token.created_at
217 assert_not_nil token.authorized_at
218 assert_not_nil token.invalidated_at
219 token = parse_token(response)
220 assert_instance_of AccessToken, token
221 assert_not_nil token.created_at
222 assert_not_nil token.authorized_at
223 assert_nil token.invalidated_at
224 assert_allowed token, [:allow_read_prefs]
226 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
227 assert_response :success
229 trace = create(:trace, :user => users(:public_user))
230 signed_get "/api/0.6/gpx/#{trace.id}", :consumer => client, :token => token
231 assert_response :forbidden
233 post "/oauth/revoke", :token => token.token
234 assert_redirected_to oauth_clients_url(token.user.display_name)
235 token = OauthToken.find_by(:token => token.token)
236 assert_not_nil token.invalidated_at
238 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
239 assert_response :unauthorized
242 def oauth10a_with_callback(client, callback_url)
243 token = get_request_token(client, :oauth_callback => callback_url)
245 get "/oauth/authorize", :oauth_token => token.token
246 assert_response :success
247 assert_template :authorize
249 post "/oauth/authorize",
250 :oauth_token => token.token,
251 :allow_write_api => true, :allow_read_gpx => true
252 assert_response :redirect
253 verifier = parse_verifier(response)
254 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
256 assert_not_nil token.created_at
257 assert_not_nil token.authorized_at
258 assert_nil token.invalidated_at
259 assert_allowed token, [:allow_write_api, :allow_read_gpx]
261 signed_get "/oauth/access_token", :consumer => client, :token => token
262 assert_response :unauthorized
264 signed_get "/oauth/access_token",
265 :consumer => client, :token => token, :oauth_verifier => verifier
266 assert_response :success
268 assert_not_nil token.created_at
269 assert_not_nil token.authorized_at
270 assert_not_nil token.invalidated_at
271 token = parse_token(response)
272 assert_instance_of AccessToken, token
273 assert_not_nil token.created_at
274 assert_not_nil token.authorized_at
275 assert_nil token.invalidated_at
276 assert_allowed token, [:allow_write_api, :allow_read_gpx]
278 trace = create(:trace, :user => users(:public_user))
279 signed_get "/api/0.6/gpx/#{trace.id}", :consumer => client, :token => token
280 assert_response :success
282 signed_get "/api/0.6/user/details", :consumer => client, :token => token
283 assert_response :forbidden
285 post "/oauth/revoke", :token => token.token
286 assert_redirected_to oauth_clients_url(token.user.display_name)
287 token = OauthToken.find_by(:token => token.token)
288 assert_not_nil token.invalidated_at
290 signed_get "/api/0.6/gpx/2", :consumer => client, :token => token
291 assert_response :unauthorized
294 def oauth10a_refused(client)
295 token = get_request_token(client, :oauth_callback => "oob")
297 get "/oauth/authorize", :oauth_token => token.token
298 assert_response :success
299 assert_template :authorize
301 post "/oauth/authorize", :oauth_token => token.token
302 assert_response :success
303 assert_template :authorize_failure
304 assert_select "p", "You have denied application #{client.name} access to your account."
306 assert_nil token.authorized_at
307 assert_not_nil token.invalidated_at
309 get "/oauth/authorize", :oauth_token => token.token
310 assert_response :success
311 assert_template :authorize_failure
312 assert_select "p", "The authorization token is not valid."
314 assert_nil token.authorized_at
315 assert_not_nil token.invalidated_at
317 post "/oauth/authorize", :oauth_token => token.token
318 assert_response :success
319 assert_template :authorize_failure
320 assert_select "p", "The authorization token is not valid."
322 assert_nil token.authorized_at
323 assert_not_nil token.invalidated_at
326 def get_request_token(client, options = {})
327 signed_get "/oauth/request_token", options.merge(:consumer => client)
328 assert_response :success
329 token = parse_token(response)
330 assert_instance_of RequestToken, token
331 assert_not_nil token.created_at
332 assert_nil token.authorized_at
333 assert_nil token.invalidated_at
334 assert_equal options[:oauth_callback], token.callback_url
335 assert_allowed token, client.permissions
340 def signed_get(uri, options)
342 uri.scheme ||= "http"
343 uri.host ||= "www.example.com"
345 helper = OAuth::Client::Helper.new(nil, options)
347 request = OAuth::RequestProxy.proxy(
350 "parameters" => helper.oauth_parameters
353 request.sign!(options)
355 get request.signed_uri
358 def parse_token(response)
359 params = CGI.parse(response.body)
361 token = OauthToken.find_by(:token => params["oauth_token"].first)
362 assert_equal token.secret, params["oauth_token_secret"].first
367 def parse_verifier(response)
368 params = CGI.parse(URI.parse(response.location).query)
370 assert_not_nil params["oauth_verifier"]
371 assert params["oauth_verifier"].first.present?
373 params["oauth_verifier"].first
376 def assert_allowed(token, allowed)
377 ClientApplication.all_permissions.each do |p|
378 assert_equal allowed.include?(p), token.attributes[p.to_s]