3 class OAuthTest < ActionDispatch::IntegrationTest
4 fixtures :users, :client_applications, :gpx_files
5 set_fixture_class :gpx_files => Trace
13 def test_oauth10_web_app
14 client = client_applications(:oauth_web_app)
16 post_via_redirect "/login", :username => client.user.email, :password => "test"
17 assert_response :success
19 oauth10_without_callback(client)
20 oauth10_with_callback(client, "http://another.web.app.org/callback")
21 oauth10_refused(client)
24 def test_oauth10_desktop_app
25 client = client_applications(:oauth_desktop_app)
27 post_via_redirect "/login", :username => client.user.email, :password => "test"
28 assert_response :success
30 oauth10_without_callback(client)
31 oauth10_refused(client)
34 def test_oauth10a_web_app
35 client = client_applications(:oauth_web_app)
37 post_via_redirect "/login", :username => client.user.email, :password => "test"
38 assert_response :success
40 oauth10a_without_callback(client)
41 oauth10a_with_callback(client, "http://another.web.app.org/callback")
42 oauth10a_refused(client)
45 def test_oauth10a_desktop_app
46 client = client_applications(:oauth_desktop_app)
48 post_via_redirect "/login", :username => client.user.email, :password => "test"
49 assert_response :success
51 oauth10a_without_callback(client)
52 oauth10a_refused(client)
57 def oauth10_without_callback(client)
58 token = get_request_token(client)
60 get "/oauth/authorize", :oauth_token => token.token
61 assert_response :success
62 assert_template :authorize
64 post "/oauth/authorize",
65 :oauth_token => token.token,
66 :allow_read_prefs => true, :allow_write_prefs => true
67 if client.callback_url
68 assert_response :redirect
69 assert_redirected_to "#{client.callback_url}?oauth_token=#{token.token}"
71 assert_response :success
72 assert_template :authorize_success
75 assert_not_nil token.created_at
76 assert_not_nil token.authorized_at
77 assert_nil token.invalidated_at
78 assert_allowed token, [:allow_read_prefs]
80 signed_get "/oauth/access_token", :consumer => client, :token => token
81 assert_response :success
83 assert_not_nil token.created_at
84 assert_not_nil token.authorized_at
85 assert_not_nil token.invalidated_at
86 token = parse_token(response)
87 assert_instance_of AccessToken, token
88 assert_not_nil token.created_at
89 assert_not_nil token.authorized_at
90 assert_nil token.invalidated_at
91 assert_allowed token, [:allow_read_prefs]
93 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
94 assert_response :success
96 signed_get "/api/0.6/gpx/2", :consumer => client, :token => token
97 assert_response :forbidden
99 post "/oauth/revoke", :token => token.token
100 assert_redirected_to oauth_clients_url(token.user.display_name)
101 token = OauthToken.find_by(:token => token.token)
102 assert_not_nil token.invalidated_at
104 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
105 assert_response :unauthorized
108 def oauth10_refused(client)
109 token = get_request_token(client)
111 get "/oauth/authorize", :oauth_token => token.token
112 assert_response :success
113 assert_template :authorize
115 post "/oauth/authorize", :oauth_token => token.token
116 assert_response :success
117 assert_template :authorize_failure
118 assert_select "p", "You have denied application #{client.name} access to your account."
120 assert_nil token.authorized_at
121 assert_not_nil token.invalidated_at
123 get "/oauth/authorize", :oauth_token => token.token
124 assert_response :success
125 assert_template :authorize_failure
126 assert_select "p", "The authorization token is not valid."
128 assert_nil token.authorized_at
129 assert_not_nil token.invalidated_at
131 post "/oauth/authorize", :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
140 def oauth10_with_callback(client, callback_url)
141 token = get_request_token(client)
143 get "/oauth/authorize", :oauth_token => token.token
144 assert_response :success
145 assert_template :authorize
147 post "/oauth/authorize",
148 :oauth_token => token.token, :oauth_callback => callback_url,
149 :allow_write_api => true, :allow_read_gpx => true
150 assert_response :redirect
151 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}"
153 assert_not_nil token.created_at
154 assert_not_nil token.authorized_at
155 assert_nil token.invalidated_at
156 assert_allowed token, [:allow_write_api, :allow_read_gpx]
158 signed_get "/oauth/access_token", :consumer => client, :token => token
159 assert_response :success
161 assert_not_nil token.created_at
162 assert_not_nil token.authorized_at
163 assert_not_nil token.invalidated_at
164 token = parse_token(response)
165 assert_instance_of AccessToken, token
166 assert_not_nil token.created_at
167 assert_not_nil token.authorized_at
168 assert_nil token.invalidated_at
169 assert_allowed token, [:allow_write_api, :allow_read_gpx]
171 signed_get "/api/0.6/gpx/2", :consumer => client, :token => token
172 assert_response :success
174 signed_get "/api/0.6/user/details", :consumer => client, :token => token
175 assert_response :forbidden
177 post "/oauth/revoke", :token => token.token
178 assert_redirected_to oauth_clients_url(token.user.display_name)
179 token = OauthToken.find_by(:token => token.token)
180 assert_not_nil token.invalidated_at
182 signed_get "/api/0.6/gpx/2", :consumer => client, :token => token
183 assert_response :unauthorized
186 def oauth10a_without_callback(client)
187 token = get_request_token(client, :oauth_callback => "oob")
189 get "/oauth/authorize", :oauth_token => token.token
190 assert_response :success
191 assert_template :authorize
193 post "/oauth/authorize",
194 :oauth_token => token.token,
195 :allow_read_prefs => true, :allow_write_prefs => true
196 if client.callback_url
197 assert_response :redirect
198 verifier = parse_verifier(response)
199 assert_redirected_to "http://some.web.app.org/callback?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
201 assert_response :success
202 assert_template :authorize_success
203 m = response.body.match("<p>The verification code is ([A-Za-z0-9]+).</p>")
208 assert_not_nil token.created_at
209 assert_not_nil token.authorized_at
210 assert_nil token.invalidated_at
211 assert_allowed token, [:allow_read_prefs]
213 signed_get "/oauth/access_token", :consumer => client, :token => token
214 assert_response :unauthorized
216 signed_get "/oauth/access_token",
217 :consumer => client, :token => token, :oauth_verifier => verifier
218 assert_response :success
220 assert_not_nil token.created_at
221 assert_not_nil token.authorized_at
222 assert_not_nil token.invalidated_at
223 token = parse_token(response)
224 assert_instance_of AccessToken, token
225 assert_not_nil token.created_at
226 assert_not_nil token.authorized_at
227 assert_nil token.invalidated_at
228 assert_allowed token, [:allow_read_prefs]
230 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
231 assert_response :success
233 signed_get "/api/0.6/gpx/2", :consumer => client, :token => token
234 assert_response :forbidden
236 post "/oauth/revoke", :token => token.token
237 assert_redirected_to oauth_clients_url(token.user.display_name)
238 token = OauthToken.find_by(:token => token.token)
239 assert_not_nil token.invalidated_at
241 signed_get "/api/0.6/user/preferences", :consumer => client, :token => token
242 assert_response :unauthorized
245 def oauth10a_with_callback(client, callback_url)
246 token = get_request_token(client, :oauth_callback => callback_url)
248 get "/oauth/authorize", :oauth_token => token.token
249 assert_response :success
250 assert_template :authorize
252 post "/oauth/authorize",
253 :oauth_token => token.token,
254 :allow_write_api => true, :allow_read_gpx => true
255 assert_response :redirect
256 verifier = parse_verifier(response)
257 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
259 assert_not_nil token.created_at
260 assert_not_nil token.authorized_at
261 assert_nil token.invalidated_at
262 assert_allowed token, [:allow_write_api, :allow_read_gpx]
264 signed_get "/oauth/access_token", :consumer => client, :token => token
265 assert_response :unauthorized
267 signed_get "/oauth/access_token",
268 :consumer => client, :token => token, :oauth_verifier => verifier
269 assert_response :success
271 assert_not_nil token.created_at
272 assert_not_nil token.authorized_at
273 assert_not_nil token.invalidated_at
274 token = parse_token(response)
275 assert_instance_of AccessToken, token
276 assert_not_nil token.created_at
277 assert_not_nil token.authorized_at
278 assert_nil token.invalidated_at
279 assert_allowed token, [:allow_write_api, :allow_read_gpx]
281 signed_get "/api/0.6/gpx/2", :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]