]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/users_controller_test.rb
Merge remote-tracking branch 'upstream/pull/7633'
[rails.git] / test / controllers / api / users_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 module Api
6   class UsersControllerTest < ActionDispatch::IntegrationTest
7     ##
8     # test all routes which lead to this controller
9     def test_routes
10       assert_routing(
11         { :path => "/api/0.6/user/1", :method => :get },
12         { :controller => "api/users", :action => "show", :id => "1" }
13       )
14       assert_routing(
15         { :path => "/api/0.6/user/1.json", :method => :get },
16         { :controller => "api/users", :action => "show", :id => "1", :format => "json" }
17       )
18       assert_routing(
19         { :path => "/api/0.6/user/details", :method => :get },
20         { :controller => "api/users", :action => "details" }
21       )
22       assert_routing(
23         { :path => "/api/0.6/user/details.json", :method => :get },
24         { :controller => "api/users", :action => "details", :format => "json" }
25       )
26       assert_routing(
27         { :path => "/api/0.6/users", :method => :get },
28         { :controller => "api/users", :action => "index" }
29       )
30       assert_routing(
31         { :path => "/api/0.6/users.json", :method => :get },
32         { :controller => "api/users", :action => "index", :format => "json" }
33       )
34     end
35
36     def test_show
37       user = create(:user,
38                     :description => "test",
39                     :terms_agreed => Date.yesterday,
40                     :home_lat => 12.1, :home_lon => 23.4,
41                     :languages => ["en"]) do |user|
42                       create(:social_link, :user => user, :url => "https://twitter.com/testuser")
43                       create(:social_link, :user => user, :url => "https://github.com/testuser")
44                     end
45
46       # check that a visible user is returned properly
47       get api_user_path(:id => user.id)
48       assert_response :success
49       assert_equal "application/xml", response.media_type
50
51       # check the data that is returned
52       check_xml_details(user, false, false)
53
54       # check that a suspended user is not returned
55       get api_user_path(:id => create(:user, :suspended).id)
56       assert_response :gone
57
58       # check that a deleted user is not returned
59       get api_user_path(:id => create(:user, :deleted).id)
60       assert_response :gone
61
62       # check that a non-existent user is not returned
63       get api_user_path(:id => 0)
64       assert_response :not_found
65
66       # check that a visible user is returned properly in json
67       get api_user_path(:id => user.id, :format => "json")
68       assert_response :success
69       assert_equal "application/json", response.media_type
70
71       # parse the response
72       js = ActiveSupport::JSON.decode(@response.body)
73       assert_not_nil js
74
75       # check the data that is returned
76       check_json_details(js, user, false, false)
77     end
78
79     def test_show_oauth2
80       user = create(:user,
81                     :home_lat => 12.1, :home_lon => 23.4,
82                     :languages => ["en"])
83       good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
84       bad_auth = bearer_authorization_header(user, :scopes => %w[])
85       other_user = create(:user,
86                           :home_lat => 12.1, :home_lon => 23.4,
87                           :languages => ["en"])
88
89       # check that we can fetch our own details as XML with read_prefs
90       get api_user_path(:id => user.id), :headers => good_auth
91       assert_response :success
92       assert_equal "application/xml", response.media_type
93
94       # check the data that is returned
95       check_xml_details(user, true, false)
96
97       # check that we can fetch a different user's details as XML with read_prefs
98       get api_user_path(:id => other_user.id), :headers => good_auth
99       assert_response :success
100       assert_equal "application/xml", response.media_type
101
102       # check the data that is returned
103       check_xml_details(other_user, false, false)
104
105       # check that we can fetch our own details as XML without read_prefs
106       get api_user_path(:id => user.id), :headers => bad_auth
107       assert_response :success
108       assert_equal "application/xml", response.media_type
109
110       # check the data that is returned
111       check_xml_details(user, false, false)
112
113       # check that we can fetch our own details as JSON with read_prefs
114       get api_user_path(:id => user.id, :format => "json"), :headers => good_auth
115       assert_response :success
116       assert_equal "application/json", response.media_type
117
118       # parse the response
119       js = ActiveSupport::JSON.decode(@response.body)
120       assert_not_nil js
121
122       # check the data that is returned
123       check_json_details(js, user, true, false)
124
125       # check that we can fetch a different user's details as JSON with read_prefs
126       get api_user_path(:id => other_user.id, :format => "json"), :headers => good_auth
127       assert_response :success
128       assert_equal "application/json", response.media_type
129
130       # parse the response
131       js = ActiveSupport::JSON.decode(@response.body)
132       assert_not_nil js
133
134       # check the data that is returned
135       check_json_details(js, other_user, false, false)
136
137       # check that we can fetch our own details as JSON without read_prefs
138       get api_user_path(:id => user.id, :format => "json"), :headers => bad_auth
139       assert_response :success
140       assert_equal "application/json", response.media_type
141
142       # parse the response
143       js = ActiveSupport::JSON.decode(@response.body)
144       assert_not_nil js
145
146       # check the data that is returned
147       check_json_details(js, user, false, false)
148     end
149
150     def test_details
151       user = create(:user,
152                     :description => "test",
153                     :terms_agreed => Date.yesterday,
154                     :home_lat => 12.1, :home_lon => 23.4,
155                     :languages => ["en"])
156       create(:message, :read, :recipient => user)
157       create(:message, :sender => user)
158
159       # check that nothing is returned when not logged in
160       get api_user_details_path
161       assert_response :unauthorized
162
163       # check that we get a response when logged in
164       auth_header = bearer_authorization_header user
165       get api_user_details_path, :headers => auth_header
166       assert_response :success
167       assert_equal "application/xml", response.media_type
168
169       # check the data that is returned
170       check_xml_details(user, true, false)
171
172       # check that data is returned properly in json
173       auth_header = bearer_authorization_header user
174       get api_user_details_path(:format => "json"), :headers => auth_header
175       assert_response :success
176       assert_equal "application/json", response.media_type
177
178       # parse the response
179       js = ActiveSupport::JSON.decode(@response.body)
180       assert_not_nil js
181
182       # check the data that is returned
183       check_json_details(js, user, true, false)
184     end
185
186     ##
187     # an unexpected error while authenticating should be raised as it
188     # is, and not hidden by an error about authorization not being checked
189     def test_details_unexpected_error
190       auth_header = bearer_authorization_header
191
192       User.stub(:find, ->(*) { raise ActiveRecord::ConnectionNotEstablished, "database unavailable" }) do
193         assert_raises ActiveRecord::ConnectionNotEstablished do
194           get api_user_details_path, :headers => auth_header
195         end
196       end
197     end
198
199     def test_details_oauth2
200       user = create(:user,
201                     :home_lat => 12.1, :home_lon => 23.4,
202                     :languages => ["en"])
203       good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
204       bad_auth = bearer_authorization_header(user, :scopes => %w[])
205       email_auth = bearer_authorization_header(user, :scopes => %w[read_prefs read_email])
206
207       # check that we can't fetch details as XML without read_prefs
208       get api_user_details_path, :headers => bad_auth
209       assert_response :forbidden
210
211       # check that we can fetch details as XML without read_email
212       get api_user_details_path, :headers => good_auth
213       assert_response :success
214       assert_equal "application/xml", response.media_type
215
216       # check the data that is returned
217       check_xml_details(user, true, false)
218
219       # check that we can fetch details as XML with read_email
220       get api_user_details_path, :headers => email_auth
221       assert_response :success
222       assert_equal "application/xml", response.media_type
223
224       # check the data that is returned
225       check_xml_details(user, true, true)
226
227       # check that we can't fetch details as JSON without read_prefs
228       get api_user_details_path(:format => "json"), :headers => bad_auth
229       assert_response :forbidden
230
231       # check that we can fetch details as JSON without read_email
232       get api_user_details_path(:format => "json"), :headers => good_auth
233       assert_response :success
234       assert_equal "application/json", response.media_type
235
236       # parse the response
237       js = ActiveSupport::JSON.decode(@response.body)
238       assert_not_nil js
239
240       # check the data that is returned
241       check_json_details(js, user, true, false)
242
243       # check that we can fetch details as JSON with read_email
244       get api_user_details_path(:format => "json"), :headers => email_auth
245       assert_response :success
246       assert_equal "application/json", response.media_type
247
248       # parse the response
249       js = ActiveSupport::JSON.decode(@response.body)
250       assert_not_nil js
251
252       # check the data that is returned
253       check_json_details(js, user, true, true)
254     end
255
256     def test_index
257       user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
258       user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
259       user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
260
261       get api_users_path, :params => { :users => user1.id }
262       assert_response :success
263       assert_equal "application/xml", response.media_type
264       assert_select "user", :count => 1 do
265         check_xml_details(user1, false, false)
266         assert_select "user[id='#{user2.id}']", :count => 0
267         assert_select "user[id='#{user3.id}']", :count => 0
268       end
269
270       get api_users_path, :params => { :users => user2.id }
271       assert_response :success
272       assert_equal "application/xml", response.media_type
273       assert_select "user", :count => 1 do
274         assert_select "user[id='#{user1.id}']", :count => 0
275         check_xml_details(user2, false, false)
276         assert_select "user[id='#{user3.id}']", :count => 0
277       end
278
279       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }
280       assert_response :success
281       assert_equal "application/xml", response.media_type
282       assert_select "user", :count => 2 do
283         check_xml_details(user1, false, false)
284         assert_select "user[id='#{user2.id}']", :count => 0
285         check_xml_details(user3, false, false)
286       end
287
288       get api_users_path, :params => { :users => user1.id, :format => "json" }
289       assert_response :success
290       assert_equal "application/json", response.media_type
291       js = ActiveSupport::JSON.decode(@response.body)
292       assert_not_nil js
293       assert_equal 1, js["users"].count
294       check_json_details(js["users"][0], user1, false, false)
295
296       get api_users_path, :params => { :users => user2.id, :format => "json" }
297       assert_response :success
298       assert_equal "application/json", response.media_type
299       js = ActiveSupport::JSON.decode(@response.body)
300       assert_not_nil js
301       assert_equal 1, js["users"].count
302       check_json_details(js["users"][0], user2, false, false)
303
304       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }
305       assert_response :success
306       assert_equal "application/json", response.media_type
307       js = ActiveSupport::JSON.decode(@response.body)
308       assert_not_nil js
309       assert_equal 2, js["users"].count
310       check_json_details(js["users"][0], user1, false, false)
311       check_json_details(js["users"][1], user3, false, false)
312
313       get api_users_path, :params => { :users => create(:user, :suspended).id }
314       assert_response :success
315       assert_equal "application/xml", response.media_type
316       assert_select "user", :count => 0
317
318       get api_users_path, :params => { :users => create(:user, :deleted).id }
319       assert_response :success
320       assert_equal "application/xml", response.media_type
321       assert_select "user", :count => 0
322
323       get api_users_path, :params => { :users => 0 }
324       assert_response :success
325       assert_equal "application/xml", response.media_type
326       assert_select "user", :count => 0
327     end
328
329     def test_index_oauth2
330       user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
331       user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
332       user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
333       good_auth = bearer_authorization_header(user1, :scopes => %w[read_prefs])
334       bad_auth = bearer_authorization_header(user1, :scopes => %w[])
335
336       get api_users_path, :params => { :users => user1.id }, :headers => good_auth
337       assert_response :success
338       assert_equal "application/xml", response.media_type
339       assert_select "user", :count => 1 do
340         check_xml_details(user1, true, false)
341         assert_select "user[id='#{user2.id}']", :count => 0
342         assert_select "user[id='#{user3.id}']", :count => 0
343       end
344
345       get api_users_path, :params => { :users => user2.id }, :headers => good_auth
346       assert_response :success
347       assert_equal "application/xml", response.media_type
348       assert_select "user", :count => 1 do
349         assert_select "user[id='#{user1.id}']", :count => 0
350         check_xml_details(user2, false, false)
351         assert_select "user[id='#{user3.id}']", :count => 0
352       end
353
354       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => good_auth
355       assert_response :success
356       assert_equal "application/xml", response.media_type
357       assert_select "user", :count => 2 do
358         check_xml_details(user1, true, false)
359         assert_select "user[id='#{user2.id}']", :count => 0
360         check_xml_details(user3, false, false)
361       end
362
363       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bad_auth
364       assert_response :success
365       assert_equal "application/xml", response.media_type
366       assert_select "user", :count => 2 do
367         check_xml_details(user1, false, false)
368         assert_select "user[id='#{user2.id}']", :count => 0
369         check_xml_details(user3, false, false)
370       end
371
372       get api_users_path, :params => { :users => user1.id, :format => "json" }, :headers => good_auth
373       assert_response :success
374       assert_equal "application/json", response.media_type
375       js = ActiveSupport::JSON.decode(@response.body)
376       assert_not_nil js
377       assert_equal 1, js["users"].count
378       check_json_details(js["users"][0], user1, true, false)
379
380       get api_users_path, :params => { :users => user2.id, :format => "json" }, :headers => good_auth
381       assert_response :success
382       assert_equal "application/json", response.media_type
383       js = ActiveSupport::JSON.decode(@response.body)
384       assert_not_nil js
385       assert_equal 1, js["users"].count
386       check_json_details(js["users"][0], user2, false, false)
387
388       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => good_auth
389       assert_response :success
390       assert_equal "application/json", response.media_type
391       js = ActiveSupport::JSON.decode(@response.body)
392       assert_not_nil js
393       assert_equal 2, js["users"].count
394       check_json_details(js["users"][0], user1, true, false)
395       check_json_details(js["users"][1], user3, false, false)
396
397       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bad_auth
398       assert_response :success
399       assert_equal "application/json", response.media_type
400       js = ActiveSupport::JSON.decode(@response.body)
401       assert_not_nil js
402       assert_equal 2, js["users"].count
403       check_json_details(js["users"][0], user1, false, false)
404       check_json_details(js["users"][1], user3, false, false)
405
406       get api_users_path, :params => { :users => create(:user, :suspended).id }, :headers => good_auth
407       assert_response :success
408       assert_equal "application/xml", response.media_type
409       assert_select "user", :count => 0
410
411       get api_users_path, :params => { :users => create(:user, :deleted).id }, :headers => good_auth
412       assert_response :success
413       assert_equal "application/xml", response.media_type
414       assert_select "user", :count => 0
415
416       get api_users_path, :params => { :users => 0 }, :headers => good_auth
417       assert_response :success
418       assert_equal "application/xml", response.media_type
419       assert_select "user", :count => 0
420     end
421
422     private
423
424     def check_xml_details(user, include_private, include_email)
425       assert_select "user[id='#{user.id}']", :count => 1 do
426         assert_select "description", :count => 1, :text => user.description
427         assert_select "company", :count => 1, :text => user.company if user.company
428
429         if user.social_links.present?
430           assert_select "social-links link", :count => user.social_links.count do
431             user.social_links.each do |link|
432               details = link.parsed
433               assert_select "link[platform='#{details[:platform]}']", :count => 1, :text => details[:url]
434             end
435           end
436         else
437           assert_select "social-links link", :count => 0
438         end
439
440         assert_select "contributor-terms", :count => 1 do
441           if user.terms_agreed.present?
442             assert_select "[agreed='true']", :count => 1
443           else
444             assert_select "[agreed='false']", :count => 1
445           end
446
447           if include_private
448             assert_select "[pd='false']", :count => 1
449           else
450             assert_select "[pd]", :count => 0
451           end
452         end
453
454         assert_select "img", :count => 0
455
456         assert_select "roles", :count => 1 do
457           assert_select "role", :count => 0
458         end
459
460         assert_select "changesets", :count => 1 do
461           assert_select "[count='0']", :count => 1
462         end
463
464         assert_select "traces", :count => 1 do
465           assert_select "[count='0']", :count => 1
466         end
467
468         assert_select "blocks", :count => 1 do
469           assert_select "received", :count => 1 do
470             assert_select "[count='0'][active='0']", :count => 1
471           end
472
473           assert_select "issued", :count => 0
474         end
475
476         if include_private && user.home_lat.present? && user.home_lon.present?
477           assert_select "home", :count => 1 do
478             assert_select "[lat='12.1'][lon='23.4'][zoom='3']", :count => 1
479           end
480         else
481           assert_select "home", :count => 0
482         end
483
484         if include_private
485           assert_select "languages", :count => 1 do
486             assert_select "lang", :count => user.languages.count
487
488             user.languages.each do |language|
489               assert_select "lang", :count => 1, :text => language
490             end
491           end
492
493           assert_select "messages", :count => 1 do
494             assert_select "received", :count => 1 do
495               assert_select "[count='#{user.messages.count}'][unread='0']", :count => 1
496             end
497
498             assert_select "sent", :count => 1 do
499               assert_select "[count='#{user.sent_messages.count}']", :count => 1
500             end
501           end
502         else
503           assert_select "languages", :count => 0
504           assert_select "messages", :count => 0
505         end
506
507         if include_email
508           assert_select "email", :count => 1, :text => user.email
509         else
510           assert_select "email", :count => 0
511         end
512       end
513     end
514
515     def check_json_details(js, user, include_private, include_email)
516       assert_equal user.id, js["user"]["id"]
517       assert_equal user.description, js["user"]["description"]
518       if user.company
519         assert_equal user.company, js["user"]["company"]
520       else
521         assert_nil js["user"]["company"]
522       end
523
524       if user.social_links.present?
525         ordered_js_links = js.dig("user", "social_links").sort_by { |link| link["url"] }
526         ordered_user_links = user.social_links.sort_by(&:url).map(&:parsed)
527
528         assert_equal ordered_user_links.count, ordered_js_links.count
529
530         ordered_user_links.zip(ordered_js_links).each do |user_link, js_link|
531           assert_equal user_link[:url], js_link["url"]
532           assert_equal user_link[:platform], js_link["platform"]
533         end
534       else
535         assert_empty js["user"]["social_links"]
536       end
537
538       assert_operator js["user"]["contributor_terms"], :[], "agreed"
539
540       if include_private
541         assert_not js["user"]["contributor_terms"]["pd"]
542       else
543         assert_nil js["user"]["contributor_terms"]["pd"]
544       end
545
546       assert_nil js["user"]["img"]
547       assert_empty js["user"]["roles"]
548       assert_equal 0, js["user"]["changesets"]["count"]
549       assert_equal 0, js["user"]["traces"]["count"]
550       assert_equal 0, js["user"]["blocks"]["received"]["count"]
551       assert_equal 0, js["user"]["blocks"]["received"]["active"]
552       assert_nil js["user"]["blocks"]["issued"]
553
554       if include_private && user.home_lat.present? && user.home_lon.present?
555         assert_in_delta 12.1, js["user"]["home"]["lat"]
556         assert_in_delta 23.4, js["user"]["home"]["lon"]
557         assert_equal 3, js["user"]["home"]["zoom"]
558       else
559         assert_nil js["user"]["home"]
560       end
561
562       if include_private && user.languages.present?
563         assert_equal user.languages, js["user"]["languages"]
564       else
565         assert_nil js["user"]["languages"]
566       end
567
568       if include_private
569         assert_equal user.messages.count, js["user"]["messages"]["received"]["count"]
570         assert_equal 0, js["user"]["messages"]["received"]["unread"]
571         assert_equal user.sent_messages.count, js["user"]["messages"]["sent"]["count"]
572       else
573         assert_nil js["user"]["messages"]
574       end
575
576       if include_email
577         assert_equal user.email, js["user"]["email"]
578       else
579         assert_nil js["user"]["email"]
580       end
581     end
582   end
583 end