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