3 class OAuthTest < ActionDispatch::IntegrationTest
10 def test_oauth10_web_app
11 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)
13 post_via_redirect "/login", :username => client.user.email, :password => "test"
14 assert_response :success
16 oauth10_without_callback(client)
17 oauth10_with_callback(client, "http://another.web.app.example.org/callback")
18 oauth10_refused(client)
21 def test_oauth10_desktop_app
22 client = create(:client_application, :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)
24 post_via_redirect "/login", :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_via_redirect "/login", :username => client.user.email, :password => "test"
35 assert_response :success
37 oauth10a_without_callback(client)
38 oauth10a_with_callback(client, "http://another.web.app.example.org/callback")
39 oauth10a_refused(client)
42 def test_oauth10a_desktop_app
43 client = create(:client_application, :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)
45 post_via_redirect "/login", :username => client.user.email, :password => "test"
46 assert_response :success
48 oauth10a_without_callback(client)
49 oauth10a_refused(client)
54 def oauth10_without_callback(client)
55 token = get_request_token(client)
57 get "/oauth/authorize", :oauth_token => token.token
58 assert_response :success
59 assert_template :authorize
61 post "/oauth/authorize",
62 :oauth_token => token.token,
63 :allow_read_prefs => true, :allow_write_prefs => true
64 if client.callback_url
65 assert_response :redirect
66 assert_redirected_to "#{client.callback_url}?oauth_token=#{token.token}"
68 assert_response :success
69 assert_template :authorize_success
72 assert_not_nil token.created_at
73 assert_not_nil token.authorized_at
74 assert_nil token.invalidated_at
75 assert_allowed token, [:allow_read_prefs]
77 signed_get "/oauth/access_token", :consumer => client, :token => token
78 assert_response :success
80 assert_not_nil token.created_at
81 assert_not_nil token.authorized_at
82 assert_not_nil token.invalidated_at
83 token = parse_token(response)
84 assert_instance_of AccessToken, token
85 assert_not_nil token.created_at
86 assert_not_nil token.authorized_at
87 assert_nil token.invalidated_at
88 assert_allowed token, [:allow_read_prefs]
90 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
91 assert_response :success
93 signed_get "/api/0.6/gpx/2", :consumer => client, :token => token
94 assert_response :forbidden
96 post "/oauth/revoke", :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", :consumer => client, :token => token
102 assert_response :unauthorized
105 def oauth10_refused(client)
106 token = get_request_token(client)
108 get "/oauth/authorize", :oauth_token => token.token
109 assert_response :success
110 assert_template :authorize
112 post "/oauth/authorize", :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", :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", :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", :oauth_token => token.token
141 assert_response :success
142 assert_template :authorize
144 post "/oauth/authorize",
145 :oauth_token => token.token, :oauth_callback => callback_url,
146 :allow_write_api => true, :allow_read_gpx => true
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", :consumer => client, :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]
168 trace = create(:trace, :user => client.user)
169 signed_get "/api/0.6/gpx/#{trace.id}", :consumer => client, :token => token
170 assert_response :success
172 signed_get "/api/0.6/user/details", :consumer => client, :token => token
173 assert_response :forbidden
175 post "/oauth/revoke", :token => token.token
176 assert_redirected_to oauth_clients_url(token.user.display_name)
177 token = OauthToken.find_by(:token => token.token)
178 assert_not_nil token.invalidated_at
180 signed_get "/api/0.6/gpx/2", :consumer => client, :token => token
181 assert_response :unauthorized
184 def oauth10a_without_callback(client)
185 token = get_request_token(client, :oauth_callback => "oob")
187 get "/oauth/authorize", :oauth_token => token.token
188 assert_response :success
189 assert_template :authorize
191 post "/oauth/authorize",
192 :oauth_token => token.token,
193 :allow_read_prefs => true, :allow_write_prefs => true
194 if client.callback_url
195 assert_response :redirect
196 verifier = parse_verifier(response)
197 assert_redirected_to "http://some.web.app.example.org/callback?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
199 assert_response :success
200 assert_template :authorize_success
201 m = response.body.match("<p>The verification code is ([A-Za-z0-9]+).</p>")
206 assert_not_nil token.created_at
207 assert_not_nil token.authorized_at
208 assert_nil token.invalidated_at
209 assert_allowed token, [:allow_read_prefs]
211 signed_get "/oauth/access_token", :consumer => client, :token => token
212 assert_response :unauthorized
214 signed_get "/oauth/access_token",
215 :consumer => client, :token => token, :oauth_verifier => verifier
216 assert_response :success
218 assert_not_nil token.created_at
219 assert_not_nil token.authorized_at
220 assert_not_nil token.invalidated_at
221 token = parse_token(response)
222 assert_instance_of AccessToken, token
223 assert_not_nil token.created_at
224 assert_not_nil token.authorized_at
225 assert_nil token.invalidated_at
226 assert_allowed token, [:allow_read_prefs]
228 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
229 assert_response :success
231 trace = create(:trace, :user => client.user)
232 signed_get "/api/0.6/gpx/#{trace.id}", :consumer => client, :token => token
233 assert_response :forbidden
235 post "/oauth/revoke", :token => token.token
236 assert_redirected_to oauth_clients_url(token.user.display_name)
237 token = OauthToken.find_by(:token => token.token)
238 assert_not_nil token.invalidated_at
240 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
241 assert_response :unauthorized
244 def oauth10a_with_callback(client, callback_url)
245 token = get_request_token(client, :oauth_callback => callback_url)
247 get "/oauth/authorize", :oauth_token => token.token
248 assert_response :success
249 assert_template :authorize
251 post "/oauth/authorize",
252 :oauth_token => token.token,
253 :allow_write_api => true, :allow_read_gpx => true
254 assert_response :redirect
255 verifier = parse_verifier(response)
256 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
258 assert_not_nil token.created_at
259 assert_not_nil token.authorized_at
260 assert_nil token.invalidated_at
261 assert_allowed token, [:allow_write_api, :allow_read_gpx]
263 signed_get "/oauth/access_token", :consumer => client, :token => token
264 assert_response :unauthorized
266 signed_get "/oauth/access_token",
267 :consumer => client, :token => token, :oauth_verifier => verifier
268 assert_response :success
270 assert_not_nil token.created_at
271 assert_not_nil token.authorized_at
272 assert_not_nil token.invalidated_at
273 token = parse_token(response)
274 assert_instance_of AccessToken, token
275 assert_not_nil token.created_at
276 assert_not_nil token.authorized_at
277 assert_nil token.invalidated_at
278 assert_allowed token, [:allow_write_api, :allow_read_gpx]
280 trace = create(:trace, :user => client.user)
281 signed_get "/api/0.6/gpx/#{trace.id}", :consumer => client, :token => token
282 assert_response :success
284 signed_get "/api/0.6/user/details", :consumer => client, :token => token
285 assert_response :forbidden
287 post "/oauth/revoke", :token => token.token
288 assert_redirected_to oauth_clients_url(token.user.display_name)
289 token = OauthToken.find_by(:token => token.token)
290 assert_not_nil token.invalidated_at
292 signed_get "/api/0.6/gpx/2", :consumer => client, :token => token
293 assert_response :unauthorized
296 def oauth10a_refused(client)
297 token = get_request_token(client, :oauth_callback => "oob")
299 get "/oauth/authorize", :oauth_token => token.token
300 assert_response :success
301 assert_template :authorize
303 post "/oauth/authorize", :oauth_token => token.token
304 assert_response :success
305 assert_template :authorize_failure
306 assert_select "p", "You have denied application #{client.name} access to your account."
308 assert_nil token.authorized_at
309 assert_not_nil token.invalidated_at
311 get "/oauth/authorize", :oauth_token => token.token
312 assert_response :success
313 assert_template :authorize_failure
314 assert_select "p", "The authorization token is not valid."
316 assert_nil token.authorized_at
317 assert_not_nil token.invalidated_at
319 post "/oauth/authorize", :oauth_token => token.token
320 assert_response :success
321 assert_template :authorize_failure
322 assert_select "p", "The authorization token is not valid."
324 assert_nil token.authorized_at
325 assert_not_nil token.invalidated_at
328 def get_request_token(client, options = {})
329 signed_get "/oauth/request_token", options.merge(:consumer => client)
330 assert_response :success
331 token = parse_token(response)
332 assert_instance_of RequestToken, token
333 assert_not_nil token.created_at
334 assert_nil token.authorized_at
335 assert_nil token.invalidated_at
336 assert_equal_allowing_nil options[:oauth_callback], token.callback_url
337 assert_allowed token, client.permissions
342 def signed_get(uri, options)
344 uri.scheme ||= "http"
345 uri.host ||= "www.example.com"
347 helper = OAuth::Client::Helper.new(nil, options)
349 request = OAuth::RequestProxy.proxy(
352 "parameters" => helper.oauth_parameters
355 request.sign!(options)
357 get request.signed_uri
360 def parse_token(response)
361 params = CGI.parse(response.body)
363 token = OauthToken.find_by(:token => params["oauth_token"].first)
364 assert_equal token.secret, params["oauth_token_secret"].first
369 def parse_verifier(response)
370 params = CGI.parse(URI.parse(response.location).query)
372 assert_not_nil params["oauth_verifier"]
373 assert params["oauth_verifier"].first.present?
375 params["oauth_verifier"].first
378 def assert_allowed(token, allowed)
379 ClientApplication.all_permissions.each do |p|
380 assert_equal allowed.include?(p), token.attributes[p.to_s]