]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/users_controller_test.rb
Check that user email address are only returned with read_email
[rails.git] / test / controllers / api / users_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class UsersControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/user/1", :method => :get },
10         { :controller => "api/users", :action => "show", :id => "1" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/user/1.json", :method => :get },
14         { :controller => "api/users", :action => "show", :id => "1", :format => "json" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/user/details", :method => :get },
18         { :controller => "api/users", :action => "details" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/user/details.json", :method => :get },
22         { :controller => "api/users", :action => "details", :format => "json" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/user/gpx_files", :method => :get },
26         { :controller => "api/users", :action => "gpx_files" }
27       )
28       assert_routing(
29         { :path => "/api/0.6/users", :method => :get },
30         { :controller => "api/users", :action => "index" }
31       )
32       assert_routing(
33         { :path => "/api/0.6/users.json", :method => :get },
34         { :controller => "api/users", :action => "index", :format => "json" }
35       )
36     end
37
38     def test_show
39       user = create(:user,
40                     :description => "test",
41                     :terms_agreed => Date.yesterday,
42                     :home_lat => 12.1, :home_lon => 23.4,
43                     :languages => ["en"])
44
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
49
50       # check the data that is returned
51       check_xml_details(user, false, false)
52
53       # check that a suspended user is not returned
54       get api_user_path(:id => create(:user, :suspended).id)
55       assert_response :gone
56
57       # check that a deleted user is not returned
58       get api_user_path(:id => create(:user, :deleted).id)
59       assert_response :gone
60
61       # check that a non-existent user is not returned
62       get api_user_path(:id => 0)
63       assert_response :not_found
64
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
69
70       # parse the response
71       js = ActiveSupport::JSON.decode(@response.body)
72       assert_not_nil js
73
74       # check the data that is returned
75       check_json_details(js, user, false, false)
76     end
77
78     def test_show_oauth1
79       user = create(:user,
80                     :home_lat => 12.1, :home_lon => 23.4,
81                     :languages => ["en"])
82       good_token = create(:access_token,
83                           :user => user,
84                           :allow_read_prefs => true)
85       bad_token = create(:access_token,
86                          :user => user)
87       other_user = create(:user,
88                           :home_lat => 12.1, :home_lon => 23.4,
89                           :languages => ["en"])
90
91       # check that we can fetch our own details as XML with read_prefs
92       signed_get api_user_path(:id => user.id), :oauth => { :token => good_token }
93       assert_response :success
94       assert_equal "application/xml", response.media_type
95
96       # check the data that is returned
97       check_xml_details(user, true, false)
98
99       # check that we can fetch a different user's details as XML with read_prefs
100       signed_get api_user_path(:id => other_user.id), :oauth => { :token => good_token }
101       assert_response :success
102       assert_equal "application/xml", response.media_type
103
104       # check the data that is returned
105       check_xml_details(other_user, false, false)
106
107       # check that we can fetch our own details as XML without read_prefs
108       signed_get api_user_path(:id => user.id), :oauth => { :token => bad_token }
109       assert_response :success
110       assert_equal "application/xml", response.media_type
111
112       # check the data that is returned
113       check_xml_details(user, false, false)
114
115       # check that we can fetch our own details as JSON with read_prefs
116       signed_get api_user_path(:id => user.id, :format => "json"), :oauth => { :token => good_token }
117       assert_response :success
118       assert_equal "application/json", response.media_type
119
120       # parse the response
121       js = ActiveSupport::JSON.decode(@response.body)
122       assert_not_nil js
123
124       # check the data that is returned
125       check_json_details(js, user, true, false)
126
127       # check that we can fetch a different user's details as JSON with read_prefs
128       signed_get api_user_path(:id => other_user.id, :format => "json"), :oauth => { :token => good_token }
129       assert_response :success
130       assert_equal "application/json", response.media_type
131
132       # parse the response
133       js = ActiveSupport::JSON.decode(@response.body)
134       assert_not_nil js
135
136       # check the data that is returned
137       check_json_details(js, other_user, false, false)
138
139       # check that we can fetch our own details as JSON without read_prefs
140       signed_get api_user_path(:id => other_user.id, :format => "json"), :oauth => { :token => bad_token }
141       assert_response :success
142       assert_equal "application/json", response.media_type
143
144       # parse the response
145       js = ActiveSupport::JSON.decode(@response.body)
146       assert_not_nil js
147
148       # check the data that is returned
149       check_json_details(js, other_user, false, false)
150     end
151
152     def test_show_oauth2
153       user = create(:user,
154                     :home_lat => 12.1, :home_lon => 23.4,
155                     :languages => ["en"])
156       good_token = create(:oauth_access_token,
157                           :resource_owner_id => user.id,
158                           :scopes => %w[read_prefs])
159       bad_token = create(:oauth_access_token,
160                          :resource_owner_id => user.id,
161                          :scopes => %w[])
162       other_user = create(:user,
163                           :home_lat => 12.1, :home_lon => 23.4,
164                           :languages => ["en"])
165
166       # check that we can fetch our own details as XML with read_prefs
167       get api_user_path(:id => user.id), :headers => bearer_authorization_header(good_token.token)
168       assert_response :success
169       assert_equal "application/xml", response.media_type
170
171       # check the data that is returned
172       check_xml_details(user, true, false)
173
174       # check that we can fetch a different user's details as XML with read_prefs
175       get api_user_path(:id => other_user.id), :headers => bearer_authorization_header(good_token.token)
176       assert_response :success
177       assert_equal "application/xml", response.media_type
178
179       # check the data that is returned
180       check_xml_details(other_user, false, false)
181
182       # check that we can fetch our own details as XML without read_prefs
183       get api_user_path(:id => user.id), :headers => bearer_authorization_header(bad_token.token)
184       assert_response :success
185       assert_equal "application/xml", response.media_type
186
187       # check the data that is returned
188       check_xml_details(user, false, false)
189
190       # check that we can fetch our own details as JSON with read_prefs
191       get api_user_path(:id => user.id, :format => "json"), :headers => bearer_authorization_header(good_token.token)
192       assert_response :success
193       assert_equal "application/json", response.media_type
194
195       # parse the response
196       js = ActiveSupport::JSON.decode(@response.body)
197       assert_not_nil js
198
199       # check the data that is returned
200       check_json_details(js, user, true, false)
201
202       # check that we can fetch a different user's details as JSON with read_prefs
203       get api_user_path(:id => other_user.id, :format => "json"), :headers => bearer_authorization_header(good_token.token)
204       assert_response :success
205       assert_equal "application/json", response.media_type
206
207       # parse the response
208       js = ActiveSupport::JSON.decode(@response.body)
209       assert_not_nil js
210
211       # check the data that is returned
212       check_json_details(js, other_user, false, false)
213
214       # check that we can fetch our own details as JSON without read_prefs
215       get api_user_path(:id => user.id, :format => "json"), :headers => bearer_authorization_header(bad_token.token)
216       assert_response :success
217       assert_equal "application/json", response.media_type
218
219       # parse the response
220       js = ActiveSupport::JSON.decode(@response.body)
221       assert_not_nil js
222
223       # check the data that is returned
224       check_json_details(js, user, false, false)
225     end
226
227     def test_details
228       user = create(:user,
229                     :description => "test",
230                     :terms_agreed => Date.yesterday,
231                     :home_lat => 12.1, :home_lon => 23.4,
232                     :languages => ["en"])
233       create(:message, :read, :recipient => user)
234       create(:message, :sender => user)
235
236       # check that nothing is returned when not logged in
237       get user_details_path
238       assert_response :unauthorized
239
240       # check that we get a response when logged in
241       auth_header = basic_authorization_header user.email, "test"
242       get user_details_path, :headers => auth_header
243       assert_response :success
244       assert_equal "application/xml", response.media_type
245
246       # check the data that is returned
247       check_xml_details(user, true, false)
248
249       # check that data is returned properly in json
250       auth_header = basic_authorization_header user.email, "test"
251       get user_details_path(:format => "json"), :headers => auth_header
252       assert_response :success
253       assert_equal "application/json", response.media_type
254
255       # parse the response
256       js = ActiveSupport::JSON.decode(@response.body)
257       assert_not_nil js
258
259       # check the data that is returned
260       check_json_details(js, user, true, false)
261     end
262
263     def test_details_oauth1
264       user = create(:user,
265                     :home_lat => 12.1, :home_lon => 23.4,
266                     :languages => ["en"])
267       good_token = create(:access_token,
268                           :user => user,
269                           :allow_read_prefs => true)
270       bad_token = create(:access_token,
271                          :user => user)
272
273       # check that we can't fetch details as XML without read_prefs
274       signed_get user_details_path, :oauth => { :token => bad_token }
275       assert_response :forbidden
276
277       # check that we can fetch details as XML
278       signed_get user_details_path, :oauth => { :token => good_token }
279       assert_response :success
280       assert_equal "application/xml", response.media_type
281
282       # check the data that is returned
283       check_xml_details(user, true, false)
284
285       # check that we can't fetch details as JSON without read_prefs
286       signed_get user_details_path(:format => "json"), :oauth => { :token => bad_token }
287       assert_response :forbidden
288
289       # check that we can fetch details as JSON
290       signed_get user_details_path(:format => "json"), :oauth => { :token => good_token }
291       assert_response :success
292       assert_equal "application/json", response.media_type
293
294       # parse the response
295       js = ActiveSupport::JSON.decode(@response.body)
296       assert_not_nil js
297
298       # check the data that is returned
299       check_json_details(js, user, true, false)
300     end
301
302     def test_details_oauth2
303       user = create(:user,
304                     :home_lat => 12.1, :home_lon => 23.4,
305                     :languages => ["en"])
306       good_token = create(:oauth_access_token,
307                           :resource_owner_id => user.id,
308                           :scopes => %w[read_prefs])
309       bad_token = create(:oauth_access_token,
310                          :resource_owner_id => user.id)
311       email_token = create(:oauth_access_token,
312                            :resource_owner_id => user.id,
313                            :scopes => %w[read_prefs read_email])
314
315       # check that we can't fetch details as XML without read_prefs
316       get user_details_path, :headers => bearer_authorization_header(bad_token.token)
317       assert_response :forbidden
318
319       # check that we can fetch details as XML without read_email
320       get user_details_path, :headers => bearer_authorization_header(good_token.token)
321       assert_response :success
322       assert_equal "application/xml", response.media_type
323
324       # check the data that is returned
325       check_xml_details(user, true, false)
326
327       # check that we can fetch details as XML with read_email
328       get user_details_path, :headers => bearer_authorization_header(email_token.token)
329       assert_response :success
330       assert_equal "application/xml", response.media_type
331
332       # check the data that is returned
333       check_xml_details(user, true, true)
334
335       # check that we can't fetch details as JSON without read_prefs
336       get user_details_path(:format => "json"), :headers => bearer_authorization_header(bad_token.token)
337       assert_response :forbidden
338
339       # check that we can fetch details as JSON without read_email
340       get user_details_path(:format => "json"), :headers => bearer_authorization_header(good_token.token)
341       assert_response :success
342       assert_equal "application/json", response.media_type
343
344       # parse the response
345       js = ActiveSupport::JSON.decode(@response.body)
346       assert_not_nil js
347
348       # check the data that is returned
349       check_json_details(js, user, true, false)
350
351       # check that we can fetch details as JSON with read_email
352       get user_details_path(:format => "json"), :headers => bearer_authorization_header(email_token.token)
353       assert_response :success
354       assert_equal "application/json", response.media_type
355
356       # parse the response
357       js = ActiveSupport::JSON.decode(@response.body)
358       assert_not_nil js
359
360       # check the data that is returned
361       check_json_details(js, user, true, true)
362     end
363
364     def test_index
365       user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
366       user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
367       user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
368
369       get api_users_path, :params => { :users => user1.id }
370       assert_response :success
371       assert_equal "application/xml", response.media_type
372       assert_select "user", :count => 1 do
373         check_xml_details(user1, false, false)
374         assert_select "user[id='#{user2.id}']", :count => 0
375         assert_select "user[id='#{user3.id}']", :count => 0
376       end
377
378       get api_users_path, :params => { :users => user2.id }
379       assert_response :success
380       assert_equal "application/xml", response.media_type
381       assert_select "user", :count => 1 do
382         assert_select "user[id='#{user1.id}']", :count => 0
383         check_xml_details(user2, false, false)
384         assert_select "user[id='#{user3.id}']", :count => 0
385       end
386
387       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }
388       assert_response :success
389       assert_equal "application/xml", response.media_type
390       assert_select "user", :count => 2 do
391         check_xml_details(user1, false, false)
392         assert_select "user[id='#{user2.id}']", :count => 0
393         check_xml_details(user3, false, false)
394       end
395
396       get api_users_path, :params => { :users => user1.id, :format => "json" }
397       assert_response :success
398       assert_equal "application/json", response.media_type
399       js = ActiveSupport::JSON.decode(@response.body)
400       assert_not_nil js
401       assert_equal 1, js["users"].count
402       check_json_details(js["users"][0], user1, false, false)
403
404       get api_users_path, :params => { :users => user2.id, :format => "json" }
405       assert_response :success
406       assert_equal "application/json", response.media_type
407       js = ActiveSupport::JSON.decode(@response.body)
408       assert_not_nil js
409       assert_equal 1, js["users"].count
410       check_json_details(js["users"][0], user2, false, false)
411
412       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }
413       assert_response :success
414       assert_equal "application/json", response.media_type
415       js = ActiveSupport::JSON.decode(@response.body)
416       assert_not_nil js
417       assert_equal 2, js["users"].count
418       check_json_details(js["users"][0], user1, false, false)
419       check_json_details(js["users"][1], user3, false, false)
420
421       get api_users_path, :params => { :users => create(:user, :suspended).id }
422       assert_response :not_found
423
424       get api_users_path, :params => { :users => create(:user, :deleted).id }
425       assert_response :not_found
426
427       get api_users_path, :params => { :users => 0 }
428       assert_response :not_found
429     end
430
431     def test_index_oauth1
432       user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
433       user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
434       user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
435       good_token = create(:access_token, :user => user1, :allow_read_prefs => true)
436       bad_token = create(:access_token, :user => user1)
437
438       signed_get api_users_path, :params => { :users => user1.id }, :oauth => { :token => good_token }
439       assert_response :success
440       assert_equal "application/xml", response.media_type
441       assert_select "user", :count => 1 do
442         check_xml_details(user1, true, false)
443         assert_select "user[id='#{user2.id}']", :count => 0
444         assert_select "user[id='#{user3.id}']", :count => 0
445       end
446
447       signed_get api_users_path, :params => { :users => user2.id }, :oauth => { :token => good_token }
448       assert_response :success
449       assert_equal "application/xml", response.media_type
450       assert_select "user", :count => 1 do
451         assert_select "user[id='#{user1.id}']", :count => 0
452         check_xml_details(user2, false, false)
453         assert_select "user[id='#{user3.id}']", :count => 0
454       end
455
456       signed_get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :oauth => { :token => good_token }
457       assert_response :success
458       assert_equal "application/xml", response.media_type
459       assert_select "user", :count => 2 do
460         check_xml_details(user1, true, false)
461         assert_select "user[id='#{user2.id}']", :count => 0
462         check_xml_details(user3, false, false)
463       end
464
465       signed_get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :oauth => { :token => bad_token }
466       assert_response :success
467       assert_equal "application/xml", response.media_type
468       assert_select "user", :count => 2 do
469         check_xml_details(user1, false, false)
470         assert_select "user[id='#{user2.id}']", :count => 0
471         check_xml_details(user3, false, false)
472       end
473
474       signed_get api_users_path, :params => { :users => user1.id, :format => "json" }, :oauth => { :token => good_token }
475       assert_response :success
476       assert_equal "application/json", response.media_type
477       js = ActiveSupport::JSON.decode(@response.body)
478       assert_not_nil js
479       assert_equal 1, js["users"].count
480       check_json_details(js["users"][0], user1, true, false)
481
482       signed_get api_users_path, :params => { :users => user2.id, :format => "json" }, :oauth => { :token => good_token }
483       assert_response :success
484       assert_equal "application/json", response.media_type
485       js = ActiveSupport::JSON.decode(@response.body)
486       assert_not_nil js
487       assert_equal 1, js["users"].count
488       check_json_details(js["users"][0], user2, false, false)
489
490       signed_get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :oauth => { :token => good_token }
491       assert_response :success
492       assert_equal "application/json", response.media_type
493       js = ActiveSupport::JSON.decode(@response.body)
494       assert_not_nil js
495       assert_equal 2, js["users"].count
496       check_json_details(js["users"][0], user1, true, false)
497       check_json_details(js["users"][1], user3, false, false)
498
499       signed_get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :oauth => { :token => bad_token }
500       assert_response :success
501       assert_equal "application/json", response.media_type
502       js = ActiveSupport::JSON.decode(@response.body)
503       assert_not_nil js
504       assert_equal 2, js["users"].count
505       check_json_details(js["users"][0], user1, false, false)
506       check_json_details(js["users"][1], user3, false, false)
507
508       signed_get api_users_path, :params => { :users => create(:user, :suspended).id }, :oauth => { :token => good_token }
509       assert_response :not_found
510
511       signed_get api_users_path, :params => { :users => create(:user, :deleted).id }, :oauth => { :token => good_token }
512       assert_response :not_found
513
514       signed_get api_users_path, :params => { :users => 0 }, :oauth => { :token => good_token }
515       assert_response :not_found
516     end
517
518     def test_index_oauth2
519       user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
520       user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
521       user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
522       good_token = create(:oauth_access_token, :resource_owner_id => user1.id, :scopes => %w[read_prefs])
523       bad_token = create(:oauth_access_token, :resource_owner_id => user1.id, :scopes => %w[])
524
525       get api_users_path, :params => { :users => user1.id }, :headers => bearer_authorization_header(good_token.token)
526       assert_response :success
527       assert_equal "application/xml", response.media_type
528       assert_select "user", :count => 1 do
529         check_xml_details(user1, true, false)
530         assert_select "user[id='#{user2.id}']", :count => 0
531         assert_select "user[id='#{user3.id}']", :count => 0
532       end
533
534       get api_users_path, :params => { :users => user2.id }, :headers => bearer_authorization_header(good_token.token)
535       assert_response :success
536       assert_equal "application/xml", response.media_type
537       assert_select "user", :count => 1 do
538         assert_select "user[id='#{user1.id}']", :count => 0
539         check_xml_details(user2, false, false)
540         assert_select "user[id='#{user3.id}']", :count => 0
541       end
542
543       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bearer_authorization_header(good_token.token)
544       assert_response :success
545       assert_equal "application/xml", response.media_type
546       assert_select "user", :count => 2 do
547         check_xml_details(user1, true, false)
548         assert_select "user[id='#{user2.id}']", :count => 0
549         check_xml_details(user3, false, false)
550       end
551
552       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bearer_authorization_header(bad_token.token)
553       assert_response :success
554       assert_equal "application/xml", response.media_type
555       assert_select "user", :count => 2 do
556         check_xml_details(user1, false, false)
557         assert_select "user[id='#{user2.id}']", :count => 0
558         check_xml_details(user3, false, false)
559       end
560
561       get api_users_path, :params => { :users => user1.id, :format => "json" }, :headers => bearer_authorization_header(good_token.token)
562       assert_response :success
563       assert_equal "application/json", response.media_type
564       js = ActiveSupport::JSON.decode(@response.body)
565       assert_not_nil js
566       assert_equal 1, js["users"].count
567       check_json_details(js["users"][0], user1, true, false)
568
569       get api_users_path, :params => { :users => user2.id, :format => "json" }, :headers => bearer_authorization_header(good_token.token)
570       assert_response :success
571       assert_equal "application/json", response.media_type
572       js = ActiveSupport::JSON.decode(@response.body)
573       assert_not_nil js
574       assert_equal 1, js["users"].count
575       check_json_details(js["users"][0], user2, false, false)
576
577       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bearer_authorization_header(good_token.token)
578       assert_response :success
579       assert_equal "application/json", response.media_type
580       js = ActiveSupport::JSON.decode(@response.body)
581       assert_not_nil js
582       assert_equal 2, js["users"].count
583       check_json_details(js["users"][0], user1, true, false)
584       check_json_details(js["users"][1], user3, false, false)
585
586       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bearer_authorization_header(bad_token.token)
587       assert_response :success
588       assert_equal "application/json", response.media_type
589       js = ActiveSupport::JSON.decode(@response.body)
590       assert_not_nil js
591       assert_equal 2, js["users"].count
592       check_json_details(js["users"][0], user1, false, false)
593       check_json_details(js["users"][1], user3, false, false)
594
595       get api_users_path, :params => { :users => create(:user, :suspended).id }, :headers => bearer_authorization_header(good_token.token)
596       assert_response :not_found
597
598       get api_users_path, :params => { :users => create(:user, :deleted).id }, :headers => bearer_authorization_header(good_token.token)
599       assert_response :not_found
600
601       get api_users_path, :params => { :users => 0 }, :headers => bearer_authorization_header(good_token.token)
602       assert_response :not_found
603     end
604
605     def test_gpx_files
606       user = create(:user)
607       trace1 = create(:trace, :user => user) do |trace|
608         create(:tracetag, :trace => trace, :tag => "London")
609       end
610       trace2 = create(:trace, :user => user) do |trace|
611         create(:tracetag, :trace => trace, :tag => "Birmingham")
612       end
613       # check that nothing is returned when not logged in
614       get user_gpx_files_path
615       assert_response :unauthorized
616
617       # check that we get a response when logged in
618       auth_header = basic_authorization_header user.email, "test"
619       get user_gpx_files_path, :headers => auth_header
620       assert_response :success
621       assert_equal "application/xml", response.media_type
622
623       # check the data that is returned
624       assert_select "gpx_file[id='#{trace1.id}']", 1 do
625         assert_select "tag", "London"
626       end
627       assert_select "gpx_file[id='#{trace2.id}']", 1 do
628         assert_select "tag", "Birmingham"
629       end
630     end
631
632     private
633
634     def check_xml_details(user, include_private, include_email)
635       assert_select "user[id='#{user.id}']", :count => 1 do
636         assert_select "description", :count => 1, :text => user.description
637
638         assert_select "contributor-terms", :count => 1 do
639           if user.terms_agreed.present?
640             assert_select "[agreed='true']", :count => 1
641           else
642             assert_select "[agreed='false']", :count => 1
643           end
644
645           if include_private
646             assert_select "[pd='false']", :count => 1
647           else
648             assert_select "[pd]", :count => 0
649           end
650         end
651
652         assert_select "img", :count => 0
653
654         assert_select "roles", :count => 1 do
655           assert_select "role", :count => 0
656         end
657
658         assert_select "changesets", :count => 1 do
659           assert_select "[count='0']", :count => 1
660         end
661
662         assert_select "traces", :count => 1 do
663           assert_select "[count='0']", :count => 1
664         end
665
666         assert_select "blocks", :count => 1 do
667           assert_select "received", :count => 1 do
668             assert_select "[count='0'][active='0']", :count => 1
669           end
670
671           assert_select "issued", :count => 0
672         end
673
674         if include_private && user.home_lat.present? && user.home_lon.present?
675           assert_select "home", :count => 1 do
676             assert_select "[lat='12.1'][lon='23.4'][zoom='3']", :count => 1
677           end
678         else
679           assert_select "home", :count => 0
680         end
681
682         if include_private
683           assert_select "languages", :count => 1 do
684             assert_select "lang", :count => user.languages.count
685
686             user.languages.each do |language|
687               assert_select "lang", :count => 1, :text => language
688             end
689           end
690
691           assert_select "messages", :count => 1 do
692             assert_select "received", :count => 1 do
693               assert_select "[count='#{user.messages.count}'][unread='0']", :count => 1
694             end
695
696             assert_select "sent", :count => 1 do
697               assert_select "[count='#{user.sent_messages.count}']", :count => 1
698             end
699           end
700         else
701           assert_select "languages", :count => 0
702           assert_select "messages", :count => 0
703         end
704
705         if include_email
706           assert_select "email", :count => 1, :text => user.email
707         else
708           assert_select "email", :count => 0
709         end
710       end
711     end
712
713     def check_json_details(js, user, include_private, include_email)
714       assert_equal user.id, js["user"]["id"]
715       assert_equal user.description, js["user"]["description"]
716       assert js["user"]["contributor_terms"]["agreed"]
717
718       if include_private
719         assert_not js["user"]["contributor_terms"]["pd"]
720       else
721         assert_nil js["user"]["contributor_terms"]["pd"]
722       end
723
724       assert_nil js["user"]["img"]
725       assert_empty js["user"]["roles"]
726       assert_equal 0, js["user"]["changesets"]["count"]
727       assert_equal 0, js["user"]["traces"]["count"]
728       assert_equal 0, js["user"]["blocks"]["received"]["count"]
729       assert_equal 0, js["user"]["blocks"]["received"]["active"]
730       assert_nil js["user"]["blocks"]["issued"]
731
732       if include_private && user.home_lat.present? && user.home_lon.present?
733         assert_in_delta 12.1, js["user"]["home"]["lat"]
734         assert_in_delta 23.4, js["user"]["home"]["lon"]
735         assert_equal 3, js["user"]["home"]["zoom"]
736       else
737         assert_nil js["user"]["home"]
738       end
739
740       if include_private && user.languages.present?
741         assert_equal user.languages, js["user"]["languages"]
742       else
743         assert_nil js["user"]["languages"]
744       end
745
746       if include_private
747         assert_equal user.messages.count, js["user"]["messages"]["received"]["count"]
748         assert_equal 0, js["user"]["messages"]["received"]["unread"]
749         assert_equal user.sent_messages.count, js["user"]["messages"]["sent"]["count"]
750       else
751         assert_nil js["user"]["messages"]
752       end
753
754       if include_email
755         assert_equal user.email, js["user"]["email"]
756       else
757         assert_nil js["user"]["email"]
758       end
759     end
760   end
761 end