4 class UsersControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/user/1", :method => :get },
10 { :controller => "api/users", :action => "show", :id => "1" }
13 { :path => "/api/0.6/user/1.json", :method => :get },
14 { :controller => "api/users", :action => "show", :id => "1", :format => "json" }
17 { :path => "/api/0.6/user/details", :method => :get },
18 { :controller => "api/users", :action => "details" }
21 { :path => "/api/0.6/user/details.json", :method => :get },
22 { :controller => "api/users", :action => "details", :format => "json" }
25 { :path => "/api/0.6/user/gpx_files", :method => :get },
26 { :controller => "api/users", :action => "gpx_files" }
29 { :path => "/api/0.6/users", :method => :get },
30 { :controller => "api/users", :action => "index" }
33 { :path => "/api/0.6/users.json", :method => :get },
34 { :controller => "api/users", :action => "index", :format => "json" }
40 :description => "test",
41 :terms_agreed => Date.yesterday,
42 :home_lat => 12.1, :home_lon => 23.4,
45 # check that a visible user is returned properly
46 get api_user_path(:id => user.id)
47 assert_response :success
48 assert_equal "application/xml", response.media_type
50 # check the data that is returned
51 check_xml_details(user, false, false)
53 # check that a suspended user is not returned
54 get api_user_path(:id => create(:user, :suspended).id)
57 # check that a deleted user is not returned
58 get api_user_path(:id => create(:user, :deleted).id)
61 # check that a non-existent user is not returned
62 get api_user_path(:id => 0)
63 assert_response :not_found
65 # check that a visible user is returned properly in json
66 get api_user_path(:id => user.id, :format => "json")
67 assert_response :success
68 assert_equal "application/json", response.media_type
71 js = ActiveSupport::JSON.decode(@response.body)
74 # check the data that is returned
75 check_json_details(js, user, false, false)
80 :home_lat => 12.1, :home_lon => 23.4,
82 good_token = create(:oauth_access_token,
83 :resource_owner_id => user.id,
84 :scopes => %w[read_prefs])
85 bad_token = create(:oauth_access_token,
86 :resource_owner_id => user.id,
88 other_user = create(:user,
89 :home_lat => 12.1, :home_lon => 23.4,
92 # check that we can fetch our own details as XML with read_prefs
93 get api_user_path(:id => user.id), :headers => bearer_authorization_header(good_token.token)
94 assert_response :success
95 assert_equal "application/xml", response.media_type
97 # check the data that is returned
98 check_xml_details(user, true, false)
100 # check that we can fetch a different user's details as XML with read_prefs
101 get api_user_path(:id => other_user.id), :headers => bearer_authorization_header(good_token.token)
102 assert_response :success
103 assert_equal "application/xml", response.media_type
105 # check the data that is returned
106 check_xml_details(other_user, false, false)
108 # check that we can fetch our own details as XML without read_prefs
109 get api_user_path(:id => user.id), :headers => bearer_authorization_header(bad_token.token)
110 assert_response :success
111 assert_equal "application/xml", response.media_type
113 # check the data that is returned
114 check_xml_details(user, false, false)
116 # check that we can fetch our own details as JSON with read_prefs
117 get api_user_path(:id => user.id, :format => "json"), :headers => bearer_authorization_header(good_token.token)
118 assert_response :success
119 assert_equal "application/json", response.media_type
122 js = ActiveSupport::JSON.decode(@response.body)
125 # check the data that is returned
126 check_json_details(js, user, true, false)
128 # check that we can fetch a different user's details as JSON with read_prefs
129 get api_user_path(:id => other_user.id, :format => "json"), :headers => bearer_authorization_header(good_token.token)
130 assert_response :success
131 assert_equal "application/json", response.media_type
134 js = ActiveSupport::JSON.decode(@response.body)
137 # check the data that is returned
138 check_json_details(js, other_user, false, false)
140 # check that we can fetch our own details as JSON without read_prefs
141 get api_user_path(:id => user.id, :format => "json"), :headers => bearer_authorization_header(bad_token.token)
142 assert_response :success
143 assert_equal "application/json", response.media_type
146 js = ActiveSupport::JSON.decode(@response.body)
149 # check the data that is returned
150 check_json_details(js, user, false, false)
155 :description => "test",
156 :terms_agreed => Date.yesterday,
157 :home_lat => 12.1, :home_lon => 23.4,
158 :languages => ["en"])
159 create(:message, :read, :recipient => user)
160 create(:message, :sender => user)
162 # check that nothing is returned when not logged in
163 get user_details_path
164 assert_response :unauthorized
166 # check that we get a response when logged in
167 auth_header = bearer_authorization_header user
168 get user_details_path, :headers => auth_header
169 assert_response :success
170 assert_equal "application/xml", response.media_type
172 # check the data that is returned
173 check_xml_details(user, true, false)
175 # check that data is returned properly in json
176 auth_header = bearer_authorization_header user
177 get user_details_path(:format => "json"), :headers => auth_header
178 assert_response :success
179 assert_equal "application/json", response.media_type
182 js = ActiveSupport::JSON.decode(@response.body)
185 # check the data that is returned
186 check_json_details(js, user, true, false)
189 def test_details_oauth2
191 :home_lat => 12.1, :home_lon => 23.4,
192 :languages => ["en"])
193 good_token = create(:oauth_access_token,
194 :resource_owner_id => user.id,
195 :scopes => %w[read_prefs])
196 bad_token = create(:oauth_access_token,
197 :resource_owner_id => user.id)
198 email_token = create(:oauth_access_token,
199 :resource_owner_id => user.id,
200 :scopes => %w[read_prefs read_email])
202 # check that we can't fetch details as XML without read_prefs
203 get user_details_path, :headers => bearer_authorization_header(bad_token.token)
204 assert_response :forbidden
206 # check that we can fetch details as XML without read_email
207 get user_details_path, :headers => bearer_authorization_header(good_token.token)
208 assert_response :success
209 assert_equal "application/xml", response.media_type
211 # check the data that is returned
212 check_xml_details(user, true, false)
214 # check that we can fetch details as XML with read_email
215 get user_details_path, :headers => bearer_authorization_header(email_token.token)
216 assert_response :success
217 assert_equal "application/xml", response.media_type
219 # check the data that is returned
220 check_xml_details(user, true, true)
222 # check that we can't fetch details as JSON without read_prefs
223 get user_details_path(:format => "json"), :headers => bearer_authorization_header(bad_token.token)
224 assert_response :forbidden
226 # check that we can fetch details as JSON without read_email
227 get user_details_path(:format => "json"), :headers => bearer_authorization_header(good_token.token)
228 assert_response :success
229 assert_equal "application/json", response.media_type
232 js = ActiveSupport::JSON.decode(@response.body)
235 # check the data that is returned
236 check_json_details(js, user, true, false)
238 # check that we can fetch details as JSON with read_email
239 get user_details_path(:format => "json"), :headers => bearer_authorization_header(email_token.token)
240 assert_response :success
241 assert_equal "application/json", response.media_type
244 js = ActiveSupport::JSON.decode(@response.body)
247 # check the data that is returned
248 check_json_details(js, user, true, true)
252 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
253 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
254 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
256 get api_users_path, :params => { :users => user1.id }
257 assert_response :success
258 assert_equal "application/xml", response.media_type
259 assert_select "user", :count => 1 do
260 check_xml_details(user1, false, false)
261 assert_select "user[id='#{user2.id}']", :count => 0
262 assert_select "user[id='#{user3.id}']", :count => 0
265 get api_users_path, :params => { :users => user2.id }
266 assert_response :success
267 assert_equal "application/xml", response.media_type
268 assert_select "user", :count => 1 do
269 assert_select "user[id='#{user1.id}']", :count => 0
270 check_xml_details(user2, false, false)
271 assert_select "user[id='#{user3.id}']", :count => 0
274 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }
275 assert_response :success
276 assert_equal "application/xml", response.media_type
277 assert_select "user", :count => 2 do
278 check_xml_details(user1, false, false)
279 assert_select "user[id='#{user2.id}']", :count => 0
280 check_xml_details(user3, false, false)
283 get api_users_path, :params => { :users => user1.id, :format => "json" }
284 assert_response :success
285 assert_equal "application/json", response.media_type
286 js = ActiveSupport::JSON.decode(@response.body)
288 assert_equal 1, js["users"].count
289 check_json_details(js["users"][0], user1, false, false)
291 get api_users_path, :params => { :users => user2.id, :format => "json" }
292 assert_response :success
293 assert_equal "application/json", response.media_type
294 js = ActiveSupport::JSON.decode(@response.body)
296 assert_equal 1, js["users"].count
297 check_json_details(js["users"][0], user2, false, false)
299 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }
300 assert_response :success
301 assert_equal "application/json", response.media_type
302 js = ActiveSupport::JSON.decode(@response.body)
304 assert_equal 2, js["users"].count
305 check_json_details(js["users"][0], user1, false, false)
306 check_json_details(js["users"][1], user3, false, false)
308 get api_users_path, :params => { :users => create(:user, :suspended).id }
309 assert_response :success
310 assert_equal "application/xml", response.media_type
311 assert_select "user", :count => 0
313 get api_users_path, :params => { :users => create(:user, :deleted).id }
314 assert_response :success
315 assert_equal "application/xml", response.media_type
316 assert_select "user", :count => 0
318 get api_users_path, :params => { :users => 0 }
319 assert_response :success
320 assert_equal "application/xml", response.media_type
321 assert_select "user", :count => 0
324 def test_index_oauth2
325 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
326 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
327 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
328 good_token = create(:oauth_access_token, :resource_owner_id => user1.id, :scopes => %w[read_prefs])
329 bad_token = create(:oauth_access_token, :resource_owner_id => user1.id, :scopes => %w[])
331 get api_users_path, :params => { :users => user1.id }, :headers => bearer_authorization_header(good_token.token)
332 assert_response :success
333 assert_equal "application/xml", response.media_type
334 assert_select "user", :count => 1 do
335 check_xml_details(user1, true, false)
336 assert_select "user[id='#{user2.id}']", :count => 0
337 assert_select "user[id='#{user3.id}']", :count => 0
340 get api_users_path, :params => { :users => user2.id }, :headers => bearer_authorization_header(good_token.token)
341 assert_response :success
342 assert_equal "application/xml", response.media_type
343 assert_select "user", :count => 1 do
344 assert_select "user[id='#{user1.id}']", :count => 0
345 check_xml_details(user2, false, false)
346 assert_select "user[id='#{user3.id}']", :count => 0
349 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bearer_authorization_header(good_token.token)
350 assert_response :success
351 assert_equal "application/xml", response.media_type
352 assert_select "user", :count => 2 do
353 check_xml_details(user1, true, false)
354 assert_select "user[id='#{user2.id}']", :count => 0
355 check_xml_details(user3, false, false)
358 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bearer_authorization_header(bad_token.token)
359 assert_response :success
360 assert_equal "application/xml", response.media_type
361 assert_select "user", :count => 2 do
362 check_xml_details(user1, false, false)
363 assert_select "user[id='#{user2.id}']", :count => 0
364 check_xml_details(user3, false, false)
367 get api_users_path, :params => { :users => user1.id, :format => "json" }, :headers => bearer_authorization_header(good_token.token)
368 assert_response :success
369 assert_equal "application/json", response.media_type
370 js = ActiveSupport::JSON.decode(@response.body)
372 assert_equal 1, js["users"].count
373 check_json_details(js["users"][0], user1, true, false)
375 get api_users_path, :params => { :users => user2.id, :format => "json" }, :headers => bearer_authorization_header(good_token.token)
376 assert_response :success
377 assert_equal "application/json", response.media_type
378 js = ActiveSupport::JSON.decode(@response.body)
380 assert_equal 1, js["users"].count
381 check_json_details(js["users"][0], user2, false, false)
383 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bearer_authorization_header(good_token.token)
384 assert_response :success
385 assert_equal "application/json", response.media_type
386 js = ActiveSupport::JSON.decode(@response.body)
388 assert_equal 2, js["users"].count
389 check_json_details(js["users"][0], user1, true, false)
390 check_json_details(js["users"][1], user3, false, false)
392 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bearer_authorization_header(bad_token.token)
393 assert_response :success
394 assert_equal "application/json", response.media_type
395 js = ActiveSupport::JSON.decode(@response.body)
397 assert_equal 2, js["users"].count
398 check_json_details(js["users"][0], user1, false, false)
399 check_json_details(js["users"][1], user3, false, false)
401 get api_users_path, :params => { :users => create(:user, :suspended).id }, :headers => bearer_authorization_header(good_token.token)
402 assert_response :success
403 assert_equal "application/xml", response.media_type
404 assert_select "user", :count => 0
406 get api_users_path, :params => { :users => create(:user, :deleted).id }, :headers => bearer_authorization_header(good_token.token)
407 assert_response :success
408 assert_equal "application/xml", response.media_type
409 assert_select "user", :count => 0
411 get api_users_path, :params => { :users => 0 }, :headers => bearer_authorization_header(good_token.token)
412 assert_response :success
413 assert_equal "application/xml", response.media_type
414 assert_select "user", :count => 0
419 trace1 = create(:trace, :user => user) do |trace|
420 create(:tracetag, :trace => trace, :tag => "London")
422 trace2 = create(:trace, :user => user) do |trace|
423 create(:tracetag, :trace => trace, :tag => "Birmingham")
425 # check that nothing is returned when not logged in
426 get user_gpx_files_path
427 assert_response :unauthorized
429 # check that we get a response when logged in
430 auth_header = bearer_authorization_header user
431 get user_gpx_files_path, :headers => auth_header
432 assert_response :success
433 assert_equal "application/xml", response.media_type
435 # check the data that is returned
436 assert_select "gpx_file[id='#{trace1.id}']", 1 do
437 assert_select "tag", "London"
439 assert_select "gpx_file[id='#{trace2.id}']", 1 do
440 assert_select "tag", "Birmingham"
446 def check_xml_details(user, include_private, include_email)
447 assert_select "user[id='#{user.id}']", :count => 1 do
448 assert_select "description", :count => 1, :text => user.description
450 assert_select "contributor-terms", :count => 1 do
451 if user.terms_agreed.present?
452 assert_select "[agreed='true']", :count => 1
454 assert_select "[agreed='false']", :count => 1
458 assert_select "[pd='false']", :count => 1
460 assert_select "[pd]", :count => 0
464 assert_select "img", :count => 0
466 assert_select "roles", :count => 1 do
467 assert_select "role", :count => 0
470 assert_select "changesets", :count => 1 do
471 assert_select "[count='0']", :count => 1
474 assert_select "traces", :count => 1 do
475 assert_select "[count='0']", :count => 1
478 assert_select "blocks", :count => 1 do
479 assert_select "received", :count => 1 do
480 assert_select "[count='0'][active='0']", :count => 1
483 assert_select "issued", :count => 0
486 if include_private && user.home_lat.present? && user.home_lon.present?
487 assert_select "home", :count => 1 do
488 assert_select "[lat='12.1'][lon='23.4'][zoom='3']", :count => 1
491 assert_select "home", :count => 0
495 assert_select "languages", :count => 1 do
496 assert_select "lang", :count => user.languages.count
498 user.languages.each do |language|
499 assert_select "lang", :count => 1, :text => language
503 assert_select "messages", :count => 1 do
504 assert_select "received", :count => 1 do
505 assert_select "[count='#{user.messages.count}'][unread='0']", :count => 1
508 assert_select "sent", :count => 1 do
509 assert_select "[count='#{user.sent_messages.count}']", :count => 1
513 assert_select "languages", :count => 0
514 assert_select "messages", :count => 0
518 assert_select "email", :count => 1, :text => user.email
520 assert_select "email", :count => 0
525 def check_json_details(js, user, include_private, include_email)
526 assert_equal user.id, js["user"]["id"]
527 assert_equal user.description, js["user"]["description"]
528 assert_operator js["user"]["contributor_terms"], :[], "agreed"
531 assert_not js["user"]["contributor_terms"]["pd"]
533 assert_nil js["user"]["contributor_terms"]["pd"]
536 assert_nil js["user"]["img"]
537 assert_empty js["user"]["roles"]
538 assert_equal 0, js["user"]["changesets"]["count"]
539 assert_equal 0, js["user"]["traces"]["count"]
540 assert_equal 0, js["user"]["blocks"]["received"]["count"]
541 assert_equal 0, js["user"]["blocks"]["received"]["active"]
542 assert_nil js["user"]["blocks"]["issued"]
544 if include_private && user.home_lat.present? && user.home_lon.present?
545 assert_in_delta 12.1, js["user"]["home"]["lat"]
546 assert_in_delta 23.4, js["user"]["home"]["lon"]
547 assert_equal 3, js["user"]["home"]["zoom"]
549 assert_nil js["user"]["home"]
552 if include_private && user.languages.present?
553 assert_equal user.languages, js["user"]["languages"]
555 assert_nil js["user"]["languages"]
559 assert_equal user.messages.count, js["user"]["messages"]["received"]["count"]
560 assert_equal 0, js["user"]["messages"]["received"]["unread"]
561 assert_equal user.sent_messages.count, js["user"]["messages"]["sent"]["count"]
563 assert_nil js["user"]["messages"]
567 assert_equal user.email, js["user"]["email"]
569 assert_nil js["user"]["email"]