]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/users_controller_test.rb
Add frozen_string_literal comments to ruby files
[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"])
42
43       # check that a visible user is returned properly
44       get api_user_path(:id => user.id)
45       assert_response :success
46       assert_equal "application/xml", response.media_type
47
48       # check the data that is returned
49       check_xml_details(user, false, false)
50
51       # check that a suspended user is not returned
52       get api_user_path(:id => create(:user, :suspended).id)
53       assert_response :gone
54
55       # check that a deleted user is not returned
56       get api_user_path(:id => create(:user, :deleted).id)
57       assert_response :gone
58
59       # check that a non-existent user is not returned
60       get api_user_path(:id => 0)
61       assert_response :not_found
62
63       # check that a visible user is returned properly in json
64       get api_user_path(:id => user.id, :format => "json")
65       assert_response :success
66       assert_equal "application/json", response.media_type
67
68       # parse the response
69       js = ActiveSupport::JSON.decode(@response.body)
70       assert_not_nil js
71
72       # check the data that is returned
73       check_json_details(js, user, false, false)
74     end
75
76     def test_show_oauth2
77       user = create(:user,
78                     :home_lat => 12.1, :home_lon => 23.4,
79                     :languages => ["en"])
80       good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
81       bad_auth = bearer_authorization_header(user, :scopes => %w[])
82       other_user = create(:user,
83                           :home_lat => 12.1, :home_lon => 23.4,
84                           :languages => ["en"])
85
86       # check that we can fetch our own details as XML with read_prefs
87       get api_user_path(:id => user.id), :headers => good_auth
88       assert_response :success
89       assert_equal "application/xml", response.media_type
90
91       # check the data that is returned
92       check_xml_details(user, true, false)
93
94       # check that we can fetch a different user's details as XML with read_prefs
95       get api_user_path(:id => other_user.id), :headers => good_auth
96       assert_response :success
97       assert_equal "application/xml", response.media_type
98
99       # check the data that is returned
100       check_xml_details(other_user, false, false)
101
102       # check that we can fetch our own details as XML without read_prefs
103       get api_user_path(:id => user.id), :headers => bad_auth
104       assert_response :success
105       assert_equal "application/xml", response.media_type
106
107       # check the data that is returned
108       check_xml_details(user, false, false)
109
110       # check that we can fetch our own details as JSON with read_prefs
111       get api_user_path(:id => user.id, :format => "json"), :headers => good_auth
112       assert_response :success
113       assert_equal "application/json", response.media_type
114
115       # parse the response
116       js = ActiveSupport::JSON.decode(@response.body)
117       assert_not_nil js
118
119       # check the data that is returned
120       check_json_details(js, user, true, false)
121
122       # check that we can fetch a different user's details as JSON with read_prefs
123       get api_user_path(:id => other_user.id, :format => "json"), :headers => good_auth
124       assert_response :success
125       assert_equal "application/json", response.media_type
126
127       # parse the response
128       js = ActiveSupport::JSON.decode(@response.body)
129       assert_not_nil js
130
131       # check the data that is returned
132       check_json_details(js, other_user, false, false)
133
134       # check that we can fetch our own details as JSON without read_prefs
135       get api_user_path(:id => user.id, :format => "json"), :headers => bad_auth
136       assert_response :success
137       assert_equal "application/json", response.media_type
138
139       # parse the response
140       js = ActiveSupport::JSON.decode(@response.body)
141       assert_not_nil js
142
143       # check the data that is returned
144       check_json_details(js, user, false, false)
145     end
146
147     def test_details
148       user = create(:user,
149                     :description => "test",
150                     :terms_agreed => Date.yesterday,
151                     :home_lat => 12.1, :home_lon => 23.4,
152                     :languages => ["en"])
153       create(:message, :read, :recipient => user)
154       create(:message, :sender => user)
155
156       # check that nothing is returned when not logged in
157       get api_user_details_path
158       assert_response :unauthorized
159
160       # check that we get a response when logged in
161       auth_header = bearer_authorization_header user
162       get api_user_details_path, :headers => auth_header
163       assert_response :success
164       assert_equal "application/xml", response.media_type
165
166       # check the data that is returned
167       check_xml_details(user, true, false)
168
169       # check that data is returned properly in json
170       auth_header = bearer_authorization_header user
171       get api_user_details_path(:format => "json"), :headers => auth_header
172       assert_response :success
173       assert_equal "application/json", response.media_type
174
175       # parse the response
176       js = ActiveSupport::JSON.decode(@response.body)
177       assert_not_nil js
178
179       # check the data that is returned
180       check_json_details(js, user, true, false)
181     end
182
183     def test_details_oauth2
184       user = create(:user,
185                     :home_lat => 12.1, :home_lon => 23.4,
186                     :languages => ["en"])
187       good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
188       bad_auth = bearer_authorization_header(user, :scopes => %w[])
189       email_auth = bearer_authorization_header(user, :scopes => %w[read_prefs read_email])
190
191       # check that we can't fetch details as XML without read_prefs
192       get api_user_details_path, :headers => bad_auth
193       assert_response :forbidden
194
195       # check that we can fetch details as XML without read_email
196       get api_user_details_path, :headers => good_auth
197       assert_response :success
198       assert_equal "application/xml", response.media_type
199
200       # check the data that is returned
201       check_xml_details(user, true, false)
202
203       # check that we can fetch details as XML with read_email
204       get api_user_details_path, :headers => email_auth
205       assert_response :success
206       assert_equal "application/xml", response.media_type
207
208       # check the data that is returned
209       check_xml_details(user, true, true)
210
211       # check that we can't fetch details as JSON without read_prefs
212       get api_user_details_path(:format => "json"), :headers => bad_auth
213       assert_response :forbidden
214
215       # check that we can fetch details as JSON without read_email
216       get api_user_details_path(:format => "json"), :headers => good_auth
217       assert_response :success
218       assert_equal "application/json", response.media_type
219
220       # parse the response
221       js = ActiveSupport::JSON.decode(@response.body)
222       assert_not_nil js
223
224       # check the data that is returned
225       check_json_details(js, user, true, false)
226
227       # check that we can fetch details as JSON with read_email
228       get api_user_details_path(:format => "json"), :headers => email_auth
229       assert_response :success
230       assert_equal "application/json", response.media_type
231
232       # parse the response
233       js = ActiveSupport::JSON.decode(@response.body)
234       assert_not_nil js
235
236       # check the data that is returned
237       check_json_details(js, user, true, true)
238     end
239
240     def test_index
241       user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
242       user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
243       user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
244
245       get api_users_path, :params => { :users => user1.id }
246       assert_response :success
247       assert_equal "application/xml", response.media_type
248       assert_select "user", :count => 1 do
249         check_xml_details(user1, false, false)
250         assert_select "user[id='#{user2.id}']", :count => 0
251         assert_select "user[id='#{user3.id}']", :count => 0
252       end
253
254       get api_users_path, :params => { :users => user2.id }
255       assert_response :success
256       assert_equal "application/xml", response.media_type
257       assert_select "user", :count => 1 do
258         assert_select "user[id='#{user1.id}']", :count => 0
259         check_xml_details(user2, false, false)
260         assert_select "user[id='#{user3.id}']", :count => 0
261       end
262
263       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }
264       assert_response :success
265       assert_equal "application/xml", response.media_type
266       assert_select "user", :count => 2 do
267         check_xml_details(user1, false, false)
268         assert_select "user[id='#{user2.id}']", :count => 0
269         check_xml_details(user3, false, false)
270       end
271
272       get api_users_path, :params => { :users => user1.id, :format => "json" }
273       assert_response :success
274       assert_equal "application/json", response.media_type
275       js = ActiveSupport::JSON.decode(@response.body)
276       assert_not_nil js
277       assert_equal 1, js["users"].count
278       check_json_details(js["users"][0], user1, false, false)
279
280       get api_users_path, :params => { :users => user2.id, :format => "json" }
281       assert_response :success
282       assert_equal "application/json", response.media_type
283       js = ActiveSupport::JSON.decode(@response.body)
284       assert_not_nil js
285       assert_equal 1, js["users"].count
286       check_json_details(js["users"][0], user2, false, false)
287
288       get api_users_path, :params => { :users => "#{user1.id},#{user3.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 2, js["users"].count
294       check_json_details(js["users"][0], user1, false, false)
295       check_json_details(js["users"][1], user3, false, false)
296
297       get api_users_path, :params => { :users => create(:user, :suspended).id }
298       assert_response :success
299       assert_equal "application/xml", response.media_type
300       assert_select "user", :count => 0
301
302       get api_users_path, :params => { :users => create(:user, :deleted).id }
303       assert_response :success
304       assert_equal "application/xml", response.media_type
305       assert_select "user", :count => 0
306
307       get api_users_path, :params => { :users => 0 }
308       assert_response :success
309       assert_equal "application/xml", response.media_type
310       assert_select "user", :count => 0
311     end
312
313     def test_index_oauth2
314       user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
315       user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
316       user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
317       good_auth = bearer_authorization_header(user1, :scopes => %w[read_prefs])
318       bad_auth = bearer_authorization_header(user1, :scopes => %w[])
319
320       get api_users_path, :params => { :users => user1.id }, :headers => good_auth
321       assert_response :success
322       assert_equal "application/xml", response.media_type
323       assert_select "user", :count => 1 do
324         check_xml_details(user1, true, false)
325         assert_select "user[id='#{user2.id}']", :count => 0
326         assert_select "user[id='#{user3.id}']", :count => 0
327       end
328
329       get api_users_path, :params => { :users => user2.id }, :headers => good_auth
330       assert_response :success
331       assert_equal "application/xml", response.media_type
332       assert_select "user", :count => 1 do
333         assert_select "user[id='#{user1.id}']", :count => 0
334         check_xml_details(user2, false, false)
335         assert_select "user[id='#{user3.id}']", :count => 0
336       end
337
338       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => good_auth
339       assert_response :success
340       assert_equal "application/xml", response.media_type
341       assert_select "user", :count => 2 do
342         check_xml_details(user1, true, false)
343         assert_select "user[id='#{user2.id}']", :count => 0
344         check_xml_details(user3, false, false)
345       end
346
347       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bad_auth
348       assert_response :success
349       assert_equal "application/xml", response.media_type
350       assert_select "user", :count => 2 do
351         check_xml_details(user1, false, false)
352         assert_select "user[id='#{user2.id}']", :count => 0
353         check_xml_details(user3, false, false)
354       end
355
356       get api_users_path, :params => { :users => user1.id, :format => "json" }, :headers => good_auth
357       assert_response :success
358       assert_equal "application/json", response.media_type
359       js = ActiveSupport::JSON.decode(@response.body)
360       assert_not_nil js
361       assert_equal 1, js["users"].count
362       check_json_details(js["users"][0], user1, true, false)
363
364       get api_users_path, :params => { :users => user2.id, :format => "json" }, :headers => good_auth
365       assert_response :success
366       assert_equal "application/json", response.media_type
367       js = ActiveSupport::JSON.decode(@response.body)
368       assert_not_nil js
369       assert_equal 1, js["users"].count
370       check_json_details(js["users"][0], user2, false, false)
371
372       get api_users_path, :params => { :users => "#{user1.id},#{user3.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 2, js["users"].count
378       check_json_details(js["users"][0], user1, true, false)
379       check_json_details(js["users"][1], user3, false, false)
380
381       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bad_auth
382       assert_response :success
383       assert_equal "application/json", response.media_type
384       js = ActiveSupport::JSON.decode(@response.body)
385       assert_not_nil js
386       assert_equal 2, js["users"].count
387       check_json_details(js["users"][0], user1, false, false)
388       check_json_details(js["users"][1], user3, false, false)
389
390       get api_users_path, :params => { :users => create(:user, :suspended).id }, :headers => good_auth
391       assert_response :success
392       assert_equal "application/xml", response.media_type
393       assert_select "user", :count => 0
394
395       get api_users_path, :params => { :users => create(:user, :deleted).id }, :headers => good_auth
396       assert_response :success
397       assert_equal "application/xml", response.media_type
398       assert_select "user", :count => 0
399
400       get api_users_path, :params => { :users => 0 }, :headers => good_auth
401       assert_response :success
402       assert_equal "application/xml", response.media_type
403       assert_select "user", :count => 0
404     end
405
406     private
407
408     def check_xml_details(user, include_private, include_email)
409       assert_select "user[id='#{user.id}']", :count => 1 do
410         assert_select "description", :count => 1, :text => user.description
411
412         assert_select "contributor-terms", :count => 1 do
413           if user.terms_agreed.present?
414             assert_select "[agreed='true']", :count => 1
415           else
416             assert_select "[agreed='false']", :count => 1
417           end
418
419           if include_private
420             assert_select "[pd='false']", :count => 1
421           else
422             assert_select "[pd]", :count => 0
423           end
424         end
425
426         assert_select "img", :count => 0
427
428         assert_select "roles", :count => 1 do
429           assert_select "role", :count => 0
430         end
431
432         assert_select "changesets", :count => 1 do
433           assert_select "[count='0']", :count => 1
434         end
435
436         assert_select "traces", :count => 1 do
437           assert_select "[count='0']", :count => 1
438         end
439
440         assert_select "blocks", :count => 1 do
441           assert_select "received", :count => 1 do
442             assert_select "[count='0'][active='0']", :count => 1
443           end
444
445           assert_select "issued", :count => 0
446         end
447
448         if include_private && user.home_lat.present? && user.home_lon.present?
449           assert_select "home", :count => 1 do
450             assert_select "[lat='12.1'][lon='23.4'][zoom='3']", :count => 1
451           end
452         else
453           assert_select "home", :count => 0
454         end
455
456         if include_private
457           assert_select "languages", :count => 1 do
458             assert_select "lang", :count => user.languages.count
459
460             user.languages.each do |language|
461               assert_select "lang", :count => 1, :text => language
462             end
463           end
464
465           assert_select "messages", :count => 1 do
466             assert_select "received", :count => 1 do
467               assert_select "[count='#{user.messages.count}'][unread='0']", :count => 1
468             end
469
470             assert_select "sent", :count => 1 do
471               assert_select "[count='#{user.sent_messages.count}']", :count => 1
472             end
473           end
474         else
475           assert_select "languages", :count => 0
476           assert_select "messages", :count => 0
477         end
478
479         if include_email
480           assert_select "email", :count => 1, :text => user.email
481         else
482           assert_select "email", :count => 0
483         end
484       end
485     end
486
487     def check_json_details(js, user, include_private, include_email)
488       assert_equal user.id, js["user"]["id"]
489       assert_equal user.description, js["user"]["description"]
490       assert_operator js["user"]["contributor_terms"], :[], "agreed"
491
492       if include_private
493         assert_not js["user"]["contributor_terms"]["pd"]
494       else
495         assert_nil js["user"]["contributor_terms"]["pd"]
496       end
497
498       assert_nil js["user"]["img"]
499       assert_empty js["user"]["roles"]
500       assert_equal 0, js["user"]["changesets"]["count"]
501       assert_equal 0, js["user"]["traces"]["count"]
502       assert_equal 0, js["user"]["blocks"]["received"]["count"]
503       assert_equal 0, js["user"]["blocks"]["received"]["active"]
504       assert_nil js["user"]["blocks"]["issued"]
505
506       if include_private && user.home_lat.present? && user.home_lon.present?
507         assert_in_delta 12.1, js["user"]["home"]["lat"]
508         assert_in_delta 23.4, js["user"]["home"]["lon"]
509         assert_equal 3, js["user"]["home"]["zoom"]
510       else
511         assert_nil js["user"]["home"]
512       end
513
514       if include_private && user.languages.present?
515         assert_equal user.languages, js["user"]["languages"]
516       else
517         assert_nil js["user"]["languages"]
518       end
519
520       if include_private
521         assert_equal user.messages.count, js["user"]["messages"]["received"]["count"]
522         assert_equal 0, js["user"]["messages"]["received"]["unread"]
523         assert_equal user.sent_messages.count, js["user"]["messages"]["sent"]["count"]
524       else
525         assert_nil js["user"]["messages"]
526       end
527
528       if include_email
529         assert_equal user.email, js["user"]["email"]
530       else
531         assert_nil js["user"]["email"]
532       end
533     end
534   end
535 end