]> git.openstreetmap.org Git - rails.git/blob - test/integration/oauth_test.rb
Look up email addresses case insensitively for password resets
[rails.git] / test / integration / oauth_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class OAuthTest < ActionController::IntegrationTest
4   fixtures :users, :client_applications
5
6   include OAuth::Helper
7
8   def test_oauth10_web_app
9     client = client_applications(:oauth_web_app)
10
11     post_via_redirect "/login", 
12       :username => client.user.email, :password => "test"
13     assert_response :success
14
15     signed_get "/oauth/request_token", :consumer => client
16     assert_response :success
17     token = parse_token(response)
18     assert_instance_of RequestToken, token
19     assert_not_nil token.created_at
20     assert_nil token.authorized_at
21     assert_nil token.invalidated_at
22     assert_allowed token, client.permissions
23
24     post "/oauth/authorize", 
25       :oauth_token => token.token, 
26       :allow_read_prefs => true, :allow_write_prefs => true
27     assert_response :redirect
28     assert_redirected_to "http://some.web.app.org/callback?oauth_token=#{token.token}"
29     token.reload
30     assert_not_nil token.created_at
31     assert_not_nil token.authorized_at
32     assert_nil token.invalidated_at
33     assert_allowed token, [ :allow_read_prefs ]
34
35     signed_get "/oauth/access_token", :consumer => client, :token => token
36     assert_response :success
37     token.reload
38     assert_not_nil token.created_at
39     assert_not_nil token.authorized_at
40     assert_not_nil token.invalidated_at
41     token = parse_token(response)
42     assert_instance_of AccessToken, token
43     assert_not_nil token.created_at
44     assert_not_nil token.authorized_at
45     assert_nil token.invalidated_at
46     assert_allowed token, [ :allow_read_prefs ]
47
48     signed_get "/oauth/request_token", :consumer => client
49     assert_response :success
50     token = parse_token(response)
51     assert_instance_of RequestToken, token
52     assert_not_nil token.created_at
53     assert_nil token.authorized_at
54     assert_nil token.invalidated_at
55     assert_allowed token, client.permissions
56
57     post "/oauth/authorize", 
58       :oauth_token => token.token, 
59       :oauth_callback => "http://another.web.app.org/callback", 
60       :allow_write_api => true, :allow_read_gpx => true
61     assert_response :redirect
62     assert_redirected_to "http://another.web.app.org/callback?oauth_token=#{token.token}"
63     token.reload
64     assert_not_nil token.created_at
65     assert_not_nil token.authorized_at
66     assert_nil token.invalidated_at
67     assert_allowed token, [ :allow_write_api, :allow_read_gpx ]
68
69     signed_get "/oauth/access_token", :consumer => client, :token => token
70     assert_response :success
71     token.reload
72     assert_not_nil token.created_at
73     assert_not_nil token.authorized_at
74     assert_not_nil token.invalidated_at
75     token = parse_token(response)
76     assert_instance_of AccessToken, token
77     assert_not_nil token.created_at
78     assert_not_nil token.authorized_at
79     assert_nil token.invalidated_at
80     assert_allowed token, [ :allow_write_api, :allow_read_gpx ]
81   end
82
83   def test_oauth10_desktop_app
84     client = client_applications(:oauth_desktop_app)
85
86     post_via_redirect "/login", 
87       :username => client.user.email, :password => "test"
88     assert_response :success
89
90     signed_get "/oauth/request_token", :consumer => client
91     assert_response :success
92     token = parse_token(response)
93     assert_instance_of RequestToken, token
94     assert_not_nil token.created_at
95     assert_nil token.authorized_at
96     assert_nil token.invalidated_at
97     assert_allowed token, client.permissions
98
99     post "/oauth/authorize", 
100       :oauth_token => token.token, 
101       :allow_read_prefs => true, :allow_write_prefs => true
102     assert_response :success
103     assert_template "authorize_success"
104     token.reload
105     assert_not_nil token.created_at
106     assert_not_nil token.authorized_at
107     assert_nil token.invalidated_at
108     assert_allowed token, [ :allow_read_prefs ]
109
110     signed_get "/oauth/access_token", :consumer => client, :token => token
111     assert_response :success
112     token.reload
113     assert_not_nil token.created_at
114     assert_not_nil token.authorized_at
115     assert_not_nil token.invalidated_at
116     token = parse_token(response)
117     assert_instance_of AccessToken, token
118     assert_not_nil token.created_at
119     assert_not_nil token.authorized_at
120     assert_nil token.invalidated_at
121     assert_allowed token, [ :allow_read_prefs ]
122   end
123
124   def test_oauth10a_web_app
125     client = client_applications(:oauth_web_app)
126
127     post_via_redirect "/login",
128       :username => client.user.email, :password => "test"
129     assert_response :success
130
131     signed_get "/oauth/request_token",
132       :consumer => client, :oauth_callback => "oob"
133     assert_response :success
134     token = parse_token(response)
135     assert_instance_of RequestToken, token
136     assert_not_nil token.created_at
137     assert_nil token.authorized_at
138     assert_nil token.invalidated_at
139     assert_allowed token, client.permissions
140
141     post "/oauth/authorize",
142       :oauth_token => token.token,
143       :allow_read_prefs => true, :allow_write_prefs => true
144     assert_response :redirect
145     verifier = parse_verifier(response)
146     assert_redirected_to "http://some.web.app.org/callback?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
147     token.reload
148     assert_not_nil token.created_at
149     assert_not_nil token.authorized_at
150     assert_nil token.invalidated_at
151     assert_allowed token, [ :allow_read_prefs ]
152
153     signed_get "/oauth/access_token", :consumer => client, :token => token
154     assert_response :unauthorized
155
156     signed_get "/oauth/access_token",
157       :consumer => client, :token => token, :oauth_verifier => verifier
158     assert_response :success
159     token.reload
160     assert_not_nil token.created_at
161     assert_not_nil token.authorized_at
162     assert_not_nil token.invalidated_at
163     token = parse_token(response)
164     assert_instance_of AccessToken, token
165     assert_not_nil token.created_at
166     assert_not_nil token.authorized_at
167     assert_nil token.invalidated_at
168     assert_allowed token, [ :allow_read_prefs ]
169
170     signed_get "/oauth/request_token",
171       :consumer => client,
172       :oauth_callback => "http://another.web.app.org/callback"
173     assert_response :success
174     token = parse_token(response)
175     assert_instance_of RequestToken, token
176     assert_not_nil token.created_at
177     assert_nil token.authorized_at
178     assert_nil token.invalidated_at
179     assert_allowed token, client.permissions
180
181     post "/oauth/authorize",
182       :oauth_token => token.token,
183       :allow_write_api => true, :allow_read_gpx => true
184     assert_response :redirect
185     verifier = parse_verifier(response)
186     assert_redirected_to "http://another.web.app.org/callback?oauth_token=#{token.token}&oauth_verifier=#{verifier}"
187     token.reload
188     assert_not_nil token.created_at
189     assert_not_nil token.authorized_at
190     assert_nil token.invalidated_at
191     assert_allowed token, [ :allow_write_api, :allow_read_gpx ]
192
193     signed_get "/oauth/access_token", :consumer => client, :token => token
194     assert_response :unauthorized
195
196     signed_get "/oauth/access_token",
197       :consumer => client, :token => token, :oauth_verifier => verifier
198     assert_response :success
199     token.reload
200     assert_not_nil token.created_at
201     assert_not_nil token.authorized_at
202     assert_not_nil token.invalidated_at
203     token = parse_token(response)
204     assert_instance_of AccessToken, token
205     assert_not_nil token.created_at
206     assert_not_nil token.authorized_at
207     assert_nil token.invalidated_at
208     assert_allowed token, [ :allow_write_api, :allow_read_gpx ]
209   end
210
211   def test_oauth10a_desktop_app
212     client = client_applications(:oauth_desktop_app)
213
214     post_via_redirect "/login", 
215       :username => client.user.email, :password => "test"
216     assert_response :success
217
218     signed_get "/oauth/request_token",
219       :consumer => client, :oauth_callback => "oob"
220     assert_response :success
221     token = parse_token(response)
222     assert_instance_of RequestToken, token
223     assert_not_nil token.created_at
224     assert_nil token.authorized_at
225     assert_nil token.invalidated_at
226     assert_allowed token, client.permissions
227
228     post "/oauth/authorize", 
229       :oauth_token => token.token, 
230       :allow_read_prefs => true, :allow_write_prefs => true
231     assert_response :success
232     assert_template "authorize_success"
233     m = response.body.match("<p>The verification code is ([A-Za-z0-9]+)</p>")
234     assert_not_nil m
235     verifier = m[1]
236     token.reload
237     assert_not_nil token.created_at
238     assert_not_nil token.authorized_at
239     assert_nil token.invalidated_at
240     assert_allowed token, [ :allow_read_prefs ]
241
242     signed_get "/oauth/access_token", :consumer => client, :token => token
243     assert_response :unauthorized
244
245     signed_get "/oauth/access_token",
246       :consumer => client, :token => token, :oauth_verifier => verifier
247     assert_response :success
248     token.reload
249     assert_not_nil token.created_at
250     assert_not_nil token.authorized_at
251     assert_not_nil token.invalidated_at
252     token = parse_token(response)
253     assert_instance_of AccessToken, token
254     assert_not_nil token.created_at
255     assert_not_nil token.authorized_at
256     assert_nil token.invalidated_at
257     assert_allowed token, [ :allow_read_prefs ]
258   end
259
260 private
261
262   def signed_get(uri, options)
263     uri = URI.parse(uri)
264     uri.scheme ||= "http"
265     uri.host ||= host
266
267     helper = OAuth::Client::Helper.new(nil, options)
268
269     request = OAuth::RequestProxy.proxy(
270       "method" => "GET",
271       "uri" => uri,
272       "parameters" => helper.oauth_parameters
273     )
274
275     request.sign!(options)
276
277     get request.signed_uri
278   end
279
280   def parse_token(response)
281     params = CGI.parse(response.body)
282
283     token = OauthToken.find_by_token(params["oauth_token"].first)
284     assert_equal token.secret, params["oauth_token_secret"].first
285
286     token
287   end
288
289   def parse_verifier(response)
290     params = CGI.parse(URI.parse(response.location).query)
291
292     assert_not_nil params["oauth_verifier"]
293     assert_present params["oauth_verifier"].first
294
295     params["oauth_verifier"].first
296   end
297
298   def assert_allowed(token, allowed)
299     ClientApplication.all_permissions.each do |p|
300       assert_equal allowed.include?(p), token.attributes[p.to_s]
301     end
302   end
303 end