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_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
83 bad_auth = bearer_authorization_header(user, :scopes => %w[])
84 other_user = create(:user,
85 :home_lat => 12.1, :home_lon => 23.4,
88 # check that we can fetch our own details as XML with read_prefs
89 get api_user_path(:id => user.id), :headers => good_auth
90 assert_response :success
91 assert_equal "application/xml", response.media_type
93 # check the data that is returned
94 check_xml_details(user, true, false)
96 # check that we can fetch a different user's details as XML with read_prefs
97 get api_user_path(:id => other_user.id), :headers => good_auth
98 assert_response :success
99 assert_equal "application/xml", response.media_type
101 # check the data that is returned
102 check_xml_details(other_user, false, false)
104 # check that we can fetch our own details as XML without read_prefs
105 get api_user_path(:id => user.id), :headers => bad_auth
106 assert_response :success
107 assert_equal "application/xml", response.media_type
109 # check the data that is returned
110 check_xml_details(user, false, false)
112 # check that we can fetch our own details as JSON with read_prefs
113 get api_user_path(:id => user.id, :format => "json"), :headers => good_auth
114 assert_response :success
115 assert_equal "application/json", response.media_type
118 js = ActiveSupport::JSON.decode(@response.body)
121 # check the data that is returned
122 check_json_details(js, user, true, false)
124 # check that we can fetch a different user's details as JSON with read_prefs
125 get api_user_path(:id => other_user.id, :format => "json"), :headers => good_auth
126 assert_response :success
127 assert_equal "application/json", response.media_type
130 js = ActiveSupport::JSON.decode(@response.body)
133 # check the data that is returned
134 check_json_details(js, other_user, false, false)
136 # check that we can fetch our own details as JSON without read_prefs
137 get api_user_path(:id => user.id, :format => "json"), :headers => bad_auth
138 assert_response :success
139 assert_equal "application/json", response.media_type
142 js = ActiveSupport::JSON.decode(@response.body)
145 # check the data that is returned
146 check_json_details(js, user, false, false)
151 :description => "test",
152 :terms_agreed => Date.yesterday,
153 :home_lat => 12.1, :home_lon => 23.4,
154 :languages => ["en"])
155 create(:message, :read, :recipient => user)
156 create(:message, :sender => user)
158 # check that nothing is returned when not logged in
159 get user_details_path
160 assert_response :unauthorized
162 # check that we get a response when logged in
163 auth_header = bearer_authorization_header user
164 get user_details_path, :headers => auth_header
165 assert_response :success
166 assert_equal "application/xml", response.media_type
168 # check the data that is returned
169 check_xml_details(user, true, false)
171 # check that data is returned properly in json
172 auth_header = bearer_authorization_header user
173 get user_details_path(:format => "json"), :headers => auth_header
174 assert_response :success
175 assert_equal "application/json", response.media_type
178 js = ActiveSupport::JSON.decode(@response.body)
181 # check the data that is returned
182 check_json_details(js, user, true, false)
185 def test_details_oauth2
187 :home_lat => 12.1, :home_lon => 23.4,
188 :languages => ["en"])
189 good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
190 bad_auth = bearer_authorization_header(user, :scopes => %w[])
191 email_auth = bearer_authorization_header(user, :scopes => %w[read_prefs read_email])
193 # check that we can't fetch details as XML without read_prefs
194 get user_details_path, :headers => bad_auth
195 assert_response :forbidden
197 # check that we can fetch details as XML without read_email
198 get user_details_path, :headers => good_auth
199 assert_response :success
200 assert_equal "application/xml", response.media_type
202 # check the data that is returned
203 check_xml_details(user, true, false)
205 # check that we can fetch details as XML with read_email
206 get user_details_path, :headers => email_auth
207 assert_response :success
208 assert_equal "application/xml", response.media_type
210 # check the data that is returned
211 check_xml_details(user, true, true)
213 # check that we can't fetch details as JSON without read_prefs
214 get user_details_path(:format => "json"), :headers => bad_auth
215 assert_response :forbidden
217 # check that we can fetch details as JSON without read_email
218 get user_details_path(:format => "json"), :headers => good_auth
219 assert_response :success
220 assert_equal "application/json", response.media_type
223 js = ActiveSupport::JSON.decode(@response.body)
226 # check the data that is returned
227 check_json_details(js, user, true, false)
229 # check that we can fetch details as JSON with read_email
230 get user_details_path(:format => "json"), :headers => email_auth
231 assert_response :success
232 assert_equal "application/json", response.media_type
235 js = ActiveSupport::JSON.decode(@response.body)
238 # check the data that is returned
239 check_json_details(js, user, true, true)
243 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
244 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
245 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
247 get api_users_path, :params => { :users => user1.id }
248 assert_response :success
249 assert_equal "application/xml", response.media_type
250 assert_select "user", :count => 1 do
251 check_xml_details(user1, false, false)
252 assert_select "user[id='#{user2.id}']", :count => 0
253 assert_select "user[id='#{user3.id}']", :count => 0
256 get api_users_path, :params => { :users => user2.id }
257 assert_response :success
258 assert_equal "application/xml", response.media_type
259 assert_select "user", :count => 1 do
260 assert_select "user[id='#{user1.id}']", :count => 0
261 check_xml_details(user2, false, false)
262 assert_select "user[id='#{user3.id}']", :count => 0
265 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }
266 assert_response :success
267 assert_equal "application/xml", response.media_type
268 assert_select "user", :count => 2 do
269 check_xml_details(user1, false, false)
270 assert_select "user[id='#{user2.id}']", :count => 0
271 check_xml_details(user3, false, false)
274 get api_users_path, :params => { :users => user1.id, :format => "json" }
275 assert_response :success
276 assert_equal "application/json", response.media_type
277 js = ActiveSupport::JSON.decode(@response.body)
279 assert_equal 1, js["users"].count
280 check_json_details(js["users"][0], user1, false, false)
282 get api_users_path, :params => { :users => user2.id, :format => "json" }
283 assert_response :success
284 assert_equal "application/json", response.media_type
285 js = ActiveSupport::JSON.decode(@response.body)
287 assert_equal 1, js["users"].count
288 check_json_details(js["users"][0], user2, false, false)
290 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }
291 assert_response :success
292 assert_equal "application/json", response.media_type
293 js = ActiveSupport::JSON.decode(@response.body)
295 assert_equal 2, js["users"].count
296 check_json_details(js["users"][0], user1, false, false)
297 check_json_details(js["users"][1], user3, false, false)
299 get api_users_path, :params => { :users => create(:user, :suspended).id }
300 assert_response :success
301 assert_equal "application/xml", response.media_type
302 assert_select "user", :count => 0
304 get api_users_path, :params => { :users => create(:user, :deleted).id }
305 assert_response :success
306 assert_equal "application/xml", response.media_type
307 assert_select "user", :count => 0
309 get api_users_path, :params => { :users => 0 }
310 assert_response :success
311 assert_equal "application/xml", response.media_type
312 assert_select "user", :count => 0
315 def test_index_oauth2
316 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
317 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
318 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
319 good_auth = bearer_authorization_header(user1, :scopes => %w[read_prefs])
320 bad_auth = bearer_authorization_header(user1, :scopes => %w[])
322 get api_users_path, :params => { :users => user1.id }, :headers => good_auth
323 assert_response :success
324 assert_equal "application/xml", response.media_type
325 assert_select "user", :count => 1 do
326 check_xml_details(user1, true, false)
327 assert_select "user[id='#{user2.id}']", :count => 0
328 assert_select "user[id='#{user3.id}']", :count => 0
331 get api_users_path, :params => { :users => user2.id }, :headers => good_auth
332 assert_response :success
333 assert_equal "application/xml", response.media_type
334 assert_select "user", :count => 1 do
335 assert_select "user[id='#{user1.id}']", :count => 0
336 check_xml_details(user2, false, false)
337 assert_select "user[id='#{user3.id}']", :count => 0
340 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => good_auth
341 assert_response :success
342 assert_equal "application/xml", response.media_type
343 assert_select "user", :count => 2 do
344 check_xml_details(user1, true, false)
345 assert_select "user[id='#{user2.id}']", :count => 0
346 check_xml_details(user3, false, false)
349 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bad_auth
350 assert_response :success
351 assert_equal "application/xml", response.media_type
352 assert_select "user", :count => 2 do
353 check_xml_details(user1, false, 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, :format => "json" }, :headers => good_auth
359 assert_response :success
360 assert_equal "application/json", response.media_type
361 js = ActiveSupport::JSON.decode(@response.body)
363 assert_equal 1, js["users"].count
364 check_json_details(js["users"][0], user1, true, false)
366 get api_users_path, :params => { :users => user2.id, :format => "json" }, :headers => good_auth
367 assert_response :success
368 assert_equal "application/json", response.media_type
369 js = ActiveSupport::JSON.decode(@response.body)
371 assert_equal 1, js["users"].count
372 check_json_details(js["users"][0], user2, false, false)
374 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => good_auth
375 assert_response :success
376 assert_equal "application/json", response.media_type
377 js = ActiveSupport::JSON.decode(@response.body)
379 assert_equal 2, js["users"].count
380 check_json_details(js["users"][0], user1, true, false)
381 check_json_details(js["users"][1], user3, false, false)
383 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bad_auth
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, false, false)
390 check_json_details(js["users"][1], user3, false, false)
392 get api_users_path, :params => { :users => create(:user, :suspended).id }, :headers => good_auth
393 assert_response :success
394 assert_equal "application/xml", response.media_type
395 assert_select "user", :count => 0
397 get api_users_path, :params => { :users => create(:user, :deleted).id }, :headers => good_auth
398 assert_response :success
399 assert_equal "application/xml", response.media_type
400 assert_select "user", :count => 0
402 get api_users_path, :params => { :users => 0 }, :headers => good_auth
403 assert_response :success
404 assert_equal "application/xml", response.media_type
405 assert_select "user", :count => 0
410 trace1 = create(:trace, :user => user) do |trace|
411 create(:tracetag, :trace => trace, :tag => "London")
413 trace2 = create(:trace, :user => user) do |trace|
414 create(:tracetag, :trace => trace, :tag => "Birmingham")
416 # check that nothing is returned when not logged in
417 get user_gpx_files_path
418 assert_response :unauthorized
420 # check that we get a response when logged in
421 auth_header = bearer_authorization_header user
422 get user_gpx_files_path, :headers => auth_header
423 assert_response :success
424 assert_equal "application/xml", response.media_type
426 # check the data that is returned
427 assert_select "gpx_file[id='#{trace1.id}']", 1 do
428 assert_select "tag", "London"
430 assert_select "gpx_file[id='#{trace2.id}']", 1 do
431 assert_select "tag", "Birmingham"
437 def check_xml_details(user, include_private, include_email)
438 assert_select "user[id='#{user.id}']", :count => 1 do
439 assert_select "description", :count => 1, :text => user.description
441 assert_select "contributor-terms", :count => 1 do
442 if user.terms_agreed.present?
443 assert_select "[agreed='true']", :count => 1
445 assert_select "[agreed='false']", :count => 1
449 assert_select "[pd='false']", :count => 1
451 assert_select "[pd]", :count => 0
455 assert_select "img", :count => 0
457 assert_select "roles", :count => 1 do
458 assert_select "role", :count => 0
461 assert_select "changesets", :count => 1 do
462 assert_select "[count='0']", :count => 1
465 assert_select "traces", :count => 1 do
466 assert_select "[count='0']", :count => 1
469 assert_select "blocks", :count => 1 do
470 assert_select "received", :count => 1 do
471 assert_select "[count='0'][active='0']", :count => 1
474 assert_select "issued", :count => 0
477 if include_private && user.home_lat.present? && user.home_lon.present?
478 assert_select "home", :count => 1 do
479 assert_select "[lat='12.1'][lon='23.4'][zoom='3']", :count => 1
482 assert_select "home", :count => 0
486 assert_select "languages", :count => 1 do
487 assert_select "lang", :count => user.languages.count
489 user.languages.each do |language|
490 assert_select "lang", :count => 1, :text => language
494 assert_select "messages", :count => 1 do
495 assert_select "received", :count => 1 do
496 assert_select "[count='#{user.messages.count}'][unread='0']", :count => 1
499 assert_select "sent", :count => 1 do
500 assert_select "[count='#{user.sent_messages.count}']", :count => 1
504 assert_select "languages", :count => 0
505 assert_select "messages", :count => 0
509 assert_select "email", :count => 1, :text => user.email
511 assert_select "email", :count => 0
516 def check_json_details(js, user, include_private, include_email)
517 assert_equal user.id, js["user"]["id"]
518 assert_equal user.description, js["user"]["description"]
519 assert_operator js["user"]["contributor_terms"], :[], "agreed"
522 assert_not js["user"]["contributor_terms"]["pd"]
524 assert_nil js["user"]["contributor_terms"]["pd"]
527 assert_nil js["user"]["img"]
528 assert_empty js["user"]["roles"]
529 assert_equal 0, js["user"]["changesets"]["count"]
530 assert_equal 0, js["user"]["traces"]["count"]
531 assert_equal 0, js["user"]["blocks"]["received"]["count"]
532 assert_equal 0, js["user"]["blocks"]["received"]["active"]
533 assert_nil js["user"]["blocks"]["issued"]
535 if include_private && user.home_lat.present? && user.home_lon.present?
536 assert_in_delta 12.1, js["user"]["home"]["lat"]
537 assert_in_delta 23.4, js["user"]["home"]["lon"]
538 assert_equal 3, js["user"]["home"]["zoom"]
540 assert_nil js["user"]["home"]
543 if include_private && user.languages.present?
544 assert_equal user.languages, js["user"]["languages"]
546 assert_nil js["user"]["languages"]
550 assert_equal user.messages.count, js["user"]["messages"]["received"]["count"]
551 assert_equal 0, js["user"]["messages"]["received"]["unread"]
552 assert_equal user.sent_messages.count, js["user"]["messages"]["sent"]["count"]
554 assert_nil js["user"]["messages"]
558 assert_equal user.email, js["user"]["email"]
560 assert_nil js["user"]["email"]