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", :oauth => { :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", :oauth => { :token => token }
95 assert_response :success
97 signed_get "/api/0.6/gpx/2", :oauth => { :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", :oauth => { :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", :oauth => { :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}", :oauth => { :token => token }
174 assert_response :success
176 signed_get "/api/0.6/user/details", :oauth => { :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", :oauth => { :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", :oauth => { :token => token }
216 assert_response :unauthorized
218 signed_get "/oauth/access_token", :oauth => { :token => token, :oauth_verifier => verifier }
219 assert_response :success
221 assert_not_nil token.created_at
222 assert_not_nil token.authorized_at
223 assert_not_nil token.invalidated_at
224 token = parse_token(response)
225 assert_instance_of AccessToken, token
226 assert_not_nil token.created_at
227 assert_not_nil token.authorized_at
228 assert_nil token.invalidated_at
229 assert_allowed token, [:allow_read_prefs]
231 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
232 assert_response :success
234 trace = create(:trace, :user => client.user)
235 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
236 assert_response :forbidden
238 post "/oauth/revoke", :params => { :token => token.token }
239 assert_redirected_to oauth_clients_url(token.user.display_name)
240 token = OauthToken.find_by(:token => token.token)
241 assert_not_nil token.invalidated_at
243 signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
244 assert_response :unauthorized
247 def oauth10a_with_callback(client, callback_url)
248 token = get_request_token(client, :oauth_callback => callback_url)
250 get "/oauth/authorize", :params => { :oauth_token => token.token }
251 assert_response :success
252 assert_template :authorize
254 post "/oauth/authorize",
255 :params => { :oauth_token => token.token,
256 :allow_write_api => true, :allow_read_gpx => true }
257 assert_response :redirect
258 verifier = parse_verifier(response)
259 assert_redirected_to "#{callback_url}?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
261 assert_not_nil token.created_at
262 assert_not_nil token.authorized_at
263 assert_nil token.invalidated_at
264 assert_allowed token, [:allow_write_api, :allow_read_gpx]
266 signed_get "/oauth/access_token", :oauth => { :token => token }
267 assert_response :unauthorized
269 signed_get "/oauth/access_token", :oauth => { :token => token, :oauth_verifier => verifier }
270 assert_response :success
272 assert_not_nil token.created_at
273 assert_not_nil token.authorized_at
274 assert_not_nil token.invalidated_at
275 token = parse_token(response)
276 assert_instance_of AccessToken, token
277 assert_not_nil token.created_at
278 assert_not_nil token.authorized_at
279 assert_nil token.invalidated_at
280 assert_allowed token, [:allow_write_api, :allow_read_gpx]
282 trace = create(:trace, :user => client.user)
283 signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
284 assert_response :success
286 signed_get "/api/0.6/user/details", :oauth => { :token => token }
287 assert_response :forbidden
289 post "/oauth/revoke", :params => { :token => token.token }
290 assert_redirected_to oauth_clients_url(token.user.display_name)
291 token = OauthToken.find_by(:token => token.token)
292 assert_not_nil token.invalidated_at
294 signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
295 assert_response :unauthorized
298 def oauth10a_refused(client)
299 token = get_request_token(client, :oauth_callback => "oob")
301 get "/oauth/authorize", :params => { :oauth_token => token.token }
302 assert_response :success
303 assert_template :authorize
305 post "/oauth/authorize", :params => { :oauth_token => token.token }
306 assert_response :success
307 assert_template :authorize_failure
308 assert_select "p", "You have denied application #{client.name} access to your account."
310 assert_nil token.authorized_at
311 assert_not_nil token.invalidated_at
313 get "/oauth/authorize", :params => { :oauth_token => token.token }
314 assert_response :success
315 assert_template :authorize_failure
316 assert_select "p", "The authorization token is not valid."
318 assert_nil token.authorized_at
319 assert_not_nil token.invalidated_at
321 post "/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
330 def get_request_token(client, options = {})
331 signed_get "/oauth/request_token", :oauth => options.merge(:consumer => client)
332 assert_response :success
333 token = parse_token(response)
334 assert_instance_of RequestToken, token
335 assert_not_nil token.created_at
336 assert_nil token.authorized_at
337 assert_nil token.invalidated_at
338 assert_equal_allowing_nil options[:oauth_callback], token.callback_url
339 assert_allowed token, client.permissions
344 def parse_token(response)
345 params = CGI.parse(response.body)
347 token = OauthToken.find_by(:token => params["oauth_token"].first)
348 assert_equal token.secret, params["oauth_token_secret"].first
353 def parse_verifier(response)
354 params = CGI.parse(URI.parse(response.location).query)
356 assert_not_nil params["oauth_verifier"]
357 assert params["oauth_verifier"].first.present?
359 params["oauth_verifier"].first
362 def assert_allowed(token, allowed)
363 ClientApplication.all_permissions.each do |p|
364 assert_equal allowed.include?(p), token.attributes[p.to_s]