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 post "/login", :params => { :username => 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)
27 post "/login", :params => { :username => user.email, :password => "test" }
29 assert_response :success
31 oauth10_without_callback(client)
32 oauth10_refused(client)
35 def test_oauth10a_web_app
36 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)
41 post "/login", :params => { :username => user.email, :password => "test" }
43 assert_response :success
45 oauth10a_without_callback(client)
46 oauth10a_with_callback(client, "http://another.web.app.example.org/callback")
47 oauth10a_refused(client)
50 def test_oauth10a_desktop_app
51 client = create(:client_application, :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)
56 post "/login", :params => { :username => user.email, :password => "test" }
58 assert_response :success
60 oauth10a_without_callback(client)
61 oauth10a_refused(client)
66 def oauth10_without_callback(client)
67 token = get_request_token(client)
69 get "/oauth/authorize", :params => { :oauth_token => token.token }
70 assert_response :success
71 assert_template :authorize
73 post "/oauth/authorize",
74 :params => { :oauth_token => token.token,
75 :allow_read_prefs => "1", :allow_write_prefs => "1" }
76 if client.callback_url
77 assert_response :redirect
78 assert_redirected_to "#{client.callback_url}?oauth_token=#{token.token}"
80 assert_response :success
81 assert_template :authorize_success
84 assert_not_nil token.created_at
85 assert_not_nil token.authorized_at
86 assert_nil token.invalidated_at
87 assert_allowed token, [:allow_read_prefs]
89 signed_get "/oauth/access_token", :oauth => { :token => token }
90 assert_response :success
92 assert_not_nil token.created_at
93 assert_not_nil token.authorized_at
94 assert_not_nil token.invalidated_at
95 token = parse_token(response)
96 assert_instance_of AccessToken, token
97 assert_not_nil token.created_at
98 assert_not_nil token.authorized_at
99 assert_nil token.invalidated_at
100 assert_allowed token, [:allow_read_prefs]
102 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
103 assert_response :success
105 signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
106 assert_response :forbidden
108 post "/oauth/revoke", :params => { :token => token.token }
109 assert_redirected_to oauth_clients_url(token.user.display_name)
110 token = OauthToken.find_by(:token => token.token)
111 assert_not_nil token.invalidated_at
113 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
114 assert_response :unauthorized
117 def oauth10_refused(client)
118 token = get_request_token(client)
120 get "/oauth/authorize", :params => { :oauth_token => token.token }
121 assert_response :success
122 assert_template :authorize
124 post "/oauth/authorize", :params => { :oauth_token => token.token }
125 assert_response :success
126 assert_template :authorize_failure
127 assert_select "p", "You have denied application #{client.name} access to your account."
129 assert_nil token.authorized_at
130 assert_not_nil token.invalidated_at
132 get "/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
140 post "/oauth/authorize", :params => { :oauth_token => token.token }
141 assert_response :success
142 assert_template :authorize_failure
143 assert_select "p", "The authorization token is not valid."
145 assert_nil token.authorized_at
146 assert_not_nil token.invalidated_at
149 def oauth10_with_callback(client, callback_url)
150 token = get_request_token(client)
152 get "/oauth/authorize", :params => { :oauth_token => token.token }
153 assert_response :success
154 assert_template :authorize
156 post "/oauth/authorize",
157 :params => { :oauth_token => token.token, :oauth_callback => callback_url,
158 :allow_write_api => "1", :allow_read_gpx => "1" }
159 assert_response :redirect
160 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}"
162 assert_not_nil token.created_at
163 assert_not_nil token.authorized_at
164 assert_nil token.invalidated_at
165 assert_allowed token, [:allow_write_api, :allow_read_gpx]
167 signed_get "/oauth/access_token", :oauth => { :token => token }
168 assert_response :success
170 assert_not_nil token.created_at
171 assert_not_nil token.authorized_at
172 assert_not_nil token.invalidated_at
173 token = parse_token(response)
174 assert_instance_of AccessToken, token
175 assert_not_nil token.created_at
176 assert_not_nil token.authorized_at
177 assert_nil token.invalidated_at
178 assert_allowed token, [:allow_write_api, :allow_read_gpx]
180 trace = create(:trace, :user => token.user)
181 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
182 assert_response :success
184 signed_get "/api/0.6/user/details", :oauth => { :token => token }
185 assert_response :forbidden
187 post "/oauth/revoke", :params => { :token => token.token }
188 assert_redirected_to oauth_clients_url(token.user.display_name)
189 token = OauthToken.find_by(:token => token.token)
190 assert_not_nil token.invalidated_at
192 signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
193 assert_response :unauthorized
196 def oauth10a_without_callback(client)
197 token = get_request_token(client, :oauth_callback => "oob")
199 get "/oauth/authorize", :params => { :oauth_token => token.token }
200 assert_response :success
201 assert_template :authorize
203 post "/oauth/authorize",
204 :params => { :oauth_token => token.token,
205 :allow_read_prefs => "1", :allow_write_prefs => "1" }
206 if client.callback_url
207 assert_response :redirect
208 verifier = parse_verifier(response)
209 assert_redirected_to "http://some.web.app.example.org/callback?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
211 assert_response :success
212 assert_template :authorize_success
213 m = response.body.match("<p>The verification code is ([A-Za-z0-9]+).</p>")
218 assert_not_nil token.created_at
219 assert_not_nil token.authorized_at
220 assert_nil token.invalidated_at
221 assert_allowed token, [:allow_read_prefs]
223 signed_get "/oauth/access_token", :oauth => { :token => token }
224 assert_response :unauthorized
226 signed_get "/oauth/access_token", :oauth => { :token => token, :oauth_verifier => verifier }
227 assert_response :success
229 assert_not_nil token.created_at
230 assert_not_nil token.authorized_at
231 assert_not_nil token.invalidated_at
232 token = parse_token(response)
233 assert_instance_of AccessToken, token
234 assert_not_nil token.created_at
235 assert_not_nil token.authorized_at
236 assert_nil token.invalidated_at
237 assert_allowed token, [:allow_read_prefs]
239 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
240 assert_response :success
242 trace = create(:trace, :user => token.user)
243 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
244 assert_response :forbidden
246 post "/oauth/revoke", :params => { :token => token.token }
247 assert_redirected_to oauth_clients_url(token.user.display_name)
248 token = OauthToken.find_by(:token => token.token)
249 assert_not_nil token.invalidated_at
251 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
252 assert_response :unauthorized
255 def oauth10a_with_callback(client, callback_url)
256 token = get_request_token(client, :oauth_callback => callback_url)
258 get "/oauth/authorize", :params => { :oauth_token => token.token }
259 assert_response :success
260 assert_template :authorize
262 post "/oauth/authorize",
263 :params => { :oauth_token => token.token,
264 :allow_write_api => "1", :allow_read_gpx => "1" }
265 assert_response :redirect
266 verifier = parse_verifier(response)
267 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
269 assert_not_nil token.created_at
270 assert_not_nil token.authorized_at
271 assert_nil token.invalidated_at
272 assert_allowed token, [:allow_write_api, :allow_read_gpx]
274 signed_get "/oauth/access_token", :oauth => { :token => token }
275 assert_response :unauthorized
277 signed_get "/oauth/access_token", :oauth => { :token => token, :oauth_verifier => verifier }
278 assert_response :success
280 assert_not_nil token.created_at
281 assert_not_nil token.authorized_at
282 assert_not_nil token.invalidated_at
283 token = parse_token(response)
284 assert_instance_of AccessToken, token
285 assert_not_nil token.created_at
286 assert_not_nil token.authorized_at
287 assert_nil token.invalidated_at
288 assert_allowed token, [:allow_write_api, :allow_read_gpx]
290 trace = create(:trace, :user => token.user)
291 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
292 assert_response :success
294 signed_get "/api/0.6/user/details", :oauth => { :token => token }
295 assert_response :forbidden
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]