]> git.openstreetmap.org Git - rails.git/blob - test/integration/oauth_test.rb
Merge remote-tracking branch 'upstream/pull/3065'
[rails.git] / test / integration / oauth_test.rb
1 require "test_helper"
2
3 class OAuthTest < ActionDispatch::IntegrationTest
4   include OAuth::Helper
5
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)
8
9     post "/login", :params => { :username => client.user.email, :password => "test" }
10     follow_redirect!
11     follow_redirect!
12     assert_response :success
13
14     oauth10_without_callback(client)
15     oauth10_with_callback(client, "http://another.web.app.example.org/callback")
16     oauth10_refused(client)
17   end
18
19   def test_oauth10_desktop_app
20     client = create(:client_application, :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)
21
22     post "/login", :params => { :username => client.user.email, :password => "test" }
23     follow_redirect!
24     follow_redirect!
25     assert_response :success
26
27     oauth10_without_callback(client)
28     oauth10_refused(client)
29   end
30
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)
33
34     post "/login", :params => { :username => client.user.email, :password => "test" }
35     follow_redirect!
36     follow_redirect!
37     assert_response :success
38
39     oauth10a_without_callback(client)
40     oauth10a_with_callback(client, "http://another.web.app.example.org/callback")
41     oauth10a_refused(client)
42   end
43
44   def test_oauth10a_desktop_app
45     client = create(:client_application, :allow_read_prefs => true, :allow_write_api => true, :allow_read_gpx => true)
46
47     post "/login", :params => { :username => client.user.email, :password => "test" }
48     follow_redirect!
49     follow_redirect!
50     assert_response :success
51
52     oauth10a_without_callback(client)
53     oauth10a_refused(client)
54   end
55
56   private
57
58   def oauth10_without_callback(client)
59     token = get_request_token(client)
60
61     get "/oauth/authorize", :params => { :oauth_token => token.token }
62     assert_response :success
63     assert_template :authorize
64
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}"
71     else
72       assert_response :success
73       assert_template :authorize_success
74     end
75     token.reload
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]
80
81     signed_get "/oauth/access_token", :oauth => { :token => token }
82     assert_response :success
83     token.reload
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]
93
94     signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
95     assert_response :success
96
97     signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
98     assert_response :forbidden
99
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
104
105     signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
106     assert_response :unauthorized
107   end
108
109   def oauth10_refused(client)
110     token = get_request_token(client)
111
112     get "/oauth/authorize", :params => { :oauth_token => token.token }
113     assert_response :success
114     assert_template :authorize
115
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."
120     token.reload
121     assert_nil token.authorized_at
122     assert_not_nil token.invalidated_at
123
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."
128     token.reload
129     assert_nil token.authorized_at
130     assert_not_nil token.invalidated_at
131
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."
136     token.reload
137     assert_nil token.authorized_at
138     assert_not_nil token.invalidated_at
139   end
140
141   def oauth10_with_callback(client, callback_url)
142     token = get_request_token(client)
143
144     get "/oauth/authorize", :params => { :oauth_token => token.token }
145     assert_response :success
146     assert_template :authorize
147
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}"
153     token.reload
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]
158
159     signed_get "/oauth/access_token", :oauth => { :token => token }
160     assert_response :success
161     token.reload
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]
171
172     trace = create(:trace, :user => client.user)
173     signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
174     assert_response :success
175
176     signed_get "/api/0.6/user/details", :oauth => { :token => token }
177     assert_response :forbidden
178
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
183
184     signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
185     assert_response :unauthorized
186   end
187
188   def oauth10a_without_callback(client)
189     token = get_request_token(client, :oauth_callback => "oob")
190
191     get "/oauth/authorize", :params => { :oauth_token => token.token }
192     assert_response :success
193     assert_template :authorize
194
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}"
202     else
203       assert_response :success
204       assert_template :authorize_success
205       m = response.body.match("<p>The verification code is ([A-Za-z0-9]+).</p>")
206       assert_not_nil m
207       verifier = m[1]
208     end
209     token.reload
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]
214
215     signed_get "/oauth/access_token", :oauth => { :token => token }
216     assert_response :unauthorized
217
218     signed_get "/oauth/access_token", :oauth => { :token => token, :oauth_verifier => verifier }
219     assert_response :success
220     token.reload
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]
230
231     signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
232     assert_response :success
233
234     trace = create(:trace, :user => client.user)
235     signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
236     assert_response :forbidden
237
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
242
243     signed_get "/api/0.6/user/preferences", :oauth => { :token => token }
244     assert_response :unauthorized
245   end
246
247   def oauth10a_with_callback(client, callback_url)
248     token = get_request_token(client, :oauth_callback => callback_url)
249
250     get "/oauth/authorize", :params => { :oauth_token => token.token }
251     assert_response :success
252     assert_template :authorize
253
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}"
260     token.reload
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]
265
266     signed_get "/oauth/access_token", :oauth => { :token => token }
267     assert_response :unauthorized
268
269     signed_get "/oauth/access_token", :oauth => { :token => token, :oauth_verifier => verifier }
270     assert_response :success
271     token.reload
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]
281
282     trace = create(:trace, :user => client.user)
283     signed_get "/api/0.6/gpx/#{trace.id}", :oauth => { :token => token }
284     assert_response :success
285
286     signed_get "/api/0.6/user/details", :oauth => { :token => token }
287     assert_response :forbidden
288
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
293
294     signed_get "/api/0.6/gpx/2", :oauth => { :token => token }
295     assert_response :unauthorized
296   end
297
298   def oauth10a_refused(client)
299     token = get_request_token(client, :oauth_callback => "oob")
300
301     get "/oauth/authorize", :params => { :oauth_token => token.token }
302     assert_response :success
303     assert_template :authorize
304
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."
309     token.reload
310     assert_nil token.authorized_at
311     assert_not_nil token.invalidated_at
312
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."
317     token.reload
318     assert_nil token.authorized_at
319     assert_not_nil token.invalidated_at
320
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."
325     token.reload
326     assert_nil token.authorized_at
327     assert_not_nil token.invalidated_at
328   end
329
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
340
341     token
342   end
343
344   def parse_token(response)
345     params = CGI.parse(response.body)
346
347     token = OauthToken.find_by(:token => params["oauth_token"].first)
348     assert_equal token.secret, params["oauth_token_secret"].first
349
350     token
351   end
352
353   def parse_verifier(response)
354     params = CGI.parse(URI.parse(response.location).query)
355
356     assert_not_nil params["oauth_verifier"]
357     assert params["oauth_verifier"].first.present?
358
359     params["oauth_verifier"].first
360   end
361
362   def assert_allowed(token, allowed)
363     ClientApplication.all_permissions.each do |p|
364       assert_equal allowed.include?(p), token.attributes[p.to_s]
365     end
366   end
367 end