]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/users_controller_test.rb
Fix new rubocop warnings
[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 :success
423       assert_equal "application/xml", response.media_type
424       assert_select "user", :count => 0
425
426       get api_users_path, :params => { :users => create(:user, :deleted).id }
427       assert_response :success
428       assert_equal "application/xml", response.media_type
429       assert_select "user", :count => 0
430
431       get api_users_path, :params => { :users => 0 }
432       assert_response :success
433       assert_equal "application/xml", response.media_type
434       assert_select "user", :count => 0
435     end
436
437     def test_index_oauth1
438       user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
439       user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
440       user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
441       good_token = create(:access_token, :user => user1, :allow_read_prefs => true)
442       bad_token = create(:access_token, :user => user1)
443
444       signed_get api_users_path, :params => { :users => user1.id }, :oauth => { :token => good_token }
445       assert_response :success
446       assert_equal "application/xml", response.media_type
447       assert_select "user", :count => 1 do
448         check_xml_details(user1, true, false)
449         assert_select "user[id='#{user2.id}']", :count => 0
450         assert_select "user[id='#{user3.id}']", :count => 0
451       end
452
453       signed_get api_users_path, :params => { :users => user2.id }, :oauth => { :token => good_token }
454       assert_response :success
455       assert_equal "application/xml", response.media_type
456       assert_select "user", :count => 1 do
457         assert_select "user[id='#{user1.id}']", :count => 0
458         check_xml_details(user2, false, false)
459         assert_select "user[id='#{user3.id}']", :count => 0
460       end
461
462       signed_get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :oauth => { :token => good_token }
463       assert_response :success
464       assert_equal "application/xml", response.media_type
465       assert_select "user", :count => 2 do
466         check_xml_details(user1, true, false)
467         assert_select "user[id='#{user2.id}']", :count => 0
468         check_xml_details(user3, false, false)
469       end
470
471       signed_get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :oauth => { :token => bad_token }
472       assert_response :success
473       assert_equal "application/xml", response.media_type
474       assert_select "user", :count => 2 do
475         check_xml_details(user1, false, false)
476         assert_select "user[id='#{user2.id}']", :count => 0
477         check_xml_details(user3, false, false)
478       end
479
480       signed_get api_users_path, :params => { :users => user1.id, :format => "json" }, :oauth => { :token => good_token }
481       assert_response :success
482       assert_equal "application/json", response.media_type
483       js = ActiveSupport::JSON.decode(@response.body)
484       assert_not_nil js
485       assert_equal 1, js["users"].count
486       check_json_details(js["users"][0], user1, true, false)
487
488       signed_get api_users_path, :params => { :users => user2.id, :format => "json" }, :oauth => { :token => good_token }
489       assert_response :success
490       assert_equal "application/json", response.media_type
491       js = ActiveSupport::JSON.decode(@response.body)
492       assert_not_nil js
493       assert_equal 1, js["users"].count
494       check_json_details(js["users"][0], user2, false, false)
495
496       signed_get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :oauth => { :token => good_token }
497       assert_response :success
498       assert_equal "application/json", response.media_type
499       js = ActiveSupport::JSON.decode(@response.body)
500       assert_not_nil js
501       assert_equal 2, js["users"].count
502       check_json_details(js["users"][0], user1, true, false)
503       check_json_details(js["users"][1], user3, false, false)
504
505       signed_get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :oauth => { :token => bad_token }
506       assert_response :success
507       assert_equal "application/json", response.media_type
508       js = ActiveSupport::JSON.decode(@response.body)
509       assert_not_nil js
510       assert_equal 2, js["users"].count
511       check_json_details(js["users"][0], user1, false, false)
512       check_json_details(js["users"][1], user3, false, false)
513
514       signed_get api_users_path, :params => { :users => create(:user, :suspended).id }, :oauth => { :token => good_token }
515       assert_response :success
516       assert_equal "application/xml", response.media_type
517       assert_select "user", :count => 0
518
519       signed_get api_users_path, :params => { :users => create(:user, :deleted).id }, :oauth => { :token => good_token }
520       assert_response :success
521       assert_equal "application/xml", response.media_type
522       assert_select "user", :count => 0
523
524       signed_get api_users_path, :params => { :users => 0 }, :oauth => { :token => good_token }
525       assert_response :success
526       assert_equal "application/xml", response.media_type
527       assert_select "user", :count => 0
528     end
529
530     def test_index_oauth2
531       user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
532       user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
533       user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
534       good_token = create(:oauth_access_token, :resource_owner_id => user1.id, :scopes => %w[read_prefs])
535       bad_token = create(:oauth_access_token, :resource_owner_id => user1.id, :scopes => %w[])
536
537       get api_users_path, :params => { :users => user1.id }, :headers => bearer_authorization_header(good_token.token)
538       assert_response :success
539       assert_equal "application/xml", response.media_type
540       assert_select "user", :count => 1 do
541         check_xml_details(user1, true, false)
542         assert_select "user[id='#{user2.id}']", :count => 0
543         assert_select "user[id='#{user3.id}']", :count => 0
544       end
545
546       get api_users_path, :params => { :users => user2.id }, :headers => bearer_authorization_header(good_token.token)
547       assert_response :success
548       assert_equal "application/xml", response.media_type
549       assert_select "user", :count => 1 do
550         assert_select "user[id='#{user1.id}']", :count => 0
551         check_xml_details(user2, false, false)
552         assert_select "user[id='#{user3.id}']", :count => 0
553       end
554
555       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bearer_authorization_header(good_token.token)
556       assert_response :success
557       assert_equal "application/xml", response.media_type
558       assert_select "user", :count => 2 do
559         check_xml_details(user1, true, false)
560         assert_select "user[id='#{user2.id}']", :count => 0
561         check_xml_details(user3, false, false)
562       end
563
564       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bearer_authorization_header(bad_token.token)
565       assert_response :success
566       assert_equal "application/xml", response.media_type
567       assert_select "user", :count => 2 do
568         check_xml_details(user1, false, false)
569         assert_select "user[id='#{user2.id}']", :count => 0
570         check_xml_details(user3, false, false)
571       end
572
573       get api_users_path, :params => { :users => user1.id, :format => "json" }, :headers => bearer_authorization_header(good_token.token)
574       assert_response :success
575       assert_equal "application/json", response.media_type
576       js = ActiveSupport::JSON.decode(@response.body)
577       assert_not_nil js
578       assert_equal 1, js["users"].count
579       check_json_details(js["users"][0], user1, true, false)
580
581       get api_users_path, :params => { :users => user2.id, :format => "json" }, :headers => bearer_authorization_header(good_token.token)
582       assert_response :success
583       assert_equal "application/json", response.media_type
584       js = ActiveSupport::JSON.decode(@response.body)
585       assert_not_nil js
586       assert_equal 1, js["users"].count
587       check_json_details(js["users"][0], user2, false, false)
588
589       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bearer_authorization_header(good_token.token)
590       assert_response :success
591       assert_equal "application/json", response.media_type
592       js = ActiveSupport::JSON.decode(@response.body)
593       assert_not_nil js
594       assert_equal 2, js["users"].count
595       check_json_details(js["users"][0], user1, true, false)
596       check_json_details(js["users"][1], user3, false, false)
597
598       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bearer_authorization_header(bad_token.token)
599       assert_response :success
600       assert_equal "application/json", response.media_type
601       js = ActiveSupport::JSON.decode(@response.body)
602       assert_not_nil js
603       assert_equal 2, js["users"].count
604       check_json_details(js["users"][0], user1, false, false)
605       check_json_details(js["users"][1], user3, false, false)
606
607       get api_users_path, :params => { :users => create(:user, :suspended).id }, :headers => bearer_authorization_header(good_token.token)
608       assert_response :success
609       assert_equal "application/xml", response.media_type
610       assert_select "user", :count => 0
611
612       get api_users_path, :params => { :users => create(:user, :deleted).id }, :headers => bearer_authorization_header(good_token.token)
613       assert_response :success
614       assert_equal "application/xml", response.media_type
615       assert_select "user", :count => 0
616
617       get api_users_path, :params => { :users => 0 }, :headers => bearer_authorization_header(good_token.token)
618       assert_response :success
619       assert_equal "application/xml", response.media_type
620       assert_select "user", :count => 0
621     end
622
623     def test_gpx_files
624       user = create(:user)
625       trace1 = create(:trace, :user => user) do |trace|
626         create(:tracetag, :trace => trace, :tag => "London")
627       end
628       trace2 = create(:trace, :user => user) do |trace|
629         create(:tracetag, :trace => trace, :tag => "Birmingham")
630       end
631       # check that nothing is returned when not logged in
632       get user_gpx_files_path
633       assert_response :unauthorized
634
635       # check that we get a response when logged in
636       auth_header = basic_authorization_header user.email, "test"
637       get user_gpx_files_path, :headers => auth_header
638       assert_response :success
639       assert_equal "application/xml", response.media_type
640
641       # check the data that is returned
642       assert_select "gpx_file[id='#{trace1.id}']", 1 do
643         assert_select "tag", "London"
644       end
645       assert_select "gpx_file[id='#{trace2.id}']", 1 do
646         assert_select "tag", "Birmingham"
647       end
648     end
649
650     private
651
652     def check_xml_details(user, include_private, include_email)
653       assert_select "user[id='#{user.id}']", :count => 1 do
654         assert_select "description", :count => 1, :text => user.description
655
656         assert_select "contributor-terms", :count => 1 do
657           if user.terms_agreed.present?
658             assert_select "[agreed='true']", :count => 1
659           else
660             assert_select "[agreed='false']", :count => 1
661           end
662
663           if include_private
664             assert_select "[pd='false']", :count => 1
665           else
666             assert_select "[pd]", :count => 0
667           end
668         end
669
670         assert_select "img", :count => 0
671
672         assert_select "roles", :count => 1 do
673           assert_select "role", :count => 0
674         end
675
676         assert_select "changesets", :count => 1 do
677           assert_select "[count='0']", :count => 1
678         end
679
680         assert_select "traces", :count => 1 do
681           assert_select "[count='0']", :count => 1
682         end
683
684         assert_select "blocks", :count => 1 do
685           assert_select "received", :count => 1 do
686             assert_select "[count='0'][active='0']", :count => 1
687           end
688
689           assert_select "issued", :count => 0
690         end
691
692         if include_private && user.home_lat.present? && user.home_lon.present?
693           assert_select "home", :count => 1 do
694             assert_select "[lat='12.1'][lon='23.4'][zoom='3']", :count => 1
695           end
696         else
697           assert_select "home", :count => 0
698         end
699
700         if include_private
701           assert_select "languages", :count => 1 do
702             assert_select "lang", :count => user.languages.count
703
704             user.languages.each do |language|
705               assert_select "lang", :count => 1, :text => language
706             end
707           end
708
709           assert_select "messages", :count => 1 do
710             assert_select "received", :count => 1 do
711               assert_select "[count='#{user.messages.count}'][unread='0']", :count => 1
712             end
713
714             assert_select "sent", :count => 1 do
715               assert_select "[count='#{user.sent_messages.count}']", :count => 1
716             end
717           end
718         else
719           assert_select "languages", :count => 0
720           assert_select "messages", :count => 0
721         end
722
723         if include_email
724           assert_select "email", :count => 1, :text => user.email
725         else
726           assert_select "email", :count => 0
727         end
728       end
729     end
730
731     def check_json_details(js, user, include_private, include_email)
732       assert_equal user.id, js["user"]["id"]
733       assert_equal user.description, js["user"]["description"]
734       assert_operator js["user"]["contributor_terms"], :[], "agreed"
735
736       if include_private
737         assert_not js["user"]["contributor_terms"]["pd"]
738       else
739         assert_nil js["user"]["contributor_terms"]["pd"]
740       end
741
742       assert_nil js["user"]["img"]
743       assert_empty js["user"]["roles"]
744       assert_equal 0, js["user"]["changesets"]["count"]
745       assert_equal 0, js["user"]["traces"]["count"]
746       assert_equal 0, js["user"]["blocks"]["received"]["count"]
747       assert_equal 0, js["user"]["blocks"]["received"]["active"]
748       assert_nil js["user"]["blocks"]["issued"]
749
750       if include_private && user.home_lat.present? && user.home_lon.present?
751         assert_in_delta 12.1, js["user"]["home"]["lat"]
752         assert_in_delta 23.4, js["user"]["home"]["lon"]
753         assert_equal 3, js["user"]["home"]["zoom"]
754       else
755         assert_nil js["user"]["home"]
756       end
757
758       if include_private && user.languages.present?
759         assert_equal user.languages, js["user"]["languages"]
760       else
761         assert_nil js["user"]["languages"]
762       end
763
764       if include_private
765         assert_equal user.messages.count, js["user"]["messages"]["received"]["count"]
766         assert_equal 0, js["user"]["messages"]["received"]["unread"]
767         assert_equal user.sent_messages.count, js["user"]["messages"]["sent"]["count"]
768       else
769         assert_nil js["user"]["messages"]
770       end
771
772       if include_email
773         assert_equal user.email, js["user"]["email"]
774       else
775         assert_nil js["user"]["email"]
776       end
777     end
778   end
779 end