1 require File.dirname(__FILE__) + '/../test_helper'
3 class OAuthTest < ActionController::IntegrationTest
4 fixtures :users, :client_applications
8 def test_oauth10_web_app
9 client = client_applications(:oauth_web_app)
11 post_via_redirect "/login",
12 :username => client.user.email, :password => "test"
13 assert_response :success
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
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}"
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 ]
35 signed_get "/oauth/access_token", :consumer => client, :token => token
36 assert_response :success
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 ]
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
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}"
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 ]
69 signed_get "/oauth/access_token", :consumer => client, :token => token
70 assert_response :success
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 ]
83 def test_oauth10_desktop_app
84 client = client_applications(:oauth_desktop_app)
86 post_via_redirect "/login",
87 :username => client.user.email, :password => "test"
88 assert_response :success
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
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"
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 ]
110 signed_get "/oauth/access_token", :consumer => client, :token => token
111 assert_response :success
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 ]
124 def test_oauth10a_web_app
125 client = client_applications(:oauth_web_app)
127 post_via_redirect "/login",
128 :username => client.user.email, :password => "test"
129 assert_response :success
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
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}"
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 ]
153 signed_get "/oauth/access_token", :consumer => client, :token => token
154 assert_response :unauthorized
156 signed_get "/oauth/access_token",
157 :consumer => client, :token => token, :oauth_verifier => verifier
158 assert_response :success
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 ]
170 signed_get "/oauth/request_token",
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
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}"
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 ]
193 signed_get "/oauth/access_token", :consumer => client, :token => token
194 assert_response :unauthorized
196 signed_get "/oauth/access_token",
197 :consumer => client, :token => token, :oauth_verifier => verifier
198 assert_response :success
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 ]
211 def test_oauth10a_desktop_app
212 client = client_applications(:oauth_desktop_app)
214 post_via_redirect "/login",
215 :username => client.user.email, :password => "test"
216 assert_response :success
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
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>")
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 ]
242 signed_get "/oauth/access_token", :consumer => client, :token => token
243 assert_response :unauthorized
245 signed_get "/oauth/access_token",
246 :consumer => client, :token => token, :oauth_verifier => verifier
247 assert_response :success
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 ]
262 def signed_get(uri, options)
264 uri.scheme ||= "http"
267 helper = OAuth::Client::Helper.new(nil, options)
269 request = OAuth::RequestProxy.proxy(
272 "parameters" => helper.oauth_parameters
275 request.sign!(options)
277 get request.signed_uri
280 def parse_token(response)
281 params = CGI.parse(response.body)
283 token = OauthToken.find_by_token(params["oauth_token"].first)
284 assert_equal token.secret, params["oauth_token_secret"].first
289 def parse_verifier(response)
290 params = CGI.parse(URI.parse(response.location).query)
292 assert_not_nil params["oauth_verifier"]
293 assert_present params["oauth_verifier"].first
295 params["oauth_verifier"].first
298 def assert_allowed(token, allowed)
299 ClientApplication.all_permissions.each do |p|
300 assert_equal allowed.include?(p), token.attributes[p.to_s]